cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

hda.c (38968B)


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