xref: /openbmc/linux/sound/soc/sof/intel/hda.c (revision 6331b876)
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)
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 	config = &sof_dai->dai_config[sof_dai->current_config];
62 
63 	/*
64 	 * For static pipelines, the DAI widget would already be set up and calling
65 	 * sof_widget_setup() simply returns without doing anything.
66 	 * For dynamic pipelines, the DAI widget will be set up now.
67 	 */
68 	ret = sof_widget_setup(sdev, swidget);
69 	if (ret < 0) {
70 		dev_err(sdev->dev, "error: failed setting up DAI widget %s\n", w->name);
71 		return ret;
72 	}
73 
74 	/* set HW_PARAMS flag */
75 	config->flags = FIELD_PREP(SOF_DAI_CONFIG_FLAGS_MASK, SOF_DAI_CONFIG_FLAGS_HW_PARAMS);
76 
77 	/* send DAI_CONFIG IPC */
78 	ret = sof_ipc_tx_message(sdev->ipc, config->hdr.cmd, config, config->hdr.size,
79 				 &reply, sizeof(reply));
80 	if (ret < 0) {
81 		dev_err(sdev->dev, "error: failed setting DAI config for %s\n", w->name);
82 		return ret;
83 	}
84 
85 	sof_dai->configured = true;
86 
87 	return 0;
88 }
89 
90 int hda_ctrl_dai_widget_free(struct snd_soc_dapm_widget *w)
91 {
92 	struct snd_sof_widget *swidget = w->dobj.private;
93 	struct snd_soc_component *component = swidget->scomp;
94 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
95 	struct sof_ipc_dai_config *config;
96 	struct snd_sof_dai *sof_dai;
97 	struct sof_ipc_reply reply;
98 	int ret;
99 
100 	sof_dai = swidget->private;
101 
102 	if (!sof_dai || !sof_dai->dai_config) {
103 		dev_err(sdev->dev, "error: No config to free DAI %s\n", w->name);
104 		return -EINVAL;
105 	}
106 
107 	/* nothing to do if hw_free() is called without restarting the stream after resume. */
108 	if (!sof_dai->configured)
109 		return 0;
110 
111 	config = &sof_dai->dai_config[sof_dai->current_config];
112 
113 	/* set HW_FREE flag */
114 	config->flags = FIELD_PREP(SOF_DAI_CONFIG_FLAGS_MASK, SOF_DAI_CONFIG_FLAGS_HW_FREE);
115 
116 	ret = sof_ipc_tx_message(sdev->ipc, config->hdr.cmd, config, config->hdr.size,
117 				 &reply, sizeof(reply));
118 	if (ret < 0)
119 		dev_err(sdev->dev, "error: failed resetting DAI config for %s\n", w->name);
120 
121 	/*
122 	 * Reset the configured_flag and free the widget even if the IPC fails to keep
123 	 * the widget use_count balanced
124 	 */
125 	sof_dai->configured = false;
126 
127 	return sof_widget_free(sdev, swidget);
128 }
129 
130 static const struct sof_intel_dsp_desc
131 	*get_chip_info(struct snd_sof_pdata *pdata)
132 {
133 	const struct sof_dev_desc *desc = pdata->desc;
134 	const struct sof_intel_dsp_desc *chip_info;
135 
136 	chip_info = desc->chip_info;
137 
138 	return chip_info;
139 }
140 
141 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
142 
143 /*
144  * The default for SoundWire clock stop quirks is to power gate the IP
145  * and do a Bus Reset, this will need to be modified when the DSP
146  * needs to remain in D0i3 so that the Master does not lose context
147  * and enumeration is not required on clock restart
148  */
149 static int sdw_clock_stop_quirks = SDW_INTEL_CLK_STOP_BUS_RESET;
150 module_param(sdw_clock_stop_quirks, int, 0444);
151 MODULE_PARM_DESC(sdw_clock_stop_quirks, "SOF SoundWire clock stop quirks");
152 
153 static int sdw_dai_config_ipc(struct snd_sof_dev *sdev,
154 			      struct snd_soc_dapm_widget *w,
155 			      int link_id, int alh_stream_id, int dai_id, bool setup)
156 {
157 	struct snd_sof_widget *swidget = w->dobj.private;
158 	struct sof_ipc_dai_config *config;
159 	struct snd_sof_dai *sof_dai;
160 
161 	if (!swidget) {
162 		dev_err(sdev->dev, "error: No private data for widget %s\n", w->name);
163 		return -EINVAL;
164 	}
165 
166 	sof_dai = swidget->private;
167 
168 	if (!sof_dai || !sof_dai->dai_config) {
169 		dev_err(sdev->dev, "error: No config for DAI %s\n", w->name);
170 		return -EINVAL;
171 	}
172 
173 	config = &sof_dai->dai_config[sof_dai->current_config];
174 
175 	/* update config with link and stream ID */
176 	config->dai_index = (link_id << 8) | dai_id;
177 	config->alh.stream_id = alh_stream_id;
178 
179 	if (setup)
180 		return hda_ctrl_dai_widget_setup(w);
181 
182 	return hda_ctrl_dai_widget_free(w);
183 }
184 
185 static int sdw_params_stream(struct device *dev,
186 			     struct sdw_intel_stream_params_data *params_data)
187 {
188 	struct snd_pcm_substream *substream = params_data->substream;
189 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
190 	struct snd_soc_dai *d = params_data->dai;
191 	struct snd_soc_dapm_widget *w;
192 
193 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
194 		w = d->playback_widget;
195 	else
196 		w = d->capture_widget;
197 
198 	return sdw_dai_config_ipc(sdev, w, params_data->link_id, params_data->alh_stream_id,
199 				  d->id, true);
200 }
201 
202 static int sdw_free_stream(struct device *dev,
203 			   struct sdw_intel_stream_free_data *free_data)
204 {
205 	struct snd_pcm_substream *substream = free_data->substream;
206 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
207 	struct snd_soc_dai *d = free_data->dai;
208 	struct snd_soc_dapm_widget *w;
209 
210 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
211 		w = d->playback_widget;
212 	else
213 		w = d->capture_widget;
214 
215 	/* send invalid stream_id */
216 	return sdw_dai_config_ipc(sdev, w, free_data->link_id, 0xFFFF, d->id, false);
217 }
218 
219 static const struct sdw_intel_ops sdw_callback = {
220 	.params_stream = sdw_params_stream,
221 	.free_stream = sdw_free_stream,
222 };
223 
224 void hda_sdw_int_enable(struct snd_sof_dev *sdev, bool enable)
225 {
226 	sdw_intel_enable_irq(sdev->bar[HDA_DSP_BAR], enable);
227 }
228 
229 static int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
230 {
231 	struct sof_intel_hda_dev *hdev;
232 	acpi_handle handle;
233 	int ret;
234 
235 	handle = ACPI_HANDLE(sdev->dev);
236 
237 	/* save ACPI info for the probe step */
238 	hdev = sdev->pdata->hw_pdata;
239 
240 	ret = sdw_intel_acpi_scan(handle, &hdev->info);
241 	if (ret < 0)
242 		return -EINVAL;
243 
244 	return 0;
245 }
246 
247 static int hda_sdw_probe(struct snd_sof_dev *sdev)
248 {
249 	struct sof_intel_hda_dev *hdev;
250 	struct sdw_intel_res res;
251 	void *sdw;
252 
253 	hdev = sdev->pdata->hw_pdata;
254 
255 	memset(&res, 0, sizeof(res));
256 
257 	res.mmio_base = sdev->bar[HDA_DSP_BAR];
258 	res.shim_base = hdev->desc->sdw_shim_base;
259 	res.alh_base = hdev->desc->sdw_alh_base;
260 	res.irq = sdev->ipc_irq;
261 	res.handle = hdev->info.handle;
262 	res.parent = sdev->dev;
263 	res.ops = &sdw_callback;
264 	res.dev = sdev->dev;
265 	res.clock_stop_quirks = sdw_clock_stop_quirks;
266 
267 	/*
268 	 * ops and arg fields are not populated for now,
269 	 * they will be needed when the DAI callbacks are
270 	 * provided
271 	 */
272 
273 	/* we could filter links here if needed, e.g for quirks */
274 	res.count = hdev->info.count;
275 	res.link_mask = hdev->info.link_mask;
276 
277 	sdw = sdw_intel_probe(&res);
278 	if (!sdw) {
279 		dev_err(sdev->dev, "error: SoundWire probe failed\n");
280 		return -EINVAL;
281 	}
282 
283 	/* save context */
284 	hdev->sdw = sdw;
285 
286 	return 0;
287 }
288 
289 int hda_sdw_startup(struct snd_sof_dev *sdev)
290 {
291 	struct sof_intel_hda_dev *hdev;
292 	struct snd_sof_pdata *pdata = sdev->pdata;
293 
294 	hdev = sdev->pdata->hw_pdata;
295 
296 	if (!hdev->sdw)
297 		return 0;
298 
299 	if (pdata->machine && !pdata->machine->mach_params.link_mask)
300 		return 0;
301 
302 	return sdw_intel_startup(hdev->sdw);
303 }
304 
305 static int hda_sdw_exit(struct snd_sof_dev *sdev)
306 {
307 	struct sof_intel_hda_dev *hdev;
308 
309 	hdev = sdev->pdata->hw_pdata;
310 
311 	hda_sdw_int_enable(sdev, false);
312 
313 	if (hdev->sdw)
314 		sdw_intel_exit(hdev->sdw);
315 	hdev->sdw = NULL;
316 
317 	return 0;
318 }
319 
320 bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev)
321 {
322 	struct sof_intel_hda_dev *hdev;
323 	bool ret = false;
324 	u32 irq_status;
325 
326 	hdev = sdev->pdata->hw_pdata;
327 
328 	if (!hdev->sdw)
329 		return ret;
330 
331 	/* store status */
332 	irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2);
333 
334 	/* invalid message ? */
335 	if (irq_status == 0xffffffff)
336 		goto out;
337 
338 	/* SDW message ? */
339 	if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW)
340 		ret = true;
341 
342 out:
343 	return ret;
344 }
345 
346 static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
347 {
348 	const struct sof_intel_dsp_desc *chip;
349 
350 	chip = get_chip_info(sdev->pdata);
351 	if (chip && chip->check_sdw_irq)
352 		return chip->check_sdw_irq(sdev);
353 
354 	return false;
355 }
356 
357 static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
358 {
359 	return sdw_intel_thread(irq, context);
360 }
361 
362 static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
363 {
364 	struct sof_intel_hda_dev *hdev;
365 
366 	hdev = sdev->pdata->hw_pdata;
367 	if (hdev->sdw &&
368 	    snd_sof_dsp_read(sdev, HDA_DSP_BAR,
369 			     hdev->desc->sdw_shim_base + SDW_SHIM_WAKESTS))
370 		return true;
371 
372 	return false;
373 }
374 
375 void hda_sdw_process_wakeen(struct snd_sof_dev *sdev)
376 {
377 	struct sof_intel_hda_dev *hdev;
378 
379 	hdev = sdev->pdata->hw_pdata;
380 	if (!hdev->sdw)
381 		return;
382 
383 	sdw_intel_process_wakeen_event(hdev->sdw);
384 }
385 
386 #else /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
387 static inline int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
388 {
389 	return 0;
390 }
391 
392 static inline int hda_sdw_probe(struct snd_sof_dev *sdev)
393 {
394 	return 0;
395 }
396 
397 static inline int hda_sdw_exit(struct snd_sof_dev *sdev)
398 {
399 	return 0;
400 }
401 
402 static inline bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
403 {
404 	return false;
405 }
406 
407 static inline irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
408 {
409 	return IRQ_HANDLED;
410 }
411 
412 static inline bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
413 {
414 	return false;
415 }
416 
417 #endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
418 
419 /*
420  * Debug
421  */
422 
423 struct hda_dsp_msg_code {
424 	u32 code;
425 	const char *msg;
426 };
427 
428 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
429 static bool hda_use_msi = true;
430 module_param_named(use_msi, hda_use_msi, bool, 0444);
431 MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode");
432 #else
433 #define hda_use_msi	(1)
434 #endif
435 
436 static char *hda_model;
437 module_param(hda_model, charp, 0444);
438 MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
439 
440 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
441 static int hda_dmic_num = -1;
442 module_param_named(dmic_num, hda_dmic_num, int, 0444);
443 MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number");
444 #endif
445 
446 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
447 static bool hda_codec_use_common_hdmi = IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI);
448 module_param_named(use_common_hdmi, hda_codec_use_common_hdmi, bool, 0444);
449 MODULE_PARM_DESC(use_common_hdmi, "SOF HDA use common HDMI codec driver");
450 #endif
451 
452 static const struct hda_dsp_msg_code hda_dsp_rom_msg[] = {
453 	{HDA_DSP_ROM_FW_MANIFEST_LOADED, "status: manifest loaded"},
454 	{HDA_DSP_ROM_FW_FW_LOADED, "status: fw loaded"},
455 	{HDA_DSP_ROM_FW_ENTERED, "status: fw entered"},
456 	{HDA_DSP_ROM_CSE_ERROR, "error: cse error"},
457 	{HDA_DSP_ROM_CSE_WRONG_RESPONSE, "error: cse wrong response"},
458 	{HDA_DSP_ROM_IMR_TO_SMALL, "error: IMR too small"},
459 	{HDA_DSP_ROM_BASE_FW_NOT_FOUND, "error: base fw not found"},
460 	{HDA_DSP_ROM_CSE_VALIDATION_FAILED, "error: signature verification failed"},
461 	{HDA_DSP_ROM_IPC_FATAL_ERROR, "error: ipc fatal error"},
462 	{HDA_DSP_ROM_L2_CACHE_ERROR, "error: L2 cache error"},
463 	{HDA_DSP_ROM_LOAD_OFFSET_TO_SMALL, "error: load offset too small"},
464 	{HDA_DSP_ROM_API_PTR_INVALID, "error: API ptr invalid"},
465 	{HDA_DSP_ROM_BASEFW_INCOMPAT, "error: base fw incompatible"},
466 	{HDA_DSP_ROM_UNHANDLED_INTERRUPT, "error: unhandled interrupt"},
467 	{HDA_DSP_ROM_MEMORY_HOLE_ECC, "error: ECC memory hole"},
468 	{HDA_DSP_ROM_KERNEL_EXCEPTION, "error: kernel exception"},
469 	{HDA_DSP_ROM_USER_EXCEPTION, "error: user exception"},
470 	{HDA_DSP_ROM_UNEXPECTED_RESET, "error: unexpected reset"},
471 	{HDA_DSP_ROM_NULL_FW_ENTRY,	"error: null FW entry point"},
472 };
473 
474 static void hda_dsp_get_status(struct snd_sof_dev *sdev)
475 {
476 	u32 status;
477 	int i;
478 
479 	status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
480 				  HDA_DSP_SRAM_REG_ROM_STATUS);
481 
482 	for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
483 		if (status == hda_dsp_rom_msg[i].code) {
484 			dev_err(sdev->dev, "%s - code %8.8x\n",
485 				hda_dsp_rom_msg[i].msg, status);
486 			return;
487 		}
488 	}
489 
490 	/* not for us, must be generic sof message */
491 	dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
492 }
493 
494 static void hda_dsp_get_registers(struct snd_sof_dev *sdev,
495 				  struct sof_ipc_dsp_oops_xtensa *xoops,
496 				  struct sof_ipc_panic_info *panic_info,
497 				  u32 *stack, size_t stack_words)
498 {
499 	u32 offset = sdev->dsp_oops_offset;
500 
501 	/* first read registers */
502 	sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
503 
504 	/* note: variable AR register array is not read */
505 
506 	/* then get panic info */
507 	if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
508 		dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
509 			xoops->arch_hdr.totalsize);
510 		return;
511 	}
512 	offset += xoops->arch_hdr.totalsize;
513 	sof_block_read(sdev, sdev->mmio_bar, offset,
514 		       panic_info, sizeof(*panic_info));
515 
516 	/* then get the stack */
517 	offset += sizeof(*panic_info);
518 	sof_block_read(sdev, sdev->mmio_bar, offset, stack,
519 		       stack_words * sizeof(u32));
520 }
521 
522 /* dump the first 8 dwords representing the extended ROM status */
523 static void hda_dsp_dump_ext_rom_status(struct snd_sof_dev *sdev, u32 flags)
524 {
525 	char msg[128];
526 	int len = 0;
527 	u32 value;
528 	int i;
529 
530 	for (i = 0; i < HDA_EXT_ROM_STATUS_SIZE; i++) {
531 		value = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_ROM_STATUS + i * 0x4);
532 		len += snprintf(msg + len, sizeof(msg) - len, " 0x%x", value);
533 	}
534 
535 	dev_err(sdev->dev, "extended rom status: %s", msg);
536 
537 }
538 
539 void hda_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
540 {
541 	struct sof_ipc_dsp_oops_xtensa xoops;
542 	struct sof_ipc_panic_info panic_info;
543 	u32 stack[HDA_DSP_STACK_DUMP_SIZE];
544 
545 	/* print ROM/FW status */
546 	hda_dsp_get_status(sdev);
547 
548 	if (flags & SOF_DBG_DUMP_REGS) {
549 		u32 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_STATUS);
550 		u32 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_TRACEP);
551 
552 		hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
553 				      HDA_DSP_STACK_DUMP_SIZE);
554 		snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
555 				   stack, HDA_DSP_STACK_DUMP_SIZE);
556 	} else {
557 		hda_dsp_dump_ext_rom_status(sdev, flags);
558 	}
559 }
560 
561 void hda_ipc_irq_dump(struct snd_sof_dev *sdev)
562 {
563 	struct hdac_bus *bus = sof_to_bus(sdev);
564 	u32 adspis;
565 	u32 intsts;
566 	u32 intctl;
567 	u32 ppsts;
568 	u8 rirbsts;
569 
570 	/* read key IRQ stats and config registers */
571 	adspis = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
572 	intsts = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS);
573 	intctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL);
574 	ppsts = snd_sof_dsp_read(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPSTS);
575 	rirbsts = snd_hdac_chip_readb(bus, RIRBSTS);
576 
577 	dev_err(sdev->dev, "hda irq intsts 0x%8.8x intlctl 0x%8.8x rirb %2.2x\n",
578 		intsts, intctl, rirbsts);
579 	dev_err(sdev->dev, "dsp irq ppsts 0x%8.8x adspis 0x%8.8x\n", ppsts, adspis);
580 }
581 
582 void hda_ipc_dump(struct snd_sof_dev *sdev)
583 {
584 	u32 hipcie;
585 	u32 hipct;
586 	u32 hipcctl;
587 
588 	hda_ipc_irq_dump(sdev);
589 
590 	/* read IPC status */
591 	hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
592 	hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
593 	hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL);
594 
595 	/* dump the IPC regs */
596 	/* TODO: parse the raw msg */
597 	dev_err(sdev->dev, "host status 0x%8.8x dsp status 0x%8.8x mask 0x%8.8x\n",
598 		hipcie, hipct, hipcctl);
599 }
600 
601 static int hda_init(struct snd_sof_dev *sdev)
602 {
603 	struct hda_bus *hbus;
604 	struct hdac_bus *bus;
605 	struct pci_dev *pci = to_pci_dev(sdev->dev);
606 	int ret;
607 
608 	hbus = sof_to_hbus(sdev);
609 	bus = sof_to_bus(sdev);
610 
611 	/* HDA bus init */
612 	sof_hda_bus_init(bus, &pci->dev);
613 
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 irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
814 {
815 	struct snd_sof_dev *sdev = context;
816 
817 	/*
818 	 * Get global interrupt status. It includes all hardware interrupt
819 	 * sources in the Intel HD Audio controller.
820 	 */
821 	if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
822 	    SOF_HDA_INTSTS_GIS) {
823 
824 		/* disable GIE interrupt */
825 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
826 					SOF_HDA_INTCTL,
827 					SOF_HDA_INT_GLOBAL_EN,
828 					0);
829 
830 		return IRQ_WAKE_THREAD;
831 	}
832 
833 	return IRQ_NONE;
834 }
835 
836 static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
837 {
838 	struct snd_sof_dev *sdev = context;
839 	struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
840 
841 	/* deal with streams and controller first */
842 	if (hda_dsp_check_stream_irq(sdev))
843 		hda_dsp_stream_threaded_handler(irq, sdev);
844 
845 	if (hda_dsp_check_ipc_irq(sdev))
846 		sof_ops(sdev)->irq_thread(irq, sdev);
847 
848 	if (hda_dsp_check_sdw_irq(sdev))
849 		hda_dsp_sdw_thread(irq, hdev->sdw);
850 
851 	if (hda_sdw_check_wakeen_irq(sdev))
852 		hda_sdw_process_wakeen(sdev);
853 
854 	/* enable GIE interrupt */
855 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
856 				SOF_HDA_INTCTL,
857 				SOF_HDA_INT_GLOBAL_EN,
858 				SOF_HDA_INT_GLOBAL_EN);
859 
860 	return IRQ_HANDLED;
861 }
862 
863 int hda_dsp_probe(struct snd_sof_dev *sdev)
864 {
865 	struct pci_dev *pci = to_pci_dev(sdev->dev);
866 	struct sof_intel_hda_dev *hdev;
867 	struct hdac_bus *bus;
868 	const struct sof_intel_dsp_desc *chip;
869 	int ret = 0;
870 
871 	/*
872 	 * detect DSP by checking class/subclass/prog-id information
873 	 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
874 	 * class=04 subclass 01 prog-if 00: DSP is present
875 	 *   (and may be required e.g. for DMIC or SSP support)
876 	 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
877 	 */
878 	if (pci->class == 0x040300) {
879 		dev_err(sdev->dev, "error: the DSP is not enabled on this platform, aborting probe\n");
880 		return -ENODEV;
881 	} else if (pci->class != 0x040100 && pci->class != 0x040380) {
882 		dev_err(sdev->dev, "error: unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n", pci->class);
883 		return -ENODEV;
884 	}
885 	dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n", pci->class);
886 
887 	chip = get_chip_info(sdev->pdata);
888 	if (!chip) {
889 		dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
890 			pci->device);
891 		ret = -EIO;
892 		goto err;
893 	}
894 
895 	hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
896 	if (!hdev)
897 		return -ENOMEM;
898 	sdev->pdata->hw_pdata = hdev;
899 	hdev->desc = chip;
900 
901 	hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
902 						       PLATFORM_DEVID_NONE,
903 						       NULL, 0);
904 	if (IS_ERR(hdev->dmic_dev)) {
905 		dev_err(sdev->dev, "error: failed to create DMIC device\n");
906 		return PTR_ERR(hdev->dmic_dev);
907 	}
908 
909 	/*
910 	 * use position update IPC if either it is forced
911 	 * or we don't have other choice
912 	 */
913 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
914 	hdev->no_ipc_position = 0;
915 #else
916 	hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
917 #endif
918 
919 	/* set up HDA base */
920 	bus = sof_to_bus(sdev);
921 	ret = hda_init(sdev);
922 	if (ret < 0)
923 		goto hdac_bus_unmap;
924 
925 	/* DSP base */
926 	sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
927 	if (!sdev->bar[HDA_DSP_BAR]) {
928 		dev_err(sdev->dev, "error: ioremap error\n");
929 		ret = -ENXIO;
930 		goto hdac_bus_unmap;
931 	}
932 
933 	sdev->mmio_bar = HDA_DSP_BAR;
934 	sdev->mailbox_bar = HDA_DSP_BAR;
935 
936 	/* allow 64bit DMA address if supported by H/W */
937 	if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(64))) {
938 		dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
939 		dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32));
940 	}
941 
942 	/* init streams */
943 	ret = hda_dsp_stream_init(sdev);
944 	if (ret < 0) {
945 		dev_err(sdev->dev, "error: failed to init streams\n");
946 		/*
947 		 * not all errors are due to memory issues, but trying
948 		 * to free everything does not harm
949 		 */
950 		goto free_streams;
951 	}
952 
953 	/*
954 	 * register our IRQ
955 	 * let's try to enable msi firstly
956 	 * if it fails, use legacy interrupt mode
957 	 * TODO: support msi multiple vectors
958 	 */
959 	if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
960 		dev_info(sdev->dev, "use msi interrupt mode\n");
961 		sdev->ipc_irq = pci_irq_vector(pci, 0);
962 		/* initialised to "false" by kzalloc() */
963 		sdev->msi_enabled = true;
964 	}
965 
966 	if (!sdev->msi_enabled) {
967 		dev_info(sdev->dev, "use legacy interrupt mode\n");
968 		/*
969 		 * in IO-APIC mode, hda->irq and ipc_irq are using the same
970 		 * irq number of pci->irq
971 		 */
972 		sdev->ipc_irq = pci->irq;
973 	}
974 
975 	dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
976 	ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
977 				   hda_dsp_interrupt_thread,
978 				   IRQF_SHARED, "AudioDSP", sdev);
979 	if (ret < 0) {
980 		dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
981 			sdev->ipc_irq);
982 		goto free_irq_vector;
983 	}
984 
985 	pci_set_master(pci);
986 	synchronize_irq(pci->irq);
987 
988 	/*
989 	 * clear TCSEL to clear playback on some HD Audio
990 	 * codecs. PCI TCSEL is defined in the Intel manuals.
991 	 */
992 	snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
993 
994 	/* init HDA capabilities */
995 	ret = hda_init_caps(sdev);
996 	if (ret < 0)
997 		goto free_ipc_irq;
998 
999 	/* enable ppcap interrupt */
1000 	hda_dsp_ctrl_ppcap_enable(sdev, true);
1001 	hda_dsp_ctrl_ppcap_int_enable(sdev, true);
1002 
1003 	/* set default mailbox offset for FW ready message */
1004 	sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
1005 
1006 	INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
1007 
1008 	return 0;
1009 
1010 free_ipc_irq:
1011 	free_irq(sdev->ipc_irq, sdev);
1012 free_irq_vector:
1013 	if (sdev->msi_enabled)
1014 		pci_free_irq_vectors(pci);
1015 free_streams:
1016 	hda_dsp_stream_free(sdev);
1017 /* dsp_unmap: not currently used */
1018 	iounmap(sdev->bar[HDA_DSP_BAR]);
1019 hdac_bus_unmap:
1020 	platform_device_unregister(hdev->dmic_dev);
1021 	iounmap(bus->remap_addr);
1022 	hda_codec_i915_exit(sdev);
1023 err:
1024 	return ret;
1025 }
1026 
1027 int hda_dsp_remove(struct snd_sof_dev *sdev)
1028 {
1029 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
1030 	struct hdac_bus *bus = sof_to_bus(sdev);
1031 	struct pci_dev *pci = to_pci_dev(sdev->dev);
1032 	const struct sof_intel_dsp_desc *chip = hda->desc;
1033 
1034 	/* cancel any attempt for DSP D0I3 */
1035 	cancel_delayed_work_sync(&hda->d0i3_work);
1036 
1037 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1038 	/* codec removal, invoke bus_device_remove */
1039 	snd_hdac_ext_bus_device_remove(bus);
1040 #endif
1041 
1042 	hda_sdw_exit(sdev);
1043 
1044 	if (!IS_ERR_OR_NULL(hda->dmic_dev))
1045 		platform_device_unregister(hda->dmic_dev);
1046 
1047 	/* disable DSP IRQ */
1048 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
1049 				SOF_HDA_PPCTL_PIE, 0);
1050 
1051 	/* disable CIE and GIE interrupts */
1052 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
1053 				SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
1054 
1055 	/* disable cores */
1056 	if (chip)
1057 		snd_sof_dsp_core_power_down(sdev, chip->host_managed_cores_mask);
1058 
1059 	/* disable DSP */
1060 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
1061 				SOF_HDA_PPCTL_GPROCEN, 0);
1062 
1063 	free_irq(sdev->ipc_irq, sdev);
1064 	if (sdev->msi_enabled)
1065 		pci_free_irq_vectors(pci);
1066 
1067 	hda_dsp_stream_free(sdev);
1068 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1069 	snd_hdac_link_free_all(bus);
1070 #endif
1071 
1072 	iounmap(sdev->bar[HDA_DSP_BAR]);
1073 	iounmap(bus->remap_addr);
1074 
1075 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1076 	snd_hdac_ext_bus_exit(bus);
1077 #endif
1078 	hda_codec_i915_exit(sdev);
1079 
1080 	return 0;
1081 }
1082 
1083 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1084 static int hda_generic_machine_select(struct snd_sof_dev *sdev)
1085 {
1086 	struct hdac_bus *bus = sof_to_bus(sdev);
1087 	struct snd_soc_acpi_mach_params *mach_params;
1088 	struct snd_soc_acpi_mach *hda_mach;
1089 	struct snd_sof_pdata *pdata = sdev->pdata;
1090 	const char *tplg_filename;
1091 	const char *idisp_str;
1092 	int dmic_num = 0;
1093 	int codec_num = 0;
1094 	int ret;
1095 	int i;
1096 
1097 	/* codec detection */
1098 	if (!bus->codec_mask) {
1099 		dev_info(bus->dev, "no hda codecs found!\n");
1100 	} else {
1101 		dev_info(bus->dev, "hda codecs found, mask %lx\n",
1102 			 bus->codec_mask);
1103 
1104 		for (i = 0; i < HDA_MAX_CODECS; i++) {
1105 			if (bus->codec_mask & (1 << i))
1106 				codec_num++;
1107 		}
1108 
1109 		/*
1110 		 * If no machine driver is found, then:
1111 		 *
1112 		 * generic hda machine driver can handle:
1113 		 *  - one HDMI codec, and/or
1114 		 *  - one external HDAudio codec
1115 		 */
1116 		if (!pdata->machine && codec_num <= 2) {
1117 			hda_mach = snd_soc_acpi_intel_hda_machines;
1118 
1119 			dev_info(bus->dev, "using HDA machine driver %s now\n",
1120 				 hda_mach->drv_name);
1121 
1122 			if (codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask))
1123 				idisp_str = "-idisp";
1124 			else
1125 				idisp_str = "";
1126 
1127 			/* topology: use the info from hda_machines */
1128 			tplg_filename = hda_mach->sof_tplg_filename;
1129 			ret = dmic_topology_fixup(sdev, &tplg_filename, idisp_str, &dmic_num);
1130 			if (ret < 0)
1131 				return ret;
1132 
1133 			hda_mach->mach_params.dmic_num = dmic_num;
1134 			pdata->machine = hda_mach;
1135 			pdata->tplg_filename = tplg_filename;
1136 
1137 			if (codec_num == 2) {
1138 				/*
1139 				 * Prevent SoundWire links from starting when an external
1140 				 * HDaudio codec is used
1141 				 */
1142 				hda_mach->mach_params.link_mask = 0;
1143 			}
1144 		}
1145 	}
1146 
1147 	/* used by hda machine driver to create dai links */
1148 	if (pdata->machine) {
1149 		mach_params = (struct snd_soc_acpi_mach_params *)
1150 			&pdata->machine->mach_params;
1151 		mach_params->codec_mask = bus->codec_mask;
1152 		mach_params->common_hdmi_codec_drv = hda_codec_use_common_hdmi;
1153 	}
1154 
1155 	return 0;
1156 }
1157 #else
1158 static int hda_generic_machine_select(struct snd_sof_dev *sdev)
1159 {
1160 	return 0;
1161 }
1162 #endif
1163 
1164 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1165 /* Check if all Slaves defined on the link can be found */
1166 static bool link_slaves_found(struct snd_sof_dev *sdev,
1167 			      const struct snd_soc_acpi_link_adr *link,
1168 			      struct sdw_intel_ctx *sdw)
1169 {
1170 	struct hdac_bus *bus = sof_to_bus(sdev);
1171 	struct sdw_intel_slave_id *ids = sdw->ids;
1172 	int num_slaves = sdw->num_slaves;
1173 	unsigned int part_id, link_id, unique_id, mfg_id;
1174 	int i, j, k;
1175 
1176 	for (i = 0; i < link->num_adr; i++) {
1177 		u64 adr = link->adr_d[i].adr;
1178 		int reported_part_count = 0;
1179 
1180 		mfg_id = SDW_MFG_ID(adr);
1181 		part_id = SDW_PART_ID(adr);
1182 		link_id = SDW_DISCO_LINK_ID(adr);
1183 
1184 		for (j = 0; j < num_slaves; j++) {
1185 			/* find out how many identical parts were reported on that link */
1186 			if (ids[j].link_id == link_id &&
1187 			    ids[j].id.part_id == part_id &&
1188 			    ids[j].id.mfg_id == mfg_id)
1189 				reported_part_count++;
1190 		}
1191 
1192 		for (j = 0; j < num_slaves; j++) {
1193 			int expected_part_count = 0;
1194 
1195 			if (ids[j].link_id != link_id ||
1196 			    ids[j].id.part_id != part_id ||
1197 			    ids[j].id.mfg_id != mfg_id)
1198 				continue;
1199 
1200 			/* find out how many identical parts are expected */
1201 			for (k = 0; k < link->num_adr; k++) {
1202 				u64 adr2 = link->adr_d[k].adr;
1203 				unsigned int part_id2, link_id2, mfg_id2;
1204 
1205 				mfg_id2 = SDW_MFG_ID(adr2);
1206 				part_id2 = SDW_PART_ID(adr2);
1207 				link_id2 = SDW_DISCO_LINK_ID(adr2);
1208 
1209 				if (link_id2 == link_id &&
1210 				    part_id2 == part_id &&
1211 				    mfg_id2 == mfg_id)
1212 					expected_part_count++;
1213 			}
1214 
1215 			if (reported_part_count == expected_part_count) {
1216 				/*
1217 				 * we have to check unique id
1218 				 * if there is more than one
1219 				 * Slave on the link
1220 				 */
1221 				unique_id = SDW_UNIQUE_ID(adr);
1222 				if (reported_part_count == 1 ||
1223 				    ids[j].id.unique_id == unique_id) {
1224 					dev_dbg(bus->dev, "found %x at link %d\n",
1225 						part_id, link_id);
1226 					break;
1227 				}
1228 			} else {
1229 				dev_dbg(bus->dev, "part %x reported %d expected %d on link %d, skipping\n",
1230 					part_id, reported_part_count, expected_part_count, link_id);
1231 			}
1232 		}
1233 		if (j == num_slaves) {
1234 			dev_dbg(bus->dev,
1235 				"Slave %x not found\n",
1236 				part_id);
1237 			return false;
1238 		}
1239 	}
1240 	return true;
1241 }
1242 
1243 static int hda_sdw_machine_select(struct snd_sof_dev *sdev)
1244 {
1245 	struct snd_sof_pdata *pdata = sdev->pdata;
1246 	const struct snd_soc_acpi_link_adr *link;
1247 	struct snd_soc_acpi_mach *mach;
1248 	struct sof_intel_hda_dev *hdev;
1249 	u32 link_mask;
1250 	int i;
1251 
1252 	hdev = pdata->hw_pdata;
1253 	link_mask = hdev->info.link_mask;
1254 
1255 	/*
1256 	 * Select SoundWire machine driver if needed using the
1257 	 * alternate tables. This case deals with SoundWire-only
1258 	 * machines, for mixed cases with I2C/I2S the detection relies
1259 	 * on the HID list.
1260 	 */
1261 	if (link_mask && !pdata->machine) {
1262 		for (mach = pdata->desc->alt_machines;
1263 		     mach && mach->link_mask; mach++) {
1264 			/*
1265 			 * On some platforms such as Up Extreme all links
1266 			 * are enabled but only one link can be used by
1267 			 * external codec. Instead of exact match of two masks,
1268 			 * first check whether link_mask of mach is subset of
1269 			 * link_mask supported by hw and then go on searching
1270 			 * link_adr
1271 			 */
1272 			if (~link_mask & mach->link_mask)
1273 				continue;
1274 
1275 			/* No need to match adr if there is no links defined */
1276 			if (!mach->links)
1277 				break;
1278 
1279 			link = mach->links;
1280 			for (i = 0; i < hdev->info.count && link->num_adr;
1281 			     i++, link++) {
1282 				/*
1283 				 * Try next machine if any expected Slaves
1284 				 * are not found on this link.
1285 				 */
1286 				if (!link_slaves_found(sdev, link, hdev->sdw))
1287 					break;
1288 			}
1289 			/* Found if all Slaves are checked */
1290 			if (i == hdev->info.count || !link->num_adr)
1291 				break;
1292 		}
1293 		if (mach && mach->link_mask) {
1294 			int dmic_num = 0;
1295 
1296 			pdata->machine = mach;
1297 			mach->mach_params.links = mach->links;
1298 			mach->mach_params.link_mask = mach->link_mask;
1299 			mach->mach_params.platform = dev_name(sdev->dev);
1300 			if (mach->sof_fw_filename)
1301 				pdata->fw_filename = mach->sof_fw_filename;
1302 			else
1303 				pdata->fw_filename = pdata->desc->default_fw_filename;
1304 			pdata->tplg_filename = mach->sof_tplg_filename;
1305 
1306 			/*
1307 			 * DMICs use up to 4 pins and are typically pin-muxed with SoundWire
1308 			 * link 2 and 3, thus we only try to enable dmics if all conditions
1309 			 * are true:
1310 			 * a) link 2 and 3 are not used by SoundWire
1311 			 * b) the NHLT table reports the presence of microphones
1312 			 */
1313 			if (!(mach->link_mask & GENMASK(3, 2))) {
1314 				const char *tplg_filename = mach->sof_tplg_filename;
1315 				int ret;
1316 
1317 				ret = dmic_topology_fixup(sdev, &tplg_filename, "", &dmic_num);
1318 
1319 				if (ret < 0)
1320 					return ret;
1321 
1322 				pdata->tplg_filename = tplg_filename;
1323 			}
1324 			mach->mach_params.dmic_num = dmic_num;
1325 
1326 			dev_dbg(sdev->dev,
1327 				"SoundWire machine driver %s topology %s\n",
1328 				mach->drv_name,
1329 				pdata->tplg_filename);
1330 		} else {
1331 			dev_info(sdev->dev,
1332 				 "No SoundWire machine driver found\n");
1333 		}
1334 	}
1335 
1336 	return 0;
1337 }
1338 #else
1339 static int hda_sdw_machine_select(struct snd_sof_dev *sdev)
1340 {
1341 	return 0;
1342 }
1343 #endif
1344 
1345 void hda_set_mach_params(const struct snd_soc_acpi_mach *mach,
1346 			 struct snd_sof_dev *sdev)
1347 {
1348 	struct snd_sof_pdata *pdata = sdev->pdata;
1349 	const struct sof_dev_desc *desc = pdata->desc;
1350 	struct snd_soc_acpi_mach_params *mach_params;
1351 
1352 	mach_params = (struct snd_soc_acpi_mach_params *)&mach->mach_params;
1353 	mach_params->platform = dev_name(sdev->dev);
1354 	mach_params->num_dai_drivers = desc->ops->num_drv;
1355 	mach_params->dai_drivers = desc->ops->drv;
1356 }
1357 
1358 void hda_machine_select(struct snd_sof_dev *sdev)
1359 {
1360 	struct snd_sof_pdata *sof_pdata = sdev->pdata;
1361 	const struct sof_dev_desc *desc = sof_pdata->desc;
1362 	struct snd_soc_acpi_mach *mach;
1363 
1364 	mach = snd_soc_acpi_find_machine(desc->machines);
1365 	if (mach) {
1366 		/*
1367 		 * If tplg file name is overridden, use it instead of
1368 		 * the one set in mach table
1369 		 */
1370 		if (!sof_pdata->tplg_filename)
1371 			sof_pdata->tplg_filename = mach->sof_tplg_filename;
1372 
1373 		sof_pdata->machine = mach;
1374 
1375 		if (mach->link_mask) {
1376 			mach->mach_params.links = mach->links;
1377 			mach->mach_params.link_mask = mach->link_mask;
1378 		}
1379 	}
1380 
1381 	/*
1382 	 * If I2S fails, try SoundWire
1383 	 */
1384 	hda_sdw_machine_select(sdev);
1385 
1386 	/*
1387 	 * Choose HDA generic machine driver if mach is NULL.
1388 	 * Otherwise, set certain mach params.
1389 	 */
1390 	hda_generic_machine_select(sdev);
1391 
1392 	if (!sof_pdata->machine)
1393 		dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1394 }
1395 
1396 int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1397 {
1398 	int ret;
1399 
1400 	ret = snd_intel_dsp_driver_probe(pci);
1401 	if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1402 		dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1403 		return -ENODEV;
1404 	}
1405 
1406 	return sof_pci_probe(pci, pci_id);
1407 }
1408 EXPORT_SYMBOL_NS(hda_pci_intel_probe, SND_SOC_SOF_INTEL_HDA_COMMON);
1409 
1410 MODULE_LICENSE("Dual BSD/GPL");
1411 MODULE_IMPORT_NS(SND_SOC_SOF_PCI_DEV);
1412 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
1413 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
1414 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
1415 MODULE_IMPORT_NS(SND_INTEL_SOUNDWIRE_ACPI);
1416 MODULE_IMPORT_NS(SOUNDWIRE_INTEL_INIT);
1417