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.irq = sdev->ipc_irq;
170 res.handle = hdev->info.handle;
171 res.parent = sdev->dev;
172 res.ops = &sdw_callback;
174 res.clock_stop_quirks = sdw_clock_stop_quirks;
177 * ops and arg fields are not populated for now,
178 * they will be needed when the DAI callbacks are
182 /* we could filter links here if needed, e.g for quirks */
183 res.count = hdev->info.count;
184 res.link_mask = hdev->info.link_mask;
186 sdw = sdw_intel_probe(&res);
188 dev_err(sdev->dev, "error: SoundWire probe failed\n");
198 int hda_sdw_startup(struct snd_sof_dev *sdev)
200 struct sof_intel_hda_dev *hdev;
202 hdev = sdev->pdata->hw_pdata;
207 return sdw_intel_startup(hdev->sdw);
210 static int hda_sdw_exit(struct snd_sof_dev *sdev)
212 struct sof_intel_hda_dev *hdev;
214 hdev = sdev->pdata->hw_pdata;
216 hda_sdw_int_enable(sdev, false);
219 sdw_intel_exit(hdev->sdw);
225 bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev)
227 struct sof_intel_hda_dev *hdev;
231 hdev = sdev->pdata->hw_pdata;
237 irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2);
239 /* invalid message ? */
240 if (irq_status == 0xffffffff)
244 if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW)
251 static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
253 const struct sof_intel_dsp_desc *chip;
255 chip = get_chip_info(sdev->pdata);
256 if (chip && chip->check_sdw_irq)
257 return chip->check_sdw_irq(sdev);
262 static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
264 return sdw_intel_thread(irq, context);
267 static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
269 struct sof_intel_hda_dev *hdev;
271 hdev = sdev->pdata->hw_pdata;
273 snd_sof_dsp_read(sdev, HDA_DSP_BAR,
274 hdev->desc->sdw_shim_base + SDW_SHIM_WAKESTS))
280 void hda_sdw_process_wakeen(struct snd_sof_dev *sdev)
282 struct sof_intel_hda_dev *hdev;
284 hdev = sdev->pdata->hw_pdata;
288 sdw_intel_process_wakeen_event(hdev->sdw);
297 struct hda_dsp_msg_code {
302 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
303 static bool hda_use_msi = true;
304 module_param_named(use_msi, hda_use_msi, bool, 0444);
305 MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode");
307 #define hda_use_msi (1)
310 static char *hda_model;
311 module_param(hda_model, charp, 0444);
312 MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
314 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
315 static int hda_dmic_num = -1;
316 module_param_named(dmic_num, hda_dmic_num, int, 0444);
317 MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number");
320 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
321 static bool hda_codec_use_common_hdmi = IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI);
322 module_param_named(use_common_hdmi, hda_codec_use_common_hdmi, bool, 0444);
323 MODULE_PARM_DESC(use_common_hdmi, "SOF HDA use common HDMI codec driver");
326 static const struct hda_dsp_msg_code hda_dsp_rom_msg[] = {
327 {HDA_DSP_ROM_FW_MANIFEST_LOADED, "status: manifest loaded"},
328 {HDA_DSP_ROM_FW_FW_LOADED, "status: fw loaded"},
329 {HDA_DSP_ROM_FW_ENTERED, "status: fw entered"},
330 {HDA_DSP_ROM_CSE_ERROR, "error: cse error"},
331 {HDA_DSP_ROM_CSE_WRONG_RESPONSE, "error: cse wrong response"},
332 {HDA_DSP_ROM_IMR_TO_SMALL, "error: IMR too small"},
333 {HDA_DSP_ROM_BASE_FW_NOT_FOUND, "error: base fw not found"},
334 {HDA_DSP_ROM_CSE_VALIDATION_FAILED, "error: signature verification failed"},
335 {HDA_DSP_ROM_IPC_FATAL_ERROR, "error: ipc fatal error"},
336 {HDA_DSP_ROM_L2_CACHE_ERROR, "error: L2 cache error"},
337 {HDA_DSP_ROM_LOAD_OFFSET_TO_SMALL, "error: load offset too small"},
338 {HDA_DSP_ROM_API_PTR_INVALID, "error: API ptr invalid"},
339 {HDA_DSP_ROM_BASEFW_INCOMPAT, "error: base fw incompatible"},
340 {HDA_DSP_ROM_UNHANDLED_INTERRUPT, "error: unhandled interrupt"},
341 {HDA_DSP_ROM_MEMORY_HOLE_ECC, "error: ECC memory hole"},
342 {HDA_DSP_ROM_KERNEL_EXCEPTION, "error: kernel exception"},
343 {HDA_DSP_ROM_USER_EXCEPTION, "error: user exception"},
344 {HDA_DSP_ROM_UNEXPECTED_RESET, "error: unexpected reset"},
345 {HDA_DSP_ROM_NULL_FW_ENTRY, "error: null FW entry point"},
348 static void hda_dsp_get_status(struct snd_sof_dev *sdev)
353 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
354 HDA_DSP_SRAM_REG_ROM_STATUS);
356 for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
357 if (status == hda_dsp_rom_msg[i].code) {
358 dev_err(sdev->dev, "%s - code %8.8x\n",
359 hda_dsp_rom_msg[i].msg, status);
364 /* not for us, must be generic sof message */
365 dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
368 static void hda_dsp_get_registers(struct snd_sof_dev *sdev,
369 struct sof_ipc_dsp_oops_xtensa *xoops,
370 struct sof_ipc_panic_info *panic_info,
371 u32 *stack, size_t stack_words)
373 u32 offset = sdev->dsp_oops_offset;
375 /* first read registers */
376 sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
378 /* note: variable AR register array is not read */
380 /* then get panic info */
381 if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
382 dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
383 xoops->arch_hdr.totalsize);
386 offset += xoops->arch_hdr.totalsize;
387 sof_block_read(sdev, sdev->mmio_bar, offset,
388 panic_info, sizeof(*panic_info));
390 /* then get the stack */
391 offset += sizeof(*panic_info);
392 sof_block_read(sdev, sdev->mmio_bar, offset, stack,
393 stack_words * sizeof(u32));
396 /* dump the first 8 dwords representing the extended ROM status */
397 static void hda_dsp_dump_ext_rom_status(struct snd_sof_dev *sdev, u32 flags)
404 for (i = 0; i < HDA_EXT_ROM_STATUS_SIZE; i++) {
405 value = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_ROM_STATUS + i * 0x4);
406 len += snprintf(msg + len, sizeof(msg) - len, " 0x%x", value);
409 sof_dev_dbg_or_err(sdev->dev, flags & SOF_DBG_DUMP_FORCE_ERR_LEVEL,
410 "extended rom status: %s", msg);
414 void hda_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
416 struct sof_ipc_dsp_oops_xtensa xoops;
417 struct sof_ipc_panic_info panic_info;
418 u32 stack[HDA_DSP_STACK_DUMP_SIZE];
420 /* print ROM/FW status */
421 hda_dsp_get_status(sdev);
423 /* print panic info if FW boot is complete. Otherwise, print the extended ROM status */
424 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) {
425 u32 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_STATUS);
426 u32 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_TRACEP);
428 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
429 HDA_DSP_STACK_DUMP_SIZE);
430 snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
431 stack, HDA_DSP_STACK_DUMP_SIZE);
433 hda_dsp_dump_ext_rom_status(sdev, flags);
437 void hda_ipc_irq_dump(struct snd_sof_dev *sdev)
439 struct hdac_bus *bus = sof_to_bus(sdev);
446 /* read key IRQ stats and config registers */
447 adspis = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
448 intsts = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS);
449 intctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL);
450 ppsts = snd_sof_dsp_read(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPSTS);
451 rirbsts = snd_hdac_chip_readb(bus, RIRBSTS);
454 "error: hda irq intsts 0x%8.8x intlctl 0x%8.8x rirb %2.2x\n",
455 intsts, intctl, rirbsts);
457 "error: dsp irq ppsts 0x%8.8x adspis 0x%8.8x\n",
461 void hda_ipc_dump(struct snd_sof_dev *sdev)
467 hda_ipc_irq_dump(sdev);
469 /* read IPC status */
470 hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
471 hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
472 hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL);
474 /* dump the IPC regs */
475 /* TODO: parse the raw msg */
477 "error: host status 0x%8.8x dsp status 0x%8.8x mask 0x%8.8x\n",
478 hipcie, hipct, hipcctl);
481 static int hda_init(struct snd_sof_dev *sdev)
483 struct hda_bus *hbus;
484 struct hdac_bus *bus;
485 struct pci_dev *pci = to_pci_dev(sdev->dev);
488 hbus = sof_to_hbus(sdev);
489 bus = sof_to_bus(sdev);
492 sof_hda_bus_init(bus, &pci->dev);
495 bus->bdl_pos_adj = 0;
498 mutex_init(&hbus->prepare_mutex);
500 hbus->mixer_assigned = -1;
501 hbus->modelname = hda_model;
503 /* initialise hdac bus */
504 bus->addr = pci_resource_start(pci, 0);
505 bus->remap_addr = pci_ioremap_bar(pci, 0);
506 if (!bus->remap_addr) {
507 dev_err(bus->dev, "error: ioremap error\n");
512 sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
514 /* init i915 and HDMI codecs */
515 ret = hda_codec_i915_init(sdev);
517 dev_warn(sdev->dev, "init of i915 and HDMI codec failed\n");
519 /* get controller capabilities */
520 ret = hda_dsp_ctrl_get_caps(sdev);
522 dev_err(sdev->dev, "error: get caps error\n");
527 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
529 static int check_nhlt_dmic(struct snd_sof_dev *sdev)
531 struct nhlt_acpi_table *nhlt;
534 nhlt = intel_nhlt_init(sdev->dev);
536 dmic_num = intel_nhlt_get_dmic_geo(sdev->dev, nhlt);
537 intel_nhlt_free(nhlt);
538 if (dmic_num >= 1 && dmic_num <= 4)
545 static const char *fixup_tplg_name(struct snd_sof_dev *sdev,
546 const char *sof_tplg_filename,
547 const char *idisp_str,
548 const char *dmic_str)
550 const char *tplg_filename = NULL;
551 char *filename, *tmp;
552 const char *split_ext;
554 filename = kstrdup(sof_tplg_filename, GFP_KERNEL);
558 /* this assumes a .tplg extension */
560 split_ext = strsep(&tmp, ".");
562 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
564 split_ext, idisp_str, dmic_str);
567 return tplg_filename;
570 static int dmic_topology_fixup(struct snd_sof_dev *sdev,
571 const char **tplg_filename,
572 const char *idisp_str,
575 const char *default_tplg_filename = *tplg_filename;
576 const char *fixed_tplg_filename;
577 const char *dmic_str;
580 /* first check NHLT for DMICs */
581 dmic_num = check_nhlt_dmic(sdev);
583 /* allow for module parameter override */
584 if (hda_dmic_num != -1) {
586 "overriding DMICs detected in NHLT tables %d by kernel param %d\n",
587 dmic_num, hda_dmic_num);
588 dmic_num = hda_dmic_num;
610 fixed_tplg_filename = fixup_tplg_name(sdev, default_tplg_filename,
611 idisp_str, dmic_str);
612 if (!fixed_tplg_filename)
615 dev_info(sdev->dev, "DMICs detected in NHLT tables: %d\n", dmic_num);
616 *dmic_found = dmic_num;
617 *tplg_filename = fixed_tplg_filename;
623 static int hda_init_caps(struct snd_sof_dev *sdev)
625 struct hdac_bus *bus = sof_to_bus(sdev);
626 struct snd_sof_pdata *pdata = sdev->pdata;
627 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
628 struct hdac_ext_link *hlink;
630 struct sof_intel_hda_dev *hdev = pdata->hw_pdata;
634 /* check if dsp is there */
636 dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
638 /* Init HDA controller after i915 init */
639 ret = hda_dsp_ctrl_init_chip(sdev, true);
641 dev_err(bus->dev, "error: init chip failed with ret: %d\n",
646 /* scan SoundWire capabilities exposed by DSDT */
647 ret = hda_sdw_acpi_scan(sdev);
649 dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
653 link_mask = hdev->info.link_mask;
655 dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n");
660 * probe/allocate SoundWire resources.
661 * The hardware configuration takes place in hda_sdw_startup
662 * after power rails are enabled.
663 * It's entirely possible to have a mix of I2S/DMIC/SoundWire
664 * devices, so we allocate the resources in all cases.
666 ret = hda_sdw_probe(sdev);
668 dev_err(sdev->dev, "error: SoundWire probe error\n");
674 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
676 snd_hdac_ext_bus_get_ml_capabilities(bus);
678 /* create codec instances */
679 hda_codec_probe_bus(sdev, hda_codec_use_common_hdmi);
681 if (!HDA_IDISP_CODEC(bus->codec_mask))
682 hda_codec_i915_display_power(sdev, false);
685 * we are done probing so decrement link counts
687 list_for_each_entry(hlink, &bus->hlink_list, list)
688 snd_hdac_ext_bus_link_put(bus, hlink);
693 static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
695 struct snd_sof_dev *sdev = context;
698 * Get global interrupt status. It includes all hardware interrupt
699 * sources in the Intel HD Audio controller.
701 if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
702 SOF_HDA_INTSTS_GIS) {
704 /* disable GIE interrupt */
705 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
707 SOF_HDA_INT_GLOBAL_EN,
710 return IRQ_WAKE_THREAD;
716 static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
718 struct snd_sof_dev *sdev = context;
719 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
721 /* deal with streams and controller first */
722 if (hda_dsp_check_stream_irq(sdev))
723 hda_dsp_stream_threaded_handler(irq, sdev);
725 if (hda_dsp_check_ipc_irq(sdev))
726 sof_ops(sdev)->irq_thread(irq, sdev);
728 if (hda_dsp_check_sdw_irq(sdev))
729 hda_dsp_sdw_thread(irq, hdev->sdw);
731 if (hda_sdw_check_wakeen_irq(sdev))
732 hda_sdw_process_wakeen(sdev);
734 /* enable GIE interrupt */
735 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
737 SOF_HDA_INT_GLOBAL_EN,
738 SOF_HDA_INT_GLOBAL_EN);
743 int hda_dsp_probe(struct snd_sof_dev *sdev)
745 struct pci_dev *pci = to_pci_dev(sdev->dev);
746 struct sof_intel_hda_dev *hdev;
747 struct hdac_bus *bus;
748 const struct sof_intel_dsp_desc *chip;
752 * detect DSP by checking class/subclass/prog-id information
753 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
754 * class=04 subclass 01 prog-if 00: DSP is present
755 * (and may be required e.g. for DMIC or SSP support)
756 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
758 if (pci->class == 0x040300) {
759 dev_err(sdev->dev, "error: the DSP is not enabled on this platform, aborting probe\n");
761 } else if (pci->class != 0x040100 && pci->class != 0x040380) {
762 dev_err(sdev->dev, "error: unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n", pci->class);
765 dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n", pci->class);
767 chip = get_chip_info(sdev->pdata);
769 dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
775 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
778 sdev->pdata->hw_pdata = hdev;
781 hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
784 if (IS_ERR(hdev->dmic_dev)) {
785 dev_err(sdev->dev, "error: failed to create DMIC device\n");
786 return PTR_ERR(hdev->dmic_dev);
790 * use position update IPC if either it is forced
791 * or we don't have other choice
793 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
794 hdev->no_ipc_position = 0;
796 hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
799 /* set up HDA base */
800 bus = sof_to_bus(sdev);
801 ret = hda_init(sdev);
806 sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
807 if (!sdev->bar[HDA_DSP_BAR]) {
808 dev_err(sdev->dev, "error: ioremap error\n");
813 sdev->mmio_bar = HDA_DSP_BAR;
814 sdev->mailbox_bar = HDA_DSP_BAR;
816 /* allow 64bit DMA address if supported by H/W */
817 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(64))) {
818 dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
819 dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32));
823 ret = hda_dsp_stream_init(sdev);
825 dev_err(sdev->dev, "error: failed to init streams\n");
827 * not all errors are due to memory issues, but trying
828 * to free everything does not harm
835 * let's try to enable msi firstly
836 * if it fails, use legacy interrupt mode
837 * TODO: support msi multiple vectors
839 if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
840 dev_info(sdev->dev, "use msi interrupt mode\n");
841 sdev->ipc_irq = pci_irq_vector(pci, 0);
842 /* initialised to "false" by kzalloc() */
843 sdev->msi_enabled = true;
846 if (!sdev->msi_enabled) {
847 dev_info(sdev->dev, "use legacy interrupt mode\n");
849 * in IO-APIC mode, hda->irq and ipc_irq are using the same
850 * irq number of pci->irq
852 sdev->ipc_irq = pci->irq;
855 dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
856 ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
857 hda_dsp_interrupt_thread,
858 IRQF_SHARED, "AudioDSP", sdev);
860 dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
862 goto free_irq_vector;
866 synchronize_irq(pci->irq);
869 * clear TCSEL to clear playback on some HD Audio
870 * codecs. PCI TCSEL is defined in the Intel manuals.
872 snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
874 /* init HDA capabilities */
875 ret = hda_init_caps(sdev);
879 /* enable ppcap interrupt */
880 hda_dsp_ctrl_ppcap_enable(sdev, true);
881 hda_dsp_ctrl_ppcap_int_enable(sdev, true);
883 /* set default mailbox offset for FW ready message */
884 sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
886 INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
891 free_irq(sdev->ipc_irq, sdev);
893 if (sdev->msi_enabled)
894 pci_free_irq_vectors(pci);
896 hda_dsp_stream_free(sdev);
897 /* dsp_unmap: not currently used */
898 iounmap(sdev->bar[HDA_DSP_BAR]);
900 platform_device_unregister(hdev->dmic_dev);
901 iounmap(bus->remap_addr);
902 hda_codec_i915_exit(sdev);
907 int hda_dsp_remove(struct snd_sof_dev *sdev)
909 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
910 struct hdac_bus *bus = sof_to_bus(sdev);
911 struct pci_dev *pci = to_pci_dev(sdev->dev);
912 const struct sof_intel_dsp_desc *chip = hda->desc;
914 /* cancel any attempt for DSP D0I3 */
915 cancel_delayed_work_sync(&hda->d0i3_work);
917 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
918 /* codec removal, invoke bus_device_remove */
919 snd_hdac_ext_bus_device_remove(bus);
924 if (!IS_ERR_OR_NULL(hda->dmic_dev))
925 platform_device_unregister(hda->dmic_dev);
927 /* disable DSP IRQ */
928 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
929 SOF_HDA_PPCTL_PIE, 0);
931 /* disable CIE and GIE interrupts */
932 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
933 SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
937 snd_sof_dsp_core_power_down(sdev, chip->host_managed_cores_mask);
940 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
941 SOF_HDA_PPCTL_GPROCEN, 0);
943 free_irq(sdev->ipc_irq, sdev);
944 if (sdev->msi_enabled)
945 pci_free_irq_vectors(pci);
947 hda_dsp_stream_free(sdev);
948 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
949 snd_hdac_link_free_all(bus);
952 iounmap(sdev->bar[HDA_DSP_BAR]);
953 iounmap(bus->remap_addr);
955 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
956 snd_hdac_ext_bus_exit(bus);
958 hda_codec_i915_exit(sdev);
963 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
964 static int hda_generic_machine_select(struct snd_sof_dev *sdev)
966 struct hdac_bus *bus = sof_to_bus(sdev);
967 struct snd_soc_acpi_mach_params *mach_params;
968 struct snd_soc_acpi_mach *hda_mach;
969 struct snd_sof_pdata *pdata = sdev->pdata;
970 const char *tplg_filename;
971 const char *idisp_str;
977 /* codec detection */
978 if (!bus->codec_mask) {
979 dev_info(bus->dev, "no hda codecs found!\n");
981 dev_info(bus->dev, "hda codecs found, mask %lx\n",
984 for (i = 0; i < HDA_MAX_CODECS; i++) {
985 if (bus->codec_mask & (1 << i))
990 * If no machine driver is found, then:
992 * generic hda machine driver can handle:
993 * - one HDMI codec, and/or
994 * - one external HDAudio codec
996 if (!pdata->machine && codec_num <= 2) {
997 hda_mach = snd_soc_acpi_intel_hda_machines;
999 dev_info(bus->dev, "using HDA machine driver %s now\n",
1000 hda_mach->drv_name);
1002 if (codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask))
1003 idisp_str = "-idisp";
1007 /* topology: use the info from hda_machines */
1008 tplg_filename = hda_mach->sof_tplg_filename;
1009 ret = dmic_topology_fixup(sdev, &tplg_filename, idisp_str, &dmic_num);
1013 hda_mach->mach_params.dmic_num = dmic_num;
1014 pdata->machine = hda_mach;
1015 pdata->tplg_filename = tplg_filename;
1019 /* used by hda machine driver to create dai links */
1020 if (pdata->machine) {
1021 mach_params = (struct snd_soc_acpi_mach_params *)
1022 &pdata->machine->mach_params;
1023 mach_params->codec_mask = bus->codec_mask;
1024 mach_params->common_hdmi_codec_drv = hda_codec_use_common_hdmi;
1030 static int hda_generic_machine_select(struct snd_sof_dev *sdev)
1036 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1037 /* Check if all Slaves defined on the link can be found */
1038 static bool link_slaves_found(struct snd_sof_dev *sdev,
1039 const struct snd_soc_acpi_link_adr *link,
1040 struct sdw_intel_ctx *sdw)
1042 struct hdac_bus *bus = sof_to_bus(sdev);
1043 struct sdw_intel_slave_id *ids = sdw->ids;
1044 int num_slaves = sdw->num_slaves;
1045 unsigned int part_id, link_id, unique_id, mfg_id;
1048 for (i = 0; i < link->num_adr; i++) {
1049 u64 adr = link->adr_d[i].adr;
1050 int reported_part_count = 0;
1052 mfg_id = SDW_MFG_ID(adr);
1053 part_id = SDW_PART_ID(adr);
1054 link_id = SDW_DISCO_LINK_ID(adr);
1056 for (j = 0; j < num_slaves; j++) {
1057 /* find out how many identical parts were reported on that link */
1058 if (ids[j].link_id == link_id &&
1059 ids[j].id.part_id == part_id &&
1060 ids[j].id.mfg_id == mfg_id)
1061 reported_part_count++;
1064 for (j = 0; j < num_slaves; j++) {
1065 int expected_part_count = 0;
1067 if (ids[j].link_id != link_id ||
1068 ids[j].id.part_id != part_id ||
1069 ids[j].id.mfg_id != mfg_id)
1072 /* find out how many identical parts are expected */
1073 for (k = 0; k < link->num_adr; k++) {
1074 u64 adr2 = link->adr_d[k].adr;
1075 unsigned int part_id2, link_id2, mfg_id2;
1077 mfg_id2 = SDW_MFG_ID(adr2);
1078 part_id2 = SDW_PART_ID(adr2);
1079 link_id2 = SDW_DISCO_LINK_ID(adr2);
1081 if (link_id2 == link_id &&
1082 part_id2 == part_id &&
1084 expected_part_count++;
1087 if (reported_part_count == expected_part_count) {
1089 * we have to check unique id
1090 * if there is more than one
1093 unique_id = SDW_UNIQUE_ID(adr);
1094 if (reported_part_count == 1 ||
1095 ids[j].id.unique_id == unique_id) {
1096 dev_dbg(bus->dev, "found %x at link %d\n",
1101 dev_dbg(bus->dev, "part %x reported %d expected %d on link %d, skipping\n",
1102 part_id, reported_part_count, expected_part_count, link_id);
1105 if (j == num_slaves) {
1107 "Slave %x not found\n",
1115 static int hda_sdw_machine_select(struct snd_sof_dev *sdev)
1117 struct snd_sof_pdata *pdata = sdev->pdata;
1118 const struct snd_soc_acpi_link_adr *link;
1119 struct snd_soc_acpi_mach *mach;
1120 struct sof_intel_hda_dev *hdev;
1124 hdev = pdata->hw_pdata;
1125 link_mask = hdev->info.link_mask;
1128 * Select SoundWire machine driver if needed using the
1129 * alternate tables. This case deals with SoundWire-only
1130 * machines, for mixed cases with I2C/I2S the detection relies
1133 if (link_mask && !pdata->machine) {
1134 for (mach = pdata->desc->alt_machines;
1135 mach && mach->link_mask; mach++) {
1137 * On some platforms such as Up Extreme all links
1138 * are enabled but only one link can be used by
1139 * external codec. Instead of exact match of two masks,
1140 * first check whether link_mask of mach is subset of
1141 * link_mask supported by hw and then go on searching
1144 if (~link_mask & mach->link_mask)
1147 /* No need to match adr if there is no links defined */
1152 for (i = 0; i < hdev->info.count && link->num_adr;
1155 * Try next machine if any expected Slaves
1156 * are not found on this link.
1158 if (!link_slaves_found(sdev, link, hdev->sdw))
1161 /* Found if all Slaves are checked */
1162 if (i == hdev->info.count || !link->num_adr)
1165 if (mach && mach->link_mask) {
1168 pdata->machine = mach;
1169 mach->mach_params.links = mach->links;
1170 mach->mach_params.link_mask = mach->link_mask;
1171 mach->mach_params.platform = dev_name(sdev->dev);
1172 if (mach->sof_fw_filename)
1173 pdata->fw_filename = mach->sof_fw_filename;
1175 pdata->fw_filename = pdata->desc->default_fw_filename;
1176 pdata->tplg_filename = mach->sof_tplg_filename;
1179 * DMICs use up to 4 pins and are typically pin-muxed with SoundWire
1180 * link 2 and 3, thus we only try to enable dmics if all conditions
1182 * a) link 2 and 3 are not used by SoundWire
1183 * b) the NHLT table reports the presence of microphones
1185 if (!(mach->link_mask & GENMASK(3, 2))) {
1186 const char *tplg_filename = mach->sof_tplg_filename;
1189 ret = dmic_topology_fixup(sdev, &tplg_filename, "", &dmic_num);
1194 pdata->tplg_filename = tplg_filename;
1196 mach->mach_params.dmic_num = dmic_num;
1199 "SoundWire machine driver %s topology %s\n",
1201 pdata->tplg_filename);
1204 "No SoundWire machine driver found\n");
1211 static int hda_sdw_machine_select(struct snd_sof_dev *sdev)
1217 void hda_set_mach_params(const struct snd_soc_acpi_mach *mach,
1218 struct snd_sof_dev *sdev)
1220 struct snd_sof_pdata *pdata = sdev->pdata;
1221 const struct sof_dev_desc *desc = pdata->desc;
1222 struct snd_soc_acpi_mach_params *mach_params;
1224 mach_params = (struct snd_soc_acpi_mach_params *)&mach->mach_params;
1225 mach_params->platform = dev_name(sdev->dev);
1226 mach_params->num_dai_drivers = desc->ops->num_drv;
1227 mach_params->dai_drivers = desc->ops->drv;
1230 void hda_machine_select(struct snd_sof_dev *sdev)
1232 struct snd_sof_pdata *sof_pdata = sdev->pdata;
1233 const struct sof_dev_desc *desc = sof_pdata->desc;
1234 struct snd_soc_acpi_mach *mach;
1236 mach = snd_soc_acpi_find_machine(desc->machines);
1239 * If tplg file name is overridden, use it instead of
1240 * the one set in mach table
1242 if (!sof_pdata->tplg_filename)
1243 sof_pdata->tplg_filename = mach->sof_tplg_filename;
1245 sof_pdata->machine = mach;
1247 if (mach->link_mask) {
1248 mach->mach_params.links = mach->links;
1249 mach->mach_params.link_mask = mach->link_mask;
1254 * If I2S fails, try SoundWire
1256 hda_sdw_machine_select(sdev);
1259 * Choose HDA generic machine driver if mach is NULL.
1260 * Otherwise, set certain mach params.
1262 hda_generic_machine_select(sdev);
1264 if (!sof_pdata->machine)
1265 dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1268 int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1272 ret = snd_intel_dsp_driver_probe(pci);
1273 if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1274 dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1278 return sof_pci_probe(pci, pci_id);
1280 EXPORT_SYMBOL_NS(hda_pci_intel_probe, SND_SOC_SOF_INTEL_HDA_COMMON);
1282 MODULE_LICENSE("Dual BSD/GPL");
1283 MODULE_IMPORT_NS(SND_SOC_SOF_PCI_DEV);
1284 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
1285 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
1286 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
1287 MODULE_IMPORT_NS(SND_INTEL_SOUNDWIRE_ACPI);
1288 MODULE_IMPORT_NS(SOUNDWIRE_INTEL_INIT);