Loading...
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 <sound/hda-mlink.h>
30#include "../sof-audio.h"
31#include "../sof-pci-dev.h"
32#include "../ops.h"
33#include "hda.h"
34#include "telemetry.h"
35
36#define CREATE_TRACE_POINTS
37#include <trace/events/sof_intel.h>
38
39#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
40#include <sound/soc-acpi-intel-match.h>
41#endif
42
43/* platform specific devices */
44#include "shim.h"
45
46#define EXCEPT_MAX_HDR_SIZE 0x400
47#define HDA_EXT_ROM_STATUS_SIZE 8
48
49static u32 hda_get_interface_mask(struct snd_sof_dev *sdev)
50{
51 const struct sof_intel_dsp_desc *chip;
52 u32 interface_mask[2] = { 0 };
53
54 chip = get_chip_info(sdev->pdata);
55 switch (chip->hw_ip_version) {
56 case SOF_INTEL_TANGIER:
57 case SOF_INTEL_BAYTRAIL:
58 case SOF_INTEL_BROADWELL:
59 interface_mask[0] = BIT(SOF_DAI_INTEL_SSP);
60 break;
61 case SOF_INTEL_CAVS_1_5:
62 case SOF_INTEL_CAVS_1_5_PLUS:
63 interface_mask[0] = BIT(SOF_DAI_INTEL_SSP) | BIT(SOF_DAI_INTEL_DMIC) |
64 BIT(SOF_DAI_INTEL_HDA);
65 interface_mask[1] = BIT(SOF_DAI_INTEL_HDA);
66 break;
67 case SOF_INTEL_CAVS_1_8:
68 case SOF_INTEL_CAVS_2_0:
69 case SOF_INTEL_CAVS_2_5:
70 case SOF_INTEL_ACE_1_0:
71 interface_mask[0] = BIT(SOF_DAI_INTEL_SSP) | BIT(SOF_DAI_INTEL_DMIC) |
72 BIT(SOF_DAI_INTEL_HDA) | BIT(SOF_DAI_INTEL_ALH);
73 interface_mask[1] = BIT(SOF_DAI_INTEL_HDA);
74 break;
75 case SOF_INTEL_ACE_2_0:
76 interface_mask[0] = BIT(SOF_DAI_INTEL_SSP) | BIT(SOF_DAI_INTEL_DMIC) |
77 BIT(SOF_DAI_INTEL_HDA) | BIT(SOF_DAI_INTEL_ALH);
78 interface_mask[1] = interface_mask[0]; /* all interfaces accessible without DSP */
79 break;
80 default:
81 break;
82 }
83
84 return interface_mask[sdev->dspless_mode_selected];
85}
86
87#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
88
89/*
90 * The default for SoundWire clock stop quirks is to power gate the IP
91 * and do a Bus Reset, this will need to be modified when the DSP
92 * needs to remain in D0i3 so that the Master does not lose context
93 * and enumeration is not required on clock restart
94 */
95static int sdw_clock_stop_quirks = SDW_INTEL_CLK_STOP_BUS_RESET;
96module_param(sdw_clock_stop_quirks, int, 0444);
97MODULE_PARM_DESC(sdw_clock_stop_quirks, "SOF SoundWire clock stop quirks");
98
99static int sdw_params_stream(struct device *dev,
100 struct sdw_intel_stream_params_data *params_data)
101{
102 struct snd_soc_dai *d = params_data->dai;
103 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(d, params_data->substream->stream);
104 struct snd_sof_dai_config_data data = { 0 };
105
106 data.dai_index = (params_data->link_id << 8) | d->id;
107 data.dai_data = params_data->alh_stream_id;
108
109 return hda_dai_config(w, SOF_DAI_CONFIG_FLAGS_HW_PARAMS, &data);
110}
111
112struct sdw_intel_ops sdw_callback = {
113 .params_stream = sdw_params_stream,
114};
115
116static int sdw_ace2x_params_stream(struct device *dev,
117 struct sdw_intel_stream_params_data *params_data)
118{
119 return sdw_hda_dai_hw_params(params_data->substream,
120 params_data->hw_params,
121 params_data->dai,
122 params_data->link_id);
123}
124
125static int sdw_ace2x_free_stream(struct device *dev,
126 struct sdw_intel_stream_free_data *free_data)
127{
128 return sdw_hda_dai_hw_free(free_data->substream,
129 free_data->dai,
130 free_data->link_id);
131}
132
133static int sdw_ace2x_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
134{
135 return sdw_hda_dai_trigger(substream, cmd, dai);
136}
137
138static struct sdw_intel_ops sdw_ace2x_callback = {
139 .params_stream = sdw_ace2x_params_stream,
140 .free_stream = sdw_ace2x_free_stream,
141 .trigger = sdw_ace2x_trigger,
142};
143
144void hda_common_enable_sdw_irq(struct snd_sof_dev *sdev, bool enable)
145{
146 struct sof_intel_hda_dev *hdev;
147
148 hdev = sdev->pdata->hw_pdata;
149
150 if (!hdev->sdw)
151 return;
152
153 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC2,
154 HDA_DSP_REG_ADSPIC2_SNDW,
155 enable ? HDA_DSP_REG_ADSPIC2_SNDW : 0);
156}
157
158void hda_sdw_int_enable(struct snd_sof_dev *sdev, bool enable)
159{
160 u32 interface_mask = hda_get_interface_mask(sdev);
161 const struct sof_intel_dsp_desc *chip;
162
163 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
164 return;
165
166 chip = get_chip_info(sdev->pdata);
167 if (chip && chip->enable_sdw_irq)
168 chip->enable_sdw_irq(sdev, enable);
169}
170
171static int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
172{
173 u32 interface_mask = hda_get_interface_mask(sdev);
174 struct sof_intel_hda_dev *hdev;
175 acpi_handle handle;
176 int ret;
177
178 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
179 return -EINVAL;
180
181 handle = ACPI_HANDLE(sdev->dev);
182
183 /* save ACPI info for the probe step */
184 hdev = sdev->pdata->hw_pdata;
185
186 ret = sdw_intel_acpi_scan(handle, &hdev->info);
187 if (ret < 0)
188 return -EINVAL;
189
190 return 0;
191}
192
193static int hda_sdw_probe(struct snd_sof_dev *sdev)
194{
195 const struct sof_intel_dsp_desc *chip;
196 struct sof_intel_hda_dev *hdev;
197 struct sdw_intel_res res;
198 void *sdw;
199
200 hdev = sdev->pdata->hw_pdata;
201
202 memset(&res, 0, sizeof(res));
203
204 chip = get_chip_info(sdev->pdata);
205 if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) {
206 res.mmio_base = sdev->bar[HDA_DSP_BAR];
207 res.hw_ops = &sdw_intel_cnl_hw_ops;
208 res.shim_base = hdev->desc->sdw_shim_base;
209 res.alh_base = hdev->desc->sdw_alh_base;
210 res.ext = false;
211 res.ops = &sdw_callback;
212 } else {
213 /*
214 * retrieve eml_lock needed to protect shared registers
215 * in the HDaudio multi-link areas
216 */
217 res.eml_lock = hdac_bus_eml_get_mutex(sof_to_bus(sdev), true,
218 AZX_REG_ML_LEPTR_ID_SDW);
219 if (!res.eml_lock)
220 return -ENODEV;
221
222 res.mmio_base = sdev->bar[HDA_DSP_HDA_BAR];
223 /*
224 * the SHIM and SoundWire register offsets are link-specific
225 * and will be determined when adding auxiliary devices
226 */
227 res.hw_ops = &sdw_intel_lnl_hw_ops;
228 res.ext = true;
229 res.ops = &sdw_ace2x_callback;
230
231 }
232 res.irq = sdev->ipc_irq;
233 res.handle = hdev->info.handle;
234 res.parent = sdev->dev;
235
236 res.dev = sdev->dev;
237 res.clock_stop_quirks = sdw_clock_stop_quirks;
238 res.hbus = sof_to_bus(sdev);
239
240 /*
241 * ops and arg fields are not populated for now,
242 * they will be needed when the DAI callbacks are
243 * provided
244 */
245
246 /* we could filter links here if needed, e.g for quirks */
247 res.count = hdev->info.count;
248 res.link_mask = hdev->info.link_mask;
249
250 sdw = sdw_intel_probe(&res);
251 if (!sdw) {
252 dev_err(sdev->dev, "error: SoundWire probe failed\n");
253 return -EINVAL;
254 }
255
256 /* save context */
257 hdev->sdw = sdw;
258
259 return 0;
260}
261
262int hda_sdw_check_lcount_common(struct snd_sof_dev *sdev)
263{
264 struct sof_intel_hda_dev *hdev;
265 struct sdw_intel_ctx *ctx;
266 u32 caps;
267
268 hdev = sdev->pdata->hw_pdata;
269 ctx = hdev->sdw;
270
271 caps = snd_sof_dsp_read(sdev, HDA_DSP_BAR, ctx->shim_base + SDW_SHIM_LCAP);
272 caps &= SDW_SHIM_LCAP_LCOUNT_MASK;
273
274 /* Check HW supported vs property value */
275 if (caps < ctx->count) {
276 dev_err(sdev->dev,
277 "%s: BIOS master count %d is larger than hardware capabilities %d\n",
278 __func__, ctx->count, caps);
279 return -EINVAL;
280 }
281
282 return 0;
283}
284
285int hda_sdw_check_lcount_ext(struct snd_sof_dev *sdev)
286{
287 struct sof_intel_hda_dev *hdev;
288 struct sdw_intel_ctx *ctx;
289 struct hdac_bus *bus;
290 u32 slcount;
291
292 bus = sof_to_bus(sdev);
293
294 hdev = sdev->pdata->hw_pdata;
295 ctx = hdev->sdw;
296
297 slcount = hdac_bus_eml_get_count(bus, true, AZX_REG_ML_LEPTR_ID_SDW);
298
299 /* Check HW supported vs property value */
300 if (slcount < ctx->count) {
301 dev_err(sdev->dev,
302 "%s: BIOS master count %d is larger than hardware capabilities %d\n",
303 __func__, ctx->count, slcount);
304 return -EINVAL;
305 }
306
307 return 0;
308}
309
310static int hda_sdw_check_lcount(struct snd_sof_dev *sdev)
311{
312 const struct sof_intel_dsp_desc *chip;
313
314 chip = get_chip_info(sdev->pdata);
315 if (chip && chip->read_sdw_lcount)
316 return chip->read_sdw_lcount(sdev);
317
318 return 0;
319}
320
321int hda_sdw_startup(struct snd_sof_dev *sdev)
322{
323 struct sof_intel_hda_dev *hdev;
324 struct snd_sof_pdata *pdata = sdev->pdata;
325 int ret;
326
327 hdev = sdev->pdata->hw_pdata;
328
329 if (!hdev->sdw)
330 return 0;
331
332 if (pdata->machine && !pdata->machine->mach_params.link_mask)
333 return 0;
334
335 ret = hda_sdw_check_lcount(sdev);
336 if (ret < 0)
337 return ret;
338
339 return sdw_intel_startup(hdev->sdw);
340}
341
342static int hda_sdw_exit(struct snd_sof_dev *sdev)
343{
344 struct sof_intel_hda_dev *hdev;
345
346 hdev = sdev->pdata->hw_pdata;
347
348 hda_sdw_int_enable(sdev, false);
349
350 if (hdev->sdw)
351 sdw_intel_exit(hdev->sdw);
352 hdev->sdw = NULL;
353
354 return 0;
355}
356
357bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev)
358{
359 struct sof_intel_hda_dev *hdev;
360 bool ret = false;
361 u32 irq_status;
362
363 hdev = sdev->pdata->hw_pdata;
364
365 if (!hdev->sdw)
366 return ret;
367
368 /* store status */
369 irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2);
370
371 /* invalid message ? */
372 if (irq_status == 0xffffffff)
373 goto out;
374
375 /* SDW message ? */
376 if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW)
377 ret = true;
378
379out:
380 return ret;
381}
382
383static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
384{
385 u32 interface_mask = hda_get_interface_mask(sdev);
386 const struct sof_intel_dsp_desc *chip;
387
388 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
389 return false;
390
391 chip = get_chip_info(sdev->pdata);
392 if (chip && chip->check_sdw_irq)
393 return chip->check_sdw_irq(sdev);
394
395 return false;
396}
397
398static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
399{
400 return sdw_intel_thread(irq, context);
401}
402
403bool hda_sdw_check_wakeen_irq_common(struct snd_sof_dev *sdev)
404{
405 struct sof_intel_hda_dev *hdev;
406
407 hdev = sdev->pdata->hw_pdata;
408 if (hdev->sdw &&
409 snd_sof_dsp_read(sdev, HDA_DSP_BAR,
410 hdev->desc->sdw_shim_base + SDW_SHIM_WAKESTS))
411 return true;
412
413 return false;
414}
415
416static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
417{
418 u32 interface_mask = hda_get_interface_mask(sdev);
419 const struct sof_intel_dsp_desc *chip;
420
421 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
422 return false;
423
424 chip = get_chip_info(sdev->pdata);
425 if (chip && chip->check_sdw_wakeen_irq)
426 return chip->check_sdw_wakeen_irq(sdev);
427
428 return false;
429}
430
431void hda_sdw_process_wakeen(struct snd_sof_dev *sdev)
432{
433 u32 interface_mask = hda_get_interface_mask(sdev);
434 struct sof_intel_hda_dev *hdev;
435
436 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
437 return;
438
439 hdev = sdev->pdata->hw_pdata;
440 if (!hdev->sdw)
441 return;
442
443 sdw_intel_process_wakeen_event(hdev->sdw);
444}
445
446#else /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
447static inline int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
448{
449 return 0;
450}
451
452static inline int hda_sdw_probe(struct snd_sof_dev *sdev)
453{
454 return 0;
455}
456
457static inline int hda_sdw_exit(struct snd_sof_dev *sdev)
458{
459 return 0;
460}
461
462static inline bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
463{
464 return false;
465}
466
467static inline irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
468{
469 return IRQ_HANDLED;
470}
471
472static inline bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
473{
474 return false;
475}
476
477#endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
478
479/*
480 * Debug
481 */
482
483struct hda_dsp_msg_code {
484 u32 code;
485 const char *text;
486};
487
488#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
489static bool hda_use_msi = true;
490module_param_named(use_msi, hda_use_msi, bool, 0444);
491MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode");
492#else
493#define hda_use_msi (1)
494#endif
495
496int sof_hda_position_quirk = SOF_HDA_POSITION_QUIRK_USE_DPIB_REGISTERS;
497module_param_named(position_quirk, sof_hda_position_quirk, int, 0444);
498MODULE_PARM_DESC(position_quirk, "SOF HDaudio position quirk");
499
500static char *hda_model;
501module_param(hda_model, charp, 0444);
502MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
503
504static int dmic_num_override = -1;
505module_param_named(dmic_num, dmic_num_override, int, 0444);
506MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number");
507
508static int mclk_id_override = -1;
509module_param_named(mclk_id, mclk_id_override, int, 0444);
510MODULE_PARM_DESC(mclk_id, "SOF SSP mclk_id");
511
512static const struct hda_dsp_msg_code hda_dsp_rom_fw_error_texts[] = {
513 {HDA_DSP_ROM_CSE_ERROR, "error: cse error"},
514 {HDA_DSP_ROM_CSE_WRONG_RESPONSE, "error: cse wrong response"},
515 {HDA_DSP_ROM_IMR_TO_SMALL, "error: IMR too small"},
516 {HDA_DSP_ROM_BASE_FW_NOT_FOUND, "error: base fw not found"},
517 {HDA_DSP_ROM_CSE_VALIDATION_FAILED, "error: signature verification failed"},
518 {HDA_DSP_ROM_IPC_FATAL_ERROR, "error: ipc fatal error"},
519 {HDA_DSP_ROM_L2_CACHE_ERROR, "error: L2 cache error"},
520 {HDA_DSP_ROM_LOAD_OFFSET_TO_SMALL, "error: load offset too small"},
521 {HDA_DSP_ROM_API_PTR_INVALID, "error: API ptr invalid"},
522 {HDA_DSP_ROM_BASEFW_INCOMPAT, "error: base fw incompatible"},
523 {HDA_DSP_ROM_UNHANDLED_INTERRUPT, "error: unhandled interrupt"},
524 {HDA_DSP_ROM_MEMORY_HOLE_ECC, "error: ECC memory hole"},
525 {HDA_DSP_ROM_KERNEL_EXCEPTION, "error: kernel exception"},
526 {HDA_DSP_ROM_USER_EXCEPTION, "error: user exception"},
527 {HDA_DSP_ROM_UNEXPECTED_RESET, "error: unexpected reset"},
528 {HDA_DSP_ROM_NULL_FW_ENTRY, "error: null FW entry point"},
529};
530
531#define FSR_ROM_STATE_ENTRY(state) {FSR_STATE_ROM_##state, #state}
532static const struct hda_dsp_msg_code fsr_rom_state_names[] = {
533 FSR_ROM_STATE_ENTRY(INIT),
534 FSR_ROM_STATE_ENTRY(INIT_DONE),
535 FSR_ROM_STATE_ENTRY(CSE_MANIFEST_LOADED),
536 FSR_ROM_STATE_ENTRY(FW_MANIFEST_LOADED),
537 FSR_ROM_STATE_ENTRY(FW_FW_LOADED),
538 FSR_ROM_STATE_ENTRY(FW_ENTERED),
539 FSR_ROM_STATE_ENTRY(VERIFY_FEATURE_MASK),
540 FSR_ROM_STATE_ENTRY(GET_LOAD_OFFSET),
541 FSR_ROM_STATE_ENTRY(FETCH_ROM_EXT),
542 FSR_ROM_STATE_ENTRY(FETCH_ROM_EXT_DONE),
543 /* CSE states */
544 FSR_ROM_STATE_ENTRY(CSE_IMR_REQUEST),
545 FSR_ROM_STATE_ENTRY(CSE_IMR_GRANTED),
546 FSR_ROM_STATE_ENTRY(CSE_VALIDATE_IMAGE_REQUEST),
547 FSR_ROM_STATE_ENTRY(CSE_IMAGE_VALIDATED),
548 FSR_ROM_STATE_ENTRY(CSE_IPC_IFACE_INIT),
549 FSR_ROM_STATE_ENTRY(CSE_IPC_RESET_PHASE_1),
550 FSR_ROM_STATE_ENTRY(CSE_IPC_OPERATIONAL_ENTRY),
551 FSR_ROM_STATE_ENTRY(CSE_IPC_OPERATIONAL),
552 FSR_ROM_STATE_ENTRY(CSE_IPC_DOWN),
553};
554
555#define FSR_BRINGUP_STATE_ENTRY(state) {FSR_STATE_BRINGUP_##state, #state}
556static const struct hda_dsp_msg_code fsr_bringup_state_names[] = {
557 FSR_BRINGUP_STATE_ENTRY(INIT),
558 FSR_BRINGUP_STATE_ENTRY(INIT_DONE),
559 FSR_BRINGUP_STATE_ENTRY(HPSRAM_LOAD),
560 FSR_BRINGUP_STATE_ENTRY(UNPACK_START),
561 FSR_BRINGUP_STATE_ENTRY(IMR_RESTORE),
562 FSR_BRINGUP_STATE_ENTRY(FW_ENTERED),
563};
564
565#define FSR_WAIT_STATE_ENTRY(state) {FSR_WAIT_FOR_##state, #state}
566static const struct hda_dsp_msg_code fsr_wait_state_names[] = {
567 FSR_WAIT_STATE_ENTRY(IPC_BUSY),
568 FSR_WAIT_STATE_ENTRY(IPC_DONE),
569 FSR_WAIT_STATE_ENTRY(CACHE_INVALIDATION),
570 FSR_WAIT_STATE_ENTRY(LP_SRAM_OFF),
571 FSR_WAIT_STATE_ENTRY(DMA_BUFFER_FULL),
572 FSR_WAIT_STATE_ENTRY(CSE_CSR),
573};
574
575#define FSR_MODULE_NAME_ENTRY(mod) [FSR_MOD_##mod] = #mod
576static const char * const fsr_module_names[] = {
577 FSR_MODULE_NAME_ENTRY(ROM),
578 FSR_MODULE_NAME_ENTRY(ROM_BYP),
579 FSR_MODULE_NAME_ENTRY(BASE_FW),
580 FSR_MODULE_NAME_ENTRY(LP_BOOT),
581 FSR_MODULE_NAME_ENTRY(BRNGUP),
582 FSR_MODULE_NAME_ENTRY(ROM_EXT),
583};
584
585static const char *
586hda_dsp_get_state_text(u32 code, const struct hda_dsp_msg_code *msg_code,
587 size_t array_size)
588{
589 int i;
590
591 for (i = 0; i < array_size; i++) {
592 if (code == msg_code[i].code)
593 return msg_code[i].text;
594 }
595
596 return NULL;
597}
598
599static void hda_dsp_get_state(struct snd_sof_dev *sdev, const char *level)
600{
601 const struct sof_intel_dsp_desc *chip = get_chip_info(sdev->pdata);
602 const char *state_text, *error_text, *module_text;
603 u32 fsr, state, wait_state, module, error_code;
604
605 fsr = snd_sof_dsp_read(sdev, HDA_DSP_BAR, chip->rom_status_reg);
606 state = FSR_TO_STATE_CODE(fsr);
607 wait_state = FSR_TO_WAIT_STATE_CODE(fsr);
608 module = FSR_TO_MODULE_CODE(fsr);
609
610 if (module > FSR_MOD_ROM_EXT)
611 module_text = "unknown";
612 else
613 module_text = fsr_module_names[module];
614
615 if (module == FSR_MOD_BRNGUP)
616 state_text = hda_dsp_get_state_text(state, fsr_bringup_state_names,
617 ARRAY_SIZE(fsr_bringup_state_names));
618 else
619 state_text = hda_dsp_get_state_text(state, fsr_rom_state_names,
620 ARRAY_SIZE(fsr_rom_state_names));
621
622 /* not for us, must be generic sof message */
623 if (!state_text) {
624 dev_printk(level, sdev->dev, "%#010x: unknown ROM status value\n", fsr);
625 return;
626 }
627
628 if (wait_state) {
629 const char *wait_state_text;
630
631 wait_state_text = hda_dsp_get_state_text(wait_state, fsr_wait_state_names,
632 ARRAY_SIZE(fsr_wait_state_names));
633 if (!wait_state_text)
634 wait_state_text = "unknown";
635
636 dev_printk(level, sdev->dev,
637 "%#010x: module: %s, state: %s, waiting for: %s, %s\n",
638 fsr, module_text, state_text, wait_state_text,
639 fsr & FSR_HALTED ? "not running" : "running");
640 } else {
641 dev_printk(level, sdev->dev, "%#010x: module: %s, state: %s, %s\n",
642 fsr, module_text, state_text,
643 fsr & FSR_HALTED ? "not running" : "running");
644 }
645
646 error_code = snd_sof_dsp_read(sdev, HDA_DSP_BAR, chip->rom_status_reg + 4);
647 if (!error_code)
648 return;
649
650 error_text = hda_dsp_get_state_text(error_code, hda_dsp_rom_fw_error_texts,
651 ARRAY_SIZE(hda_dsp_rom_fw_error_texts));
652 if (!error_text)
653 error_text = "unknown";
654
655 if (state == FSR_STATE_FW_ENTERED)
656 dev_printk(level, sdev->dev, "status code: %#x (%s)\n", error_code,
657 error_text);
658 else
659 dev_printk(level, sdev->dev, "error code: %#x (%s)\n", error_code,
660 error_text);
661}
662
663static void hda_dsp_get_registers(struct snd_sof_dev *sdev,
664 struct sof_ipc_dsp_oops_xtensa *xoops,
665 struct sof_ipc_panic_info *panic_info,
666 u32 *stack, size_t stack_words)
667{
668 u32 offset = sdev->dsp_oops_offset;
669
670 /* first read registers */
671 sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
672
673 /* note: variable AR register array is not read */
674
675 /* then get panic info */
676 if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
677 dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
678 xoops->arch_hdr.totalsize);
679 return;
680 }
681 offset += xoops->arch_hdr.totalsize;
682 sof_block_read(sdev, sdev->mmio_bar, offset,
683 panic_info, sizeof(*panic_info));
684
685 /* then get the stack */
686 offset += sizeof(*panic_info);
687 sof_block_read(sdev, sdev->mmio_bar, offset, stack,
688 stack_words * sizeof(u32));
689}
690
691/* dump the first 8 dwords representing the extended ROM status */
692static void hda_dsp_dump_ext_rom_status(struct snd_sof_dev *sdev, const char *level,
693 u32 flags)
694{
695 const struct sof_intel_dsp_desc *chip;
696 char msg[128];
697 int len = 0;
698 u32 value;
699 int i;
700
701 chip = get_chip_info(sdev->pdata);
702 for (i = 0; i < HDA_EXT_ROM_STATUS_SIZE; i++) {
703 value = snd_sof_dsp_read(sdev, HDA_DSP_BAR, chip->rom_status_reg + i * 0x4);
704 len += scnprintf(msg + len, sizeof(msg) - len, " 0x%x", value);
705 }
706
707 dev_printk(level, sdev->dev, "extended rom status: %s", msg);
708
709}
710
711void hda_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
712{
713 char *level = (flags & SOF_DBG_DUMP_OPTIONAL) ? KERN_DEBUG : KERN_ERR;
714 struct sof_ipc_dsp_oops_xtensa xoops;
715 struct sof_ipc_panic_info panic_info;
716 u32 stack[HDA_DSP_STACK_DUMP_SIZE];
717
718 /* print ROM/FW status */
719 hda_dsp_get_state(sdev, level);
720
721 /* The firmware register dump only available with IPC3 */
722 if (flags & SOF_DBG_DUMP_REGS && sdev->pdata->ipc_type == SOF_IPC_TYPE_3) {
723 u32 status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_STATUS);
724 u32 panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_TRACEP);
725
726 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
727 HDA_DSP_STACK_DUMP_SIZE);
728 sof_print_oops_and_stack(sdev, level, status, panic, &xoops,
729 &panic_info, stack, HDA_DSP_STACK_DUMP_SIZE);
730 } else {
731 hda_dsp_dump_ext_rom_status(sdev, level, flags);
732 }
733}
734
735void hda_ipc4_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
736{
737 char *level = (flags & SOF_DBG_DUMP_OPTIONAL) ? KERN_DEBUG : KERN_ERR;
738
739 /* print ROM/FW status */
740 hda_dsp_get_state(sdev, level);
741
742 if (flags & SOF_DBG_DUMP_REGS)
743 sof_ipc4_intel_dump_telemetry_state(sdev, flags);
744 else
745 hda_dsp_dump_ext_rom_status(sdev, level, flags);
746}
747
748static bool hda_check_ipc_irq(struct snd_sof_dev *sdev)
749{
750 const struct sof_intel_dsp_desc *chip;
751
752 chip = get_chip_info(sdev->pdata);
753 if (chip && chip->check_ipc_irq)
754 return chip->check_ipc_irq(sdev);
755
756 return false;
757}
758
759void hda_ipc_irq_dump(struct snd_sof_dev *sdev)
760{
761 u32 adspis;
762 u32 intsts;
763 u32 intctl;
764 u32 ppsts;
765 u8 rirbsts;
766
767 /* read key IRQ stats and config registers */
768 adspis = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
769 intsts = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS);
770 intctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL);
771 ppsts = snd_sof_dsp_read(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPSTS);
772 rirbsts = snd_sof_dsp_read8(sdev, HDA_DSP_HDA_BAR, AZX_REG_RIRBSTS);
773
774 dev_err(sdev->dev, "hda irq intsts 0x%8.8x intlctl 0x%8.8x rirb %2.2x\n",
775 intsts, intctl, rirbsts);
776 dev_err(sdev->dev, "dsp irq ppsts 0x%8.8x adspis 0x%8.8x\n", ppsts, adspis);
777}
778
779void hda_ipc_dump(struct snd_sof_dev *sdev)
780{
781 u32 hipcie;
782 u32 hipct;
783 u32 hipcctl;
784
785 hda_ipc_irq_dump(sdev);
786
787 /* read IPC status */
788 hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
789 hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
790 hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL);
791
792 /* dump the IPC regs */
793 /* TODO: parse the raw msg */
794 dev_err(sdev->dev, "host status 0x%8.8x dsp status 0x%8.8x mask 0x%8.8x\n",
795 hipcie, hipct, hipcctl);
796}
797
798void hda_ipc4_dump(struct snd_sof_dev *sdev)
799{
800 u32 hipci, hipcie, hipct, hipcte, hipcctl;
801
802 hda_ipc_irq_dump(sdev);
803
804 hipci = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCI);
805 hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
806 hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
807 hipcte = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCTE);
808 hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL);
809
810 /* dump the IPC regs */
811 /* TODO: parse the raw msg */
812 dev_err(sdev->dev, "Host IPC initiator: %#x|%#x, target: %#x|%#x, ctl: %#x\n",
813 hipci, hipcie, hipct, hipcte, hipcctl);
814}
815
816bool hda_ipc4_tx_is_busy(struct snd_sof_dev *sdev)
817{
818 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
819 const struct sof_intel_dsp_desc *chip = hda->desc;
820 u32 val;
821
822 val = snd_sof_dsp_read(sdev, HDA_DSP_BAR, chip->ipc_req);
823
824 return !!(val & chip->ipc_req_mask);
825}
826
827static int hda_init(struct snd_sof_dev *sdev)
828{
829 struct hda_bus *hbus;
830 struct hdac_bus *bus;
831 struct pci_dev *pci = to_pci_dev(sdev->dev);
832 int ret;
833
834 hbus = sof_to_hbus(sdev);
835 bus = sof_to_bus(sdev);
836
837 /* HDA bus init */
838 sof_hda_bus_init(sdev, &pci->dev);
839
840 if (sof_hda_position_quirk == SOF_HDA_POSITION_QUIRK_USE_DPIB_REGISTERS)
841 bus->use_posbuf = 0;
842 else
843 bus->use_posbuf = 1;
844 bus->bdl_pos_adj = 0;
845 bus->sync_write = 1;
846
847 mutex_init(&hbus->prepare_mutex);
848 hbus->pci = pci;
849 hbus->mixer_assigned = -1;
850 hbus->modelname = hda_model;
851
852 /* initialise hdac bus */
853 bus->addr = pci_resource_start(pci, 0);
854 bus->remap_addr = pci_ioremap_bar(pci, 0);
855 if (!bus->remap_addr) {
856 dev_err(bus->dev, "error: ioremap error\n");
857 return -ENXIO;
858 }
859
860 /* HDA base */
861 sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
862
863 /* init i915 and HDMI codecs */
864 ret = hda_codec_i915_init(sdev);
865 if (ret < 0 && ret != -ENODEV) {
866 dev_err_probe(sdev->dev, ret, "init of i915 and HDMI codec failed\n");
867 goto out;
868 }
869
870 /* get controller capabilities */
871 ret = hda_dsp_ctrl_get_caps(sdev);
872 if (ret < 0) {
873 dev_err(sdev->dev, "error: get caps error\n");
874 hda_codec_i915_exit(sdev);
875 }
876
877out:
878 if (ret < 0)
879 iounmap(sof_to_bus(sdev)->remap_addr);
880
881 return ret;
882}
883
884static int check_dmic_num(struct snd_sof_dev *sdev)
885{
886 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
887 struct nhlt_acpi_table *nhlt;
888 int dmic_num = 0;
889
890 nhlt = hdev->nhlt;
891 if (nhlt)
892 dmic_num = intel_nhlt_get_dmic_geo(sdev->dev, nhlt);
893
894 /* allow for module parameter override */
895 if (dmic_num_override != -1) {
896 dev_dbg(sdev->dev,
897 "overriding DMICs detected in NHLT tables %d by kernel param %d\n",
898 dmic_num, dmic_num_override);
899 dmic_num = dmic_num_override;
900 }
901
902 if (dmic_num < 0 || dmic_num > 4) {
903 dev_dbg(sdev->dev, "invalid dmic_number %d\n", dmic_num);
904 dmic_num = 0;
905 }
906
907 return dmic_num;
908}
909
910static int check_nhlt_ssp_mask(struct snd_sof_dev *sdev)
911{
912 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
913 struct nhlt_acpi_table *nhlt;
914 int ssp_mask = 0;
915
916 nhlt = hdev->nhlt;
917 if (!nhlt)
918 return ssp_mask;
919
920 if (intel_nhlt_has_endpoint_type(nhlt, NHLT_LINK_SSP)) {
921 ssp_mask = intel_nhlt_ssp_endpoint_mask(nhlt, NHLT_DEVICE_I2S);
922 if (ssp_mask)
923 dev_info(sdev->dev, "NHLT_DEVICE_I2S detected, ssp_mask %#x\n", ssp_mask);
924 }
925
926 return ssp_mask;
927}
928
929static int check_nhlt_ssp_mclk_mask(struct snd_sof_dev *sdev, int ssp_num)
930{
931 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
932 struct nhlt_acpi_table *nhlt;
933
934 nhlt = hdev->nhlt;
935 if (!nhlt)
936 return 0;
937
938 return intel_nhlt_ssp_mclk_mask(nhlt, ssp_num);
939}
940
941#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC) || IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
942
943static const char *fixup_tplg_name(struct snd_sof_dev *sdev,
944 const char *sof_tplg_filename,
945 const char *idisp_str,
946 const char *dmic_str)
947{
948 const char *tplg_filename = NULL;
949 char *filename, *tmp;
950 const char *split_ext;
951
952 filename = kstrdup(sof_tplg_filename, GFP_KERNEL);
953 if (!filename)
954 return NULL;
955
956 /* this assumes a .tplg extension */
957 tmp = filename;
958 split_ext = strsep(&tmp, ".");
959 if (split_ext)
960 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
961 "%s%s%s.tplg",
962 split_ext, idisp_str, dmic_str);
963 kfree(filename);
964
965 return tplg_filename;
966}
967
968static int dmic_detect_topology_fixup(struct snd_sof_dev *sdev,
969 const char **tplg_filename,
970 const char *idisp_str,
971 int *dmic_found,
972 bool tplg_fixup)
973{
974 const char *dmic_str;
975 int dmic_num;
976
977 /* first check for DMICs (using NHLT or module parameter) */
978 dmic_num = check_dmic_num(sdev);
979
980 switch (dmic_num) {
981 case 1:
982 dmic_str = "-1ch";
983 break;
984 case 2:
985 dmic_str = "-2ch";
986 break;
987 case 3:
988 dmic_str = "-3ch";
989 break;
990 case 4:
991 dmic_str = "-4ch";
992 break;
993 default:
994 dmic_num = 0;
995 dmic_str = "";
996 break;
997 }
998
999 if (tplg_fixup) {
1000 const char *default_tplg_filename = *tplg_filename;
1001 const char *fixed_tplg_filename;
1002
1003 fixed_tplg_filename = fixup_tplg_name(sdev, default_tplg_filename,
1004 idisp_str, dmic_str);
1005 if (!fixed_tplg_filename)
1006 return -ENOMEM;
1007 *tplg_filename = fixed_tplg_filename;
1008 }
1009
1010 dev_info(sdev->dev, "DMICs detected in NHLT tables: %d\n", dmic_num);
1011 *dmic_found = dmic_num;
1012
1013 return 0;
1014}
1015#endif
1016
1017static int hda_init_caps(struct snd_sof_dev *sdev)
1018{
1019 u32 interface_mask = hda_get_interface_mask(sdev);
1020 struct hdac_bus *bus = sof_to_bus(sdev);
1021 struct snd_sof_pdata *pdata = sdev->pdata;
1022 struct sof_intel_hda_dev *hdev = pdata->hw_pdata;
1023 u32 link_mask;
1024 int ret = 0;
1025
1026 /* check if dsp is there */
1027 if (bus->ppcap)
1028 dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
1029
1030 /* Init HDA controller after i915 init */
1031 ret = hda_dsp_ctrl_init_chip(sdev);
1032 if (ret < 0) {
1033 dev_err(bus->dev, "error: init chip failed with ret: %d\n",
1034 ret);
1035 return ret;
1036 }
1037
1038 hda_bus_ml_init(bus);
1039
1040 /* Skip SoundWire if it is not supported */
1041 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
1042 goto skip_soundwire;
1043
1044 /* scan SoundWire capabilities exposed by DSDT */
1045 ret = hda_sdw_acpi_scan(sdev);
1046 if (ret < 0) {
1047 dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
1048 goto skip_soundwire;
1049 }
1050
1051 link_mask = hdev->info.link_mask;
1052 if (!link_mask) {
1053 dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n");
1054 goto skip_soundwire;
1055 }
1056
1057 /*
1058 * probe/allocate SoundWire resources.
1059 * The hardware configuration takes place in hda_sdw_startup
1060 * after power rails are enabled.
1061 * It's entirely possible to have a mix of I2S/DMIC/SoundWire
1062 * devices, so we allocate the resources in all cases.
1063 */
1064 ret = hda_sdw_probe(sdev);
1065 if (ret < 0) {
1066 dev_err(sdev->dev, "error: SoundWire probe error\n");
1067 return ret;
1068 }
1069
1070skip_soundwire:
1071
1072 /* create codec instances */
1073 hda_codec_probe_bus(sdev);
1074
1075 if (!HDA_IDISP_CODEC(bus->codec_mask))
1076 hda_codec_i915_display_power(sdev, false);
1077
1078 hda_bus_ml_put_all(bus);
1079
1080 return 0;
1081}
1082
1083static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
1084{
1085 struct snd_sof_dev *sdev = context;
1086
1087 /*
1088 * Get global interrupt status. It includes all hardware interrupt
1089 * sources in the Intel HD Audio controller.
1090 */
1091 if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
1092 SOF_HDA_INTSTS_GIS) {
1093
1094 /* disable GIE interrupt */
1095 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
1096 SOF_HDA_INTCTL,
1097 SOF_HDA_INT_GLOBAL_EN,
1098 0);
1099
1100 return IRQ_WAKE_THREAD;
1101 }
1102
1103 return IRQ_NONE;
1104}
1105
1106static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
1107{
1108 struct snd_sof_dev *sdev = context;
1109 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
1110
1111 /* deal with streams and controller first */
1112 if (hda_dsp_check_stream_irq(sdev)) {
1113 trace_sof_intel_hda_irq(sdev, "stream");
1114 hda_dsp_stream_threaded_handler(irq, sdev);
1115 }
1116
1117 if (hda_check_ipc_irq(sdev)) {
1118 trace_sof_intel_hda_irq(sdev, "ipc");
1119 sof_ops(sdev)->irq_thread(irq, sdev);
1120 }
1121
1122 if (hda_dsp_check_sdw_irq(sdev)) {
1123 trace_sof_intel_hda_irq(sdev, "sdw");
1124 hda_dsp_sdw_thread(irq, hdev->sdw);
1125 }
1126
1127 if (hda_sdw_check_wakeen_irq(sdev)) {
1128 trace_sof_intel_hda_irq(sdev, "wakeen");
1129 hda_sdw_process_wakeen(sdev);
1130 }
1131
1132 hda_codec_check_for_state_change(sdev);
1133
1134 /* enable GIE interrupt */
1135 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
1136 SOF_HDA_INTCTL,
1137 SOF_HDA_INT_GLOBAL_EN,
1138 SOF_HDA_INT_GLOBAL_EN);
1139
1140 return IRQ_HANDLED;
1141}
1142
1143int hda_dsp_probe_early(struct snd_sof_dev *sdev)
1144{
1145 struct pci_dev *pci = to_pci_dev(sdev->dev);
1146 struct sof_intel_hda_dev *hdev;
1147 const struct sof_intel_dsp_desc *chip;
1148 int ret = 0;
1149
1150 if (!sdev->dspless_mode_selected) {
1151 /*
1152 * detect DSP by checking class/subclass/prog-id information
1153 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
1154 * class=04 subclass 01 prog-if 00: DSP is present
1155 * (and may be required e.g. for DMIC or SSP support)
1156 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
1157 */
1158 if (pci->class == 0x040300) {
1159 dev_err(sdev->dev, "the DSP is not enabled on this platform, aborting probe\n");
1160 return -ENODEV;
1161 } else if (pci->class != 0x040100 && pci->class != 0x040380) {
1162 dev_err(sdev->dev, "unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n",
1163 pci->class);
1164 return -ENODEV;
1165 }
1166 dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n",
1167 pci->class);
1168 }
1169
1170 chip = get_chip_info(sdev->pdata);
1171 if (!chip) {
1172 dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
1173 pci->device);
1174 ret = -EIO;
1175 goto err;
1176 }
1177
1178 sdev->num_cores = chip->cores_num;
1179
1180 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
1181 if (!hdev)
1182 return -ENOMEM;
1183 sdev->pdata->hw_pdata = hdev;
1184 hdev->desc = chip;
1185 ret = hda_init(sdev);
1186
1187err:
1188 return ret;
1189}
1190
1191int hda_dsp_probe(struct snd_sof_dev *sdev)
1192{
1193 struct pci_dev *pci = to_pci_dev(sdev->dev);
1194 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
1195 int ret = 0;
1196
1197 hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
1198 PLATFORM_DEVID_NONE,
1199 NULL, 0);
1200 if (IS_ERR(hdev->dmic_dev)) {
1201 dev_err(sdev->dev, "error: failed to create DMIC device\n");
1202 return PTR_ERR(hdev->dmic_dev);
1203 }
1204
1205 /*
1206 * use position update IPC if either it is forced
1207 * or we don't have other choice
1208 */
1209#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
1210 hdev->no_ipc_position = 0;
1211#else
1212 hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
1213#endif
1214
1215 if (sdev->dspless_mode_selected)
1216 hdev->no_ipc_position = 1;
1217
1218 if (sdev->dspless_mode_selected)
1219 goto skip_dsp_setup;
1220
1221 /* DSP base */
1222 sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
1223 if (!sdev->bar[HDA_DSP_BAR]) {
1224 dev_err(sdev->dev, "error: ioremap error\n");
1225 ret = -ENXIO;
1226 goto hdac_bus_unmap;
1227 }
1228
1229 sdev->mmio_bar = HDA_DSP_BAR;
1230 sdev->mailbox_bar = HDA_DSP_BAR;
1231skip_dsp_setup:
1232
1233 /* allow 64bit DMA address if supported by H/W */
1234 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(64))) {
1235 dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
1236 dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32));
1237 }
1238 dma_set_max_seg_size(&pci->dev, UINT_MAX);
1239
1240 /* init streams */
1241 ret = hda_dsp_stream_init(sdev);
1242 if (ret < 0) {
1243 dev_err(sdev->dev, "error: failed to init streams\n");
1244 /*
1245 * not all errors are due to memory issues, but trying
1246 * to free everything does not harm
1247 */
1248 goto free_streams;
1249 }
1250
1251 /*
1252 * register our IRQ
1253 * let's try to enable msi firstly
1254 * if it fails, use legacy interrupt mode
1255 * TODO: support msi multiple vectors
1256 */
1257 if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
1258 dev_info(sdev->dev, "use msi interrupt mode\n");
1259 sdev->ipc_irq = pci_irq_vector(pci, 0);
1260 /* initialised to "false" by kzalloc() */
1261 sdev->msi_enabled = true;
1262 }
1263
1264 if (!sdev->msi_enabled) {
1265 dev_info(sdev->dev, "use legacy interrupt mode\n");
1266 /*
1267 * in IO-APIC mode, hda->irq and ipc_irq are using the same
1268 * irq number of pci->irq
1269 */
1270 sdev->ipc_irq = pci->irq;
1271 }
1272
1273 dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
1274 ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
1275 hda_dsp_interrupt_thread,
1276 IRQF_SHARED, "AudioDSP", sdev);
1277 if (ret < 0) {
1278 dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
1279 sdev->ipc_irq);
1280 goto free_irq_vector;
1281 }
1282
1283 pci_set_master(pci);
1284 synchronize_irq(pci->irq);
1285
1286 /*
1287 * clear TCSEL to clear playback on some HD Audio
1288 * codecs. PCI TCSEL is defined in the Intel manuals.
1289 */
1290 snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
1291
1292 /* init HDA capabilities */
1293 ret = hda_init_caps(sdev);
1294 if (ret < 0)
1295 goto free_ipc_irq;
1296
1297 if (!sdev->dspless_mode_selected) {
1298 /* enable ppcap interrupt */
1299 hda_dsp_ctrl_ppcap_enable(sdev, true);
1300 hda_dsp_ctrl_ppcap_int_enable(sdev, true);
1301
1302 /* set default mailbox offset for FW ready message */
1303 sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
1304
1305 INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
1306 }
1307
1308 init_waitqueue_head(&hdev->waitq);
1309
1310 hdev->nhlt = intel_nhlt_init(sdev->dev);
1311
1312 return 0;
1313
1314free_ipc_irq:
1315 free_irq(sdev->ipc_irq, sdev);
1316free_irq_vector:
1317 if (sdev->msi_enabled)
1318 pci_free_irq_vectors(pci);
1319free_streams:
1320 hda_dsp_stream_free(sdev);
1321/* dsp_unmap: not currently used */
1322 if (!sdev->dspless_mode_selected)
1323 iounmap(sdev->bar[HDA_DSP_BAR]);
1324hdac_bus_unmap:
1325 platform_device_unregister(hdev->dmic_dev);
1326
1327 return ret;
1328}
1329
1330void hda_dsp_remove(struct snd_sof_dev *sdev)
1331{
1332 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
1333 const struct sof_intel_dsp_desc *chip = hda->desc;
1334 struct pci_dev *pci = to_pci_dev(sdev->dev);
1335 struct nhlt_acpi_table *nhlt = hda->nhlt;
1336
1337 if (nhlt)
1338 intel_nhlt_free(nhlt);
1339
1340 if (!sdev->dspless_mode_selected)
1341 /* cancel any attempt for DSP D0I3 */
1342 cancel_delayed_work_sync(&hda->d0i3_work);
1343
1344 hda_codec_device_remove(sdev);
1345
1346 hda_sdw_exit(sdev);
1347
1348 if (!IS_ERR_OR_NULL(hda->dmic_dev))
1349 platform_device_unregister(hda->dmic_dev);
1350
1351 if (!sdev->dspless_mode_selected) {
1352 /* disable DSP IRQ */
1353 hda_dsp_ctrl_ppcap_int_enable(sdev, false);
1354 }
1355
1356 /* disable CIE and GIE interrupts */
1357 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
1358 SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
1359
1360 if (sdev->dspless_mode_selected)
1361 goto skip_disable_dsp;
1362
1363 /* no need to check for error as the DSP will be disabled anyway */
1364 if (chip && chip->power_down_dsp)
1365 chip->power_down_dsp(sdev);
1366
1367 /* disable DSP */
1368 hda_dsp_ctrl_ppcap_enable(sdev, false);
1369
1370skip_disable_dsp:
1371 free_irq(sdev->ipc_irq, sdev);
1372 if (sdev->msi_enabled)
1373 pci_free_irq_vectors(pci);
1374
1375 hda_dsp_stream_free(sdev);
1376
1377 hda_bus_ml_free(sof_to_bus(sdev));
1378
1379 if (!sdev->dspless_mode_selected)
1380 iounmap(sdev->bar[HDA_DSP_BAR]);
1381}
1382
1383void hda_dsp_remove_late(struct snd_sof_dev *sdev)
1384{
1385 iounmap(sof_to_bus(sdev)->remap_addr);
1386 sof_hda_bus_exit(sdev);
1387 hda_codec_i915_exit(sdev);
1388}
1389
1390int hda_power_down_dsp(struct snd_sof_dev *sdev)
1391{
1392 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
1393 const struct sof_intel_dsp_desc *chip = hda->desc;
1394
1395 return hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
1396}
1397
1398#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
1399static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1400 struct snd_soc_acpi_mach **mach)
1401{
1402 struct hdac_bus *bus = sof_to_bus(sdev);
1403 struct snd_soc_acpi_mach_params *mach_params;
1404 struct snd_soc_acpi_mach *hda_mach;
1405 struct snd_sof_pdata *pdata = sdev->pdata;
1406 const char *tplg_filename;
1407 const char *idisp_str;
1408 int dmic_num = 0;
1409 int codec_num = 0;
1410 int ret;
1411 int i;
1412
1413 /* codec detection */
1414 if (!bus->codec_mask) {
1415 dev_info(bus->dev, "no hda codecs found!\n");
1416 } else {
1417 dev_info(bus->dev, "hda codecs found, mask %lx\n",
1418 bus->codec_mask);
1419
1420 for (i = 0; i < HDA_MAX_CODECS; i++) {
1421 if (bus->codec_mask & (1 << i))
1422 codec_num++;
1423 }
1424
1425 /*
1426 * If no machine driver is found, then:
1427 *
1428 * generic hda machine driver can handle:
1429 * - one HDMI codec, and/or
1430 * - one external HDAudio codec
1431 */
1432 if (!*mach && codec_num <= 2) {
1433 bool tplg_fixup;
1434
1435 hda_mach = snd_soc_acpi_intel_hda_machines;
1436
1437 dev_info(bus->dev, "using HDA machine driver %s now\n",
1438 hda_mach->drv_name);
1439
1440 if (codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask))
1441 idisp_str = "-idisp";
1442 else
1443 idisp_str = "";
1444
1445 /* topology: use the info from hda_machines */
1446 if (pdata->tplg_filename) {
1447 tplg_fixup = false;
1448 tplg_filename = pdata->tplg_filename;
1449 } else {
1450 tplg_fixup = true;
1451 tplg_filename = hda_mach->sof_tplg_filename;
1452 }
1453 ret = dmic_detect_topology_fixup(sdev, &tplg_filename, idisp_str, &dmic_num,
1454 tplg_fixup);
1455 if (ret < 0)
1456 return;
1457
1458 hda_mach->mach_params.dmic_num = dmic_num;
1459 pdata->tplg_filename = tplg_filename;
1460
1461 if (codec_num == 2 ||
1462 (codec_num == 1 && !HDA_IDISP_CODEC(bus->codec_mask))) {
1463 /*
1464 * Prevent SoundWire links from starting when an external
1465 * HDaudio codec is used
1466 */
1467 hda_mach->mach_params.link_mask = 0;
1468 } else {
1469 /*
1470 * Allow SoundWire links to start when no external HDaudio codec
1471 * was detected. This will not create a SoundWire card but
1472 * will help detect if any SoundWire codec reports as ATTACHED.
1473 */
1474 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
1475
1476 hda_mach->mach_params.link_mask = hdev->info.link_mask;
1477 }
1478
1479 *mach = hda_mach;
1480 }
1481 }
1482
1483 /* used by hda machine driver to create dai links */
1484 if (*mach) {
1485 mach_params = &(*mach)->mach_params;
1486 mach_params->codec_mask = bus->codec_mask;
1487 mach_params->common_hdmi_codec_drv = true;
1488 }
1489}
1490#else
1491static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1492 struct snd_soc_acpi_mach **mach)
1493{
1494}
1495#endif
1496
1497#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1498
1499static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1500{
1501 struct snd_sof_pdata *pdata = sdev->pdata;
1502 const struct snd_soc_acpi_link_adr *link;
1503 struct snd_soc_acpi_mach *mach;
1504 struct sof_intel_hda_dev *hdev;
1505 u32 link_mask;
1506 int i;
1507
1508 hdev = pdata->hw_pdata;
1509 link_mask = hdev->info.link_mask;
1510
1511 /*
1512 * Select SoundWire machine driver if needed using the
1513 * alternate tables. This case deals with SoundWire-only
1514 * machines, for mixed cases with I2C/I2S the detection relies
1515 * on the HID list.
1516 */
1517 if (link_mask) {
1518 for (mach = pdata->desc->alt_machines;
1519 mach && mach->link_mask; mach++) {
1520 /*
1521 * On some platforms such as Up Extreme all links
1522 * are enabled but only one link can be used by
1523 * external codec. Instead of exact match of two masks,
1524 * first check whether link_mask of mach is subset of
1525 * link_mask supported by hw and then go on searching
1526 * link_adr
1527 */
1528 if (~link_mask & mach->link_mask)
1529 continue;
1530
1531 /* No need to match adr if there is no links defined */
1532 if (!mach->links)
1533 break;
1534
1535 link = mach->links;
1536 for (i = 0; i < hdev->info.count && link->num_adr;
1537 i++, link++) {
1538 /*
1539 * Try next machine if any expected Slaves
1540 * are not found on this link.
1541 */
1542 if (!snd_soc_acpi_sdw_link_slaves_found(sdev->dev, link,
1543 hdev->sdw->ids,
1544 hdev->sdw->num_slaves))
1545 break;
1546 }
1547 /* Found if all Slaves are checked */
1548 if (i == hdev->info.count || !link->num_adr)
1549 break;
1550 }
1551 if (mach && mach->link_mask) {
1552 int dmic_num = 0;
1553 bool tplg_fixup;
1554 const char *tplg_filename;
1555
1556 mach->mach_params.links = mach->links;
1557 mach->mach_params.link_mask = mach->link_mask;
1558 mach->mach_params.platform = dev_name(sdev->dev);
1559
1560 if (pdata->tplg_filename) {
1561 tplg_fixup = false;
1562 } else {
1563 tplg_fixup = true;
1564 tplg_filename = mach->sof_tplg_filename;
1565 }
1566
1567 /*
1568 * DMICs use up to 4 pins and are typically pin-muxed with SoundWire
1569 * link 2 and 3, or link 1 and 2, thus we only try to enable dmics
1570 * if all conditions are true:
1571 * a) 2 or fewer links are used by SoundWire
1572 * b) the NHLT table reports the presence of microphones
1573 */
1574 if (hweight_long(mach->link_mask) <= 2) {
1575 int ret;
1576
1577 ret = dmic_detect_topology_fixup(sdev, &tplg_filename, "",
1578 &dmic_num, tplg_fixup);
1579 if (ret < 0)
1580 return NULL;
1581 }
1582 if (tplg_fixup)
1583 pdata->tplg_filename = tplg_filename;
1584 mach->mach_params.dmic_num = dmic_num;
1585
1586 dev_dbg(sdev->dev,
1587 "SoundWire machine driver %s topology %s\n",
1588 mach->drv_name,
1589 pdata->tplg_filename);
1590
1591 return mach;
1592 }
1593
1594 dev_info(sdev->dev, "No SoundWire machine driver found\n");
1595 }
1596
1597 return NULL;
1598}
1599#else
1600static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1601{
1602 return NULL;
1603}
1604#endif
1605
1606void hda_set_mach_params(struct snd_soc_acpi_mach *mach,
1607 struct snd_sof_dev *sdev)
1608{
1609 struct snd_sof_pdata *pdata = sdev->pdata;
1610 const struct sof_dev_desc *desc = pdata->desc;
1611 struct snd_soc_acpi_mach_params *mach_params;
1612
1613 mach_params = &mach->mach_params;
1614 mach_params->platform = dev_name(sdev->dev);
1615 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
1616 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
1617 mach_params->num_dai_drivers = SOF_SKL_NUM_DAIS_NOCODEC;
1618 else
1619 mach_params->num_dai_drivers = desc->ops->num_drv;
1620 mach_params->dai_drivers = desc->ops->drv;
1621}
1622
1623struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev)
1624{
1625 u32 interface_mask = hda_get_interface_mask(sdev);
1626 struct snd_sof_pdata *sof_pdata = sdev->pdata;
1627 const struct sof_dev_desc *desc = sof_pdata->desc;
1628 struct snd_soc_acpi_mach *mach = NULL;
1629 const char *tplg_filename;
1630
1631 /* Try I2S or DMIC if it is supported */
1632 if (interface_mask & (BIT(SOF_DAI_INTEL_SSP) | BIT(SOF_DAI_INTEL_DMIC)))
1633 mach = snd_soc_acpi_find_machine(desc->machines);
1634
1635 if (mach) {
1636 bool add_extension = false;
1637 bool tplg_fixup = false;
1638
1639 /*
1640 * If tplg file name is overridden, use it instead of
1641 * the one set in mach table
1642 */
1643 if (!sof_pdata->tplg_filename) {
1644 sof_pdata->tplg_filename = mach->sof_tplg_filename;
1645 tplg_fixup = true;
1646 }
1647
1648 /* report to machine driver if any DMICs are found */
1649 mach->mach_params.dmic_num = check_dmic_num(sdev);
1650
1651 if (tplg_fixup &&
1652 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER &&
1653 mach->mach_params.dmic_num) {
1654 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1655 "%s%s%d%s",
1656 sof_pdata->tplg_filename,
1657 "-dmic",
1658 mach->mach_params.dmic_num,
1659 "ch");
1660 if (!tplg_filename)
1661 return NULL;
1662
1663 sof_pdata->tplg_filename = tplg_filename;
1664 add_extension = true;
1665 }
1666
1667 if (mach->link_mask) {
1668 mach->mach_params.links = mach->links;
1669 mach->mach_params.link_mask = mach->link_mask;
1670 }
1671
1672 /* report SSP link mask to machine driver */
1673 mach->mach_params.i2s_link_mask = check_nhlt_ssp_mask(sdev);
1674
1675 if (tplg_fixup &&
1676 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER &&
1677 mach->mach_params.i2s_link_mask) {
1678 const struct sof_intel_dsp_desc *chip = get_chip_info(sdev->pdata);
1679 int ssp_num;
1680 int mclk_mask;
1681
1682 if (hweight_long(mach->mach_params.i2s_link_mask) > 1 &&
1683 !(mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_MSB))
1684 dev_warn(sdev->dev, "More than one SSP exposed by NHLT, choosing MSB\n");
1685
1686 /* fls returns 1-based results, SSPs indices are 0-based */
1687 ssp_num = fls(mach->mach_params.i2s_link_mask) - 1;
1688
1689 if (ssp_num >= chip->ssp_count) {
1690 dev_err(sdev->dev, "Invalid SSP %d, max on this platform is %d\n",
1691 ssp_num, chip->ssp_count);
1692 return NULL;
1693 }
1694
1695 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1696 "%s%s%d",
1697 sof_pdata->tplg_filename,
1698 "-ssp",
1699 ssp_num);
1700 if (!tplg_filename)
1701 return NULL;
1702
1703 sof_pdata->tplg_filename = tplg_filename;
1704 add_extension = true;
1705
1706 mclk_mask = check_nhlt_ssp_mclk_mask(sdev, ssp_num);
1707
1708 if (mclk_mask < 0) {
1709 dev_err(sdev->dev, "Invalid MCLK configuration\n");
1710 return NULL;
1711 }
1712
1713 dev_dbg(sdev->dev, "MCLK mask %#x found in NHLT\n", mclk_mask);
1714
1715 if (mclk_mask) {
1716 dev_info(sdev->dev, "Overriding topology with MCLK mask %#x from NHLT\n", mclk_mask);
1717 sdev->mclk_id_override = true;
1718 sdev->mclk_id_quirk = (mclk_mask & BIT(0)) ? 0 : 1;
1719 }
1720 }
1721
1722 if (tplg_fixup && add_extension) {
1723 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1724 "%s%s",
1725 sof_pdata->tplg_filename,
1726 ".tplg");
1727 if (!tplg_filename)
1728 return NULL;
1729
1730 sof_pdata->tplg_filename = tplg_filename;
1731 }
1732
1733 /* check if mclk_id should be modified from topology defaults */
1734 if (mclk_id_override >= 0) {
1735 dev_info(sdev->dev, "Overriding topology with MCLK %d from kernel_parameter\n", mclk_id_override);
1736 sdev->mclk_id_override = true;
1737 sdev->mclk_id_quirk = mclk_id_override;
1738 }
1739 }
1740
1741 /* If I2S fails, try SoundWire if it is supported */
1742 if (!mach && (interface_mask & BIT(SOF_DAI_INTEL_ALH)))
1743 mach = hda_sdw_machine_select(sdev);
1744
1745 /*
1746 * Choose HDA generic machine driver if mach is NULL.
1747 * Otherwise, set certain mach params.
1748 */
1749 hda_generic_machine_select(sdev, &mach);
1750 if (!mach)
1751 dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1752
1753 return mach;
1754}
1755
1756int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1757{
1758 int ret;
1759
1760 ret = snd_intel_dsp_driver_probe(pci);
1761 if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1762 dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1763 return -ENODEV;
1764 }
1765
1766 return sof_pci_probe(pci, pci_id);
1767}
1768EXPORT_SYMBOL_NS(hda_pci_intel_probe, SND_SOC_SOF_INTEL_HDA_COMMON);
1769
1770int hda_register_clients(struct snd_sof_dev *sdev)
1771{
1772 return hda_probes_register(sdev);
1773}
1774
1775void hda_unregister_clients(struct snd_sof_dev *sdev)
1776{
1777 hda_probes_unregister(sdev);
1778}
1779
1780MODULE_LICENSE("Dual BSD/GPL");
1781MODULE_IMPORT_NS(SND_SOC_SOF_PCI_DEV);
1782MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC);
1783MODULE_IMPORT_NS(SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
1784MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
1785MODULE_IMPORT_NS(SND_INTEL_SOUNDWIRE_ACPI);
1786MODULE_IMPORT_NS(SOUNDWIRE_INTEL_INIT);
1787MODULE_IMPORT_NS(SOUNDWIRE_INTEL);
1788MODULE_IMPORT_NS(SND_SOC_SOF_HDA_MLINK);
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
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/debugfs.h>
23#include <linux/module.h>
24#include <linux/soundwire/sdw.h>
25#include <linux/soundwire/sdw_intel.h>
26#include <sound/intel-dsp-config.h>
27#include <sound/intel-nhlt.h>
28#include <sound/soc-acpi-intel-ssp-common.h>
29#include <sound/sof.h>
30#include <sound/sof/xtensa.h>
31#include <sound/hda-mlink.h>
32#include "../sof-audio.h"
33#include "../sof-pci-dev.h"
34#include "../ops.h"
35#include "../ipc4-topology.h"
36#include "hda.h"
37
38#include <trace/events/sof_intel.h>
39
40#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
41#include <sound/soc-acpi-intel-match.h>
42#endif
43
44/* platform specific devices */
45#include "shim.h"
46
47#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
48
49/*
50 * The default for SoundWire clock stop quirks is to power gate the IP
51 * and do a Bus Reset, this will need to be modified when the DSP
52 * needs to remain in D0i3 so that the Master does not lose context
53 * and enumeration is not required on clock restart
54 */
55static int sdw_clock_stop_quirks = SDW_INTEL_CLK_STOP_BUS_RESET;
56module_param(sdw_clock_stop_quirks, int, 0444);
57MODULE_PARM_DESC(sdw_clock_stop_quirks, "SOF SoundWire clock stop quirks");
58
59static int sdw_params_stream(struct device *dev,
60 struct sdw_intel_stream_params_data *params_data)
61{
62 struct snd_soc_dai *d = params_data->dai;
63 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(d, params_data->substream->stream);
64 struct snd_sof_dai_config_data data = { 0 };
65
66 if (!w) {
67 dev_err(dev, "%s widget not found, check amp link num in the topology\n",
68 d->name);
69 return -EINVAL;
70 }
71 data.dai_index = (params_data->link_id << 8) | d->id;
72 data.dai_data = params_data->alh_stream_id;
73 data.dai_node_id = data.dai_data;
74
75 return hda_dai_config(w, SOF_DAI_CONFIG_FLAGS_HW_PARAMS, &data);
76}
77
78static int sdw_params_free(struct device *dev, struct sdw_intel_stream_free_data *free_data)
79{
80 struct snd_soc_dai *d = free_data->dai;
81 struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(d, free_data->substream->stream);
82 struct snd_sof_dev *sdev = widget_to_sdev(w);
83
84 if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) {
85 struct snd_sof_widget *swidget = w->dobj.private;
86 struct snd_sof_dai *dai = swidget->private;
87 struct sof_ipc4_copier_data *copier_data;
88 struct sof_ipc4_copier *ipc4_copier;
89
90 ipc4_copier = dai->private;
91 ipc4_copier->dai_index = 0;
92 copier_data = &ipc4_copier->data;
93
94 /* clear the node ID */
95 copier_data->gtw_cfg.node_id &= ~SOF_IPC4_NODE_INDEX_MASK;
96 }
97
98 return 0;
99}
100
101struct sdw_intel_ops sdw_callback = {
102 .params_stream = sdw_params_stream,
103 .free_stream = sdw_params_free,
104};
105
106static int sdw_ace2x_params_stream(struct device *dev,
107 struct sdw_intel_stream_params_data *params_data)
108{
109 return sdw_hda_dai_hw_params(params_data->substream,
110 params_data->hw_params,
111 params_data->dai,
112 params_data->link_id,
113 params_data->alh_stream_id);
114}
115
116static int sdw_ace2x_free_stream(struct device *dev,
117 struct sdw_intel_stream_free_data *free_data)
118{
119 return sdw_hda_dai_hw_free(free_data->substream,
120 free_data->dai,
121 free_data->link_id);
122}
123
124static int sdw_ace2x_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
125{
126 return sdw_hda_dai_trigger(substream, cmd, dai);
127}
128
129static struct sdw_intel_ops sdw_ace2x_callback = {
130 .params_stream = sdw_ace2x_params_stream,
131 .free_stream = sdw_ace2x_free_stream,
132 .trigger = sdw_ace2x_trigger,
133};
134
135static int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
136{
137 u32 interface_mask = hda_get_interface_mask(sdev);
138 struct sof_intel_hda_dev *hdev;
139 acpi_handle handle;
140 int ret;
141
142 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
143 return -EINVAL;
144
145 handle = ACPI_HANDLE(sdev->dev);
146
147 /* save ACPI info for the probe step */
148 hdev = sdev->pdata->hw_pdata;
149
150 ret = sdw_intel_acpi_scan(handle, &hdev->info);
151 if (ret < 0)
152 return -EINVAL;
153
154 return 0;
155}
156
157static int hda_sdw_probe(struct snd_sof_dev *sdev)
158{
159 const struct sof_intel_dsp_desc *chip;
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 chip = get_chip_info(sdev->pdata);
169 if (chip->hw_ip_version < SOF_INTEL_ACE_2_0) {
170 res.mmio_base = sdev->bar[HDA_DSP_BAR];
171 res.hw_ops = &sdw_intel_cnl_hw_ops;
172 res.shim_base = hdev->desc->sdw_shim_base;
173 res.alh_base = hdev->desc->sdw_alh_base;
174 res.ext = false;
175 res.ops = &sdw_callback;
176 } else {
177 /*
178 * retrieve eml_lock needed to protect shared registers
179 * in the HDaudio multi-link areas
180 */
181 res.eml_lock = hdac_bus_eml_get_mutex(sof_to_bus(sdev), true,
182 AZX_REG_ML_LEPTR_ID_SDW);
183 if (!res.eml_lock)
184 return -ENODEV;
185
186 res.mmio_base = sdev->bar[HDA_DSP_HDA_BAR];
187 /*
188 * the SHIM and SoundWire register offsets are link-specific
189 * and will be determined when adding auxiliary devices
190 */
191 res.hw_ops = &sdw_intel_lnl_hw_ops;
192 res.ext = true;
193 res.ops = &sdw_ace2x_callback;
194
195 }
196 res.irq = sdev->ipc_irq;
197 res.handle = hdev->info.handle;
198 res.parent = sdev->dev;
199
200 res.dev = sdev->dev;
201 res.clock_stop_quirks = sdw_clock_stop_quirks;
202 res.hbus = sof_to_bus(sdev);
203
204 /*
205 * ops and arg fields are not populated for now,
206 * they will be needed when the DAI callbacks are
207 * provided
208 */
209
210 /* we could filter links here if needed, e.g for quirks */
211 res.count = hdev->info.count;
212 res.link_mask = hdev->info.link_mask;
213
214 sdw = sdw_intel_probe(&res);
215 if (!sdw) {
216 dev_err(sdev->dev, "error: SoundWire probe failed\n");
217 return -EINVAL;
218 }
219
220 /* save context */
221 hdev->sdw = sdw;
222
223 return 0;
224}
225
226int hda_sdw_startup(struct snd_sof_dev *sdev)
227{
228 struct sof_intel_hda_dev *hdev;
229 struct snd_sof_pdata *pdata = sdev->pdata;
230 int ret;
231
232 hdev = sdev->pdata->hw_pdata;
233
234 if (!hdev->sdw)
235 return 0;
236
237 if (pdata->machine && !pdata->machine->mach_params.link_mask)
238 return 0;
239
240 ret = hda_sdw_check_lcount(sdev);
241 if (ret < 0)
242 return ret;
243
244 return sdw_intel_startup(hdev->sdw);
245}
246EXPORT_SYMBOL_NS(hda_sdw_startup, "SND_SOC_SOF_INTEL_HDA_GENERIC");
247
248static int hda_sdw_exit(struct snd_sof_dev *sdev)
249{
250 struct sof_intel_hda_dev *hdev;
251
252 hdev = sdev->pdata->hw_pdata;
253
254 if (hdev->sdw)
255 sdw_intel_exit(hdev->sdw);
256 hdev->sdw = NULL;
257
258 hda_sdw_int_enable(sdev, false);
259
260 return 0;
261}
262
263bool hda_common_check_sdw_irq(struct snd_sof_dev *sdev)
264{
265 struct sof_intel_hda_dev *hdev;
266 bool ret = false;
267 u32 irq_status;
268
269 hdev = sdev->pdata->hw_pdata;
270
271 if (!hdev->sdw)
272 return ret;
273
274 /* store status */
275 irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS2);
276
277 /* invalid message ? */
278 if (irq_status == 0xffffffff)
279 goto out;
280
281 /* SDW message ? */
282 if (irq_status & HDA_DSP_REG_ADSPIS2_SNDW)
283 ret = true;
284
285out:
286 return ret;
287}
288EXPORT_SYMBOL_NS(hda_common_check_sdw_irq, "SND_SOC_SOF_INTEL_HDA_GENERIC");
289
290static bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
291{
292 u32 interface_mask = hda_get_interface_mask(sdev);
293 const struct sof_intel_dsp_desc *chip;
294
295 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
296 return false;
297
298 chip = get_chip_info(sdev->pdata);
299 if (chip && chip->check_sdw_irq)
300 return chip->check_sdw_irq(sdev);
301
302 return false;
303}
304
305static irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
306{
307 return sdw_intel_thread(irq, context);
308}
309
310bool hda_sdw_check_wakeen_irq_common(struct snd_sof_dev *sdev)
311{
312 struct sof_intel_hda_dev *hdev;
313
314 hdev = sdev->pdata->hw_pdata;
315 if (hdev->sdw &&
316 snd_sof_dsp_read(sdev, HDA_DSP_BAR,
317 hdev->desc->sdw_shim_base + SDW_SHIM_WAKESTS))
318 return true;
319
320 return false;
321}
322EXPORT_SYMBOL_NS(hda_sdw_check_wakeen_irq_common, "SND_SOC_SOF_INTEL_HDA_GENERIC");
323
324static bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
325{
326 u32 interface_mask = hda_get_interface_mask(sdev);
327 const struct sof_intel_dsp_desc *chip;
328
329 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
330 return false;
331
332 chip = get_chip_info(sdev->pdata);
333 if (chip && chip->check_sdw_wakeen_irq)
334 return chip->check_sdw_wakeen_irq(sdev);
335
336 return false;
337}
338
339void hda_sdw_process_wakeen_common(struct snd_sof_dev *sdev)
340{
341 u32 interface_mask = hda_get_interface_mask(sdev);
342 struct sof_intel_hda_dev *hdev;
343
344 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
345 return;
346
347 hdev = sdev->pdata->hw_pdata;
348 if (!hdev->sdw)
349 return;
350
351 sdw_intel_process_wakeen_event(hdev->sdw);
352}
353EXPORT_SYMBOL_NS(hda_sdw_process_wakeen_common, "SND_SOC_SOF_INTEL_HDA_GENERIC");
354
355#else /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
356static inline int hda_sdw_acpi_scan(struct snd_sof_dev *sdev)
357{
358 return 0;
359}
360
361static inline int hda_sdw_probe(struct snd_sof_dev *sdev)
362{
363 return 0;
364}
365
366static inline int hda_sdw_exit(struct snd_sof_dev *sdev)
367{
368 return 0;
369}
370
371static inline bool hda_dsp_check_sdw_irq(struct snd_sof_dev *sdev)
372{
373 return false;
374}
375
376static inline irqreturn_t hda_dsp_sdw_thread(int irq, void *context)
377{
378 return IRQ_HANDLED;
379}
380
381static inline bool hda_sdw_check_wakeen_irq(struct snd_sof_dev *sdev)
382{
383 return false;
384}
385
386#endif /* IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE) */
387
388/* pre fw run operations */
389int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev)
390{
391 /* disable clock gating and power gating */
392 return hda_dsp_ctrl_clock_power_gating(sdev, false);
393}
394
395/* post fw run operations */
396int hda_dsp_post_fw_run(struct snd_sof_dev *sdev)
397{
398 int ret;
399
400 if (sdev->first_boot) {
401 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
402
403 ret = hda_sdw_startup(sdev);
404 if (ret < 0) {
405 dev_err(sdev->dev,
406 "error: could not startup SoundWire links\n");
407 return ret;
408 }
409
410 /* Check if IMR boot is usable */
411 if (!sof_debug_check_flag(SOF_DBG_IGNORE_D3_PERSISTENT) &&
412 (sdev->fw_ready.flags & SOF_IPC_INFO_D3_PERSISTENT ||
413 sdev->pdata->ipc_type == SOF_IPC_TYPE_4)) {
414 hdev->imrboot_supported = true;
415 debugfs_create_bool("skip_imr_boot",
416 0644, sdev->debugfs_root,
417 &hdev->skip_imr_boot);
418 }
419 }
420
421 hda_sdw_int_enable(sdev, true);
422
423 /* re-enable clock gating and power gating */
424 return hda_dsp_ctrl_clock_power_gating(sdev, true);
425}
426EXPORT_SYMBOL_NS(hda_dsp_post_fw_run, "SND_SOC_SOF_INTEL_HDA_GENERIC");
427
428/*
429 * Debug
430 */
431
432#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
433static bool hda_use_msi = true;
434module_param_named(use_msi, hda_use_msi, bool, 0444);
435MODULE_PARM_DESC(use_msi, "SOF HDA use PCI MSI mode");
436#else
437#define hda_use_msi (1)
438#endif
439
440static char *hda_model;
441module_param(hda_model, charp, 0444);
442MODULE_PARM_DESC(hda_model, "Use the given HDA board model.");
443
444static int dmic_num_override = -1;
445module_param_named(dmic_num, dmic_num_override, int, 0444);
446MODULE_PARM_DESC(dmic_num, "SOF HDA DMIC number");
447
448static int mclk_id_override = -1;
449module_param_named(mclk_id, mclk_id_override, int, 0444);
450MODULE_PARM_DESC(mclk_id, "SOF SSP mclk_id");
451
452static int bt_link_mask_override;
453module_param_named(bt_link_mask, bt_link_mask_override, int, 0444);
454MODULE_PARM_DESC(bt_link_mask, "SOF BT offload link mask");
455
456static int hda_init(struct snd_sof_dev *sdev)
457{
458 struct hda_bus *hbus;
459 struct hdac_bus *bus;
460 struct pci_dev *pci = to_pci_dev(sdev->dev);
461 int ret;
462
463 hbus = sof_to_hbus(sdev);
464 bus = sof_to_bus(sdev);
465
466 /* HDA bus init */
467 sof_hda_bus_init(sdev, &pci->dev);
468
469 if (sof_hda_position_quirk == SOF_HDA_POSITION_QUIRK_USE_DPIB_REGISTERS)
470 bus->use_posbuf = 0;
471 else
472 bus->use_posbuf = 1;
473 bus->bdl_pos_adj = 0;
474 bus->sync_write = 1;
475
476 mutex_init(&hbus->prepare_mutex);
477 hbus->pci = pci;
478 hbus->mixer_assigned = -1;
479 hbus->modelname = hda_model;
480
481 /* initialise hdac bus */
482 bus->addr = pci_resource_start(pci, 0);
483 bus->remap_addr = pci_ioremap_bar(pci, 0);
484 if (!bus->remap_addr) {
485 dev_err(bus->dev, "error: ioremap error\n");
486 return -ENXIO;
487 }
488
489 /* HDA base */
490 sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
491
492 /* init i915 and HDMI codecs */
493 ret = hda_codec_i915_init(sdev);
494 if (ret < 0 && ret != -ENODEV) {
495 dev_err_probe(sdev->dev, ret, "init of i915 and HDMI codec failed\n");
496 goto out;
497 }
498
499 /* get controller capabilities */
500 ret = hda_dsp_ctrl_get_caps(sdev);
501 if (ret < 0) {
502 dev_err(sdev->dev, "error: get caps error\n");
503 hda_codec_i915_exit(sdev);
504 }
505
506out:
507 if (ret < 0)
508 iounmap(sof_to_bus(sdev)->remap_addr);
509
510 return ret;
511}
512
513static int check_dmic_num(struct snd_sof_dev *sdev)
514{
515 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
516 struct nhlt_acpi_table *nhlt;
517 int dmic_num = 0;
518
519 nhlt = hdev->nhlt;
520 if (nhlt)
521 dmic_num = intel_nhlt_get_dmic_geo(sdev->dev, nhlt);
522
523 dev_info(sdev->dev, "DMICs detected in NHLT tables: %d\n", dmic_num);
524
525 /* allow for module parameter override */
526 if (dmic_num_override != -1) {
527 dev_dbg(sdev->dev,
528 "overriding DMICs detected in NHLT tables %d by kernel param %d\n",
529 dmic_num, dmic_num_override);
530 dmic_num = dmic_num_override;
531 }
532
533 if (dmic_num < 0 || dmic_num > 4) {
534 dev_dbg(sdev->dev, "invalid dmic_number %d\n", dmic_num);
535 dmic_num = 0;
536 }
537
538 return dmic_num;
539}
540
541static int check_nhlt_ssp_mask(struct snd_sof_dev *sdev, u8 device_type)
542{
543 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
544 struct nhlt_acpi_table *nhlt;
545 int ssp_mask = 0;
546
547 nhlt = hdev->nhlt;
548 if (!nhlt)
549 return ssp_mask;
550
551 if (intel_nhlt_has_endpoint_type(nhlt, NHLT_LINK_SSP)) {
552 ssp_mask = intel_nhlt_ssp_endpoint_mask(nhlt, device_type);
553 if (ssp_mask)
554 dev_info(sdev->dev, "NHLT device %s(%d) detected, ssp_mask %#x\n",
555 device_type == NHLT_DEVICE_BT ? "BT" : "I2S",
556 device_type, ssp_mask);
557 }
558
559 return ssp_mask;
560}
561
562static int check_nhlt_ssp_mclk_mask(struct snd_sof_dev *sdev, int ssp_num)
563{
564 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
565 struct nhlt_acpi_table *nhlt;
566
567 nhlt = hdev->nhlt;
568 if (!nhlt)
569 return 0;
570
571 return intel_nhlt_ssp_mclk_mask(nhlt, ssp_num);
572}
573
574static int hda_init_caps(struct snd_sof_dev *sdev)
575{
576 u32 interface_mask = hda_get_interface_mask(sdev);
577 struct hdac_bus *bus = sof_to_bus(sdev);
578 struct snd_sof_pdata *pdata = sdev->pdata;
579 struct sof_intel_hda_dev *hdev = pdata->hw_pdata;
580 u32 link_mask;
581 int ret = 0;
582
583 /* check if dsp is there */
584 if (bus->ppcap)
585 dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
586
587 /* Init HDA controller after i915 init */
588 ret = hda_dsp_ctrl_init_chip(sdev);
589 if (ret < 0) {
590 dev_err(bus->dev, "error: init chip failed with ret: %d\n",
591 ret);
592 return ret;
593 }
594
595 hda_bus_ml_init(bus);
596
597 /* Skip SoundWire if it is not supported */
598 if (!(interface_mask & BIT(SOF_DAI_INTEL_ALH)))
599 goto skip_soundwire;
600
601 /* scan SoundWire capabilities exposed by DSDT */
602 ret = hda_sdw_acpi_scan(sdev);
603 if (ret < 0) {
604 dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
605 goto skip_soundwire;
606 }
607
608 link_mask = hdev->info.link_mask;
609 if (!link_mask) {
610 dev_dbg(sdev->dev, "skipping SoundWire, no links enabled\n");
611 goto skip_soundwire;
612 }
613
614 /*
615 * probe/allocate SoundWire resources.
616 * The hardware configuration takes place in hda_sdw_startup
617 * after power rails are enabled.
618 * It's entirely possible to have a mix of I2S/DMIC/SoundWire
619 * devices, so we allocate the resources in all cases.
620 */
621 ret = hda_sdw_probe(sdev);
622 if (ret < 0) {
623 dev_err(sdev->dev, "error: SoundWire probe error\n");
624 return ret;
625 }
626
627skip_soundwire:
628
629 /* create codec instances */
630 hda_codec_probe_bus(sdev);
631
632 if (!HDA_IDISP_CODEC(bus->codec_mask))
633 hda_codec_i915_display_power(sdev, false);
634
635 hda_bus_ml_put_all(bus);
636
637 return 0;
638}
639
640static irqreturn_t hda_dsp_interrupt_handler(int irq, void *context)
641{
642 struct snd_sof_dev *sdev = context;
643
644 /*
645 * Get global interrupt status. It includes all hardware interrupt
646 * sources in the Intel HD Audio controller.
647 */
648 if (snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS) &
649 SOF_HDA_INTSTS_GIS) {
650
651 /* disable GIE interrupt */
652 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
653 SOF_HDA_INTCTL,
654 SOF_HDA_INT_GLOBAL_EN,
655 0);
656
657 return IRQ_WAKE_THREAD;
658 }
659
660 return IRQ_NONE;
661}
662
663static irqreturn_t hda_dsp_interrupt_thread(int irq, void *context)
664{
665 struct snd_sof_dev *sdev = context;
666 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
667
668 /* deal with streams and controller first */
669 if (hda_dsp_check_stream_irq(sdev)) {
670 trace_sof_intel_hda_irq(sdev, "stream");
671 hda_dsp_stream_threaded_handler(irq, sdev);
672 }
673
674 if (hda_check_ipc_irq(sdev)) {
675 trace_sof_intel_hda_irq(sdev, "ipc");
676 sof_ops(sdev)->irq_thread(irq, sdev);
677 }
678
679 if (hda_dsp_check_sdw_irq(sdev)) {
680 trace_sof_intel_hda_irq(sdev, "sdw");
681 hda_dsp_sdw_thread(irq, hdev->sdw);
682 }
683
684 if (hda_sdw_check_wakeen_irq(sdev)) {
685 trace_sof_intel_hda_irq(sdev, "wakeen");
686 hda_sdw_process_wakeen(sdev);
687 }
688
689 hda_codec_check_for_state_change(sdev);
690
691 /* enable GIE interrupt */
692 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
693 SOF_HDA_INTCTL,
694 SOF_HDA_INT_GLOBAL_EN,
695 SOF_HDA_INT_GLOBAL_EN);
696
697 return IRQ_HANDLED;
698}
699
700int hda_dsp_probe_early(struct snd_sof_dev *sdev)
701{
702 struct pci_dev *pci = to_pci_dev(sdev->dev);
703 struct sof_intel_hda_dev *hdev;
704 const struct sof_intel_dsp_desc *chip;
705 int ret = 0;
706
707 if (!sdev->dspless_mode_selected) {
708 /*
709 * detect DSP by checking class/subclass/prog-id information
710 * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
711 * class=04 subclass 01 prog-if 00: DSP is present
712 * (and may be required e.g. for DMIC or SSP support)
713 * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
714 */
715 if (pci->class == 0x040300) {
716 dev_err(sdev->dev, "the DSP is not enabled on this platform, aborting probe\n");
717 return -ENODEV;
718 } else if (pci->class != 0x040100 && pci->class != 0x040380) {
719 dev_err(sdev->dev, "unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n",
720 pci->class);
721 return -ENODEV;
722 }
723 dev_info_once(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n",
724 pci->class);
725 }
726
727 chip = get_chip_info(sdev->pdata);
728 if (!chip) {
729 dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
730 pci->device);
731 ret = -EIO;
732 goto err;
733 }
734
735 sdev->num_cores = chip->cores_num;
736
737 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
738 if (!hdev)
739 return -ENOMEM;
740 sdev->pdata->hw_pdata = hdev;
741 hdev->desc = chip;
742 ret = hda_init(sdev);
743
744err:
745 return ret;
746}
747EXPORT_SYMBOL_NS(hda_dsp_probe_early, "SND_SOC_SOF_INTEL_HDA_GENERIC");
748
749int hda_dsp_probe(struct snd_sof_dev *sdev)
750{
751 struct pci_dev *pci = to_pci_dev(sdev->dev);
752 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
753 const struct sof_intel_dsp_desc *chip;
754 int ret = 0;
755
756 hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
757 PLATFORM_DEVID_NONE,
758 NULL, 0);
759 if (IS_ERR(hdev->dmic_dev)) {
760 dev_err(sdev->dev, "error: failed to create DMIC device\n");
761 return PTR_ERR(hdev->dmic_dev);
762 }
763
764 /*
765 * use position update IPC if either it is forced
766 * or we don't have other choice
767 */
768#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
769 hdev->no_ipc_position = 0;
770#else
771 hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
772#endif
773
774 if (sdev->dspless_mode_selected)
775 hdev->no_ipc_position = 1;
776
777 if (sdev->dspless_mode_selected)
778 goto skip_dsp_setup;
779
780 /* DSP base */
781 sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
782 if (!sdev->bar[HDA_DSP_BAR]) {
783 dev_err(sdev->dev, "error: ioremap error\n");
784 ret = -ENXIO;
785 goto hdac_bus_unmap;
786 }
787
788 sdev->mmio_bar = HDA_DSP_BAR;
789 sdev->mailbox_bar = HDA_DSP_BAR;
790skip_dsp_setup:
791
792 /* allow 64bit DMA address if supported by H/W */
793 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(64))) {
794 dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
795 dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(32));
796 }
797 dma_set_max_seg_size(&pci->dev, UINT_MAX);
798
799 /* init streams */
800 ret = hda_dsp_stream_init(sdev);
801 if (ret < 0) {
802 dev_err(sdev->dev, "error: failed to init streams\n");
803 /*
804 * not all errors are due to memory issues, but trying
805 * to free everything does not harm
806 */
807 goto free_streams;
808 }
809
810 /*
811 * register our IRQ
812 * let's try to enable msi firstly
813 * if it fails, use legacy interrupt mode
814 * TODO: support msi multiple vectors
815 */
816 if (hda_use_msi && pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI) > 0) {
817 dev_info(sdev->dev, "use msi interrupt mode\n");
818 sdev->ipc_irq = pci_irq_vector(pci, 0);
819 /* initialised to "false" by kzalloc() */
820 sdev->msi_enabled = true;
821 }
822
823 if (!sdev->msi_enabled) {
824 dev_info(sdev->dev, "use legacy interrupt mode\n");
825 /*
826 * in IO-APIC mode, hda->irq and ipc_irq are using the same
827 * irq number of pci->irq
828 */
829 sdev->ipc_irq = pci->irq;
830 }
831
832 dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
833 ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_interrupt_handler,
834 hda_dsp_interrupt_thread,
835 IRQF_SHARED, "AudioDSP", sdev);
836 if (ret < 0) {
837 dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
838 sdev->ipc_irq);
839 goto free_irq_vector;
840 }
841
842 pci_set_master(pci);
843 synchronize_irq(pci->irq);
844
845 /*
846 * clear TCSEL to clear playback on some HD Audio
847 * codecs. PCI TCSEL is defined in the Intel manuals.
848 */
849 snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
850
851 /* init HDA capabilities */
852 ret = hda_init_caps(sdev);
853 if (ret < 0)
854 goto free_ipc_irq;
855
856 if (!sdev->dspless_mode_selected) {
857 /* enable ppcap interrupt */
858 hda_dsp_ctrl_ppcap_enable(sdev, true);
859 hda_dsp_ctrl_ppcap_int_enable(sdev, true);
860
861 /* set default mailbox offset for FW ready message */
862 sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
863
864 INIT_DELAYED_WORK(&hdev->d0i3_work, hda_dsp_d0i3_work);
865 }
866
867 chip = get_chip_info(sdev->pdata);
868 if (chip && chip->hw_ip_version >= SOF_INTEL_ACE_2_0) {
869 ret = hda_sdw_startup(sdev);
870 if (ret < 0) {
871 dev_err(sdev->dev, "could not startup SoundWire links\n");
872 goto disable_pp_cap;
873 }
874 }
875
876 init_waitqueue_head(&hdev->waitq);
877
878 hdev->nhlt = intel_nhlt_init(sdev->dev);
879
880 return 0;
881
882disable_pp_cap:
883 if (!sdev->dspless_mode_selected) {
884 hda_dsp_ctrl_ppcap_int_enable(sdev, false);
885 hda_dsp_ctrl_ppcap_enable(sdev, false);
886 }
887free_ipc_irq:
888 free_irq(sdev->ipc_irq, sdev);
889free_irq_vector:
890 if (sdev->msi_enabled)
891 pci_free_irq_vectors(pci);
892free_streams:
893 hda_dsp_stream_free(sdev);
894/* dsp_unmap: not currently used */
895 if (!sdev->dspless_mode_selected)
896 iounmap(sdev->bar[HDA_DSP_BAR]);
897hdac_bus_unmap:
898 platform_device_unregister(hdev->dmic_dev);
899
900 return ret;
901}
902EXPORT_SYMBOL_NS(hda_dsp_probe, "SND_SOC_SOF_INTEL_HDA_GENERIC");
903
904void hda_dsp_remove(struct snd_sof_dev *sdev)
905{
906 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
907 const struct sof_intel_dsp_desc *chip = hda->desc;
908 struct pci_dev *pci = to_pci_dev(sdev->dev);
909 struct nhlt_acpi_table *nhlt = hda->nhlt;
910
911 if (nhlt)
912 intel_nhlt_free(nhlt);
913
914 if (!sdev->dspless_mode_selected)
915 /* cancel any attempt for DSP D0I3 */
916 cancel_delayed_work_sync(&hda->d0i3_work);
917
918 hda_codec_device_remove(sdev);
919
920 hda_sdw_exit(sdev);
921
922 if (!IS_ERR_OR_NULL(hda->dmic_dev))
923 platform_device_unregister(hda->dmic_dev);
924
925 if (!sdev->dspless_mode_selected) {
926 /* disable DSP IRQ */
927 hda_dsp_ctrl_ppcap_int_enable(sdev, false);
928 }
929
930 /* disable CIE and GIE interrupts */
931 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
932 SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
933
934 if (sdev->dspless_mode_selected)
935 goto skip_disable_dsp;
936
937 /* no need to check for error as the DSP will be disabled anyway */
938 if (chip && chip->power_down_dsp)
939 chip->power_down_dsp(sdev);
940
941 /* disable DSP */
942 hda_dsp_ctrl_ppcap_enable(sdev, false);
943
944 /* Free the persistent DMA buffers used for base firmware download */
945 if (hda->cl_dmab.area)
946 snd_dma_free_pages(&hda->cl_dmab);
947 if (hda->iccmax_dmab.area)
948 snd_dma_free_pages(&hda->iccmax_dmab);
949
950skip_disable_dsp:
951 free_irq(sdev->ipc_irq, sdev);
952 if (sdev->msi_enabled)
953 pci_free_irq_vectors(pci);
954
955 hda_dsp_stream_free(sdev);
956
957 hda_bus_ml_free(sof_to_bus(sdev));
958
959 if (!sdev->dspless_mode_selected)
960 iounmap(sdev->bar[HDA_DSP_BAR]);
961}
962EXPORT_SYMBOL_NS(hda_dsp_remove, "SND_SOC_SOF_INTEL_HDA_GENERIC");
963
964void hda_dsp_remove_late(struct snd_sof_dev *sdev)
965{
966 iounmap(sof_to_bus(sdev)->remap_addr);
967 sof_hda_bus_exit(sdev);
968 hda_codec_i915_exit(sdev);
969}
970
971int hda_power_down_dsp(struct snd_sof_dev *sdev)
972{
973 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
974 const struct sof_intel_dsp_desc *chip = hda->desc;
975
976 return hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
977}
978EXPORT_SYMBOL_NS(hda_power_down_dsp, "SND_SOC_SOF_INTEL_HDA_GENERIC");
979
980#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
981static void hda_generic_machine_select(struct snd_sof_dev *sdev,
982 struct snd_soc_acpi_mach **mach)
983{
984 struct hdac_bus *bus = sof_to_bus(sdev);
985 struct snd_soc_acpi_mach_params *mach_params;
986 struct snd_soc_acpi_mach *hda_mach;
987 struct snd_sof_pdata *pdata = sdev->pdata;
988 const char *tplg_filename;
989 int codec_num = 0;
990 int i;
991
992 /* codec detection */
993 if (!bus->codec_mask) {
994 dev_info(bus->dev, "no hda codecs found!\n");
995 } else {
996 dev_info(bus->dev, "hda codecs found, mask %lx\n",
997 bus->codec_mask);
998
999 for (i = 0; i < HDA_MAX_CODECS; i++) {
1000 if (bus->codec_mask & (1 << i))
1001 codec_num++;
1002 }
1003
1004 /*
1005 * If no machine driver is found, then:
1006 *
1007 * generic hda machine driver can handle:
1008 * - one HDMI codec, and/or
1009 * - one external HDAudio codec
1010 */
1011 if (!*mach && codec_num <= 2) {
1012 bool tplg_fixup = false;
1013
1014 hda_mach = snd_soc_acpi_intel_hda_machines;
1015
1016 dev_info(bus->dev, "using HDA machine driver %s now\n",
1017 hda_mach->drv_name);
1018
1019 /*
1020 * topology: use the info from hda_machines since tplg file name
1021 * is not overwritten
1022 */
1023 if (!pdata->tplg_filename)
1024 tplg_fixup = true;
1025
1026 if (tplg_fixup &&
1027 codec_num == 1 && HDA_IDISP_CODEC(bus->codec_mask)) {
1028 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1029 "%s-idisp",
1030 hda_mach->sof_tplg_filename);
1031 if (!tplg_filename)
1032 return;
1033
1034 hda_mach->sof_tplg_filename = tplg_filename;
1035 }
1036
1037 if (codec_num == 2 ||
1038 (codec_num == 1 && !HDA_IDISP_CODEC(bus->codec_mask))) {
1039 /*
1040 * Prevent SoundWire links from starting when an external
1041 * HDaudio codec is used
1042 */
1043 hda_mach->mach_params.link_mask = 0;
1044 } else {
1045 /*
1046 * Allow SoundWire links to start when no external HDaudio codec
1047 * was detected. This will not create a SoundWire card but
1048 * will help detect if any SoundWire codec reports as ATTACHED.
1049 */
1050 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
1051
1052 hda_mach->mach_params.link_mask = hdev->info.link_mask;
1053 }
1054
1055 *mach = hda_mach;
1056 }
1057 }
1058
1059 /* used by hda machine driver to create dai links */
1060 if (*mach) {
1061 mach_params = &(*mach)->mach_params;
1062 mach_params->codec_mask = bus->codec_mask;
1063 }
1064}
1065#else
1066static void hda_generic_machine_select(struct snd_sof_dev *sdev,
1067 struct snd_soc_acpi_mach **mach)
1068{
1069}
1070#endif
1071
1072#if IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE)
1073
1074static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1075{
1076 struct snd_sof_pdata *pdata = sdev->pdata;
1077 const struct snd_soc_acpi_link_adr *link;
1078 struct sdw_peripherals *peripherals;
1079 struct snd_soc_acpi_mach *mach;
1080 struct sof_intel_hda_dev *hdev;
1081 u32 link_mask;
1082 int i;
1083
1084 hdev = pdata->hw_pdata;
1085 link_mask = hdev->info.link_mask;
1086
1087 if (!link_mask) {
1088 dev_info(sdev->dev, "SoundWire links not enabled\n");
1089 return NULL;
1090 }
1091
1092 if (!hdev->sdw) {
1093 dev_dbg(sdev->dev, "SoundWire context not allocated\n");
1094 return NULL;
1095 }
1096
1097 if (!hdev->sdw->peripherals || !hdev->sdw->peripherals->num_peripherals) {
1098 dev_warn(sdev->dev, "No SoundWire peripheral detected in ACPI tables\n");
1099 return NULL;
1100 }
1101
1102 /*
1103 * Select SoundWire machine driver if needed using the
1104 * alternate tables. This case deals with SoundWire-only
1105 * machines, for mixed cases with I2C/I2S the detection relies
1106 * on the HID list.
1107 */
1108 for (mach = pdata->desc->alt_machines;
1109 mach && mach->link_mask; mach++) {
1110 /*
1111 * On some platforms such as Up Extreme all links
1112 * are enabled but only one link can be used by
1113 * external codec. Instead of exact match of two masks,
1114 * first check whether link_mask of mach is subset of
1115 * link_mask supported by hw and then go on searching
1116 * link_adr
1117 */
1118 if (~link_mask & mach->link_mask)
1119 continue;
1120
1121 /* No need to match adr if there is no links defined */
1122 if (!mach->links)
1123 break;
1124
1125 link = mach->links;
1126 for (i = 0; i < hdev->info.count && link->num_adr;
1127 i++, link++) {
1128 /*
1129 * Try next machine if any expected Slaves
1130 * are not found on this link.
1131 */
1132 if (!snd_soc_acpi_sdw_link_slaves_found(sdev->dev, link,
1133 hdev->sdw->peripherals))
1134 break;
1135 }
1136 /* Found if all Slaves are checked */
1137 if (i == hdev->info.count || !link->num_adr)
1138 if (!mach->machine_check || mach->machine_check(hdev->sdw))
1139 break;
1140 }
1141 if (mach && mach->link_mask) {
1142 mach->mach_params.links = mach->links;
1143 mach->mach_params.link_mask = mach->link_mask;
1144 mach->mach_params.platform = dev_name(sdev->dev);
1145
1146 return mach;
1147 }
1148
1149 dev_info(sdev->dev, "No SoundWire machine driver found for the ACPI-reported configuration:\n");
1150 peripherals = hdev->sdw->peripherals;
1151 for (i = 0; i < peripherals->num_peripherals; i++)
1152 dev_info(sdev->dev, "link %d mfg_id 0x%04x part_id 0x%04x version %#x\n",
1153 peripherals->array[i]->bus->link_id,
1154 peripherals->array[i]->id.mfg_id,
1155 peripherals->array[i]->id.part_id,
1156 peripherals->array[i]->id.sdw_version);
1157
1158 return NULL;
1159}
1160#else
1161static struct snd_soc_acpi_mach *hda_sdw_machine_select(struct snd_sof_dev *sdev)
1162{
1163 return NULL;
1164}
1165#endif
1166
1167void hda_set_mach_params(struct snd_soc_acpi_mach *mach,
1168 struct snd_sof_dev *sdev)
1169{
1170 struct snd_sof_pdata *pdata = sdev->pdata;
1171 const struct sof_dev_desc *desc = pdata->desc;
1172 struct snd_soc_acpi_mach_params *mach_params;
1173
1174 mach_params = &mach->mach_params;
1175 mach_params->platform = dev_name(sdev->dev);
1176 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) &&
1177 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC))
1178 mach_params->num_dai_drivers = SOF_SKL_NUM_DAIS_NOCODEC;
1179 else
1180 mach_params->num_dai_drivers = desc->ops->num_drv;
1181 mach_params->dai_drivers = desc->ops->drv;
1182}
1183
1184static int check_tplg_quirk_mask(struct snd_soc_acpi_mach *mach)
1185{
1186 u32 dmic_ssp_quirk;
1187 u32 codec_amp_name_quirk;
1188
1189 /*
1190 * In current implementation dmic and ssp quirks are designed for es8336
1191 * machine driver and could not be mixed with codec name and amp name
1192 * quirks.
1193 */
1194 dmic_ssp_quirk = mach->tplg_quirk_mask &
1195 (SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER | SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER);
1196 codec_amp_name_quirk = mach->tplg_quirk_mask &
1197 (SND_SOC_ACPI_TPLG_INTEL_AMP_NAME | SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME);
1198
1199 if (dmic_ssp_quirk && codec_amp_name_quirk)
1200 return -EINVAL;
1201
1202 return 0;
1203}
1204
1205static char *remove_file_ext(const char *tplg_filename)
1206{
1207 char *filename, *tmp;
1208
1209 filename = kstrdup(tplg_filename, GFP_KERNEL);
1210 if (!filename)
1211 return NULL;
1212
1213 /* remove file extension if exist */
1214 tmp = filename;
1215 return strsep(&tmp, ".");
1216}
1217
1218struct snd_soc_acpi_mach *hda_machine_select(struct snd_sof_dev *sdev)
1219{
1220 u32 interface_mask = hda_get_interface_mask(sdev);
1221 struct snd_sof_pdata *sof_pdata = sdev->pdata;
1222 const struct sof_dev_desc *desc = sof_pdata->desc;
1223 struct hdac_bus *bus = sof_to_bus(sdev);
1224 struct snd_soc_acpi_mach *mach = NULL;
1225 enum snd_soc_acpi_intel_codec codec_type, amp_type;
1226 const char *tplg_filename;
1227 const char *tplg_suffix;
1228 bool amp_name_valid;
1229 bool i2s_mach_found = false;
1230 bool sdw_mach_found = false;
1231
1232 /* Try I2S or DMIC if it is supported */
1233 if (interface_mask & (BIT(SOF_DAI_INTEL_SSP) | BIT(SOF_DAI_INTEL_DMIC))) {
1234 mach = snd_soc_acpi_find_machine(desc->machines);
1235 if (mach)
1236 i2s_mach_found = true;
1237 }
1238
1239 /*
1240 * If I2S fails and no external HDaudio codec is detected,
1241 * try SoundWire if it is supported
1242 */
1243 if (!mach && !HDA_EXT_CODEC(bus->codec_mask) &&
1244 (interface_mask & BIT(SOF_DAI_INTEL_ALH))) {
1245 mach = hda_sdw_machine_select(sdev);
1246 if (mach)
1247 sdw_mach_found = true;
1248 }
1249
1250 /*
1251 * Choose HDA generic machine driver if mach is NULL.
1252 * Otherwise, set certain mach params.
1253 */
1254 hda_generic_machine_select(sdev, &mach);
1255 if (!mach) {
1256 dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
1257 return NULL;
1258 }
1259
1260 /* report BT offload link mask to machine driver */
1261 mach->mach_params.bt_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_BT);
1262
1263 dev_info(sdev->dev, "BT link detected in NHLT tables: %#x\n",
1264 mach->mach_params.bt_link_mask);
1265
1266 /* allow for module parameter override */
1267 if (bt_link_mask_override) {
1268 dev_dbg(sdev->dev, "overriding BT link detected in NHLT tables %#x by kernel param %#x\n",
1269 mach->mach_params.bt_link_mask, bt_link_mask_override);
1270 mach->mach_params.bt_link_mask = bt_link_mask_override;
1271 }
1272
1273 if (hweight_long(mach->mach_params.bt_link_mask) > 1) {
1274 dev_warn(sdev->dev, "invalid BT link mask %#x found, reset the mask\n",
1275 mach->mach_params.bt_link_mask);
1276 mach->mach_params.bt_link_mask = 0;
1277 }
1278
1279 /*
1280 * Fixup tplg file name by appending dmic num, ssp num, codec/amplifier
1281 * name string if quirk flag is set.
1282 */
1283 if (mach) {
1284 bool tplg_fixup = false;
1285 bool dmic_fixup = false;
1286
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 /* remove file extension if it exists */
1293 tplg_filename = remove_file_ext(mach->sof_tplg_filename);
1294 if (!tplg_filename)
1295 return NULL;
1296
1297 sof_pdata->tplg_filename = tplg_filename;
1298 tplg_fixup = true;
1299 }
1300
1301 /*
1302 * Checking quirk mask integrity; some quirk flags could not be
1303 * set concurrently.
1304 */
1305 if (tplg_fixup &&
1306 check_tplg_quirk_mask(mach)) {
1307 dev_err(sdev->dev, "Invalid tplg quirk mask 0x%x\n",
1308 mach->tplg_quirk_mask);
1309 return NULL;
1310 }
1311
1312 /* report to machine driver if any DMICs are found */
1313 mach->mach_params.dmic_num = check_dmic_num(sdev);
1314
1315 if (sdw_mach_found) {
1316 /*
1317 * DMICs use up to 4 pins and are typically pin-muxed with SoundWire
1318 * link 2 and 3, or link 1 and 2, thus we only try to enable dmics
1319 * if all conditions are true:
1320 * a) 2 or fewer links are used by SoundWire
1321 * b) the NHLT table reports the presence of microphones
1322 */
1323 if (hweight_long(mach->link_mask) <= 2)
1324 dmic_fixup = true;
1325 else
1326 mach->mach_params.dmic_num = 0;
1327 } else {
1328 if (mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_DMIC_NUMBER)
1329 dmic_fixup = true;
1330 }
1331
1332 if (tplg_fixup &&
1333 dmic_fixup &&
1334 mach->mach_params.dmic_num) {
1335 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1336 "%s%s%d%s",
1337 sof_pdata->tplg_filename,
1338 i2s_mach_found ? "-dmic" : "-",
1339 mach->mach_params.dmic_num,
1340 "ch");
1341 if (!tplg_filename)
1342 return NULL;
1343
1344 sof_pdata->tplg_filename = tplg_filename;
1345 }
1346
1347 if (mach->link_mask) {
1348 mach->mach_params.links = mach->links;
1349 mach->mach_params.link_mask = mach->link_mask;
1350 }
1351
1352 /* report SSP link mask to machine driver */
1353 mach->mach_params.i2s_link_mask = check_nhlt_ssp_mask(sdev, NHLT_DEVICE_I2S);
1354
1355 if (tplg_fixup &&
1356 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER &&
1357 mach->mach_params.i2s_link_mask) {
1358 const struct sof_intel_dsp_desc *chip = get_chip_info(sdev->pdata);
1359 int ssp_num;
1360 int mclk_mask;
1361
1362 if (hweight_long(mach->mach_params.i2s_link_mask) > 1 &&
1363 !(mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_MSB))
1364 dev_warn(sdev->dev, "More than one SSP exposed by NHLT, choosing MSB\n");
1365
1366 /* fls returns 1-based results, SSPs indices are 0-based */
1367 ssp_num = fls(mach->mach_params.i2s_link_mask) - 1;
1368
1369 if (ssp_num >= chip->ssp_count) {
1370 dev_err(sdev->dev, "Invalid SSP %d, max on this platform is %d\n",
1371 ssp_num, chip->ssp_count);
1372 return NULL;
1373 }
1374
1375 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1376 "%s%s%d",
1377 sof_pdata->tplg_filename,
1378 "-ssp",
1379 ssp_num);
1380 if (!tplg_filename)
1381 return NULL;
1382
1383 sof_pdata->tplg_filename = tplg_filename;
1384
1385 mclk_mask = check_nhlt_ssp_mclk_mask(sdev, ssp_num);
1386
1387 if (mclk_mask < 0) {
1388 dev_err(sdev->dev, "Invalid MCLK configuration\n");
1389 return NULL;
1390 }
1391
1392 dev_dbg(sdev->dev, "MCLK mask %#x found in NHLT\n", mclk_mask);
1393
1394 if (mclk_mask) {
1395 dev_info(sdev->dev, "Overriding topology with MCLK mask %#x from NHLT\n", mclk_mask);
1396 sdev->mclk_id_override = true;
1397 sdev->mclk_id_quirk = (mclk_mask & BIT(0)) ? 0 : 1;
1398 }
1399 }
1400
1401 amp_type = snd_soc_acpi_intel_detect_amp_type(sdev->dev);
1402 codec_type = snd_soc_acpi_intel_detect_codec_type(sdev->dev);
1403 amp_name_valid = amp_type != CODEC_NONE && amp_type != codec_type;
1404
1405 if (tplg_fixup && amp_name_valid &&
1406 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_AMP_NAME) {
1407 tplg_suffix = snd_soc_acpi_intel_get_amp_tplg_suffix(amp_type);
1408 if (!tplg_suffix) {
1409 dev_err(sdev->dev, "no tplg suffix found, amp %d\n",
1410 amp_type);
1411 return NULL;
1412 }
1413
1414 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1415 "%s-%s",
1416 sof_pdata->tplg_filename,
1417 tplg_suffix);
1418 if (!tplg_filename)
1419 return NULL;
1420
1421 sof_pdata->tplg_filename = tplg_filename;
1422 }
1423
1424
1425 if (tplg_fixup &&
1426 mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_CODEC_NAME &&
1427 codec_type != CODEC_NONE) {
1428 tplg_suffix = snd_soc_acpi_intel_get_codec_tplg_suffix(codec_type);
1429 if (!tplg_suffix) {
1430 dev_err(sdev->dev, "no tplg suffix found, codec %d\n",
1431 codec_type);
1432 return NULL;
1433 }
1434
1435 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1436 "%s-%s",
1437 sof_pdata->tplg_filename,
1438 tplg_suffix);
1439 if (!tplg_filename)
1440 return NULL;
1441
1442 sof_pdata->tplg_filename = tplg_filename;
1443 }
1444
1445 if (tplg_fixup) {
1446 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
1447 "%s%s",
1448 sof_pdata->tplg_filename,
1449 ".tplg");
1450 if (!tplg_filename)
1451 return NULL;
1452
1453 sof_pdata->tplg_filename = tplg_filename;
1454 }
1455
1456 /* check if mclk_id should be modified from topology defaults */
1457 if (mclk_id_override >= 0) {
1458 dev_info(sdev->dev, "Overriding topology with MCLK %d from kernel_parameter\n", mclk_id_override);
1459 sdev->mclk_id_override = true;
1460 sdev->mclk_id_quirk = mclk_id_override;
1461 }
1462 }
1463
1464 return mach;
1465}
1466
1467int hda_pci_intel_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1468{
1469 int ret;
1470
1471 ret = snd_intel_dsp_driver_probe(pci);
1472 if (ret != SND_INTEL_DSP_DRIVER_ANY && ret != SND_INTEL_DSP_DRIVER_SOF) {
1473 dev_dbg(&pci->dev, "SOF PCI driver not selected, aborting probe\n");
1474 return -ENODEV;
1475 }
1476
1477 return sof_pci_probe(pci, pci_id);
1478}
1479EXPORT_SYMBOL_NS(hda_pci_intel_probe, "SND_SOC_SOF_INTEL_HDA_GENERIC");
1480
1481int hda_register_clients(struct snd_sof_dev *sdev)
1482{
1483 return hda_probes_register(sdev);
1484}
1485
1486void hda_unregister_clients(struct snd_sof_dev *sdev)
1487{
1488 hda_probes_unregister(sdev);
1489}
1490
1491MODULE_LICENSE("Dual BSD/GPL");
1492MODULE_DESCRIPTION("SOF support for HDaudio platforms");
1493MODULE_IMPORT_NS("SND_SOC_SOF_PCI_DEV");
1494MODULE_IMPORT_NS("SND_SOC_SOF_HDA_AUDIO_CODEC");
1495MODULE_IMPORT_NS("SND_SOC_SOF_HDA_AUDIO_CODEC_I915");
1496MODULE_IMPORT_NS("SND_SOC_SOF_XTENSA");
1497MODULE_IMPORT_NS("SND_INTEL_SOUNDWIRE_ACPI");
1498MODULE_IMPORT_NS("SOUNDWIRE_INTEL_INIT");
1499MODULE_IMPORT_NS("SOUNDWIRE_INTEL");
1500MODULE_IMPORT_NS("SND_SOC_SOF_HDA_MLINK");
1501MODULE_IMPORT_NS("SND_SOC_SOF_INTEL_HDA_COMMON");
1502MODULE_IMPORT_NS("SND_SOC_ACPI_INTEL_MATCH");