GNU Linux-libre 4.9.326-gnu1
[releases.git] / sound / pci / hda / hda_intel.c
1 /*
2  *
3  *  hda_intel.c - Implementation of primary alsa driver code base
4  *                for Intel HD Audio.
5  *
6  *  Copyright(c) 2004 Intel Corporation. All rights reserved.
7  *
8  *  Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
9  *                     PeiSen Hou <pshou@realtek.com.tw>
10  *
11  *  This program is free software; you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License as published by the Free
13  *  Software Foundation; either version 2 of the License, or (at your option)
14  *  any later version.
15  *
16  *  This program is distributed in the hope that it will be useful, but WITHOUT
17  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19  *  more details.
20  *
21  *  You should have received a copy of the GNU General Public License along with
22  *  this program; if not, write to the Free Software Foundation, Inc., 59
23  *  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  *  CONTACTS:
26  *
27  *  Matt Jared          matt.jared@intel.com
28  *  Andy Kopp           andy.kopp@intel.com
29  *  Dan Kogan           dan.d.kogan@intel.com
30  *
31  *  CHANGES:
32  *
33  *  2004.12.01  Major rewrite by tiwai, merged the work of pshou
34  * 
35  */
36
37 #include <linux/delay.h>
38 #include <linux/interrupt.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/moduleparam.h>
43 #include <linux/init.h>
44 #include <linux/slab.h>
45 #include <linux/pci.h>
46 #include <linux/mutex.h>
47 #include <linux/io.h>
48 #include <linux/pm_runtime.h>
49 #include <linux/clocksource.h>
50 #include <linux/time.h>
51 #include <linux/completion.h>
52
53 #ifdef CONFIG_X86
54 /* for snoop control */
55 #include <asm/pgtable.h>
56 #include <asm/cacheflush.h>
57 #include <asm/cpufeature.h>
58 #endif
59 #include <sound/core.h>
60 #include <sound/initval.h>
61 #include <sound/hdaudio.h>
62 #include <sound/hda_i915.h>
63 #include <linux/vgaarb.h>
64 #include <linux/vga_switcheroo.h>
65 #include <linux/firmware.h>
66 #include "hda_codec.h"
67 #include "hda_controller.h"
68 #include "hda_intel.h"
69
70 #define CREATE_TRACE_POINTS
71 #include "hda_intel_trace.h"
72
73 /* position fix mode */
74 enum {
75         POS_FIX_AUTO,
76         POS_FIX_LPIB,
77         POS_FIX_POSBUF,
78         POS_FIX_VIACOMBO,
79         POS_FIX_COMBO,
80 };
81
82 /* Defines for ATI HD Audio support in SB450 south bridge */
83 #define ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR   0x42
84 #define ATI_SB450_HDAUDIO_ENABLE_SNOOP      0x02
85
86 /* Defines for Nvidia HDA support */
87 #define NVIDIA_HDA_TRANSREG_ADDR      0x4e
88 #define NVIDIA_HDA_ENABLE_COHBITS     0x0f
89 #define NVIDIA_HDA_ISTRM_COH          0x4d
90 #define NVIDIA_HDA_OSTRM_COH          0x4c
91 #define NVIDIA_HDA_ENABLE_COHBIT      0x01
92
93 /* Defines for Intel SCH HDA snoop control */
94 #define INTEL_HDA_CGCTL  0x48
95 #define INTEL_HDA_CGCTL_MISCBDCGE        (0x1 << 6)
96 #define INTEL_SCH_HDA_DEVC      0x78
97 #define INTEL_SCH_HDA_DEVC_NOSNOOP       (0x1<<11)
98
99 /* Define IN stream 0 FIFO size offset in VIA controller */
100 #define VIA_IN_STREAM0_FIFO_SIZE_OFFSET 0x90
101 /* Define VIA HD Audio Device ID*/
102 #define VIA_HDAC_DEVICE_ID              0x3288
103
104 /* max number of SDs */
105 /* ICH, ATI and VIA have 4 playback and 4 capture */
106 #define ICH6_NUM_CAPTURE        4
107 #define ICH6_NUM_PLAYBACK       4
108
109 /* ULI has 6 playback and 5 capture */
110 #define ULI_NUM_CAPTURE         5
111 #define ULI_NUM_PLAYBACK        6
112
113 /* ATI HDMI may have up to 8 playbacks and 0 capture */
114 #define ATIHDMI_NUM_CAPTURE     0
115 #define ATIHDMI_NUM_PLAYBACK    8
116
117 /* TERA has 4 playback and 3 capture */
118 #define TERA_NUM_CAPTURE        3
119 #define TERA_NUM_PLAYBACK       4
120
121
122 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
123 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
124 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
125 static char *model[SNDRV_CARDS];
126 static int position_fix[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
127 static int bdl_pos_adj[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
128 static int probe_mask[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = -1};
129 static int probe_only[SNDRV_CARDS];
130 static int jackpoll_ms[SNDRV_CARDS];
131 static bool single_cmd;
132 static int enable_msi = -1;
133 #ifdef CONFIG_SND_HDA_PATCH_LOADER
134 static char *patch[SNDRV_CARDS];
135 #endif
136 #ifdef CONFIG_SND_HDA_INPUT_BEEP
137 static bool beep_mode[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] =
138                                         CONFIG_SND_HDA_INPUT_BEEP_MODE};
139 #endif
140
141 module_param_array(index, int, NULL, 0444);
142 MODULE_PARM_DESC(index, "Index value for Intel HD audio interface.");
143 module_param_array(id, charp, NULL, 0444);
144 MODULE_PARM_DESC(id, "ID string for Intel HD audio interface.");
145 module_param_array(enable, bool, NULL, 0444);
146 MODULE_PARM_DESC(enable, "Enable Intel HD audio interface.");
147 module_param_array(model, charp, NULL, 0444);
148 MODULE_PARM_DESC(model, "Use the given board model.");
149 module_param_array(position_fix, int, NULL, 0444);
150 MODULE_PARM_DESC(position_fix, "DMA pointer read method."
151                  "(-1 = system default, 0 = auto, 1 = LPIB, 2 = POSBUF, 3 = VIACOMBO, 4 = COMBO).");
152 module_param_array(bdl_pos_adj, int, NULL, 0644);
153 MODULE_PARM_DESC(bdl_pos_adj, "BDL position adjustment offset.");
154 module_param_array(probe_mask, int, NULL, 0444);
155 MODULE_PARM_DESC(probe_mask, "Bitmask to probe codecs (default = -1).");
156 module_param_array(probe_only, int, NULL, 0444);
157 MODULE_PARM_DESC(probe_only, "Only probing and no codec initialization.");
158 module_param_array(jackpoll_ms, int, NULL, 0444);
159 MODULE_PARM_DESC(jackpoll_ms, "Ms between polling for jack events (default = 0, using unsol events only)");
160 module_param(single_cmd, bool, 0444);
161 MODULE_PARM_DESC(single_cmd, "Use single command to communicate with codecs "
162                  "(for debugging only).");
163 module_param(enable_msi, bint, 0444);
164 MODULE_PARM_DESC(enable_msi, "Enable Message Signaled Interrupt (MSI)");
165 #ifdef CONFIG_SND_HDA_PATCH_LOADER
166 module_param_array(patch, charp, NULL, 0444);
167 MODULE_PARM_DESC(patch, "Patch file for Intel HD audio interface.");
168 #endif
169 #ifdef CONFIG_SND_HDA_INPUT_BEEP
170 module_param_array(beep_mode, bool, NULL, 0444);
171 MODULE_PARM_DESC(beep_mode, "Select HDA Beep registration mode "
172                             "(0=off, 1=on) (default=1).");
173 #endif
174
175 #ifdef CONFIG_PM
176 static int param_set_xint(const char *val, const struct kernel_param *kp);
177 static const struct kernel_param_ops param_ops_xint = {
178         .set = param_set_xint,
179         .get = param_get_int,
180 };
181 #define param_check_xint param_check_int
182
183 static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
184 module_param(power_save, xint, 0644);
185 MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
186                  "(in second, 0 = disable).");
187
188 static bool pm_blacklist = true;
189 module_param(pm_blacklist, bool, 0644);
190 MODULE_PARM_DESC(pm_blacklist, "Enable power-management blacklist");
191
192 /* reset the HD-audio controller in power save mode.
193  * this may give more power-saving, but will take longer time to
194  * wake up.
195  */
196 static bool power_save_controller = 1;
197 module_param(power_save_controller, bool, 0644);
198 MODULE_PARM_DESC(power_save_controller, "Reset controller in power save mode.");
199 #else
200 #define power_save      0
201 #endif /* CONFIG_PM */
202
203 static int align_buffer_size = -1;
204 module_param(align_buffer_size, bint, 0644);
205 MODULE_PARM_DESC(align_buffer_size,
206                 "Force buffer and period sizes to be multiple of 128 bytes.");
207
208 #ifdef CONFIG_X86
209 static int hda_snoop = -1;
210 module_param_named(snoop, hda_snoop, bint, 0444);
211 MODULE_PARM_DESC(snoop, "Enable/disable snooping");
212 #else
213 #define hda_snoop               true
214 #endif
215
216
217 MODULE_LICENSE("GPL");
218 MODULE_SUPPORTED_DEVICE("{{Intel, ICH6},"
219                          "{Intel, ICH6M},"
220                          "{Intel, ICH7},"
221                          "{Intel, ESB2},"
222                          "{Intel, ICH8},"
223                          "{Intel, ICH9},"
224                          "{Intel, ICH10},"
225                          "{Intel, PCH},"
226                          "{Intel, CPT},"
227                          "{Intel, PPT},"
228                          "{Intel, LPT},"
229                          "{Intel, LPT_LP},"
230                          "{Intel, WPT_LP},"
231                          "{Intel, SPT},"
232                          "{Intel, SPT_LP},"
233                          "{Intel, HPT},"
234                          "{Intel, PBG},"
235                          "{Intel, SCH},"
236                          "{ATI, SB450},"
237                          "{ATI, SB600},"
238                          "{ATI, RS600},"
239                          "{ATI, RS690},"
240                          "{ATI, RS780},"
241                          "{ATI, R600},"
242                          "{ATI, RV630},"
243                          "{ATI, RV610},"
244                          "{ATI, RV670},"
245                          "{ATI, RV635},"
246                          "{ATI, RV620},"
247                          "{ATI, RV770},"
248                          "{VIA, VT8251},"
249                          "{VIA, VT8237A},"
250                          "{SiS, SIS966},"
251                          "{ULI, M5461}}");
252 MODULE_DESCRIPTION("Intel HDA driver");
253
254 #if defined(CONFIG_PM) && defined(CONFIG_VGA_SWITCHEROO)
255 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
256 #define SUPPORT_VGA_SWITCHEROO
257 #endif
258 #endif
259
260
261 /*
262  */
263
264 /* driver types */
265 enum {
266         AZX_DRIVER_ICH,
267         AZX_DRIVER_PCH,
268         AZX_DRIVER_SCH,
269         AZX_DRIVER_HDMI,
270         AZX_DRIVER_ATI,
271         AZX_DRIVER_ATIHDMI,
272         AZX_DRIVER_ATIHDMI_NS,
273         AZX_DRIVER_VIA,
274         AZX_DRIVER_SIS,
275         AZX_DRIVER_ULI,
276         AZX_DRIVER_NVIDIA,
277         AZX_DRIVER_TERA,
278         AZX_DRIVER_CTX,
279         AZX_DRIVER_CTHDA,
280         AZX_DRIVER_CMEDIA,
281         AZX_DRIVER_GENERIC,
282         AZX_NUM_DRIVERS, /* keep this as last entry */
283 };
284
285 #define azx_get_snoop_type(chip) \
286         (((chip)->driver_caps & AZX_DCAPS_SNOOP_MASK) >> 10)
287 #define AZX_DCAPS_SNOOP_TYPE(type) ((AZX_SNOOP_TYPE_ ## type) << 10)
288
289 /* quirks for old Intel chipsets */
290 #define AZX_DCAPS_INTEL_ICH \
291         (AZX_DCAPS_OLD_SSYNC | AZX_DCAPS_NO_ALIGN_BUFSIZE)
292
293 /* quirks for Intel PCH */
294 #define AZX_DCAPS_INTEL_PCH_BASE \
295         (AZX_DCAPS_NO_ALIGN_BUFSIZE | AZX_DCAPS_COUNT_LPIB_DELAY |\
296          AZX_DCAPS_SNOOP_TYPE(SCH))
297
298 /* PCH up to IVB; no runtime PM */
299 #define AZX_DCAPS_INTEL_PCH_NOPM \
300         (AZX_DCAPS_INTEL_PCH_BASE)
301
302 /* PCH for HSW/BDW; with runtime PM */
303 #define AZX_DCAPS_INTEL_PCH \
304         (AZX_DCAPS_INTEL_PCH_BASE | AZX_DCAPS_PM_RUNTIME)
305
306 /* HSW HDMI */
307 #define AZX_DCAPS_INTEL_HASWELL \
308         (/*AZX_DCAPS_ALIGN_BUFSIZE |*/ AZX_DCAPS_COUNT_LPIB_DELAY |\
309          AZX_DCAPS_PM_RUNTIME | AZX_DCAPS_I915_POWERWELL |\
310          AZX_DCAPS_SNOOP_TYPE(SCH))
311
312 /* Broadwell HDMI can't use position buffer reliably, force to use LPIB */
313 #define AZX_DCAPS_INTEL_BROADWELL \
314         (/*AZX_DCAPS_ALIGN_BUFSIZE |*/ AZX_DCAPS_POSFIX_LPIB |\
315          AZX_DCAPS_PM_RUNTIME | AZX_DCAPS_I915_POWERWELL |\
316          AZX_DCAPS_SNOOP_TYPE(SCH))
317
318 #define AZX_DCAPS_INTEL_BAYTRAIL \
319         (AZX_DCAPS_INTEL_PCH_NOPM | AZX_DCAPS_I915_POWERWELL)
320
321 #define AZX_DCAPS_INTEL_BRASWELL \
322         (AZX_DCAPS_INTEL_PCH | AZX_DCAPS_I915_POWERWELL)
323
324 #define AZX_DCAPS_INTEL_SKYLAKE \
325         (AZX_DCAPS_INTEL_PCH | AZX_DCAPS_SEPARATE_STREAM_TAG |\
326          AZX_DCAPS_I915_POWERWELL)
327
328 #define AZX_DCAPS_INTEL_BROXTON \
329         (AZX_DCAPS_INTEL_PCH | AZX_DCAPS_SEPARATE_STREAM_TAG |\
330          AZX_DCAPS_I915_POWERWELL)
331
332 /* quirks for ATI SB / AMD Hudson */
333 #define AZX_DCAPS_PRESET_ATI_SB \
334         (AZX_DCAPS_NO_TCSEL | AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB |\
335          AZX_DCAPS_SNOOP_TYPE(ATI))
336
337 /* quirks for ATI/AMD HDMI */
338 #define AZX_DCAPS_PRESET_ATI_HDMI \
339         (AZX_DCAPS_NO_TCSEL | AZX_DCAPS_SYNC_WRITE | AZX_DCAPS_POSFIX_LPIB|\
340          AZX_DCAPS_NO_MSI64)
341
342 /* quirks for ATI HDMI with snoop off */
343 #define AZX_DCAPS_PRESET_ATI_HDMI_NS \
344         (AZX_DCAPS_PRESET_ATI_HDMI | AZX_DCAPS_SNOOP_OFF)
345
346 /* quirks for Nvidia */
347 #define AZX_DCAPS_PRESET_NVIDIA \
348         (AZX_DCAPS_NO_MSI | AZX_DCAPS_CORBRP_SELF_CLEAR |\
349          AZX_DCAPS_SNOOP_TYPE(NVIDIA))
350
351 #define AZX_DCAPS_PRESET_CTHDA \
352         (AZX_DCAPS_NO_MSI | AZX_DCAPS_POSFIX_LPIB |\
353          AZX_DCAPS_NO_64BIT |\
354          AZX_DCAPS_4K_BDLE_BOUNDARY | AZX_DCAPS_SNOOP_OFF)
355
356 /*
357  * vga_switcheroo support
358  */
359 #ifdef SUPPORT_VGA_SWITCHEROO
360 #define use_vga_switcheroo(chip)        ((chip)->use_vga_switcheroo)
361 #else
362 #define use_vga_switcheroo(chip)        0
363 #endif
364
365 #define CONTROLLER_IN_GPU(pci) (((pci)->device == 0x0a0c) || \
366                                         ((pci)->device == 0x0c0c) || \
367                                         ((pci)->device == 0x0d0c) || \
368                                         ((pci)->device == 0x160c))
369
370 #define IS_SKL(pci) ((pci)->vendor == 0x8086 && (pci)->device == 0xa170)
371 #define IS_SKL_LP(pci) ((pci)->vendor == 0x8086 && (pci)->device == 0x9d70)
372 #define IS_KBL(pci) ((pci)->vendor == 0x8086 && (pci)->device == 0xa171)
373 #define IS_KBL_LP(pci) ((pci)->vendor == 0x8086 && (pci)->device == 0x9d71)
374 #define IS_KBL_H(pci) ((pci)->vendor == 0x8086 && (pci)->device == 0xa2f0)
375 #define IS_BXT(pci) ((pci)->vendor == 0x8086 && (pci)->device == 0x5a98)
376 #define IS_GLK(pci) ((pci)->vendor == 0x8086 && (pci)->device == 0x3198)
377 #define IS_SKL_PLUS(pci) (IS_SKL(pci) || IS_SKL_LP(pci) || IS_BXT(pci)) || \
378                         IS_KBL(pci) || IS_KBL_LP(pci) || IS_KBL_H(pci)  || \
379                         IS_GLK(pci)
380
381 static char *driver_short_names[] = {
382         [AZX_DRIVER_ICH] = "HDA Intel",
383         [AZX_DRIVER_PCH] = "HDA Intel PCH",
384         [AZX_DRIVER_SCH] = "HDA Intel MID",
385         [AZX_DRIVER_HDMI] = "HDA Intel HDMI",
386         [AZX_DRIVER_ATI] = "HDA ATI SB",
387         [AZX_DRIVER_ATIHDMI] = "HDA ATI HDMI",
388         [AZX_DRIVER_ATIHDMI_NS] = "HDA ATI HDMI",
389         [AZX_DRIVER_VIA] = "HDA VIA VT82xx",
390         [AZX_DRIVER_SIS] = "HDA SIS966",
391         [AZX_DRIVER_ULI] = "HDA ULI M5461",
392         [AZX_DRIVER_NVIDIA] = "HDA NVidia",
393         [AZX_DRIVER_TERA] = "HDA Teradici", 
394         [AZX_DRIVER_CTX] = "HDA Creative", 
395         [AZX_DRIVER_CTHDA] = "HDA Creative",
396         [AZX_DRIVER_CMEDIA] = "HDA C-Media",
397         [AZX_DRIVER_GENERIC] = "HD-Audio Generic",
398 };
399
400 #ifdef CONFIG_X86
401 static void __mark_pages_wc(struct azx *chip, struct snd_dma_buffer *dmab, bool on)
402 {
403         int pages;
404
405         if (azx_snoop(chip))
406                 return;
407         if (!dmab || !dmab->area || !dmab->bytes)
408                 return;
409
410 #ifdef CONFIG_SND_DMA_SGBUF
411         if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_SG) {
412                 struct snd_sg_buf *sgbuf = dmab->private_data;
413                 if (!chip->uc_buffer)
414                         return; /* deal with only CORB/RIRB buffers */
415                 if (on)
416                         set_pages_array_wc(sgbuf->page_table, sgbuf->pages);
417                 else
418                         set_pages_array_wb(sgbuf->page_table, sgbuf->pages);
419                 return;
420         }
421 #endif
422
423         pages = (dmab->bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
424         if (on)
425                 set_memory_wc((unsigned long)dmab->area, pages);
426         else
427                 set_memory_wb((unsigned long)dmab->area, pages);
428 }
429
430 static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
431                                  bool on)
432 {
433         __mark_pages_wc(chip, buf, on);
434 }
435 static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
436                                    struct snd_pcm_substream *substream, bool on)
437 {
438         if (azx_dev->wc_marked != on) {
439                 __mark_pages_wc(chip, snd_pcm_get_dma_buf(substream), on);
440                 azx_dev->wc_marked = on;
441         }
442 }
443 #else
444 /* NOP for other archs */
445 static inline void mark_pages_wc(struct azx *chip, struct snd_dma_buffer *buf,
446                                  bool on)
447 {
448 }
449 static inline void mark_runtime_wc(struct azx *chip, struct azx_dev *azx_dev,
450                                    struct snd_pcm_substream *substream, bool on)
451 {
452 }
453 #endif
454
455 static int azx_acquire_irq(struct azx *chip, int do_disconnect);
456
457 /*
458  * initialize the PCI registers
459  */
460 /* update bits in a PCI register byte */
461 static void update_pci_byte(struct pci_dev *pci, unsigned int reg,
462                             unsigned char mask, unsigned char val)
463 {
464         unsigned char data;
465
466         pci_read_config_byte(pci, reg, &data);
467         data &= ~mask;
468         data |= (val & mask);
469         pci_write_config_byte(pci, reg, data);
470 }
471
472 static void azx_init_pci(struct azx *chip)
473 {
474         int snoop_type = azx_get_snoop_type(chip);
475
476         /* Clear bits 0-2 of PCI register TCSEL (at offset 0x44)
477          * TCSEL == Traffic Class Select Register, which sets PCI express QOS
478          * Ensuring these bits are 0 clears playback static on some HD Audio
479          * codecs.
480          * The PCI register TCSEL is defined in the Intel manuals.
481          */
482         if (!(chip->driver_caps & AZX_DCAPS_NO_TCSEL)) {
483                 dev_dbg(chip->card->dev, "Clearing TCSEL\n");
484                 update_pci_byte(chip->pci, AZX_PCIREG_TCSEL, 0x07, 0);
485         }
486
487         /* For ATI SB450/600/700/800/900 and AMD Hudson azalia HD audio,
488          * we need to enable snoop.
489          */
490         if (snoop_type == AZX_SNOOP_TYPE_ATI) {
491                 dev_dbg(chip->card->dev, "Setting ATI snoop: %d\n",
492                         azx_snoop(chip));
493                 update_pci_byte(chip->pci,
494                                 ATI_SB450_HDAUDIO_MISC_CNTR2_ADDR, 0x07,
495                                 azx_snoop(chip) ? ATI_SB450_HDAUDIO_ENABLE_SNOOP : 0);
496         }
497
498         /* For NVIDIA HDA, enable snoop */
499         if (snoop_type == AZX_SNOOP_TYPE_NVIDIA) {
500                 dev_dbg(chip->card->dev, "Setting Nvidia snoop: %d\n",
501                         azx_snoop(chip));
502                 update_pci_byte(chip->pci,
503                                 NVIDIA_HDA_TRANSREG_ADDR,
504                                 0x0f, NVIDIA_HDA_ENABLE_COHBITS);
505                 update_pci_byte(chip->pci,
506                                 NVIDIA_HDA_ISTRM_COH,
507                                 0x01, NVIDIA_HDA_ENABLE_COHBIT);
508                 update_pci_byte(chip->pci,
509                                 NVIDIA_HDA_OSTRM_COH,
510                                 0x01, NVIDIA_HDA_ENABLE_COHBIT);
511         }
512
513         /* Enable SCH/PCH snoop if needed */
514         if (snoop_type == AZX_SNOOP_TYPE_SCH) {
515                 unsigned short snoop;
516                 pci_read_config_word(chip->pci, INTEL_SCH_HDA_DEVC, &snoop);
517                 if ((!azx_snoop(chip) && !(snoop & INTEL_SCH_HDA_DEVC_NOSNOOP)) ||
518                     (azx_snoop(chip) && (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP))) {
519                         snoop &= ~INTEL_SCH_HDA_DEVC_NOSNOOP;
520                         if (!azx_snoop(chip))
521                                 snoop |= INTEL_SCH_HDA_DEVC_NOSNOOP;
522                         pci_write_config_word(chip->pci, INTEL_SCH_HDA_DEVC, snoop);
523                         pci_read_config_word(chip->pci,
524                                 INTEL_SCH_HDA_DEVC, &snoop);
525                 }
526                 dev_dbg(chip->card->dev, "SCH snoop: %s\n",
527                         (snoop & INTEL_SCH_HDA_DEVC_NOSNOOP) ?
528                         "Disabled" : "Enabled");
529         }
530 }
531
532 /*
533  * In BXT-P A0, HD-Audio DMA requests is later than expected,
534  * and makes an audio stream sensitive to system latencies when
535  * 24/32 bits are playing.
536  * Adjusting threshold of DMA fifo to force the DMA request
537  * sooner to improve latency tolerance at the expense of power.
538  */
539 static void bxt_reduce_dma_latency(struct azx *chip)
540 {
541         u32 val;
542
543         val = azx_readl(chip, SKL_EM4L);
544         val &= (0x3 << 20);
545         azx_writel(chip, SKL_EM4L, val);
546 }
547
548 static void hda_intel_init_chip(struct azx *chip, bool full_reset)
549 {
550         struct hdac_bus *bus = azx_bus(chip);
551         struct pci_dev *pci = chip->pci;
552         u32 val;
553
554         if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
555                 snd_hdac_set_codec_wakeup(bus, true);
556         if (IS_SKL_PLUS(pci)) {
557                 pci_read_config_dword(pci, INTEL_HDA_CGCTL, &val);
558                 val = val & ~INTEL_HDA_CGCTL_MISCBDCGE;
559                 pci_write_config_dword(pci, INTEL_HDA_CGCTL, val);
560         }
561         azx_init_chip(chip, full_reset);
562         if (IS_SKL_PLUS(pci)) {
563                 pci_read_config_dword(pci, INTEL_HDA_CGCTL, &val);
564                 val = val | INTEL_HDA_CGCTL_MISCBDCGE;
565                 pci_write_config_dword(pci, INTEL_HDA_CGCTL, val);
566         }
567         if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
568                 snd_hdac_set_codec_wakeup(bus, false);
569
570         /* reduce dma latency to avoid noise */
571         if (IS_BXT(pci))
572                 bxt_reduce_dma_latency(chip);
573 }
574
575 /* calculate runtime delay from LPIB */
576 static int azx_get_delay_from_lpib(struct azx *chip, struct azx_dev *azx_dev,
577                                    unsigned int pos)
578 {
579         struct snd_pcm_substream *substream = azx_dev->core.substream;
580         int stream = substream->stream;
581         unsigned int lpib_pos = azx_get_pos_lpib(chip, azx_dev);
582         int delay;
583
584         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
585                 delay = pos - lpib_pos;
586         else
587                 delay = lpib_pos - pos;
588         if (delay < 0) {
589                 if (delay >= azx_dev->core.delay_negative_threshold)
590                         delay = 0;
591                 else
592                         delay += azx_dev->core.bufsize;
593         }
594
595         if (delay >= azx_dev->core.period_bytes) {
596                 dev_info(chip->card->dev,
597                          "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
598                          delay, azx_dev->core.period_bytes);
599                 delay = 0;
600                 chip->driver_caps &= ~AZX_DCAPS_COUNT_LPIB_DELAY;
601                 chip->get_delay[stream] = NULL;
602         }
603
604         return bytes_to_frames(substream->runtime, delay);
605 }
606
607 static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev);
608
609 /* called from IRQ */
610 static int azx_position_check(struct azx *chip, struct azx_dev *azx_dev)
611 {
612         struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
613         int ok;
614
615         ok = azx_position_ok(chip, azx_dev);
616         if (ok == 1) {
617                 azx_dev->irq_pending = 0;
618                 return ok;
619         } else if (ok == 0) {
620                 /* bogus IRQ, process it later */
621                 azx_dev->irq_pending = 1;
622                 schedule_work(&hda->irq_pending_work);
623         }
624         return 0;
625 }
626
627 /* Enable/disable i915 display power for the link */
628 static int azx_intel_link_power(struct azx *chip, bool enable)
629 {
630         struct hdac_bus *bus = azx_bus(chip);
631
632         return snd_hdac_display_power(bus, enable);
633 }
634
635 /*
636  * Check whether the current DMA position is acceptable for updating
637  * periods.  Returns non-zero if it's OK.
638  *
639  * Many HD-audio controllers appear pretty inaccurate about
640  * the update-IRQ timing.  The IRQ is issued before actually the
641  * data is processed.  So, we need to process it afterwords in a
642  * workqueue.
643  */
644 static int azx_position_ok(struct azx *chip, struct azx_dev *azx_dev)
645 {
646         struct snd_pcm_substream *substream = azx_dev->core.substream;
647         int stream = substream->stream;
648         u32 wallclk;
649         unsigned int pos;
650
651         wallclk = azx_readl(chip, WALLCLK) - azx_dev->core.start_wallclk;
652         if (wallclk < (azx_dev->core.period_wallclk * 2) / 3)
653                 return -1;      /* bogus (too early) interrupt */
654
655         if (chip->get_position[stream])
656                 pos = chip->get_position[stream](chip, azx_dev);
657         else { /* use the position buffer as default */
658                 pos = azx_get_pos_posbuf(chip, azx_dev);
659                 if (!pos || pos == (u32)-1) {
660                         dev_info(chip->card->dev,
661                                  "Invalid position buffer, using LPIB read method instead.\n");
662                         chip->get_position[stream] = azx_get_pos_lpib;
663                         if (chip->get_position[0] == azx_get_pos_lpib &&
664                             chip->get_position[1] == azx_get_pos_lpib)
665                                 azx_bus(chip)->use_posbuf = false;
666                         pos = azx_get_pos_lpib(chip, azx_dev);
667                         chip->get_delay[stream] = NULL;
668                 } else {
669                         chip->get_position[stream] = azx_get_pos_posbuf;
670                         if (chip->driver_caps & AZX_DCAPS_COUNT_LPIB_DELAY)
671                                 chip->get_delay[stream] = azx_get_delay_from_lpib;
672                 }
673         }
674
675         if (pos >= azx_dev->core.bufsize)
676                 pos = 0;
677
678         if (WARN_ONCE(!azx_dev->core.period_bytes,
679                       "hda-intel: zero azx_dev->period_bytes"))
680                 return -1; /* this shouldn't happen! */
681         if (wallclk < (azx_dev->core.period_wallclk * 5) / 4 &&
682             pos % azx_dev->core.period_bytes > azx_dev->core.period_bytes / 2)
683                 /* NG - it's below the first next period boundary */
684                 return chip->bdl_pos_adj ? 0 : -1;
685         azx_dev->core.start_wallclk += wallclk;
686         return 1; /* OK, it's fine */
687 }
688
689 /*
690  * The work for pending PCM period updates.
691  */
692 static void azx_irq_pending_work(struct work_struct *work)
693 {
694         struct hda_intel *hda = container_of(work, struct hda_intel, irq_pending_work);
695         struct azx *chip = &hda->chip;
696         struct hdac_bus *bus = azx_bus(chip);
697         struct hdac_stream *s;
698         int pending, ok;
699
700         if (!hda->irq_pending_warned) {
701                 dev_info(chip->card->dev,
702                          "IRQ timing workaround is activated for card #%d. Suggest a bigger bdl_pos_adj.\n",
703                          chip->card->number);
704                 hda->irq_pending_warned = 1;
705         }
706
707         for (;;) {
708                 pending = 0;
709                 spin_lock_irq(&bus->reg_lock);
710                 list_for_each_entry(s, &bus->stream_list, list) {
711                         struct azx_dev *azx_dev = stream_to_azx_dev(s);
712                         if (!azx_dev->irq_pending ||
713                             !s->substream ||
714                             !s->running)
715                                 continue;
716                         ok = azx_position_ok(chip, azx_dev);
717                         if (ok > 0) {
718                                 azx_dev->irq_pending = 0;
719                                 spin_unlock(&bus->reg_lock);
720                                 snd_pcm_period_elapsed(s->substream);
721                                 spin_lock(&bus->reg_lock);
722                         } else if (ok < 0) {
723                                 pending = 0;    /* too early */
724                         } else
725                                 pending++;
726                 }
727                 spin_unlock_irq(&bus->reg_lock);
728                 if (!pending)
729                         return;
730                 msleep(1);
731         }
732 }
733
734 /* clear irq_pending flags and assure no on-going workq */
735 static void azx_clear_irq_pending(struct azx *chip)
736 {
737         struct hdac_bus *bus = azx_bus(chip);
738         struct hdac_stream *s;
739
740         spin_lock_irq(&bus->reg_lock);
741         list_for_each_entry(s, &bus->stream_list, list) {
742                 struct azx_dev *azx_dev = stream_to_azx_dev(s);
743                 azx_dev->irq_pending = 0;
744         }
745         spin_unlock_irq(&bus->reg_lock);
746 }
747
748 static int azx_acquire_irq(struct azx *chip, int do_disconnect)
749 {
750         struct hdac_bus *bus = azx_bus(chip);
751
752         if (request_irq(chip->pci->irq, azx_interrupt,
753                         chip->msi ? 0 : IRQF_SHARED,
754                         chip->card->irq_descr, chip)) {
755                 dev_err(chip->card->dev,
756                         "unable to grab IRQ %d, disabling device\n",
757                         chip->pci->irq);
758                 if (do_disconnect)
759                         snd_card_disconnect(chip->card);
760                 return -1;
761         }
762         bus->irq = chip->pci->irq;
763         pci_intx(chip->pci, !chip->msi);
764         return 0;
765 }
766
767 /* get the current DMA position with correction on VIA chips */
768 static unsigned int azx_via_get_position(struct azx *chip,
769                                          struct azx_dev *azx_dev)
770 {
771         unsigned int link_pos, mini_pos, bound_pos;
772         unsigned int mod_link_pos, mod_dma_pos, mod_mini_pos;
773         unsigned int fifo_size;
774
775         link_pos = snd_hdac_stream_get_pos_lpib(azx_stream(azx_dev));
776         if (azx_dev->core.substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
777                 /* Playback, no problem using link position */
778                 return link_pos;
779         }
780
781         /* Capture */
782         /* For new chipset,
783          * use mod to get the DMA position just like old chipset
784          */
785         mod_dma_pos = le32_to_cpu(*azx_dev->core.posbuf);
786         mod_dma_pos %= azx_dev->core.period_bytes;
787
788         /* azx_dev->fifo_size can't get FIFO size of in stream.
789          * Get from base address + offset.
790          */
791         fifo_size = readw(azx_bus(chip)->remap_addr +
792                           VIA_IN_STREAM0_FIFO_SIZE_OFFSET);
793
794         if (azx_dev->insufficient) {
795                 /* Link position never gather than FIFO size */
796                 if (link_pos <= fifo_size)
797                         return 0;
798
799                 azx_dev->insufficient = 0;
800         }
801
802         if (link_pos <= fifo_size)
803                 mini_pos = azx_dev->core.bufsize + link_pos - fifo_size;
804         else
805                 mini_pos = link_pos - fifo_size;
806
807         /* Find nearest previous boudary */
808         mod_mini_pos = mini_pos % azx_dev->core.period_bytes;
809         mod_link_pos = link_pos % azx_dev->core.period_bytes;
810         if (mod_link_pos >= fifo_size)
811                 bound_pos = link_pos - mod_link_pos;
812         else if (mod_dma_pos >= mod_mini_pos)
813                 bound_pos = mini_pos - mod_mini_pos;
814         else {
815                 bound_pos = mini_pos - mod_mini_pos + azx_dev->core.period_bytes;
816                 if (bound_pos >= azx_dev->core.bufsize)
817                         bound_pos = 0;
818         }
819
820         /* Calculate real DMA position we want */
821         return bound_pos + mod_dma_pos;
822 }
823
824 #ifdef CONFIG_PM
825 static DEFINE_MUTEX(card_list_lock);
826 static LIST_HEAD(card_list);
827
828 static void azx_add_card_list(struct azx *chip)
829 {
830         struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
831         mutex_lock(&card_list_lock);
832         list_add(&hda->list, &card_list);
833         mutex_unlock(&card_list_lock);
834 }
835
836 static void azx_del_card_list(struct azx *chip)
837 {
838         struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
839         mutex_lock(&card_list_lock);
840         list_del_init(&hda->list);
841         mutex_unlock(&card_list_lock);
842 }
843
844 /* trigger power-save check at writing parameter */
845 static int param_set_xint(const char *val, const struct kernel_param *kp)
846 {
847         struct hda_intel *hda;
848         struct azx *chip;
849         int prev = power_save;
850         int ret = param_set_int(val, kp);
851
852         if (ret || prev == power_save)
853                 return ret;
854
855         mutex_lock(&card_list_lock);
856         list_for_each_entry(hda, &card_list, list) {
857                 chip = &hda->chip;
858                 if (!hda->probe_continued || chip->disabled)
859                         continue;
860                 snd_hda_set_power_save(&chip->bus, power_save * 1000);
861         }
862         mutex_unlock(&card_list_lock);
863         return 0;
864 }
865 #else
866 #define azx_add_card_list(chip) /* NOP */
867 #define azx_del_card_list(chip) /* NOP */
868 #endif /* CONFIG_PM */
869
870 #if defined(CONFIG_PM_SLEEP) || defined(SUPPORT_VGA_SWITCHEROO)
871 /*
872  * power management
873  */
874 static int azx_suspend(struct device *dev)
875 {
876         struct snd_card *card = dev_get_drvdata(dev);
877         struct azx *chip;
878         struct hda_intel *hda;
879         struct hdac_bus *bus;
880
881         if (!card)
882                 return 0;
883
884         chip = card->private_data;
885         hda = container_of(chip, struct hda_intel, chip);
886         if (chip->disabled || hda->init_failed || !chip->running)
887                 return 0;
888
889         bus = azx_bus(chip);
890         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
891         azx_clear_irq_pending(chip);
892         azx_stop_chip(chip);
893         azx_enter_link_reset(chip);
894         if (bus->irq >= 0) {
895                 free_irq(bus->irq, chip);
896                 bus->irq = -1;
897         }
898
899         if (chip->msi)
900                 pci_disable_msi(chip->pci);
901         if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL
902                 && hda->need_i915_power)
903                 snd_hdac_display_power(bus, false);
904
905         trace_azx_suspend(chip);
906         return 0;
907 }
908
909 static int azx_resume(struct device *dev)
910 {
911         struct pci_dev *pci = to_pci_dev(dev);
912         struct snd_card *card = dev_get_drvdata(dev);
913         struct azx *chip;
914         struct hda_intel *hda;
915         struct hdac_bus *bus;
916
917         if (!card)
918                 return 0;
919
920         chip = card->private_data;
921         hda = container_of(chip, struct hda_intel, chip);
922         bus = azx_bus(chip);
923         if (chip->disabled || hda->init_failed || !chip->running)
924                 return 0;
925
926         if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
927                 snd_hdac_display_power(bus, true);
928                 if (hda->need_i915_power)
929                         snd_hdac_i915_set_bclk(bus);
930         }
931
932         if (chip->msi)
933                 if (pci_enable_msi(pci) < 0)
934                         chip->msi = 0;
935         if (azx_acquire_irq(chip, 1) < 0)
936                 return -EIO;
937         azx_init_pci(chip);
938
939         hda_intel_init_chip(chip, true);
940
941         /* power down again for link-controlled chips */
942         if ((chip->driver_caps & AZX_DCAPS_I915_POWERWELL) &&
943             !hda->need_i915_power)
944                 snd_hdac_display_power(bus, false);
945
946         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
947
948         trace_azx_resume(chip);
949         return 0;
950 }
951 #endif /* CONFIG_PM_SLEEP || SUPPORT_VGA_SWITCHEROO */
952
953 #ifdef CONFIG_PM_SLEEP
954 /* put codec down to D3 at hibernation for Intel SKL+;
955  * otherwise BIOS may still access the codec and screw up the driver
956  */
957 static int azx_freeze_noirq(struct device *dev)
958 {
959         struct pci_dev *pci = to_pci_dev(dev);
960
961         if (IS_SKL_PLUS(pci))
962                 pci_set_power_state(pci, PCI_D3hot);
963
964         return 0;
965 }
966
967 static int azx_thaw_noirq(struct device *dev)
968 {
969         struct pci_dev *pci = to_pci_dev(dev);
970
971         if (IS_SKL_PLUS(pci))
972                 pci_set_power_state(pci, PCI_D0);
973
974         return 0;
975 }
976 #endif /* CONFIG_PM_SLEEP */
977
978 #ifdef CONFIG_PM
979 static int azx_runtime_suspend(struct device *dev)
980 {
981         struct snd_card *card = dev_get_drvdata(dev);
982         struct azx *chip;
983         struct hda_intel *hda;
984
985         if (!card)
986                 return 0;
987
988         chip = card->private_data;
989         hda = container_of(chip, struct hda_intel, chip);
990         if (chip->disabled || hda->init_failed)
991                 return 0;
992
993         if (!azx_has_pm_runtime(chip))
994                 return 0;
995
996         /* enable controller wake up event */
997         azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) |
998                   STATESTS_INT_MASK);
999
1000         azx_stop_chip(chip);
1001         azx_enter_link_reset(chip);
1002         azx_clear_irq_pending(chip);
1003         if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL
1004                 && hda->need_i915_power)
1005                 snd_hdac_display_power(azx_bus(chip), false);
1006
1007         trace_azx_runtime_suspend(chip);
1008         return 0;
1009 }
1010
1011 static int azx_runtime_resume(struct device *dev)
1012 {
1013         struct snd_card *card = dev_get_drvdata(dev);
1014         struct azx *chip;
1015         struct hda_intel *hda;
1016         struct hdac_bus *bus;
1017         struct hda_codec *codec;
1018         int status;
1019
1020         if (!card)
1021                 return 0;
1022
1023         chip = card->private_data;
1024         hda = container_of(chip, struct hda_intel, chip);
1025         bus = azx_bus(chip);
1026         if (chip->disabled || hda->init_failed)
1027                 return 0;
1028
1029         if (!azx_has_pm_runtime(chip))
1030                 return 0;
1031
1032         if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
1033                 snd_hdac_display_power(bus, true);
1034                 if (hda->need_i915_power)
1035                         snd_hdac_i915_set_bclk(bus);
1036         }
1037
1038         /* Read STATESTS before controller reset */
1039         status = azx_readw(chip, STATESTS);
1040
1041         azx_init_pci(chip);
1042         hda_intel_init_chip(chip, true);
1043
1044         if (status) {
1045                 list_for_each_codec(codec, &chip->bus)
1046                         if (status & (1 << codec->addr))
1047                                 schedule_delayed_work(&codec->jackpoll_work,
1048                                                       codec->jackpoll_interval);
1049         }
1050
1051         /* disable controller Wake Up event*/
1052         azx_writew(chip, WAKEEN, azx_readw(chip, WAKEEN) &
1053                         ~STATESTS_INT_MASK);
1054
1055         /* power down again for link-controlled chips */
1056         if ((chip->driver_caps & AZX_DCAPS_I915_POWERWELL) &&
1057             !hda->need_i915_power)
1058                 snd_hdac_display_power(bus, false);
1059
1060         trace_azx_runtime_resume(chip);
1061         return 0;
1062 }
1063
1064 static int azx_runtime_idle(struct device *dev)
1065 {
1066         struct snd_card *card = dev_get_drvdata(dev);
1067         struct azx *chip;
1068         struct hda_intel *hda;
1069
1070         if (!card)
1071                 return 0;
1072
1073         chip = card->private_data;
1074         hda = container_of(chip, struct hda_intel, chip);
1075         if (chip->disabled || hda->init_failed)
1076                 return 0;
1077
1078         if (!power_save_controller || !azx_has_pm_runtime(chip) ||
1079             azx_bus(chip)->codec_powered || !chip->running)
1080                 return -EBUSY;
1081
1082         return 0;
1083 }
1084
1085 static const struct dev_pm_ops azx_pm = {
1086         SET_SYSTEM_SLEEP_PM_OPS(azx_suspend, azx_resume)
1087 #ifdef CONFIG_PM_SLEEP
1088         .freeze_noirq = azx_freeze_noirq,
1089         .thaw_noirq = azx_thaw_noirq,
1090 #endif
1091         SET_RUNTIME_PM_OPS(azx_runtime_suspend, azx_runtime_resume, azx_runtime_idle)
1092 };
1093
1094 #define AZX_PM_OPS      &azx_pm
1095 #else
1096 #define AZX_PM_OPS      NULL
1097 #endif /* CONFIG_PM */
1098
1099
1100 static int azx_probe_continue(struct azx *chip);
1101
1102 #ifdef SUPPORT_VGA_SWITCHEROO
1103 static struct pci_dev *get_bound_vga(struct pci_dev *pci);
1104
1105 static void azx_vs_set_state(struct pci_dev *pci,
1106                              enum vga_switcheroo_state state)
1107 {
1108         struct snd_card *card = pci_get_drvdata(pci);
1109         struct azx *chip = card->private_data;
1110         struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
1111         bool disabled;
1112
1113         wait_for_completion(&hda->probe_wait);
1114         if (hda->init_failed)
1115                 return;
1116
1117         disabled = (state == VGA_SWITCHEROO_OFF);
1118         if (chip->disabled == disabled)
1119                 return;
1120
1121         if (!hda->probe_continued) {
1122                 chip->disabled = disabled;
1123                 if (!disabled) {
1124                         dev_info(chip->card->dev,
1125                                  "Start delayed initialization\n");
1126                         if (azx_probe_continue(chip) < 0) {
1127                                 dev_err(chip->card->dev, "initialization error\n");
1128                                 hda->init_failed = true;
1129                         }
1130                 }
1131         } else {
1132                 dev_info(chip->card->dev, "%s via vga_switcheroo\n",
1133                          disabled ? "Disabling" : "Enabling");
1134                 if (disabled) {
1135                         pm_runtime_put_sync_suspend(card->dev);
1136                         azx_suspend(card->dev);
1137                         /* when we get suspended by vga_switcheroo we end up in D3cold,
1138                          * however we have no ACPI handle, so pci/acpi can't put us there,
1139                          * put ourselves there */
1140                         pci->current_state = PCI_D3cold;
1141                         chip->disabled = true;
1142                         if (snd_hda_lock_devices(&chip->bus))
1143                                 dev_warn(chip->card->dev,
1144                                          "Cannot lock devices!\n");
1145                 } else {
1146                         snd_hda_unlock_devices(&chip->bus);
1147                         pm_runtime_get_noresume(card->dev);
1148                         chip->disabled = false;
1149                         azx_resume(card->dev);
1150                 }
1151         }
1152 }
1153
1154 static bool azx_vs_can_switch(struct pci_dev *pci)
1155 {
1156         struct snd_card *card = pci_get_drvdata(pci);
1157         struct azx *chip = card->private_data;
1158         struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
1159
1160         wait_for_completion(&hda->probe_wait);
1161         if (hda->init_failed)
1162                 return false;
1163         if (chip->disabled || !hda->probe_continued)
1164                 return true;
1165         if (snd_hda_lock_devices(&chip->bus))
1166                 return false;
1167         snd_hda_unlock_devices(&chip->bus);
1168         return true;
1169 }
1170
1171 static void init_vga_switcheroo(struct azx *chip)
1172 {
1173         struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
1174         struct pci_dev *p = get_bound_vga(chip->pci);
1175         if (p) {
1176                 dev_info(chip->card->dev,
1177                          "Handle vga_switcheroo audio client\n");
1178                 hda->use_vga_switcheroo = 1;
1179                 pci_dev_put(p);
1180         }
1181 }
1182
1183 static const struct vga_switcheroo_client_ops azx_vs_ops = {
1184         .set_gpu_state = azx_vs_set_state,
1185         .can_switch = azx_vs_can_switch,
1186 };
1187
1188 static int register_vga_switcheroo(struct azx *chip)
1189 {
1190         struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
1191         int err;
1192
1193         if (!hda->use_vga_switcheroo)
1194                 return 0;
1195         /* FIXME: currently only handling DIS controller
1196          * is there any machine with two switchable HDMI audio controllers?
1197          */
1198         err = vga_switcheroo_register_audio_client(chip->pci, &azx_vs_ops,
1199                                                    VGA_SWITCHEROO_DIS);
1200         if (err < 0)
1201                 return err;
1202         hda->vga_switcheroo_registered = 1;
1203
1204         /* register as an optimus hdmi audio power domain */
1205         vga_switcheroo_init_domain_pm_optimus_hdmi_audio(chip->card->dev,
1206                                                          &hda->hdmi_pm_domain);
1207         return 0;
1208 }
1209 #else
1210 #define init_vga_switcheroo(chip)               /* NOP */
1211 #define register_vga_switcheroo(chip)           0
1212 #define check_hdmi_disabled(pci)        false
1213 #endif /* SUPPORT_VGA_SWITCHER */
1214
1215 /*
1216  * destructor
1217  */
1218 static int azx_free(struct azx *chip)
1219 {
1220         struct pci_dev *pci = chip->pci;
1221         struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
1222         struct hdac_bus *bus = azx_bus(chip);
1223
1224         if (azx_has_pm_runtime(chip) && chip->running)
1225                 pm_runtime_get_noresume(&pci->dev);
1226
1227         azx_del_card_list(chip);
1228
1229         hda->init_failed = 1; /* to be sure */
1230         complete_all(&hda->probe_wait);
1231
1232         if (use_vga_switcheroo(hda)) {
1233                 if (chip->disabled && hda->probe_continued)
1234                         snd_hda_unlock_devices(&chip->bus);
1235                 if (hda->vga_switcheroo_registered) {
1236                         vga_switcheroo_unregister_client(chip->pci);
1237                         vga_switcheroo_fini_domain_pm_ops(chip->card->dev);
1238                 }
1239         }
1240
1241         if (bus->chip_init) {
1242                 azx_clear_irq_pending(chip);
1243                 azx_stop_all_streams(chip);
1244                 azx_stop_chip(chip);
1245         }
1246
1247         if (bus->irq >= 0)
1248                 free_irq(bus->irq, (void*)chip);
1249         if (chip->msi)
1250                 pci_disable_msi(chip->pci);
1251         iounmap(bus->remap_addr);
1252
1253         azx_free_stream_pages(chip);
1254         azx_free_streams(chip);
1255         snd_hdac_bus_exit(bus);
1256
1257         if (chip->region_requested)
1258                 pci_release_regions(chip->pci);
1259
1260         pci_disable_device(chip->pci);
1261 #ifdef CONFIG_SND_HDA_PATCH_LOADER
1262         release_firmware(chip->fw);
1263 #endif
1264
1265         if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
1266                 if (hda->need_i915_power)
1267                         snd_hdac_display_power(bus, false);
1268                 snd_hdac_i915_exit(bus);
1269         }
1270         kfree(hda);
1271
1272         return 0;
1273 }
1274
1275 static int azx_dev_disconnect(struct snd_device *device)
1276 {
1277         struct azx *chip = device->device_data;
1278         struct hdac_bus *bus = azx_bus(chip);
1279
1280         chip->bus.shutdown = 1;
1281         cancel_work_sync(&bus->unsol_work);
1282
1283         return 0;
1284 }
1285
1286 static int azx_dev_free(struct snd_device *device)
1287 {
1288         return azx_free(device->device_data);
1289 }
1290
1291 #ifdef SUPPORT_VGA_SWITCHEROO
1292 /*
1293  * Check of disabled HDMI controller by vga_switcheroo
1294  */
1295 static struct pci_dev *get_bound_vga(struct pci_dev *pci)
1296 {
1297         struct pci_dev *p;
1298
1299         /* check only discrete GPU */
1300         switch (pci->vendor) {
1301         case PCI_VENDOR_ID_ATI:
1302         case PCI_VENDOR_ID_AMD:
1303         case PCI_VENDOR_ID_NVIDIA:
1304                 if (pci->devfn == 1) {
1305                         p = pci_get_domain_bus_and_slot(pci_domain_nr(pci->bus),
1306                                                         pci->bus->number, 0);
1307                         if (p) {
1308                                 if ((p->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1309                                         return p;
1310                                 pci_dev_put(p);
1311                         }
1312                 }
1313                 break;
1314         }
1315         return NULL;
1316 }
1317
1318 static bool check_hdmi_disabled(struct pci_dev *pci)
1319 {
1320         bool vga_inactive = false;
1321         struct pci_dev *p = get_bound_vga(pci);
1322
1323         if (p) {
1324                 if (vga_switcheroo_get_client_state(p) == VGA_SWITCHEROO_OFF)
1325                         vga_inactive = true;
1326                 pci_dev_put(p);
1327         }
1328         return vga_inactive;
1329 }
1330 #endif /* SUPPORT_VGA_SWITCHEROO */
1331
1332 /*
1333  * white/black-listing for position_fix
1334  */
1335 static struct snd_pci_quirk position_fix_list[] = {
1336         SND_PCI_QUIRK(0x1028, 0x01cc, "Dell D820", POS_FIX_LPIB),
1337         SND_PCI_QUIRK(0x1028, 0x01de, "Dell Precision 390", POS_FIX_LPIB),
1338         SND_PCI_QUIRK(0x103c, 0x306d, "HP dv3", POS_FIX_LPIB),
1339         SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", POS_FIX_LPIB),
1340         SND_PCI_QUIRK(0x1043, 0x81b3, "ASUS", POS_FIX_LPIB),
1341         SND_PCI_QUIRK(0x1043, 0x81e7, "ASUS M2V", POS_FIX_LPIB),
1342         SND_PCI_QUIRK(0x104d, 0x9069, "Sony VPCS11V9E", POS_FIX_LPIB),
1343         SND_PCI_QUIRK(0x10de, 0xcb89, "Macbook Pro 7,1", POS_FIX_LPIB),
1344         SND_PCI_QUIRK(0x1297, 0x3166, "Shuttle", POS_FIX_LPIB),
1345         SND_PCI_QUIRK(0x1458, 0xa022, "ga-ma770-ud3", POS_FIX_LPIB),
1346         SND_PCI_QUIRK(0x1462, 0x1002, "MSI Wind U115", POS_FIX_LPIB),
1347         SND_PCI_QUIRK(0x1565, 0x8218, "Biostar Microtech", POS_FIX_LPIB),
1348         SND_PCI_QUIRK(0x1849, 0x0888, "775Dual-VSTA", POS_FIX_LPIB),
1349         SND_PCI_QUIRK(0x8086, 0x2503, "DG965OT AAD63733-203", POS_FIX_LPIB),
1350         {}
1351 };
1352
1353 static int check_position_fix(struct azx *chip, int fix)
1354 {
1355         const struct snd_pci_quirk *q;
1356
1357         switch (fix) {
1358         case POS_FIX_AUTO:
1359         case POS_FIX_LPIB:
1360         case POS_FIX_POSBUF:
1361         case POS_FIX_VIACOMBO:
1362         case POS_FIX_COMBO:
1363                 return fix;
1364         }
1365
1366         q = snd_pci_quirk_lookup(chip->pci, position_fix_list);
1367         if (q) {
1368                 dev_info(chip->card->dev,
1369                          "position_fix set to %d for device %04x:%04x\n",
1370                          q->value, q->subvendor, q->subdevice);
1371                 return q->value;
1372         }
1373
1374         /* Check VIA/ATI HD Audio Controller exist */
1375         if (chip->driver_type == AZX_DRIVER_VIA) {
1376                 dev_dbg(chip->card->dev, "Using VIACOMBO position fix\n");
1377                 return POS_FIX_VIACOMBO;
1378         }
1379         if (chip->driver_caps & AZX_DCAPS_POSFIX_LPIB) {
1380                 dev_dbg(chip->card->dev, "Using LPIB position fix\n");
1381                 return POS_FIX_LPIB;
1382         }
1383         return POS_FIX_AUTO;
1384 }
1385
1386 static void assign_position_fix(struct azx *chip, int fix)
1387 {
1388         static azx_get_pos_callback_t callbacks[] = {
1389                 [POS_FIX_AUTO] = NULL,
1390                 [POS_FIX_LPIB] = azx_get_pos_lpib,
1391                 [POS_FIX_POSBUF] = azx_get_pos_posbuf,
1392                 [POS_FIX_VIACOMBO] = azx_via_get_position,
1393                 [POS_FIX_COMBO] = azx_get_pos_lpib,
1394         };
1395
1396         chip->get_position[0] = chip->get_position[1] = callbacks[fix];
1397
1398         /* combo mode uses LPIB only for playback */
1399         if (fix == POS_FIX_COMBO)
1400                 chip->get_position[1] = NULL;
1401
1402         if (fix == POS_FIX_POSBUF &&
1403             (chip->driver_caps & AZX_DCAPS_COUNT_LPIB_DELAY)) {
1404                 chip->get_delay[0] = chip->get_delay[1] =
1405                         azx_get_delay_from_lpib;
1406         }
1407
1408 }
1409
1410 /*
1411  * black-lists for probe_mask
1412  */
1413 static struct snd_pci_quirk probe_mask_list[] = {
1414         /* Thinkpad often breaks the controller communication when accessing
1415          * to the non-working (or non-existing) modem codec slot.
1416          */
1417         SND_PCI_QUIRK(0x1014, 0x05b7, "Thinkpad Z60", 0x01),
1418         SND_PCI_QUIRK(0x17aa, 0x2010, "Thinkpad X/T/R60", 0x01),
1419         SND_PCI_QUIRK(0x17aa, 0x20ac, "Thinkpad X/T/R61", 0x01),
1420         /* broken BIOS */
1421         SND_PCI_QUIRK(0x1028, 0x20ac, "Dell Studio Desktop", 0x01),
1422         /* including bogus ALC268 in slot#2 that conflicts with ALC888 */
1423         SND_PCI_QUIRK(0x17c0, 0x4085, "Medion MD96630", 0x01),
1424         /* forced codec slots */
1425         SND_PCI_QUIRK(0x1043, 0x1262, "ASUS W5Fm", 0x103),
1426         SND_PCI_QUIRK(0x1046, 0x1262, "ASUS W5F", 0x103),
1427         SND_PCI_QUIRK(0x1558, 0x0351, "Schenker Dock 15", 0x105),
1428         /* WinFast VP200 H (Teradici) user reported broken communication */
1429         SND_PCI_QUIRK(0x3a21, 0x040d, "WinFast VP200 H", 0x101),
1430         {}
1431 };
1432
1433 #define AZX_FORCE_CODEC_MASK    0x100
1434
1435 static void check_probe_mask(struct azx *chip, int dev)
1436 {
1437         const struct snd_pci_quirk *q;
1438
1439         chip->codec_probe_mask = probe_mask[dev];
1440         if (chip->codec_probe_mask == -1) {
1441                 q = snd_pci_quirk_lookup(chip->pci, probe_mask_list);
1442                 if (q) {
1443                         dev_info(chip->card->dev,
1444                                  "probe_mask set to 0x%x for device %04x:%04x\n",
1445                                  q->value, q->subvendor, q->subdevice);
1446                         chip->codec_probe_mask = q->value;
1447                 }
1448         }
1449
1450         /* check forced option */
1451         if (chip->codec_probe_mask != -1 &&
1452             (chip->codec_probe_mask & AZX_FORCE_CODEC_MASK)) {
1453                 azx_bus(chip)->codec_mask = chip->codec_probe_mask & 0xff;
1454                 dev_info(chip->card->dev, "codec_mask forced to 0x%x\n",
1455                          (int)azx_bus(chip)->codec_mask);
1456         }
1457 }
1458
1459 /*
1460  * white/black-list for enable_msi
1461  */
1462 static struct snd_pci_quirk msi_black_list[] = {
1463         SND_PCI_QUIRK(0x103c, 0x2191, "HP", 0), /* AMD Hudson */
1464         SND_PCI_QUIRK(0x103c, 0x2192, "HP", 0), /* AMD Hudson */
1465         SND_PCI_QUIRK(0x103c, 0x21f7, "HP", 0), /* AMD Hudson */
1466         SND_PCI_QUIRK(0x103c, 0x21fa, "HP", 0), /* AMD Hudson */
1467         SND_PCI_QUIRK(0x1043, 0x81f2, "ASUS", 0), /* Athlon64 X2 + nvidia */
1468         SND_PCI_QUIRK(0x1043, 0x81f6, "ASUS", 0), /* nvidia */
1469         SND_PCI_QUIRK(0x1043, 0x822d, "ASUS", 0), /* Athlon64 X2 + nvidia MCP55 */
1470         SND_PCI_QUIRK(0x1179, 0xfb44, "Toshiba Satellite C870", 0), /* AMD Hudson */
1471         SND_PCI_QUIRK(0x1849, 0x0888, "ASRock", 0), /* Athlon64 X2 + nvidia */
1472         SND_PCI_QUIRK(0xa0a0, 0x0575, "Aopen MZ915-M", 0), /* ICH6 */
1473         {}
1474 };
1475
1476 static void check_msi(struct azx *chip)
1477 {
1478         const struct snd_pci_quirk *q;
1479
1480         if (enable_msi >= 0) {
1481                 chip->msi = !!enable_msi;
1482                 return;
1483         }
1484         chip->msi = 1;  /* enable MSI as default */
1485         q = snd_pci_quirk_lookup(chip->pci, msi_black_list);
1486         if (q) {
1487                 dev_info(chip->card->dev,
1488                          "msi for device %04x:%04x set to %d\n",
1489                          q->subvendor, q->subdevice, q->value);
1490                 chip->msi = q->value;
1491                 return;
1492         }
1493
1494         /* NVidia chipsets seem to cause troubles with MSI */
1495         if (chip->driver_caps & AZX_DCAPS_NO_MSI) {
1496                 dev_info(chip->card->dev, "Disabling MSI\n");
1497                 chip->msi = 0;
1498         }
1499 }
1500
1501 /* check the snoop mode availability */
1502 static void azx_check_snoop_available(struct azx *chip)
1503 {
1504         int snoop = hda_snoop;
1505
1506         if (snoop >= 0) {
1507                 dev_info(chip->card->dev, "Force to %s mode by module option\n",
1508                          snoop ? "snoop" : "non-snoop");
1509                 chip->snoop = snoop;
1510                 chip->uc_buffer = !snoop;
1511                 return;
1512         }
1513
1514         snoop = true;
1515         if (azx_get_snoop_type(chip) == AZX_SNOOP_TYPE_NONE &&
1516             chip->driver_type == AZX_DRIVER_VIA) {
1517                 /* force to non-snoop mode for a new VIA controller
1518                  * when BIOS is set
1519                  */
1520                 u8 val;
1521                 pci_read_config_byte(chip->pci, 0x42, &val);
1522                 if (!(val & 0x80) && (chip->pci->revision == 0x30 ||
1523                                       chip->pci->revision == 0x20))
1524                         snoop = false;
1525         }
1526
1527         if (chip->driver_caps & AZX_DCAPS_SNOOP_OFF)
1528                 snoop = false;
1529
1530         chip->snoop = snoop;
1531         if (!snoop) {
1532                 dev_info(chip->card->dev, "Force to non-snoop mode\n");
1533                 /* C-Media requires non-cached pages only for CORB/RIRB */
1534                 if (chip->driver_type != AZX_DRIVER_CMEDIA)
1535                         chip->uc_buffer = true;
1536         }
1537 }
1538
1539 static void azx_probe_work(struct work_struct *work)
1540 {
1541         struct hda_intel *hda = container_of(work, struct hda_intel, probe_work);
1542         azx_probe_continue(&hda->chip);
1543 }
1544
1545 static int default_bdl_pos_adj(struct azx *chip)
1546 {
1547         /* some exceptions: Atoms seem problematic with value 1 */
1548         if (chip->pci->vendor == PCI_VENDOR_ID_INTEL) {
1549                 switch (chip->pci->device) {
1550                 case 0x0f04: /* Baytrail */
1551                 case 0x2284: /* Braswell */
1552                         return 32;
1553                 }
1554         }
1555
1556         switch (chip->driver_type) {
1557         case AZX_DRIVER_ICH:
1558         case AZX_DRIVER_PCH:
1559                 return 1;
1560         default:
1561                 return 32;
1562         }
1563 }
1564
1565 /*
1566  * constructor
1567  */
1568 static const struct hdac_io_ops pci_hda_io_ops;
1569 static const struct hda_controller_ops pci_hda_ops;
1570
1571 static int azx_create(struct snd_card *card, struct pci_dev *pci,
1572                       int dev, unsigned int driver_caps,
1573                       struct azx **rchip)
1574 {
1575         static struct snd_device_ops ops = {
1576                 .dev_disconnect = azx_dev_disconnect,
1577                 .dev_free = azx_dev_free,
1578         };
1579         struct hda_intel *hda;
1580         struct azx *chip;
1581         int err;
1582
1583         *rchip = NULL;
1584
1585         err = pci_enable_device(pci);
1586         if (err < 0)
1587                 return err;
1588
1589         hda = kzalloc(sizeof(*hda), GFP_KERNEL);
1590         if (!hda) {
1591                 pci_disable_device(pci);
1592                 return -ENOMEM;
1593         }
1594
1595         chip = &hda->chip;
1596         mutex_init(&chip->open_mutex);
1597         chip->card = card;
1598         chip->pci = pci;
1599         chip->ops = &pci_hda_ops;
1600         chip->driver_caps = driver_caps;
1601         chip->driver_type = driver_caps & 0xff;
1602         check_msi(chip);
1603         chip->dev_index = dev;
1604         chip->jackpoll_ms = jackpoll_ms;
1605         INIT_LIST_HEAD(&chip->pcm_list);
1606         INIT_WORK(&hda->irq_pending_work, azx_irq_pending_work);
1607         INIT_LIST_HEAD(&hda->list);
1608         init_vga_switcheroo(chip);
1609         init_completion(&hda->probe_wait);
1610
1611         assign_position_fix(chip, check_position_fix(chip, position_fix[dev]));
1612
1613         chip->single_cmd = single_cmd;
1614         azx_check_snoop_available(chip);
1615
1616         if (bdl_pos_adj[dev] < 0)
1617                 chip->bdl_pos_adj = default_bdl_pos_adj(chip);
1618         else
1619                 chip->bdl_pos_adj = bdl_pos_adj[dev];
1620
1621         err = azx_bus_init(chip, model[dev], &pci_hda_io_ops);
1622         if (err < 0) {
1623                 kfree(hda);
1624                 pci_disable_device(pci);
1625                 return err;
1626         }
1627
1628         if (chip->driver_type == AZX_DRIVER_NVIDIA) {
1629                 dev_dbg(chip->card->dev, "Enable delay in RIRB handling\n");
1630                 chip->bus.needs_damn_long_delay = 1;
1631         }
1632
1633         check_probe_mask(chip, dev);
1634
1635         err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1636         if (err < 0) {
1637                 dev_err(card->dev, "Error creating device [card]!\n");
1638                 azx_free(chip);
1639                 return err;
1640         }
1641
1642         /* continue probing in work context as may trigger request module */
1643         INIT_WORK(&hda->probe_work, azx_probe_work);
1644
1645         *rchip = chip;
1646
1647         return 0;
1648 }
1649
1650 static int azx_first_init(struct azx *chip)
1651 {
1652         int dev = chip->dev_index;
1653         struct pci_dev *pci = chip->pci;
1654         struct snd_card *card = chip->card;
1655         struct hdac_bus *bus = azx_bus(chip);
1656         int err;
1657         unsigned short gcap;
1658         unsigned int dma_bits = 64;
1659
1660 #if BITS_PER_LONG != 64
1661         /* Fix up base address on ULI M5461 */
1662         if (chip->driver_type == AZX_DRIVER_ULI) {
1663                 u16 tmp3;
1664                 pci_read_config_word(pci, 0x40, &tmp3);
1665                 pci_write_config_word(pci, 0x40, tmp3 | 0x10);
1666                 pci_write_config_dword(pci, PCI_BASE_ADDRESS_1, 0);
1667         }
1668 #endif
1669
1670         err = pci_request_regions(pci, "ICH HD audio");
1671         if (err < 0)
1672                 return err;
1673         chip->region_requested = 1;
1674
1675         bus->addr = pci_resource_start(pci, 0);
1676         bus->remap_addr = pci_ioremap_bar(pci, 0);
1677         if (bus->remap_addr == NULL) {
1678                 dev_err(card->dev, "ioremap error\n");
1679                 return -ENXIO;
1680         }
1681
1682         if (IS_SKL_PLUS(pci))
1683                 snd_hdac_bus_parse_capabilities(bus);
1684
1685         /*
1686          * Some Intel CPUs has always running timer (ART) feature and
1687          * controller may have Global time sync reporting capability, so
1688          * check both of these before declaring synchronized time reporting
1689          * capability SNDRV_PCM_INFO_HAS_LINK_SYNCHRONIZED_ATIME
1690          */
1691         chip->gts_present = false;
1692
1693 #ifdef CONFIG_X86
1694         if (bus->ppcap && boot_cpu_has(X86_FEATURE_ART))
1695                 chip->gts_present = true;
1696 #endif
1697
1698         if (chip->msi) {
1699                 if (chip->driver_caps & AZX_DCAPS_NO_MSI64) {
1700                         dev_dbg(card->dev, "Disabling 64bit MSI\n");
1701                         pci->no_64bit_msi = true;
1702                 }
1703                 if (pci_enable_msi(pci) < 0)
1704                         chip->msi = 0;
1705         }
1706
1707         pci_set_master(pci);
1708         synchronize_irq(bus->irq);
1709
1710         gcap = azx_readw(chip, GCAP);
1711         dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
1712
1713         /* AMD devices support 40 or 48bit DMA, take the safe one */
1714         if (chip->pci->vendor == PCI_VENDOR_ID_AMD)
1715                 dma_bits = 40;
1716
1717         /* disable SB600 64bit support for safety */
1718         if (chip->pci->vendor == PCI_VENDOR_ID_ATI) {
1719                 struct pci_dev *p_smbus;
1720                 dma_bits = 40;
1721                 p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
1722                                          PCI_DEVICE_ID_ATI_SBX00_SMBUS,
1723                                          NULL);
1724                 if (p_smbus) {
1725                         if (p_smbus->revision < 0x30)
1726                                 gcap &= ~AZX_GCAP_64OK;
1727                         pci_dev_put(p_smbus);
1728                 }
1729         }
1730
1731         /* NVidia hardware normally only supports up to 40 bits of DMA */
1732         if (chip->pci->vendor == PCI_VENDOR_ID_NVIDIA)
1733                 dma_bits = 40;
1734
1735         /* disable 64bit DMA address on some devices */
1736         if (chip->driver_caps & AZX_DCAPS_NO_64BIT) {
1737                 dev_dbg(card->dev, "Disabling 64bit DMA\n");
1738                 gcap &= ~AZX_GCAP_64OK;
1739         }
1740
1741         /* disable buffer size rounding to 128-byte multiples if supported */
1742         if (align_buffer_size >= 0)
1743                 chip->align_buffer_size = !!align_buffer_size;
1744         else {
1745                 if (chip->driver_caps & AZX_DCAPS_NO_ALIGN_BUFSIZE)
1746                         chip->align_buffer_size = 0;
1747                 else
1748                         chip->align_buffer_size = 1;
1749         }
1750
1751         /* allow 64bit DMA address if supported by H/W */
1752         if (!(gcap & AZX_GCAP_64OK))
1753                 dma_bits = 32;
1754         if (!dma_set_mask(&pci->dev, DMA_BIT_MASK(dma_bits))) {
1755                 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(dma_bits));
1756         } else {
1757                 dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
1758                 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(32));
1759         }
1760
1761         /* read number of streams from GCAP register instead of using
1762          * hardcoded value
1763          */
1764         chip->capture_streams = (gcap >> 8) & 0x0f;
1765         chip->playback_streams = (gcap >> 12) & 0x0f;
1766         if (!chip->playback_streams && !chip->capture_streams) {
1767                 /* gcap didn't give any info, switching to old method */
1768
1769                 switch (chip->driver_type) {
1770                 case AZX_DRIVER_ULI:
1771                         chip->playback_streams = ULI_NUM_PLAYBACK;
1772                         chip->capture_streams = ULI_NUM_CAPTURE;
1773                         break;
1774                 case AZX_DRIVER_ATIHDMI:
1775                 case AZX_DRIVER_ATIHDMI_NS:
1776                         chip->playback_streams = ATIHDMI_NUM_PLAYBACK;
1777                         chip->capture_streams = ATIHDMI_NUM_CAPTURE;
1778                         break;
1779                 case AZX_DRIVER_GENERIC:
1780                 default:
1781                         chip->playback_streams = ICH6_NUM_PLAYBACK;
1782                         chip->capture_streams = ICH6_NUM_CAPTURE;
1783                         break;
1784                 }
1785         }
1786         chip->capture_index_offset = 0;
1787         chip->playback_index_offset = chip->capture_streams;
1788         chip->num_streams = chip->playback_streams + chip->capture_streams;
1789
1790         /* initialize streams */
1791         err = azx_init_streams(chip);
1792         if (err < 0)
1793                 return err;
1794
1795         err = azx_alloc_stream_pages(chip);
1796         if (err < 0)
1797                 return err;
1798
1799         /* initialize chip */
1800         azx_init_pci(chip);
1801
1802         if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL)
1803                 snd_hdac_i915_set_bclk(bus);
1804
1805         hda_intel_init_chip(chip, (probe_only[dev] & 2) == 0);
1806
1807         /* codec detection */
1808         if (!azx_bus(chip)->codec_mask) {
1809                 dev_err(card->dev, "no codecs found!\n");
1810                 return -ENODEV;
1811         }
1812
1813         if (azx_acquire_irq(chip, 0) < 0)
1814                 return -EBUSY;
1815
1816         strcpy(card->driver, "HDA-Intel");
1817         strlcpy(card->shortname, driver_short_names[chip->driver_type],
1818                 sizeof(card->shortname));
1819         snprintf(card->longname, sizeof(card->longname),
1820                  "%s at 0x%lx irq %i",
1821                  card->shortname, bus->addr, bus->irq);
1822
1823         return 0;
1824 }
1825
1826 #ifdef CONFIG_SND_HDA_PATCH_LOADER
1827 /* callback from request_firmware_nowait() */
1828 static void azx_firmware_cb(const struct firmware *fw, void *context)
1829 {
1830         struct snd_card *card = context;
1831         struct azx *chip = card->private_data;
1832
1833         if (fw)
1834                 chip->fw = fw;
1835         else
1836                 dev_err(card->dev, "Cannot load firmware, continue without patching\n");
1837         if (!chip->disabled) {
1838                 /* continue probing */
1839                 azx_probe_continue(chip);
1840         }
1841 }
1842 #endif
1843
1844 /*
1845  * HDA controller ops.
1846  */
1847
1848 /* PCI register access. */
1849 static void pci_azx_writel(u32 value, u32 __iomem *addr)
1850 {
1851         writel(value, addr);
1852 }
1853
1854 static u32 pci_azx_readl(u32 __iomem *addr)
1855 {
1856         return readl(addr);
1857 }
1858
1859 static void pci_azx_writew(u16 value, u16 __iomem *addr)
1860 {
1861         writew(value, addr);
1862 }
1863
1864 static u16 pci_azx_readw(u16 __iomem *addr)
1865 {
1866         return readw(addr);
1867 }
1868
1869 static void pci_azx_writeb(u8 value, u8 __iomem *addr)
1870 {
1871         writeb(value, addr);
1872 }
1873
1874 static u8 pci_azx_readb(u8 __iomem *addr)
1875 {
1876         return readb(addr);
1877 }
1878
1879 static int disable_msi_reset_irq(struct azx *chip)
1880 {
1881         struct hdac_bus *bus = azx_bus(chip);
1882         int err;
1883
1884         free_irq(bus->irq, chip);
1885         bus->irq = -1;
1886         pci_disable_msi(chip->pci);
1887         chip->msi = 0;
1888         err = azx_acquire_irq(chip, 1);
1889         if (err < 0)
1890                 return err;
1891
1892         return 0;
1893 }
1894
1895 /* DMA page allocation helpers.  */
1896 static int dma_alloc_pages(struct hdac_bus *bus,
1897                            int type,
1898                            size_t size,
1899                            struct snd_dma_buffer *buf)
1900 {
1901         struct azx *chip = bus_to_azx(bus);
1902         int err;
1903
1904         err = snd_dma_alloc_pages(type,
1905                                   bus->dev,
1906                                   size, buf);
1907         if (err < 0)
1908                 return err;
1909         mark_pages_wc(chip, buf, true);
1910         return 0;
1911 }
1912
1913 static void dma_free_pages(struct hdac_bus *bus, struct snd_dma_buffer *buf)
1914 {
1915         struct azx *chip = bus_to_azx(bus);
1916
1917         mark_pages_wc(chip, buf, false);
1918         snd_dma_free_pages(buf);
1919 }
1920
1921 static int substream_alloc_pages(struct azx *chip,
1922                                  struct snd_pcm_substream *substream,
1923                                  size_t size)
1924 {
1925         struct azx_dev *azx_dev = get_azx_dev(substream);
1926         int ret;
1927
1928         mark_runtime_wc(chip, azx_dev, substream, false);
1929         ret = snd_pcm_lib_malloc_pages(substream, size);
1930         if (ret < 0)
1931                 return ret;
1932         mark_runtime_wc(chip, azx_dev, substream, true);
1933         return 0;
1934 }
1935
1936 static int substream_free_pages(struct azx *chip,
1937                                 struct snd_pcm_substream *substream)
1938 {
1939         struct azx_dev *azx_dev = get_azx_dev(substream);
1940         mark_runtime_wc(chip, azx_dev, substream, false);
1941         return snd_pcm_lib_free_pages(substream);
1942 }
1943
1944 static void pcm_mmap_prepare(struct snd_pcm_substream *substream,
1945                              struct vm_area_struct *area)
1946 {
1947 #ifdef CONFIG_X86
1948         struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
1949         struct azx *chip = apcm->chip;
1950         if (chip->uc_buffer)
1951                 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
1952 #endif
1953 }
1954
1955 static const struct hdac_io_ops pci_hda_io_ops = {
1956         .reg_writel = pci_azx_writel,
1957         .reg_readl = pci_azx_readl,
1958         .reg_writew = pci_azx_writew,
1959         .reg_readw = pci_azx_readw,
1960         .reg_writeb = pci_azx_writeb,
1961         .reg_readb = pci_azx_readb,
1962         .dma_alloc_pages = dma_alloc_pages,
1963         .dma_free_pages = dma_free_pages,
1964 };
1965
1966 /* Blacklist for skipping the whole probe:
1967  * some HD-audio PCI entries are exposed without any codecs, and such devices
1968  * should be ignored from the beginning.
1969  */
1970 static const struct pci_device_id driver_blacklist[] = {
1971         { PCI_DEVICE_SUB(0x1022, 0x1487, 0x1043, 0x874f) }, /* ASUS ROG Zenith II / Strix */
1972         { PCI_DEVICE_SUB(0x1022, 0x1487, 0x1462, 0xcb59) }, /* MSI TRX40 Creator */
1973         { PCI_DEVICE_SUB(0x1022, 0x1487, 0x1462, 0xcb60) }, /* MSI TRX40 */
1974         {}
1975 };
1976
1977 static const struct hda_controller_ops pci_hda_ops = {
1978         .disable_msi_reset_irq = disable_msi_reset_irq,
1979         .substream_alloc_pages = substream_alloc_pages,
1980         .substream_free_pages = substream_free_pages,
1981         .pcm_mmap_prepare = pcm_mmap_prepare,
1982         .position_check = azx_position_check,
1983         .link_power = azx_intel_link_power,
1984 };
1985
1986 static int azx_probe(struct pci_dev *pci,
1987                      const struct pci_device_id *pci_id)
1988 {
1989         static int dev;
1990         struct snd_card *card;
1991         struct hda_intel *hda;
1992         struct azx *chip;
1993         bool schedule_probe;
1994         int err;
1995
1996         if (pci_match_id(driver_blacklist, pci)) {
1997                 dev_info(&pci->dev, "Skipping the blacklisted device\n");
1998                 return -ENODEV;
1999         }
2000
2001         if (dev >= SNDRV_CARDS)
2002                 return -ENODEV;
2003         if (!enable[dev]) {
2004                 dev++;
2005                 return -ENOENT;
2006         }
2007
2008         err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2009                            0, &card);
2010         if (err < 0) {
2011                 dev_err(&pci->dev, "Error creating card!\n");
2012                 return err;
2013         }
2014
2015         err = azx_create(card, pci, dev, pci_id->driver_data, &chip);
2016         if (err < 0)
2017                 goto out_free;
2018         card->private_data = chip;
2019         hda = container_of(chip, struct hda_intel, chip);
2020
2021         pci_set_drvdata(pci, card);
2022
2023         err = register_vga_switcheroo(chip);
2024         if (err < 0) {
2025                 dev_err(card->dev, "Error registering vga_switcheroo client\n");
2026                 goto out_free;
2027         }
2028
2029         if (check_hdmi_disabled(pci)) {
2030                 dev_info(card->dev, "VGA controller is disabled\n");
2031                 dev_info(card->dev, "Delaying initialization\n");
2032                 chip->disabled = true;
2033         }
2034
2035         schedule_probe = !chip->disabled;
2036
2037 #ifdef CONFIG_SND_HDA_PATCH_LOADER
2038         if (patch[dev] && *patch[dev]) {
2039                 dev_info(card->dev, "Applying patch firmware '%s'\n",
2040                          patch[dev]);
2041                 err = request_firmware_nowait(THIS_MODULE, true, patch[dev],
2042                                               &pci->dev, GFP_KERNEL, card,
2043                                               azx_firmware_cb);
2044                 if (err < 0)
2045                         goto out_free;
2046                 schedule_probe = false; /* continued in azx_firmware_cb() */
2047         }
2048 #endif /* CONFIG_SND_HDA_PATCH_LOADER */
2049
2050 #ifndef CONFIG_SND_HDA_I915
2051         if (CONTROLLER_IN_GPU(pci))
2052                 dev_err(card->dev, "Haswell/Broadwell HDMI/DP must build in CONFIG_SND_HDA_I915\n");
2053 #endif
2054
2055         if (schedule_probe)
2056                 schedule_work(&hda->probe_work);
2057
2058         dev++;
2059         if (chip->disabled)
2060                 complete_all(&hda->probe_wait);
2061         return 0;
2062
2063 out_free:
2064         snd_card_free(card);
2065         return err;
2066 }
2067
2068 #ifdef CONFIG_PM
2069 /* On some boards setting power_save to a non 0 value leads to clicking /
2070  * popping sounds when ever we enter/leave powersaving mode. Ideally we would
2071  * figure out how to avoid these sounds, but that is not always feasible.
2072  * So we keep a list of devices where we disable powersaving as its known
2073  * to causes problems on these devices.
2074  */
2075 static struct snd_pci_quirk power_save_blacklist[] = {
2076         /* https://bugzilla.redhat.com/show_bug.cgi?id=1525104 */
2077         SND_PCI_QUIRK(0x1849, 0xc892, "Asrock B85M-ITX", 0),
2078         /* https://bugzilla.redhat.com/show_bug.cgi?id=1525104 */
2079         SND_PCI_QUIRK(0x1043, 0x8733, "Asus Prime X370-Pro", 0),
2080         /* https://bugzilla.redhat.com/show_bug.cgi?id=1572975 */
2081         SND_PCI_QUIRK(0x17aa, 0x36a7, "Lenovo C50 All in one", 0),
2082         /* https://bugzilla.kernel.org/show_bug.cgi?id=198611 */
2083         SND_PCI_QUIRK(0x17aa, 0x2227, "Lenovo X1 Carbon 3rd Gen", 0),
2084         {}
2085 };
2086 #endif /* CONFIG_PM */
2087
2088 /* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
2089 static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] = {
2090         [AZX_DRIVER_NVIDIA] = 8,
2091         [AZX_DRIVER_TERA] = 1,
2092 };
2093
2094 static int azx_probe_continue(struct azx *chip)
2095 {
2096         struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
2097         struct hdac_bus *bus = azx_bus(chip);
2098         struct pci_dev *pci = chip->pci;
2099         int dev = chip->dev_index;
2100         int val;
2101         int err;
2102
2103         to_hda_bus(bus)->bus_probing = 1;
2104         hda->probe_continued = 1;
2105
2106         /* Request display power well for the HDA controller or codec. For
2107          * Haswell/Broadwell, both the display HDA controller and codec need
2108          * this power. For other platforms, like Baytrail/Braswell, only the
2109          * display codec needs the power and it can be released after probe.
2110          */
2111         if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL) {
2112                 /* HSW/BDW controllers need this power */
2113                 if (CONTROLLER_IN_GPU(pci))
2114                         hda->need_i915_power = 1;
2115
2116                 err = snd_hdac_i915_init(bus);
2117                 if (err < 0) {
2118                         /* if the controller is bound only with HDMI/DP
2119                          * (for HSW and BDW), we need to abort the probe;
2120                          * for other chips, still continue probing as other
2121                          * codecs can be on the same link.
2122                          */
2123                         if (CONTROLLER_IN_GPU(pci)) {
2124                                 dev_err(chip->card->dev,
2125                                         "HSW/BDW HD-audio HDMI/DP requires binding with gfx driver\n");
2126                                 goto out_free;
2127                         } else
2128                                 goto skip_i915;
2129                 }
2130
2131                 err = snd_hdac_display_power(bus, true);
2132                 if (err < 0) {
2133                         dev_err(chip->card->dev,
2134                                 "Cannot turn on display power on i915\n");
2135                         goto i915_power_fail;
2136                 }
2137         }
2138
2139  skip_i915:
2140         err = azx_first_init(chip);
2141         if (err < 0)
2142                 goto out_free;
2143
2144 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2145         chip->beep_mode = beep_mode[dev];
2146 #endif
2147
2148         /* create codec instances */
2149         err = azx_probe_codecs(chip, azx_max_codecs[chip->driver_type]);
2150         if (err < 0)
2151                 goto out_free;
2152
2153 #ifdef CONFIG_SND_HDA_PATCH_LOADER
2154         if (chip->fw) {
2155                 err = snd_hda_load_patch(&chip->bus, chip->fw->size,
2156                                          chip->fw->data);
2157                 if (err < 0)
2158                         goto out_free;
2159 #ifndef CONFIG_PM
2160                 release_firmware(chip->fw); /* no longer needed */
2161                 chip->fw = NULL;
2162 #endif
2163         }
2164 #endif
2165         if ((probe_only[dev] & 1) == 0) {
2166                 err = azx_codec_configure(chip);
2167                 if (err < 0)
2168                         goto out_free;
2169         }
2170
2171         err = snd_card_register(chip->card);
2172         if (err < 0)
2173                 goto out_free;
2174
2175         chip->running = 1;
2176         azx_add_card_list(chip);
2177
2178         val = power_save;
2179 #ifdef CONFIG_PM
2180         if (pm_blacklist) {
2181                 const struct snd_pci_quirk *q;
2182
2183                 q = snd_pci_quirk_lookup(chip->pci, power_save_blacklist);
2184                 if (q && val) {
2185                         dev_info(chip->card->dev, "device %04x:%04x is on the power_save blacklist, forcing power_save to 0\n",
2186                                  q->subvendor, q->subdevice);
2187                         val = 0;
2188                 }
2189         }
2190 #endif /* CONFIG_PM */
2191         snd_hda_set_power_save(&chip->bus, val * 1000);
2192         if (azx_has_pm_runtime(chip) || hda->use_vga_switcheroo)
2193                 pm_runtime_put_autosuspend(&pci->dev);
2194
2195 out_free:
2196         if (chip->driver_caps & AZX_DCAPS_I915_POWERWELL
2197                 && !hda->need_i915_power)
2198                 snd_hdac_display_power(bus, false);
2199
2200 i915_power_fail:
2201         if (err < 0)
2202                 hda->init_failed = 1;
2203         complete_all(&hda->probe_wait);
2204         to_hda_bus(bus)->bus_probing = 0;
2205         return err;
2206 }
2207
2208 static void azx_remove(struct pci_dev *pci)
2209 {
2210         struct snd_card *card = pci_get_drvdata(pci);
2211         struct azx *chip;
2212         struct hda_intel *hda;
2213
2214         if (card) {
2215                 /* cancel the pending probing work */
2216                 chip = card->private_data;
2217                 hda = container_of(chip, struct hda_intel, chip);
2218                 /* FIXME: below is an ugly workaround.
2219                  * Both device_release_driver() and driver_probe_device()
2220                  * take *both* the device's and its parent's lock before
2221                  * calling the remove() and probe() callbacks.  The codec
2222                  * probe takes the locks of both the codec itself and its
2223                  * parent, i.e. the PCI controller dev.  Meanwhile, when
2224                  * the PCI controller is unbound, it takes its lock, too
2225                  * ==> ouch, a deadlock!
2226                  * As a workaround, we unlock temporarily here the controller
2227                  * device during cancel_work_sync() call.
2228                  */
2229                 device_unlock(&pci->dev);
2230                 cancel_work_sync(&hda->probe_work);
2231                 device_lock(&pci->dev);
2232
2233                 snd_card_free(card);
2234         }
2235 }
2236
2237 static void azx_shutdown(struct pci_dev *pci)
2238 {
2239         struct snd_card *card = pci_get_drvdata(pci);
2240         struct azx *chip;
2241
2242         if (!card)
2243                 return;
2244         chip = card->private_data;
2245         if (chip && chip->running)
2246                 azx_stop_chip(chip);
2247 }
2248
2249 /* PCI IDs */
2250 static const struct pci_device_id azx_ids[] = {
2251         /* CPT */
2252         { PCI_DEVICE(0x8086, 0x1c20),
2253           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
2254         /* PBG */
2255         { PCI_DEVICE(0x8086, 0x1d20),
2256           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
2257         /* Panther Point */
2258         { PCI_DEVICE(0x8086, 0x1e20),
2259           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH_NOPM },
2260         /* Lynx Point */
2261         { PCI_DEVICE(0x8086, 0x8c20),
2262           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
2263         /* 9 Series */
2264         { PCI_DEVICE(0x8086, 0x8ca0),
2265           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
2266         /* Wellsburg */
2267         { PCI_DEVICE(0x8086, 0x8d20),
2268           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
2269         { PCI_DEVICE(0x8086, 0x8d21),
2270           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
2271         /* Lewisburg */
2272         { PCI_DEVICE(0x8086, 0xa1f0),
2273           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_SKYLAKE },
2274         { PCI_DEVICE(0x8086, 0xa270),
2275           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_SKYLAKE },
2276         /* Lynx Point-LP */
2277         { PCI_DEVICE(0x8086, 0x9c20),
2278           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
2279         /* Lynx Point-LP */
2280         { PCI_DEVICE(0x8086, 0x9c21),
2281           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
2282         /* Wildcat Point-LP */
2283         { PCI_DEVICE(0x8086, 0x9ca0),
2284           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_PCH },
2285         /* Sunrise Point */
2286         { PCI_DEVICE(0x8086, 0xa170),
2287           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_SKYLAKE },
2288         /* Sunrise Point-LP */
2289         { PCI_DEVICE(0x8086, 0x9d70),
2290           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_SKYLAKE },
2291         /* Kabylake */
2292         { PCI_DEVICE(0x8086, 0xa171),
2293           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_SKYLAKE },
2294         /* Kabylake-LP */
2295         { PCI_DEVICE(0x8086, 0x9d71),
2296           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_SKYLAKE },
2297         /* Kabylake-H */
2298         { PCI_DEVICE(0x8086, 0xa2f0),
2299           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_SKYLAKE },
2300         /* Broxton-P(Apollolake) */
2301         { PCI_DEVICE(0x8086, 0x5a98),
2302           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_BROXTON },
2303         /* Broxton-T */
2304         { PCI_DEVICE(0x8086, 0x1a98),
2305           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_BROXTON },
2306         /* Haswell */
2307         { PCI_DEVICE(0x8086, 0x0a0c),
2308           .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
2309         { PCI_DEVICE(0x8086, 0x0c0c),
2310           .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
2311         { PCI_DEVICE(0x8086, 0x0d0c),
2312           .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_HASWELL },
2313         /* Broadwell */
2314         { PCI_DEVICE(0x8086, 0x160c),
2315           .driver_data = AZX_DRIVER_HDMI | AZX_DCAPS_INTEL_BROADWELL },
2316         /* 5 Series/3400 */
2317         { PCI_DEVICE(0x8086, 0x3b56),
2318           .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_NOPM },
2319         /* Poulsbo */
2320         { PCI_DEVICE(0x8086, 0x811b),
2321           .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_BASE },
2322         /* Oaktrail */
2323         { PCI_DEVICE(0x8086, 0x080a),
2324           .driver_data = AZX_DRIVER_SCH | AZX_DCAPS_INTEL_PCH_BASE },
2325         /* BayTrail */
2326         { PCI_DEVICE(0x8086, 0x0f04),
2327           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_BAYTRAIL },
2328         /* Braswell */
2329         { PCI_DEVICE(0x8086, 0x2284),
2330           .driver_data = AZX_DRIVER_PCH | AZX_DCAPS_INTEL_BRASWELL },
2331         /* ICH6 */
2332         { PCI_DEVICE(0x8086, 0x2668),
2333           .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_INTEL_ICH },
2334         /* ICH7 */
2335         { PCI_DEVICE(0x8086, 0x27d8),
2336           .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_INTEL_ICH },
2337         /* ESB2 */
2338         { PCI_DEVICE(0x8086, 0x269a),
2339           .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_INTEL_ICH },
2340         /* ICH8 */
2341         { PCI_DEVICE(0x8086, 0x284b),
2342           .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_INTEL_ICH },
2343         /* ICH9 */
2344         { PCI_DEVICE(0x8086, 0x293e),
2345           .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_INTEL_ICH },
2346         /* ICH9 */
2347         { PCI_DEVICE(0x8086, 0x293f),
2348           .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_INTEL_ICH },
2349         /* ICH10 */
2350         { PCI_DEVICE(0x8086, 0x3a3e),
2351           .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_INTEL_ICH },
2352         /* ICH10 */
2353         { PCI_DEVICE(0x8086, 0x3a6e),
2354           .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_INTEL_ICH },
2355         /* Generic Intel */
2356         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
2357           .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
2358           .class_mask = 0xffffff,
2359           .driver_data = AZX_DRIVER_ICH | AZX_DCAPS_NO_ALIGN_BUFSIZE },
2360         /* ATI SB 450/600/700/800/900 */
2361         { PCI_DEVICE(0x1002, 0x437b),
2362           .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
2363         { PCI_DEVICE(0x1002, 0x4383),
2364           .driver_data = AZX_DRIVER_ATI | AZX_DCAPS_PRESET_ATI_SB },
2365         /* AMD Hudson */
2366         { PCI_DEVICE(0x1022, 0x780d),
2367           .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_SB },
2368         /* AMD Stoney */
2369         { PCI_DEVICE(0x1022, 0x157a),
2370           .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_SB |
2371                          AZX_DCAPS_PM_RUNTIME },
2372         /* AMD Raven */
2373         { PCI_DEVICE(0x1022, 0x15e3),
2374           .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_SB |
2375                          AZX_DCAPS_PM_RUNTIME },
2376         /* ATI HDMI */
2377         { PCI_DEVICE(0x1002, 0x0002),
2378           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2379         { PCI_DEVICE(0x1002, 0x1308),
2380           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2381         { PCI_DEVICE(0x1002, 0x157a),
2382           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2383         { PCI_DEVICE(0x1002, 0x15b3),
2384           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2385         { PCI_DEVICE(0x1002, 0x793b),
2386           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2387         { PCI_DEVICE(0x1002, 0x7919),
2388           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2389         { PCI_DEVICE(0x1002, 0x960f),
2390           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2391         { PCI_DEVICE(0x1002, 0x970f),
2392           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2393         { PCI_DEVICE(0x1002, 0x9840),
2394           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2395         { PCI_DEVICE(0x1002, 0xaa00),
2396           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2397         { PCI_DEVICE(0x1002, 0xaa08),
2398           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2399         { PCI_DEVICE(0x1002, 0xaa10),
2400           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2401         { PCI_DEVICE(0x1002, 0xaa18),
2402           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2403         { PCI_DEVICE(0x1002, 0xaa20),
2404           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2405         { PCI_DEVICE(0x1002, 0xaa28),
2406           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2407         { PCI_DEVICE(0x1002, 0xaa30),
2408           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2409         { PCI_DEVICE(0x1002, 0xaa38),
2410           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2411         { PCI_DEVICE(0x1002, 0xaa40),
2412           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2413         { PCI_DEVICE(0x1002, 0xaa48),
2414           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2415         { PCI_DEVICE(0x1002, 0xaa50),
2416           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2417         { PCI_DEVICE(0x1002, 0xaa58),
2418           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2419         { PCI_DEVICE(0x1002, 0xaa60),
2420           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2421         { PCI_DEVICE(0x1002, 0xaa68),
2422           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2423         { PCI_DEVICE(0x1002, 0xaa80),
2424           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2425         { PCI_DEVICE(0x1002, 0xaa88),
2426           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2427         { PCI_DEVICE(0x1002, 0xaa90),
2428           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2429         { PCI_DEVICE(0x1002, 0xaa98),
2430           .driver_data = AZX_DRIVER_ATIHDMI | AZX_DCAPS_PRESET_ATI_HDMI },
2431         { PCI_DEVICE(0x1002, 0x9902),
2432           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2433         { PCI_DEVICE(0x1002, 0xaaa0),
2434           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2435         { PCI_DEVICE(0x1002, 0xaaa8),
2436           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2437         { PCI_DEVICE(0x1002, 0xaab0),
2438           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2439         { PCI_DEVICE(0x1002, 0xaac0),
2440           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2441         { PCI_DEVICE(0x1002, 0xaac8),
2442           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2443         { PCI_DEVICE(0x1002, 0xaad8),
2444           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2445         { PCI_DEVICE(0x1002, 0xaae8),
2446           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2447         { PCI_DEVICE(0x1002, 0xaae0),
2448           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2449         { PCI_DEVICE(0x1002, 0xaaf0),
2450           .driver_data = AZX_DRIVER_ATIHDMI_NS | AZX_DCAPS_PRESET_ATI_HDMI_NS },
2451         /* VIA VT8251/VT8237A */
2452         { PCI_DEVICE(0x1106, 0x3288), .driver_data = AZX_DRIVER_VIA },
2453         /* VIA GFX VT7122/VX900 */
2454         { PCI_DEVICE(0x1106, 0x9170), .driver_data = AZX_DRIVER_GENERIC },
2455         /* VIA GFX VT6122/VX11 */
2456         { PCI_DEVICE(0x1106, 0x9140), .driver_data = AZX_DRIVER_GENERIC },
2457         /* SIS966 */
2458         { PCI_DEVICE(0x1039, 0x7502), .driver_data = AZX_DRIVER_SIS },
2459         /* ULI M5461 */
2460         { PCI_DEVICE(0x10b9, 0x5461), .driver_data = AZX_DRIVER_ULI },
2461         /* NVIDIA MCP */
2462         { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
2463           .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
2464           .class_mask = 0xffffff,
2465           .driver_data = AZX_DRIVER_NVIDIA | AZX_DCAPS_PRESET_NVIDIA },
2466         /* Teradici */
2467         { PCI_DEVICE(0x6549, 0x1200),
2468           .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
2469         { PCI_DEVICE(0x6549, 0x2200),
2470           .driver_data = AZX_DRIVER_TERA | AZX_DCAPS_NO_64BIT },
2471         /* Creative X-Fi (CA0110-IBG) */
2472         /* CTHDA chips */
2473         { PCI_DEVICE(0x1102, 0x0010),
2474           .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
2475         { PCI_DEVICE(0x1102, 0x0012),
2476           .driver_data = AZX_DRIVER_CTHDA | AZX_DCAPS_PRESET_CTHDA },
2477 #if !IS_ENABLED(CONFIG_SND_CTXFI)
2478         /* the following entry conflicts with snd-ctxfi driver,
2479          * as ctxfi driver mutates from HD-audio to native mode with
2480          * a special command sequence.
2481          */
2482         { PCI_DEVICE(PCI_VENDOR_ID_CREATIVE, PCI_ANY_ID),
2483           .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
2484           .class_mask = 0xffffff,
2485           .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
2486           AZX_DCAPS_NO_64BIT | AZX_DCAPS_POSFIX_LPIB },
2487 #else
2488         /* this entry seems still valid -- i.e. without emu20kx chip */
2489         { PCI_DEVICE(0x1102, 0x0009),
2490           .driver_data = AZX_DRIVER_CTX | AZX_DCAPS_CTX_WORKAROUND |
2491           AZX_DCAPS_NO_64BIT | AZX_DCAPS_POSFIX_LPIB },
2492 #endif
2493         /* CM8888 */
2494         { PCI_DEVICE(0x13f6, 0x5011),
2495           .driver_data = AZX_DRIVER_CMEDIA |
2496           AZX_DCAPS_NO_MSI | AZX_DCAPS_POSFIX_LPIB | AZX_DCAPS_SNOOP_OFF },
2497         /* Vortex86MX */
2498         { PCI_DEVICE(0x17f3, 0x3010), .driver_data = AZX_DRIVER_GENERIC },
2499         /* VMware HDAudio */
2500         { PCI_DEVICE(0x15ad, 0x1977), .driver_data = AZX_DRIVER_GENERIC },
2501         /* AMD/ATI Generic, PCI class code and Vendor ID for HD Audio */
2502         { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_ANY_ID),
2503           .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
2504           .class_mask = 0xffffff,
2505           .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
2506         { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_ANY_ID),
2507           .class = PCI_CLASS_MULTIMEDIA_HD_AUDIO << 8,
2508           .class_mask = 0xffffff,
2509           .driver_data = AZX_DRIVER_GENERIC | AZX_DCAPS_PRESET_ATI_HDMI },
2510         { 0, }
2511 };
2512 MODULE_DEVICE_TABLE(pci, azx_ids);
2513
2514 /* pci_driver definition */
2515 static struct pci_driver azx_driver = {
2516         .name = KBUILD_MODNAME,
2517         .id_table = azx_ids,
2518         .probe = azx_probe,
2519         .remove = azx_remove,
2520         .shutdown = azx_shutdown,
2521         .driver = {
2522                 .pm = AZX_PM_OPS,
2523         },
2524 };
2525
2526 module_pci_driver(azx_driver);