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