xref: /openbmc/linux/sound/soc/sof/intel/hda.c (revision b7b3c35e)
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>
12 //
13 
14 /*
15  * Hardware interface for generic Intel audio DSP HDA IP
16  */
17 
18 #include <sound/hdaudio_ext.h>
19 #include <sound/hda_register.h>
20 
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"
31 #include "../ops.h"
32 #include "hda.h"
33 
34 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
35 #include <sound/soc-acpi-intel-match.h>
36 #endif
37 
38 /* platform specific devices */
39 #include "shim.h"
40 
41 #define EXCEPT_MAX_HDR_SIZE	0x400
42 #define HDA_EXT_ROM_STATUS_SIZE 8
43 
44 int hda_ctrl_dai_widget_setup(struct snd_soc_dapm_widget *w, unsigned int quirk_flags)
45 {
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;
52 	int ret;
53 
54 	sof_dai = swidget->private;
55 
56 	if (!sof_dai || !sof_dai->dai_config) {
57 		dev_err(sdev->dev, "No config for DAI %s\n", w->name);
58 		return -EINVAL;
59 	}
60 
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);
64 		if (ret < 0)
65 			return ret;
66 	}
67 
68 	config = &sof_dai->dai_config[sof_dai->current_config];
69 
70 	/*
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.
74 	 */
75 	ret = sof_widget_setup(sdev, swidget);
76 	if (ret < 0) {
77 		dev_err(sdev->dev, "error: failed setting up DAI widget %s\n", w->name);
78 		return ret;
79 	}
80 
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;
84 
85 
86 	/* send DAI_CONFIG IPC */
87 	ret = sof_ipc_tx_message(sdev->ipc, config->hdr.cmd, config, config->hdr.size,
88 				 &reply, sizeof(reply));
89 	if (ret < 0) {
90 		dev_err(sdev->dev, "error: failed setting DAI config for %s\n", w->name);
91 		return ret;
92 	}
93 
94 	sof_dai->configured = true;
95 
96 	return 0;
97 }
98 
99 int hda_ctrl_dai_widget_free(struct snd_soc_dapm_widget *w, unsigned int quirk_flags)
100 {
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;
107 	int ret;
108 
109 	sof_dai = swidget->private;
110 
111 	if (!sof_dai || !sof_dai->dai_config) {
112 		dev_err(sdev->dev, "error: No config to free DAI %s\n", w->name);
113 		return -EINVAL;
114 	}
115 
116 	/* nothing to do if hw_free() is called without restarting the stream after resume. */
117 	if (!sof_dai->configured)
118 		return 0;
119 
120 	config = &sof_dai->dai_config[sof_dai->current_config];
121 
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;
125 
126 	ret = sof_ipc_tx_message(sdev->ipc, config->hdr.cmd, config, config->hdr.size,
127 				 &reply, sizeof(reply));
128 	if (ret < 0)
129 		dev_err(sdev->dev, "error: failed resetting DAI config for %s\n", w->name);
130 
131 	/*
132 	 * Reset the configured_flag and free the widget even if the IPC fails to keep
133 	 * the widget use_count balanced
134 	 */
135 	sof_dai->configured = false;
136 
137 	return sof_widget_free(sdev, swidget);
138 }
139 
140 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
141 
142 /*
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
147  */
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");
151 
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)
155 {
156 	struct snd_sof_widget *swidget = w->dobj.private;
157 	struct sof_ipc_dai_config *config;
158 	struct snd_sof_dai *sof_dai;
159 
160 	if (!swidget) {
161 		dev_err(sdev->dev, "error: No private data for widget %s\n", w->name);
162 		return -EINVAL;
163 	}
164 
165 	sof_dai = swidget->private;
166 
167 	if (!sof_dai || !sof_dai->dai_config) {
168 		dev_err(sdev->dev, "error: No config for DAI %s\n", w->name);
169 		return -EINVAL;
170 	}
171 
172 	config = &sof_dai->dai_config[sof_dai->current_config];
173 
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;
177 
178 	if (setup)
179 		return hda_ctrl_dai_widget_setup(w, SOF_DAI_CONFIG_FLAGS_NONE);
180 
181 	return hda_ctrl_dai_widget_free(w, SOF_DAI_CONFIG_FLAGS_NONE);
182 }
183 
184 static int sdw_params_stream(struct device *dev,
185 			     struct sdw_intel_stream_params_data *params_data)
186 {
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;
190 
191 	w = snd_soc_dai_get_widget(d, params_data->stream);
192 
193 	return sdw_dai_config_ipc(sdev, w, params_data->link_id, params_data->alh_stream_id,
194 				  d->id, true);
195 }
196 
197 static int sdw_free_stream(struct device *dev,
198 			   struct sdw_intel_stream_free_data *free_data)
199 {
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;
203 
204 	w = snd_soc_dai_get_widget(d, free_data->stream);
205 
206 	/* send invalid stream_id */
207 	return sdw_dai_config_ipc(sdev, w, free_data->link_id, 0xFFFF, d->id, false);
208 }
209 
210 static const struct sdw_intel_ops sdw_callback = {
211 	.params_stream = sdw_params_stream,
212 	.free_stream = sdw_free_stream,
213 };
214 
215 void hda_sdw_int_enable(struct snd_sof_dev *sdev, bool enable)
216 {
217 	sdw_intel_enable_irq(sdev->bar[HDA_DSP_BAR], enable);
218 }
219 
220 static int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
221 {
222 	struct sof_intel_hda_dev *hdev;
223 	acpi_handle handle;
224 	int ret;
225 
226 	handle = ACPI_HANDLE(sdev->dev);
227 
228 	/* save ACPI info for the probe step */
229 	hdev = sdev->pdata->hw_pdata;
230 
231 	ret = sdw_intel_acpi_scan(handle, &hdev->info);
232 	if (ret < 0)
233 		return -EINVAL;
234 
235 	return 0;
236 }
237 
238 static int hda_sdw_probe(struct snd_sof_dev *sdev)
239 {
240 	struct sof_intel_hda_dev *hdev;
241 	struct sdw_intel_res res;
242 	void *sdw;
243 
244 	hdev = sdev->pdata->hw_pdata;
245 
246 	memset(&res, 0, sizeof(res));
247 
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;
255 	res.dev = sdev->dev;
256 	res.clock_stop_quirks = sdw_clock_stop_quirks;
257 
258 	/*
259 	 * ops and arg fields are not populated for now,
260 	 * they will be needed when the DAI callbacks are
261 	 * provided
262 	 */
263 
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;
267 
268 	sdw = sdw_intel_probe(&res);
269 	if (!sdw) {
270 		dev_err(sdev->dev, "error: SoundWire probe failed\n");
271 		return -EINVAL;
272 	}
273 
274 	/* save context */
275 	hdev->sdw = sdw;
276 
277 	return 0;
278 }
279 
280 int hda_sdw_startup(struct snd_sof_dev *sdev)
281 {
282 	struct sof_intel_hda_dev *hdev;
283 	struct snd_sof_pdata *pdata = sdev->pdata;
284 
285 	hdev = sdev->pdata->hw_pdata;
286 
287 	if (!hdev->sdw)
288 		return 0;
289 
290 	if (pdata->machine && !pdata->machine->mach_params.link_mask)
291 		return 0;
292 
293 	return sdw_intel_startup(hdev->sdw);
294 }
295 
296 static int hda_sdw_exit(struct snd_sof_dev *sdev)
297 {
298 	struct sof_intel_hda_dev *hdev;
299 
300 	hdev = sdev->pdata->hw_pdata;
301 
302 	hda_sdw_int_enable(sdev, false);
303 
304 	if (hdev->sdw)
305 		sdw_intel_exit(hdev->sdw);
306 	hdev->sdw = NULL;
307 
308 	return 0;
309 }
310 
311 bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev)
312 {
313 	struct sof_intel_hda_dev *hdev;
314 	bool ret = false;
315 	u32 irq_status;
316 
317 	hdev = sdev->pdata->hw_pdata;
318 
319 	if (!hdev->sdw)
320 		return ret;
321 
322 	/* store status */
323 	irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2);
324 
325 	/* invalid message ? */
326 	if (irq_status == 0xffffffff)
327 		goto out;
328 
329 	/* SDW message ? */
330 	if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW)
331 		ret = true;
332 
333 out:
334 	return ret;
335 }
336 
337 static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
338 {
339 	const struct sof_intel_dsp_desc *chip;
340 
341 	chip = get_chip_info(sdev->pdata);
342 	if (chip && chip->check_sdw_irq)
343 		return chip->check_sdw_irq(sdev);
344 
345 	return false;
346 }
347 
348 static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
349 {
350 	return sdw_intel_thread(irq, context);
351 }
352 
353 static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
354 {
355 	struct sof_intel_hda_dev *hdev;
356 
357 	hdev = sdev->pdata->hw_pdata;
358 	if (hdev->sdw &&
359 	    snd_sof_dsp_read(sdev, HDA_DSP_BAR,
360 			     hdev->desc->sdw_shim_base + SDW_SHIM_WAKESTS))
361 		return true;
362 
363 	return false;
364 }
365 
366 void hda_sdw_process_wakeen(struct snd_sof_dev *sdev)
367 {
368 	struct sof_intel_hda_dev *hdev;
369 
370 	hdev = sdev->pdata->hw_pdata;
371 	if (!hdev->sdw)
372 		return;
373 
374 	sdw_intel_process_wakeen_event(hdev->sdw);
375 }
376 
377 #else /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
378 static inline int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
379 {
380 	return 0;
381 }
382 
383 static inline int hda_sdw_probe(struct snd_sof_dev *sdev)
384 {
385 	return 0;
386 }
387 
388 static inline int hda_sdw_exit(struct snd_sof_dev *sdev)
389 {
390 	return 0;
391 }
392 
393 static inline bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
394 {
395 	return false;
396 }
397 
398 static inline irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
399 {
400 	return IRQ_HANDLED;
401 }
402 
403 static inline bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
404 {
405 	return false;
406 }
407 
408 #endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
409 
410 /*
411  * Debug
412  */
413 
414 struct hda_dsp_msg_code {
415 	u32 code;
416 	const char *msg;
417 };
418 
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");
423 #else
424 #define hda_use_msi	(1)
425 #endif
426 
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");
430 
431 static char *hda_model;
432 module_param(hda_model, charp, 0444);
433 MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
434 
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");
439 #endif
440 
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");
445 #endif
446 
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"},
467 };
468 
469 static void hda_dsp_get_status(struct snd_sof_dev *sdev, const char *level)
470 {
471 	u32 status;
472 	int i;
473 
474 	status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
475 				  HDA_DSP_SRAM_REG_ROM_STATUS);
476 
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);
481 			return;
482 		}
483 	}
484 
485 	/* not for us, must be generic sof message */
486 	dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
487 }
488 
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)
493 {
494 	u32 offset = sdev->dsp_oops_offset;
495 
496 	/* first read registers */
497 	sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
498 
499 	/* note: variable AR register array is not read */
500 
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);
505 		return;
506 	}
507 	offset += xoops->arch_hdr.totalsize;
508 	sof_block_read(sdev, sdev->mmio_bar, offset,
509 		       panic_info, sizeof(*panic_info));
510 
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));
515 }
516 
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,
519 					u32 flags)
520 {
521 	char msg[128];
522 	int len = 0;
523 	u32 value;
524 	int i;
525 
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);
529 	}
530 
531 	dev_printk(level, sdev->dev, "extended rom status: %s", msg);
532 
533 }
534 
535 void hda_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
536 {
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];
541 
542 	/* print ROM/FW status */
543 	hda_dsp_get_status(sdev, level);
544 
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);
548 
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);
553 	} else {
554 		hda_dsp_dump_ext_rom_status(sdev, level, flags);
555 	}
556 }
557 
558 void hda_ipc_irq_dump(struct snd_sof_dev *sdev)
559 {
560 	struct hdac_bus *bus = sof_to_bus(sdev);
561 	u32 adspis;
562 	u32 intsts;
563 	u32 intctl;
564 	u32 ppsts;
565 	u8 rirbsts;
566 
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);
573 
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);
577 }
578 
579 void hda_ipc_dump(struct snd_sof_dev *sdev)
580 {
581 	u32 hipcie;
582 	u32 hipct;
583 	u32 hipcctl;
584 
585 	hda_ipc_irq_dump(sdev);
586 
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);
591 
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);
596 }
597 
598 static int hda_init(struct snd_sof_dev *sdev)
599 {
600 	struct hda_bus *hbus;
601 	struct hdac_bus *bus;
602 	struct pci_dev *pci = to_pci_dev(sdev->dev);
603 	int ret;
604 
605 	hbus = sof_to_hbus(sdev);
606 	bus = sof_to_bus(sdev);
607 
608 	/* HDA bus init */
609 	sof_hda_bus_init(bus, &pci->dev);
610 
611 	if (sof_hda_position_quirk == SOF_HDA_POSITION_QUIRK_USE_DPIB_REGISTERS)
612 		bus->use_posbuf = 0;
613 	else
614 		bus->use_posbuf = 1;
615 	bus->bdl_pos_adj = 0;
616 	bus->sync_write = 1;
617 
618 	mutex_init(&hbus->prepare_mutex);
619 	hbus->pci = pci;
620 	hbus->mixer_assigned = -1;
621 	hbus->modelname = hda_model;
622 
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");
628 		return -ENXIO;
629 	}
630 
631 	/* HDA base */
632 	sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
633 
634 	/* init i915 and HDMI codecs */
635 	ret = hda_codec_i915_init(sdev);
636 	if (ret < 0)
637 		dev_warn(sdev->dev, "init of i915 and HDMI codec failed\n");
638 
639 	/* get controller capabilities */
640 	ret = hda_dsp_ctrl_get_caps(sdev);
641 	if (ret < 0)
642 		dev_err(sdev->dev, "error: get caps error\n");
643 
644 	return ret;
645 }
646 
647 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
648 
649 static int check_nhlt_dmic(struct snd_sof_dev *sdev)
650 {
651 	struct nhlt_acpi_table *nhlt;
652 	int dmic_num;
653 
654 	nhlt = intel_nhlt_init(sdev->dev);
655 	if (nhlt) {
656 		dmic_num = intel_nhlt_get_dmic_geo(sdev->dev, nhlt);
657 		intel_nhlt_free(nhlt);
658 		if (dmic_num >= 1 && dmic_num <= 4)
659 			return dmic_num;
660 	}
661 
662 	return 0;
663 }
664 
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)
669 {
670 	const char *tplg_filename = NULL;
671 	char *filename, *tmp;
672 	const char *split_ext;
673 
674 	filename = kstrdup(sof_tplg_filename, GFP_KERNEL);
675 	if (!filename)
676 		return NULL;
677 
678 	/* this assumes a .tplg extension */
679 	tmp = filename;
680 	split_ext = strsep(&tmp, ".");
681 	if (split_ext)
682 		tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
683 					       "%s%s%s.tplg",
684 					       split_ext, idisp_str, dmic_str);
685 	kfree(filename);
686 
687 	return tplg_filename;
688 }
689 
690 static int dmic_topology_fixup(struct snd_sof_dev *sdev,
691 			       const char **tplg_filename,
692 			       const char *idisp_str,
693 			       int *dmic_found)
694 {
695 	const char *default_tplg_filename = *tplg_filename;
696 	const char *fixed_tplg_filename;
697 	const char *dmic_str;
698 	int dmic_num;
699 
700 	/* first check NHLT for DMICs */
701 	dmic_num = check_nhlt_dmic(sdev);
702 
703 	/* allow for module parameter override */
704 	if (hda_dmic_num != -1) {
705 		dev_dbg(sdev->dev,
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;
709 	}
710 
711 	switch (dmic_num) {
712 	case 1:
713 		dmic_str = "-1ch";
714 		break;
715 	case 2:
716 		dmic_str = "-2ch";
717 		break;
718 	case 3:
719 		dmic_str = "-3ch";
720 		break;
721 	case 4:
722 		dmic_str = "-4ch";
723 		break;
724 	default:
725 		dmic_num = 0;
726 		dmic_str = "";
727 		break;
728 	}
729 
730 	fixed_tplg_filename = fixup_tplg_name(sdev, default_tplg_filename,
731 					      idisp_str, dmic_str);
732 	if (!fixed_tplg_filename)
733 		return -ENOMEM;
734 
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;
738 
739 	return 0;
740 }
741 #endif
742 
743 static int hda_init_caps(struct snd_sof_dev *sdev)
744 {
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;
749 #endif
750 	struct sof_intel_hda_dev *hdev = pdata->hw_pdata;
751 	u32 link_mask;
752 	int ret = 0;
753 
754 	/* check if dsp is there */
755 	if (bus->ppcap)
756 		dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
757 
758 	/* Init HDA controller after i915 init */
759 	ret = hda_dsp_ctrl_init_chip(sdev, true);
760 	if (ret < 0) {
761 		dev_err(bus->dev, "error: init chip failed with ret: %d\n",
762 			ret);
763 		return ret;
764 	}
765 
766 	/* scan SoundWire capabilities exposed by DSDT */
767 	ret = hda_sdw_acpi_scan(sdev);
768 	if (ret < 0) {
769 		dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
770 		goto skip_soundwire;
771 	}
772 
773 	link_mask = hdev->info.link_mask;
774 	if (!link_mask) {
775 		dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n");
776 		goto skip_soundwire;
777 	}
778 
779 	/*
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.
785 	 */
786 	ret = hda_sdw_probe(sdev);
787 	if (ret < 0) {
788 		dev_err(sdev->dev, "error: SoundWire probe error\n");
789 		return ret;
790 	}
791 
792 skip_soundwire:
793 
794 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
795 	if (bus->mlcap)
796 		snd_hdac_ext_bus_get_ml_capabilities(bus);
797 
798 	/* create codec instances */
799 	hda_codec_probe_bus(sdev, hda_codec_use_common_hdmi);
800 
801 	if (!HDA_IDISP_CODEC(bus->codec_mask))
802 		hda_codec_i915_display_power(sdev, false);
803 
804 	/*
805 	 * we are done probing so decrement link counts
806 	 */
807 	list_for_each_entry(hlink, &bus->hlink_list, list)
808 		snd_hdac_ext_bus_link_put(bus, hlink);
809 #endif
810 	return 0;
811 }
812 
813 static void hda_check_for_state_change(struct snd_sof_dev *sdev)
814 {
815 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
816 	struct hdac_bus *bus = sof_to_bus(sdev);
817 	unsigned int codec_mask;
818 
819 	codec_mask = snd_hdac_chip_readw(bus, STATESTS);
820 	if (codec_mask) {
821 		hda_codec_jack_check(sdev);
822 		snd_hdac_chip_writew(bus, STATESTS, codec_mask);
823 	}
824 #endif
825 }
826 
827 static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
828 {
829 	struct snd_sof_dev *sdev = context;
830 
831 	/*
832 	 * Get global interrupt status. It includes all hardware interrupt
833 	 * sources in the Intel HD Audio controller.
834 	 */
835 	if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
836 	    SOF_HDA_INTSTS_GIS) {
837 
838 		/* disable GIE interrupt */
839 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
840 					SOF_HDA_INTCTL,
841 					SOF_HDA_INT_GLOBAL_EN,
842 					0);
843 
844 		return IRQ_WAKE_THREAD;
845 	}
846 
847 	return IRQ_NONE;
848 }
849 
850 static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
851 {
852 	struct snd_sof_dev *sdev = context;
853 	struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
854 
855 	/* deal with streams and controller first */
856 	if (hda_dsp_check_stream_irq(sdev))
857 		hda_dsp_stream_threaded_handler(irq, sdev);
858 
859 	if (hda_dsp_check_ipc_irq(sdev))
860 		sof_ops(sdev)->irq_thread(irq, sdev);
861 
862 	if (hda_dsp_check_sdw_irq(sdev))
863 		hda_dsp_sdw_thread(irq, hdev->sdw);
864 
865 	if (hda_sdw_check_wakeen_irq(sdev))
866 		hda_sdw_process_wakeen(sdev);
867 
868 	hda_check_for_state_change(sdev);
869 
870 	/* enable GIE interrupt */
871 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
872 				SOF_HDA_INTCTL,
873 				SOF_HDA_INT_GLOBAL_EN,
874 				SOF_HDA_INT_GLOBAL_EN);
875 
876 	return IRQ_HANDLED;
877 }
878 
879 int hda_dsp_probe(struct snd_sof_dev *sdev)
880 {
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;
885 	int ret = 0;
886 
887 	/*
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
893 	 */
894 	if (pci->class == 0x040300) {
895 		dev_err(sdev->dev, "error: the DSP is not enabled on this platform, aborting probe\n");
896 		return -ENODEV;
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);
899 		return -ENODEV;
900 	}
901 	dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n", pci->class);
902 
903 	chip = get_chip_info(sdev->pdata);
904 	if (!chip) {
905 		dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
906 			pci->device);
907 		ret = -EIO;
908 		goto err;
909 	}
910 
911 	sdev->num_cores = chip->cores_num;
912 
913 	hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
914 	if (!hdev)
915 		return -ENOMEM;
916 	sdev->pdata->hw_pdata = hdev;
917 	hdev->desc = chip;
918 
919 	hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
920 						       PLATFORM_DEVID_NONE,
921 						       NULL, 0);
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);
925 	}
926 
927 	/*
928 	 * use position update IPC if either it is forced
929 	 * or we don't have other choice
930 	 */
931 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
932 	hdev->no_ipc_position = 0;
933 #else
934 	hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
935 #endif
936 
937 	/* set up HDA base */
938 	bus = sof_to_bus(sdev);
939 	ret = hda_init(sdev);
940 	if (ret < 0)
941 		goto hdac_bus_unmap;
942 
943 	/* DSP base */
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");
947 		ret = -ENXIO;
948 		goto hdac_bus_unmap;
949 	}
950 
951 	sdev->mmio_bar = HDA_DSP_BAR;
952 	sdev->mailbox_bar = HDA_DSP_BAR;
953 
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));
958 	}
959 
960 	/* init streams */
961 	ret = hda_dsp_stream_init(sdev);
962 	if (ret < 0) {
963 		dev_err(sdev->dev, "error: failed to init streams\n");
964 		/*
965 		 * not all errors are due to memory issues, but trying
966 		 * to free everything does not harm
967 		 */
968 		goto free_streams;
969 	}
970 
971 	/*
972 	 * register our IRQ
973 	 * let's try to enable msi firstly
974 	 * if it fails, use legacy interrupt mode
975 	 * TODO: support msi multiple vectors
976 	 */
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;
982 	}
983 
984 	if (!sdev->msi_enabled) {
985 		dev_info(sdev->dev, "use legacy interrupt mode\n");
986 		/*
987 		 * in IO-APIC mode, hda->irq and ipc_irq are using the same
988 		 * irq number of pci->irq
989 		 */
990 		sdev->ipc_irq = pci->irq;
991 	}
992 
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);
997 	if (ret < 0) {
998 		dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
999 			sdev->ipc_irq);
1000 		goto free_irq_vector;
1001 	}
1002 
1003 	pci_set_master(pci);
1004 	synchronize_irq(pci->irq);
1005 
1006 	/*
1007 	 * clear TCSEL to clear playback on some HD Audio
1008 	 * codecs. PCI TCSEL is defined in the Intel manuals.
1009 	 */
1010 	snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
1011 
1012 	/* init HDA capabilities */
1013 	ret = hda_init_caps(sdev);
1014 	if (ret < 0)
1015 		goto free_ipc_irq;
1016 
1017 	/* enable ppcap interrupt */
1018 	hda_dsp_ctrl_ppcap_enable(sdev, true);
1019 	hda_dsp_ctrl_ppcap_int_enable(sdev, true);
1020 
1021 	/* set default mailbox offset for FW ready message */
1022 	sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
1023 
1024 	INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
1025 
1026 	return 0;
1027 
1028 free_ipc_irq:
1029 	free_irq(sdev->ipc_irq, sdev);
1030 free_irq_vector:
1031 	if (sdev->msi_enabled)
1032 		pci_free_irq_vectors(pci);
1033 free_streams:
1034 	hda_dsp_stream_free(sdev);
1035 /* dsp_unmap: not currently used */
1036 	iounmap(sdev->bar[HDA_DSP_BAR]);
1037 hdac_bus_unmap:
1038 	platform_device_unregister(hdev->dmic_dev);
1039 	iounmap(bus->remap_addr);
1040 	hda_codec_i915_exit(sdev);
1041 err:
1042 	return ret;
1043 }
1044 
1045 int hda_dsp_remove(struct snd_sof_dev *sdev)
1046 {
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);
1051 
1052 	/* cancel any attempt for DSP D0I3 */
1053 	cancel_delayed_work_sync(&hda->d0i3_work);
1054 
1055 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1056 	/* codec removal, invoke bus_device_remove */
1057 	snd_hdac_ext_bus_device_remove(bus);
1058 #endif
1059 
1060 	hda_sdw_exit(sdev);
1061 
1062 	if (!IS_ERR_OR_NULL(hda->dmic_dev))
1063 		platform_device_unregister(hda->dmic_dev);
1064 
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);
1068 
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);
1072 
1073 	/* disable cores */
1074 	if (chip)
1075 		hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
1076 
1077 	/* disable DSP */
1078 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
1079 				SOF_HDA_PPCTL_GPROCEN, 0);
1080 
1081 	free_irq(sdev->ipc_irq, sdev);
1082 	if (sdev->msi_enabled)
1083 		pci_free_irq_vectors(pci);
1084 
1085 	hda_dsp_stream_free(sdev);
1086 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1087 	snd_hdac_link_free_all(bus);
1088 #endif
1089 
1090 	iounmap(sdev->bar[HDA_DSP_BAR]);
1091 	iounmap(bus->remap_addr);
1092 
1093 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1094 	snd_hdac_ext_bus_exit(bus);
1095 #endif
1096 	hda_codec_i915_exit(sdev);
1097 
1098 	return 0;
1099 }
1100 
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)
1104 {
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;
1111 	int dmic_num = 0;
1112 	int codec_num = 0;
1113 	int ret;
1114 	int i;
1115 
1116 	/* codec detection */
1117 	if (!bus->codec_mask) {
1118 		dev_info(bus->dev, "no hda codecs found!\n");
1119 	} else {
1120 		dev_info(bus->dev, "hda codecs found, mask %lx\n",
1121 			 bus->codec_mask);
1122 
1123 		for (i = 0; i < HDA_MAX_CODECS; i++) {
1124 			if (bus->codec_mask & (1 << i))
1125 				codec_num++;
1126 		}
1127 
1128 		/*
1129 		 * If no machine driver is found, then:
1130 		 *
1131 		 * generic hda machine driver can handle:
1132 		 *  - one HDMI codec, and/or
1133 		 *  - one external HDAudio codec
1134 		 */
1135 		if (!*mach && codec_num <= 2) {
1136 			hda_mach = snd_soc_acpi_intel_hda_machines;
1137 
1138 			dev_info(bus->dev, "using HDA machine driver %s now\n",
1139 				 hda_mach->drv_name);
1140 
1141 			if (codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask))
1142 				idisp_str = "-idisp";
1143 			else
1144 				idisp_str = "";
1145 
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);
1149 			if (ret < 0)
1150 				return;
1151 
1152 			hda_mach->mach_params.dmic_num = dmic_num;
1153 			pdata->tplg_filename = tplg_filename;
1154 
1155 			if (codec_num == 2) {
1156 				/*
1157 				 * Prevent SoundWire links from starting when an external
1158 				 * HDaudio codec is used
1159 				 */
1160 				hda_mach->mach_params.link_mask = 0;
1161 			}
1162 
1163 			*mach = hda_mach;
1164 		}
1165 	}
1166 
1167 	/* used by hda machine driver to create dai links */
1168 	if (*mach) {
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;
1172 	}
1173 }
1174 #else
1175 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1176 				       struct snd_soc_acpi_mach **mach)
1177 {
1178 }
1179 #endif
1180 
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)
1186 {
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;
1191 	int i, j, k;
1192 
1193 	for (i = 0; i < link->num_adr; i++) {
1194 		u64 adr = link->adr_d[i].adr;
1195 		int reported_part_count = 0;
1196 
1197 		mfg_id = SDW_MFG_ID(adr);
1198 		part_id = SDW_PART_ID(adr);
1199 		link_id = SDW_DISCO_LINK_ID(adr);
1200 
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++;
1207 		}
1208 
1209 		for (j = 0; j < num_slaves; j++) {
1210 			int expected_part_count = 0;
1211 
1212 			if (ids[j].link_id != link_id ||
1213 			    ids[j].id.part_id != part_id ||
1214 			    ids[j].id.mfg_id != mfg_id)
1215 				continue;
1216 
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;
1221 
1222 				mfg_id2 = SDW_MFG_ID(adr2);
1223 				part_id2 = SDW_PART_ID(adr2);
1224 				link_id2 = SDW_DISCO_LINK_ID(adr2);
1225 
1226 				if (link_id2 == link_id &&
1227 				    part_id2 == part_id &&
1228 				    mfg_id2 == mfg_id)
1229 					expected_part_count++;
1230 			}
1231 
1232 			if (reported_part_count == expected_part_count) {
1233 				/*
1234 				 * we have to check unique id
1235 				 * if there is more than one
1236 				 * Slave on the link
1237 				 */
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",
1242 						part_id, link_id);
1243 					break;
1244 				}
1245 			} else {
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);
1248 			}
1249 		}
1250 		if (j == num_slaves) {
1251 			dev_dbg(bus->dev,
1252 				"Slave %x not found\n",
1253 				part_id);
1254 			return false;
1255 		}
1256 	}
1257 	return true;
1258 }
1259 
1260 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1261 {
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;
1266 	u32 link_mask;
1267 	int i;
1268 
1269 	hdev = pdata->hw_pdata;
1270 	link_mask = hdev->info.link_mask;
1271 
1272 	/*
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
1276 	 * on the HID list.
1277 	 */
1278 	if (link_mask) {
1279 		for (mach = pdata->desc->alt_machines;
1280 		     mach && mach->link_mask; mach++) {
1281 			/*
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
1287 			 * link_adr
1288 			 */
1289 			if (~link_mask & mach->link_mask)
1290 				continue;
1291 
1292 			/* No need to match adr if there is no links defined */
1293 			if (!mach->links)
1294 				break;
1295 
1296 			link = mach->links;
1297 			for (i = 0; i < hdev->info.count && link->num_adr;
1298 			     i++, link++) {
1299 				/*
1300 				 * Try next machine if any expected Slaves
1301 				 * are not found on this link.
1302 				 */
1303 				if (!link_slaves_found(sdev, link, hdev->sdw))
1304 					break;
1305 			}
1306 			/* Found if all Slaves are checked */
1307 			if (i == hdev->info.count || !link->num_adr)
1308 				break;
1309 		}
1310 		if (mach && mach->link_mask) {
1311 			int dmic_num = 0;
1312 
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;
1318 			else
1319 				pdata->fw_filename = pdata->desc->default_fw_filename;
1320 			pdata->tplg_filename = mach->sof_tplg_filename;
1321 
1322 			/*
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
1325 			 * are true:
1326 			 * a) link 2 and 3 are not used by SoundWire
1327 			 * b) the NHLT table reports the presence of microphones
1328 			 */
1329 			if (!(mach->link_mask & GENMASK(3, 2))) {
1330 				const char *tplg_filename = mach->sof_tplg_filename;
1331 				int ret;
1332 
1333 				ret = dmic_topology_fixup(sdev, &tplg_filename, "", &dmic_num);
1334 				if (ret < 0)
1335 					return NULL;
1336 
1337 				pdata->tplg_filename = tplg_filename;
1338 			}
1339 			mach->mach_params.dmic_num = dmic_num;
1340 
1341 			dev_dbg(sdev->dev,
1342 				"SoundWire machine driver %s topology %s\n",
1343 				mach->drv_name,
1344 				pdata->tplg_filename);
1345 
1346 			return mach;
1347 		}
1348 
1349 		dev_info(sdev->dev, "No SoundWire machine driver found\n");
1350 	}
1351 
1352 	return NULL;
1353 }
1354 #else
1355 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1356 {
1357 	return NULL;
1358 }
1359 #endif
1360 
1361 void hda_set_mach_params(struct snd_soc_acpi_mach *mach,
1362 			 struct snd_sof_dev *sdev)
1363 {
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;
1367 
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;
1372 }
1373 
1374 struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev)
1375 {
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;
1379 
1380 	mach = snd_soc_acpi_find_machine(desc->machines);
1381 	if (mach) {
1382 		/*
1383 		 * If tplg file name is overridden, use it instead of
1384 		 * the one set in mach table
1385 		 */
1386 		if (!sof_pdata->tplg_filename)
1387 			sof_pdata->tplg_filename = mach->sof_tplg_filename;
1388 
1389 		if (mach->link_mask) {
1390 			mach->mach_params.links = mach->links;
1391 			mach->mach_params.link_mask = mach->link_mask;
1392 		}
1393 	}
1394 
1395 	/*
1396 	 * If I2S fails, try SoundWire
1397 	 */
1398 	if (!mach)
1399 		mach = hda_sdw_machine_select(sdev);
1400 
1401 	/*
1402 	 * Choose HDA generic machine driver if mach is NULL.
1403 	 * Otherwise, set certain mach params.
1404 	 */
1405 	hda_generic_machine_select(sdev, &mach);
1406 	if (!mach)
1407 		dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1408 
1409 	return mach;
1410 }
1411 
1412 int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1413 {
1414 	int ret;
1415 
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");
1419 		return -ENODEV;
1420 	}
1421 
1422 	return sof_pci_probe(pci, pci_id);
1423 }
1424 EXPORT_SYMBOL_NS(hda_pci_intel_probe, SND_SOC_SOF_INTEL_HDA_COMMON);
1425 
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);
1433