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