1 // SPDX-License-Identifier: (GPL-2.0-only 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/acpi.h>
22 #include <linux/module.h>
23 #include <linux/soundwire/sdw.h>
24 #include <linux/soundwire/sdw_intel.h>
25 #include <sound/intel-dsp-config.h>
26 #include <sound/intel-nhlt.h>
27 #include <sound/sof.h>
28 #include <sound/sof/xtensa.h>
29 #include "../sof-audio.h"
30 #include "../sof-pci-dev.h"
34 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
35 #include <sound/soc-acpi-intel-match.h>
38 /* platform specific devices */
41 #define EXCEPT_MAX_HDR_SIZE 0x400
42 #define HDA_EXT_ROM_STATUS_SIZE 8
44 static const struct sof_intel_dsp_desc
45 *get_chip_info(struct snd_sof_pdata *pdata)
47 const struct sof_dev_desc *desc = pdata->desc;
48 const struct sof_intel_dsp_desc *chip_info;
50 chip_info = desc->chip_info;
55 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
58 * The default for SoundWire clock stop quirks is to power gate the IP
59 * and do a Bus Reset, this will need to be modified when the DSP
60 * needs to remain in D0i3 so that the Master does not lose context
61 * and enumeration is not required on clock restart
63 static int sdw_clock_stop_quirks = SDW_INTEL_CLK_STOP_BUS_RESET;
64 module_param(sdw_clock_stop_quirks, int, 0444);
65 MODULE_PARM_DESC(sdw_clock_stop_quirks, "SOF SoundWire clock stop quirks");
67 static int sdw_params_stream(struct device *dev,
68 struct sdw_intel_stream_params_data *params_data)
70 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
71 struct snd_soc_dai *d = params_data->dai;
72 struct sof_ipc_dai_config config;
73 struct sof_ipc_reply reply;
74 int link_id = params_data->link_id;
75 int alh_stream_id = params_data->alh_stream_id;
77 u32 size = sizeof(config);
79 memset(&config, 0, size);
80 config.hdr.size = size;
81 config.hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
82 config.type = SOF_DAI_INTEL_ALH;
83 config.dai_index = (link_id << 8) | (d->id);
84 config.alh.stream_id = alh_stream_id;
86 /* send message to DSP */
87 ret = sof_ipc_tx_message(sdev->ipc,
88 config.hdr.cmd, &config, size, &reply,
92 "error: failed to set DAI hw_params for link %d dai->id %d ALH %d\n",
93 link_id, d->id, alh_stream_id);
99 static int sdw_free_stream(struct device *dev,
100 struct sdw_intel_stream_free_data *free_data)
102 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
103 struct snd_soc_dai *d = free_data->dai;
104 struct sof_ipc_dai_config config;
105 struct sof_ipc_reply reply;
106 int link_id = free_data->link_id;
108 u32 size = sizeof(config);
110 memset(&config, 0, size);
111 config.hdr.size = size;
112 config.hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
113 config.type = SOF_DAI_INTEL_ALH;
114 config.dai_index = (link_id << 8) | d->id;
115 config.alh.stream_id = 0xFFFF; /* invalid value on purpose */
117 /* send message to DSP */
118 ret = sof_ipc_tx_message(sdev->ipc,
119 config.hdr.cmd, &config, size, &reply,
123 "error: failed to free stream for link %d dai->id %d\n",
130 static const struct sdw_intel_ops sdw_callback = {
131 .params_stream = sdw_params_stream,
132 .free_stream = sdw_free_stream,
135 void hda_sdw_int_enable(struct snd_sof_dev *sdev, bool enable)
137 sdw_intel_enable_irq(sdev->bar[HDA_DSP_BAR], enable);
140 static int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
142 struct sof_intel_hda_dev *hdev;
146 handle = ACPI_HANDLE(sdev->dev);
148 /* save ACPI info for the probe step */
149 hdev = sdev->pdata->hw_pdata;
151 ret = sdw_intel_acpi_scan(handle, &hdev->info);
158 static int hda_sdw_probe(struct snd_sof_dev *sdev)
160 struct sof_intel_hda_dev *hdev;
161 struct sdw_intel_res res;
164 hdev = sdev->pdata->hw_pdata;
166 memset(&res, 0, sizeof(res));
168 res.mmio_base = sdev->bar[HDA_DSP_BAR];
169 res.shim_base = hdev->desc->sdw_shim_base;
170 res.alh_base = hdev->desc->sdw_alh_base;
171 res.irq = sdev->ipc_irq;
172 res.handle = hdev->info.handle;
173 res.parent = sdev->dev;
174 res.ops = &sdw_callback;
176 res.clock_stop_quirks = sdw_clock_stop_quirks;
179 * ops and arg fields are not populated for now,
180 * they will be needed when the DAI callbacks are
184 /* we could filter links here if needed, e.g for quirks */
185 res.count = hdev->info.count;
186 res.link_mask = hdev->info.link_mask;
188 sdw = sdw_intel_probe(&res);
190 dev_err(sdev->dev, "error: SoundWire probe failed\n");
200 int hda_sdw_startup(struct snd_sof_dev *sdev)
202 struct sof_intel_hda_dev *hdev;
203 struct snd_sof_pdata *pdata = sdev->pdata;
205 hdev = sdev->pdata->hw_pdata;
210 if (pdata->machine && !pdata->machine->mach_params.link_mask)
213 return sdw_intel_startup(hdev->sdw);
216 static int hda_sdw_exit(struct snd_sof_dev *sdev)
218 struct sof_intel_hda_dev *hdev;
220 hdev = sdev->pdata->hw_pdata;
222 hda_sdw_int_enable(sdev, false);
225 sdw_intel_exit(hdev->sdw);
231 bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev)
233 struct sof_intel_hda_dev *hdev;
237 hdev = sdev->pdata->hw_pdata;
243 irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2);
245 /* invalid message ? */
246 if (irq_status == 0xffffffff)
250 if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW)
257 static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
259 const struct sof_intel_dsp_desc *chip;
261 chip = get_chip_info(sdev->pdata);
262 if (chip && chip->check_sdw_irq)
263 return chip->check_sdw_irq(sdev);
268 static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
270 return sdw_intel_thread(irq, context);
273 static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
275 struct sof_intel_hda_dev *hdev;
277 hdev = sdev->pdata->hw_pdata;
279 snd_sof_dsp_read(sdev, HDA_DSP_BAR,
280 hdev->desc->sdw_shim_base + SDW_SHIM_WAKESTS))
286 void hda_sdw_process_wakeen(struct snd_sof_dev *sdev)
288 struct sof_intel_hda_dev *hdev;
290 hdev = sdev->pdata->hw_pdata;
294 sdw_intel_process_wakeen_event(hdev->sdw);
303 struct hda_dsp_msg_code {
308 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
309 static bool hda_use_msi = true;
310 module_param_named(use_msi, hda_use_msi, bool, 0444);
311 MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode");
313 #define hda_use_msi (1)
316 static char *hda_model;
317 module_param(hda_model, charp, 0444);
318 MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
320 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
321 static int hda_dmic_num = -1;
322 module_param_named(dmic_num, hda_dmic_num, int, 0444);
323 MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number");
326 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
327 static bool hda_codec_use_common_hdmi = IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI);
328 module_param_named(use_common_hdmi, hda_codec_use_common_hdmi, bool, 0444);
329 MODULE_PARM_DESC(use_common_hdmi, "SOF HDA use common HDMI codec driver");
332 static const struct hda_dsp_msg_code hda_dsp_rom_msg[] = {
333 {HDA_DSP_ROM_FW_MANIFEST_LOADED, "status: manifest loaded"},
334 {HDA_DSP_ROM_FW_FW_LOADED, "status: fw loaded"},
335 {HDA_DSP_ROM_FW_ENTERED, "status: fw entered"},
336 {HDA_DSP_ROM_CSE_ERROR, "error: cse error"},
337 {HDA_DSP_ROM_CSE_WRONG_RESPONSE, "error: cse wrong response"},
338 {HDA_DSP_ROM_IMR_TO_SMALL, "error: IMR too small"},
339 {HDA_DSP_ROM_BASE_FW_NOT_FOUND, "error: base fw not found"},
340 {HDA_DSP_ROM_CSE_VALIDATION_FAILED, "error: signature verification failed"},
341 {HDA_DSP_ROM_IPC_FATAL_ERROR, "error: ipc fatal error"},
342 {HDA_DSP_ROM_L2_CACHE_ERROR, "error: L2 cache error"},
343 {HDA_DSP_ROM_LOAD_OFFSET_TO_SMALL, "error: load offset too small"},
344 {HDA_DSP_ROM_API_PTR_INVALID, "error: API ptr invalid"},
345 {HDA_DSP_ROM_BASEFW_INCOMPAT, "error: base fw incompatible"},
346 {HDA_DSP_ROM_UNHANDLED_INTERRUPT, "error: unhandled interrupt"},
347 {HDA_DSP_ROM_MEMORY_HOLE_ECC, "error: ECC memory hole"},
348 {HDA_DSP_ROM_KERNEL_EXCEPTION, "error: kernel exception"},
349 {HDA_DSP_ROM_USER_EXCEPTION, "error: user exception"},
350 {HDA_DSP_ROM_UNEXPECTED_RESET, "error: unexpected reset"},
351 {HDA_DSP_ROM_NULL_FW_ENTRY, "error: null FW entry point"},
354 static void hda_dsp_get_status(struct snd_sof_dev *sdev)
356 const struct sof_intel_dsp_desc *chip;
360 chip = get_chip_info(sdev->pdata);
361 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
362 chip->rom_status_reg);
364 for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
365 if (status == hda_dsp_rom_msg[i].code) {
366 dev_err(sdev->dev, "%s - code %8.8x\n",
367 hda_dsp_rom_msg[i].msg, status);
372 /* not for us, must be generic sof message */
373 dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
376 static void hda_dsp_get_registers(struct snd_sof_dev *sdev,
377 struct sof_ipc_dsp_oops_xtensa *xoops,
378 struct sof_ipc_panic_info *panic_info,
379 u32 *stack, size_t stack_words)
381 u32 offset = sdev->dsp_oops_offset;
383 /* first read registers */
384 sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
386 /* note: variable AR register array is not read */
388 /* then get panic info */
389 if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
390 dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
391 xoops->arch_hdr.totalsize);
394 offset += xoops->arch_hdr.totalsize;
395 sof_block_read(sdev, sdev->mmio_bar, offset,
396 panic_info, sizeof(*panic_info));
398 /* then get the stack */
399 offset += sizeof(*panic_info);
400 sof_block_read(sdev, sdev->mmio_bar, offset, stack,
401 stack_words * sizeof(u32));
404 /* dump the first 8 dwords representing the extended ROM status */
405 static void hda_dsp_dump_ext_rom_status(struct snd_sof_dev *sdev, u32 flags)
407 const struct sof_intel_dsp_desc *chip;
413 chip = get_chip_info(sdev->pdata);
414 for (i = 0; i < HDA_EXT_ROM_STATUS_SIZE; i++) {
415 value = snd_sof_dsp_read(sdev, HDA_DSP_BAR, chip->rom_status_reg + i * 0x4);
416 len += snprintf(msg + len, sizeof(msg) - len, " 0x%x", value);
419 sof_dev_dbg_or_err(sdev->dev, flags & SOF_DBG_DUMP_FORCE_ERR_LEVEL,
420 "extended rom status: %s", msg);
424 void hda_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
426 struct sof_ipc_dsp_oops_xtensa xoops;
427 struct sof_ipc_panic_info panic_info;
428 u32 stack[HDA_DSP_STACK_DUMP_SIZE];
430 /* print ROM/FW status */
431 hda_dsp_get_status(sdev);
433 /* print panic info if FW boot is complete. Otherwise, print the extended ROM status */
434 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) {
435 u32 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_STATUS);
436 u32 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_TRACEP);
438 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
439 HDA_DSP_STACK_DUMP_SIZE);
440 snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
441 stack, HDA_DSP_STACK_DUMP_SIZE);
443 hda_dsp_dump_ext_rom_status(sdev, flags);
447 void hda_ipc_irq_dump(struct snd_sof_dev *sdev)
449 struct hdac_bus *bus = sof_to_bus(sdev);
456 /* read key IRQ stats and config registers */
457 adspis = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
458 intsts = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS);
459 intctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL);
460 ppsts = snd_sof_dsp_read(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPSTS);
461 rirbsts = snd_hdac_chip_readb(bus, RIRBSTS);
464 "error: hda irq intsts 0x%8.8x intlctl 0x%8.8x rirb %2.2x\n",
465 intsts, intctl, rirbsts);
467 "error: dsp irq ppsts 0x%8.8x adspis 0x%8.8x\n",
471 void hda_ipc_dump(struct snd_sof_dev *sdev)
477 hda_ipc_irq_dump(sdev);
479 /* read IPC status */
480 hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
481 hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
482 hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL);
484 /* dump the IPC regs */
485 /* TODO: parse the raw msg */
487 "error: host status 0x%8.8x dsp status 0x%8.8x mask 0x%8.8x\n",
488 hipcie, hipct, hipcctl);
491 static int hda_init(struct snd_sof_dev *sdev)
493 struct hda_bus *hbus;
494 struct hdac_bus *bus;
495 struct pci_dev *pci = to_pci_dev(sdev->dev);
498 hbus = sof_to_hbus(sdev);
499 bus = sof_to_bus(sdev);
502 sof_hda_bus_init(bus, &pci->dev);
505 bus->bdl_pos_adj = 0;
508 mutex_init(&hbus->prepare_mutex);
510 hbus->mixer_assigned = -1;
511 hbus->modelname = hda_model;
513 /* initialise hdac bus */
514 bus->addr = pci_resource_start(pci, 0);
515 bus->remap_addr = pci_ioremap_bar(pci, 0);
516 if (!bus->remap_addr) {
517 dev_err(bus->dev, "error: ioremap error\n");
522 sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
524 /* init i915 and HDMI codecs */
525 ret = hda_codec_i915_init(sdev);
527 dev_warn(sdev->dev, "init of i915 and HDMI codec failed\n");
529 /* get controller capabilities */
530 ret = hda_dsp_ctrl_get_caps(sdev);
532 dev_err(sdev->dev, "error: get caps error\n");
537 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
539 static int check_nhlt_dmic(struct snd_sof_dev *sdev)
541 struct nhlt_acpi_table *nhlt;
544 nhlt = intel_nhlt_init(sdev->dev);
546 dmic_num = intel_nhlt_get_dmic_geo(sdev->dev, nhlt);
547 intel_nhlt_free(nhlt);
548 if (dmic_num >= 1 && dmic_num <= 4)
555 static const char *fixup_tplg_name(struct snd_sof_dev *sdev,
556 const char *sof_tplg_filename,
557 const char *idisp_str,
558 const char *dmic_str)
560 const char *tplg_filename = NULL;
561 char *filename, *tmp;
562 const char *split_ext;
564 filename = kstrdup(sof_tplg_filename, GFP_KERNEL);
568 /* this assumes a .tplg extension */
570 split_ext = strsep(&tmp, ".");
572 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
574 split_ext, idisp_str, dmic_str);
577 return tplg_filename;
580 static int dmic_topology_fixup(struct snd_sof_dev *sdev,
581 const char **tplg_filename,
582 const char *idisp_str,
585 const char *default_tplg_filename = *tplg_filename;
586 const char *fixed_tplg_filename;
587 const char *dmic_str;
590 /* first check NHLT for DMICs */
591 dmic_num = check_nhlt_dmic(sdev);
593 /* allow for module parameter override */
594 if (hda_dmic_num != -1) {
596 "overriding DMICs detected in NHLT tables %d by kernel param %d\n",
597 dmic_num, hda_dmic_num);
598 dmic_num = hda_dmic_num;
620 fixed_tplg_filename = fixup_tplg_name(sdev, default_tplg_filename,
621 idisp_str, dmic_str);
622 if (!fixed_tplg_filename)
625 dev_info(sdev->dev, "DMICs detected in NHLT tables: %d\n", dmic_num);
626 *dmic_found = dmic_num;
627 *tplg_filename = fixed_tplg_filename;
633 static int hda_init_caps(struct snd_sof_dev *sdev)
635 struct hdac_bus *bus = sof_to_bus(sdev);
636 struct snd_sof_pdata *pdata = sdev->pdata;
637 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
638 struct hdac_ext_link *hlink;
640 struct sof_intel_hda_dev *hdev = pdata->hw_pdata;
644 /* check if dsp is there */
646 dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
648 /* Init HDA controller after i915 init */
649 ret = hda_dsp_ctrl_init_chip(sdev, true);
651 dev_err(bus->dev, "error: init chip failed with ret: %d\n",
656 /* scan SoundWire capabilities exposed by DSDT */
657 ret = hda_sdw_acpi_scan(sdev);
659 dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
663 link_mask = hdev->info.link_mask;
665 dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n");
670 * probe/allocate SoundWire resources.
671 * The hardware configuration takes place in hda_sdw_startup
672 * after power rails are enabled.
673 * It's entirely possible to have a mix of I2S/DMIC/SoundWire
674 * devices, so we allocate the resources in all cases.
676 ret = hda_sdw_probe(sdev);
678 dev_err(sdev->dev, "error: SoundWire probe error\n");
684 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
686 snd_hdac_ext_bus_get_ml_capabilities(bus);
688 /* create codec instances */
689 hda_codec_probe_bus(sdev, hda_codec_use_common_hdmi);
691 if (!HDA_IDISP_CODEC(bus->codec_mask))
692 hda_codec_i915_display_power(sdev, false);
695 * we are done probing so decrement link counts
697 list_for_each_entry(hlink, &bus->hlink_list, list)
698 snd_hdac_ext_bus_link_put(bus, hlink);
703 static void hda_check_for_state_change(struct snd_sof_dev *sdev)
705 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
706 struct hdac_bus *bus = sof_to_bus(sdev);
707 unsigned int codec_mask;
709 codec_mask = snd_hdac_chip_readw(bus, STATESTS);
711 hda_codec_jack_check(sdev);
712 snd_hdac_chip_writew(bus, STATESTS, codec_mask);
717 static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
719 struct snd_sof_dev *sdev = context;
722 * Get global interrupt status. It includes all hardware interrupt
723 * sources in the Intel HD Audio controller.
725 if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
726 SOF_HDA_INTSTS_GIS) {
728 /* disable GIE interrupt */
729 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
731 SOF_HDA_INT_GLOBAL_EN,
734 return IRQ_WAKE_THREAD;
740 static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
742 struct snd_sof_dev *sdev = context;
743 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
745 /* deal with streams and controller first */
746 if (hda_dsp_check_stream_irq(sdev))
747 hda_dsp_stream_threaded_handler(irq, sdev);
749 if (hda_dsp_check_ipc_irq(sdev))
750 sof_ops(sdev)->irq_thread(irq, sdev);
752 if (hda_dsp_check_sdw_irq(sdev))
753 hda_dsp_sdw_thread(irq, hdev->sdw);
755 if (hda_sdw_check_wakeen_irq(sdev))
756 hda_sdw_process_wakeen(sdev);
758 hda_check_for_state_change(sdev);
760 /* enable GIE interrupt */
761 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
763 SOF_HDA_INT_GLOBAL_EN,
764 SOF_HDA_INT_GLOBAL_EN);
769 int hda_dsp_probe(struct snd_sof_dev *sdev)
771 struct pci_dev *pci = to_pci_dev(sdev->dev);
772 struct sof_intel_hda_dev *hdev;
773 struct hdac_bus *bus;
774 const struct sof_intel_dsp_desc *chip;
778 * detect DSP by checking class/subclass/prog-id information
779 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
780 * class=04 subclass 01 prog-if 00: DSP is present
781 * (and may be required e.g. for DMIC or SSP support)
782 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
784 if (pci->class == 0x040300) {
785 dev_err(sdev->dev, "error: the DSP is not enabled on this platform, aborting probe\n");
787 } else if (pci->class != 0x040100 && pci->class != 0x040380) {
788 dev_err(sdev->dev, "error: unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n", pci->class);
791 dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n", pci->class);
793 chip = get_chip_info(sdev->pdata);
795 dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
801 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
804 sdev->pdata->hw_pdata = hdev;
807 hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
810 if (IS_ERR(hdev->dmic_dev)) {
811 dev_err(sdev->dev, "error: failed to create DMIC device\n");
812 return PTR_ERR(hdev->dmic_dev);
816 * use position update IPC if either it is forced
817 * or we don't have other choice
819 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
820 hdev->no_ipc_position = 0;
822 hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
825 /* set up HDA base */
826 bus = sof_to_bus(sdev);
827 ret = hda_init(sdev);
832 sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
833 if (!sdev->bar[HDA_DSP_BAR]) {
834 dev_err(sdev->dev, "error: ioremap error\n");
839 sdev->mmio_bar = HDA_DSP_BAR;
840 sdev->mailbox_bar = HDA_DSP_BAR;
842 /* allow 64bit DMA address if supported by H/W */
843 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(64))) {
844 dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
845 dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32));
849 ret = hda_dsp_stream_init(sdev);
851 dev_err(sdev->dev, "error: failed to init streams\n");
853 * not all errors are due to memory issues, but trying
854 * to free everything does not harm
861 * let's try to enable msi firstly
862 * if it fails, use legacy interrupt mode
863 * TODO: support msi multiple vectors
865 if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
866 dev_info(sdev->dev, "use msi interrupt mode\n");
867 sdev->ipc_irq = pci_irq_vector(pci, 0);
868 /* initialised to "false" by kzalloc() */
869 sdev->msi_enabled = true;
872 if (!sdev->msi_enabled) {
873 dev_info(sdev->dev, "use legacy interrupt mode\n");
875 * in IO-APIC mode, hda->irq and ipc_irq are using the same
876 * irq number of pci->irq
878 sdev->ipc_irq = pci->irq;
881 dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
882 ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
883 hda_dsp_interrupt_thread,
884 IRQF_SHARED, "AudioDSP", sdev);
886 dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
888 goto free_irq_vector;
892 synchronize_irq(pci->irq);
895 * clear TCSEL to clear playback on some HD Audio
896 * codecs. PCI TCSEL is defined in the Intel manuals.
898 snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
900 /* init HDA capabilities */
901 ret = hda_init_caps(sdev);
905 /* enable ppcap interrupt */
906 hda_dsp_ctrl_ppcap_enable(sdev, true);
907 hda_dsp_ctrl_ppcap_int_enable(sdev, true);
909 /* set default mailbox offset for FW ready message */
910 sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
912 INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
917 free_irq(sdev->ipc_irq, sdev);
919 if (sdev->msi_enabled)
920 pci_free_irq_vectors(pci);
922 hda_dsp_stream_free(sdev);
923 /* dsp_unmap: not currently used */
924 iounmap(sdev->bar[HDA_DSP_BAR]);
926 platform_device_unregister(hdev->dmic_dev);
927 iounmap(bus->remap_addr);
928 hda_codec_i915_exit(sdev);
933 int hda_dsp_remove(struct snd_sof_dev *sdev)
935 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
936 struct hdac_bus *bus = sof_to_bus(sdev);
937 struct pci_dev *pci = to_pci_dev(sdev->dev);
938 const struct sof_intel_dsp_desc *chip = hda->desc;
940 /* cancel any attempt for DSP D0I3 */
941 cancel_delayed_work_sync(&hda->d0i3_work);
943 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
944 /* codec removal, invoke bus_device_remove */
945 snd_hdac_ext_bus_device_remove(bus);
950 if (!IS_ERR_OR_NULL(hda->dmic_dev))
951 platform_device_unregister(hda->dmic_dev);
953 /* disable DSP IRQ */
954 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
955 SOF_HDA_PPCTL_PIE, 0);
957 /* disable CIE and GIE interrupts */
958 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
959 SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
963 snd_sof_dsp_core_power_down(sdev, chip->host_managed_cores_mask);
966 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
967 SOF_HDA_PPCTL_GPROCEN, 0);
969 free_irq(sdev->ipc_irq, sdev);
970 if (sdev->msi_enabled)
971 pci_free_irq_vectors(pci);
973 hda_dsp_stream_free(sdev);
974 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
975 snd_hdac_link_free_all(bus);
978 iounmap(sdev->bar[HDA_DSP_BAR]);
979 iounmap(bus->remap_addr);
981 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
982 snd_hdac_ext_bus_exit(bus);
984 hda_codec_i915_exit(sdev);
989 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
990 static int hda_generic_machine_select(struct snd_sof_dev *sdev)
992 struct hdac_bus *bus = sof_to_bus(sdev);
993 struct snd_soc_acpi_mach_params *mach_params;
994 struct snd_soc_acpi_mach *hda_mach;
995 struct snd_sof_pdata *pdata = sdev->pdata;
996 const char *tplg_filename;
997 const char *idisp_str;
1003 /* codec detection */
1004 if (!bus->codec_mask) {
1005 dev_info(bus->dev, "no hda codecs found!\n");
1007 dev_info(bus->dev, "hda codecs found, mask %lx\n",
1010 for (i = 0; i < HDA_MAX_CODECS; i++) {
1011 if (bus->codec_mask & (1 << i))
1016 * If no machine driver is found, then:
1018 * generic hda machine driver can handle:
1019 * - one HDMI codec, and/or
1020 * - one external HDAudio codec
1022 if (!pdata->machine && codec_num <= 2) {
1023 hda_mach = snd_soc_acpi_intel_hda_machines;
1025 dev_info(bus->dev, "using HDA machine driver %s now\n",
1026 hda_mach->drv_name);
1028 if (codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask))
1029 idisp_str = "-idisp";
1033 /* topology: use the info from hda_machines */
1034 tplg_filename = hda_mach->sof_tplg_filename;
1035 ret = dmic_topology_fixup(sdev, &tplg_filename, idisp_str, &dmic_num);
1039 hda_mach->mach_params.dmic_num = dmic_num;
1040 pdata->machine = hda_mach;
1041 pdata->tplg_filename = tplg_filename;
1043 if (codec_num == 2) {
1045 * Prevent SoundWire links from starting when an external
1046 * HDaudio codec is used
1048 hda_mach->mach_params.link_mask = 0;
1053 /* used by hda machine driver to create dai links */
1054 if (pdata->machine) {
1055 mach_params = (struct snd_soc_acpi_mach_params *)
1056 &pdata->machine->mach_params;
1057 mach_params->codec_mask = bus->codec_mask;
1058 mach_params->common_hdmi_codec_drv = hda_codec_use_common_hdmi;
1064 static int hda_generic_machine_select(struct snd_sof_dev *sdev)
1070 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1071 /* Check if all Slaves defined on the link can be found */
1072 static bool link_slaves_found(struct snd_sof_dev *sdev,
1073 const struct snd_soc_acpi_link_adr *link,
1074 struct sdw_intel_ctx *sdw)
1076 struct hdac_bus *bus = sof_to_bus(sdev);
1077 struct sdw_intel_slave_id *ids = sdw->ids;
1078 int num_slaves = sdw->num_slaves;
1079 unsigned int part_id, link_id, unique_id, mfg_id, version;
1082 for (i = 0; i < link->num_adr; i++) {
1083 u64 adr = link->adr_d[i].adr;
1084 int reported_part_count = 0;
1086 mfg_id = SDW_MFG_ID(adr);
1087 part_id = SDW_PART_ID(adr);
1088 link_id = SDW_DISCO_LINK_ID(adr);
1089 version = SDW_VERSION(adr);
1091 for (j = 0; j < num_slaves; j++) {
1092 /* find out how many identical parts were reported on that link */
1093 if (ids[j].link_id == link_id &&
1094 ids[j].id.part_id == part_id &&
1095 ids[j].id.mfg_id == mfg_id &&
1096 ids[j].id.sdw_version == version)
1097 reported_part_count++;
1100 for (j = 0; j < num_slaves; j++) {
1101 int expected_part_count = 0;
1103 if (ids[j].link_id != link_id ||
1104 ids[j].id.part_id != part_id ||
1105 ids[j].id.mfg_id != mfg_id ||
1106 ids[j].id.sdw_version != version)
1109 /* find out how many identical parts are expected */
1110 for (k = 0; k < link->num_adr; k++) {
1111 u64 adr2 = link->adr_d[k].adr;
1112 unsigned int part_id2, link_id2, mfg_id2, version2;
1114 mfg_id2 = SDW_MFG_ID(adr2);
1115 part_id2 = SDW_PART_ID(adr2);
1116 link_id2 = SDW_DISCO_LINK_ID(adr2);
1117 version2 = SDW_VERSION(adr2);
1119 if (link_id2 == link_id &&
1120 part_id2 == part_id &&
1121 mfg_id2 == mfg_id &&
1122 version2 == version)
1123 expected_part_count++;
1126 if (reported_part_count == expected_part_count) {
1128 * we have to check unique id
1129 * if there is more than one
1132 unique_id = SDW_UNIQUE_ID(adr);
1133 if (reported_part_count == 1 ||
1134 ids[j].id.unique_id == unique_id) {
1135 dev_dbg(bus->dev, "found %x at link %d\n",
1140 dev_dbg(bus->dev, "part %x reported %d expected %d on link %d, skipping\n",
1141 part_id, reported_part_count, expected_part_count, link_id);
1144 if (j == num_slaves) {
1146 "Slave %x not found\n",
1154 static int hda_sdw_machine_select(struct snd_sof_dev *sdev)
1156 struct snd_sof_pdata *pdata = sdev->pdata;
1157 const struct snd_soc_acpi_link_adr *link;
1158 struct snd_soc_acpi_mach *mach;
1159 struct sof_intel_hda_dev *hdev;
1163 hdev = pdata->hw_pdata;
1164 link_mask = hdev->info.link_mask;
1167 * Select SoundWire machine driver if needed using the
1168 * alternate tables. This case deals with SoundWire-only
1169 * machines, for mixed cases with I2C/I2S the detection relies
1172 if (link_mask && !pdata->machine) {
1173 for (mach = pdata->desc->alt_machines;
1174 mach && mach->link_mask; mach++) {
1176 * On some platforms such as Up Extreme all links
1177 * are enabled but only one link can be used by
1178 * external codec. Instead of exact match of two masks,
1179 * first check whether link_mask of mach is subset of
1180 * link_mask supported by hw and then go on searching
1183 if (~link_mask & mach->link_mask)
1186 /* No need to match adr if there is no links defined */
1191 for (i = 0; i < hdev->info.count && link->num_adr;
1194 * Try next machine if any expected Slaves
1195 * are not found on this link.
1197 if (!link_slaves_found(sdev, link, hdev->sdw))
1200 /* Found if all Slaves are checked */
1201 if (i == hdev->info.count || !link->num_adr)
1204 if (mach && mach->link_mask) {
1207 pdata->machine = mach;
1208 mach->mach_params.links = mach->links;
1209 mach->mach_params.link_mask = mach->link_mask;
1210 mach->mach_params.platform = dev_name(sdev->dev);
1211 if (mach->sof_fw_filename)
1212 pdata->fw_filename = mach->sof_fw_filename;
1214 pdata->fw_filename = pdata->desc->default_fw_filename;
1215 pdata->tplg_filename = mach->sof_tplg_filename;
1218 * DMICs use up to 4 pins and are typically pin-muxed with SoundWire
1219 * link 2 and 3, thus we only try to enable dmics if all conditions
1221 * a) link 2 and 3 are not used by SoundWire
1222 * b) the NHLT table reports the presence of microphones
1224 if (!(mach->link_mask & GENMASK(3, 2))) {
1225 const char *tplg_filename = mach->sof_tplg_filename;
1228 ret = dmic_topology_fixup(sdev, &tplg_filename, "", &dmic_num);
1233 pdata->tplg_filename = tplg_filename;
1235 mach->mach_params.dmic_num = dmic_num;
1238 "SoundWire machine driver %s topology %s\n",
1240 pdata->tplg_filename);
1243 "No SoundWire machine driver found\n");
1250 static int hda_sdw_machine_select(struct snd_sof_dev *sdev)
1256 void hda_set_mach_params(const struct snd_soc_acpi_mach *mach,
1257 struct snd_sof_dev *sdev)
1259 struct snd_sof_pdata *pdata = sdev->pdata;
1260 const struct sof_dev_desc *desc = pdata->desc;
1261 struct snd_soc_acpi_mach_params *mach_params;
1263 mach_params = (struct snd_soc_acpi_mach_params *)&mach->mach_params;
1264 mach_params->platform = dev_name(sdev->dev);
1265 mach_params->num_dai_drivers = desc->ops->num_drv;
1266 mach_params->dai_drivers = desc->ops->drv;
1269 void hda_machine_select(struct snd_sof_dev *sdev)
1271 struct snd_sof_pdata *sof_pdata = sdev->pdata;
1272 const struct sof_dev_desc *desc = sof_pdata->desc;
1273 struct snd_soc_acpi_mach *mach;
1275 mach = snd_soc_acpi_find_machine(desc->machines);
1278 * If tplg file name is overridden, use it instead of
1279 * the one set in mach table
1281 if (!sof_pdata->tplg_filename)
1282 sof_pdata->tplg_filename = mach->sof_tplg_filename;
1284 sof_pdata->machine = mach;
1286 if (mach->link_mask) {
1287 mach->mach_params.links = mach->links;
1288 mach->mach_params.link_mask = mach->link_mask;
1293 * If I2S fails, try SoundWire
1295 hda_sdw_machine_select(sdev);
1298 * Choose HDA generic machine driver if mach is NULL.
1299 * Otherwise, set certain mach params.
1301 hda_generic_machine_select(sdev);
1303 if (!sof_pdata->machine)
1304 dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1307 int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1311 ret = snd_intel_dsp_driver_probe(pci);
1312 if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1313 dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1317 return sof_pci_probe(pci, pci_id);
1319 EXPORT_SYMBOL_NS(hda_pci_intel_probe, SND_SOC_SOF_INTEL_HDA_COMMON);
1321 MODULE_LICENSE("Dual BSD/GPL");
1322 MODULE_IMPORT_NS(SND_SOC_SOF_PCI_DEV);
1323 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
1324 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
1325 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
1326 MODULE_IMPORT_NS(SND_INTEL_SOUNDWIRE_ACPI);
1327 MODULE_IMPORT_NS(SOUNDWIRE_INTEL_INIT);