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 <linux/module.h>
19 #include <sound/hdaudio_ext.h>
20 #include <sound/sof.h>
21 #include <sound/sof/xtensa.h>
24 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
25 #include "../../codecs/hdac_hda.h"
28 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
29 #include <sound/soc-acpi-intel-match.h>
32 /* platform specific devices */
39 struct hda_dsp_msg_code {
44 static const struct hda_dsp_msg_code hda_dsp_rom_msg[] = {
45 {HDA_DSP_ROM_FW_MANIFEST_LOADED, "status: manifest loaded"},
46 {HDA_DSP_ROM_FW_FW_LOADED, "status: fw loaded"},
47 {HDA_DSP_ROM_FW_ENTERED, "status: fw entered"},
48 {HDA_DSP_ROM_CSE_ERROR, "error: cse error"},
49 {HDA_DSP_ROM_CSE_WRONG_RESPONSE, "error: cse wrong response"},
50 {HDA_DSP_ROM_IMR_TO_SMALL, "error: IMR too small"},
51 {HDA_DSP_ROM_BASE_FW_NOT_FOUND, "error: base fw not found"},
52 {HDA_DSP_ROM_CSE_VALIDATION_FAILED, "error: signature verification failed"},
53 {HDA_DSP_ROM_IPC_FATAL_ERROR, "error: ipc fatal error"},
54 {HDA_DSP_ROM_L2_CACHE_ERROR, "error: L2 cache error"},
55 {HDA_DSP_ROM_LOAD_OFFSET_TO_SMALL, "error: load offset too small"},
56 {HDA_DSP_ROM_API_PTR_INVALID, "error: API ptr invalid"},
57 {HDA_DSP_ROM_BASEFW_INCOMPAT, "error: base fw incompatible"},
58 {HDA_DSP_ROM_UNHANDLED_INTERRUPT, "error: unhandled interrupt"},
59 {HDA_DSP_ROM_MEMORY_HOLE_ECC, "error: ECC memory hole"},
60 {HDA_DSP_ROM_KERNEL_EXCEPTION, "error: kernel exception"},
61 {HDA_DSP_ROM_USER_EXCEPTION, "error: user exception"},
62 {HDA_DSP_ROM_UNEXPECTED_RESET, "error: unexpected reset"},
63 {HDA_DSP_ROM_NULL_FW_ENTRY, "error: null FW entry point"},
66 static void hda_dsp_get_status_skl(struct snd_sof_dev *sdev)
71 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
72 HDA_ADSP_FW_STATUS_SKL);
74 for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
75 if (status == hda_dsp_rom_msg[i].code) {
76 dev_err(sdev->dev, "%s - code %8.8x\n",
77 hda_dsp_rom_msg[i].msg, status);
82 /* not for us, must be generic sof message */
83 dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
86 static void hda_dsp_get_status(struct snd_sof_dev *sdev)
91 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
92 HDA_DSP_SRAM_REG_ROM_STATUS);
94 for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
95 if (status == hda_dsp_rom_msg[i].code) {
96 dev_err(sdev->dev, "%s - code %8.8x\n",
97 hda_dsp_rom_msg[i].msg, status);
102 /* not for us, must be generic sof message */
103 dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
106 static void hda_dsp_get_registers(struct snd_sof_dev *sdev,
107 struct sof_ipc_dsp_oops_xtensa *xoops,
108 struct sof_ipc_panic_info *panic_info,
109 u32 *stack, size_t stack_words)
111 u32 offset = sdev->dsp_oops_offset;
113 /* first read registers */
114 sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
116 /* note: variable AR register array is not read */
118 /* then get panic info */
119 offset += xoops->arch_hdr.totalsize;
120 sof_block_read(sdev, sdev->mmio_bar, offset,
121 panic_info, sizeof(*panic_info));
123 /* then get the stack */
124 offset += sizeof(*panic_info);
125 sof_block_read(sdev, sdev->mmio_bar, offset, stack,
126 stack_words * sizeof(u32));
129 void hda_dsp_dump_skl(struct snd_sof_dev *sdev, u32 flags)
131 struct sof_ipc_dsp_oops_xtensa xoops;
132 struct sof_ipc_panic_info panic_info;
133 u32 stack[HDA_DSP_STACK_DUMP_SIZE];
136 /* try APL specific status message types first */
137 hda_dsp_get_status_skl(sdev);
139 /* now try generic SOF status messages */
140 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
141 HDA_ADSP_ERROR_CODE_SKL);
143 /*TODO: Check: there is no define in spec, but it is used in the code*/
144 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
145 HDA_ADSP_ERROR_CODE_SKL + 0x4);
147 if (sdev->boot_complete) {
148 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
149 HDA_DSP_STACK_DUMP_SIZE);
150 snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
151 stack, HDA_DSP_STACK_DUMP_SIZE);
153 dev_err(sdev->dev, "error: status = 0x%8.8x panic = 0x%8.8x\n",
155 hda_dsp_get_status_skl(sdev);
159 void hda_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
161 struct sof_ipc_dsp_oops_xtensa xoops;
162 struct sof_ipc_panic_info panic_info;
163 u32 stack[HDA_DSP_STACK_DUMP_SIZE];
166 /* try APL specific status message types first */
167 hda_dsp_get_status(sdev);
169 /* now try generic SOF status messages */
170 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
171 HDA_DSP_SRAM_REG_FW_STATUS);
172 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_TRACEP);
174 if (sdev->boot_complete) {
175 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
176 HDA_DSP_STACK_DUMP_SIZE);
177 snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
178 stack, HDA_DSP_STACK_DUMP_SIZE);
180 dev_err(sdev->dev, "error: status = 0x%8.8x panic = 0x%8.8x\n",
182 hda_dsp_get_status(sdev);
186 void hda_ipc_dump(struct snd_sof_dev *sdev)
192 /* read IPC status */
193 hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
194 hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
195 hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL);
197 /* dump the IPC regs */
198 /* TODO: parse the raw msg */
200 "error: host status 0x%8.8x dsp status 0x%8.8x mask 0x%8.8x\n",
201 hipcie, hipct, hipcctl);
204 static int hda_init(struct snd_sof_dev *sdev)
206 struct hda_bus *hbus;
207 struct hdac_bus *bus;
208 struct hdac_ext_bus_ops *ext_ops = NULL;
209 struct pci_dev *pci = to_pci_dev(sdev->dev);
212 hbus = sof_to_hbus(sdev);
213 bus = sof_to_bus(sdev);
216 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
217 ext_ops = snd_soc_hdac_hda_get_ops();
219 sof_hda_bus_init(bus, &pci->dev, ext_ops);
221 bus->bdl_pos_adj = 0;
223 mutex_init(&hbus->prepare_mutex);
225 hbus->mixer_assigned = -1;
226 hbus->modelname = "sofbus";
228 /* initialise hdac bus */
229 bus->addr = pci_resource_start(pci, 0);
230 bus->remap_addr = pci_ioremap_bar(pci, 0);
231 if (!bus->remap_addr) {
232 dev_err(bus->dev, "error: ioremap error\n");
237 sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
239 /* get controller capabilities */
240 ret = hda_dsp_ctrl_get_caps(sdev);
242 dev_err(sdev->dev, "error: get caps error\n");
247 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
249 static const char *fixup_tplg_name(struct snd_sof_dev *sdev,
250 const char *sof_tplg_filename)
252 const char *tplg_filename = NULL;
256 filename = devm_kstrdup(sdev->dev, sof_tplg_filename, GFP_KERNEL);
260 /* this assumes a .tplg extension */
261 split_ext = strsep(&filename, ".");
263 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
264 "%s-idisp.tplg", split_ext);
268 return tplg_filename;
273 static int hda_init_caps(struct snd_sof_dev *sdev)
275 struct hdac_bus *bus = sof_to_bus(sdev);
276 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
277 struct hdac_ext_link *hlink;
278 struct snd_soc_acpi_mach_params *mach_params;
279 struct snd_soc_acpi_mach *hda_mach;
280 struct snd_sof_pdata *pdata = sdev->pdata;
281 struct snd_soc_acpi_mach *mach;
282 const char *tplg_filename;
288 device_disable_async_suspend(bus->dev);
290 /* check if dsp is there */
292 dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
294 ret = hda_dsp_ctrl_init_chip(sdev, true);
296 dev_err(bus->dev, "error: init chip failed with ret: %d\n",
301 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
303 snd_hdac_ext_bus_get_ml_capabilities(bus);
305 /* init i915 and HDMI codecs */
306 ret = hda_codec_i915_init(sdev);
308 dev_err(sdev->dev, "error: no HDMI audio devices found\n");
312 /* codec detection */
313 if (!bus->codec_mask) {
314 dev_info(bus->dev, "no hda codecs found!\n");
316 dev_info(bus->dev, "hda codecs found, mask %lx\n",
319 for (i = 0; i < HDA_MAX_CODECS; i++) {
320 if (bus->codec_mask & (1 << i))
325 * If no machine driver is found, then:
327 * hda machine driver is used if :
328 * 1. there is one HDMI codec and one external HDAudio codec
331 if (!pdata->machine && codec_num <= 2 &&
332 HDA_IDISP_CODEC(bus->codec_mask)) {
333 hda_mach = snd_soc_acpi_intel_hda_machines;
334 pdata->machine = hda_mach;
336 /* topology: use the info from hda_machines */
337 pdata->tplg_filename =
338 hda_mach->sof_tplg_filename;
340 /* firmware: pick the first in machine list */
341 mach = pdata->desc->machines;
342 pdata->fw_filename = mach->sof_fw_filename;
344 dev_info(bus->dev, "using HDA machine driver %s now\n",
347 /* fixup topology file for HDMI only platforms */
348 if (codec_num == 1) {
349 /* use local variable for readability */
350 tplg_filename = pdata->tplg_filename;
351 tplg_filename = fixup_tplg_name(sdev, tplg_filename);
352 if (!tplg_filename) {
353 hda_codec_i915_exit(sdev);
356 pdata->tplg_filename = tplg_filename;
361 /* used by hda machine driver to create dai links */
362 if (pdata->machine) {
363 mach_params = (struct snd_soc_acpi_mach_params *)
364 &pdata->machine->mach_params;
365 mach_params->codec_mask = bus->codec_mask;
366 mach_params->platform = dev_name(sdev->dev);
369 /* create codec instances */
370 hda_codec_probe_bus(sdev);
372 hda_codec_i915_put(sdev);
375 * we are done probing so decrement link counts
377 list_for_each_entry(hlink, &bus->hlink_list, list)
378 snd_hdac_ext_bus_link_put(bus, hlink);
383 static const struct sof_intel_dsp_desc
384 *get_chip_info(struct snd_sof_pdata *pdata)
386 const struct sof_dev_desc *desc = pdata->desc;
387 const struct sof_intel_dsp_desc *chip_info;
389 chip_info = desc->chip_info;
394 int hda_dsp_probe(struct snd_sof_dev *sdev)
396 struct pci_dev *pci = to_pci_dev(sdev->dev);
397 struct sof_intel_hda_dev *hdev;
398 struct hdac_bus *bus;
399 const struct sof_intel_dsp_desc *chip;
403 * detect DSP by checking class/subclass/prog-id information
404 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
405 * class=04 subclass 01 prog-if 00: DSP is present
406 * (and may be required e.g. for DMIC or SSP support)
407 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
409 if (pci->class == 0x040300) {
410 dev_err(sdev->dev, "error: the DSP is not enabled on this platform, aborting probe\n");
412 } else if (pci->class != 0x040100 && pci->class != 0x040380) {
413 dev_err(sdev->dev, "error: unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n", pci->class);
416 dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n", pci->class);
418 chip = get_chip_info(sdev->pdata);
420 dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
426 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
429 sdev->pdata->hw_pdata = hdev;
432 hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
435 if (IS_ERR(hdev->dmic_dev)) {
436 dev_err(sdev->dev, "error: failed to create DMIC device\n");
437 return PTR_ERR(hdev->dmic_dev);
441 * use position update IPC if either it is forced
442 * or we don't have other choice
444 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
445 hdev->no_ipc_position = 0;
447 hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
450 /* set up HDA base */
451 bus = sof_to_bus(sdev);
452 ret = hda_init(sdev);
457 sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
458 if (!sdev->bar[HDA_DSP_BAR]) {
459 dev_err(sdev->dev, "error: ioremap error\n");
464 sdev->mmio_bar = HDA_DSP_BAR;
465 sdev->mailbox_bar = HDA_DSP_BAR;
467 /* allow 64bit DMA address if supported by H/W */
468 if (!dma_set_mask(&pci->dev, DMA_BIT_MASK(64))) {
469 dev_dbg(sdev->dev, "DMA mask is 64 bit\n");
470 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(64));
472 dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
473 dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
474 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32));
478 ret = hda_dsp_stream_init(sdev);
480 dev_err(sdev->dev, "error: failed to init streams\n");
482 * not all errors are due to memory issues, but trying
483 * to free everything does not harm
490 * let's try to enable msi firstly
491 * if it fails, use legacy interrupt mode
492 * TODO: support interrupt mode selection with kernel parameter
493 * support msi multiple vectors
495 #if IS_ENABLED(CONFIG_PCI)
496 ret = pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI);
499 dev_info(sdev->dev, "use legacy interrupt mode\n");
501 * in IO-APIC mode, hda->irq and ipc_irq are using the same
502 * irq number of pci->irq
504 hdev->irq = pci->irq;
505 sdev->ipc_irq = pci->irq;
506 sdev->msi_enabled = 0;
508 dev_info(sdev->dev, "use msi interrupt mode\n");
509 #if IS_ENABLED(CONFIG_PCI)
510 hdev->irq = pci_irq_vector(pci, 0);
512 /* ipc irq number is the same of hda irq */
513 sdev->ipc_irq = hdev->irq;
514 sdev->msi_enabled = 1;
517 dev_dbg(sdev->dev, "using HDA IRQ %d\n", hdev->irq);
518 ret = request_threaded_irq(hdev->irq, hda_dsp_stream_interrupt,
519 hda_dsp_stream_threaded_handler,
520 IRQF_SHARED, "AudioHDA", bus);
522 dev_err(sdev->dev, "error: failed to register HDA IRQ %d\n",
524 goto free_irq_vector;
527 dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
528 ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_ipc_irq_handler,
529 sof_ops(sdev)->irq_thread, IRQF_SHARED,
532 dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
538 synchronize_irq(pci->irq);
541 * clear TCSEL to clear playback on some HD Audio
542 * codecs. PCI TCSEL is defined in the Intel manuals.
544 snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
546 /* init HDA capabilities */
547 ret = hda_init_caps(sdev);
551 /* enable ppcap interrupt */
552 hda_dsp_ctrl_ppcap_enable(sdev, true);
553 hda_dsp_ctrl_ppcap_int_enable(sdev, true);
555 /* initialize waitq for code loading */
556 init_waitqueue_head(&sdev->waitq);
558 /* set default mailbox offset for FW ready message */
559 sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
564 free_irq(sdev->ipc_irq, sdev);
566 free_irq(hdev->irq, bus);
568 #if IS_ENABLED(CONFIG_PCI)
569 if (sdev->msi_enabled)
570 pci_free_irq_vectors(pci);
573 hda_dsp_stream_free(sdev);
574 /* dsp_unmap: not currently used */
575 iounmap(sdev->bar[HDA_DSP_BAR]);
577 iounmap(bus->remap_addr);
582 int hda_dsp_remove(struct snd_sof_dev *sdev)
584 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
585 struct hdac_bus *bus = sof_to_bus(sdev);
586 const struct sof_intel_dsp_desc *chip = hda->desc;
588 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
589 /* codec removal, invoke bus_device_remove */
590 snd_hdac_ext_bus_device_remove(bus);
593 if (!IS_ERR_OR_NULL(hda->dmic_dev))
594 platform_device_unregister(hda->dmic_dev);
596 /* disable DSP IRQ */
597 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
598 SOF_HDA_PPCTL_PIE, 0);
600 /* disable CIE and GIE interrupts */
601 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
602 SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
606 hda_dsp_core_reset_power_down(sdev, chip->cores_mask);
609 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
610 SOF_HDA_PPCTL_GPROCEN, 0);
612 free_irq(sdev->ipc_irq, sdev);
613 free_irq(hda->irq, bus);
614 #if IS_ENABLED(CONFIG_PCI)
615 if (sdev->msi_enabled) {
616 struct pci_dev *pci = to_pci_dev(sdev->dev);
617 pci_free_irq_vectors(pci);
621 hda_dsp_stream_free(sdev);
622 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
623 snd_hdac_link_free_all(bus);
626 iounmap(sdev->bar[HDA_DSP_BAR]);
627 iounmap(bus->remap_addr);
629 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
630 snd_hdac_ext_bus_exit(bus);
632 hda_codec_i915_exit(sdev);
637 MODULE_LICENSE("Dual BSD/GPL");