hda_intel.c 70 KB

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