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