File Index
Files and directories
Sign in
FILE

hda.c

File preview with syntax highlighting.

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