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