Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / sound / soc / sof / intel / hda.c
blob509a9b25642301bd4d8af17c8a11646e3c0bdfc3
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
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.
5 //
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
7 //
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-nhlt.h>
26 #include <sound/sof.h>
27 #include <sound/sof/xtensa.h>
28 #include "../sof-audio.h"
29 #include "../ops.h"
30 #include "hda.h"
32 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
33 #include <sound/soc-acpi-intel-match.h>
34 #endif
36 /* platform specific devices */
37 #include "shim.h"
39 #define EXCEPT_MAX_HDR_SIZE 0x400
40 #define HDA_EXT_ROM_STATUS_SIZE 8
42 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
45 * The default for SoundWire clock stop quirks is to power gate the IP
46 * and do a Bus Reset, this will need to be modified when the DSP
47 * needs to remain in D0i3 so that the Master does not lose context
48 * and enumeration is not required on clock restart
50 static int sdw_clock_stop_quirks = SDW_INTEL_CLK_STOP_BUS_RESET;
51 module_param(sdw_clock_stop_quirks, int, 0444);
52 MODULE_PARM_DESC(sdw_clock_stop_quirks, "SOF SoundWire clock stop quirks");
54 static int sdw_params_stream(struct device *dev,
55 struct sdw_intel_stream_params_data *params_data)
57 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
58 struct snd_soc_dai *d = params_data->dai;
59 struct sof_ipc_dai_config config;
60 struct sof_ipc_reply reply;
61 int link_id = params_data->link_id;
62 int alh_stream_id = params_data->alh_stream_id;
63 int ret;
64 u32 size = sizeof(config);
66 memset(&config, 0, size);
67 config.hdr.size = size;
68 config.hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
69 config.type = SOF_DAI_INTEL_ALH;
70 config.dai_index = (link_id << 8) | (d->id);
71 config.alh.stream_id = alh_stream_id;
73 /* send message to DSP */
74 ret = sof_ipc_tx_message(sdev->ipc,
75 config.hdr.cmd, &config, size, &reply,
76 sizeof(reply));
77 if (ret < 0) {
78 dev_err(sdev->dev,
79 "error: failed to set DAI hw_params for link %d dai->id %d ALH %d\n",
80 link_id, d->id, alh_stream_id);
83 return ret;
86 static int sdw_free_stream(struct device *dev,
87 struct sdw_intel_stream_free_data *free_data)
89 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
90 struct snd_soc_dai *d = free_data->dai;
91 struct sof_ipc_dai_config config;
92 struct sof_ipc_reply reply;
93 int link_id = free_data->link_id;
94 int ret;
95 u32 size = sizeof(config);
97 memset(&config, 0, size);
98 config.hdr.size = size;
99 config.hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
100 config.type = SOF_DAI_INTEL_ALH;
101 config.dai_index = (link_id << 8) | d->id;
102 config.alh.stream_id = 0xFFFF; /* invalid value on purpose */
104 /* send message to DSP */
105 ret = sof_ipc_tx_message(sdev->ipc,
106 config.hdr.cmd, &config, size, &reply,
107 sizeof(reply));
108 if (ret < 0) {
109 dev_err(sdev->dev,
110 "error: failed to free stream for link %d dai->id %d\n",
111 link_id, d->id);
114 return ret;
117 static const struct sdw_intel_ops sdw_callback = {
118 .params_stream = sdw_params_stream,
119 .free_stream = sdw_free_stream,
122 void hda_sdw_int_enable(struct snd_sof_dev *sdev, bool enable)
124 sdw_intel_enable_irq(sdev->bar[HDA_DSP_BAR], enable);
127 static int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
129 struct sof_intel_hda_dev *hdev;
130 acpi_handle handle;
131 int ret;
133 handle = ACPI_HANDLE(sdev->dev);
135 /* save ACPI info for the probe step */
136 hdev = sdev->pdata->hw_pdata;
138 ret = sdw_intel_acpi_scan(handle, &hdev->info);
139 if (ret < 0)
140 return -EINVAL;
142 return 0;
145 static int hda_sdw_probe(struct snd_sof_dev *sdev)
147 struct sof_intel_hda_dev *hdev;
148 struct sdw_intel_res res;
149 void *sdw;
151 hdev = sdev->pdata->hw_pdata;
153 memset(&res, 0, sizeof(res));
155 res.mmio_base = sdev->bar[HDA_DSP_BAR];
156 res.irq = sdev->ipc_irq;
157 res.handle = hdev->info.handle;
158 res.parent = sdev->dev;
159 res.ops = &sdw_callback;
160 res.dev = sdev->dev;
161 res.clock_stop_quirks = sdw_clock_stop_quirks;
164 * ops and arg fields are not populated for now,
165 * they will be needed when the DAI callbacks are
166 * provided
169 /* we could filter links here if needed, e.g for quirks */
170 res.count = hdev->info.count;
171 res.link_mask = hdev->info.link_mask;
173 sdw = sdw_intel_probe(&res);
174 if (!sdw) {
175 dev_err(sdev->dev, "error: SoundWire probe failed\n");
176 return -EINVAL;
179 /* save context */
180 hdev->sdw = sdw;
182 return 0;
185 int hda_sdw_startup(struct snd_sof_dev *sdev)
187 struct sof_intel_hda_dev *hdev;
189 hdev = sdev->pdata->hw_pdata;
191 if (!hdev->sdw)
192 return 0;
194 return sdw_intel_startup(hdev->sdw);
197 static int hda_sdw_exit(struct snd_sof_dev *sdev)
199 struct sof_intel_hda_dev *hdev;
201 hdev = sdev->pdata->hw_pdata;
203 hda_sdw_int_enable(sdev, false);
205 if (hdev->sdw)
206 sdw_intel_exit(hdev->sdw);
207 hdev->sdw = NULL;
209 return 0;
212 static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
214 struct sof_intel_hda_dev *hdev;
215 bool ret = false;
216 u32 irq_status;
218 hdev = sdev->pdata->hw_pdata;
220 if (!hdev->sdw)
221 return ret;
223 /* store status */
224 irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2);
226 /* invalid message ? */
227 if (irq_status == 0xffffffff)
228 goto out;
230 /* SDW message ? */
231 if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW)
232 ret = true;
234 out:
235 return ret;
238 static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
240 return sdw_intel_thread(irq, context);
243 static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
245 struct sof_intel_hda_dev *hdev;
247 hdev = sdev->pdata->hw_pdata;
248 if (hdev->sdw &&
249 snd_sof_dsp_read(sdev, HDA_DSP_BAR,
250 HDA_DSP_REG_SNDW_WAKE_STS))
251 return true;
253 return false;
256 void hda_sdw_process_wakeen(struct snd_sof_dev *sdev)
258 struct sof_intel_hda_dev *hdev;
260 hdev = sdev->pdata->hw_pdata;
261 if (!hdev->sdw)
262 return;
264 sdw_intel_process_wakeen_event(hdev->sdw);
267 #endif
270 * Debug
273 struct hda_dsp_msg_code {
274 u32 code;
275 const char *msg;
278 static bool hda_use_msi = IS_ENABLED(CONFIG_PCI);
279 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
280 module_param_named(use_msi, hda_use_msi, bool, 0444);
281 MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode");
282 #endif
284 static char *hda_model;
285 module_param(hda_model, charp, 0444);
286 MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
288 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
289 static int hda_dmic_num = -1;
290 module_param_named(dmic_num, hda_dmic_num, int, 0444);
291 MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number");
293 static bool hda_codec_use_common_hdmi = IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI);
294 module_param_named(use_common_hdmi, hda_codec_use_common_hdmi, bool, 0444);
295 MODULE_PARM_DESC(use_common_hdmi, "SOF HDA use common HDMI codec driver");
296 #endif
298 static const struct hda_dsp_msg_code hda_dsp_rom_msg[] = {
299 {HDA_DSP_ROM_FW_MANIFEST_LOADED, "status: manifest loaded"},
300 {HDA_DSP_ROM_FW_FW_LOADED, "status: fw loaded"},
301 {HDA_DSP_ROM_FW_ENTERED, "status: fw entered"},
302 {HDA_DSP_ROM_CSE_ERROR, "error: cse error"},
303 {HDA_DSP_ROM_CSE_WRONG_RESPONSE, "error: cse wrong response"},
304 {HDA_DSP_ROM_IMR_TO_SMALL, "error: IMR too small"},
305 {HDA_DSP_ROM_BASE_FW_NOT_FOUND, "error: base fw not found"},
306 {HDA_DSP_ROM_CSE_VALIDATION_FAILED, "error: signature verification failed"},
307 {HDA_DSP_ROM_IPC_FATAL_ERROR, "error: ipc fatal error"},
308 {HDA_DSP_ROM_L2_CACHE_ERROR, "error: L2 cache error"},
309 {HDA_DSP_ROM_LOAD_OFFSET_TO_SMALL, "error: load offset too small"},
310 {HDA_DSP_ROM_API_PTR_INVALID, "error: API ptr invalid"},
311 {HDA_DSP_ROM_BASEFW_INCOMPAT, "error: base fw incompatible"},
312 {HDA_DSP_ROM_UNHANDLED_INTERRUPT, "error: unhandled interrupt"},
313 {HDA_DSP_ROM_MEMORY_HOLE_ECC, "error: ECC memory hole"},
314 {HDA_DSP_ROM_KERNEL_EXCEPTION, "error: kernel exception"},
315 {HDA_DSP_ROM_USER_EXCEPTION, "error: user exception"},
316 {HDA_DSP_ROM_UNEXPECTED_RESET, "error: unexpected reset"},
317 {HDA_DSP_ROM_NULL_FW_ENTRY, "error: null FW entry point"},
320 static void hda_dsp_get_status_skl(struct snd_sof_dev *sdev)
322 u32 status;
323 int i;
325 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
326 HDA_ADSP_FW_STATUS_SKL);
328 for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
329 if (status == hda_dsp_rom_msg[i].code) {
330 dev_err(sdev->dev, "%s - code %8.8x\n",
331 hda_dsp_rom_msg[i].msg, status);
332 return;
336 /* not for us, must be generic sof message */
337 dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
340 static void hda_dsp_get_status(struct snd_sof_dev *sdev)
342 u32 status;
343 int i;
345 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
346 HDA_DSP_SRAM_REG_ROM_STATUS);
348 for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
349 if (status == hda_dsp_rom_msg[i].code) {
350 dev_err(sdev->dev, "%s - code %8.8x\n",
351 hda_dsp_rom_msg[i].msg, status);
352 return;
356 /* not for us, must be generic sof message */
357 dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
360 static void hda_dsp_get_registers(struct snd_sof_dev *sdev,
361 struct sof_ipc_dsp_oops_xtensa *xoops,
362 struct sof_ipc_panic_info *panic_info,
363 u32 *stack, size_t stack_words)
365 u32 offset = sdev->dsp_oops_offset;
367 /* first read registers */
368 sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
370 /* note: variable AR register array is not read */
372 /* then get panic info */
373 if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
374 dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
375 xoops->arch_hdr.totalsize);
376 return;
378 offset += xoops->arch_hdr.totalsize;
379 sof_block_read(sdev, sdev->mmio_bar, offset,
380 panic_info, sizeof(*panic_info));
382 /* then get the stack */
383 offset += sizeof(*panic_info);
384 sof_block_read(sdev, sdev->mmio_bar, offset, stack,
385 stack_words * sizeof(u32));
388 void hda_dsp_dump_skl(struct snd_sof_dev *sdev, u32 flags)
390 struct sof_ipc_dsp_oops_xtensa xoops;
391 struct sof_ipc_panic_info panic_info;
392 u32 stack[HDA_DSP_STACK_DUMP_SIZE];
393 u32 status, panic;
395 /* try APL specific status message types first */
396 hda_dsp_get_status_skl(sdev);
398 /* now try generic SOF status messages */
399 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
400 HDA_ADSP_ERROR_CODE_SKL);
402 /*TODO: Check: there is no define in spec, but it is used in the code*/
403 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
404 HDA_ADSP_ERROR_CODE_SKL + 0x4);
406 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) {
407 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
408 HDA_DSP_STACK_DUMP_SIZE);
409 snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
410 stack, HDA_DSP_STACK_DUMP_SIZE);
411 } else {
412 dev_err(sdev->dev, "error: status = 0x%8.8x panic = 0x%8.8x\n",
413 status, panic);
414 hda_dsp_get_status_skl(sdev);
418 /* dump the first 8 dwords representing the extended ROM status */
419 static void hda_dsp_dump_ext_rom_status(struct snd_sof_dev *sdev, u32 flags)
421 char msg[128];
422 int len = 0;
423 u32 value;
424 int i;
426 for (i = 0; i < HDA_EXT_ROM_STATUS_SIZE; i++) {
427 value = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_ROM_STATUS + i * 0x4);
428 len += snprintf(msg + len, sizeof(msg) - len, " 0x%x", value);
431 sof_dev_dbg_or_err(sdev->dev, flags & SOF_DBG_DUMP_FORCE_ERR_LEVEL,
432 "extended rom status: %s", msg);
436 void hda_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
438 struct sof_ipc_dsp_oops_xtensa xoops;
439 struct sof_ipc_panic_info panic_info;
440 u32 stack[HDA_DSP_STACK_DUMP_SIZE];
441 u32 status, panic;
443 /* try APL specific status message types first */
444 hda_dsp_get_status(sdev);
446 /* now try generic SOF status messages */
447 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
448 HDA_DSP_SRAM_REG_FW_STATUS);
449 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_TRACEP);
451 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) {
452 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
453 HDA_DSP_STACK_DUMP_SIZE);
454 snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
455 stack, HDA_DSP_STACK_DUMP_SIZE);
456 } else {
457 sof_dev_dbg_or_err(sdev->dev, flags & SOF_DBG_DUMP_FORCE_ERR_LEVEL,
458 "status = 0x%8.8x panic = 0x%8.8x\n",
459 status, panic);
461 hda_dsp_dump_ext_rom_status(sdev, flags);
462 hda_dsp_get_status(sdev);
466 void hda_ipc_irq_dump(struct snd_sof_dev *sdev)
468 struct hdac_bus *bus = sof_to_bus(sdev);
469 u32 adspis;
470 u32 intsts;
471 u32 intctl;
472 u32 ppsts;
473 u8 rirbsts;
475 /* read key IRQ stats and config registers */
476 adspis = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
477 intsts = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS);
478 intctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL);
479 ppsts = snd_sof_dsp_read(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPSTS);
480 rirbsts = snd_hdac_chip_readb(bus, RIRBSTS);
482 dev_err(sdev->dev,
483 "error: hda irq intsts 0x%8.8x intlctl 0x%8.8x rirb %2.2x\n",
484 intsts, intctl, rirbsts);
485 dev_err(sdev->dev,
486 "error: dsp irq ppsts 0x%8.8x adspis 0x%8.8x\n",
487 ppsts, adspis);
490 void hda_ipc_dump(struct snd_sof_dev *sdev)
492 u32 hipcie;
493 u32 hipct;
494 u32 hipcctl;
496 hda_ipc_irq_dump(sdev);
498 /* read IPC status */
499 hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
500 hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
501 hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL);
503 /* dump the IPC regs */
504 /* TODO: parse the raw msg */
505 dev_err(sdev->dev,
506 "error: host status 0x%8.8x dsp status 0x%8.8x mask 0x%8.8x\n",
507 hipcie, hipct, hipcctl);
510 static int hda_init(struct snd_sof_dev *sdev)
512 struct hda_bus *hbus;
513 struct hdac_bus *bus;
514 struct pci_dev *pci = to_pci_dev(sdev->dev);
515 int ret;
517 hbus = sof_to_hbus(sdev);
518 bus = sof_to_bus(sdev);
520 /* HDA bus init */
521 sof_hda_bus_init(bus, &pci->dev);
523 bus->use_posbuf = 1;
524 bus->bdl_pos_adj = 0;
525 bus->sync_write = 1;
527 mutex_init(&hbus->prepare_mutex);
528 hbus->pci = pci;
529 hbus->mixer_assigned = -1;
530 hbus->modelname = hda_model;
532 /* initialise hdac bus */
533 bus->addr = pci_resource_start(pci, 0);
534 #if IS_ENABLED(CONFIG_PCI)
535 bus->remap_addr = pci_ioremap_bar(pci, 0);
536 #endif
537 if (!bus->remap_addr) {
538 dev_err(bus->dev, "error: ioremap error\n");
539 return -ENXIO;
542 /* HDA base */
543 sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
545 /* init i915 and HDMI codecs */
546 ret = hda_codec_i915_init(sdev);
547 if (ret < 0)
548 dev_warn(sdev->dev, "init of i915 and HDMI codec failed\n");
550 /* get controller capabilities */
551 ret = hda_dsp_ctrl_get_caps(sdev);
552 if (ret < 0)
553 dev_err(sdev->dev, "error: get caps error\n");
555 return ret;
558 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
560 static int check_nhlt_dmic(struct snd_sof_dev *sdev)
562 struct nhlt_acpi_table *nhlt;
563 int dmic_num;
565 nhlt = intel_nhlt_init(sdev->dev);
566 if (nhlt) {
567 dmic_num = intel_nhlt_get_dmic_geo(sdev->dev, nhlt);
568 intel_nhlt_free(nhlt);
569 if (dmic_num >= 1 && dmic_num <= 4)
570 return dmic_num;
573 return 0;
576 static const char *fixup_tplg_name(struct snd_sof_dev *sdev,
577 const char *sof_tplg_filename,
578 const char *idisp_str,
579 const char *dmic_str)
581 const char *tplg_filename = NULL;
582 char *filename;
583 char *split_ext;
585 filename = devm_kstrdup(sdev->dev, sof_tplg_filename, GFP_KERNEL);
586 if (!filename)
587 return NULL;
589 /* this assumes a .tplg extension */
590 split_ext = strsep(&filename, ".");
591 if (split_ext) {
592 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
593 "%s%s%s.tplg",
594 split_ext, idisp_str, dmic_str);
595 if (!tplg_filename)
596 return NULL;
598 return tplg_filename;
601 #endif
603 static int hda_init_caps(struct snd_sof_dev *sdev)
605 struct hdac_bus *bus = sof_to_bus(sdev);
606 struct snd_sof_pdata *pdata = sdev->pdata;
607 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
608 struct hdac_ext_link *hlink;
609 #endif
610 struct sof_intel_hda_dev *hdev = pdata->hw_pdata;
611 u32 link_mask;
612 int ret = 0;
614 device_disable_async_suspend(bus->dev);
616 /* check if dsp is there */
617 if (bus->ppcap)
618 dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
620 /* Init HDA controller after i915 init */
621 ret = hda_dsp_ctrl_init_chip(sdev, true);
622 if (ret < 0) {
623 dev_err(bus->dev, "error: init chip failed with ret: %d\n",
624 ret);
625 return ret;
628 /* scan SoundWire capabilities exposed by DSDT */
629 ret = hda_sdw_acpi_scan(sdev);
630 if (ret < 0) {
631 dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
632 goto skip_soundwire;
635 link_mask = hdev->info.link_mask;
636 if (!link_mask) {
637 dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n");
638 goto skip_soundwire;
642 * probe/allocate SoundWire resources.
643 * The hardware configuration takes place in hda_sdw_startup
644 * after power rails are enabled.
645 * It's entirely possible to have a mix of I2S/DMIC/SoundWire
646 * devices, so we allocate the resources in all cases.
648 ret = hda_sdw_probe(sdev);
649 if (ret < 0) {
650 dev_err(sdev->dev, "error: SoundWire probe error\n");
651 return ret;
654 skip_soundwire:
656 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
657 if (bus->mlcap)
658 snd_hdac_ext_bus_get_ml_capabilities(bus);
660 /* create codec instances */
661 hda_codec_probe_bus(sdev, hda_codec_use_common_hdmi);
663 if (!HDA_IDISP_CODEC(bus->codec_mask))
664 hda_codec_i915_display_power(sdev, false);
667 * we are done probing so decrement link counts
669 list_for_each_entry(hlink, &bus->hlink_list, list)
670 snd_hdac_ext_bus_link_put(bus, hlink);
671 #endif
672 return 0;
675 static const struct sof_intel_dsp_desc
676 *get_chip_info(struct snd_sof_pdata *pdata)
678 const struct sof_dev_desc *desc = pdata->desc;
679 const struct sof_intel_dsp_desc *chip_info;
681 chip_info = desc->chip_info;
683 return chip_info;
686 static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
688 struct snd_sof_dev *sdev = context;
691 * Get global interrupt status. It includes all hardware interrupt
692 * sources in the Intel HD Audio controller.
694 if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
695 SOF_HDA_INTSTS_GIS) {
697 /* disable GIE interrupt */
698 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
699 SOF_HDA_INTCTL,
700 SOF_HDA_INT_GLOBAL_EN,
703 return IRQ_WAKE_THREAD;
706 return IRQ_NONE;
709 static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
711 struct snd_sof_dev *sdev = context;
712 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
714 /* deal with streams and controller first */
715 if (hda_dsp_check_stream_irq(sdev))
716 hda_dsp_stream_threaded_handler(irq, sdev);
718 if (hda_dsp_check_ipc_irq(sdev))
719 sof_ops(sdev)->irq_thread(irq, sdev);
721 if (hda_dsp_check_sdw_irq(sdev))
722 hda_dsp_sdw_thread(irq, hdev->sdw);
724 if (hda_sdw_check_wakeen_irq(sdev))
725 hda_sdw_process_wakeen(sdev);
727 /* enable GIE interrupt */
728 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
729 SOF_HDA_INTCTL,
730 SOF_HDA_INT_GLOBAL_EN,
731 SOF_HDA_INT_GLOBAL_EN);
733 return IRQ_HANDLED;
736 int hda_dsp_probe(struct snd_sof_dev *sdev)
738 struct pci_dev *pci = to_pci_dev(sdev->dev);
739 struct sof_intel_hda_dev *hdev;
740 struct hdac_bus *bus;
741 const struct sof_intel_dsp_desc *chip;
742 int ret = 0;
745 * detect DSP by checking class/subclass/prog-id information
746 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
747 * class=04 subclass 01 prog-if 00: DSP is present
748 * (and may be required e.g. for DMIC or SSP support)
749 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
751 if (pci->class == 0x040300) {
752 dev_err(sdev->dev, "error: the DSP is not enabled on this platform, aborting probe\n");
753 return -ENODEV;
754 } else if (pci->class != 0x040100 && pci->class != 0x040380) {
755 dev_err(sdev->dev, "error: unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n", pci->class);
756 return -ENODEV;
758 dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n", pci->class);
760 chip = get_chip_info(sdev->pdata);
761 if (!chip) {
762 dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
763 pci->device);
764 ret = -EIO;
765 goto err;
768 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
769 if (!hdev)
770 return -ENOMEM;
771 sdev->pdata->hw_pdata = hdev;
772 hdev->desc = chip;
774 hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
775 PLATFORM_DEVID_NONE,
776 NULL, 0);
777 if (IS_ERR(hdev->dmic_dev)) {
778 dev_err(sdev->dev, "error: failed to create DMIC device\n");
779 return PTR_ERR(hdev->dmic_dev);
783 * use position update IPC if either it is forced
784 * or we don't have other choice
786 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
787 hdev->no_ipc_position = 0;
788 #else
789 hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
790 #endif
792 /* set up HDA base */
793 bus = sof_to_bus(sdev);
794 ret = hda_init(sdev);
795 if (ret < 0)
796 goto hdac_bus_unmap;
798 /* DSP base */
799 #if IS_ENABLED(CONFIG_PCI)
800 sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
801 #endif
802 if (!sdev->bar[HDA_DSP_BAR]) {
803 dev_err(sdev->dev, "error: ioremap error\n");
804 ret = -ENXIO;
805 goto hdac_bus_unmap;
808 sdev->mmio_bar = HDA_DSP_BAR;
809 sdev->mailbox_bar = HDA_DSP_BAR;
811 /* allow 64bit DMA address if supported by H/W */
812 if (!dma_set_mask(&pci->dev, DMA_BIT_MASK(64))) {
813 dev_dbg(sdev->dev, "DMA mask is 64 bit\n");
814 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(64));
815 } else {
816 dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
817 dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
818 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32));
821 /* init streams */
822 ret = hda_dsp_stream_init(sdev);
823 if (ret < 0) {
824 dev_err(sdev->dev, "error: failed to init streams\n");
826 * not all errors are due to memory issues, but trying
827 * to free everything does not harm
829 goto free_streams;
833 * register our IRQ
834 * let's try to enable msi firstly
835 * if it fails, use legacy interrupt mode
836 * TODO: support msi multiple vectors
838 if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
839 dev_info(sdev->dev, "use msi interrupt mode\n");
840 sdev->ipc_irq = pci_irq_vector(pci, 0);
841 /* initialised to "false" by kzalloc() */
842 sdev->msi_enabled = true;
845 if (!sdev->msi_enabled) {
846 dev_info(sdev->dev, "use legacy interrupt mode\n");
848 * in IO-APIC mode, hda->irq and ipc_irq are using the same
849 * irq number of pci->irq
851 sdev->ipc_irq = pci->irq;
854 dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
855 ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
856 hda_dsp_interrupt_thread,
857 IRQF_SHARED, "AudioDSP", sdev);
858 if (ret < 0) {
859 dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
860 sdev->ipc_irq);
861 goto free_irq_vector;
864 pci_set_master(pci);
865 synchronize_irq(pci->irq);
868 * clear TCSEL to clear playback on some HD Audio
869 * codecs. PCI TCSEL is defined in the Intel manuals.
871 snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
873 /* init HDA capabilities */
874 ret = hda_init_caps(sdev);
875 if (ret < 0)
876 goto free_ipc_irq;
878 /* enable ppcap interrupt */
879 hda_dsp_ctrl_ppcap_enable(sdev, true);
880 hda_dsp_ctrl_ppcap_int_enable(sdev, true);
882 /* set default mailbox offset for FW ready message */
883 sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
885 INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
887 return 0;
889 free_ipc_irq:
890 free_irq(sdev->ipc_irq, sdev);
891 free_irq_vector:
892 if (sdev->msi_enabled)
893 pci_free_irq_vectors(pci);
894 free_streams:
895 hda_dsp_stream_free(sdev);
896 /* dsp_unmap: not currently used */
897 iounmap(sdev->bar[HDA_DSP_BAR]);
898 hdac_bus_unmap:
899 iounmap(bus->remap_addr);
900 hda_codec_i915_exit(sdev);
901 err:
902 return ret;
905 int hda_dsp_remove(struct snd_sof_dev *sdev)
907 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
908 struct hdac_bus *bus = sof_to_bus(sdev);
909 struct pci_dev *pci = to_pci_dev(sdev->dev);
910 const struct sof_intel_dsp_desc *chip = hda->desc;
912 /* cancel any attempt for DSP D0I3 */
913 cancel_delayed_work_sync(&hda->d0i3_work);
915 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
916 /* codec removal, invoke bus_device_remove */
917 snd_hdac_ext_bus_device_remove(bus);
918 #endif
920 hda_sdw_exit(sdev);
922 if (!IS_ERR_OR_NULL(hda->dmic_dev))
923 platform_device_unregister(hda->dmic_dev);
925 /* disable DSP IRQ */
926 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
927 SOF_HDA_PPCTL_PIE, 0);
929 /* disable CIE and GIE interrupts */
930 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
931 SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
933 /* disable cores */
934 if (chip)
935 hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
937 /* disable DSP */
938 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
939 SOF_HDA_PPCTL_GPROCEN, 0);
941 free_irq(sdev->ipc_irq, sdev);
942 if (sdev->msi_enabled)
943 pci_free_irq_vectors(pci);
945 hda_dsp_stream_free(sdev);
946 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
947 snd_hdac_link_free_all(bus);
948 #endif
950 iounmap(sdev->bar[HDA_DSP_BAR]);
951 iounmap(bus->remap_addr);
953 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
954 snd_hdac_ext_bus_exit(bus);
955 #endif
956 hda_codec_i915_exit(sdev);
958 return 0;
961 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
962 static int hda_generic_machine_select(struct snd_sof_dev *sdev)
964 struct hdac_bus *bus = sof_to_bus(sdev);
965 struct snd_soc_acpi_mach_params *mach_params;
966 struct snd_soc_acpi_mach *hda_mach;
967 struct snd_sof_pdata *pdata = sdev->pdata;
968 const char *tplg_filename;
969 const char *idisp_str;
970 const char *dmic_str;
971 int dmic_num = 0;
972 int codec_num = 0;
973 int i;
975 /* codec detection */
976 if (!bus->codec_mask) {
977 dev_info(bus->dev, "no hda codecs found!\n");
978 } else {
979 dev_info(bus->dev, "hda codecs found, mask %lx\n",
980 bus->codec_mask);
982 for (i = 0; i < HDA_MAX_CODECS; i++) {
983 if (bus->codec_mask & (1 << i))
984 codec_num++;
988 * If no machine driver is found, then:
990 * generic hda machine driver can handle:
991 * - one HDMI codec, and/or
992 * - one external HDAudio codec
994 if (!pdata->machine && codec_num <= 2) {
995 hda_mach = snd_soc_acpi_intel_hda_machines;
997 /* topology: use the info from hda_machines */
998 pdata->tplg_filename =
999 hda_mach->sof_tplg_filename;
1001 dev_info(bus->dev, "using HDA machine driver %s now\n",
1002 hda_mach->drv_name);
1004 if (codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask))
1005 idisp_str = "-idisp";
1006 else
1007 idisp_str = "";
1009 /* first check NHLT for DMICs */
1010 dmic_num = check_nhlt_dmic(sdev);
1012 /* allow for module parameter override */
1013 if (hda_dmic_num != -1)
1014 dmic_num = hda_dmic_num;
1016 switch (dmic_num) {
1017 case 1:
1018 dmic_str = "-1ch";
1019 break;
1020 case 2:
1021 dmic_str = "-2ch";
1022 break;
1023 case 3:
1024 dmic_str = "-3ch";
1025 break;
1026 case 4:
1027 dmic_str = "-4ch";
1028 break;
1029 default:
1030 dmic_num = 0;
1031 dmic_str = "";
1032 break;
1035 tplg_filename = pdata->tplg_filename;
1036 tplg_filename = fixup_tplg_name(sdev, tplg_filename,
1037 idisp_str, dmic_str);
1038 if (!tplg_filename)
1039 return -EINVAL;
1041 dev_info(bus->dev,
1042 "DMICs detected in NHLT tables: %d\n",
1043 dmic_num);
1045 pdata->machine = hda_mach;
1046 pdata->tplg_filename = tplg_filename;
1050 /* used by hda machine driver to create dai links */
1051 if (pdata->machine) {
1052 mach_params = (struct snd_soc_acpi_mach_params *)
1053 &pdata->machine->mach_params;
1054 mach_params->codec_mask = bus->codec_mask;
1055 mach_params->common_hdmi_codec_drv = hda_codec_use_common_hdmi;
1056 mach_params->dmic_num = dmic_num;
1059 return 0;
1061 #else
1062 static int hda_generic_machine_select(struct snd_sof_dev *sdev)
1064 return 0;
1066 #endif
1068 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1069 /* Check if all Slaves defined on the link can be found */
1070 static bool link_slaves_found(struct snd_sof_dev *sdev,
1071 const struct snd_soc_acpi_link_adr *link,
1072 struct sdw_intel_ctx *sdw)
1074 struct hdac_bus *bus = sof_to_bus(sdev);
1075 struct sdw_intel_slave_id *ids = sdw->ids;
1076 int num_slaves = sdw->num_slaves;
1077 unsigned int part_id, link_id, unique_id, mfg_id;
1078 int i, j;
1080 for (i = 0; i < link->num_adr; i++) {
1081 u64 adr = link->adr_d[i].adr;
1083 mfg_id = SDW_MFG_ID(adr);
1084 part_id = SDW_PART_ID(adr);
1085 link_id = SDW_DISCO_LINK_ID(adr);
1086 for (j = 0; j < num_slaves; j++) {
1087 if (ids[j].link_id != link_id ||
1088 ids[j].id.part_id != part_id ||
1089 ids[j].id.mfg_id != mfg_id)
1090 continue;
1092 * we have to check unique id
1093 * if there is more than one
1094 * Slave on the link
1096 unique_id = SDW_UNIQUE_ID(adr);
1097 if (link->num_adr == 1 ||
1098 ids[j].id.unique_id == SDW_IGNORED_UNIQUE_ID ||
1099 ids[j].id.unique_id == unique_id) {
1100 dev_dbg(bus->dev,
1101 "found %x at link %d\n",
1102 part_id, link_id);
1103 break;
1106 if (j == num_slaves) {
1107 dev_dbg(bus->dev,
1108 "Slave %x not found\n",
1109 part_id);
1110 return false;
1113 return true;
1116 static int hda_sdw_machine_select(struct snd_sof_dev *sdev)
1118 struct snd_sof_pdata *pdata = sdev->pdata;
1119 const struct snd_soc_acpi_link_adr *link;
1120 struct hdac_bus *bus = sof_to_bus(sdev);
1121 struct snd_soc_acpi_mach *mach;
1122 struct sof_intel_hda_dev *hdev;
1123 u32 link_mask;
1124 int i;
1126 hdev = pdata->hw_pdata;
1127 link_mask = hdev->info.link_mask;
1130 * Select SoundWire machine driver if needed using the
1131 * alternate tables. This case deals with SoundWire-only
1132 * machines, for mixed cases with I2C/I2S the detection relies
1133 * on the HID list.
1135 if (link_mask && !pdata->machine) {
1136 for (mach = pdata->desc->alt_machines;
1137 mach && mach->link_mask; mach++) {
1139 * On some platforms such as Up Extreme all links
1140 * are enabled but only one link can be used by
1141 * external codec. Instead of exact match of two masks,
1142 * first check whether link_mask of mach is subset of
1143 * link_mask supported by hw and then go on searching
1144 * link_adr
1146 if (~link_mask & mach->link_mask)
1147 continue;
1149 /* No need to match adr if there is no links defined */
1150 if (!mach->links)
1151 break;
1153 link = mach->links;
1154 for (i = 0; i < hdev->info.count && link->num_adr;
1155 i++, link++) {
1157 * Try next machine if any expected Slaves
1158 * are not found on this link.
1160 if (!link_slaves_found(sdev, link, hdev->sdw))
1161 break;
1163 /* Found if all Slaves are checked */
1164 if (i == hdev->info.count || !link->num_adr)
1165 break;
1167 if (mach && mach->link_mask) {
1168 dev_dbg(bus->dev,
1169 "SoundWire machine driver %s topology %s\n",
1170 mach->drv_name,
1171 mach->sof_tplg_filename);
1172 pdata->machine = mach;
1173 mach->mach_params.links = mach->links;
1174 mach->mach_params.link_mask = mach->link_mask;
1175 mach->mach_params.platform = dev_name(sdev->dev);
1176 pdata->fw_filename = mach->sof_fw_filename;
1177 pdata->tplg_filename = mach->sof_tplg_filename;
1178 } else {
1179 dev_info(sdev->dev,
1180 "No SoundWire machine driver found\n");
1184 return 0;
1186 #else
1187 static int hda_sdw_machine_select(struct snd_sof_dev *sdev)
1189 return 0;
1191 #endif
1193 void hda_set_mach_params(const struct snd_soc_acpi_mach *mach,
1194 struct device *dev)
1196 struct snd_soc_acpi_mach_params *mach_params;
1198 mach_params = (struct snd_soc_acpi_mach_params *)&mach->mach_params;
1199 mach_params->platform = dev_name(dev);
1202 void hda_machine_select(struct snd_sof_dev *sdev)
1204 struct snd_sof_pdata *sof_pdata = sdev->pdata;
1205 const struct sof_dev_desc *desc = sof_pdata->desc;
1206 struct snd_soc_acpi_mach *mach;
1208 mach = snd_soc_acpi_find_machine(desc->machines);
1209 if (mach) {
1211 * If tplg file name is overridden, use it instead of
1212 * the one set in mach table
1214 if (!sof_pdata->tplg_filename)
1215 sof_pdata->tplg_filename = mach->sof_tplg_filename;
1217 sof_pdata->machine = mach;
1219 if (mach->link_mask) {
1220 mach->mach_params.links = mach->links;
1221 mach->mach_params.link_mask = mach->link_mask;
1226 * If I2S fails, try SoundWire
1228 hda_sdw_machine_select(sdev);
1231 * Choose HDA generic machine driver if mach is NULL.
1232 * Otherwise, set certain mach params.
1234 hda_generic_machine_select(sdev);
1236 if (!sof_pdata->machine)
1237 dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1240 MODULE_LICENSE("Dual BSD/GPL");
1241 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
1242 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
1243 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
1244 MODULE_IMPORT_NS(SOUNDWIRE_INTEL_INIT);