xref: /openbmc/linux/sound/soc/sof/intel/hda.c (revision 979452fb)
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 	dma_set_max_seg_size(&pci->dev, UINT_MAX);
960 
961 	/* init streams */
962 	ret = hda_dsp_stream_init(sdev);
963 	if (ret < 0) {
964 		dev_err(sdev->dev, "error: failed to init streams\n");
965 		/*
966 		 * not all errors are due to memory issues, but trying
967 		 * to free everything does not harm
968 		 */
969 		goto free_streams;
970 	}
971 
972 	/*
973 	 * register our IRQ
974 	 * let's try to enable msi firstly
975 	 * if it fails, use legacy interrupt mode
976 	 * TODO: support msi multiple vectors
977 	 */
978 	if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
979 		dev_info(sdev->dev, "use msi interrupt mode\n");
980 		sdev->ipc_irq = pci_irq_vector(pci, 0);
981 		/* initialised to "false" by kzalloc() */
982 		sdev->msi_enabled = true;
983 	}
984 
985 	if (!sdev->msi_enabled) {
986 		dev_info(sdev->dev, "use legacy interrupt mode\n");
987 		/*
988 		 * in IO-APIC mode, hda->irq and ipc_irq are using the same
989 		 * irq number of pci->irq
990 		 */
991 		sdev->ipc_irq = pci->irq;
992 	}
993 
994 	dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
995 	ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
996 				   hda_dsp_interrupt_thread,
997 				   IRQF_SHARED, "AudioDSP", sdev);
998 	if (ret < 0) {
999 		dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
1000 			sdev->ipc_irq);
1001 		goto free_irq_vector;
1002 	}
1003 
1004 	pci_set_master(pci);
1005 	synchronize_irq(pci->irq);
1006 
1007 	/*
1008 	 * clear TCSEL to clear playback on some HD Audio
1009 	 * codecs. PCI TCSEL is defined in the Intel manuals.
1010 	 */
1011 	snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
1012 
1013 	/* init HDA capabilities */
1014 	ret = hda_init_caps(sdev);
1015 	if (ret < 0)
1016 		goto free_ipc_irq;
1017 
1018 	/* enable ppcap interrupt */
1019 	hda_dsp_ctrl_ppcap_enable(sdev, true);
1020 	hda_dsp_ctrl_ppcap_int_enable(sdev, true);
1021 
1022 	/* set default mailbox offset for FW ready message */
1023 	sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
1024 
1025 	INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
1026 
1027 	return 0;
1028 
1029 free_ipc_irq:
1030 	free_irq(sdev->ipc_irq, sdev);
1031 free_irq_vector:
1032 	if (sdev->msi_enabled)
1033 		pci_free_irq_vectors(pci);
1034 free_streams:
1035 	hda_dsp_stream_free(sdev);
1036 /* dsp_unmap: not currently used */
1037 	iounmap(sdev->bar[HDA_DSP_BAR]);
1038 hdac_bus_unmap:
1039 	platform_device_unregister(hdev->dmic_dev);
1040 	iounmap(bus->remap_addr);
1041 	hda_codec_i915_exit(sdev);
1042 err:
1043 	return ret;
1044 }
1045 
1046 int hda_dsp_remove(struct snd_sof_dev *sdev)
1047 {
1048 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
1049 	const struct sof_intel_dsp_desc *chip = hda->desc;
1050 	struct hdac_bus *bus = sof_to_bus(sdev);
1051 	struct pci_dev *pci = to_pci_dev(sdev->dev);
1052 
1053 	/* cancel any attempt for DSP D0I3 */
1054 	cancel_delayed_work_sync(&hda->d0i3_work);
1055 
1056 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1057 	/* codec removal, invoke bus_device_remove */
1058 	snd_hdac_ext_bus_device_remove(bus);
1059 #endif
1060 
1061 	hda_sdw_exit(sdev);
1062 
1063 	if (!IS_ERR_OR_NULL(hda->dmic_dev))
1064 		platform_device_unregister(hda->dmic_dev);
1065 
1066 	/* disable DSP IRQ */
1067 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
1068 				SOF_HDA_PPCTL_PIE, 0);
1069 
1070 	/* disable CIE and GIE interrupts */
1071 	snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
1072 				SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
1073 
1074 	/* disable cores */
1075 	if (chip)
1076 		hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
1077 
1078 	/* disable DSP */
1079 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
1080 				SOF_HDA_PPCTL_GPROCEN, 0);
1081 
1082 	free_irq(sdev->ipc_irq, sdev);
1083 	if (sdev->msi_enabled)
1084 		pci_free_irq_vectors(pci);
1085 
1086 	hda_dsp_stream_free(sdev);
1087 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1088 	snd_hdac_link_free_all(bus);
1089 #endif
1090 
1091 	iounmap(sdev->bar[HDA_DSP_BAR]);
1092 	iounmap(bus->remap_addr);
1093 
1094 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1095 	snd_hdac_ext_bus_exit(bus);
1096 #endif
1097 	hda_codec_i915_exit(sdev);
1098 
1099 	return 0;
1100 }
1101 
1102 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
1103 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1104 				       struct snd_soc_acpi_mach **mach)
1105 {
1106 	struct hdac_bus *bus = sof_to_bus(sdev);
1107 	struct snd_soc_acpi_mach_params *mach_params;
1108 	struct snd_soc_acpi_mach *hda_mach;
1109 	struct snd_sof_pdata *pdata = sdev->pdata;
1110 	const char *tplg_filename;
1111 	const char *idisp_str;
1112 	int dmic_num = 0;
1113 	int codec_num = 0;
1114 	int ret;
1115 	int i;
1116 
1117 	/* codec detection */
1118 	if (!bus->codec_mask) {
1119 		dev_info(bus->dev, "no hda codecs found!\n");
1120 	} else {
1121 		dev_info(bus->dev, "hda codecs found, mask %lx\n",
1122 			 bus->codec_mask);
1123 
1124 		for (i = 0; i < HDA_MAX_CODECS; i++) {
1125 			if (bus->codec_mask & (1 << i))
1126 				codec_num++;
1127 		}
1128 
1129 		/*
1130 		 * If no machine driver is found, then:
1131 		 *
1132 		 * generic hda machine driver can handle:
1133 		 *  - one HDMI codec, and/or
1134 		 *  - one external HDAudio codec
1135 		 */
1136 		if (!*mach && codec_num <= 2) {
1137 			hda_mach = snd_soc_acpi_intel_hda_machines;
1138 
1139 			dev_info(bus->dev, "using HDA machine driver %s now\n",
1140 				 hda_mach->drv_name);
1141 
1142 			if (codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask))
1143 				idisp_str = "-idisp";
1144 			else
1145 				idisp_str = "";
1146 
1147 			/* topology: use the info from hda_machines */
1148 			tplg_filename = hda_mach->sof_tplg_filename;
1149 			ret = dmic_topology_fixup(sdev, &tplg_filename, idisp_str, &dmic_num);
1150 			if (ret < 0)
1151 				return;
1152 
1153 			hda_mach->mach_params.dmic_num = dmic_num;
1154 			pdata->tplg_filename = tplg_filename;
1155 
1156 			if (codec_num == 2) {
1157 				/*
1158 				 * Prevent SoundWire links from starting when an external
1159 				 * HDaudio codec is used
1160 				 */
1161 				hda_mach->mach_params.link_mask = 0;
1162 			}
1163 
1164 			*mach = hda_mach;
1165 		}
1166 	}
1167 
1168 	/* used by hda machine driver to create dai links */
1169 	if (*mach) {
1170 		mach_params = &(*mach)->mach_params;
1171 		mach_params->codec_mask = bus->codec_mask;
1172 		mach_params->common_hdmi_codec_drv = hda_codec_use_common_hdmi;
1173 	}
1174 }
1175 #else
1176 static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1177 				       struct snd_soc_acpi_mach **mach)
1178 {
1179 }
1180 #endif
1181 
1182 #if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1183 /* Check if all Slaves defined on the link can be found */
1184 static bool link_slaves_found(struct snd_sof_dev *sdev,
1185 			      const struct snd_soc_acpi_link_adr *link,
1186 			      struct sdw_intel_ctx *sdw)
1187 {
1188 	struct hdac_bus *bus = sof_to_bus(sdev);
1189 	struct sdw_intel_slave_id *ids = sdw->ids;
1190 	int num_slaves = sdw->num_slaves;
1191 	unsigned int part_id, link_id, unique_id, mfg_id;
1192 	int i, j, k;
1193 
1194 	for (i = 0; i < link->num_adr; i++) {
1195 		u64 adr = link->adr_d[i].adr;
1196 		int reported_part_count = 0;
1197 
1198 		mfg_id = SDW_MFG_ID(adr);
1199 		part_id = SDW_PART_ID(adr);
1200 		link_id = SDW_DISCO_LINK_ID(adr);
1201 
1202 		for (j = 0; j < num_slaves; j++) {
1203 			/* find out how many identical parts were reported on that link */
1204 			if (ids[j].link_id == link_id &&
1205 			    ids[j].id.part_id == part_id &&
1206 			    ids[j].id.mfg_id == mfg_id)
1207 				reported_part_count++;
1208 		}
1209 
1210 		for (j = 0; j < num_slaves; j++) {
1211 			int expected_part_count = 0;
1212 
1213 			if (ids[j].link_id != link_id ||
1214 			    ids[j].id.part_id != part_id ||
1215 			    ids[j].id.mfg_id != mfg_id)
1216 				continue;
1217 
1218 			/* find out how many identical parts are expected */
1219 			for (k = 0; k < link->num_adr; k++) {
1220 				u64 adr2 = link->adr_d[k].adr;
1221 				unsigned int part_id2, link_id2, mfg_id2;
1222 
1223 				mfg_id2 = SDW_MFG_ID(adr2);
1224 				part_id2 = SDW_PART_ID(adr2);
1225 				link_id2 = SDW_DISCO_LINK_ID(adr2);
1226 
1227 				if (link_id2 == link_id &&
1228 				    part_id2 == part_id &&
1229 				    mfg_id2 == mfg_id)
1230 					expected_part_count++;
1231 			}
1232 
1233 			if (reported_part_count == expected_part_count) {
1234 				/*
1235 				 * we have to check unique id
1236 				 * if there is more than one
1237 				 * Slave on the link
1238 				 */
1239 				unique_id = SDW_UNIQUE_ID(adr);
1240 				if (reported_part_count == 1 ||
1241 				    ids[j].id.unique_id == unique_id) {
1242 					dev_dbg(bus->dev, "found %x at link %d\n",
1243 						part_id, link_id);
1244 					break;
1245 				}
1246 			} else {
1247 				dev_dbg(bus->dev, "part %x reported %d expected %d on link %d, skipping\n",
1248 					part_id, reported_part_count, expected_part_count, link_id);
1249 			}
1250 		}
1251 		if (j == num_slaves) {
1252 			dev_dbg(bus->dev,
1253 				"Slave %x not found\n",
1254 				part_id);
1255 			return false;
1256 		}
1257 	}
1258 	return true;
1259 }
1260 
1261 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1262 {
1263 	struct snd_sof_pdata *pdata = sdev->pdata;
1264 	const struct snd_soc_acpi_link_adr *link;
1265 	struct snd_soc_acpi_mach *mach;
1266 	struct sof_intel_hda_dev *hdev;
1267 	u32 link_mask;
1268 	int i;
1269 
1270 	hdev = pdata->hw_pdata;
1271 	link_mask = hdev->info.link_mask;
1272 
1273 	/*
1274 	 * Select SoundWire machine driver if needed using the
1275 	 * alternate tables. This case deals with SoundWire-only
1276 	 * machines, for mixed cases with I2C/I2S the detection relies
1277 	 * on the HID list.
1278 	 */
1279 	if (link_mask) {
1280 		for (mach = pdata->desc->alt_machines;
1281 		     mach && mach->link_mask; mach++) {
1282 			/*
1283 			 * On some platforms such as Up Extreme all links
1284 			 * are enabled but only one link can be used by
1285 			 * external codec. Instead of exact match of two masks,
1286 			 * first check whether link_mask of mach is subset of
1287 			 * link_mask supported by hw and then go on searching
1288 			 * link_adr
1289 			 */
1290 			if (~link_mask & mach->link_mask)
1291 				continue;
1292 
1293 			/* No need to match adr if there is no links defined */
1294 			if (!mach->links)
1295 				break;
1296 
1297 			link = mach->links;
1298 			for (i = 0; i < hdev->info.count && link->num_adr;
1299 			     i++, link++) {
1300 				/*
1301 				 * Try next machine if any expected Slaves
1302 				 * are not found on this link.
1303 				 */
1304 				if (!link_slaves_found(sdev, link, hdev->sdw))
1305 					break;
1306 			}
1307 			/* Found if all Slaves are checked */
1308 			if (i == hdev->info.count || !link->num_adr)
1309 				break;
1310 		}
1311 		if (mach && mach->link_mask) {
1312 			int dmic_num = 0;
1313 
1314 			mach->mach_params.links = mach->links;
1315 			mach->mach_params.link_mask = mach->link_mask;
1316 			mach->mach_params.platform = dev_name(sdev->dev);
1317 			if (mach->sof_fw_filename)
1318 				pdata->fw_filename = mach->sof_fw_filename;
1319 			else
1320 				pdata->fw_filename = pdata->desc->default_fw_filename;
1321 			pdata->tplg_filename = mach->sof_tplg_filename;
1322 
1323 			/*
1324 			 * DMICs use up to 4 pins and are typically pin-muxed with SoundWire
1325 			 * link 2 and 3, thus we only try to enable dmics if all conditions
1326 			 * are true:
1327 			 * a) link 2 and 3 are not used by SoundWire
1328 			 * b) the NHLT table reports the presence of microphones
1329 			 */
1330 			if (!(mach->link_mask & GENMASK(3, 2))) {
1331 				const char *tplg_filename = mach->sof_tplg_filename;
1332 				int ret;
1333 
1334 				ret = dmic_topology_fixup(sdev, &tplg_filename, "", &dmic_num);
1335 				if (ret < 0)
1336 					return NULL;
1337 
1338 				pdata->tplg_filename = tplg_filename;
1339 			}
1340 			mach->mach_params.dmic_num = dmic_num;
1341 
1342 			dev_dbg(sdev->dev,
1343 				"SoundWire machine driver %s topology %s\n",
1344 				mach->drv_name,
1345 				pdata->tplg_filename);
1346 
1347 			return mach;
1348 		}
1349 
1350 		dev_info(sdev->dev, "No SoundWire machine driver found\n");
1351 	}
1352 
1353 	return NULL;
1354 }
1355 #else
1356 static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1357 {
1358 	return NULL;
1359 }
1360 #endif
1361 
1362 void hda_set_mach_params(struct snd_soc_acpi_mach *mach,
1363 			 struct snd_sof_dev *sdev)
1364 {
1365 	struct snd_sof_pdata *pdata = sdev->pdata;
1366 	const struct sof_dev_desc *desc = pdata->desc;
1367 	struct snd_soc_acpi_mach_params *mach_params;
1368 
1369 	mach_params = &mach->mach_params;
1370 	mach_params->platform = dev_name(sdev->dev);
1371 	mach_params->num_dai_drivers = desc->ops->num_drv;
1372 	mach_params->dai_drivers = desc->ops->drv;
1373 }
1374 
1375 struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev)
1376 {
1377 	struct snd_sof_pdata *sof_pdata = sdev->pdata;
1378 	const struct sof_dev_desc *desc = sof_pdata->desc;
1379 	struct snd_soc_acpi_mach *mach;
1380 
1381 	mach = snd_soc_acpi_find_machine(desc->machines);
1382 	if (mach) {
1383 		/*
1384 		 * If tplg file name is overridden, use it instead of
1385 		 * the one set in mach table
1386 		 */
1387 		if (!sof_pdata->tplg_filename)
1388 			sof_pdata->tplg_filename = mach->sof_tplg_filename;
1389 
1390 		if (mach->link_mask) {
1391 			mach->mach_params.links = mach->links;
1392 			mach->mach_params.link_mask = mach->link_mask;
1393 		}
1394 	}
1395 
1396 	/*
1397 	 * If I2S fails, try SoundWire
1398 	 */
1399 	if (!mach)
1400 		mach = hda_sdw_machine_select(sdev);
1401 
1402 	/*
1403 	 * Choose HDA generic machine driver if mach is NULL.
1404 	 * Otherwise, set certain mach params.
1405 	 */
1406 	hda_generic_machine_select(sdev, &mach);
1407 	if (!mach)
1408 		dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1409 
1410 	return mach;
1411 }
1412 
1413 int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1414 {
1415 	int ret;
1416 
1417 	ret = snd_intel_dsp_driver_probe(pci);
1418 	if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1419 		dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1420 		return -ENODEV;
1421 	}
1422 
1423 	return sof_pci_probe(pci, pci_id);
1424 }
1425 EXPORT_SYMBOL_NS(hda_pci_intel_probe, SND_SOC_SOF_INTEL_HDA_COMMON);
1426 
1427 MODULE_LICENSE("Dual BSD/GPL");
1428 MODULE_IMPORT_NS(SND_SOC_SOF_PCI_DEV);
1429 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
1430 MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
1431 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
1432 MODULE_IMPORT_NS(SND_INTEL_SOUNDWIRE_ACPI);
1433 MODULE_IMPORT_NS(SOUNDWIRE_INTEL_INIT);
1434