intel_dpll_mgr.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  1. /*
  2. * Copyright © 2006-2016 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: Display PLLs
  26. *
  27. * Display PLLs used for driving outputs vary by platform. While some have
  28. * per-pipe or per-encoder dedicated PLLs, others allow the use of any PLL
  29. * from a pool. In the latter scenario, it is possible that multiple pipes
  30. * share a PLL if their configurations match.
  31. *
  32. * This file provides an abstraction over display PLLs. The function
  33. * intel_shared_dpll_init() initializes the PLLs for the given platform. The
  34. * users of a PLL are tracked and that tracking is integrated with the atomic
  35. * modest interface. During an atomic operation, a PLL can be requested for a
  36. * given CRTC and encoder configuration by calling intel_get_shared_dpll() and
  37. * a previously used PLL can be released with intel_release_shared_dpll().
  38. * Changes to the users are first staged in the atomic state, and then made
  39. * effective by calling intel_shared_dpll_swap_state() during the atomic
  40. * commit phase.
  41. */
  42. static void
  43. intel_atomic_duplicate_dpll_state(struct drm_i915_private *dev_priv,
  44. struct intel_shared_dpll_state *shared_dpll)
  45. {
  46. enum intel_dpll_id i;
  47. /* Copy shared dpll state */
  48. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  49. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  50. shared_dpll[i] = pll->state;
  51. }
  52. }
  53. static struct intel_shared_dpll_state *
  54. intel_atomic_get_shared_dpll_state(struct drm_atomic_state *s)
  55. {
  56. struct intel_atomic_state *state = to_intel_atomic_state(s);
  57. WARN_ON(!drm_modeset_is_locked(&s->dev->mode_config.connection_mutex));
  58. if (!state->dpll_set) {
  59. state->dpll_set = true;
  60. intel_atomic_duplicate_dpll_state(to_i915(s->dev),
  61. state->shared_dpll);
  62. }
  63. return state->shared_dpll;
  64. }
  65. /**
  66. * intel_get_shared_dpll_by_id - get a DPLL given its id
  67. * @dev_priv: i915 device instance
  68. * @id: pll id
  69. *
  70. * Returns:
  71. * A pointer to the DPLL with @id
  72. */
  73. struct intel_shared_dpll *
  74. intel_get_shared_dpll_by_id(struct drm_i915_private *dev_priv,
  75. enum intel_dpll_id id)
  76. {
  77. return &dev_priv->shared_dplls[id];
  78. }
  79. /**
  80. * intel_get_shared_dpll_id - get the id of a DPLL
  81. * @dev_priv: i915 device instance
  82. * @pll: the DPLL
  83. *
  84. * Returns:
  85. * The id of @pll
  86. */
  87. enum intel_dpll_id
  88. intel_get_shared_dpll_id(struct drm_i915_private *dev_priv,
  89. struct intel_shared_dpll *pll)
  90. {
  91. if (WARN_ON(pll < dev_priv->shared_dplls||
  92. pll > &dev_priv->shared_dplls[dev_priv->num_shared_dpll]))
  93. return -1;
  94. return (enum intel_dpll_id) (pll - dev_priv->shared_dplls);
  95. }
  96. /* For ILK+ */
  97. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  98. struct intel_shared_dpll *pll,
  99. bool state)
  100. {
  101. bool cur_state;
  102. struct intel_dpll_hw_state hw_state;
  103. if (WARN(!pll, "asserting DPLL %s with no DPLL\n", onoff(state)))
  104. return;
  105. cur_state = pll->funcs.get_hw_state(dev_priv, pll, &hw_state);
  106. I915_STATE_WARN(cur_state != state,
  107. "%s assertion failure (expected %s, current %s)\n",
  108. pll->name, onoff(state), onoff(cur_state));
  109. }
  110. /**
  111. * intel_prepare_shared_dpll - call a dpll's prepare hook
  112. * @crtc: CRTC which has a shared dpll
  113. *
  114. * This calls the PLL's prepare hook if it has one and if the PLL is not
  115. * already enabled. The prepare hook is platform specific.
  116. */
  117. void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  118. {
  119. struct drm_device *dev = crtc->base.dev;
  120. struct drm_i915_private *dev_priv = to_i915(dev);
  121. struct intel_shared_dpll *pll = crtc->config->shared_dpll;
  122. if (WARN_ON(pll == NULL))
  123. return;
  124. mutex_lock(&dev_priv->dpll_lock);
  125. WARN_ON(!pll->state.crtc_mask);
  126. if (!pll->active_mask) {
  127. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  128. WARN_ON(pll->on);
  129. assert_shared_dpll_disabled(dev_priv, pll);
  130. pll->funcs.prepare(dev_priv, pll);
  131. }
  132. mutex_unlock(&dev_priv->dpll_lock);
  133. }
  134. /**
  135. * intel_enable_shared_dpll - enable a CRTC's shared DPLL
  136. * @crtc: CRTC which has a shared DPLL
  137. *
  138. * Enable the shared DPLL used by @crtc.
  139. */
  140. void intel_enable_shared_dpll(struct intel_crtc *crtc)
  141. {
  142. struct drm_device *dev = crtc->base.dev;
  143. struct drm_i915_private *dev_priv = to_i915(dev);
  144. struct intel_shared_dpll *pll = crtc->config->shared_dpll;
  145. unsigned crtc_mask = 1 << drm_crtc_index(&crtc->base);
  146. unsigned old_mask;
  147. if (WARN_ON(pll == NULL))
  148. return;
  149. mutex_lock(&dev_priv->dpll_lock);
  150. old_mask = pll->active_mask;
  151. if (WARN_ON(!(pll->state.crtc_mask & crtc_mask)) ||
  152. WARN_ON(pll->active_mask & crtc_mask))
  153. goto out;
  154. pll->active_mask |= crtc_mask;
  155. DRM_DEBUG_KMS("enable %s (active %x, on? %d) for crtc %d\n",
  156. pll->name, pll->active_mask, pll->on,
  157. crtc->base.base.id);
  158. if (old_mask) {
  159. WARN_ON(!pll->on);
  160. assert_shared_dpll_enabled(dev_priv, pll);
  161. goto out;
  162. }
  163. WARN_ON(pll->on);
  164. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  165. pll->funcs.enable(dev_priv, pll);
  166. pll->on = true;
  167. out:
  168. mutex_unlock(&dev_priv->dpll_lock);
  169. }
  170. /**
  171. * intel_disable_shared_dpll - disable a CRTC's shared DPLL
  172. * @crtc: CRTC which has a shared DPLL
  173. *
  174. * Disable the shared DPLL used by @crtc.
  175. */
  176. void intel_disable_shared_dpll(struct intel_crtc *crtc)
  177. {
  178. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  179. struct intel_shared_dpll *pll = crtc->config->shared_dpll;
  180. unsigned crtc_mask = 1 << drm_crtc_index(&crtc->base);
  181. /* PCH only available on ILK+ */
  182. if (INTEL_GEN(dev_priv) < 5)
  183. return;
  184. if (pll == NULL)
  185. return;
  186. mutex_lock(&dev_priv->dpll_lock);
  187. if (WARN_ON(!(pll->active_mask & crtc_mask)))
  188. goto out;
  189. DRM_DEBUG_KMS("disable %s (active %x, on? %d) for crtc %d\n",
  190. pll->name, pll->active_mask, pll->on,
  191. crtc->base.base.id);
  192. assert_shared_dpll_enabled(dev_priv, pll);
  193. WARN_ON(!pll->on);
  194. pll->active_mask &= ~crtc_mask;
  195. if (pll->active_mask)
  196. goto out;
  197. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  198. pll->funcs.disable(dev_priv, pll);
  199. pll->on = false;
  200. out:
  201. mutex_unlock(&dev_priv->dpll_lock);
  202. }
  203. static struct intel_shared_dpll *
  204. intel_find_shared_dpll(struct intel_crtc *crtc,
  205. struct intel_crtc_state *crtc_state,
  206. enum intel_dpll_id range_min,
  207. enum intel_dpll_id range_max)
  208. {
  209. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  210. struct intel_shared_dpll *pll;
  211. struct intel_shared_dpll_state *shared_dpll;
  212. enum intel_dpll_id i;
  213. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  214. for (i = range_min; i <= range_max; i++) {
  215. pll = &dev_priv->shared_dplls[i];
  216. /* Only want to check enabled timings first */
  217. if (shared_dpll[i].crtc_mask == 0)
  218. continue;
  219. if (memcmp(&crtc_state->dpll_hw_state,
  220. &shared_dpll[i].hw_state,
  221. sizeof(crtc_state->dpll_hw_state)) == 0) {
  222. DRM_DEBUG_KMS("[CRTC:%d:%s] sharing existing %s (crtc mask 0x%08x, active %x)\n",
  223. crtc->base.base.id, crtc->base.name, pll->name,
  224. shared_dpll[i].crtc_mask,
  225. pll->active_mask);
  226. return pll;
  227. }
  228. }
  229. /* Ok no matching timings, maybe there's a free one? */
  230. for (i = range_min; i <= range_max; i++) {
  231. pll = &dev_priv->shared_dplls[i];
  232. if (shared_dpll[i].crtc_mask == 0) {
  233. DRM_DEBUG_KMS("[CRTC:%d:%s] allocated %s\n",
  234. crtc->base.base.id, crtc->base.name, pll->name);
  235. return pll;
  236. }
  237. }
  238. return NULL;
  239. }
  240. static void
  241. intel_reference_shared_dpll(struct intel_shared_dpll *pll,
  242. struct intel_crtc_state *crtc_state)
  243. {
  244. struct intel_shared_dpll_state *shared_dpll;
  245. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  246. enum intel_dpll_id i = pll->id;
  247. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  248. if (shared_dpll[i].crtc_mask == 0)
  249. shared_dpll[i].hw_state =
  250. crtc_state->dpll_hw_state;
  251. crtc_state->shared_dpll = pll;
  252. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  253. pipe_name(crtc->pipe));
  254. shared_dpll[pll->id].crtc_mask |= 1 << crtc->pipe;
  255. }
  256. /**
  257. * intel_shared_dpll_swap_state - make atomic DPLL configuration effective
  258. * @state: atomic state
  259. *
  260. * This is the dpll version of drm_atomic_helper_swap_state() since the
  261. * helper does not handle driver-specific global state.
  262. *
  263. * For consistency with atomic helpers this function does a complete swap,
  264. * i.e. it also puts the current state into @state, even though there is no
  265. * need for that at this moment.
  266. */
  267. void intel_shared_dpll_swap_state(struct drm_atomic_state *state)
  268. {
  269. struct drm_i915_private *dev_priv = to_i915(state->dev);
  270. struct intel_shared_dpll_state *shared_dpll;
  271. struct intel_shared_dpll *pll;
  272. enum intel_dpll_id i;
  273. if (!to_intel_atomic_state(state)->dpll_set)
  274. return;
  275. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  276. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  277. struct intel_shared_dpll_state tmp;
  278. pll = &dev_priv->shared_dplls[i];
  279. tmp = pll->state;
  280. pll->state = shared_dpll[i];
  281. shared_dpll[i] = tmp;
  282. }
  283. }
  284. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  285. struct intel_shared_dpll *pll,
  286. struct intel_dpll_hw_state *hw_state)
  287. {
  288. uint32_t val;
  289. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
  290. return false;
  291. val = I915_READ(PCH_DPLL(pll->id));
  292. hw_state->dpll = val;
  293. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  294. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  295. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  296. return val & DPLL_VCO_ENABLE;
  297. }
  298. static void ibx_pch_dpll_prepare(struct drm_i915_private *dev_priv,
  299. struct intel_shared_dpll *pll)
  300. {
  301. I915_WRITE(PCH_FP0(pll->id), pll->state.hw_state.fp0);
  302. I915_WRITE(PCH_FP1(pll->id), pll->state.hw_state.fp1);
  303. }
  304. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  305. {
  306. u32 val;
  307. bool enabled;
  308. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)));
  309. val = I915_READ(PCH_DREF_CONTROL);
  310. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  311. DREF_SUPERSPREAD_SOURCE_MASK));
  312. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  313. }
  314. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  315. struct intel_shared_dpll *pll)
  316. {
  317. /* PCH refclock must be enabled first */
  318. ibx_assert_pch_refclk_enabled(dev_priv);
  319. I915_WRITE(PCH_DPLL(pll->id), pll->state.hw_state.dpll);
  320. /* Wait for the clocks to stabilize. */
  321. POSTING_READ(PCH_DPLL(pll->id));
  322. udelay(150);
  323. /* The pixel multiplier can only be updated once the
  324. * DPLL is enabled and the clocks are stable.
  325. *
  326. * So write it again.
  327. */
  328. I915_WRITE(PCH_DPLL(pll->id), pll->state.hw_state.dpll);
  329. POSTING_READ(PCH_DPLL(pll->id));
  330. udelay(200);
  331. }
  332. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  333. struct intel_shared_dpll *pll)
  334. {
  335. struct drm_device *dev = &dev_priv->drm;
  336. struct intel_crtc *crtc;
  337. /* Make sure no transcoder isn't still depending on us. */
  338. for_each_intel_crtc(dev, crtc) {
  339. if (crtc->config->shared_dpll == pll)
  340. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  341. }
  342. I915_WRITE(PCH_DPLL(pll->id), 0);
  343. POSTING_READ(PCH_DPLL(pll->id));
  344. udelay(200);
  345. }
  346. static struct intel_shared_dpll *
  347. ibx_get_dpll(struct intel_crtc *crtc, struct intel_crtc_state *crtc_state,
  348. struct intel_encoder *encoder)
  349. {
  350. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  351. struct intel_shared_dpll *pll;
  352. enum intel_dpll_id i;
  353. if (HAS_PCH_IBX(dev_priv)) {
  354. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  355. i = (enum intel_dpll_id) crtc->pipe;
  356. pll = &dev_priv->shared_dplls[i];
  357. DRM_DEBUG_KMS("[CRTC:%d:%s] using pre-allocated %s\n",
  358. crtc->base.base.id, crtc->base.name, pll->name);
  359. } else {
  360. pll = intel_find_shared_dpll(crtc, crtc_state,
  361. DPLL_ID_PCH_PLL_A,
  362. DPLL_ID_PCH_PLL_B);
  363. }
  364. if (!pll)
  365. return NULL;
  366. /* reference the pll */
  367. intel_reference_shared_dpll(pll, crtc_state);
  368. return pll;
  369. }
  370. static void ibx_dump_hw_state(struct drm_i915_private *dev_priv,
  371. struct intel_dpll_hw_state *hw_state)
  372. {
  373. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  374. "fp0: 0x%x, fp1: 0x%x\n",
  375. hw_state->dpll,
  376. hw_state->dpll_md,
  377. hw_state->fp0,
  378. hw_state->fp1);
  379. }
  380. static const struct intel_shared_dpll_funcs ibx_pch_dpll_funcs = {
  381. .prepare = ibx_pch_dpll_prepare,
  382. .enable = ibx_pch_dpll_enable,
  383. .disable = ibx_pch_dpll_disable,
  384. .get_hw_state = ibx_pch_dpll_get_hw_state,
  385. };
  386. static void hsw_ddi_wrpll_enable(struct drm_i915_private *dev_priv,
  387. struct intel_shared_dpll *pll)
  388. {
  389. I915_WRITE(WRPLL_CTL(pll->id), pll->state.hw_state.wrpll);
  390. POSTING_READ(WRPLL_CTL(pll->id));
  391. udelay(20);
  392. }
  393. static void hsw_ddi_spll_enable(struct drm_i915_private *dev_priv,
  394. struct intel_shared_dpll *pll)
  395. {
  396. I915_WRITE(SPLL_CTL, pll->state.hw_state.spll);
  397. POSTING_READ(SPLL_CTL);
  398. udelay(20);
  399. }
  400. static void hsw_ddi_wrpll_disable(struct drm_i915_private *dev_priv,
  401. struct intel_shared_dpll *pll)
  402. {
  403. uint32_t val;
  404. val = I915_READ(WRPLL_CTL(pll->id));
  405. I915_WRITE(WRPLL_CTL(pll->id), val & ~WRPLL_PLL_ENABLE);
  406. POSTING_READ(WRPLL_CTL(pll->id));
  407. }
  408. static void hsw_ddi_spll_disable(struct drm_i915_private *dev_priv,
  409. struct intel_shared_dpll *pll)
  410. {
  411. uint32_t val;
  412. val = I915_READ(SPLL_CTL);
  413. I915_WRITE(SPLL_CTL, val & ~SPLL_PLL_ENABLE);
  414. POSTING_READ(SPLL_CTL);
  415. }
  416. static bool hsw_ddi_wrpll_get_hw_state(struct drm_i915_private *dev_priv,
  417. struct intel_shared_dpll *pll,
  418. struct intel_dpll_hw_state *hw_state)
  419. {
  420. uint32_t val;
  421. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
  422. return false;
  423. val = I915_READ(WRPLL_CTL(pll->id));
  424. hw_state->wrpll = val;
  425. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  426. return val & WRPLL_PLL_ENABLE;
  427. }
  428. static bool hsw_ddi_spll_get_hw_state(struct drm_i915_private *dev_priv,
  429. struct intel_shared_dpll *pll,
  430. struct intel_dpll_hw_state *hw_state)
  431. {
  432. uint32_t val;
  433. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
  434. return false;
  435. val = I915_READ(SPLL_CTL);
  436. hw_state->spll = val;
  437. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  438. return val & SPLL_PLL_ENABLE;
  439. }
  440. #define LC_FREQ 2700
  441. #define LC_FREQ_2K U64_C(LC_FREQ * 2000)
  442. #define P_MIN 2
  443. #define P_MAX 64
  444. #define P_INC 2
  445. /* Constraints for PLL good behavior */
  446. #define REF_MIN 48
  447. #define REF_MAX 400
  448. #define VCO_MIN 2400
  449. #define VCO_MAX 4800
  450. struct hsw_wrpll_rnp {
  451. unsigned p, n2, r2;
  452. };
  453. static unsigned hsw_wrpll_get_budget_for_freq(int clock)
  454. {
  455. unsigned budget;
  456. switch (clock) {
  457. case 25175000:
  458. case 25200000:
  459. case 27000000:
  460. case 27027000:
  461. case 37762500:
  462. case 37800000:
  463. case 40500000:
  464. case 40541000:
  465. case 54000000:
  466. case 54054000:
  467. case 59341000:
  468. case 59400000:
  469. case 72000000:
  470. case 74176000:
  471. case 74250000:
  472. case 81000000:
  473. case 81081000:
  474. case 89012000:
  475. case 89100000:
  476. case 108000000:
  477. case 108108000:
  478. case 111264000:
  479. case 111375000:
  480. case 148352000:
  481. case 148500000:
  482. case 162000000:
  483. case 162162000:
  484. case 222525000:
  485. case 222750000:
  486. case 296703000:
  487. case 297000000:
  488. budget = 0;
  489. break;
  490. case 233500000:
  491. case 245250000:
  492. case 247750000:
  493. case 253250000:
  494. case 298000000:
  495. budget = 1500;
  496. break;
  497. case 169128000:
  498. case 169500000:
  499. case 179500000:
  500. case 202000000:
  501. budget = 2000;
  502. break;
  503. case 256250000:
  504. case 262500000:
  505. case 270000000:
  506. case 272500000:
  507. case 273750000:
  508. case 280750000:
  509. case 281250000:
  510. case 286000000:
  511. case 291750000:
  512. budget = 4000;
  513. break;
  514. case 267250000:
  515. case 268500000:
  516. budget = 5000;
  517. break;
  518. default:
  519. budget = 1000;
  520. break;
  521. }
  522. return budget;
  523. }
  524. static void hsw_wrpll_update_rnp(uint64_t freq2k, unsigned budget,
  525. unsigned r2, unsigned n2, unsigned p,
  526. struct hsw_wrpll_rnp *best)
  527. {
  528. uint64_t a, b, c, d, diff, diff_best;
  529. /* No best (r,n,p) yet */
  530. if (best->p == 0) {
  531. best->p = p;
  532. best->n2 = n2;
  533. best->r2 = r2;
  534. return;
  535. }
  536. /*
  537. * Output clock is (LC_FREQ_2K / 2000) * N / (P * R), which compares to
  538. * freq2k.
  539. *
  540. * delta = 1e6 *
  541. * abs(freq2k - (LC_FREQ_2K * n2/(p * r2))) /
  542. * freq2k;
  543. *
  544. * and we would like delta <= budget.
  545. *
  546. * If the discrepancy is above the PPM-based budget, always prefer to
  547. * improve upon the previous solution. However, if you're within the
  548. * budget, try to maximize Ref * VCO, that is N / (P * R^2).
  549. */
  550. a = freq2k * budget * p * r2;
  551. b = freq2k * budget * best->p * best->r2;
  552. diff = abs_diff(freq2k * p * r2, LC_FREQ_2K * n2);
  553. diff_best = abs_diff(freq2k * best->p * best->r2,
  554. LC_FREQ_2K * best->n2);
  555. c = 1000000 * diff;
  556. d = 1000000 * diff_best;
  557. if (a < c && b < d) {
  558. /* If both are above the budget, pick the closer */
  559. if (best->p * best->r2 * diff < p * r2 * diff_best) {
  560. best->p = p;
  561. best->n2 = n2;
  562. best->r2 = r2;
  563. }
  564. } else if (a >= c && b < d) {
  565. /* If A is below the threshold but B is above it? Update. */
  566. best->p = p;
  567. best->n2 = n2;
  568. best->r2 = r2;
  569. } else if (a >= c && b >= d) {
  570. /* Both are below the limit, so pick the higher n2/(r2*r2) */
  571. if (n2 * best->r2 * best->r2 > best->n2 * r2 * r2) {
  572. best->p = p;
  573. best->n2 = n2;
  574. best->r2 = r2;
  575. }
  576. }
  577. /* Otherwise a < c && b >= d, do nothing */
  578. }
  579. static void
  580. hsw_ddi_calculate_wrpll(int clock /* in Hz */,
  581. unsigned *r2_out, unsigned *n2_out, unsigned *p_out)
  582. {
  583. uint64_t freq2k;
  584. unsigned p, n2, r2;
  585. struct hsw_wrpll_rnp best = { 0, 0, 0 };
  586. unsigned budget;
  587. freq2k = clock / 100;
  588. budget = hsw_wrpll_get_budget_for_freq(clock);
  589. /* Special case handling for 540 pixel clock: bypass WR PLL entirely
  590. * and directly pass the LC PLL to it. */
  591. if (freq2k == 5400000) {
  592. *n2_out = 2;
  593. *p_out = 1;
  594. *r2_out = 2;
  595. return;
  596. }
  597. /*
  598. * Ref = LC_FREQ / R, where Ref is the actual reference input seen by
  599. * the WR PLL.
  600. *
  601. * We want R so that REF_MIN <= Ref <= REF_MAX.
  602. * Injecting R2 = 2 * R gives:
  603. * REF_MAX * r2 > LC_FREQ * 2 and
  604. * REF_MIN * r2 < LC_FREQ * 2
  605. *
  606. * Which means the desired boundaries for r2 are:
  607. * LC_FREQ * 2 / REF_MAX < r2 < LC_FREQ * 2 / REF_MIN
  608. *
  609. */
  610. for (r2 = LC_FREQ * 2 / REF_MAX + 1;
  611. r2 <= LC_FREQ * 2 / REF_MIN;
  612. r2++) {
  613. /*
  614. * VCO = N * Ref, that is: VCO = N * LC_FREQ / R
  615. *
  616. * Once again we want VCO_MIN <= VCO <= VCO_MAX.
  617. * Injecting R2 = 2 * R and N2 = 2 * N, we get:
  618. * VCO_MAX * r2 > n2 * LC_FREQ and
  619. * VCO_MIN * r2 < n2 * LC_FREQ)
  620. *
  621. * Which means the desired boundaries for n2 are:
  622. * VCO_MIN * r2 / LC_FREQ < n2 < VCO_MAX * r2 / LC_FREQ
  623. */
  624. for (n2 = VCO_MIN * r2 / LC_FREQ + 1;
  625. n2 <= VCO_MAX * r2 / LC_FREQ;
  626. n2++) {
  627. for (p = P_MIN; p <= P_MAX; p += P_INC)
  628. hsw_wrpll_update_rnp(freq2k, budget,
  629. r2, n2, p, &best);
  630. }
  631. }
  632. *n2_out = best.n2;
  633. *p_out = best.p;
  634. *r2_out = best.r2;
  635. }
  636. static struct intel_shared_dpll *hsw_ddi_hdmi_get_dpll(int clock,
  637. struct intel_crtc *crtc,
  638. struct intel_crtc_state *crtc_state)
  639. {
  640. struct intel_shared_dpll *pll;
  641. uint32_t val;
  642. unsigned int p, n2, r2;
  643. hsw_ddi_calculate_wrpll(clock * 1000, &r2, &n2, &p);
  644. val = WRPLL_PLL_ENABLE | WRPLL_PLL_LCPLL |
  645. WRPLL_DIVIDER_REFERENCE(r2) | WRPLL_DIVIDER_FEEDBACK(n2) |
  646. WRPLL_DIVIDER_POST(p);
  647. crtc_state->dpll_hw_state.wrpll = val;
  648. pll = intel_find_shared_dpll(crtc, crtc_state,
  649. DPLL_ID_WRPLL1, DPLL_ID_WRPLL2);
  650. if (!pll)
  651. return NULL;
  652. return pll;
  653. }
  654. static struct intel_shared_dpll *
  655. hsw_ddi_dp_get_dpll(struct intel_encoder *encoder, int clock)
  656. {
  657. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  658. struct intel_shared_dpll *pll;
  659. enum intel_dpll_id pll_id;
  660. switch (clock / 2) {
  661. case 81000:
  662. pll_id = DPLL_ID_LCPLL_810;
  663. break;
  664. case 135000:
  665. pll_id = DPLL_ID_LCPLL_1350;
  666. break;
  667. case 270000:
  668. pll_id = DPLL_ID_LCPLL_2700;
  669. break;
  670. default:
  671. DRM_DEBUG_KMS("Invalid clock for DP: %d\n", clock);
  672. return NULL;
  673. }
  674. pll = intel_get_shared_dpll_by_id(dev_priv, pll_id);
  675. if (!pll)
  676. return NULL;
  677. return pll;
  678. }
  679. static struct intel_shared_dpll *
  680. hsw_get_dpll(struct intel_crtc *crtc, struct intel_crtc_state *crtc_state,
  681. struct intel_encoder *encoder)
  682. {
  683. struct intel_shared_dpll *pll;
  684. int clock = crtc_state->port_clock;
  685. memset(&crtc_state->dpll_hw_state, 0,
  686. sizeof(crtc_state->dpll_hw_state));
  687. if (encoder->type == INTEL_OUTPUT_HDMI) {
  688. pll = hsw_ddi_hdmi_get_dpll(clock, crtc, crtc_state);
  689. } else if (encoder->type == INTEL_OUTPUT_DP ||
  690. encoder->type == INTEL_OUTPUT_DP_MST ||
  691. encoder->type == INTEL_OUTPUT_EDP) {
  692. pll = hsw_ddi_dp_get_dpll(encoder, clock);
  693. } else if (encoder->type == INTEL_OUTPUT_ANALOG) {
  694. if (WARN_ON(crtc_state->port_clock / 2 != 135000))
  695. return NULL;
  696. crtc_state->dpll_hw_state.spll =
  697. SPLL_PLL_ENABLE | SPLL_PLL_FREQ_1350MHz | SPLL_PLL_SSC;
  698. pll = intel_find_shared_dpll(crtc, crtc_state,
  699. DPLL_ID_SPLL, DPLL_ID_SPLL);
  700. } else {
  701. return NULL;
  702. }
  703. if (!pll)
  704. return NULL;
  705. intel_reference_shared_dpll(pll, crtc_state);
  706. return pll;
  707. }
  708. static void hsw_dump_hw_state(struct drm_i915_private *dev_priv,
  709. struct intel_dpll_hw_state *hw_state)
  710. {
  711. DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  712. hw_state->wrpll, hw_state->spll);
  713. }
  714. static const struct intel_shared_dpll_funcs hsw_ddi_wrpll_funcs = {
  715. .enable = hsw_ddi_wrpll_enable,
  716. .disable = hsw_ddi_wrpll_disable,
  717. .get_hw_state = hsw_ddi_wrpll_get_hw_state,
  718. };
  719. static const struct intel_shared_dpll_funcs hsw_ddi_spll_funcs = {
  720. .enable = hsw_ddi_spll_enable,
  721. .disable = hsw_ddi_spll_disable,
  722. .get_hw_state = hsw_ddi_spll_get_hw_state,
  723. };
  724. static void hsw_ddi_lcpll_enable(struct drm_i915_private *dev_priv,
  725. struct intel_shared_dpll *pll)
  726. {
  727. }
  728. static void hsw_ddi_lcpll_disable(struct drm_i915_private *dev_priv,
  729. struct intel_shared_dpll *pll)
  730. {
  731. }
  732. static bool hsw_ddi_lcpll_get_hw_state(struct drm_i915_private *dev_priv,
  733. struct intel_shared_dpll *pll,
  734. struct intel_dpll_hw_state *hw_state)
  735. {
  736. return true;
  737. }
  738. static const struct intel_shared_dpll_funcs hsw_ddi_lcpll_funcs = {
  739. .enable = hsw_ddi_lcpll_enable,
  740. .disable = hsw_ddi_lcpll_disable,
  741. .get_hw_state = hsw_ddi_lcpll_get_hw_state,
  742. };
  743. struct skl_dpll_regs {
  744. i915_reg_t ctl, cfgcr1, cfgcr2;
  745. };
  746. /* this array is indexed by the *shared* pll id */
  747. static const struct skl_dpll_regs skl_dpll_regs[4] = {
  748. {
  749. /* DPLL 0 */
  750. .ctl = LCPLL1_CTL,
  751. /* DPLL 0 doesn't support HDMI mode */
  752. },
  753. {
  754. /* DPLL 1 */
  755. .ctl = LCPLL2_CTL,
  756. .cfgcr1 = DPLL_CFGCR1(SKL_DPLL1),
  757. .cfgcr2 = DPLL_CFGCR2(SKL_DPLL1),
  758. },
  759. {
  760. /* DPLL 2 */
  761. .ctl = WRPLL_CTL(0),
  762. .cfgcr1 = DPLL_CFGCR1(SKL_DPLL2),
  763. .cfgcr2 = DPLL_CFGCR2(SKL_DPLL2),
  764. },
  765. {
  766. /* DPLL 3 */
  767. .ctl = WRPLL_CTL(1),
  768. .cfgcr1 = DPLL_CFGCR1(SKL_DPLL3),
  769. .cfgcr2 = DPLL_CFGCR2(SKL_DPLL3),
  770. },
  771. };
  772. static void skl_ddi_pll_write_ctrl1(struct drm_i915_private *dev_priv,
  773. struct intel_shared_dpll *pll)
  774. {
  775. uint32_t val;
  776. val = I915_READ(DPLL_CTRL1);
  777. val &= ~(DPLL_CTRL1_HDMI_MODE(pll->id) | DPLL_CTRL1_SSC(pll->id) |
  778. DPLL_CTRL1_LINK_RATE_MASK(pll->id));
  779. val |= pll->state.hw_state.ctrl1 << (pll->id * 6);
  780. I915_WRITE(DPLL_CTRL1, val);
  781. POSTING_READ(DPLL_CTRL1);
  782. }
  783. static void skl_ddi_pll_enable(struct drm_i915_private *dev_priv,
  784. struct intel_shared_dpll *pll)
  785. {
  786. const struct skl_dpll_regs *regs = skl_dpll_regs;
  787. skl_ddi_pll_write_ctrl1(dev_priv, pll);
  788. I915_WRITE(regs[pll->id].cfgcr1, pll->state.hw_state.cfgcr1);
  789. I915_WRITE(regs[pll->id].cfgcr2, pll->state.hw_state.cfgcr2);
  790. POSTING_READ(regs[pll->id].cfgcr1);
  791. POSTING_READ(regs[pll->id].cfgcr2);
  792. /* the enable bit is always bit 31 */
  793. I915_WRITE(regs[pll->id].ctl,
  794. I915_READ(regs[pll->id].ctl) | LCPLL_PLL_ENABLE);
  795. if (intel_wait_for_register(dev_priv,
  796. DPLL_STATUS,
  797. DPLL_LOCK(pll->id),
  798. DPLL_LOCK(pll->id),
  799. 5))
  800. DRM_ERROR("DPLL %d not locked\n", pll->id);
  801. }
  802. static void skl_ddi_dpll0_enable(struct drm_i915_private *dev_priv,
  803. struct intel_shared_dpll *pll)
  804. {
  805. skl_ddi_pll_write_ctrl1(dev_priv, pll);
  806. }
  807. static void skl_ddi_pll_disable(struct drm_i915_private *dev_priv,
  808. struct intel_shared_dpll *pll)
  809. {
  810. const struct skl_dpll_regs *regs = skl_dpll_regs;
  811. /* the enable bit is always bit 31 */
  812. I915_WRITE(regs[pll->id].ctl,
  813. I915_READ(regs[pll->id].ctl) & ~LCPLL_PLL_ENABLE);
  814. POSTING_READ(regs[pll->id].ctl);
  815. }
  816. static void skl_ddi_dpll0_disable(struct drm_i915_private *dev_priv,
  817. struct intel_shared_dpll *pll)
  818. {
  819. }
  820. static bool skl_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
  821. struct intel_shared_dpll *pll,
  822. struct intel_dpll_hw_state *hw_state)
  823. {
  824. uint32_t val;
  825. const struct skl_dpll_regs *regs = skl_dpll_regs;
  826. bool ret;
  827. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
  828. return false;
  829. ret = false;
  830. val = I915_READ(regs[pll->id].ctl);
  831. if (!(val & LCPLL_PLL_ENABLE))
  832. goto out;
  833. val = I915_READ(DPLL_CTRL1);
  834. hw_state->ctrl1 = (val >> (pll->id * 6)) & 0x3f;
  835. /* avoid reading back stale values if HDMI mode is not enabled */
  836. if (val & DPLL_CTRL1_HDMI_MODE(pll->id)) {
  837. hw_state->cfgcr1 = I915_READ(regs[pll->id].cfgcr1);
  838. hw_state->cfgcr2 = I915_READ(regs[pll->id].cfgcr2);
  839. }
  840. ret = true;
  841. out:
  842. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  843. return ret;
  844. }
  845. static bool skl_ddi_dpll0_get_hw_state(struct drm_i915_private *dev_priv,
  846. struct intel_shared_dpll *pll,
  847. struct intel_dpll_hw_state *hw_state)
  848. {
  849. uint32_t val;
  850. const struct skl_dpll_regs *regs = skl_dpll_regs;
  851. bool ret;
  852. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
  853. return false;
  854. ret = false;
  855. /* DPLL0 is always enabled since it drives CDCLK */
  856. val = I915_READ(regs[pll->id].ctl);
  857. if (WARN_ON(!(val & LCPLL_PLL_ENABLE)))
  858. goto out;
  859. val = I915_READ(DPLL_CTRL1);
  860. hw_state->ctrl1 = (val >> (pll->id * 6)) & 0x3f;
  861. ret = true;
  862. out:
  863. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  864. return ret;
  865. }
  866. struct skl_wrpll_context {
  867. uint64_t min_deviation; /* current minimal deviation */
  868. uint64_t central_freq; /* chosen central freq */
  869. uint64_t dco_freq; /* chosen dco freq */
  870. unsigned int p; /* chosen divider */
  871. };
  872. static void skl_wrpll_context_init(struct skl_wrpll_context *ctx)
  873. {
  874. memset(ctx, 0, sizeof(*ctx));
  875. ctx->min_deviation = U64_MAX;
  876. }
  877. /* DCO freq must be within +1%/-6% of the DCO central freq */
  878. #define SKL_DCO_MAX_PDEVIATION 100
  879. #define SKL_DCO_MAX_NDEVIATION 600
  880. static void skl_wrpll_try_divider(struct skl_wrpll_context *ctx,
  881. uint64_t central_freq,
  882. uint64_t dco_freq,
  883. unsigned int divider)
  884. {
  885. uint64_t deviation;
  886. deviation = div64_u64(10000 * abs_diff(dco_freq, central_freq),
  887. central_freq);
  888. /* positive deviation */
  889. if (dco_freq >= central_freq) {
  890. if (deviation < SKL_DCO_MAX_PDEVIATION &&
  891. deviation < ctx->min_deviation) {
  892. ctx->min_deviation = deviation;
  893. ctx->central_freq = central_freq;
  894. ctx->dco_freq = dco_freq;
  895. ctx->p = divider;
  896. }
  897. /* negative deviation */
  898. } else if (deviation < SKL_DCO_MAX_NDEVIATION &&
  899. deviation < ctx->min_deviation) {
  900. ctx->min_deviation = deviation;
  901. ctx->central_freq = central_freq;
  902. ctx->dco_freq = dco_freq;
  903. ctx->p = divider;
  904. }
  905. }
  906. static void skl_wrpll_get_multipliers(unsigned int p,
  907. unsigned int *p0 /* out */,
  908. unsigned int *p1 /* out */,
  909. unsigned int *p2 /* out */)
  910. {
  911. /* even dividers */
  912. if (p % 2 == 0) {
  913. unsigned int half = p / 2;
  914. if (half == 1 || half == 2 || half == 3 || half == 5) {
  915. *p0 = 2;
  916. *p1 = 1;
  917. *p2 = half;
  918. } else if (half % 2 == 0) {
  919. *p0 = 2;
  920. *p1 = half / 2;
  921. *p2 = 2;
  922. } else if (half % 3 == 0) {
  923. *p0 = 3;
  924. *p1 = half / 3;
  925. *p2 = 2;
  926. } else if (half % 7 == 0) {
  927. *p0 = 7;
  928. *p1 = half / 7;
  929. *p2 = 2;
  930. }
  931. } else if (p == 3 || p == 9) { /* 3, 5, 7, 9, 15, 21, 35 */
  932. *p0 = 3;
  933. *p1 = 1;
  934. *p2 = p / 3;
  935. } else if (p == 5 || p == 7) {
  936. *p0 = p;
  937. *p1 = 1;
  938. *p2 = 1;
  939. } else if (p == 15) {
  940. *p0 = 3;
  941. *p1 = 1;
  942. *p2 = 5;
  943. } else if (p == 21) {
  944. *p0 = 7;
  945. *p1 = 1;
  946. *p2 = 3;
  947. } else if (p == 35) {
  948. *p0 = 7;
  949. *p1 = 1;
  950. *p2 = 5;
  951. }
  952. }
  953. struct skl_wrpll_params {
  954. uint32_t dco_fraction;
  955. uint32_t dco_integer;
  956. uint32_t qdiv_ratio;
  957. uint32_t qdiv_mode;
  958. uint32_t kdiv;
  959. uint32_t pdiv;
  960. uint32_t central_freq;
  961. };
  962. static void skl_wrpll_params_populate(struct skl_wrpll_params *params,
  963. uint64_t afe_clock,
  964. uint64_t central_freq,
  965. uint32_t p0, uint32_t p1, uint32_t p2)
  966. {
  967. uint64_t dco_freq;
  968. switch (central_freq) {
  969. case 9600000000ULL:
  970. params->central_freq = 0;
  971. break;
  972. case 9000000000ULL:
  973. params->central_freq = 1;
  974. break;
  975. case 8400000000ULL:
  976. params->central_freq = 3;
  977. }
  978. switch (p0) {
  979. case 1:
  980. params->pdiv = 0;
  981. break;
  982. case 2:
  983. params->pdiv = 1;
  984. break;
  985. case 3:
  986. params->pdiv = 2;
  987. break;
  988. case 7:
  989. params->pdiv = 4;
  990. break;
  991. default:
  992. WARN(1, "Incorrect PDiv\n");
  993. }
  994. switch (p2) {
  995. case 5:
  996. params->kdiv = 0;
  997. break;
  998. case 2:
  999. params->kdiv = 1;
  1000. break;
  1001. case 3:
  1002. params->kdiv = 2;
  1003. break;
  1004. case 1:
  1005. params->kdiv = 3;
  1006. break;
  1007. default:
  1008. WARN(1, "Incorrect KDiv\n");
  1009. }
  1010. params->qdiv_ratio = p1;
  1011. params->qdiv_mode = (params->qdiv_ratio == 1) ? 0 : 1;
  1012. dco_freq = p0 * p1 * p2 * afe_clock;
  1013. /*
  1014. * Intermediate values are in Hz.
  1015. * Divide by MHz to match bsepc
  1016. */
  1017. params->dco_integer = div_u64(dco_freq, 24 * MHz(1));
  1018. params->dco_fraction =
  1019. div_u64((div_u64(dco_freq, 24) -
  1020. params->dco_integer * MHz(1)) * 0x8000, MHz(1));
  1021. }
  1022. static bool
  1023. skl_ddi_calculate_wrpll(int clock /* in Hz */,
  1024. struct skl_wrpll_params *wrpll_params)
  1025. {
  1026. uint64_t afe_clock = clock * 5; /* AFE Clock is 5x Pixel clock */
  1027. uint64_t dco_central_freq[3] = {8400000000ULL,
  1028. 9000000000ULL,
  1029. 9600000000ULL};
  1030. static const int even_dividers[] = { 4, 6, 8, 10, 12, 14, 16, 18, 20,
  1031. 24, 28, 30, 32, 36, 40, 42, 44,
  1032. 48, 52, 54, 56, 60, 64, 66, 68,
  1033. 70, 72, 76, 78, 80, 84, 88, 90,
  1034. 92, 96, 98 };
  1035. static const int odd_dividers[] = { 3, 5, 7, 9, 15, 21, 35 };
  1036. static const struct {
  1037. const int *list;
  1038. int n_dividers;
  1039. } dividers[] = {
  1040. { even_dividers, ARRAY_SIZE(even_dividers) },
  1041. { odd_dividers, ARRAY_SIZE(odd_dividers) },
  1042. };
  1043. struct skl_wrpll_context ctx;
  1044. unsigned int dco, d, i;
  1045. unsigned int p0, p1, p2;
  1046. skl_wrpll_context_init(&ctx);
  1047. for (d = 0; d < ARRAY_SIZE(dividers); d++) {
  1048. for (dco = 0; dco < ARRAY_SIZE(dco_central_freq); dco++) {
  1049. for (i = 0; i < dividers[d].n_dividers; i++) {
  1050. unsigned int p = dividers[d].list[i];
  1051. uint64_t dco_freq = p * afe_clock;
  1052. skl_wrpll_try_divider(&ctx,
  1053. dco_central_freq[dco],
  1054. dco_freq,
  1055. p);
  1056. /*
  1057. * Skip the remaining dividers if we're sure to
  1058. * have found the definitive divider, we can't
  1059. * improve a 0 deviation.
  1060. */
  1061. if (ctx.min_deviation == 0)
  1062. goto skip_remaining_dividers;
  1063. }
  1064. }
  1065. skip_remaining_dividers:
  1066. /*
  1067. * If a solution is found with an even divider, prefer
  1068. * this one.
  1069. */
  1070. if (d == 0 && ctx.p)
  1071. break;
  1072. }
  1073. if (!ctx.p) {
  1074. DRM_DEBUG_DRIVER("No valid divider found for %dHz\n", clock);
  1075. return false;
  1076. }
  1077. /*
  1078. * gcc incorrectly analyses that these can be used without being
  1079. * initialized. To be fair, it's hard to guess.
  1080. */
  1081. p0 = p1 = p2 = 0;
  1082. skl_wrpll_get_multipliers(ctx.p, &p0, &p1, &p2);
  1083. skl_wrpll_params_populate(wrpll_params, afe_clock, ctx.central_freq,
  1084. p0, p1, p2);
  1085. return true;
  1086. }
  1087. static bool skl_ddi_hdmi_pll_dividers(struct intel_crtc *crtc,
  1088. struct intel_crtc_state *crtc_state,
  1089. int clock)
  1090. {
  1091. uint32_t ctrl1, cfgcr1, cfgcr2;
  1092. struct skl_wrpll_params wrpll_params = { 0, };
  1093. /*
  1094. * See comment in intel_dpll_hw_state to understand why we always use 0
  1095. * as the DPLL id in this function.
  1096. */
  1097. ctrl1 = DPLL_CTRL1_OVERRIDE(0);
  1098. ctrl1 |= DPLL_CTRL1_HDMI_MODE(0);
  1099. if (!skl_ddi_calculate_wrpll(clock * 1000, &wrpll_params))
  1100. return false;
  1101. cfgcr1 = DPLL_CFGCR1_FREQ_ENABLE |
  1102. DPLL_CFGCR1_DCO_FRACTION(wrpll_params.dco_fraction) |
  1103. wrpll_params.dco_integer;
  1104. cfgcr2 = DPLL_CFGCR2_QDIV_RATIO(wrpll_params.qdiv_ratio) |
  1105. DPLL_CFGCR2_QDIV_MODE(wrpll_params.qdiv_mode) |
  1106. DPLL_CFGCR2_KDIV(wrpll_params.kdiv) |
  1107. DPLL_CFGCR2_PDIV(wrpll_params.pdiv) |
  1108. wrpll_params.central_freq;
  1109. memset(&crtc_state->dpll_hw_state, 0,
  1110. sizeof(crtc_state->dpll_hw_state));
  1111. crtc_state->dpll_hw_state.ctrl1 = ctrl1;
  1112. crtc_state->dpll_hw_state.cfgcr1 = cfgcr1;
  1113. crtc_state->dpll_hw_state.cfgcr2 = cfgcr2;
  1114. return true;
  1115. }
  1116. static bool
  1117. skl_ddi_dp_set_dpll_hw_state(int clock,
  1118. struct intel_dpll_hw_state *dpll_hw_state)
  1119. {
  1120. uint32_t ctrl1;
  1121. /*
  1122. * See comment in intel_dpll_hw_state to understand why we always use 0
  1123. * as the DPLL id in this function.
  1124. */
  1125. ctrl1 = DPLL_CTRL1_OVERRIDE(0);
  1126. switch (clock / 2) {
  1127. case 81000:
  1128. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, 0);
  1129. break;
  1130. case 135000:
  1131. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, 0);
  1132. break;
  1133. case 270000:
  1134. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, 0);
  1135. break;
  1136. /* eDP 1.4 rates */
  1137. case 162000:
  1138. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, 0);
  1139. break;
  1140. case 108000:
  1141. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, 0);
  1142. break;
  1143. case 216000:
  1144. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, 0);
  1145. break;
  1146. }
  1147. dpll_hw_state->ctrl1 = ctrl1;
  1148. return true;
  1149. }
  1150. static struct intel_shared_dpll *
  1151. skl_get_dpll(struct intel_crtc *crtc, struct intel_crtc_state *crtc_state,
  1152. struct intel_encoder *encoder)
  1153. {
  1154. struct intel_shared_dpll *pll;
  1155. int clock = crtc_state->port_clock;
  1156. bool bret;
  1157. struct intel_dpll_hw_state dpll_hw_state;
  1158. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  1159. if (encoder->type == INTEL_OUTPUT_HDMI) {
  1160. bret = skl_ddi_hdmi_pll_dividers(crtc, crtc_state, clock);
  1161. if (!bret) {
  1162. DRM_DEBUG_KMS("Could not get HDMI pll dividers.\n");
  1163. return NULL;
  1164. }
  1165. } else if (encoder->type == INTEL_OUTPUT_DP ||
  1166. encoder->type == INTEL_OUTPUT_DP_MST ||
  1167. encoder->type == INTEL_OUTPUT_EDP) {
  1168. bret = skl_ddi_dp_set_dpll_hw_state(clock, &dpll_hw_state);
  1169. if (!bret) {
  1170. DRM_DEBUG_KMS("Could not set DP dpll HW state.\n");
  1171. return NULL;
  1172. }
  1173. crtc_state->dpll_hw_state = dpll_hw_state;
  1174. } else {
  1175. return NULL;
  1176. }
  1177. if (encoder->type == INTEL_OUTPUT_EDP)
  1178. pll = intel_find_shared_dpll(crtc, crtc_state,
  1179. DPLL_ID_SKL_DPLL0,
  1180. DPLL_ID_SKL_DPLL0);
  1181. else
  1182. pll = intel_find_shared_dpll(crtc, crtc_state,
  1183. DPLL_ID_SKL_DPLL1,
  1184. DPLL_ID_SKL_DPLL3);
  1185. if (!pll)
  1186. return NULL;
  1187. intel_reference_shared_dpll(pll, crtc_state);
  1188. return pll;
  1189. }
  1190. static void skl_dump_hw_state(struct drm_i915_private *dev_priv,
  1191. struct intel_dpll_hw_state *hw_state)
  1192. {
  1193. DRM_DEBUG_KMS("dpll_hw_state: "
  1194. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  1195. hw_state->ctrl1,
  1196. hw_state->cfgcr1,
  1197. hw_state->cfgcr2);
  1198. }
  1199. static const struct intel_shared_dpll_funcs skl_ddi_pll_funcs = {
  1200. .enable = skl_ddi_pll_enable,
  1201. .disable = skl_ddi_pll_disable,
  1202. .get_hw_state = skl_ddi_pll_get_hw_state,
  1203. };
  1204. static const struct intel_shared_dpll_funcs skl_ddi_dpll0_funcs = {
  1205. .enable = skl_ddi_dpll0_enable,
  1206. .disable = skl_ddi_dpll0_disable,
  1207. .get_hw_state = skl_ddi_dpll0_get_hw_state,
  1208. };
  1209. static void bxt_ddi_pll_enable(struct drm_i915_private *dev_priv,
  1210. struct intel_shared_dpll *pll)
  1211. {
  1212. uint32_t temp;
  1213. enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
  1214. enum dpio_phy phy;
  1215. enum dpio_channel ch;
  1216. bxt_port_to_phy_channel(dev_priv, port, &phy, &ch);
  1217. /* Non-SSC reference */
  1218. temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
  1219. temp |= PORT_PLL_REF_SEL;
  1220. I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
  1221. if (IS_GEMINILAKE(dev_priv)) {
  1222. temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
  1223. temp |= PORT_PLL_POWER_ENABLE;
  1224. I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
  1225. if (wait_for_us((I915_READ(BXT_PORT_PLL_ENABLE(port)) &
  1226. PORT_PLL_POWER_STATE), 200))
  1227. DRM_ERROR("Power state not set for PLL:%d\n", port);
  1228. }
  1229. /* Disable 10 bit clock */
  1230. temp = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
  1231. temp &= ~PORT_PLL_10BIT_CLK_ENABLE;
  1232. I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
  1233. /* Write P1 & P2 */
  1234. temp = I915_READ(BXT_PORT_PLL_EBB_0(phy, ch));
  1235. temp &= ~(PORT_PLL_P1_MASK | PORT_PLL_P2_MASK);
  1236. temp |= pll->state.hw_state.ebb0;
  1237. I915_WRITE(BXT_PORT_PLL_EBB_0(phy, ch), temp);
  1238. /* Write M2 integer */
  1239. temp = I915_READ(BXT_PORT_PLL(phy, ch, 0));
  1240. temp &= ~PORT_PLL_M2_MASK;
  1241. temp |= pll->state.hw_state.pll0;
  1242. I915_WRITE(BXT_PORT_PLL(phy, ch, 0), temp);
  1243. /* Write N */
  1244. temp = I915_READ(BXT_PORT_PLL(phy, ch, 1));
  1245. temp &= ~PORT_PLL_N_MASK;
  1246. temp |= pll->state.hw_state.pll1;
  1247. I915_WRITE(BXT_PORT_PLL(phy, ch, 1), temp);
  1248. /* Write M2 fraction */
  1249. temp = I915_READ(BXT_PORT_PLL(phy, ch, 2));
  1250. temp &= ~PORT_PLL_M2_FRAC_MASK;
  1251. temp |= pll->state.hw_state.pll2;
  1252. I915_WRITE(BXT_PORT_PLL(phy, ch, 2), temp);
  1253. /* Write M2 fraction enable */
  1254. temp = I915_READ(BXT_PORT_PLL(phy, ch, 3));
  1255. temp &= ~PORT_PLL_M2_FRAC_ENABLE;
  1256. temp |= pll->state.hw_state.pll3;
  1257. I915_WRITE(BXT_PORT_PLL(phy, ch, 3), temp);
  1258. /* Write coeff */
  1259. temp = I915_READ(BXT_PORT_PLL(phy, ch, 6));
  1260. temp &= ~PORT_PLL_PROP_COEFF_MASK;
  1261. temp &= ~PORT_PLL_INT_COEFF_MASK;
  1262. temp &= ~PORT_PLL_GAIN_CTL_MASK;
  1263. temp |= pll->state.hw_state.pll6;
  1264. I915_WRITE(BXT_PORT_PLL(phy, ch, 6), temp);
  1265. /* Write calibration val */
  1266. temp = I915_READ(BXT_PORT_PLL(phy, ch, 8));
  1267. temp &= ~PORT_PLL_TARGET_CNT_MASK;
  1268. temp |= pll->state.hw_state.pll8;
  1269. I915_WRITE(BXT_PORT_PLL(phy, ch, 8), temp);
  1270. temp = I915_READ(BXT_PORT_PLL(phy, ch, 9));
  1271. temp &= ~PORT_PLL_LOCK_THRESHOLD_MASK;
  1272. temp |= pll->state.hw_state.pll9;
  1273. I915_WRITE(BXT_PORT_PLL(phy, ch, 9), temp);
  1274. temp = I915_READ(BXT_PORT_PLL(phy, ch, 10));
  1275. temp &= ~PORT_PLL_DCO_AMP_OVR_EN_H;
  1276. temp &= ~PORT_PLL_DCO_AMP_MASK;
  1277. temp |= pll->state.hw_state.pll10;
  1278. I915_WRITE(BXT_PORT_PLL(phy, ch, 10), temp);
  1279. /* Recalibrate with new settings */
  1280. temp = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
  1281. temp |= PORT_PLL_RECALIBRATE;
  1282. I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
  1283. temp &= ~PORT_PLL_10BIT_CLK_ENABLE;
  1284. temp |= pll->state.hw_state.ebb4;
  1285. I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
  1286. /* Enable PLL */
  1287. temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
  1288. temp |= PORT_PLL_ENABLE;
  1289. I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
  1290. POSTING_READ(BXT_PORT_PLL_ENABLE(port));
  1291. if (wait_for_us((I915_READ(BXT_PORT_PLL_ENABLE(port)) & PORT_PLL_LOCK),
  1292. 200))
  1293. DRM_ERROR("PLL %d not locked\n", port);
  1294. if (IS_GEMINILAKE(dev_priv)) {
  1295. temp = I915_READ(BXT_PORT_TX_DW5_LN0(phy, ch));
  1296. temp |= DCC_DELAY_RANGE_2;
  1297. I915_WRITE(BXT_PORT_TX_DW5_GRP(phy, ch), temp);
  1298. }
  1299. /*
  1300. * While we write to the group register to program all lanes at once we
  1301. * can read only lane registers and we pick lanes 0/1 for that.
  1302. */
  1303. temp = I915_READ(BXT_PORT_PCS_DW12_LN01(phy, ch));
  1304. temp &= ~LANE_STAGGER_MASK;
  1305. temp &= ~LANESTAGGER_STRAP_OVRD;
  1306. temp |= pll->state.hw_state.pcsdw12;
  1307. I915_WRITE(BXT_PORT_PCS_DW12_GRP(phy, ch), temp);
  1308. }
  1309. static void bxt_ddi_pll_disable(struct drm_i915_private *dev_priv,
  1310. struct intel_shared_dpll *pll)
  1311. {
  1312. enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
  1313. uint32_t temp;
  1314. temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
  1315. temp &= ~PORT_PLL_ENABLE;
  1316. I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
  1317. POSTING_READ(BXT_PORT_PLL_ENABLE(port));
  1318. if (IS_GEMINILAKE(dev_priv)) {
  1319. temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
  1320. temp &= ~PORT_PLL_POWER_ENABLE;
  1321. I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
  1322. if (wait_for_us(!(I915_READ(BXT_PORT_PLL_ENABLE(port)) &
  1323. PORT_PLL_POWER_STATE), 200))
  1324. DRM_ERROR("Power state not reset for PLL:%d\n", port);
  1325. }
  1326. }
  1327. static bool bxt_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
  1328. struct intel_shared_dpll *pll,
  1329. struct intel_dpll_hw_state *hw_state)
  1330. {
  1331. enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
  1332. uint32_t val;
  1333. bool ret;
  1334. enum dpio_phy phy;
  1335. enum dpio_channel ch;
  1336. bxt_port_to_phy_channel(dev_priv, port, &phy, &ch);
  1337. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
  1338. return false;
  1339. ret = false;
  1340. val = I915_READ(BXT_PORT_PLL_ENABLE(port));
  1341. if (!(val & PORT_PLL_ENABLE))
  1342. goto out;
  1343. hw_state->ebb0 = I915_READ(BXT_PORT_PLL_EBB_0(phy, ch));
  1344. hw_state->ebb0 &= PORT_PLL_P1_MASK | PORT_PLL_P2_MASK;
  1345. hw_state->ebb4 = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
  1346. hw_state->ebb4 &= PORT_PLL_10BIT_CLK_ENABLE;
  1347. hw_state->pll0 = I915_READ(BXT_PORT_PLL(phy, ch, 0));
  1348. hw_state->pll0 &= PORT_PLL_M2_MASK;
  1349. hw_state->pll1 = I915_READ(BXT_PORT_PLL(phy, ch, 1));
  1350. hw_state->pll1 &= PORT_PLL_N_MASK;
  1351. hw_state->pll2 = I915_READ(BXT_PORT_PLL(phy, ch, 2));
  1352. hw_state->pll2 &= PORT_PLL_M2_FRAC_MASK;
  1353. hw_state->pll3 = I915_READ(BXT_PORT_PLL(phy, ch, 3));
  1354. hw_state->pll3 &= PORT_PLL_M2_FRAC_ENABLE;
  1355. hw_state->pll6 = I915_READ(BXT_PORT_PLL(phy, ch, 6));
  1356. hw_state->pll6 &= PORT_PLL_PROP_COEFF_MASK |
  1357. PORT_PLL_INT_COEFF_MASK |
  1358. PORT_PLL_GAIN_CTL_MASK;
  1359. hw_state->pll8 = I915_READ(BXT_PORT_PLL(phy, ch, 8));
  1360. hw_state->pll8 &= PORT_PLL_TARGET_CNT_MASK;
  1361. hw_state->pll9 = I915_READ(BXT_PORT_PLL(phy, ch, 9));
  1362. hw_state->pll9 &= PORT_PLL_LOCK_THRESHOLD_MASK;
  1363. hw_state->pll10 = I915_READ(BXT_PORT_PLL(phy, ch, 10));
  1364. hw_state->pll10 &= PORT_PLL_DCO_AMP_OVR_EN_H |
  1365. PORT_PLL_DCO_AMP_MASK;
  1366. /*
  1367. * While we write to the group register to program all lanes at once we
  1368. * can read only lane registers. We configure all lanes the same way, so
  1369. * here just read out lanes 0/1 and output a note if lanes 2/3 differ.
  1370. */
  1371. hw_state->pcsdw12 = I915_READ(BXT_PORT_PCS_DW12_LN01(phy, ch));
  1372. if (I915_READ(BXT_PORT_PCS_DW12_LN23(phy, ch)) != hw_state->pcsdw12)
  1373. DRM_DEBUG_DRIVER("lane stagger config different for lane 01 (%08x) and 23 (%08x)\n",
  1374. hw_state->pcsdw12,
  1375. I915_READ(BXT_PORT_PCS_DW12_LN23(phy, ch)));
  1376. hw_state->pcsdw12 &= LANE_STAGGER_MASK | LANESTAGGER_STRAP_OVRD;
  1377. ret = true;
  1378. out:
  1379. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1380. return ret;
  1381. }
  1382. /* bxt clock parameters */
  1383. struct bxt_clk_div {
  1384. int clock;
  1385. uint32_t p1;
  1386. uint32_t p2;
  1387. uint32_t m2_int;
  1388. uint32_t m2_frac;
  1389. bool m2_frac_en;
  1390. uint32_t n;
  1391. int vco;
  1392. };
  1393. /* pre-calculated values for DP linkrates */
  1394. static const struct bxt_clk_div bxt_dp_clk_val[] = {
  1395. {162000, 4, 2, 32, 1677722, 1, 1},
  1396. {270000, 4, 1, 27, 0, 0, 1},
  1397. {540000, 2, 1, 27, 0, 0, 1},
  1398. {216000, 3, 2, 32, 1677722, 1, 1},
  1399. {243000, 4, 1, 24, 1258291, 1, 1},
  1400. {324000, 4, 1, 32, 1677722, 1, 1},
  1401. {432000, 3, 1, 32, 1677722, 1, 1}
  1402. };
  1403. static bool
  1404. bxt_ddi_hdmi_pll_dividers(struct intel_crtc *intel_crtc,
  1405. struct intel_crtc_state *crtc_state, int clock,
  1406. struct bxt_clk_div *clk_div)
  1407. {
  1408. struct dpll best_clock;
  1409. /* Calculate HDMI div */
  1410. /*
  1411. * FIXME: tie the following calculation into
  1412. * i9xx_crtc_compute_clock
  1413. */
  1414. if (!bxt_find_best_dpll(crtc_state, clock, &best_clock)) {
  1415. DRM_DEBUG_DRIVER("no PLL dividers found for clock %d pipe %c\n",
  1416. clock, pipe_name(intel_crtc->pipe));
  1417. return false;
  1418. }
  1419. clk_div->p1 = best_clock.p1;
  1420. clk_div->p2 = best_clock.p2;
  1421. WARN_ON(best_clock.m1 != 2);
  1422. clk_div->n = best_clock.n;
  1423. clk_div->m2_int = best_clock.m2 >> 22;
  1424. clk_div->m2_frac = best_clock.m2 & ((1 << 22) - 1);
  1425. clk_div->m2_frac_en = clk_div->m2_frac != 0;
  1426. clk_div->vco = best_clock.vco;
  1427. return true;
  1428. }
  1429. static void bxt_ddi_dp_pll_dividers(int clock, struct bxt_clk_div *clk_div)
  1430. {
  1431. int i;
  1432. *clk_div = bxt_dp_clk_val[0];
  1433. for (i = 0; i < ARRAY_SIZE(bxt_dp_clk_val); ++i) {
  1434. if (bxt_dp_clk_val[i].clock == clock) {
  1435. *clk_div = bxt_dp_clk_val[i];
  1436. break;
  1437. }
  1438. }
  1439. clk_div->vco = clock * 10 / 2 * clk_div->p1 * clk_div->p2;
  1440. }
  1441. static bool bxt_ddi_set_dpll_hw_state(int clock,
  1442. struct bxt_clk_div *clk_div,
  1443. struct intel_dpll_hw_state *dpll_hw_state)
  1444. {
  1445. int vco = clk_div->vco;
  1446. uint32_t prop_coef, int_coef, gain_ctl, targ_cnt;
  1447. uint32_t lanestagger;
  1448. if (vco >= 6200000 && vco <= 6700000) {
  1449. prop_coef = 4;
  1450. int_coef = 9;
  1451. gain_ctl = 3;
  1452. targ_cnt = 8;
  1453. } else if ((vco > 5400000 && vco < 6200000) ||
  1454. (vco >= 4800000 && vco < 5400000)) {
  1455. prop_coef = 5;
  1456. int_coef = 11;
  1457. gain_ctl = 3;
  1458. targ_cnt = 9;
  1459. } else if (vco == 5400000) {
  1460. prop_coef = 3;
  1461. int_coef = 8;
  1462. gain_ctl = 1;
  1463. targ_cnt = 9;
  1464. } else {
  1465. DRM_ERROR("Invalid VCO\n");
  1466. return false;
  1467. }
  1468. if (clock > 270000)
  1469. lanestagger = 0x18;
  1470. else if (clock > 135000)
  1471. lanestagger = 0x0d;
  1472. else if (clock > 67000)
  1473. lanestagger = 0x07;
  1474. else if (clock > 33000)
  1475. lanestagger = 0x04;
  1476. else
  1477. lanestagger = 0x02;
  1478. dpll_hw_state->ebb0 = PORT_PLL_P1(clk_div->p1) | PORT_PLL_P2(clk_div->p2);
  1479. dpll_hw_state->pll0 = clk_div->m2_int;
  1480. dpll_hw_state->pll1 = PORT_PLL_N(clk_div->n);
  1481. dpll_hw_state->pll2 = clk_div->m2_frac;
  1482. if (clk_div->m2_frac_en)
  1483. dpll_hw_state->pll3 = PORT_PLL_M2_FRAC_ENABLE;
  1484. dpll_hw_state->pll6 = prop_coef | PORT_PLL_INT_COEFF(int_coef);
  1485. dpll_hw_state->pll6 |= PORT_PLL_GAIN_CTL(gain_ctl);
  1486. dpll_hw_state->pll8 = targ_cnt;
  1487. dpll_hw_state->pll9 = 5 << PORT_PLL_LOCK_THRESHOLD_SHIFT;
  1488. dpll_hw_state->pll10 =
  1489. PORT_PLL_DCO_AMP(PORT_PLL_DCO_AMP_DEFAULT)
  1490. | PORT_PLL_DCO_AMP_OVR_EN_H;
  1491. dpll_hw_state->ebb4 = PORT_PLL_10BIT_CLK_ENABLE;
  1492. dpll_hw_state->pcsdw12 = LANESTAGGER_STRAP_OVRD | lanestagger;
  1493. return true;
  1494. }
  1495. static bool
  1496. bxt_ddi_dp_set_dpll_hw_state(int clock,
  1497. struct intel_dpll_hw_state *dpll_hw_state)
  1498. {
  1499. struct bxt_clk_div clk_div = {0};
  1500. bxt_ddi_dp_pll_dividers(clock, &clk_div);
  1501. return bxt_ddi_set_dpll_hw_state(clock, &clk_div, dpll_hw_state);
  1502. }
  1503. static bool
  1504. bxt_ddi_hdmi_set_dpll_hw_state(struct intel_crtc *intel_crtc,
  1505. struct intel_crtc_state *crtc_state, int clock,
  1506. struct intel_dpll_hw_state *dpll_hw_state)
  1507. {
  1508. struct bxt_clk_div clk_div = { };
  1509. bxt_ddi_hdmi_pll_dividers(intel_crtc, crtc_state, clock, &clk_div);
  1510. return bxt_ddi_set_dpll_hw_state(clock, &clk_div, dpll_hw_state);
  1511. }
  1512. static struct intel_shared_dpll *
  1513. bxt_get_dpll(struct intel_crtc *crtc,
  1514. struct intel_crtc_state *crtc_state,
  1515. struct intel_encoder *encoder)
  1516. {
  1517. struct intel_dpll_hw_state dpll_hw_state = { };
  1518. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1519. struct intel_digital_port *intel_dig_port;
  1520. struct intel_shared_dpll *pll;
  1521. int i, clock = crtc_state->port_clock;
  1522. if (encoder->type == INTEL_OUTPUT_HDMI &&
  1523. !bxt_ddi_hdmi_set_dpll_hw_state(crtc, crtc_state, clock,
  1524. &dpll_hw_state))
  1525. return NULL;
  1526. if ((encoder->type == INTEL_OUTPUT_DP ||
  1527. encoder->type == INTEL_OUTPUT_EDP ||
  1528. encoder->type == INTEL_OUTPUT_DP_MST) &&
  1529. !bxt_ddi_dp_set_dpll_hw_state(clock, &dpll_hw_state))
  1530. return NULL;
  1531. memset(&crtc_state->dpll_hw_state, 0,
  1532. sizeof(crtc_state->dpll_hw_state));
  1533. crtc_state->dpll_hw_state = dpll_hw_state;
  1534. if (encoder->type == INTEL_OUTPUT_DP_MST) {
  1535. struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
  1536. intel_dig_port = intel_mst->primary;
  1537. } else
  1538. intel_dig_port = enc_to_dig_port(&encoder->base);
  1539. /* 1:1 mapping between ports and PLLs */
  1540. i = (enum intel_dpll_id) intel_dig_port->port;
  1541. pll = intel_get_shared_dpll_by_id(dev_priv, i);
  1542. DRM_DEBUG_KMS("[CRTC:%d:%s] using pre-allocated %s\n",
  1543. crtc->base.base.id, crtc->base.name, pll->name);
  1544. intel_reference_shared_dpll(pll, crtc_state);
  1545. return pll;
  1546. }
  1547. static void bxt_dump_hw_state(struct drm_i915_private *dev_priv,
  1548. struct intel_dpll_hw_state *hw_state)
  1549. {
  1550. DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  1551. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  1552. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  1553. hw_state->ebb0,
  1554. hw_state->ebb4,
  1555. hw_state->pll0,
  1556. hw_state->pll1,
  1557. hw_state->pll2,
  1558. hw_state->pll3,
  1559. hw_state->pll6,
  1560. hw_state->pll8,
  1561. hw_state->pll9,
  1562. hw_state->pll10,
  1563. hw_state->pcsdw12);
  1564. }
  1565. static const struct intel_shared_dpll_funcs bxt_ddi_pll_funcs = {
  1566. .enable = bxt_ddi_pll_enable,
  1567. .disable = bxt_ddi_pll_disable,
  1568. .get_hw_state = bxt_ddi_pll_get_hw_state,
  1569. };
  1570. static void intel_ddi_pll_init(struct drm_device *dev)
  1571. {
  1572. struct drm_i915_private *dev_priv = to_i915(dev);
  1573. if (INTEL_GEN(dev_priv) < 9) {
  1574. uint32_t val = I915_READ(LCPLL_CTL);
  1575. /*
  1576. * The LCPLL register should be turned on by the BIOS. For now
  1577. * let's just check its state and print errors in case
  1578. * something is wrong. Don't even try to turn it on.
  1579. */
  1580. if (val & LCPLL_CD_SOURCE_FCLK)
  1581. DRM_ERROR("CDCLK source is not LCPLL\n");
  1582. if (val & LCPLL_PLL_DISABLE)
  1583. DRM_ERROR("LCPLL is disabled\n");
  1584. }
  1585. }
  1586. struct dpll_info {
  1587. const char *name;
  1588. const int id;
  1589. const struct intel_shared_dpll_funcs *funcs;
  1590. uint32_t flags;
  1591. };
  1592. struct intel_dpll_mgr {
  1593. const struct dpll_info *dpll_info;
  1594. struct intel_shared_dpll *(*get_dpll)(struct intel_crtc *crtc,
  1595. struct intel_crtc_state *crtc_state,
  1596. struct intel_encoder *encoder);
  1597. void (*dump_hw_state)(struct drm_i915_private *dev_priv,
  1598. struct intel_dpll_hw_state *hw_state);
  1599. };
  1600. static const struct dpll_info pch_plls[] = {
  1601. { "PCH DPLL A", DPLL_ID_PCH_PLL_A, &ibx_pch_dpll_funcs, 0 },
  1602. { "PCH DPLL B", DPLL_ID_PCH_PLL_B, &ibx_pch_dpll_funcs, 0 },
  1603. { NULL, -1, NULL, 0 },
  1604. };
  1605. static const struct intel_dpll_mgr pch_pll_mgr = {
  1606. .dpll_info = pch_plls,
  1607. .get_dpll = ibx_get_dpll,
  1608. .dump_hw_state = ibx_dump_hw_state,
  1609. };
  1610. static const struct dpll_info hsw_plls[] = {
  1611. { "WRPLL 1", DPLL_ID_WRPLL1, &hsw_ddi_wrpll_funcs, 0 },
  1612. { "WRPLL 2", DPLL_ID_WRPLL2, &hsw_ddi_wrpll_funcs, 0 },
  1613. { "SPLL", DPLL_ID_SPLL, &hsw_ddi_spll_funcs, 0 },
  1614. { "LCPLL 810", DPLL_ID_LCPLL_810, &hsw_ddi_lcpll_funcs, INTEL_DPLL_ALWAYS_ON },
  1615. { "LCPLL 1350", DPLL_ID_LCPLL_1350, &hsw_ddi_lcpll_funcs, INTEL_DPLL_ALWAYS_ON },
  1616. { "LCPLL 2700", DPLL_ID_LCPLL_2700, &hsw_ddi_lcpll_funcs, INTEL_DPLL_ALWAYS_ON },
  1617. { NULL, -1, NULL, },
  1618. };
  1619. static const struct intel_dpll_mgr hsw_pll_mgr = {
  1620. .dpll_info = hsw_plls,
  1621. .get_dpll = hsw_get_dpll,
  1622. .dump_hw_state = hsw_dump_hw_state,
  1623. };
  1624. static const struct dpll_info skl_plls[] = {
  1625. { "DPLL 0", DPLL_ID_SKL_DPLL0, &skl_ddi_dpll0_funcs, INTEL_DPLL_ALWAYS_ON },
  1626. { "DPLL 1", DPLL_ID_SKL_DPLL1, &skl_ddi_pll_funcs, 0 },
  1627. { "DPLL 2", DPLL_ID_SKL_DPLL2, &skl_ddi_pll_funcs, 0 },
  1628. { "DPLL 3", DPLL_ID_SKL_DPLL3, &skl_ddi_pll_funcs, 0 },
  1629. { NULL, -1, NULL, },
  1630. };
  1631. static const struct intel_dpll_mgr skl_pll_mgr = {
  1632. .dpll_info = skl_plls,
  1633. .get_dpll = skl_get_dpll,
  1634. .dump_hw_state = skl_dump_hw_state,
  1635. };
  1636. static const struct dpll_info bxt_plls[] = {
  1637. { "PORT PLL A", DPLL_ID_SKL_DPLL0, &bxt_ddi_pll_funcs, 0 },
  1638. { "PORT PLL B", DPLL_ID_SKL_DPLL1, &bxt_ddi_pll_funcs, 0 },
  1639. { "PORT PLL C", DPLL_ID_SKL_DPLL2, &bxt_ddi_pll_funcs, 0 },
  1640. { NULL, -1, NULL, },
  1641. };
  1642. static const struct intel_dpll_mgr bxt_pll_mgr = {
  1643. .dpll_info = bxt_plls,
  1644. .get_dpll = bxt_get_dpll,
  1645. .dump_hw_state = bxt_dump_hw_state,
  1646. };
  1647. static void cnl_ddi_pll_enable(struct drm_i915_private *dev_priv,
  1648. struct intel_shared_dpll *pll)
  1649. {
  1650. uint32_t val;
  1651. /* 1. Enable DPLL power in DPLL_ENABLE. */
  1652. val = I915_READ(CNL_DPLL_ENABLE(pll->id));
  1653. val |= PLL_POWER_ENABLE;
  1654. I915_WRITE(CNL_DPLL_ENABLE(pll->id), val);
  1655. /* 2. Wait for DPLL power state enabled in DPLL_ENABLE. */
  1656. if (intel_wait_for_register(dev_priv,
  1657. CNL_DPLL_ENABLE(pll->id),
  1658. PLL_POWER_STATE,
  1659. PLL_POWER_STATE,
  1660. 5))
  1661. DRM_ERROR("PLL %d Power not enabled\n", pll->id);
  1662. /*
  1663. * 3. Configure DPLL_CFGCR0 to set SSC enable/disable,
  1664. * select DP mode, and set DP link rate.
  1665. */
  1666. val = pll->state.hw_state.cfgcr0;
  1667. I915_WRITE(CNL_DPLL_CFGCR0(pll->id), val);
  1668. /* 4. Reab back to ensure writes completed */
  1669. POSTING_READ(CNL_DPLL_CFGCR0(pll->id));
  1670. /* 3. Configure DPLL_CFGCR0 */
  1671. /* Avoid touch CFGCR1 if HDMI mode is not enabled */
  1672. if (pll->state.hw_state.cfgcr0 & DPLL_CTRL1_HDMI_MODE(pll->id)) {
  1673. val = pll->state.hw_state.cfgcr1;
  1674. I915_WRITE(CNL_DPLL_CFGCR1(pll->id), val);
  1675. /* 4. Reab back to ensure writes completed */
  1676. POSTING_READ(CNL_DPLL_CFGCR1(pll->id));
  1677. }
  1678. /*
  1679. * 5. If the frequency will result in a change to the voltage
  1680. * requirement, follow the Display Voltage Frequency Switching
  1681. * Sequence Before Frequency Change
  1682. *
  1683. * FIXME: (DVFS) is used to adjust the display voltage to match the
  1684. * display clock frequencies
  1685. */
  1686. /* 6. Enable DPLL in DPLL_ENABLE. */
  1687. val = I915_READ(CNL_DPLL_ENABLE(pll->id));
  1688. val |= PLL_ENABLE;
  1689. I915_WRITE(CNL_DPLL_ENABLE(pll->id), val);
  1690. /* 7. Wait for PLL lock status in DPLL_ENABLE. */
  1691. if (intel_wait_for_register(dev_priv,
  1692. CNL_DPLL_ENABLE(pll->id),
  1693. PLL_LOCK,
  1694. PLL_LOCK,
  1695. 5))
  1696. DRM_ERROR("PLL %d not locked\n", pll->id);
  1697. /*
  1698. * 8. If the frequency will result in a change to the voltage
  1699. * requirement, follow the Display Voltage Frequency Switching
  1700. * Sequence After Frequency Change
  1701. *
  1702. * FIXME: (DVFS) is used to adjust the display voltage to match the
  1703. * display clock frequencies
  1704. */
  1705. /*
  1706. * 9. turn on the clock for the DDI and map the DPLL to the DDI
  1707. * Done at intel_ddi_clk_select
  1708. */
  1709. }
  1710. static void cnl_ddi_pll_disable(struct drm_i915_private *dev_priv,
  1711. struct intel_shared_dpll *pll)
  1712. {
  1713. uint32_t val;
  1714. /*
  1715. * 1. Configure DPCLKA_CFGCR0 to turn off the clock for the DDI.
  1716. * Done at intel_ddi_post_disable
  1717. */
  1718. /*
  1719. * 2. If the frequency will result in a change to the voltage
  1720. * requirement, follow the Display Voltage Frequency Switching
  1721. * Sequence Before Frequency Change
  1722. *
  1723. * FIXME: (DVFS) is used to adjust the display voltage to match the
  1724. * display clock frequencies
  1725. */
  1726. /* 3. Disable DPLL through DPLL_ENABLE. */
  1727. val = I915_READ(CNL_DPLL_ENABLE(pll->id));
  1728. val &= ~PLL_ENABLE;
  1729. I915_WRITE(CNL_DPLL_ENABLE(pll->id), val);
  1730. /* 4. Wait for PLL not locked status in DPLL_ENABLE. */
  1731. if (intel_wait_for_register(dev_priv,
  1732. CNL_DPLL_ENABLE(pll->id),
  1733. PLL_LOCK,
  1734. 0,
  1735. 5))
  1736. DRM_ERROR("PLL %d locked\n", pll->id);
  1737. /*
  1738. * 5. If the frequency will result in a change to the voltage
  1739. * requirement, follow the Display Voltage Frequency Switching
  1740. * Sequence After Frequency Change
  1741. *
  1742. * FIXME: (DVFS) is used to adjust the display voltage to match the
  1743. * display clock frequencies
  1744. */
  1745. /* 6. Disable DPLL power in DPLL_ENABLE. */
  1746. val = I915_READ(CNL_DPLL_ENABLE(pll->id));
  1747. val &= ~PLL_POWER_ENABLE;
  1748. I915_WRITE(CNL_DPLL_ENABLE(pll->id), val);
  1749. /* 7. Wait for DPLL power state disabled in DPLL_ENABLE. */
  1750. if (intel_wait_for_register(dev_priv,
  1751. CNL_DPLL_ENABLE(pll->id),
  1752. PLL_POWER_STATE,
  1753. 0,
  1754. 5))
  1755. DRM_ERROR("PLL %d Power not disabled\n", pll->id);
  1756. }
  1757. static bool cnl_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
  1758. struct intel_shared_dpll *pll,
  1759. struct intel_dpll_hw_state *hw_state)
  1760. {
  1761. uint32_t val;
  1762. bool ret;
  1763. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
  1764. return false;
  1765. ret = false;
  1766. val = I915_READ(CNL_DPLL_ENABLE(pll->id));
  1767. if (!(val & PLL_ENABLE))
  1768. goto out;
  1769. val = I915_READ(CNL_DPLL_CFGCR0(pll->id));
  1770. hw_state->cfgcr0 = val;
  1771. /* avoid reading back stale values if HDMI mode is not enabled */
  1772. if (val & DPLL_CFGCR0_HDMI_MODE) {
  1773. hw_state->cfgcr1 = I915_READ(CNL_DPLL_CFGCR1(pll->id));
  1774. }
  1775. ret = true;
  1776. out:
  1777. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1778. return ret;
  1779. }
  1780. static void cnl_wrpll_get_multipliers(unsigned int bestdiv,
  1781. unsigned int *pdiv,
  1782. unsigned int *qdiv,
  1783. unsigned int *kdiv)
  1784. {
  1785. /* even dividers */
  1786. if (bestdiv % 2 == 0) {
  1787. if (bestdiv == 2) {
  1788. *pdiv = 2;
  1789. *qdiv = 1;
  1790. *kdiv = 1;
  1791. } else if (bestdiv % 4 == 0) {
  1792. *pdiv = 2;
  1793. *qdiv = bestdiv / 4;
  1794. *kdiv = 2;
  1795. } else if (bestdiv % 6 == 0) {
  1796. *pdiv = 3;
  1797. *qdiv = bestdiv / 6;
  1798. *kdiv = 2;
  1799. } else if (bestdiv % 5 == 0) {
  1800. *pdiv = 5;
  1801. *qdiv = bestdiv / 10;
  1802. *kdiv = 2;
  1803. } else if (bestdiv % 14 == 0) {
  1804. *pdiv = 7;
  1805. *qdiv = bestdiv / 14;
  1806. *kdiv = 2;
  1807. }
  1808. } else {
  1809. if (bestdiv == 3 || bestdiv == 5 || bestdiv == 7) {
  1810. *pdiv = bestdiv;
  1811. *qdiv = 1;
  1812. *kdiv = 1;
  1813. } else { /* 9, 15, 21 */
  1814. *pdiv = bestdiv / 3;
  1815. *qdiv = 1;
  1816. *kdiv = 3;
  1817. }
  1818. }
  1819. }
  1820. static void cnl_wrpll_params_populate(struct skl_wrpll_params *params, uint32_t dco_freq,
  1821. uint32_t ref_freq, uint32_t pdiv, uint32_t qdiv,
  1822. uint32_t kdiv)
  1823. {
  1824. switch (kdiv) {
  1825. case 1:
  1826. params->kdiv = 1;
  1827. break;
  1828. case 2:
  1829. params->kdiv = 2;
  1830. break;
  1831. case 3:
  1832. params->kdiv = 4;
  1833. break;
  1834. default:
  1835. WARN(1, "Incorrect KDiv\n");
  1836. }
  1837. switch (pdiv) {
  1838. case 2:
  1839. params->pdiv = 1;
  1840. break;
  1841. case 3:
  1842. params->pdiv = 2;
  1843. break;
  1844. case 5:
  1845. params->pdiv = 4;
  1846. break;
  1847. case 7:
  1848. params->pdiv = 8;
  1849. break;
  1850. default:
  1851. WARN(1, "Incorrect PDiv\n");
  1852. }
  1853. if (kdiv != 2)
  1854. qdiv = 1;
  1855. params->qdiv_ratio = qdiv;
  1856. params->qdiv_mode = (qdiv == 1) ? 0 : 1;
  1857. params->dco_integer = div_u64(dco_freq, ref_freq);
  1858. params->dco_fraction = div_u64((div_u64((uint64_t)dco_freq<<15, (uint64_t)ref_freq) -
  1859. ((uint64_t)params->dco_integer<<15)) * 0x8000, 0x8000);
  1860. }
  1861. static bool
  1862. cnl_ddi_calculate_wrpll(int clock /* in Hz */,
  1863. struct drm_i915_private *dev_priv,
  1864. struct skl_wrpll_params *wrpll_params)
  1865. {
  1866. uint64_t afe_clock = clock * 5 / KHz(1); /* clocks in kHz */
  1867. unsigned int dco_min = 7998 * KHz(1);
  1868. unsigned int dco_max = 10000 * KHz(1);
  1869. unsigned int dco_mid = (dco_min + dco_max) / 2;
  1870. static const int dividers[] = { 2, 4, 6, 8, 10, 12, 14, 16,
  1871. 18, 20, 24, 28, 30, 32, 36, 40,
  1872. 42, 44, 48, 50, 52, 54, 56, 60,
  1873. 64, 66, 68, 70, 72, 76, 78, 80,
  1874. 84, 88, 90, 92, 96, 98, 100, 102,
  1875. 3, 5, 7, 9, 15, 21 };
  1876. unsigned int d, dco;
  1877. unsigned int dco_centrality = 0;
  1878. unsigned int best_dco_centrality = 999999;
  1879. unsigned int best_div = 0;
  1880. unsigned int best_dco = 0;
  1881. unsigned int pdiv = 0, qdiv = 0, kdiv = 0;
  1882. for (d = 0; d < ARRAY_SIZE(dividers); d++) {
  1883. dco = afe_clock * dividers[d];
  1884. if ((dco <= dco_max) && (dco >= dco_min)) {
  1885. dco_centrality = abs(dco - dco_mid);
  1886. if (dco_centrality < best_dco_centrality) {
  1887. best_dco_centrality = dco_centrality;
  1888. best_div = dividers[d];
  1889. best_dco = dco;
  1890. }
  1891. }
  1892. }
  1893. if (best_div == 0)
  1894. return false;
  1895. cnl_wrpll_get_multipliers(best_div, &pdiv, &qdiv, &kdiv);
  1896. cnl_wrpll_params_populate(wrpll_params, best_dco,
  1897. dev_priv->cdclk.hw.ref, pdiv, qdiv, kdiv);
  1898. return true;
  1899. }
  1900. static bool cnl_ddi_hdmi_pll_dividers(struct intel_crtc *crtc,
  1901. struct intel_crtc_state *crtc_state,
  1902. int clock)
  1903. {
  1904. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1905. uint32_t cfgcr0, cfgcr1;
  1906. struct skl_wrpll_params wrpll_params = { 0, };
  1907. cfgcr0 = DPLL_CFGCR0_HDMI_MODE;
  1908. if (!cnl_ddi_calculate_wrpll(clock * 1000, dev_priv, &wrpll_params))
  1909. return false;
  1910. cfgcr0 |= DPLL_CFGCR0_DCO_FRACTION(wrpll_params.dco_fraction) |
  1911. wrpll_params.dco_integer;
  1912. cfgcr1 = DPLL_CFGCR1_QDIV_RATIO(wrpll_params.qdiv_ratio) |
  1913. DPLL_CFGCR1_QDIV_MODE(wrpll_params.qdiv_mode) |
  1914. DPLL_CFGCR1_KDIV(wrpll_params.kdiv) |
  1915. DPLL_CFGCR1_PDIV(wrpll_params.pdiv) |
  1916. wrpll_params.central_freq |
  1917. DPLL_CFGCR1_CENTRAL_FREQ;
  1918. memset(&crtc_state->dpll_hw_state, 0,
  1919. sizeof(crtc_state->dpll_hw_state));
  1920. crtc_state->dpll_hw_state.cfgcr0 = cfgcr0;
  1921. crtc_state->dpll_hw_state.cfgcr1 = cfgcr1;
  1922. return true;
  1923. }
  1924. static bool
  1925. cnl_ddi_dp_set_dpll_hw_state(int clock,
  1926. struct intel_dpll_hw_state *dpll_hw_state)
  1927. {
  1928. uint32_t cfgcr0;
  1929. cfgcr0 = DPLL_CFGCR0_SSC_ENABLE;
  1930. switch (clock / 2) {
  1931. case 81000:
  1932. cfgcr0 |= DPLL_CFGCR0_LINK_RATE_810;
  1933. break;
  1934. case 135000:
  1935. cfgcr0 |= DPLL_CFGCR0_LINK_RATE_1350;
  1936. break;
  1937. case 270000:
  1938. cfgcr0 |= DPLL_CFGCR0_LINK_RATE_2700;
  1939. break;
  1940. /* eDP 1.4 rates */
  1941. case 162000:
  1942. cfgcr0 |= DPLL_CFGCR0_LINK_RATE_1620;
  1943. break;
  1944. case 108000:
  1945. cfgcr0 |= DPLL_CFGCR0_LINK_RATE_1080;
  1946. break;
  1947. case 216000:
  1948. cfgcr0 |= DPLL_CFGCR0_LINK_RATE_2160;
  1949. break;
  1950. case 324000:
  1951. /* Some SKUs may require elevated I/O voltage to support this */
  1952. cfgcr0 |= DPLL_CFGCR0_LINK_RATE_3240;
  1953. break;
  1954. case 405000:
  1955. /* Some SKUs may require elevated I/O voltage to support this */
  1956. cfgcr0 |= DPLL_CFGCR0_LINK_RATE_4050;
  1957. break;
  1958. }
  1959. dpll_hw_state->cfgcr0 = cfgcr0;
  1960. return true;
  1961. }
  1962. static struct intel_shared_dpll *
  1963. cnl_get_dpll(struct intel_crtc *crtc, struct intel_crtc_state *crtc_state,
  1964. struct intel_encoder *encoder)
  1965. {
  1966. struct intel_shared_dpll *pll;
  1967. int clock = crtc_state->port_clock;
  1968. bool bret;
  1969. struct intel_dpll_hw_state dpll_hw_state;
  1970. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  1971. if (encoder->type == INTEL_OUTPUT_HDMI) {
  1972. bret = cnl_ddi_hdmi_pll_dividers(crtc, crtc_state, clock);
  1973. if (!bret) {
  1974. DRM_DEBUG_KMS("Could not get HDMI pll dividers.\n");
  1975. return NULL;
  1976. }
  1977. } else if (encoder->type == INTEL_OUTPUT_DP ||
  1978. encoder->type == INTEL_OUTPUT_DP_MST ||
  1979. encoder->type == INTEL_OUTPUT_EDP) {
  1980. bret = cnl_ddi_dp_set_dpll_hw_state(clock, &dpll_hw_state);
  1981. if (!bret) {
  1982. DRM_DEBUG_KMS("Could not set DP dpll HW state.\n");
  1983. return NULL;
  1984. }
  1985. crtc_state->dpll_hw_state = dpll_hw_state;
  1986. } else {
  1987. DRM_DEBUG_KMS("Skip DPLL setup for encoder %d\n",
  1988. encoder->type);
  1989. return NULL;
  1990. }
  1991. pll = intel_find_shared_dpll(crtc, crtc_state,
  1992. DPLL_ID_SKL_DPLL0,
  1993. DPLL_ID_SKL_DPLL2);
  1994. if (!pll) {
  1995. DRM_DEBUG_KMS("No PLL selected\n");
  1996. return NULL;
  1997. }
  1998. intel_reference_shared_dpll(pll, crtc_state);
  1999. return pll;
  2000. }
  2001. static void cnl_dump_hw_state(struct drm_i915_private *dev_priv,
  2002. struct intel_dpll_hw_state *hw_state)
  2003. {
  2004. DRM_DEBUG_KMS("dpll_hw_state: "
  2005. "cfgcr0: 0x%x, cfgcr1: 0x%x\n",
  2006. hw_state->cfgcr0,
  2007. hw_state->cfgcr1);
  2008. }
  2009. static const struct intel_shared_dpll_funcs cnl_ddi_pll_funcs = {
  2010. .enable = cnl_ddi_pll_enable,
  2011. .disable = cnl_ddi_pll_disable,
  2012. .get_hw_state = cnl_ddi_pll_get_hw_state,
  2013. };
  2014. static const struct dpll_info cnl_plls[] = {
  2015. { "DPLL 0", DPLL_ID_SKL_DPLL0, &cnl_ddi_pll_funcs, 0 },
  2016. { "DPLL 1", DPLL_ID_SKL_DPLL1, &cnl_ddi_pll_funcs, 0 },
  2017. { "DPLL 2", DPLL_ID_SKL_DPLL2, &cnl_ddi_pll_funcs, 0 },
  2018. { NULL, -1, NULL, },
  2019. };
  2020. static const struct intel_dpll_mgr cnl_pll_mgr = {
  2021. .dpll_info = cnl_plls,
  2022. .get_dpll = cnl_get_dpll,
  2023. .dump_hw_state = cnl_dump_hw_state,
  2024. };
  2025. /**
  2026. * intel_shared_dpll_init - Initialize shared DPLLs
  2027. * @dev: drm device
  2028. *
  2029. * Initialize shared DPLLs for @dev.
  2030. */
  2031. void intel_shared_dpll_init(struct drm_device *dev)
  2032. {
  2033. struct drm_i915_private *dev_priv = to_i915(dev);
  2034. const struct intel_dpll_mgr *dpll_mgr = NULL;
  2035. const struct dpll_info *dpll_info;
  2036. int i;
  2037. if (IS_CANNONLAKE(dev_priv))
  2038. dpll_mgr = &cnl_pll_mgr;
  2039. else if (IS_GEN9_BC(dev_priv))
  2040. dpll_mgr = &skl_pll_mgr;
  2041. else if (IS_GEN9_LP(dev_priv))
  2042. dpll_mgr = &bxt_pll_mgr;
  2043. else if (HAS_DDI(dev_priv))
  2044. dpll_mgr = &hsw_pll_mgr;
  2045. else if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  2046. dpll_mgr = &pch_pll_mgr;
  2047. if (!dpll_mgr) {
  2048. dev_priv->num_shared_dpll = 0;
  2049. return;
  2050. }
  2051. dpll_info = dpll_mgr->dpll_info;
  2052. for (i = 0; dpll_info[i].id >= 0; i++) {
  2053. WARN_ON(i != dpll_info[i].id);
  2054. dev_priv->shared_dplls[i].id = dpll_info[i].id;
  2055. dev_priv->shared_dplls[i].name = dpll_info[i].name;
  2056. dev_priv->shared_dplls[i].funcs = *dpll_info[i].funcs;
  2057. dev_priv->shared_dplls[i].flags = dpll_info[i].flags;
  2058. }
  2059. dev_priv->dpll_mgr = dpll_mgr;
  2060. dev_priv->num_shared_dpll = i;
  2061. mutex_init(&dev_priv->dpll_lock);
  2062. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  2063. /* FIXME: Move this to a more suitable place */
  2064. if (HAS_DDI(dev_priv))
  2065. intel_ddi_pll_init(dev);
  2066. }
  2067. /**
  2068. * intel_get_shared_dpll - get a shared DPLL for CRTC and encoder combination
  2069. * @crtc: CRTC
  2070. * @crtc_state: atomic state for @crtc
  2071. * @encoder: encoder
  2072. *
  2073. * Find an appropriate DPLL for the given CRTC and encoder combination. A
  2074. * reference from the @crtc to the returned pll is registered in the atomic
  2075. * state. That configuration is made effective by calling
  2076. * intel_shared_dpll_swap_state(). The reference should be released by calling
  2077. * intel_release_shared_dpll().
  2078. *
  2079. * Returns:
  2080. * A shared DPLL to be used by @crtc and @encoder with the given @crtc_state.
  2081. */
  2082. struct intel_shared_dpll *
  2083. intel_get_shared_dpll(struct intel_crtc *crtc,
  2084. struct intel_crtc_state *crtc_state,
  2085. struct intel_encoder *encoder)
  2086. {
  2087. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  2088. const struct intel_dpll_mgr *dpll_mgr = dev_priv->dpll_mgr;
  2089. if (WARN_ON(!dpll_mgr))
  2090. return NULL;
  2091. return dpll_mgr->get_dpll(crtc, crtc_state, encoder);
  2092. }
  2093. /**
  2094. * intel_release_shared_dpll - end use of DPLL by CRTC in atomic state
  2095. * @dpll: dpll in use by @crtc
  2096. * @crtc: crtc
  2097. * @state: atomic state
  2098. *
  2099. * This function releases the reference from @crtc to @dpll from the
  2100. * atomic @state. The new configuration is made effective by calling
  2101. * intel_shared_dpll_swap_state().
  2102. */
  2103. void intel_release_shared_dpll(struct intel_shared_dpll *dpll,
  2104. struct intel_crtc *crtc,
  2105. struct drm_atomic_state *state)
  2106. {
  2107. struct intel_shared_dpll_state *shared_dpll_state;
  2108. shared_dpll_state = intel_atomic_get_shared_dpll_state(state);
  2109. shared_dpll_state[dpll->id].crtc_mask &= ~(1 << crtc->pipe);
  2110. }
  2111. /**
  2112. * intel_shared_dpll_dump_hw_state - write hw_state to dmesg
  2113. * @dev_priv: i915 drm device
  2114. * @hw_state: hw state to be written to the log
  2115. *
  2116. * Write the relevant values in @hw_state to dmesg using DRM_DEBUG_KMS.
  2117. */
  2118. void intel_dpll_dump_hw_state(struct drm_i915_private *dev_priv,
  2119. struct intel_dpll_hw_state *hw_state)
  2120. {
  2121. if (dev_priv->dpll_mgr) {
  2122. dev_priv->dpll_mgr->dump_hw_state(dev_priv, hw_state);
  2123. } else {
  2124. /* fallback for platforms that don't use the shared dpll
  2125. * infrastructure
  2126. */
  2127. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  2128. "fp0: 0x%x, fp1: 0x%x\n",
  2129. hw_state->dpll,
  2130. hw_state->dpll_md,
  2131. hw_state->fp0,
  2132. hw_state->fp1);
  2133. }
  2134. }