hda_intel.c 70 KB

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