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 int hda_ctrl_dai_widget_setup(struct snd_soc_dapm_widget *w, unsigned int quirk_flags)
46 struct snd_sof_widget *swidget = w->dobj.private;
47 struct snd_soc_component *component = swidget->scomp;
48 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
49 struct sof_ipc_dai_config *config;
50 struct snd_sof_dai *sof_dai;
51 struct sof_ipc_reply reply;
54 sof_dai = swidget->private;
56 if (!sof_dai || !sof_dai->dai_config) {
57 dev_err(sdev->dev, "No config for DAI %s\n", w->name);
61 /* DAI already configured, reset it before reconfiguring it */
62 if (sof_dai->configured) {
63 ret = hda_ctrl_dai_widget_free(w, SOF_DAI_CONFIG_FLAGS_NONE);
68 config = &sof_dai->dai_config[sof_dai->current_config];
71 * For static pipelines, the DAI widget would already be set up and calling
72 * sof_widget_setup() simply returns without doing anything.
73 * For dynamic pipelines, the DAI widget will be set up now.
75 ret = sof_widget_setup(sdev, swidget);
77 dev_err(sdev->dev, "error: failed setting up DAI widget %s\n", w->name);
81 /* set HW_PARAMS flag along with quirks */
82 config->flags = SOF_DAI_CONFIG_FLAGS_HW_PARAMS |
83 quirk_flags << SOF_DAI_CONFIG_FLAGS_QUIRK_SHIFT;
86 /* send DAI_CONFIG IPC */
87 ret = sof_ipc_tx_message(sdev->ipc, config->hdr.cmd, config, config->hdr.size,
88 &reply, sizeof(reply));
90 dev_err(sdev->dev, "error: failed setting DAI config for %s\n", w->name);
94 sof_dai->configured = true;
99 int hda_ctrl_dai_widget_free(struct snd_soc_dapm_widget *w, unsigned int quirk_flags)
101 struct snd_sof_widget *swidget = w->dobj.private;
102 struct snd_soc_component *component = swidget->scomp;
103 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
104 struct sof_ipc_dai_config *config;
105 struct snd_sof_dai *sof_dai;
106 struct sof_ipc_reply reply;
109 sof_dai = swidget->private;
111 if (!sof_dai || !sof_dai->dai_config) {
112 dev_err(sdev->dev, "error: No config to free DAI %s\n", w->name);
116 /* nothing to do if hw_free() is called without restarting the stream after resume. */
117 if (!sof_dai->configured)
120 config = &sof_dai->dai_config[sof_dai->current_config];
122 /* set HW_FREE flag along with any quirks */
123 config->flags = SOF_DAI_CONFIG_FLAGS_HW_FREE |
124 quirk_flags << SOF_DAI_CONFIG_FLAGS_QUIRK_SHIFT;
126 ret = sof_ipc_tx_message(sdev->ipc, config->hdr.cmd, config, config->hdr.size,
127 &reply, sizeof(reply));
129 dev_err(sdev->dev, "error: failed resetting DAI config for %s\n", w->name);
132 * Reset the configured_flag and free the widget even if the IPC fails to keep
133 * the widget use_count balanced
135 sof_dai->configured = false;
137 return sof_widget_free(sdev, swidget);
140 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
143 * The default for SoundWire clock stop quirks is to power gate the IP
144 * and do a Bus Reset, this will need to be modified when the DSP
145 * needs to remain in D0i3 so that the Master does not lose context
146 * and enumeration is not required on clock restart
148 static int sdw_clock_stop_quirks = SDW_INTEL_CLK_STOP_BUS_RESET;
149 module_param(sdw_clock_stop_quirks, int, 0444);
150 MODULE_PARM_DESC(sdw_clock_stop_quirks, "SOF SoundWire clock stop quirks");
152 static int sdw_dai_config_ipc(struct snd_sof_dev *sdev,
153 struct snd_soc_dapm_widget *w,
154 int link_id, int alh_stream_id, int dai_id, bool setup)
156 struct snd_sof_widget *swidget = w->dobj.private;
157 struct sof_ipc_dai_config *config;
158 struct snd_sof_dai *sof_dai;
161 dev_err(sdev->dev, "error: No private data for widget %s\n", w->name);
165 sof_dai = swidget->private;
167 if (!sof_dai || !sof_dai->dai_config) {
168 dev_err(sdev->dev, "error: No config for DAI %s\n", w->name);
172 config = &sof_dai->dai_config[sof_dai->current_config];
174 /* update config with link and stream ID */
175 config->dai_index = (link_id << 8) | dai_id;
176 config->alh.stream_id = alh_stream_id;
179 return hda_ctrl_dai_widget_setup(w, SOF_DAI_CONFIG_FLAGS_NONE);
181 return hda_ctrl_dai_widget_free(w, SOF_DAI_CONFIG_FLAGS_NONE);
184 static int sdw_params_stream(struct device *dev,
185 struct sdw_intel_stream_params_data *params_data)
187 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
188 struct snd_soc_dai *d = params_data->dai;
189 struct snd_soc_dapm_widget *w;
191 w = snd_soc_dai_get_widget(d, params_data->stream);
193 return sdw_dai_config_ipc(sdev, w, params_data->link_id, params_data->alh_stream_id,
197 static int sdw_free_stream(struct device *dev,
198 struct sdw_intel_stream_free_data *free_data)
200 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
201 struct snd_soc_dai *d = free_data->dai;
202 struct snd_soc_dapm_widget *w;
204 w = snd_soc_dai_get_widget(d, free_data->stream);
206 /* send invalid stream_id */
207 return sdw_dai_config_ipc(sdev, w, free_data->link_id, 0xFFFF, d->id, false);
210 static const struct sdw_intel_ops sdw_callback = {
211 .params_stream = sdw_params_stream,
212 .free_stream = sdw_free_stream,
215 void hda_sdw_int_enable(struct snd_sof_dev *sdev, bool enable)
217 sdw_intel_enable_irq(sdev->bar[HDA_DSP_BAR], enable);
220 static int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
222 struct sof_intel_hda_dev *hdev;
226 handle = ACPI_HANDLE(sdev->dev);
228 /* save ACPI info for the probe step */
229 hdev = sdev->pdata->hw_pdata;
231 ret = sdw_intel_acpi_scan(handle, &hdev->info);
238 static int hda_sdw_probe(struct snd_sof_dev *sdev)
240 struct sof_intel_hda_dev *hdev;
241 struct sdw_intel_res res;
244 hdev = sdev->pdata->hw_pdata;
246 memset(&res, 0, sizeof(res));
248 res.mmio_base = sdev->bar[HDA_DSP_BAR];
249 res.shim_base = hdev->desc->sdw_shim_base;
250 res.alh_base = hdev->desc->sdw_alh_base;
251 res.irq = sdev->ipc_irq;
252 res.handle = hdev->info.handle;
253 res.parent = sdev->dev;
254 res.ops = &sdw_callback;
256 res.clock_stop_quirks = sdw_clock_stop_quirks;
259 * ops and arg fields are not populated for now,
260 * they will be needed when the DAI callbacks are
264 /* we could filter links here if needed, e.g for quirks */
265 res.count = hdev->info.count;
266 res.link_mask = hdev->info.link_mask;
268 sdw = sdw_intel_probe(&res);
270 dev_err(sdev->dev, "error: SoundWire probe failed\n");
280 int hda_sdw_startup(struct snd_sof_dev *sdev)
282 struct sof_intel_hda_dev *hdev;
283 struct snd_sof_pdata *pdata = sdev->pdata;
285 hdev = sdev->pdata->hw_pdata;
290 if (pdata->machine && !pdata->machine->mach_params.link_mask)
293 return sdw_intel_startup(hdev->sdw);
296 static int hda_sdw_exit(struct snd_sof_dev *sdev)
298 struct sof_intel_hda_dev *hdev;
300 hdev = sdev->pdata->hw_pdata;
302 hda_sdw_int_enable(sdev, false);
305 sdw_intel_exit(hdev->sdw);
311 bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev)
313 struct sof_intel_hda_dev *hdev;
317 hdev = sdev->pdata->hw_pdata;
323 irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2);
325 /* invalid message ? */
326 if (irq_status == 0xffffffff)
330 if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW)
337 static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
339 const struct sof_intel_dsp_desc *chip;
341 chip = get_chip_info(sdev->pdata);
342 if (chip && chip->check_sdw_irq)
343 return chip->check_sdw_irq(sdev);
348 static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
350 return sdw_intel_thread(irq, context);
353 static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
355 struct sof_intel_hda_dev *hdev;
357 hdev = sdev->pdata->hw_pdata;
359 snd_sof_dsp_read(sdev, HDA_DSP_BAR,
360 hdev->desc->sdw_shim_base + SDW_SHIM_WAKESTS))
366 void hda_sdw_process_wakeen(struct snd_sof_dev *sdev)
368 struct sof_intel_hda_dev *hdev;
370 hdev = sdev->pdata->hw_pdata;
374 sdw_intel_process_wakeen_event(hdev->sdw);
377 #else /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
378 static inline int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
383 static inline int hda_sdw_probe(struct snd_sof_dev *sdev)
388 static inline int hda_sdw_exit(struct snd_sof_dev *sdev)
393 static inline bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
398 static inline irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
403 static inline bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
408 #endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
414 struct hda_dsp_msg_code {
419 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
420 static bool hda_use_msi = true;
421 module_param_named(use_msi, hda_use_msi, bool, 0444);
422 MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode");
424 #define hda_use_msi (1)
427 int sof_hda_position_quirk = SOF_HDA_POSITION_QUIRK_USE_DPIB_REGISTERS;
428 module_param_named(position_quirk, sof_hda_position_quirk, int, 0444);
429 MODULE_PARM_DESC(position_quirk, "SOF HDaudio position quirk");
431 static char *hda_model;
432 module_param(hda_model, charp, 0444);
433 MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
435 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
436 static int hda_dmic_num = -1;
437 module_param_named(dmic_num, hda_dmic_num, int, 0444);
438 MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number");
441 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
442 static bool hda_codec_use_common_hdmi = IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI);
443 module_param_named(use_common_hdmi, hda_codec_use_common_hdmi, bool, 0444);
444 MODULE_PARM_DESC(use_common_hdmi, "SOF HDA use common HDMI codec driver");
447 static const struct hda_dsp_msg_code hda_dsp_rom_msg[] = {
448 {HDA_DSP_ROM_FW_MANIFEST_LOADED, "status: manifest loaded"},
449 {HDA_DSP_ROM_FW_FW_LOADED, "status: fw loaded"},
450 {HDA_DSP_ROM_FW_ENTERED, "status: fw entered"},
451 {HDA_DSP_ROM_CSE_ERROR, "error: cse error"},
452 {HDA_DSP_ROM_CSE_WRONG_RESPONSE, "error: cse wrong response"},
453 {HDA_DSP_ROM_IMR_TO_SMALL, "error: IMR too small"},
454 {HDA_DSP_ROM_BASE_FW_NOT_FOUND, "error: base fw not found"},
455 {HDA_DSP_ROM_CSE_VALIDATION_FAILED, "error: signature verification failed"},
456 {HDA_DSP_ROM_IPC_FATAL_ERROR, "error: ipc fatal error"},
457 {HDA_DSP_ROM_L2_CACHE_ERROR, "error: L2 cache error"},
458 {HDA_DSP_ROM_LOAD_OFFSET_TO_SMALL, "error: load offset too small"},
459 {HDA_DSP_ROM_API_PTR_INVALID, "error: API ptr invalid"},
460 {HDA_DSP_ROM_BASEFW_INCOMPAT, "error: base fw incompatible"},
461 {HDA_DSP_ROM_UNHANDLED_INTERRUPT, "error: unhandled interrupt"},
462 {HDA_DSP_ROM_MEMORY_HOLE_ECC, "error: ECC memory hole"},
463 {HDA_DSP_ROM_KERNEL_EXCEPTION, "error: kernel exception"},
464 {HDA_DSP_ROM_USER_EXCEPTION, "error: user exception"},
465 {HDA_DSP_ROM_UNEXPECTED_RESET, "error: unexpected reset"},
466 {HDA_DSP_ROM_NULL_FW_ENTRY, "error: null FW entry point"},
469 static void hda_dsp_get_status(struct snd_sof_dev *sdev, const char *level)
474 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
475 HDA_DSP_SRAM_REG_ROM_STATUS);
477 for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
478 if (status == hda_dsp_rom_msg[i].code) {
479 dev_printk(level, sdev->dev, "%s - code %8.8x\n",
480 hda_dsp_rom_msg[i].msg, status);
485 /* not for us, must be generic sof message */
486 dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
489 static void hda_dsp_get_registers(struct snd_sof_dev *sdev,
490 struct sof_ipc_dsp_oops_xtensa *xoops,
491 struct sof_ipc_panic_info *panic_info,
492 u32 *stack, size_t stack_words)
494 u32 offset = sdev->dsp_oops_offset;
496 /* first read registers */
497 sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
499 /* note: variable AR register array is not read */
501 /* then get panic info */
502 if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
503 dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
504 xoops->arch_hdr.totalsize);
507 offset += xoops->arch_hdr.totalsize;
508 sof_block_read(sdev, sdev->mmio_bar, offset,
509 panic_info, sizeof(*panic_info));
511 /* then get the stack */
512 offset += sizeof(*panic_info);
513 sof_block_read(sdev, sdev->mmio_bar, offset, stack,
514 stack_words * sizeof(u32));
517 /* dump the first 8 dwords representing the extended ROM status */
518 static void hda_dsp_dump_ext_rom_status(struct snd_sof_dev *sdev, const char *level,
526 for (i = 0; i < HDA_EXT_ROM_STATUS_SIZE; i++) {
527 value = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_ROM_STATUS + i * 0x4);
528 len += snprintf(msg + len, sizeof(msg) - len, " 0x%x", value);
531 dev_printk(level, sdev->dev, "extended rom status: %s", msg);
535 void hda_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
537 char *level = flags & SOF_DBG_DUMP_OPTIONAL ? KERN_DEBUG : KERN_ERR;
538 struct sof_ipc_dsp_oops_xtensa xoops;
539 struct sof_ipc_panic_info panic_info;
540 u32 stack[HDA_DSP_STACK_DUMP_SIZE];
542 /* print ROM/FW status */
543 hda_dsp_get_status(sdev, level);
545 if (flags & SOF_DBG_DUMP_REGS) {
546 u32 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_STATUS);
547 u32 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_TRACEP);
549 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
550 HDA_DSP_STACK_DUMP_SIZE);
551 sof_print_oops_and_stack(sdev, level, status, panic, &xoops,
552 &panic_info, stack, HDA_DSP_STACK_DUMP_SIZE);
554 hda_dsp_dump_ext_rom_status(sdev, level, flags);
558 void hda_ipc_irq_dump(struct snd_sof_dev *sdev)
560 struct hdac_bus *bus = sof_to_bus(sdev);
567 /* read key IRQ stats and config registers */
568 adspis = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
569 intsts = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS);
570 intctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL);
571 ppsts = snd_sof_dsp_read(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPSTS);
572 rirbsts = snd_hdac_chip_readb(bus, RIRBSTS);
574 dev_err(sdev->dev, "hda irq intsts 0x%8.8x intlctl 0x%8.8x rirb %2.2x\n",
575 intsts, intctl, rirbsts);
576 dev_err(sdev->dev, "dsp irq ppsts 0x%8.8x adspis 0x%8.8x\n", ppsts, adspis);
579 void hda_ipc_dump(struct snd_sof_dev *sdev)
585 hda_ipc_irq_dump(sdev);
587 /* read IPC status */
588 hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
589 hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
590 hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL);
592 /* dump the IPC regs */
593 /* TODO: parse the raw msg */
594 dev_err(sdev->dev, "host status 0x%8.8x dsp status 0x%8.8x mask 0x%8.8x\n",
595 hipcie, hipct, hipcctl);
598 static int hda_init(struct snd_sof_dev *sdev)
600 struct hda_bus *hbus;
601 struct hdac_bus *bus;
602 struct pci_dev *pci = to_pci_dev(sdev->dev);
605 hbus = sof_to_hbus(sdev);
606 bus = sof_to_bus(sdev);
609 sof_hda_bus_init(bus, &pci->dev);
611 if (sof_hda_position_quirk == SOF_HDA_POSITION_QUIRK_USE_DPIB_REGISTERS)
615 bus->bdl_pos_adj = 0;
618 mutex_init(&hbus->prepare_mutex);
620 hbus->mixer_assigned = -1;
621 hbus->modelname = hda_model;
623 /* initialise hdac bus */
624 bus->addr = pci_resource_start(pci, 0);
625 bus->remap_addr = pci_ioremap_bar(pci, 0);
626 if (!bus->remap_addr) {
627 dev_err(bus->dev, "error: ioremap error\n");
632 sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
634 /* init i915 and HDMI codecs */
635 ret = hda_codec_i915_init(sdev);
637 dev_warn(sdev->dev, "init of i915 and HDMI codec failed\n");
639 /* get controller capabilities */
640 ret = hda_dsp_ctrl_get_caps(sdev);
642 dev_err(sdev->dev, "error: get caps error\n");
647 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
649 static int check_nhlt_dmic(struct snd_sof_dev *sdev)
651 struct nhlt_acpi_table *nhlt;
654 nhlt = intel_nhlt_init(sdev->dev);
656 dmic_num = intel_nhlt_get_dmic_geo(sdev->dev, nhlt);
657 intel_nhlt_free(nhlt);
658 if (dmic_num >= 1 && dmic_num <= 4)
665 static const char *fixup_tplg_name(struct snd_sof_dev *sdev,
666 const char *sof_tplg_filename,
667 const char *idisp_str,
668 const char *dmic_str)
670 const char *tplg_filename = NULL;
671 char *filename, *tmp;
672 const char *split_ext;
674 filename = kstrdup(sof_tplg_filename, GFP_KERNEL);
678 /* this assumes a .tplg extension */
680 split_ext = strsep(&tmp, ".");
682 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
684 split_ext, idisp_str, dmic_str);
687 return tplg_filename;
690 static int dmic_topology_fixup(struct snd_sof_dev *sdev,
691 const char **tplg_filename,
692 const char *idisp_str,
695 const char *default_tplg_filename = *tplg_filename;
696 const char *fixed_tplg_filename;
697 const char *dmic_str;
700 /* first check NHLT for DMICs */
701 dmic_num = check_nhlt_dmic(sdev);
703 /* allow for module parameter override */
704 if (hda_dmic_num != -1) {
706 "overriding DMICs detected in NHLT tables %d by kernel param %d\n",
707 dmic_num, hda_dmic_num);
708 dmic_num = hda_dmic_num;
730 fixed_tplg_filename = fixup_tplg_name(sdev, default_tplg_filename,
731 idisp_str, dmic_str);
732 if (!fixed_tplg_filename)
735 dev_info(sdev->dev, "DMICs detected in NHLT tables: %d\n", dmic_num);
736 *dmic_found = dmic_num;
737 *tplg_filename = fixed_tplg_filename;
743 static int hda_init_caps(struct snd_sof_dev *sdev)
745 struct hdac_bus *bus = sof_to_bus(sdev);
746 struct snd_sof_pdata *pdata = sdev->pdata;
747 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
748 struct hdac_ext_link *hlink;
750 struct sof_intel_hda_dev *hdev = pdata->hw_pdata;
754 /* check if dsp is there */
756 dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
758 /* Init HDA controller after i915 init */
759 ret = hda_dsp_ctrl_init_chip(sdev, true);
761 dev_err(bus->dev, "error: init chip failed with ret: %d\n",
766 /* scan SoundWire capabilities exposed by DSDT */
767 ret = hda_sdw_acpi_scan(sdev);
769 dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
773 link_mask = hdev->info.link_mask;
775 dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n");
780 * probe/allocate SoundWire resources.
781 * The hardware configuration takes place in hda_sdw_startup
782 * after power rails are enabled.
783 * It's entirely possible to have a mix of I2S/DMIC/SoundWire
784 * devices, so we allocate the resources in all cases.
786 ret = hda_sdw_probe(sdev);
788 dev_err(sdev->dev, "error: SoundWire probe error\n");
794 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
796 snd_hdac_ext_bus_get_ml_capabilities(bus);
798 /* create codec instances */
799 hda_codec_probe_bus(sdev, hda_codec_use_common_hdmi);
801 if (!HDA_IDISP_CODEC(bus->codec_mask))
802 hda_codec_i915_display_power(sdev, false);
805 * we are done probing so decrement link counts
807 list_for_each_entry(hlink, &bus->hlink_list, list)
808 snd_hdac_ext_bus_link_put(bus, hlink);
813 static void hda_check_for_state_change(struct snd_sof_dev *sdev)
815 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
816 struct hdac_bus *bus = sof_to_bus(sdev);
817 unsigned int codec_mask;
819 codec_mask = snd_hdac_chip_readw(bus, STATESTS);
821 hda_codec_jack_check(sdev);
822 snd_hdac_chip_writew(bus, STATESTS, codec_mask);
827 static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
829 struct snd_sof_dev *sdev = context;
832 * Get global interrupt status. It includes all hardware interrupt
833 * sources in the Intel HD Audio controller.
835 if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
836 SOF_HDA_INTSTS_GIS) {
838 /* disable GIE interrupt */
839 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
841 SOF_HDA_INT_GLOBAL_EN,
844 return IRQ_WAKE_THREAD;
850 static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
852 struct snd_sof_dev *sdev = context;
853 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
855 /* deal with streams and controller first */
856 if (hda_dsp_check_stream_irq(sdev))
857 hda_dsp_stream_threaded_handler(irq, sdev);
859 if (hda_dsp_check_ipc_irq(sdev))
860 sof_ops(sdev)->irq_thread(irq, sdev);
862 if (hda_dsp_check_sdw_irq(sdev))
863 hda_dsp_sdw_thread(irq, hdev->sdw);
865 if (hda_sdw_check_wakeen_irq(sdev))
866 hda_sdw_process_wakeen(sdev);
868 hda_check_for_state_change(sdev);
870 /* enable GIE interrupt */
871 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
873 SOF_HDA_INT_GLOBAL_EN,
874 SOF_HDA_INT_GLOBAL_EN);
879 int hda_dsp_probe(struct snd_sof_dev *sdev)
881 struct pci_dev *pci = to_pci_dev(sdev->dev);
882 struct sof_intel_hda_dev *hdev;
883 struct hdac_bus *bus;
884 const struct sof_intel_dsp_desc *chip;
888 * detect DSP by checking class/subclass/prog-id information
889 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
890 * class=04 subclass 01 prog-if 00: DSP is present
891 * (and may be required e.g. for DMIC or SSP support)
892 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
894 if (pci->class == 0x040300) {
895 dev_err(sdev->dev, "error: the DSP is not enabled on this platform, aborting probe\n");
897 } else if (pci->class != 0x040100 && pci->class != 0x040380) {
898 dev_err(sdev->dev, "error: unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n", pci->class);
901 dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n", pci->class);
903 chip = get_chip_info(sdev->pdata);
905 dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
911 sdev->num_cores = chip->cores_num;
913 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
916 sdev->pdata->hw_pdata = hdev;
919 hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
922 if (IS_ERR(hdev->dmic_dev)) {
923 dev_err(sdev->dev, "error: failed to create DMIC device\n");
924 return PTR_ERR(hdev->dmic_dev);
928 * use position update IPC if either it is forced
929 * or we don't have other choice
931 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
932 hdev->no_ipc_position = 0;
934 hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
937 /* set up HDA base */
938 bus = sof_to_bus(sdev);
939 ret = hda_init(sdev);
944 sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
945 if (!sdev->bar[HDA_DSP_BAR]) {
946 dev_err(sdev->dev, "error: ioremap error\n");
951 sdev->mmio_bar = HDA_DSP_BAR;
952 sdev->mailbox_bar = HDA_DSP_BAR;
954 /* allow 64bit DMA address if supported by H/W */
955 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(64))) {
956 dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
957 dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32));
961 ret = hda_dsp_stream_init(sdev);
963 dev_err(sdev->dev, "error: failed to init streams\n");
965 * not all errors are due to memory issues, but trying
966 * to free everything does not harm
973 * let's try to enable msi firstly
974 * if it fails, use legacy interrupt mode
975 * TODO: support msi multiple vectors
977 if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
978 dev_info(sdev->dev, "use msi interrupt mode\n");
979 sdev->ipc_irq = pci_irq_vector(pci, 0);
980 /* initialised to "false" by kzalloc() */
981 sdev->msi_enabled = true;
984 if (!sdev->msi_enabled) {
985 dev_info(sdev->dev, "use legacy interrupt mode\n");
987 * in IO-APIC mode, hda->irq and ipc_irq are using the same
988 * irq number of pci->irq
990 sdev->ipc_irq = pci->irq;
993 dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
994 ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
995 hda_dsp_interrupt_thread,
996 IRQF_SHARED, "AudioDSP", sdev);
998 dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
1000 goto free_irq_vector;
1003 pci_set_master(pci);
1004 synchronize_irq(pci->irq);
1007 * clear TCSEL to clear playback on some HD Audio
1008 * codecs. PCI TCSEL is defined in the Intel manuals.
1010 snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
1012 /* init HDA capabilities */
1013 ret = hda_init_caps(sdev);
1017 /* enable ppcap interrupt */
1018 hda_dsp_ctrl_ppcap_enable(sdev, true);
1019 hda_dsp_ctrl_ppcap_int_enable(sdev, true);
1021 /* set default mailbox offset for FW ready message */
1022 sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
1024 INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
1029 free_irq(sdev->ipc_irq, sdev);
1031 if (sdev->msi_enabled)
1032 pci_free_irq_vectors(pci);
1034 hda_dsp_stream_free(sdev);
1035 /* dsp_unmap: not currently used */
1036 iounmap(sdev->bar[HDA_DSP_BAR]);
1038 platform_device_unregister(hdev->dmic_dev);
1039 iounmap(bus->remap_addr);
1040 hda_codec_i915_exit(sdev);
1045 int hda_dsp_remove(struct snd_sof_dev *sdev)
1047 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
1048 const struct sof_intel_dsp_desc *chip = hda->desc;
1049 struct hdac_bus *bus = sof_to_bus(sdev);
1050 struct pci_dev *pci = to_pci_dev(sdev->dev);
1052 /* cancel any attempt for DSP D0I3 */
1053 cancel_delayed_work_sync(&hda->d0i3_work);
1055 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1056 /* codec removal, invoke bus_device_remove */
1057 snd_hdac_ext_bus_device_remove(bus);
1062 if (!IS_ERR_OR_NULL(hda->dmic_dev))
1063 platform_device_unregister(hda->dmic_dev);
1065 /* disable DSP IRQ */
1066 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
1067 SOF_HDA_PPCTL_PIE, 0);
1069 /* disable CIE and GIE interrupts */
1070 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
1071 SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
1075 hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
1078 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
1079 SOF_HDA_PPCTL_GPROCEN, 0);
1081 free_irq(sdev->ipc_irq, sdev);
1082 if (sdev->msi_enabled)
1083 pci_free_irq_vectors(pci);
1085 hda_dsp_stream_free(sdev);
1086 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1087 snd_hdac_link_free_all(bus);
1090 iounmap(sdev->bar[HDA_DSP_BAR]);
1091 iounmap(bus->remap_addr);
1093 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1094 snd_hdac_ext_bus_exit(bus);
1096 hda_codec_i915_exit(sdev);
1101 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1102 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1103 struct snd_soc_acpi_mach **mach)
1105 struct hdac_bus *bus = sof_to_bus(sdev);
1106 struct snd_soc_acpi_mach_params *mach_params;
1107 struct snd_soc_acpi_mach *hda_mach;
1108 struct snd_sof_pdata *pdata = sdev->pdata;
1109 const char *tplg_filename;
1110 const char *idisp_str;
1116 /* codec detection */
1117 if (!bus->codec_mask) {
1118 dev_info(bus->dev, "no hda codecs found!\n");
1120 dev_info(bus->dev, "hda codecs found, mask %lx\n",
1123 for (i = 0; i < HDA_MAX_CODECS; i++) {
1124 if (bus->codec_mask & (1 << i))
1129 * If no machine driver is found, then:
1131 * generic hda machine driver can handle:
1132 * - one HDMI codec, and/or
1133 * - one external HDAudio codec
1135 if (!*mach && codec_num <= 2) {
1136 hda_mach = snd_soc_acpi_intel_hda_machines;
1138 dev_info(bus->dev, "using HDA machine driver %s now\n",
1139 hda_mach->drv_name);
1141 if (codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask))
1142 idisp_str = "-idisp";
1146 /* topology: use the info from hda_machines */
1147 tplg_filename = hda_mach->sof_tplg_filename;
1148 ret = dmic_topology_fixup(sdev, &tplg_filename, idisp_str, &dmic_num);
1152 hda_mach->mach_params.dmic_num = dmic_num;
1153 pdata->tplg_filename = tplg_filename;
1155 if (codec_num == 2) {
1157 * Prevent SoundWire links from starting when an external
1158 * HDaudio codec is used
1160 hda_mach->mach_params.link_mask = 0;
1167 /* used by hda machine driver to create dai links */
1169 mach_params = &(*mach)->mach_params;
1170 mach_params->codec_mask = bus->codec_mask;
1171 mach_params->common_hdmi_codec_drv = hda_codec_use_common_hdmi;
1175 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1176 struct snd_soc_acpi_mach **mach)
1181 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1182 /* Check if all Slaves defined on the link can be found */
1183 static bool link_slaves_found(struct snd_sof_dev *sdev,
1184 const struct snd_soc_acpi_link_adr *link,
1185 struct sdw_intel_ctx *sdw)
1187 struct hdac_bus *bus = sof_to_bus(sdev);
1188 struct sdw_intel_slave_id *ids = sdw->ids;
1189 int num_slaves = sdw->num_slaves;
1190 unsigned int part_id, link_id, unique_id, mfg_id;
1193 for (i = 0; i < link->num_adr; i++) {
1194 u64 adr = link->adr_d[i].adr;
1195 int reported_part_count = 0;
1197 mfg_id = SDW_MFG_ID(adr);
1198 part_id = SDW_PART_ID(adr);
1199 link_id = SDW_DISCO_LINK_ID(adr);
1201 for (j = 0; j < num_slaves; j++) {
1202 /* find out how many identical parts were reported on that link */
1203 if (ids[j].link_id == link_id &&
1204 ids[j].id.part_id == part_id &&
1205 ids[j].id.mfg_id == mfg_id)
1206 reported_part_count++;
1209 for (j = 0; j < num_slaves; j++) {
1210 int expected_part_count = 0;
1212 if (ids[j].link_id != link_id ||
1213 ids[j].id.part_id != part_id ||
1214 ids[j].id.mfg_id != mfg_id)
1217 /* find out how many identical parts are expected */
1218 for (k = 0; k < link->num_adr; k++) {
1219 u64 adr2 = link->adr_d[k].adr;
1220 unsigned int part_id2, link_id2, mfg_id2;
1222 mfg_id2 = SDW_MFG_ID(adr2);
1223 part_id2 = SDW_PART_ID(adr2);
1224 link_id2 = SDW_DISCO_LINK_ID(adr2);
1226 if (link_id2 == link_id &&
1227 part_id2 == part_id &&
1229 expected_part_count++;
1232 if (reported_part_count == expected_part_count) {
1234 * we have to check unique id
1235 * if there is more than one
1238 unique_id = SDW_UNIQUE_ID(adr);
1239 if (reported_part_count == 1 ||
1240 ids[j].id.unique_id == unique_id) {
1241 dev_dbg(bus->dev, "found %x at link %d\n",
1246 dev_dbg(bus->dev, "part %x reported %d expected %d on link %d, skipping\n",
1247 part_id, reported_part_count, expected_part_count, link_id);
1250 if (j == num_slaves) {
1252 "Slave %x not found\n",
1260 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1262 struct snd_sof_pdata *pdata = sdev->pdata;
1263 const struct snd_soc_acpi_link_adr *link;
1264 struct snd_soc_acpi_mach *mach;
1265 struct sof_intel_hda_dev *hdev;
1269 hdev = pdata->hw_pdata;
1270 link_mask = hdev->info.link_mask;
1273 * Select SoundWire machine driver if needed using the
1274 * alternate tables. This case deals with SoundWire-only
1275 * machines, for mixed cases with I2C/I2S the detection relies
1279 for (mach = pdata->desc->alt_machines;
1280 mach && mach->link_mask; mach++) {
1282 * On some platforms such as Up Extreme all links
1283 * are enabled but only one link can be used by
1284 * external codec. Instead of exact match of two masks,
1285 * first check whether link_mask of mach is subset of
1286 * link_mask supported by hw and then go on searching
1289 if (~link_mask & mach->link_mask)
1292 /* No need to match adr if there is no links defined */
1297 for (i = 0; i < hdev->info.count && link->num_adr;
1300 * Try next machine if any expected Slaves
1301 * are not found on this link.
1303 if (!link_slaves_found(sdev, link, hdev->sdw))
1306 /* Found if all Slaves are checked */
1307 if (i == hdev->info.count || !link->num_adr)
1310 if (mach && mach->link_mask) {
1313 mach->mach_params.links = mach->links;
1314 mach->mach_params.link_mask = mach->link_mask;
1315 mach->mach_params.platform = dev_name(sdev->dev);
1316 if (mach->sof_fw_filename)
1317 pdata->fw_filename = mach->sof_fw_filename;
1319 pdata->fw_filename = pdata->desc->default_fw_filename;
1320 pdata->tplg_filename = mach->sof_tplg_filename;
1323 * DMICs use up to 4 pins and are typically pin-muxed with SoundWire
1324 * link 2 and 3, thus we only try to enable dmics if all conditions
1326 * a) link 2 and 3 are not used by SoundWire
1327 * b) the NHLT table reports the presence of microphones
1329 if (!(mach->link_mask & GENMASK(3, 2))) {
1330 const char *tplg_filename = mach->sof_tplg_filename;
1333 ret = dmic_topology_fixup(sdev, &tplg_filename, "", &dmic_num);
1337 pdata->tplg_filename = tplg_filename;
1339 mach->mach_params.dmic_num = dmic_num;
1342 "SoundWire machine driver %s topology %s\n",
1344 pdata->tplg_filename);
1349 dev_info(sdev->dev, "No SoundWire machine driver found\n");
1355 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1361 void hda_set_mach_params(struct snd_soc_acpi_mach *mach,
1362 struct snd_sof_dev *sdev)
1364 struct snd_sof_pdata *pdata = sdev->pdata;
1365 const struct sof_dev_desc *desc = pdata->desc;
1366 struct snd_soc_acpi_mach_params *mach_params;
1368 mach_params = &mach->mach_params;
1369 mach_params->platform = dev_name(sdev->dev);
1370 mach_params->num_dai_drivers = desc->ops->num_drv;
1371 mach_params->dai_drivers = desc->ops->drv;
1374 struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev)
1376 struct snd_sof_pdata *sof_pdata = sdev->pdata;
1377 const struct sof_dev_desc *desc = sof_pdata->desc;
1378 struct snd_soc_acpi_mach *mach;
1380 mach = snd_soc_acpi_find_machine(desc->machines);
1383 * If tplg file name is overridden, use it instead of
1384 * the one set in mach table
1386 if (!sof_pdata->tplg_filename)
1387 sof_pdata->tplg_filename = mach->sof_tplg_filename;
1389 if (mach->link_mask) {
1390 mach->mach_params.links = mach->links;
1391 mach->mach_params.link_mask = mach->link_mask;
1396 * If I2S fails, try SoundWire
1399 mach = hda_sdw_machine_select(sdev);
1402 * Choose HDA generic machine driver if mach is NULL.
1403 * Otherwise, set certain mach params.
1405 hda_generic_machine_select(sdev, &mach);
1407 dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1412 int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1416 ret = snd_intel_dsp_driver_probe(pci);
1417 if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1418 dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1422 return sof_pci_probe(pci, pci_id);
1424 EXPORT_SYMBOL_NS(hda_pci_intel_probe, SND_SOC_SOF_INTEL_HDA_COMMON);
1426 MODULE_LICENSE("Dual BSD/GPL");
1427 MODULE_IMPORT_NS(SND_SOC_SOF_PCI_DEV);
1428 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
1429 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
1430 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
1431 MODULE_IMPORT_NS(SND_INTEL_SOUNDWIRE_ACPI);
1432 MODULE_IMPORT_NS(SOUNDWIRE_INTEL_INIT);