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