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