Merge branch 'asoc-5.3' into asoc-linus
[platform/kernel/linux-starfive.git] / sound / soc / sof / intel / hda.c
1 // SPDX-License-Identifier: (GPL-2.0 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/module.h>
22 #include <sound/sof.h>
23 #include <sound/sof/xtensa.h>
24 #include "../ops.h"
25 #include "hda.h"
26 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
27 #include "../../codecs/hdac_hda.h"
28 #endif
29
30 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
31 #include <sound/soc-acpi-intel-match.h>
32 #endif
33
34 /* platform specific devices */
35 #include "shim.h"
36
37 #define IS_CFL(pci) ((pci)->vendor == 0x8086 && (pci)->device == 0xa348)
38 #define IS_CNL(pci) ((pci)->vendor == 0x8086 && (pci)->device == 0x9dc8)
39
40 /*
41  * Debug
42  */
43
44 struct hda_dsp_msg_code {
45         u32 code;
46         const char *msg;
47 };
48
49 static const struct hda_dsp_msg_code hda_dsp_rom_msg[] = {
50         {HDA_DSP_ROM_FW_MANIFEST_LOADED, "status: manifest loaded"},
51         {HDA_DSP_ROM_FW_FW_LOADED, "status: fw loaded"},
52         {HDA_DSP_ROM_FW_ENTERED, "status: fw entered"},
53         {HDA_DSP_ROM_CSE_ERROR, "error: cse error"},
54         {HDA_DSP_ROM_CSE_WRONG_RESPONSE, "error: cse wrong response"},
55         {HDA_DSP_ROM_IMR_TO_SMALL, "error: IMR too small"},
56         {HDA_DSP_ROM_BASE_FW_NOT_FOUND, "error: base fw not found"},
57         {HDA_DSP_ROM_CSE_VALIDATION_FAILED, "error: signature verification failed"},
58         {HDA_DSP_ROM_IPC_FATAL_ERROR, "error: ipc fatal error"},
59         {HDA_DSP_ROM_L2_CACHE_ERROR, "error: L2 cache error"},
60         {HDA_DSP_ROM_LOAD_OFFSET_TO_SMALL, "error: load offset too small"},
61         {HDA_DSP_ROM_API_PTR_INVALID, "error: API ptr invalid"},
62         {HDA_DSP_ROM_BASEFW_INCOMPAT, "error: base fw incompatible"},
63         {HDA_DSP_ROM_UNHANDLED_INTERRUPT, "error: unhandled interrupt"},
64         {HDA_DSP_ROM_MEMORY_HOLE_ECC, "error: ECC memory hole"},
65         {HDA_DSP_ROM_KERNEL_EXCEPTION, "error: kernel exception"},
66         {HDA_DSP_ROM_USER_EXCEPTION, "error: user exception"},
67         {HDA_DSP_ROM_UNEXPECTED_RESET, "error: unexpected reset"},
68         {HDA_DSP_ROM_NULL_FW_ENTRY,     "error: null FW entry point"},
69 };
70
71 static void hda_dsp_get_status_skl(struct snd_sof_dev *sdev)
72 {
73         u32 status;
74         int i;
75
76         status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
77                                   HDA_ADSP_FW_STATUS_SKL);
78
79         for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
80                 if (status == hda_dsp_rom_msg[i].code) {
81                         dev_err(sdev->dev, "%s - code %8.8x\n",
82                                 hda_dsp_rom_msg[i].msg, status);
83                         return;
84                 }
85         }
86
87         /* not for us, must be generic sof message */
88         dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
89 }
90
91 static void hda_dsp_get_status(struct snd_sof_dev *sdev)
92 {
93         u32 status;
94         int i;
95
96         status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
97                                   HDA_DSP_SRAM_REG_ROM_STATUS);
98
99         for (i = 0; i < ARRAY_SIZE(hda_dsp_rom_msg); i++) {
100                 if (status == hda_dsp_rom_msg[i].code) {
101                         dev_err(sdev->dev, "%s - code %8.8x\n",
102                                 hda_dsp_rom_msg[i].msg, status);
103                         return;
104                 }
105         }
106
107         /* not for us, must be generic sof message */
108         dev_dbg(sdev->dev, "unknown ROM status value %8.8x\n", status);
109 }
110
111 static void hda_dsp_get_registers(struct snd_sof_dev *sdev,
112                                   struct sof_ipc_dsp_oops_xtensa *xoops,
113                                   struct sof_ipc_panic_info *panic_info,
114                                   u32 *stack, size_t stack_words)
115 {
116         u32 offset = sdev->dsp_oops_offset;
117
118         /* first read registers */
119         sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
120
121         /* note: variable AR register array is not read */
122
123         /* then get panic info */
124         offset += xoops->arch_hdr.totalsize;
125         sof_block_read(sdev, sdev->mmio_bar, offset,
126                        panic_info, sizeof(*panic_info));
127
128         /* then get the stack */
129         offset += sizeof(*panic_info);
130         sof_block_read(sdev, sdev->mmio_bar, offset, stack,
131                        stack_words * sizeof(u32));
132 }
133
134 void hda_dsp_dump_skl(struct snd_sof_dev *sdev, u32 flags)
135 {
136         struct sof_ipc_dsp_oops_xtensa xoops;
137         struct sof_ipc_panic_info panic_info;
138         u32 stack[HDA_DSP_STACK_DUMP_SIZE];
139         u32 status, panic;
140
141         /* try APL specific status message types first */
142         hda_dsp_get_status_skl(sdev);
143
144         /* now try generic SOF status messages */
145         status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
146                                   HDA_ADSP_ERROR_CODE_SKL);
147
148         /*TODO: Check: there is no define in spec, but it is used in the code*/
149         panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
150                                  HDA_ADSP_ERROR_CODE_SKL + 0x4);
151
152         if (sdev->boot_complete) {
153                 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
154                                       HDA_DSP_STACK_DUMP_SIZE);
155                 snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
156                                    stack, HDA_DSP_STACK_DUMP_SIZE);
157         } else {
158                 dev_err(sdev->dev, "error: status = 0x%8.8x panic = 0x%8.8x\n",
159                         status, panic);
160                 hda_dsp_get_status_skl(sdev);
161         }
162 }
163
164 void hda_dsp_dump(struct snd_sof_dev *sdev, u32 flags)
165 {
166         struct sof_ipc_dsp_oops_xtensa xoops;
167         struct sof_ipc_panic_info panic_info;
168         u32 stack[HDA_DSP_STACK_DUMP_SIZE];
169         u32 status, panic;
170
171         /* try APL specific status message types first */
172         hda_dsp_get_status(sdev);
173
174         /* now try generic SOF status messages */
175         status = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
176                                   HDA_DSP_SRAM_REG_FW_STATUS);
177         panic = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_SRAM_REG_FW_TRACEP);
178
179         if (sdev->boot_complete) {
180                 hda_dsp_get_registers(sdev, &xoops, &panic_info, stack,
181                                       HDA_DSP_STACK_DUMP_SIZE);
182                 snd_sof_get_status(sdev, status, panic, &xoops, &panic_info,
183                                    stack, HDA_DSP_STACK_DUMP_SIZE);
184         } else {
185                 dev_err(sdev->dev, "error: status = 0x%8.8x panic = 0x%8.8x\n",
186                         status, panic);
187                 hda_dsp_get_status(sdev);
188         }
189 }
190
191 void hda_ipc_irq_dump(struct snd_sof_dev *sdev)
192 {
193         struct hdac_bus *bus = sof_to_bus(sdev);
194         u32 adspis;
195         u32 intsts;
196         u32 intctl;
197         u32 ppsts;
198         u8 rirbsts;
199
200         /* read key IRQ stats and config registers */
201         adspis = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
202         intsts = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTSTS);
203         intctl = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL);
204         ppsts = snd_sof_dsp_read(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPSTS);
205         rirbsts = snd_hdac_chip_readb(bus, RIRBSTS);
206
207         dev_err(sdev->dev,
208                 "error: hda irq intsts 0x%8.8x intlctl 0x%8.8x rirb %2.2x\n",
209                 intsts, intctl, rirbsts);
210         dev_err(sdev->dev,
211                 "error: dsp irq ppsts 0x%8.8x adspis 0x%8.8x\n",
212                 ppsts, adspis);
213 }
214
215 void hda_ipc_dump(struct snd_sof_dev *sdev)
216 {
217         u32 hipcie;
218         u32 hipct;
219         u32 hipcctl;
220
221         hda_ipc_irq_dump(sdev);
222
223         /* read IPC status */
224         hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
225         hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
226         hipcctl = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL);
227
228         /* dump the IPC regs */
229         /* TODO: parse the raw msg */
230         dev_err(sdev->dev,
231                 "error: host status 0x%8.8x dsp status 0x%8.8x mask 0x%8.8x\n",
232                 hipcie, hipct, hipcctl);
233 }
234
235 static int hda_init(struct snd_sof_dev *sdev)
236 {
237         struct hda_bus *hbus;
238         struct hdac_bus *bus;
239         struct hdac_ext_bus_ops *ext_ops = NULL;
240         struct pci_dev *pci = to_pci_dev(sdev->dev);
241         int ret;
242
243         hbus = sof_to_hbus(sdev);
244         bus = sof_to_bus(sdev);
245
246         /* HDA bus init */
247 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
248         ext_ops = snd_soc_hdac_hda_get_ops();
249 #endif
250         sof_hda_bus_init(bus, &pci->dev, ext_ops);
251
252         /* Workaround for a communication error on CFL (bko#199007) and CNL */
253         if (IS_CFL(pci) || IS_CNL(pci))
254                 bus->polling_mode = 1;
255
256         bus->use_posbuf = 1;
257         bus->bdl_pos_adj = 0;
258
259         mutex_init(&hbus->prepare_mutex);
260         hbus->pci = pci;
261         hbus->mixer_assigned = -1;
262         hbus->modelname = "sofbus";
263
264         /* initialise hdac bus */
265         bus->addr = pci_resource_start(pci, 0);
266 #if IS_ENABLED(CONFIG_PCI)
267         bus->remap_addr = pci_ioremap_bar(pci, 0);
268 #endif
269         if (!bus->remap_addr) {
270                 dev_err(bus->dev, "error: ioremap error\n");
271                 return -ENXIO;
272         }
273
274         /* HDA base */
275         sdev->bar[HDA_DSP_HDA_BAR] = bus->remap_addr;
276
277         /* get controller capabilities */
278         ret = hda_dsp_ctrl_get_caps(sdev);
279         if (ret < 0)
280                 dev_err(sdev->dev, "error: get caps error\n");
281
282         return ret;
283 }
284
285 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
286
287 static const char *fixup_tplg_name(struct snd_sof_dev *sdev,
288                                    const char *sof_tplg_filename)
289 {
290         const char *tplg_filename = NULL;
291         char *filename;
292         char *split_ext;
293
294         filename = devm_kstrdup(sdev->dev, sof_tplg_filename, GFP_KERNEL);
295         if (!filename)
296                 return NULL;
297
298         /* this assumes a .tplg extension */
299         split_ext = strsep(&filename, ".");
300         if (split_ext) {
301                 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
302                                                "%s-idisp.tplg", split_ext);
303                 if (!tplg_filename)
304                         return NULL;
305         }
306         return tplg_filename;
307 }
308
309 #endif
310
311 static int hda_init_caps(struct snd_sof_dev *sdev)
312 {
313         struct hdac_bus *bus = sof_to_bus(sdev);
314 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
315         struct hdac_ext_link *hlink;
316         struct snd_soc_acpi_mach_params *mach_params;
317         struct snd_soc_acpi_mach *hda_mach;
318         struct snd_sof_pdata *pdata = sdev->pdata;
319         struct snd_soc_acpi_mach *mach;
320         const char *tplg_filename;
321         int codec_num = 0;
322         int i;
323 #endif
324         int ret = 0;
325
326         device_disable_async_suspend(bus->dev);
327
328         /* check if dsp is there */
329         if (bus->ppcap)
330                 dev_dbg(sdev->dev, "PP capability, will probe DSP later.\n");
331
332 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
333         /* init i915 and HDMI codecs */
334         ret = hda_codec_i915_init(sdev);
335         if (ret < 0) {
336                 dev_err(sdev->dev, "error: init i915 and HDMI codec failed\n");
337                 return ret;
338         }
339 #endif
340
341         /* Init HDA controller after i915 init */
342         ret = hda_dsp_ctrl_init_chip(sdev, true);
343         if (ret < 0) {
344                 dev_err(bus->dev, "error: init chip failed with ret: %d\n",
345                         ret);
346 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
347                 hda_codec_i915_exit(sdev);
348 #endif
349                 return ret;
350         }
351
352 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
353         if (bus->mlcap)
354                 snd_hdac_ext_bus_get_ml_capabilities(bus);
355
356         /* codec detection */
357         if (!bus->codec_mask) {
358                 dev_info(bus->dev, "no hda codecs found!\n");
359         } else {
360                 dev_info(bus->dev, "hda codecs found, mask %lx\n",
361                          bus->codec_mask);
362
363                 for (i = 0; i < HDA_MAX_CODECS; i++) {
364                         if (bus->codec_mask & (1 << i))
365                                 codec_num++;
366                 }
367
368                 /*
369                  * If no machine driver is found, then:
370                  *
371                  * hda machine driver is used if :
372                  * 1. there is one HDMI codec and one external HDAudio codec
373                  * 2. only HDMI codec
374                  */
375                 if (!pdata->machine && codec_num <= 2 &&
376                     HDA_IDISP_CODEC(bus->codec_mask)) {
377                         hda_mach = snd_soc_acpi_intel_hda_machines;
378                         pdata->machine = hda_mach;
379
380                         /* topology: use the info from hda_machines */
381                         pdata->tplg_filename =
382                                 hda_mach->sof_tplg_filename;
383
384                         /* firmware: pick the first in machine list */
385                         mach = pdata->desc->machines;
386                         pdata->fw_filename = mach->sof_fw_filename;
387
388                         dev_info(bus->dev, "using HDA machine driver %s now\n",
389                                  hda_mach->drv_name);
390
391                         /* fixup topology file for HDMI only platforms */
392                         if (codec_num == 1) {
393                                 /* use local variable for readability */
394                                 tplg_filename = pdata->tplg_filename;
395                                 tplg_filename = fixup_tplg_name(sdev, tplg_filename);
396                                 if (!tplg_filename) {
397                                         hda_codec_i915_exit(sdev);
398                                         return ret;
399                                 }
400                                 pdata->tplg_filename = tplg_filename;
401                         }
402                 }
403         }
404
405         /* used by hda machine driver to create dai links */
406         if (pdata->machine) {
407                 mach_params = (struct snd_soc_acpi_mach_params *)
408                         &pdata->machine->mach_params;
409                 mach_params->codec_mask = bus->codec_mask;
410                 mach_params->platform = dev_name(sdev->dev);
411         }
412
413         /* create codec instances */
414         hda_codec_probe_bus(sdev);
415
416         hda_codec_i915_put(sdev);
417
418         /*
419          * we are done probing so decrement link counts
420          */
421         list_for_each_entry(hlink, &bus->hlink_list, list)
422                 snd_hdac_ext_bus_link_put(bus, hlink);
423 #endif
424         return 0;
425 }
426
427 static const struct sof_intel_dsp_desc
428         *get_chip_info(struct snd_sof_pdata *pdata)
429 {
430         const struct sof_dev_desc *desc = pdata->desc;
431         const struct sof_intel_dsp_desc *chip_info;
432
433         chip_info = desc->chip_info;
434
435         return chip_info;
436 }
437
438 int hda_dsp_probe(struct snd_sof_dev *sdev)
439 {
440         struct pci_dev *pci = to_pci_dev(sdev->dev);
441         struct sof_intel_hda_dev *hdev;
442         struct hdac_bus *bus;
443         const struct sof_intel_dsp_desc *chip;
444         int ret = 0;
445
446         /*
447          * detect DSP by checking class/subclass/prog-id information
448          * class=04 subclass 03 prog-if 00: no DSP, legacy driver is required
449          * class=04 subclass 01 prog-if 00: DSP is present
450          *   (and may be required e.g. for DMIC or SSP support)
451          * class=04 subclass 03 prog-if 80: either of DSP or legacy mode works
452          */
453         if (pci->class == 0x040300) {
454                 dev_err(sdev->dev, "error: the DSP is not enabled on this platform, aborting probe\n");
455                 return -ENODEV;
456         } else if (pci->class != 0x040100 && pci->class != 0x040380) {
457                 dev_err(sdev->dev, "error: unknown PCI class/subclass/prog-if 0x%06x found, aborting probe\n", pci->class);
458                 return -ENODEV;
459         }
460         dev_info(sdev->dev, "DSP detected with PCI class/subclass/prog-if 0x%06x\n", pci->class);
461
462         chip = get_chip_info(sdev->pdata);
463         if (!chip) {
464                 dev_err(sdev->dev, "error: no such device supported, chip id:%x\n",
465                         pci->device);
466                 ret = -EIO;
467                 goto err;
468         }
469
470         hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
471         if (!hdev)
472                 return -ENOMEM;
473         sdev->pdata->hw_pdata = hdev;
474         hdev->desc = chip;
475
476         hdev->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
477                                                        PLATFORM_DEVID_NONE,
478                                                        NULL, 0);
479         if (IS_ERR(hdev->dmic_dev)) {
480                 dev_err(sdev->dev, "error: failed to create DMIC device\n");
481                 return PTR_ERR(hdev->dmic_dev);
482         }
483
484         /*
485          * use position update IPC if either it is forced
486          * or we don't have other choice
487          */
488 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_FORCE_IPC_POSITION)
489         hdev->no_ipc_position = 0;
490 #else
491         hdev->no_ipc_position = sof_ops(sdev)->pcm_pointer ? 1 : 0;
492 #endif
493
494         /* set up HDA base */
495         bus = sof_to_bus(sdev);
496         ret = hda_init(sdev);
497         if (ret < 0)
498                 goto hdac_bus_unmap;
499
500         /* DSP base */
501 #if IS_ENABLED(CONFIG_PCI)
502         sdev->bar[HDA_DSP_BAR] = pci_ioremap_bar(pci, HDA_DSP_BAR);
503 #endif
504         if (!sdev->bar[HDA_DSP_BAR]) {
505                 dev_err(sdev->dev, "error: ioremap error\n");
506                 ret = -ENXIO;
507                 goto hdac_bus_unmap;
508         }
509
510         sdev->mmio_bar = HDA_DSP_BAR;
511         sdev->mailbox_bar = HDA_DSP_BAR;
512
513         /* allow 64bit DMA address if supported by H/W */
514         if (!dma_set_mask(&pci->dev, DMA_BIT_MASK(64))) {
515                 dev_dbg(sdev->dev, "DMA mask is 64 bit\n");
516                 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(64));
517         } else {
518                 dev_dbg(sdev->dev, "DMA mask is 32 bit\n");
519                 dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
520                 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32));
521         }
522
523         /* init streams */
524         ret = hda_dsp_stream_init(sdev);
525         if (ret < 0) {
526                 dev_err(sdev->dev, "error: failed to init streams\n");
527                 /*
528                  * not all errors are due to memory issues, but trying
529                  * to free everything does not harm
530                  */
531                 goto free_streams;
532         }
533
534         /*
535          * register our IRQ
536          * let's try to enable msi firstly
537          * if it fails, use legacy interrupt mode
538          * TODO: support interrupt mode selection with kernel parameter
539          *       support msi multiple vectors
540          */
541         ret = pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI);
542         if (ret < 0) {
543                 dev_info(sdev->dev, "use legacy interrupt mode\n");
544                 /*
545                  * in IO-APIC mode, hda->irq and ipc_irq are using the same
546                  * irq number of pci->irq
547                  */
548                 hdev->irq = pci->irq;
549                 sdev->ipc_irq = pci->irq;
550                 sdev->msi_enabled = 0;
551         } else {
552                 dev_info(sdev->dev, "use msi interrupt mode\n");
553                 hdev->irq = pci_irq_vector(pci, 0);
554                 /* ipc irq number is the same of hda irq */
555                 sdev->ipc_irq = hdev->irq;
556                 sdev->msi_enabled = 1;
557         }
558
559         dev_dbg(sdev->dev, "using HDA IRQ %d\n", hdev->irq);
560         ret = request_threaded_irq(hdev->irq, hda_dsp_stream_interrupt,
561                                    hda_dsp_stream_threaded_handler,
562                                    IRQF_SHARED, "AudioHDA", bus);
563         if (ret < 0) {
564                 dev_err(sdev->dev, "error: failed to register HDA IRQ %d\n",
565                         hdev->irq);
566                 goto free_irq_vector;
567         }
568
569         dev_dbg(sdev->dev, "using IPC IRQ %d\n", sdev->ipc_irq);
570         ret = request_threaded_irq(sdev->ipc_irq, hda_dsp_ipc_irq_handler,
571                                    sof_ops(sdev)->irq_thread, IRQF_SHARED,
572                                    "AudioDSP", sdev);
573         if (ret < 0) {
574                 dev_err(sdev->dev, "error: failed to register IPC IRQ %d\n",
575                         sdev->ipc_irq);
576                 goto free_hda_irq;
577         }
578
579         pci_set_master(pci);
580         synchronize_irq(pci->irq);
581
582         /*
583          * clear TCSEL to clear playback on some HD Audio
584          * codecs. PCI TCSEL is defined in the Intel manuals.
585          */
586         snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
587
588         /* init HDA capabilities */
589         ret = hda_init_caps(sdev);
590         if (ret < 0)
591                 goto free_ipc_irq;
592
593         /* enable ppcap interrupt */
594         hda_dsp_ctrl_ppcap_enable(sdev, true);
595         hda_dsp_ctrl_ppcap_int_enable(sdev, true);
596
597         /* initialize waitq for code loading */
598         init_waitqueue_head(&sdev->waitq);
599
600         /* set default mailbox offset for FW ready message */
601         sdev->dsp_box.offset = HDA_DSP_MBOX_UPLINK_OFFSET;
602
603         return 0;
604
605 free_ipc_irq:
606         free_irq(sdev->ipc_irq, sdev);
607 free_hda_irq:
608         free_irq(hdev->irq, bus);
609 free_irq_vector:
610         if (sdev->msi_enabled)
611                 pci_free_irq_vectors(pci);
612 free_streams:
613         hda_dsp_stream_free(sdev);
614 /* dsp_unmap: not currently used */
615         iounmap(sdev->bar[HDA_DSP_BAR]);
616 hdac_bus_unmap:
617         iounmap(bus->remap_addr);
618 err:
619         return ret;
620 }
621
622 int hda_dsp_remove(struct snd_sof_dev *sdev)
623 {
624         struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
625         struct hdac_bus *bus = sof_to_bus(sdev);
626         struct pci_dev *pci = to_pci_dev(sdev->dev);
627         const struct sof_intel_dsp_desc *chip = hda->desc;
628
629 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
630         /* codec removal, invoke bus_device_remove */
631         snd_hdac_ext_bus_device_remove(bus);
632 #endif
633
634         if (!IS_ERR_OR_NULL(hda->dmic_dev))
635                 platform_device_unregister(hda->dmic_dev);
636
637         /* disable DSP IRQ */
638         snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
639                                 SOF_HDA_PPCTL_PIE, 0);
640
641         /* disable CIE and GIE interrupts */
642         snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
643                                 SOF_HDA_INT_CTRL_EN | SOF_HDA_INT_GLOBAL_EN, 0);
644
645         /* disable cores */
646         if (chip)
647                 hda_dsp_core_reset_power_down(sdev, chip->cores_mask);
648
649         /* disable DSP */
650         snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR, SOF_HDA_REG_PP_PPCTL,
651                                 SOF_HDA_PPCTL_GPROCEN, 0);
652
653         free_irq(sdev->ipc_irq, sdev);
654         free_irq(hda->irq, bus);
655         if (sdev->msi_enabled)
656                 pci_free_irq_vectors(pci);
657
658         hda_dsp_stream_free(sdev);
659 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
660         snd_hdac_link_free_all(bus);
661 #endif
662
663         iounmap(sdev->bar[HDA_DSP_BAR]);
664         iounmap(bus->remap_addr);
665
666 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
667         snd_hdac_ext_bus_exit(bus);
668 #endif
669         hda_codec_i915_exit(sdev);
670
671         return 0;
672 }
673
674 MODULE_LICENSE("Dual BSD/GPL");