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