intel_cdclk.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  1. /*
  2. * Copyright © 2006-2017 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "intel_drv.h"
  24. /**
  25. * DOC: CDCLK / RAWCLK
  26. *
  27. * The display engine uses several different clocks to do its work. There
  28. * are two main clocks involved that aren't directly related to the actual
  29. * pixel clock or any symbol/bit clock of the actual output port. These
  30. * are the core display clock (CDCLK) and RAWCLK.
  31. *
  32. * CDCLK clocks most of the display pipe logic, and thus its frequency
  33. * must be high enough to support the rate at which pixels are flowing
  34. * through the pipes. Downscaling must also be accounted as that increases
  35. * the effective pixel rate.
  36. *
  37. * On several platforms the CDCLK frequency can be changed dynamically
  38. * to minimize power consumption for a given display configuration.
  39. * Typically changes to the CDCLK frequency require all the display pipes
  40. * to be shut down while the frequency is being changed.
  41. *
  42. * On SKL+ the DMC will toggle the CDCLK off/on during DC5/6 entry/exit.
  43. * DMC will not change the active CDCLK frequency however, so that part
  44. * will still be performed by the driver directly.
  45. *
  46. * RAWCLK is a fixed frequency clock, often used by various auxiliary
  47. * blocks such as AUX CH or backlight PWM. Hence the only thing we
  48. * really need to know about RAWCLK is its frequency so that various
  49. * dividers can be programmed correctly.
  50. */
  51. static void fixed_133mhz_get_cdclk(struct drm_i915_private *dev_priv,
  52. struct intel_cdclk_state *cdclk_state)
  53. {
  54. cdclk_state->cdclk = 133333;
  55. }
  56. static void fixed_200mhz_get_cdclk(struct drm_i915_private *dev_priv,
  57. struct intel_cdclk_state *cdclk_state)
  58. {
  59. cdclk_state->cdclk = 200000;
  60. }
  61. static void fixed_266mhz_get_cdclk(struct drm_i915_private *dev_priv,
  62. struct intel_cdclk_state *cdclk_state)
  63. {
  64. cdclk_state->cdclk = 266667;
  65. }
  66. static void fixed_333mhz_get_cdclk(struct drm_i915_private *dev_priv,
  67. struct intel_cdclk_state *cdclk_state)
  68. {
  69. cdclk_state->cdclk = 333333;
  70. }
  71. static void fixed_400mhz_get_cdclk(struct drm_i915_private *dev_priv,
  72. struct intel_cdclk_state *cdclk_state)
  73. {
  74. cdclk_state->cdclk = 400000;
  75. }
  76. static void fixed_450mhz_get_cdclk(struct drm_i915_private *dev_priv,
  77. struct intel_cdclk_state *cdclk_state)
  78. {
  79. cdclk_state->cdclk = 450000;
  80. }
  81. static void i85x_get_cdclk(struct drm_i915_private *dev_priv,
  82. struct intel_cdclk_state *cdclk_state)
  83. {
  84. struct pci_dev *pdev = dev_priv->drm.pdev;
  85. u16 hpllcc = 0;
  86. /*
  87. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  88. * encoding is different :(
  89. * FIXME is this the right way to detect 852GM/852GMV?
  90. */
  91. if (pdev->revision == 0x1) {
  92. cdclk_state->cdclk = 133333;
  93. return;
  94. }
  95. pci_bus_read_config_word(pdev->bus,
  96. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  97. /* Assume that the hardware is in the high speed state. This
  98. * should be the default.
  99. */
  100. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  101. case GC_CLOCK_133_200:
  102. case GC_CLOCK_133_200_2:
  103. case GC_CLOCK_100_200:
  104. cdclk_state->cdclk = 200000;
  105. break;
  106. case GC_CLOCK_166_250:
  107. cdclk_state->cdclk = 250000;
  108. break;
  109. case GC_CLOCK_100_133:
  110. cdclk_state->cdclk = 133333;
  111. break;
  112. case GC_CLOCK_133_266:
  113. case GC_CLOCK_133_266_2:
  114. case GC_CLOCK_166_266:
  115. cdclk_state->cdclk = 266667;
  116. break;
  117. }
  118. }
  119. static void i915gm_get_cdclk(struct drm_i915_private *dev_priv,
  120. struct intel_cdclk_state *cdclk_state)
  121. {
  122. struct pci_dev *pdev = dev_priv->drm.pdev;
  123. u16 gcfgc = 0;
  124. pci_read_config_word(pdev, GCFGC, &gcfgc);
  125. if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
  126. cdclk_state->cdclk = 133333;
  127. return;
  128. }
  129. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  130. case GC_DISPLAY_CLOCK_333_320_MHZ:
  131. cdclk_state->cdclk = 333333;
  132. break;
  133. default:
  134. case GC_DISPLAY_CLOCK_190_200_MHZ:
  135. cdclk_state->cdclk = 190000;
  136. break;
  137. }
  138. }
  139. static void i945gm_get_cdclk(struct drm_i915_private *dev_priv,
  140. struct intel_cdclk_state *cdclk_state)
  141. {
  142. struct pci_dev *pdev = dev_priv->drm.pdev;
  143. u16 gcfgc = 0;
  144. pci_read_config_word(pdev, GCFGC, &gcfgc);
  145. if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
  146. cdclk_state->cdclk = 133333;
  147. return;
  148. }
  149. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  150. case GC_DISPLAY_CLOCK_333_320_MHZ:
  151. cdclk_state->cdclk = 320000;
  152. break;
  153. default:
  154. case GC_DISPLAY_CLOCK_190_200_MHZ:
  155. cdclk_state->cdclk = 200000;
  156. break;
  157. }
  158. }
  159. static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
  160. {
  161. static const unsigned int blb_vco[8] = {
  162. [0] = 3200000,
  163. [1] = 4000000,
  164. [2] = 5333333,
  165. [3] = 4800000,
  166. [4] = 6400000,
  167. };
  168. static const unsigned int pnv_vco[8] = {
  169. [0] = 3200000,
  170. [1] = 4000000,
  171. [2] = 5333333,
  172. [3] = 4800000,
  173. [4] = 2666667,
  174. };
  175. static const unsigned int cl_vco[8] = {
  176. [0] = 3200000,
  177. [1] = 4000000,
  178. [2] = 5333333,
  179. [3] = 6400000,
  180. [4] = 3333333,
  181. [5] = 3566667,
  182. [6] = 4266667,
  183. };
  184. static const unsigned int elk_vco[8] = {
  185. [0] = 3200000,
  186. [1] = 4000000,
  187. [2] = 5333333,
  188. [3] = 4800000,
  189. };
  190. static const unsigned int ctg_vco[8] = {
  191. [0] = 3200000,
  192. [1] = 4000000,
  193. [2] = 5333333,
  194. [3] = 6400000,
  195. [4] = 2666667,
  196. [5] = 4266667,
  197. };
  198. const unsigned int *vco_table;
  199. unsigned int vco;
  200. uint8_t tmp = 0;
  201. /* FIXME other chipsets? */
  202. if (IS_GM45(dev_priv))
  203. vco_table = ctg_vco;
  204. else if (IS_G45(dev_priv))
  205. vco_table = elk_vco;
  206. else if (IS_I965GM(dev_priv))
  207. vco_table = cl_vco;
  208. else if (IS_PINEVIEW(dev_priv))
  209. vco_table = pnv_vco;
  210. else if (IS_G33(dev_priv))
  211. vco_table = blb_vco;
  212. else
  213. return 0;
  214. tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
  215. vco = vco_table[tmp & 0x7];
  216. if (vco == 0)
  217. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  218. else
  219. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  220. return vco;
  221. }
  222. static void g33_get_cdclk(struct drm_i915_private *dev_priv,
  223. struct intel_cdclk_state *cdclk_state)
  224. {
  225. struct pci_dev *pdev = dev_priv->drm.pdev;
  226. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  227. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  228. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  229. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  230. const uint8_t *div_table;
  231. unsigned int cdclk_sel;
  232. uint16_t tmp = 0;
  233. cdclk_state->vco = intel_hpll_vco(dev_priv);
  234. pci_read_config_word(pdev, GCFGC, &tmp);
  235. cdclk_sel = (tmp >> 4) & 0x7;
  236. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  237. goto fail;
  238. switch (cdclk_state->vco) {
  239. case 3200000:
  240. div_table = div_3200;
  241. break;
  242. case 4000000:
  243. div_table = div_4000;
  244. break;
  245. case 4800000:
  246. div_table = div_4800;
  247. break;
  248. case 5333333:
  249. div_table = div_5333;
  250. break;
  251. default:
  252. goto fail;
  253. }
  254. cdclk_state->cdclk = DIV_ROUND_CLOSEST(cdclk_state->vco,
  255. div_table[cdclk_sel]);
  256. return;
  257. fail:
  258. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n",
  259. cdclk_state->vco, tmp);
  260. cdclk_state->cdclk = 190476;
  261. }
  262. static void pnv_get_cdclk(struct drm_i915_private *dev_priv,
  263. struct intel_cdclk_state *cdclk_state)
  264. {
  265. struct pci_dev *pdev = dev_priv->drm.pdev;
  266. u16 gcfgc = 0;
  267. pci_read_config_word(pdev, GCFGC, &gcfgc);
  268. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  269. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  270. cdclk_state->cdclk = 266667;
  271. break;
  272. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  273. cdclk_state->cdclk = 333333;
  274. break;
  275. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  276. cdclk_state->cdclk = 444444;
  277. break;
  278. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  279. cdclk_state->cdclk = 200000;
  280. break;
  281. default:
  282. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  283. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  284. cdclk_state->cdclk = 133333;
  285. break;
  286. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  287. cdclk_state->cdclk = 166667;
  288. break;
  289. }
  290. }
  291. static void i965gm_get_cdclk(struct drm_i915_private *dev_priv,
  292. struct intel_cdclk_state *cdclk_state)
  293. {
  294. struct pci_dev *pdev = dev_priv->drm.pdev;
  295. static const uint8_t div_3200[] = { 16, 10, 8 };
  296. static const uint8_t div_4000[] = { 20, 12, 10 };
  297. static const uint8_t div_5333[] = { 24, 16, 14 };
  298. const uint8_t *div_table;
  299. unsigned int cdclk_sel;
  300. uint16_t tmp = 0;
  301. cdclk_state->vco = intel_hpll_vco(dev_priv);
  302. pci_read_config_word(pdev, GCFGC, &tmp);
  303. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  304. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  305. goto fail;
  306. switch (cdclk_state->vco) {
  307. case 3200000:
  308. div_table = div_3200;
  309. break;
  310. case 4000000:
  311. div_table = div_4000;
  312. break;
  313. case 5333333:
  314. div_table = div_5333;
  315. break;
  316. default:
  317. goto fail;
  318. }
  319. cdclk_state->cdclk = DIV_ROUND_CLOSEST(cdclk_state->vco,
  320. div_table[cdclk_sel]);
  321. return;
  322. fail:
  323. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n",
  324. cdclk_state->vco, tmp);
  325. cdclk_state->cdclk = 200000;
  326. }
  327. static void gm45_get_cdclk(struct drm_i915_private *dev_priv,
  328. struct intel_cdclk_state *cdclk_state)
  329. {
  330. struct pci_dev *pdev = dev_priv->drm.pdev;
  331. unsigned int cdclk_sel;
  332. uint16_t tmp = 0;
  333. cdclk_state->vco = intel_hpll_vco(dev_priv);
  334. pci_read_config_word(pdev, GCFGC, &tmp);
  335. cdclk_sel = (tmp >> 12) & 0x1;
  336. switch (cdclk_state->vco) {
  337. case 2666667:
  338. case 4000000:
  339. case 5333333:
  340. cdclk_state->cdclk = cdclk_sel ? 333333 : 222222;
  341. break;
  342. case 3200000:
  343. cdclk_state->cdclk = cdclk_sel ? 320000 : 228571;
  344. break;
  345. default:
  346. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n",
  347. cdclk_state->vco, tmp);
  348. cdclk_state->cdclk = 222222;
  349. break;
  350. }
  351. }
  352. static void hsw_get_cdclk(struct drm_i915_private *dev_priv,
  353. struct intel_cdclk_state *cdclk_state)
  354. {
  355. uint32_t lcpll = I915_READ(LCPLL_CTL);
  356. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  357. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  358. cdclk_state->cdclk = 800000;
  359. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  360. cdclk_state->cdclk = 450000;
  361. else if (freq == LCPLL_CLK_FREQ_450)
  362. cdclk_state->cdclk = 450000;
  363. else if (IS_HSW_ULT(dev_priv))
  364. cdclk_state->cdclk = 337500;
  365. else
  366. cdclk_state->cdclk = 540000;
  367. }
  368. static int vlv_calc_cdclk(struct drm_i915_private *dev_priv, int min_cdclk)
  369. {
  370. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ?
  371. 333333 : 320000;
  372. /*
  373. * We seem to get an unstable or solid color picture at 200MHz.
  374. * Not sure what's wrong. For now use 200MHz only when all pipes
  375. * are off.
  376. */
  377. if (IS_VALLEYVIEW(dev_priv) && min_cdclk > freq_320)
  378. return 400000;
  379. else if (min_cdclk > 266667)
  380. return freq_320;
  381. else if (min_cdclk > 0)
  382. return 266667;
  383. else
  384. return 200000;
  385. }
  386. static u8 vlv_calc_voltage_level(struct drm_i915_private *dev_priv, int cdclk)
  387. {
  388. if (IS_VALLEYVIEW(dev_priv)) {
  389. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  390. return 2;
  391. else if (cdclk >= 266667)
  392. return 1;
  393. else
  394. return 0;
  395. } else {
  396. /*
  397. * Specs are full of misinformation, but testing on actual
  398. * hardware has shown that we just need to write the desired
  399. * CCK divider into the Punit register.
  400. */
  401. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  402. }
  403. }
  404. static void vlv_get_cdclk(struct drm_i915_private *dev_priv,
  405. struct intel_cdclk_state *cdclk_state)
  406. {
  407. u32 val;
  408. cdclk_state->vco = vlv_get_hpll_vco(dev_priv);
  409. cdclk_state->cdclk = vlv_get_cck_clock(dev_priv, "cdclk",
  410. CCK_DISPLAY_CLOCK_CONTROL,
  411. cdclk_state->vco);
  412. mutex_lock(&dev_priv->pcu_lock);
  413. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  414. mutex_unlock(&dev_priv->pcu_lock);
  415. if (IS_VALLEYVIEW(dev_priv))
  416. cdclk_state->voltage_level = (val & DSPFREQGUAR_MASK) >>
  417. DSPFREQGUAR_SHIFT;
  418. else
  419. cdclk_state->voltage_level = (val & DSPFREQGUAR_MASK_CHV) >>
  420. DSPFREQGUAR_SHIFT_CHV;
  421. }
  422. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  423. {
  424. unsigned int credits, default_credits;
  425. if (IS_CHERRYVIEW(dev_priv))
  426. default_credits = PFI_CREDIT(12);
  427. else
  428. default_credits = PFI_CREDIT(8);
  429. if (dev_priv->cdclk.hw.cdclk >= dev_priv->czclk_freq) {
  430. /* CHV suggested value is 31 or 63 */
  431. if (IS_CHERRYVIEW(dev_priv))
  432. credits = PFI_CREDIT_63;
  433. else
  434. credits = PFI_CREDIT(15);
  435. } else {
  436. credits = default_credits;
  437. }
  438. /*
  439. * WA - write default credits before re-programming
  440. * FIXME: should we also set the resend bit here?
  441. */
  442. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  443. default_credits);
  444. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  445. credits | PFI_CREDIT_RESEND);
  446. /*
  447. * FIXME is this guaranteed to clear
  448. * immediately or should we poll for it?
  449. */
  450. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  451. }
  452. static void vlv_set_cdclk(struct drm_i915_private *dev_priv,
  453. const struct intel_cdclk_state *cdclk_state)
  454. {
  455. int cdclk = cdclk_state->cdclk;
  456. u32 val, cmd = cdclk_state->voltage_level;
  457. switch (cdclk) {
  458. case 400000:
  459. case 333333:
  460. case 320000:
  461. case 266667:
  462. case 200000:
  463. break;
  464. default:
  465. MISSING_CASE(cdclk);
  466. return;
  467. }
  468. /* There are cases where we can end up here with power domains
  469. * off and a CDCLK frequency other than the minimum, like when
  470. * issuing a modeset without actually changing any display after
  471. * a system suspend. So grab the PIPE-A domain, which covers
  472. * the HW blocks needed for the following programming.
  473. */
  474. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  475. mutex_lock(&dev_priv->pcu_lock);
  476. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  477. val &= ~DSPFREQGUAR_MASK;
  478. val |= (cmd << DSPFREQGUAR_SHIFT);
  479. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  480. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  481. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  482. 50)) {
  483. DRM_ERROR("timed out waiting for CDclk change\n");
  484. }
  485. mutex_unlock(&dev_priv->pcu_lock);
  486. mutex_lock(&dev_priv->sb_lock);
  487. if (cdclk == 400000) {
  488. u32 divider;
  489. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1,
  490. cdclk) - 1;
  491. /* adjust cdclk divider */
  492. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  493. val &= ~CCK_FREQUENCY_VALUES;
  494. val |= divider;
  495. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  496. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  497. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  498. 50))
  499. DRM_ERROR("timed out waiting for CDclk change\n");
  500. }
  501. /* adjust self-refresh exit latency value */
  502. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  503. val &= ~0x7f;
  504. /*
  505. * For high bandwidth configs, we set a higher latency in the bunit
  506. * so that the core display fetch happens in time to avoid underruns.
  507. */
  508. if (cdclk == 400000)
  509. val |= 4500 / 250; /* 4.5 usec */
  510. else
  511. val |= 3000 / 250; /* 3.0 usec */
  512. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  513. mutex_unlock(&dev_priv->sb_lock);
  514. intel_update_cdclk(dev_priv);
  515. vlv_program_pfi_credits(dev_priv);
  516. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  517. }
  518. static void chv_set_cdclk(struct drm_i915_private *dev_priv,
  519. const struct intel_cdclk_state *cdclk_state)
  520. {
  521. int cdclk = cdclk_state->cdclk;
  522. u32 val, cmd = cdclk_state->voltage_level;
  523. switch (cdclk) {
  524. case 333333:
  525. case 320000:
  526. case 266667:
  527. case 200000:
  528. break;
  529. default:
  530. MISSING_CASE(cdclk);
  531. return;
  532. }
  533. /* There are cases where we can end up here with power domains
  534. * off and a CDCLK frequency other than the minimum, like when
  535. * issuing a modeset without actually changing any display after
  536. * a system suspend. So grab the PIPE-A domain, which covers
  537. * the HW blocks needed for the following programming.
  538. */
  539. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  540. mutex_lock(&dev_priv->pcu_lock);
  541. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  542. val &= ~DSPFREQGUAR_MASK_CHV;
  543. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  544. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  545. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  546. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  547. 50)) {
  548. DRM_ERROR("timed out waiting for CDclk change\n");
  549. }
  550. mutex_unlock(&dev_priv->pcu_lock);
  551. intel_update_cdclk(dev_priv);
  552. vlv_program_pfi_credits(dev_priv);
  553. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  554. }
  555. static int bdw_calc_cdclk(int min_cdclk)
  556. {
  557. if (min_cdclk > 540000)
  558. return 675000;
  559. else if (min_cdclk > 450000)
  560. return 540000;
  561. else if (min_cdclk > 337500)
  562. return 450000;
  563. else
  564. return 337500;
  565. }
  566. static u8 bdw_calc_voltage_level(int cdclk)
  567. {
  568. switch (cdclk) {
  569. default:
  570. case 337500:
  571. return 2;
  572. case 450000:
  573. return 0;
  574. case 540000:
  575. return 1;
  576. case 675000:
  577. return 3;
  578. }
  579. }
  580. static void bdw_get_cdclk(struct drm_i915_private *dev_priv,
  581. struct intel_cdclk_state *cdclk_state)
  582. {
  583. uint32_t lcpll = I915_READ(LCPLL_CTL);
  584. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  585. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  586. cdclk_state->cdclk = 800000;
  587. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  588. cdclk_state->cdclk = 450000;
  589. else if (freq == LCPLL_CLK_FREQ_450)
  590. cdclk_state->cdclk = 450000;
  591. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  592. cdclk_state->cdclk = 540000;
  593. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  594. cdclk_state->cdclk = 337500;
  595. else
  596. cdclk_state->cdclk = 675000;
  597. /*
  598. * Can't read this out :( Let's assume it's
  599. * at least what the CDCLK frequency requires.
  600. */
  601. cdclk_state->voltage_level =
  602. bdw_calc_voltage_level(cdclk_state->cdclk);
  603. }
  604. static void bdw_set_cdclk(struct drm_i915_private *dev_priv,
  605. const struct intel_cdclk_state *cdclk_state)
  606. {
  607. int cdclk = cdclk_state->cdclk;
  608. uint32_t val;
  609. int ret;
  610. if (WARN((I915_READ(LCPLL_CTL) &
  611. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  612. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  613. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  614. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  615. "trying to change cdclk frequency with cdclk not enabled\n"))
  616. return;
  617. mutex_lock(&dev_priv->pcu_lock);
  618. ret = sandybridge_pcode_write(dev_priv,
  619. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  620. mutex_unlock(&dev_priv->pcu_lock);
  621. if (ret) {
  622. DRM_ERROR("failed to inform pcode about cdclk change\n");
  623. return;
  624. }
  625. val = I915_READ(LCPLL_CTL);
  626. val |= LCPLL_CD_SOURCE_FCLK;
  627. I915_WRITE(LCPLL_CTL, val);
  628. /*
  629. * According to the spec, it should be enough to poll for this 1 us.
  630. * However, extensive testing shows that this can take longer.
  631. */
  632. if (wait_for_us(I915_READ(LCPLL_CTL) &
  633. LCPLL_CD_SOURCE_FCLK_DONE, 100))
  634. DRM_ERROR("Switching to FCLK failed\n");
  635. val = I915_READ(LCPLL_CTL);
  636. val &= ~LCPLL_CLK_FREQ_MASK;
  637. switch (cdclk) {
  638. default:
  639. MISSING_CASE(cdclk);
  640. /* fall through */
  641. case 337500:
  642. val |= LCPLL_CLK_FREQ_337_5_BDW;
  643. break;
  644. case 450000:
  645. val |= LCPLL_CLK_FREQ_450;
  646. break;
  647. case 540000:
  648. val |= LCPLL_CLK_FREQ_54O_BDW;
  649. break;
  650. case 675000:
  651. val |= LCPLL_CLK_FREQ_675_BDW;
  652. break;
  653. }
  654. I915_WRITE(LCPLL_CTL, val);
  655. val = I915_READ(LCPLL_CTL);
  656. val &= ~LCPLL_CD_SOURCE_FCLK;
  657. I915_WRITE(LCPLL_CTL, val);
  658. if (wait_for_us((I915_READ(LCPLL_CTL) &
  659. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  660. DRM_ERROR("Switching back to LCPLL failed\n");
  661. mutex_lock(&dev_priv->pcu_lock);
  662. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  663. cdclk_state->voltage_level);
  664. mutex_unlock(&dev_priv->pcu_lock);
  665. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  666. intel_update_cdclk(dev_priv);
  667. }
  668. static int skl_calc_cdclk(int min_cdclk, int vco)
  669. {
  670. if (vco == 8640000) {
  671. if (min_cdclk > 540000)
  672. return 617143;
  673. else if (min_cdclk > 432000)
  674. return 540000;
  675. else if (min_cdclk > 308571)
  676. return 432000;
  677. else
  678. return 308571;
  679. } else {
  680. if (min_cdclk > 540000)
  681. return 675000;
  682. else if (min_cdclk > 450000)
  683. return 540000;
  684. else if (min_cdclk > 337500)
  685. return 450000;
  686. else
  687. return 337500;
  688. }
  689. }
  690. static u8 skl_calc_voltage_level(int cdclk)
  691. {
  692. switch (cdclk) {
  693. default:
  694. case 308571:
  695. case 337500:
  696. return 0;
  697. case 450000:
  698. case 432000:
  699. return 1;
  700. case 540000:
  701. return 2;
  702. case 617143:
  703. case 675000:
  704. return 3;
  705. }
  706. }
  707. static void skl_dpll0_update(struct drm_i915_private *dev_priv,
  708. struct intel_cdclk_state *cdclk_state)
  709. {
  710. u32 val;
  711. cdclk_state->ref = 24000;
  712. cdclk_state->vco = 0;
  713. val = I915_READ(LCPLL1_CTL);
  714. if ((val & LCPLL_PLL_ENABLE) == 0)
  715. return;
  716. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  717. return;
  718. val = I915_READ(DPLL_CTRL1);
  719. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  720. DPLL_CTRL1_SSC(SKL_DPLL0) |
  721. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  722. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  723. return;
  724. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  725. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  726. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  727. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  728. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  729. cdclk_state->vco = 8100000;
  730. break;
  731. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  732. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  733. cdclk_state->vco = 8640000;
  734. break;
  735. default:
  736. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  737. break;
  738. }
  739. }
  740. static void skl_get_cdclk(struct drm_i915_private *dev_priv,
  741. struct intel_cdclk_state *cdclk_state)
  742. {
  743. u32 cdctl;
  744. skl_dpll0_update(dev_priv, cdclk_state);
  745. cdclk_state->cdclk = cdclk_state->ref;
  746. if (cdclk_state->vco == 0)
  747. goto out;
  748. cdctl = I915_READ(CDCLK_CTL);
  749. if (cdclk_state->vco == 8640000) {
  750. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  751. case CDCLK_FREQ_450_432:
  752. cdclk_state->cdclk = 432000;
  753. break;
  754. case CDCLK_FREQ_337_308:
  755. cdclk_state->cdclk = 308571;
  756. break;
  757. case CDCLK_FREQ_540:
  758. cdclk_state->cdclk = 540000;
  759. break;
  760. case CDCLK_FREQ_675_617:
  761. cdclk_state->cdclk = 617143;
  762. break;
  763. default:
  764. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  765. break;
  766. }
  767. } else {
  768. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  769. case CDCLK_FREQ_450_432:
  770. cdclk_state->cdclk = 450000;
  771. break;
  772. case CDCLK_FREQ_337_308:
  773. cdclk_state->cdclk = 337500;
  774. break;
  775. case CDCLK_FREQ_540:
  776. cdclk_state->cdclk = 540000;
  777. break;
  778. case CDCLK_FREQ_675_617:
  779. cdclk_state->cdclk = 675000;
  780. break;
  781. default:
  782. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  783. break;
  784. }
  785. }
  786. out:
  787. /*
  788. * Can't read this out :( Let's assume it's
  789. * at least what the CDCLK frequency requires.
  790. */
  791. cdclk_state->voltage_level =
  792. skl_calc_voltage_level(cdclk_state->cdclk);
  793. }
  794. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  795. static int skl_cdclk_decimal(int cdclk)
  796. {
  797. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  798. }
  799. static void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv,
  800. int vco)
  801. {
  802. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  803. dev_priv->skl_preferred_vco_freq = vco;
  804. if (changed)
  805. intel_update_max_cdclk(dev_priv);
  806. }
  807. static void skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  808. {
  809. u32 val;
  810. WARN_ON(vco != 8100000 && vco != 8640000);
  811. /*
  812. * We always enable DPLL0 with the lowest link rate possible, but still
  813. * taking into account the VCO required to operate the eDP panel at the
  814. * desired frequency. The usual DP link rates operate with a VCO of
  815. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  816. * The modeset code is responsible for the selection of the exact link
  817. * rate later on, with the constraint of choosing a frequency that
  818. * works with vco.
  819. */
  820. val = I915_READ(DPLL_CTRL1);
  821. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  822. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  823. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  824. if (vco == 8640000)
  825. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  826. SKL_DPLL0);
  827. else
  828. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  829. SKL_DPLL0);
  830. I915_WRITE(DPLL_CTRL1, val);
  831. POSTING_READ(DPLL_CTRL1);
  832. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  833. if (intel_wait_for_register(dev_priv,
  834. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  835. 5))
  836. DRM_ERROR("DPLL0 not locked\n");
  837. dev_priv->cdclk.hw.vco = vco;
  838. /* We'll want to keep using the current vco from now on. */
  839. skl_set_preferred_cdclk_vco(dev_priv, vco);
  840. }
  841. static void skl_dpll0_disable(struct drm_i915_private *dev_priv)
  842. {
  843. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  844. if (intel_wait_for_register(dev_priv,
  845. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  846. 1))
  847. DRM_ERROR("Couldn't disable DPLL0\n");
  848. dev_priv->cdclk.hw.vco = 0;
  849. }
  850. static void skl_set_cdclk(struct drm_i915_private *dev_priv,
  851. const struct intel_cdclk_state *cdclk_state)
  852. {
  853. int cdclk = cdclk_state->cdclk;
  854. int vco = cdclk_state->vco;
  855. u32 freq_select, cdclk_ctl;
  856. int ret;
  857. mutex_lock(&dev_priv->pcu_lock);
  858. ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
  859. SKL_CDCLK_PREPARE_FOR_CHANGE,
  860. SKL_CDCLK_READY_FOR_CHANGE,
  861. SKL_CDCLK_READY_FOR_CHANGE, 3);
  862. mutex_unlock(&dev_priv->pcu_lock);
  863. if (ret) {
  864. DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
  865. ret);
  866. return;
  867. }
  868. /* Choose frequency for this cdclk */
  869. switch (cdclk) {
  870. default:
  871. WARN_ON(cdclk != dev_priv->cdclk.hw.ref);
  872. WARN_ON(vco != 0);
  873. /* fall through */
  874. case 308571:
  875. case 337500:
  876. freq_select = CDCLK_FREQ_337_308;
  877. break;
  878. case 450000:
  879. case 432000:
  880. freq_select = CDCLK_FREQ_450_432;
  881. break;
  882. case 540000:
  883. freq_select = CDCLK_FREQ_540;
  884. break;
  885. case 617143:
  886. case 675000:
  887. freq_select = CDCLK_FREQ_675_617;
  888. break;
  889. }
  890. if (dev_priv->cdclk.hw.vco != 0 &&
  891. dev_priv->cdclk.hw.vco != vco)
  892. skl_dpll0_disable(dev_priv);
  893. cdclk_ctl = I915_READ(CDCLK_CTL);
  894. if (dev_priv->cdclk.hw.vco != vco) {
  895. /* Wa Display #1183: skl,kbl,cfl */
  896. cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
  897. cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
  898. I915_WRITE(CDCLK_CTL, cdclk_ctl);
  899. }
  900. /* Wa Display #1183: skl,kbl,cfl */
  901. cdclk_ctl |= CDCLK_DIVMUX_CD_OVERRIDE;
  902. I915_WRITE(CDCLK_CTL, cdclk_ctl);
  903. POSTING_READ(CDCLK_CTL);
  904. if (dev_priv->cdclk.hw.vco != vco)
  905. skl_dpll0_enable(dev_priv, vco);
  906. /* Wa Display #1183: skl,kbl,cfl */
  907. cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
  908. I915_WRITE(CDCLK_CTL, cdclk_ctl);
  909. cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
  910. I915_WRITE(CDCLK_CTL, cdclk_ctl);
  911. /* Wa Display #1183: skl,kbl,cfl */
  912. cdclk_ctl &= ~CDCLK_DIVMUX_CD_OVERRIDE;
  913. I915_WRITE(CDCLK_CTL, cdclk_ctl);
  914. POSTING_READ(CDCLK_CTL);
  915. /* inform PCU of the change */
  916. mutex_lock(&dev_priv->pcu_lock);
  917. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL,
  918. cdclk_state->voltage_level);
  919. mutex_unlock(&dev_priv->pcu_lock);
  920. intel_update_cdclk(dev_priv);
  921. }
  922. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  923. {
  924. uint32_t cdctl, expected;
  925. /*
  926. * check if the pre-os initialized the display
  927. * There is SWF18 scratchpad register defined which is set by the
  928. * pre-os which can be used by the OS drivers to check the status
  929. */
  930. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  931. goto sanitize;
  932. intel_update_cdclk(dev_priv);
  933. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  934. /* Is PLL enabled and locked ? */
  935. if (dev_priv->cdclk.hw.vco == 0 ||
  936. dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.ref)
  937. goto sanitize;
  938. /* DPLL okay; verify the cdclock
  939. *
  940. * Noticed in some instances that the freq selection is correct but
  941. * decimal part is programmed wrong from BIOS where pre-os does not
  942. * enable display. Verify the same as well.
  943. */
  944. cdctl = I915_READ(CDCLK_CTL);
  945. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  946. skl_cdclk_decimal(dev_priv->cdclk.hw.cdclk);
  947. if (cdctl == expected)
  948. /* All well; nothing to sanitize */
  949. return;
  950. sanitize:
  951. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  952. /* force cdclk programming */
  953. dev_priv->cdclk.hw.cdclk = 0;
  954. /* force full PLL disable + enable */
  955. dev_priv->cdclk.hw.vco = -1;
  956. }
  957. /**
  958. * skl_init_cdclk - Initialize CDCLK on SKL
  959. * @dev_priv: i915 device
  960. *
  961. * Initialize CDCLK for SKL and derivatives. This is generally
  962. * done only during the display core initialization sequence,
  963. * after which the DMC will take care of turning CDCLK off/on
  964. * as needed.
  965. */
  966. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  967. {
  968. struct intel_cdclk_state cdclk_state;
  969. skl_sanitize_cdclk(dev_priv);
  970. if (dev_priv->cdclk.hw.cdclk != 0 &&
  971. dev_priv->cdclk.hw.vco != 0) {
  972. /*
  973. * Use the current vco as our initial
  974. * guess as to what the preferred vco is.
  975. */
  976. if (dev_priv->skl_preferred_vco_freq == 0)
  977. skl_set_preferred_cdclk_vco(dev_priv,
  978. dev_priv->cdclk.hw.vco);
  979. return;
  980. }
  981. cdclk_state = dev_priv->cdclk.hw;
  982. cdclk_state.vco = dev_priv->skl_preferred_vco_freq;
  983. if (cdclk_state.vco == 0)
  984. cdclk_state.vco = 8100000;
  985. cdclk_state.cdclk = skl_calc_cdclk(0, cdclk_state.vco);
  986. cdclk_state.voltage_level = skl_calc_voltage_level(cdclk_state.cdclk);
  987. skl_set_cdclk(dev_priv, &cdclk_state);
  988. }
  989. /**
  990. * skl_uninit_cdclk - Uninitialize CDCLK on SKL
  991. * @dev_priv: i915 device
  992. *
  993. * Uninitialize CDCLK for SKL and derivatives. This is done only
  994. * during the display core uninitialization sequence.
  995. */
  996. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  997. {
  998. struct intel_cdclk_state cdclk_state = dev_priv->cdclk.hw;
  999. cdclk_state.cdclk = cdclk_state.ref;
  1000. cdclk_state.vco = 0;
  1001. cdclk_state.voltage_level = skl_calc_voltage_level(cdclk_state.cdclk);
  1002. skl_set_cdclk(dev_priv, &cdclk_state);
  1003. }
  1004. static int bxt_calc_cdclk(int min_cdclk)
  1005. {
  1006. if (min_cdclk > 576000)
  1007. return 624000;
  1008. else if (min_cdclk > 384000)
  1009. return 576000;
  1010. else if (min_cdclk > 288000)
  1011. return 384000;
  1012. else if (min_cdclk > 144000)
  1013. return 288000;
  1014. else
  1015. return 144000;
  1016. }
  1017. static int glk_calc_cdclk(int min_cdclk)
  1018. {
  1019. if (min_cdclk > 158400)
  1020. return 316800;
  1021. else if (min_cdclk > 79200)
  1022. return 158400;
  1023. else
  1024. return 79200;
  1025. }
  1026. static u8 bxt_calc_voltage_level(int cdclk)
  1027. {
  1028. return DIV_ROUND_UP(cdclk, 25000);
  1029. }
  1030. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  1031. {
  1032. int ratio;
  1033. if (cdclk == dev_priv->cdclk.hw.ref)
  1034. return 0;
  1035. switch (cdclk) {
  1036. default:
  1037. MISSING_CASE(cdclk);
  1038. /* fall through */
  1039. case 144000:
  1040. case 288000:
  1041. case 384000:
  1042. case 576000:
  1043. ratio = 60;
  1044. break;
  1045. case 624000:
  1046. ratio = 65;
  1047. break;
  1048. }
  1049. return dev_priv->cdclk.hw.ref * ratio;
  1050. }
  1051. static int glk_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  1052. {
  1053. int ratio;
  1054. if (cdclk == dev_priv->cdclk.hw.ref)
  1055. return 0;
  1056. switch (cdclk) {
  1057. default:
  1058. MISSING_CASE(cdclk);
  1059. /* fall through */
  1060. case 79200:
  1061. case 158400:
  1062. case 316800:
  1063. ratio = 33;
  1064. break;
  1065. }
  1066. return dev_priv->cdclk.hw.ref * ratio;
  1067. }
  1068. static void bxt_de_pll_update(struct drm_i915_private *dev_priv,
  1069. struct intel_cdclk_state *cdclk_state)
  1070. {
  1071. u32 val;
  1072. cdclk_state->ref = 19200;
  1073. cdclk_state->vco = 0;
  1074. val = I915_READ(BXT_DE_PLL_ENABLE);
  1075. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  1076. return;
  1077. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  1078. return;
  1079. val = I915_READ(BXT_DE_PLL_CTL);
  1080. cdclk_state->vco = (val & BXT_DE_PLL_RATIO_MASK) * cdclk_state->ref;
  1081. }
  1082. static void bxt_get_cdclk(struct drm_i915_private *dev_priv,
  1083. struct intel_cdclk_state *cdclk_state)
  1084. {
  1085. u32 divider;
  1086. int div;
  1087. bxt_de_pll_update(dev_priv, cdclk_state);
  1088. cdclk_state->cdclk = cdclk_state->ref;
  1089. if (cdclk_state->vco == 0)
  1090. goto out;
  1091. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  1092. switch (divider) {
  1093. case BXT_CDCLK_CD2X_DIV_SEL_1:
  1094. div = 2;
  1095. break;
  1096. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  1097. WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
  1098. div = 3;
  1099. break;
  1100. case BXT_CDCLK_CD2X_DIV_SEL_2:
  1101. div = 4;
  1102. break;
  1103. case BXT_CDCLK_CD2X_DIV_SEL_4:
  1104. div = 8;
  1105. break;
  1106. default:
  1107. MISSING_CASE(divider);
  1108. return;
  1109. }
  1110. cdclk_state->cdclk = DIV_ROUND_CLOSEST(cdclk_state->vco, div);
  1111. out:
  1112. /*
  1113. * Can't read this out :( Let's assume it's
  1114. * at least what the CDCLK frequency requires.
  1115. */
  1116. cdclk_state->voltage_level =
  1117. bxt_calc_voltage_level(cdclk_state->cdclk);
  1118. }
  1119. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  1120. {
  1121. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  1122. /* Timeout 200us */
  1123. if (intel_wait_for_register(dev_priv,
  1124. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  1125. 1))
  1126. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  1127. dev_priv->cdclk.hw.vco = 0;
  1128. }
  1129. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  1130. {
  1131. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk.hw.ref);
  1132. u32 val;
  1133. val = I915_READ(BXT_DE_PLL_CTL);
  1134. val &= ~BXT_DE_PLL_RATIO_MASK;
  1135. val |= BXT_DE_PLL_RATIO(ratio);
  1136. I915_WRITE(BXT_DE_PLL_CTL, val);
  1137. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  1138. /* Timeout 200us */
  1139. if (intel_wait_for_register(dev_priv,
  1140. BXT_DE_PLL_ENABLE,
  1141. BXT_DE_PLL_LOCK,
  1142. BXT_DE_PLL_LOCK,
  1143. 1))
  1144. DRM_ERROR("timeout waiting for DE PLL lock\n");
  1145. dev_priv->cdclk.hw.vco = vco;
  1146. }
  1147. static void bxt_set_cdclk(struct drm_i915_private *dev_priv,
  1148. const struct intel_cdclk_state *cdclk_state)
  1149. {
  1150. int cdclk = cdclk_state->cdclk;
  1151. int vco = cdclk_state->vco;
  1152. u32 val, divider;
  1153. int ret;
  1154. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  1155. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  1156. default:
  1157. WARN_ON(cdclk != dev_priv->cdclk.hw.ref);
  1158. WARN_ON(vco != 0);
  1159. /* fall through */
  1160. case 2:
  1161. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  1162. break;
  1163. case 3:
  1164. WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
  1165. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  1166. break;
  1167. case 4:
  1168. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  1169. break;
  1170. case 8:
  1171. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  1172. break;
  1173. }
  1174. /* Inform power controller of upcoming frequency change */
  1175. mutex_lock(&dev_priv->pcu_lock);
  1176. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  1177. 0x80000000);
  1178. mutex_unlock(&dev_priv->pcu_lock);
  1179. if (ret) {
  1180. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  1181. ret, cdclk);
  1182. return;
  1183. }
  1184. if (dev_priv->cdclk.hw.vco != 0 &&
  1185. dev_priv->cdclk.hw.vco != vco)
  1186. bxt_de_pll_disable(dev_priv);
  1187. if (dev_priv->cdclk.hw.vco != vco)
  1188. bxt_de_pll_enable(dev_priv, vco);
  1189. val = divider | skl_cdclk_decimal(cdclk);
  1190. /*
  1191. * FIXME if only the cd2x divider needs changing, it could be done
  1192. * without shutting off the pipe (if only one pipe is active).
  1193. */
  1194. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  1195. /*
  1196. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  1197. * enable otherwise.
  1198. */
  1199. if (cdclk >= 500000)
  1200. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  1201. I915_WRITE(CDCLK_CTL, val);
  1202. mutex_lock(&dev_priv->pcu_lock);
  1203. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  1204. cdclk_state->voltage_level);
  1205. mutex_unlock(&dev_priv->pcu_lock);
  1206. if (ret) {
  1207. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  1208. ret, cdclk);
  1209. return;
  1210. }
  1211. intel_update_cdclk(dev_priv);
  1212. }
  1213. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  1214. {
  1215. u32 cdctl, expected;
  1216. intel_update_cdclk(dev_priv);
  1217. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  1218. if (dev_priv->cdclk.hw.vco == 0 ||
  1219. dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.ref)
  1220. goto sanitize;
  1221. /* DPLL okay; verify the cdclock
  1222. *
  1223. * Some BIOS versions leave an incorrect decimal frequency value and
  1224. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  1225. * so sanitize this register.
  1226. */
  1227. cdctl = I915_READ(CDCLK_CTL);
  1228. /*
  1229. * Let's ignore the pipe field, since BIOS could have configured the
  1230. * dividers both synching to an active pipe, or asynchronously
  1231. * (PIPE_NONE).
  1232. */
  1233. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  1234. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  1235. skl_cdclk_decimal(dev_priv->cdclk.hw.cdclk);
  1236. /*
  1237. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  1238. * enable otherwise.
  1239. */
  1240. if (dev_priv->cdclk.hw.cdclk >= 500000)
  1241. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  1242. if (cdctl == expected)
  1243. /* All well; nothing to sanitize */
  1244. return;
  1245. sanitize:
  1246. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  1247. /* force cdclk programming */
  1248. dev_priv->cdclk.hw.cdclk = 0;
  1249. /* force full PLL disable + enable */
  1250. dev_priv->cdclk.hw.vco = -1;
  1251. }
  1252. /**
  1253. * bxt_init_cdclk - Initialize CDCLK on BXT
  1254. * @dev_priv: i915 device
  1255. *
  1256. * Initialize CDCLK for BXT and derivatives. This is generally
  1257. * done only during the display core initialization sequence,
  1258. * after which the DMC will take care of turning CDCLK off/on
  1259. * as needed.
  1260. */
  1261. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  1262. {
  1263. struct intel_cdclk_state cdclk_state;
  1264. bxt_sanitize_cdclk(dev_priv);
  1265. if (dev_priv->cdclk.hw.cdclk != 0 &&
  1266. dev_priv->cdclk.hw.vco != 0)
  1267. return;
  1268. cdclk_state = dev_priv->cdclk.hw;
  1269. /*
  1270. * FIXME:
  1271. * - The initial CDCLK needs to be read from VBT.
  1272. * Need to make this change after VBT has changes for BXT.
  1273. */
  1274. if (IS_GEMINILAKE(dev_priv)) {
  1275. cdclk_state.cdclk = glk_calc_cdclk(0);
  1276. cdclk_state.vco = glk_de_pll_vco(dev_priv, cdclk_state.cdclk);
  1277. } else {
  1278. cdclk_state.cdclk = bxt_calc_cdclk(0);
  1279. cdclk_state.vco = bxt_de_pll_vco(dev_priv, cdclk_state.cdclk);
  1280. }
  1281. cdclk_state.voltage_level = bxt_calc_voltage_level(cdclk_state.cdclk);
  1282. bxt_set_cdclk(dev_priv, &cdclk_state);
  1283. }
  1284. /**
  1285. * bxt_uninit_cdclk - Uninitialize CDCLK on BXT
  1286. * @dev_priv: i915 device
  1287. *
  1288. * Uninitialize CDCLK for BXT and derivatives. This is done only
  1289. * during the display core uninitialization sequence.
  1290. */
  1291. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  1292. {
  1293. struct intel_cdclk_state cdclk_state = dev_priv->cdclk.hw;
  1294. cdclk_state.cdclk = cdclk_state.ref;
  1295. cdclk_state.vco = 0;
  1296. cdclk_state.voltage_level = bxt_calc_voltage_level(cdclk_state.cdclk);
  1297. bxt_set_cdclk(dev_priv, &cdclk_state);
  1298. }
  1299. static int cnl_calc_cdclk(int min_cdclk)
  1300. {
  1301. if (min_cdclk > 336000)
  1302. return 528000;
  1303. else if (min_cdclk > 168000)
  1304. return 336000;
  1305. else
  1306. return 168000;
  1307. }
  1308. static u8 cnl_calc_voltage_level(int cdclk)
  1309. {
  1310. switch (cdclk) {
  1311. default:
  1312. case 168000:
  1313. return 0;
  1314. case 336000:
  1315. return 1;
  1316. case 528000:
  1317. return 2;
  1318. }
  1319. }
  1320. static void cnl_cdclk_pll_update(struct drm_i915_private *dev_priv,
  1321. struct intel_cdclk_state *cdclk_state)
  1322. {
  1323. u32 val;
  1324. if (I915_READ(SKL_DSSM) & CNL_DSSM_CDCLK_PLL_REFCLK_24MHz)
  1325. cdclk_state->ref = 24000;
  1326. else
  1327. cdclk_state->ref = 19200;
  1328. cdclk_state->vco = 0;
  1329. val = I915_READ(BXT_DE_PLL_ENABLE);
  1330. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  1331. return;
  1332. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  1333. return;
  1334. cdclk_state->vco = (val & CNL_CDCLK_PLL_RATIO_MASK) * cdclk_state->ref;
  1335. }
  1336. static void cnl_get_cdclk(struct drm_i915_private *dev_priv,
  1337. struct intel_cdclk_state *cdclk_state)
  1338. {
  1339. u32 divider;
  1340. int div;
  1341. cnl_cdclk_pll_update(dev_priv, cdclk_state);
  1342. cdclk_state->cdclk = cdclk_state->ref;
  1343. if (cdclk_state->vco == 0)
  1344. goto out;
  1345. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  1346. switch (divider) {
  1347. case BXT_CDCLK_CD2X_DIV_SEL_1:
  1348. div = 2;
  1349. break;
  1350. case BXT_CDCLK_CD2X_DIV_SEL_2:
  1351. div = 4;
  1352. break;
  1353. default:
  1354. MISSING_CASE(divider);
  1355. return;
  1356. }
  1357. cdclk_state->cdclk = DIV_ROUND_CLOSEST(cdclk_state->vco, div);
  1358. out:
  1359. /*
  1360. * Can't read this out :( Let's assume it's
  1361. * at least what the CDCLK frequency requires.
  1362. */
  1363. cdclk_state->voltage_level =
  1364. cnl_calc_voltage_level(cdclk_state->cdclk);
  1365. }
  1366. static void cnl_cdclk_pll_disable(struct drm_i915_private *dev_priv)
  1367. {
  1368. u32 val;
  1369. val = I915_READ(BXT_DE_PLL_ENABLE);
  1370. val &= ~BXT_DE_PLL_PLL_ENABLE;
  1371. I915_WRITE(BXT_DE_PLL_ENABLE, val);
  1372. /* Timeout 200us */
  1373. if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
  1374. DRM_ERROR("timout waiting for CDCLK PLL unlock\n");
  1375. dev_priv->cdclk.hw.vco = 0;
  1376. }
  1377. static void cnl_cdclk_pll_enable(struct drm_i915_private *dev_priv, int vco)
  1378. {
  1379. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk.hw.ref);
  1380. u32 val;
  1381. val = CNL_CDCLK_PLL_RATIO(ratio);
  1382. I915_WRITE(BXT_DE_PLL_ENABLE, val);
  1383. val |= BXT_DE_PLL_PLL_ENABLE;
  1384. I915_WRITE(BXT_DE_PLL_ENABLE, val);
  1385. /* Timeout 200us */
  1386. if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
  1387. DRM_ERROR("timout waiting for CDCLK PLL lock\n");
  1388. dev_priv->cdclk.hw.vco = vco;
  1389. }
  1390. static void cnl_set_cdclk(struct drm_i915_private *dev_priv,
  1391. const struct intel_cdclk_state *cdclk_state)
  1392. {
  1393. int cdclk = cdclk_state->cdclk;
  1394. int vco = cdclk_state->vco;
  1395. u32 val, divider;
  1396. int ret;
  1397. mutex_lock(&dev_priv->pcu_lock);
  1398. ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
  1399. SKL_CDCLK_PREPARE_FOR_CHANGE,
  1400. SKL_CDCLK_READY_FOR_CHANGE,
  1401. SKL_CDCLK_READY_FOR_CHANGE, 3);
  1402. mutex_unlock(&dev_priv->pcu_lock);
  1403. if (ret) {
  1404. DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
  1405. ret);
  1406. return;
  1407. }
  1408. /* cdclk = vco / 2 / div{1,2} */
  1409. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  1410. default:
  1411. WARN_ON(cdclk != dev_priv->cdclk.hw.ref);
  1412. WARN_ON(vco != 0);
  1413. /* fall through */
  1414. case 2:
  1415. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  1416. break;
  1417. case 4:
  1418. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  1419. break;
  1420. }
  1421. if (dev_priv->cdclk.hw.vco != 0 &&
  1422. dev_priv->cdclk.hw.vco != vco)
  1423. cnl_cdclk_pll_disable(dev_priv);
  1424. if (dev_priv->cdclk.hw.vco != vco)
  1425. cnl_cdclk_pll_enable(dev_priv, vco);
  1426. val = divider | skl_cdclk_decimal(cdclk);
  1427. /*
  1428. * FIXME if only the cd2x divider needs changing, it could be done
  1429. * without shutting off the pipe (if only one pipe is active).
  1430. */
  1431. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  1432. I915_WRITE(CDCLK_CTL, val);
  1433. /* inform PCU of the change */
  1434. mutex_lock(&dev_priv->pcu_lock);
  1435. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL,
  1436. cdclk_state->voltage_level);
  1437. mutex_unlock(&dev_priv->pcu_lock);
  1438. intel_update_cdclk(dev_priv);
  1439. /*
  1440. * Can't read out the voltage level :(
  1441. * Let's just assume everything is as expected.
  1442. */
  1443. dev_priv->cdclk.hw.voltage_level = cdclk_state->voltage_level;
  1444. }
  1445. static int cnl_cdclk_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  1446. {
  1447. int ratio;
  1448. if (cdclk == dev_priv->cdclk.hw.ref)
  1449. return 0;
  1450. switch (cdclk) {
  1451. default:
  1452. MISSING_CASE(cdclk);
  1453. /* fall through */
  1454. case 168000:
  1455. case 336000:
  1456. ratio = dev_priv->cdclk.hw.ref == 19200 ? 35 : 28;
  1457. break;
  1458. case 528000:
  1459. ratio = dev_priv->cdclk.hw.ref == 19200 ? 55 : 44;
  1460. break;
  1461. }
  1462. return dev_priv->cdclk.hw.ref * ratio;
  1463. }
  1464. static void cnl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  1465. {
  1466. u32 cdctl, expected;
  1467. intel_update_cdclk(dev_priv);
  1468. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  1469. if (dev_priv->cdclk.hw.vco == 0 ||
  1470. dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.ref)
  1471. goto sanitize;
  1472. /* DPLL okay; verify the cdclock
  1473. *
  1474. * Some BIOS versions leave an incorrect decimal frequency value and
  1475. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  1476. * so sanitize this register.
  1477. */
  1478. cdctl = I915_READ(CDCLK_CTL);
  1479. /*
  1480. * Let's ignore the pipe field, since BIOS could have configured the
  1481. * dividers both synching to an active pipe, or asynchronously
  1482. * (PIPE_NONE).
  1483. */
  1484. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  1485. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  1486. skl_cdclk_decimal(dev_priv->cdclk.hw.cdclk);
  1487. if (cdctl == expected)
  1488. /* All well; nothing to sanitize */
  1489. return;
  1490. sanitize:
  1491. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  1492. /* force cdclk programming */
  1493. dev_priv->cdclk.hw.cdclk = 0;
  1494. /* force full PLL disable + enable */
  1495. dev_priv->cdclk.hw.vco = -1;
  1496. }
  1497. /**
  1498. * cnl_init_cdclk - Initialize CDCLK on CNL
  1499. * @dev_priv: i915 device
  1500. *
  1501. * Initialize CDCLK for CNL. This is generally
  1502. * done only during the display core initialization sequence,
  1503. * after which the DMC will take care of turning CDCLK off/on
  1504. * as needed.
  1505. */
  1506. void cnl_init_cdclk(struct drm_i915_private *dev_priv)
  1507. {
  1508. struct intel_cdclk_state cdclk_state;
  1509. cnl_sanitize_cdclk(dev_priv);
  1510. if (dev_priv->cdclk.hw.cdclk != 0 &&
  1511. dev_priv->cdclk.hw.vco != 0)
  1512. return;
  1513. cdclk_state = dev_priv->cdclk.hw;
  1514. cdclk_state.cdclk = cnl_calc_cdclk(0);
  1515. cdclk_state.vco = cnl_cdclk_pll_vco(dev_priv, cdclk_state.cdclk);
  1516. cdclk_state.voltage_level = cnl_calc_voltage_level(cdclk_state.cdclk);
  1517. cnl_set_cdclk(dev_priv, &cdclk_state);
  1518. }
  1519. /**
  1520. * cnl_uninit_cdclk - Uninitialize CDCLK on CNL
  1521. * @dev_priv: i915 device
  1522. *
  1523. * Uninitialize CDCLK for CNL. This is done only
  1524. * during the display core uninitialization sequence.
  1525. */
  1526. void cnl_uninit_cdclk(struct drm_i915_private *dev_priv)
  1527. {
  1528. struct intel_cdclk_state cdclk_state = dev_priv->cdclk.hw;
  1529. cdclk_state.cdclk = cdclk_state.ref;
  1530. cdclk_state.vco = 0;
  1531. cdclk_state.voltage_level = cnl_calc_voltage_level(cdclk_state.cdclk);
  1532. cnl_set_cdclk(dev_priv, &cdclk_state);
  1533. }
  1534. /**
  1535. * intel_cdclk_needs_modeset - Determine if two CDCLK states require a modeset on all pipes
  1536. * @a: first CDCLK state
  1537. * @b: second CDCLK state
  1538. *
  1539. * Returns:
  1540. * True if the CDCLK states require pipes to be off during reprogramming, false if not.
  1541. */
  1542. bool intel_cdclk_needs_modeset(const struct intel_cdclk_state *a,
  1543. const struct intel_cdclk_state *b)
  1544. {
  1545. return a->cdclk != b->cdclk ||
  1546. a->vco != b->vco ||
  1547. a->ref != b->ref;
  1548. }
  1549. /**
  1550. * intel_cdclk_changed - Determine if two CDCLK states are different
  1551. * @a: first CDCLK state
  1552. * @b: second CDCLK state
  1553. *
  1554. * Returns:
  1555. * True if the CDCLK states don't match, false if they do.
  1556. */
  1557. bool intel_cdclk_changed(const struct intel_cdclk_state *a,
  1558. const struct intel_cdclk_state *b)
  1559. {
  1560. return intel_cdclk_needs_modeset(a, b) ||
  1561. a->voltage_level != b->voltage_level;
  1562. }
  1563. void intel_dump_cdclk_state(const struct intel_cdclk_state *cdclk_state,
  1564. const char *context)
  1565. {
  1566. DRM_DEBUG_DRIVER("%s %d kHz, VCO %d kHz, ref %d kHz, voltage level %d\n",
  1567. context, cdclk_state->cdclk, cdclk_state->vco,
  1568. cdclk_state->ref, cdclk_state->voltage_level);
  1569. }
  1570. /**
  1571. * intel_set_cdclk - Push the CDCLK state to the hardware
  1572. * @dev_priv: i915 device
  1573. * @cdclk_state: new CDCLK state
  1574. *
  1575. * Program the hardware based on the passed in CDCLK state,
  1576. * if necessary.
  1577. */
  1578. void intel_set_cdclk(struct drm_i915_private *dev_priv,
  1579. const struct intel_cdclk_state *cdclk_state)
  1580. {
  1581. if (!intel_cdclk_changed(&dev_priv->cdclk.hw, cdclk_state))
  1582. return;
  1583. if (WARN_ON_ONCE(!dev_priv->display.set_cdclk))
  1584. return;
  1585. intel_dump_cdclk_state(cdclk_state, "Changing CDCLK to");
  1586. dev_priv->display.set_cdclk(dev_priv, cdclk_state);
  1587. if (WARN(intel_cdclk_changed(&dev_priv->cdclk.hw, cdclk_state),
  1588. "cdclk state doesn't match!\n")) {
  1589. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "[hw state]");
  1590. intel_dump_cdclk_state(cdclk_state, "[sw state]");
  1591. }
  1592. }
  1593. static int intel_pixel_rate_to_cdclk(struct drm_i915_private *dev_priv,
  1594. int pixel_rate)
  1595. {
  1596. if (INTEL_GEN(dev_priv) >= 10)
  1597. return DIV_ROUND_UP(pixel_rate, 2);
  1598. else if (IS_GEMINILAKE(dev_priv))
  1599. /*
  1600. * FIXME: Avoid using a pixel clock that is more than 99% of the cdclk
  1601. * as a temporary workaround. Use a higher cdclk instead. (Note that
  1602. * intel_compute_max_dotclk() limits the max pixel clock to 99% of max
  1603. * cdclk.)
  1604. */
  1605. return DIV_ROUND_UP(pixel_rate * 100, 2 * 99);
  1606. else if (IS_GEN9(dev_priv) ||
  1607. IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
  1608. return pixel_rate;
  1609. else if (IS_CHERRYVIEW(dev_priv))
  1610. return DIV_ROUND_UP(pixel_rate * 100, 95);
  1611. else
  1612. return DIV_ROUND_UP(pixel_rate * 100, 90);
  1613. }
  1614. int intel_crtc_compute_min_cdclk(const struct intel_crtc_state *crtc_state)
  1615. {
  1616. struct drm_i915_private *dev_priv =
  1617. to_i915(crtc_state->base.crtc->dev);
  1618. int min_cdclk;
  1619. if (!crtc_state->base.enable)
  1620. return 0;
  1621. min_cdclk = intel_pixel_rate_to_cdclk(dev_priv, crtc_state->pixel_rate);
  1622. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  1623. if (IS_BROADWELL(dev_priv) && hsw_crtc_state_ips_capable(crtc_state))
  1624. min_cdclk = DIV_ROUND_UP(min_cdclk * 100, 95);
  1625. /* BSpec says "Do not use DisplayPort with CDCLK less than 432 MHz,
  1626. * audio enabled, port width x4, and link rate HBR2 (5.4 GHz), or else
  1627. * there may be audio corruption or screen corruption." This cdclk
  1628. * restriction for GLK is 316.8 MHz.
  1629. */
  1630. if (intel_crtc_has_dp_encoder(crtc_state) &&
  1631. crtc_state->has_audio &&
  1632. crtc_state->port_clock >= 540000 &&
  1633. crtc_state->lane_count == 4) {
  1634. if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv)) {
  1635. /* Display WA #1145: glk,cnl */
  1636. min_cdclk = max(316800, min_cdclk);
  1637. } else if (IS_GEN9(dev_priv) || IS_BROADWELL(dev_priv)) {
  1638. /* Display WA #1144: skl,bxt */
  1639. min_cdclk = max(432000, min_cdclk);
  1640. }
  1641. }
  1642. /* According to BSpec, "The CD clock frequency must be at least twice
  1643. * the frequency of the Azalia BCLK." and BCLK is 96 MHz by default.
  1644. */
  1645. if (crtc_state->has_audio && INTEL_GEN(dev_priv) >= 9)
  1646. min_cdclk = max(2 * 96000, min_cdclk);
  1647. if (min_cdclk > dev_priv->max_cdclk_freq) {
  1648. DRM_DEBUG_KMS("required cdclk (%d kHz) exceeds max (%d kHz)\n",
  1649. min_cdclk, dev_priv->max_cdclk_freq);
  1650. return -EINVAL;
  1651. }
  1652. return min_cdclk;
  1653. }
  1654. static int intel_compute_min_cdclk(struct drm_atomic_state *state)
  1655. {
  1656. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  1657. struct drm_i915_private *dev_priv = to_i915(state->dev);
  1658. struct intel_crtc *crtc;
  1659. struct intel_crtc_state *crtc_state;
  1660. int min_cdclk, i;
  1661. enum pipe pipe;
  1662. memcpy(intel_state->min_cdclk, dev_priv->min_cdclk,
  1663. sizeof(intel_state->min_cdclk));
  1664. for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
  1665. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  1666. if (min_cdclk < 0)
  1667. return min_cdclk;
  1668. intel_state->min_cdclk[i] = min_cdclk;
  1669. }
  1670. min_cdclk = 0;
  1671. for_each_pipe(dev_priv, pipe)
  1672. min_cdclk = max(intel_state->min_cdclk[pipe], min_cdclk);
  1673. return min_cdclk;
  1674. }
  1675. /*
  1676. * Note that this functions assumes that 0 is
  1677. * the lowest voltage value, and higher values
  1678. * correspond to increasingly higher voltages.
  1679. *
  1680. * Should that relationship no longer hold on
  1681. * future platforms this code will need to be
  1682. * adjusted.
  1683. */
  1684. static u8 cnl_compute_min_voltage_level(struct intel_atomic_state *state)
  1685. {
  1686. struct drm_i915_private *dev_priv = to_i915(state->base.dev);
  1687. struct intel_crtc *crtc;
  1688. struct intel_crtc_state *crtc_state;
  1689. u8 min_voltage_level;
  1690. int i;
  1691. enum pipe pipe;
  1692. memcpy(state->min_voltage_level, dev_priv->min_voltage_level,
  1693. sizeof(state->min_voltage_level));
  1694. for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
  1695. if (crtc_state->base.enable)
  1696. state->min_voltage_level[i] =
  1697. crtc_state->min_voltage_level;
  1698. else
  1699. state->min_voltage_level[i] = 0;
  1700. }
  1701. min_voltage_level = 0;
  1702. for_each_pipe(dev_priv, pipe)
  1703. min_voltage_level = max(state->min_voltage_level[pipe],
  1704. min_voltage_level);
  1705. return min_voltage_level;
  1706. }
  1707. static int vlv_modeset_calc_cdclk(struct drm_atomic_state *state)
  1708. {
  1709. struct drm_i915_private *dev_priv = to_i915(state->dev);
  1710. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  1711. int min_cdclk, cdclk;
  1712. min_cdclk = intel_compute_min_cdclk(state);
  1713. if (min_cdclk < 0)
  1714. return min_cdclk;
  1715. cdclk = vlv_calc_cdclk(dev_priv, min_cdclk);
  1716. intel_state->cdclk.logical.cdclk = cdclk;
  1717. intel_state->cdclk.logical.voltage_level =
  1718. vlv_calc_voltage_level(dev_priv, cdclk);
  1719. if (!intel_state->active_crtcs) {
  1720. cdclk = vlv_calc_cdclk(dev_priv, 0);
  1721. intel_state->cdclk.actual.cdclk = cdclk;
  1722. intel_state->cdclk.actual.voltage_level =
  1723. vlv_calc_voltage_level(dev_priv, cdclk);
  1724. } else {
  1725. intel_state->cdclk.actual =
  1726. intel_state->cdclk.logical;
  1727. }
  1728. return 0;
  1729. }
  1730. static int bdw_modeset_calc_cdclk(struct drm_atomic_state *state)
  1731. {
  1732. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  1733. int min_cdclk, cdclk;
  1734. min_cdclk = intel_compute_min_cdclk(state);
  1735. if (min_cdclk < 0)
  1736. return min_cdclk;
  1737. /*
  1738. * FIXME should also account for plane ratio
  1739. * once 64bpp pixel formats are supported.
  1740. */
  1741. cdclk = bdw_calc_cdclk(min_cdclk);
  1742. intel_state->cdclk.logical.cdclk = cdclk;
  1743. intel_state->cdclk.logical.voltage_level =
  1744. bdw_calc_voltage_level(cdclk);
  1745. if (!intel_state->active_crtcs) {
  1746. cdclk = bdw_calc_cdclk(0);
  1747. intel_state->cdclk.actual.cdclk = cdclk;
  1748. intel_state->cdclk.actual.voltage_level =
  1749. bdw_calc_voltage_level(cdclk);
  1750. } else {
  1751. intel_state->cdclk.actual =
  1752. intel_state->cdclk.logical;
  1753. }
  1754. return 0;
  1755. }
  1756. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  1757. {
  1758. struct drm_i915_private *dev_priv = to_i915(state->dev);
  1759. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  1760. int min_cdclk, cdclk, vco;
  1761. min_cdclk = intel_compute_min_cdclk(state);
  1762. if (min_cdclk < 0)
  1763. return min_cdclk;
  1764. vco = intel_state->cdclk.logical.vco;
  1765. if (!vco)
  1766. vco = dev_priv->skl_preferred_vco_freq;
  1767. /*
  1768. * FIXME should also account for plane ratio
  1769. * once 64bpp pixel formats are supported.
  1770. */
  1771. cdclk = skl_calc_cdclk(min_cdclk, vco);
  1772. intel_state->cdclk.logical.vco = vco;
  1773. intel_state->cdclk.logical.cdclk = cdclk;
  1774. intel_state->cdclk.logical.voltage_level =
  1775. skl_calc_voltage_level(cdclk);
  1776. if (!intel_state->active_crtcs) {
  1777. cdclk = skl_calc_cdclk(0, vco);
  1778. intel_state->cdclk.actual.vco = vco;
  1779. intel_state->cdclk.actual.cdclk = cdclk;
  1780. intel_state->cdclk.actual.voltage_level =
  1781. skl_calc_voltage_level(cdclk);
  1782. } else {
  1783. intel_state->cdclk.actual =
  1784. intel_state->cdclk.logical;
  1785. }
  1786. return 0;
  1787. }
  1788. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  1789. {
  1790. struct drm_i915_private *dev_priv = to_i915(state->dev);
  1791. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  1792. int min_cdclk, cdclk, vco;
  1793. min_cdclk = intel_compute_min_cdclk(state);
  1794. if (min_cdclk < 0)
  1795. return min_cdclk;
  1796. if (IS_GEMINILAKE(dev_priv)) {
  1797. cdclk = glk_calc_cdclk(min_cdclk);
  1798. vco = glk_de_pll_vco(dev_priv, cdclk);
  1799. } else {
  1800. cdclk = bxt_calc_cdclk(min_cdclk);
  1801. vco = bxt_de_pll_vco(dev_priv, cdclk);
  1802. }
  1803. intel_state->cdclk.logical.vco = vco;
  1804. intel_state->cdclk.logical.cdclk = cdclk;
  1805. intel_state->cdclk.logical.voltage_level =
  1806. bxt_calc_voltage_level(cdclk);
  1807. if (!intel_state->active_crtcs) {
  1808. if (IS_GEMINILAKE(dev_priv)) {
  1809. cdclk = glk_calc_cdclk(0);
  1810. vco = glk_de_pll_vco(dev_priv, cdclk);
  1811. } else {
  1812. cdclk = bxt_calc_cdclk(0);
  1813. vco = bxt_de_pll_vco(dev_priv, cdclk);
  1814. }
  1815. intel_state->cdclk.actual.vco = vco;
  1816. intel_state->cdclk.actual.cdclk = cdclk;
  1817. intel_state->cdclk.actual.voltage_level =
  1818. bxt_calc_voltage_level(cdclk);
  1819. } else {
  1820. intel_state->cdclk.actual =
  1821. intel_state->cdclk.logical;
  1822. }
  1823. return 0;
  1824. }
  1825. static int cnl_modeset_calc_cdclk(struct drm_atomic_state *state)
  1826. {
  1827. struct drm_i915_private *dev_priv = to_i915(state->dev);
  1828. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  1829. int min_cdclk, cdclk, vco;
  1830. min_cdclk = intel_compute_min_cdclk(state);
  1831. if (min_cdclk < 0)
  1832. return min_cdclk;
  1833. cdclk = cnl_calc_cdclk(min_cdclk);
  1834. vco = cnl_cdclk_pll_vco(dev_priv, cdclk);
  1835. intel_state->cdclk.logical.vco = vco;
  1836. intel_state->cdclk.logical.cdclk = cdclk;
  1837. intel_state->cdclk.logical.voltage_level =
  1838. max(cnl_calc_voltage_level(cdclk),
  1839. cnl_compute_min_voltage_level(intel_state));
  1840. if (!intel_state->active_crtcs) {
  1841. cdclk = cnl_calc_cdclk(0);
  1842. vco = cnl_cdclk_pll_vco(dev_priv, cdclk);
  1843. intel_state->cdclk.actual.vco = vco;
  1844. intel_state->cdclk.actual.cdclk = cdclk;
  1845. intel_state->cdclk.actual.voltage_level =
  1846. cnl_calc_voltage_level(cdclk);
  1847. } else {
  1848. intel_state->cdclk.actual =
  1849. intel_state->cdclk.logical;
  1850. }
  1851. return 0;
  1852. }
  1853. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  1854. {
  1855. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  1856. if (INTEL_GEN(dev_priv) >= 10)
  1857. return 2 * max_cdclk_freq;
  1858. else if (IS_GEMINILAKE(dev_priv))
  1859. /*
  1860. * FIXME: Limiting to 99% as a temporary workaround. See
  1861. * intel_min_cdclk() for details.
  1862. */
  1863. return 2 * max_cdclk_freq * 99 / 100;
  1864. else if (IS_GEN9(dev_priv) ||
  1865. IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
  1866. return max_cdclk_freq;
  1867. else if (IS_CHERRYVIEW(dev_priv))
  1868. return max_cdclk_freq*95/100;
  1869. else if (INTEL_INFO(dev_priv)->gen < 4)
  1870. return 2*max_cdclk_freq*90/100;
  1871. else
  1872. return max_cdclk_freq*90/100;
  1873. }
  1874. /**
  1875. * intel_update_max_cdclk - Determine the maximum support CDCLK frequency
  1876. * @dev_priv: i915 device
  1877. *
  1878. * Determine the maximum CDCLK frequency the platform supports, and also
  1879. * derive the maximum dot clock frequency the maximum CDCLK frequency
  1880. * allows.
  1881. */
  1882. void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
  1883. {
  1884. if (IS_CANNONLAKE(dev_priv)) {
  1885. dev_priv->max_cdclk_freq = 528000;
  1886. } else if (IS_GEN9_BC(dev_priv)) {
  1887. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  1888. int max_cdclk, vco;
  1889. vco = dev_priv->skl_preferred_vco_freq;
  1890. WARN_ON(vco != 8100000 && vco != 8640000);
  1891. /*
  1892. * Use the lower (vco 8640) cdclk values as a
  1893. * first guess. skl_calc_cdclk() will correct it
  1894. * if the preferred vco is 8100 instead.
  1895. */
  1896. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  1897. max_cdclk = 617143;
  1898. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  1899. max_cdclk = 540000;
  1900. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  1901. max_cdclk = 432000;
  1902. else
  1903. max_cdclk = 308571;
  1904. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  1905. } else if (IS_GEMINILAKE(dev_priv)) {
  1906. dev_priv->max_cdclk_freq = 316800;
  1907. } else if (IS_BROXTON(dev_priv)) {
  1908. dev_priv->max_cdclk_freq = 624000;
  1909. } else if (IS_BROADWELL(dev_priv)) {
  1910. /*
  1911. * FIXME with extra cooling we can allow
  1912. * 540 MHz for ULX and 675 Mhz for ULT.
  1913. * How can we know if extra cooling is
  1914. * available? PCI ID, VTB, something else?
  1915. */
  1916. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  1917. dev_priv->max_cdclk_freq = 450000;
  1918. else if (IS_BDW_ULX(dev_priv))
  1919. dev_priv->max_cdclk_freq = 450000;
  1920. else if (IS_BDW_ULT(dev_priv))
  1921. dev_priv->max_cdclk_freq = 540000;
  1922. else
  1923. dev_priv->max_cdclk_freq = 675000;
  1924. } else if (IS_CHERRYVIEW(dev_priv)) {
  1925. dev_priv->max_cdclk_freq = 320000;
  1926. } else if (IS_VALLEYVIEW(dev_priv)) {
  1927. dev_priv->max_cdclk_freq = 400000;
  1928. } else {
  1929. /* otherwise assume cdclk is fixed */
  1930. dev_priv->max_cdclk_freq = dev_priv->cdclk.hw.cdclk;
  1931. }
  1932. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  1933. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  1934. dev_priv->max_cdclk_freq);
  1935. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  1936. dev_priv->max_dotclk_freq);
  1937. }
  1938. /**
  1939. * intel_update_cdclk - Determine the current CDCLK frequency
  1940. * @dev_priv: i915 device
  1941. *
  1942. * Determine the current CDCLK frequency.
  1943. */
  1944. void intel_update_cdclk(struct drm_i915_private *dev_priv)
  1945. {
  1946. dev_priv->display.get_cdclk(dev_priv, &dev_priv->cdclk.hw);
  1947. /*
  1948. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  1949. * Programmng [sic] note: bit[9:2] should be programmed to the number
  1950. * of cdclk that generates 4MHz reference clock freq which is used to
  1951. * generate GMBus clock. This will vary with the cdclk freq.
  1952. */
  1953. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1954. I915_WRITE(GMBUSFREQ_VLV,
  1955. DIV_ROUND_UP(dev_priv->cdclk.hw.cdclk, 1000));
  1956. }
  1957. static int cnp_rawclk(struct drm_i915_private *dev_priv)
  1958. {
  1959. u32 rawclk;
  1960. int divider, fraction;
  1961. if (I915_READ(SFUSE_STRAP) & SFUSE_STRAP_RAW_FREQUENCY) {
  1962. /* 24 MHz */
  1963. divider = 24000;
  1964. fraction = 0;
  1965. } else {
  1966. /* 19.2 MHz */
  1967. divider = 19000;
  1968. fraction = 200;
  1969. }
  1970. rawclk = CNP_RAWCLK_DIV((divider / 1000) - 1);
  1971. if (fraction)
  1972. rawclk |= CNP_RAWCLK_FRAC(DIV_ROUND_CLOSEST(1000,
  1973. fraction) - 1);
  1974. I915_WRITE(PCH_RAWCLK_FREQ, rawclk);
  1975. return divider + fraction;
  1976. }
  1977. static int pch_rawclk(struct drm_i915_private *dev_priv)
  1978. {
  1979. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  1980. }
  1981. static int vlv_hrawclk(struct drm_i915_private *dev_priv)
  1982. {
  1983. /* RAWCLK_FREQ_VLV register updated from power well code */
  1984. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  1985. CCK_DISPLAY_REF_CLOCK_CONTROL);
  1986. }
  1987. static int g4x_hrawclk(struct drm_i915_private *dev_priv)
  1988. {
  1989. uint32_t clkcfg;
  1990. /* hrawclock is 1/4 the FSB frequency */
  1991. clkcfg = I915_READ(CLKCFG);
  1992. switch (clkcfg & CLKCFG_FSB_MASK) {
  1993. case CLKCFG_FSB_400:
  1994. return 100000;
  1995. case CLKCFG_FSB_533:
  1996. return 133333;
  1997. case CLKCFG_FSB_667:
  1998. return 166667;
  1999. case CLKCFG_FSB_800:
  2000. return 200000;
  2001. case CLKCFG_FSB_1067:
  2002. case CLKCFG_FSB_1067_ALT:
  2003. return 266667;
  2004. case CLKCFG_FSB_1333:
  2005. case CLKCFG_FSB_1333_ALT:
  2006. return 333333;
  2007. default:
  2008. return 133333;
  2009. }
  2010. }
  2011. /**
  2012. * intel_update_rawclk - Determine the current RAWCLK frequency
  2013. * @dev_priv: i915 device
  2014. *
  2015. * Determine the current RAWCLK frequency. RAWCLK is a fixed
  2016. * frequency clock so this needs to done only once.
  2017. */
  2018. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  2019. {
  2020. if (HAS_PCH_CNP(dev_priv))
  2021. dev_priv->rawclk_freq = cnp_rawclk(dev_priv);
  2022. else if (HAS_PCH_SPLIT(dev_priv))
  2023. dev_priv->rawclk_freq = pch_rawclk(dev_priv);
  2024. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  2025. dev_priv->rawclk_freq = vlv_hrawclk(dev_priv);
  2026. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  2027. dev_priv->rawclk_freq = g4x_hrawclk(dev_priv);
  2028. else
  2029. /* no rawclk on other platforms, or no need to know it */
  2030. return;
  2031. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  2032. }
  2033. /**
  2034. * intel_init_cdclk_hooks - Initialize CDCLK related modesetting hooks
  2035. * @dev_priv: i915 device
  2036. */
  2037. void intel_init_cdclk_hooks(struct drm_i915_private *dev_priv)
  2038. {
  2039. if (IS_CHERRYVIEW(dev_priv)) {
  2040. dev_priv->display.set_cdclk = chv_set_cdclk;
  2041. dev_priv->display.modeset_calc_cdclk =
  2042. vlv_modeset_calc_cdclk;
  2043. } else if (IS_VALLEYVIEW(dev_priv)) {
  2044. dev_priv->display.set_cdclk = vlv_set_cdclk;
  2045. dev_priv->display.modeset_calc_cdclk =
  2046. vlv_modeset_calc_cdclk;
  2047. } else if (IS_BROADWELL(dev_priv)) {
  2048. dev_priv->display.set_cdclk = bdw_set_cdclk;
  2049. dev_priv->display.modeset_calc_cdclk =
  2050. bdw_modeset_calc_cdclk;
  2051. } else if (IS_GEN9_LP(dev_priv)) {
  2052. dev_priv->display.set_cdclk = bxt_set_cdclk;
  2053. dev_priv->display.modeset_calc_cdclk =
  2054. bxt_modeset_calc_cdclk;
  2055. } else if (IS_GEN9_BC(dev_priv)) {
  2056. dev_priv->display.set_cdclk = skl_set_cdclk;
  2057. dev_priv->display.modeset_calc_cdclk =
  2058. skl_modeset_calc_cdclk;
  2059. } else if (IS_CANNONLAKE(dev_priv)) {
  2060. dev_priv->display.set_cdclk = cnl_set_cdclk;
  2061. dev_priv->display.modeset_calc_cdclk =
  2062. cnl_modeset_calc_cdclk;
  2063. }
  2064. if (IS_CANNONLAKE(dev_priv))
  2065. dev_priv->display.get_cdclk = cnl_get_cdclk;
  2066. else if (IS_GEN9_BC(dev_priv))
  2067. dev_priv->display.get_cdclk = skl_get_cdclk;
  2068. else if (IS_GEN9_LP(dev_priv))
  2069. dev_priv->display.get_cdclk = bxt_get_cdclk;
  2070. else if (IS_BROADWELL(dev_priv))
  2071. dev_priv->display.get_cdclk = bdw_get_cdclk;
  2072. else if (IS_HASWELL(dev_priv))
  2073. dev_priv->display.get_cdclk = hsw_get_cdclk;
  2074. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  2075. dev_priv->display.get_cdclk = vlv_get_cdclk;
  2076. else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2077. dev_priv->display.get_cdclk = fixed_400mhz_get_cdclk;
  2078. else if (IS_GEN5(dev_priv))
  2079. dev_priv->display.get_cdclk = fixed_450mhz_get_cdclk;
  2080. else if (IS_GM45(dev_priv))
  2081. dev_priv->display.get_cdclk = gm45_get_cdclk;
  2082. else if (IS_G45(dev_priv))
  2083. dev_priv->display.get_cdclk = g33_get_cdclk;
  2084. else if (IS_I965GM(dev_priv))
  2085. dev_priv->display.get_cdclk = i965gm_get_cdclk;
  2086. else if (IS_I965G(dev_priv))
  2087. dev_priv->display.get_cdclk = fixed_400mhz_get_cdclk;
  2088. else if (IS_PINEVIEW(dev_priv))
  2089. dev_priv->display.get_cdclk = pnv_get_cdclk;
  2090. else if (IS_G33(dev_priv))
  2091. dev_priv->display.get_cdclk = g33_get_cdclk;
  2092. else if (IS_I945GM(dev_priv))
  2093. dev_priv->display.get_cdclk = i945gm_get_cdclk;
  2094. else if (IS_I945G(dev_priv))
  2095. dev_priv->display.get_cdclk = fixed_400mhz_get_cdclk;
  2096. else if (IS_I915GM(dev_priv))
  2097. dev_priv->display.get_cdclk = i915gm_get_cdclk;
  2098. else if (IS_I915G(dev_priv))
  2099. dev_priv->display.get_cdclk = fixed_333mhz_get_cdclk;
  2100. else if (IS_I865G(dev_priv))
  2101. dev_priv->display.get_cdclk = fixed_266mhz_get_cdclk;
  2102. else if (IS_I85X(dev_priv))
  2103. dev_priv->display.get_cdclk = i85x_get_cdclk;
  2104. else if (IS_I845G(dev_priv))
  2105. dev_priv->display.get_cdclk = fixed_200mhz_get_cdclk;
  2106. else { /* 830 */
  2107. WARN(!IS_I830(dev_priv),
  2108. "Unknown platform. Assuming 133 MHz CDCLK\n");
  2109. dev_priv->display.get_cdclk = fixed_133mhz_get_cdclk;
  2110. }
  2111. }