intel_dpll_mgr.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  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. /**
  1648. * intel_shared_dpll_init - Initialize shared DPLLs
  1649. * @dev: drm device
  1650. *
  1651. * Initialize shared DPLLs for @dev.
  1652. */
  1653. void intel_shared_dpll_init(struct drm_device *dev)
  1654. {
  1655. struct drm_i915_private *dev_priv = to_i915(dev);
  1656. const struct intel_dpll_mgr *dpll_mgr = NULL;
  1657. const struct dpll_info *dpll_info;
  1658. int i;
  1659. if (IS_GEN9_BC(dev_priv))
  1660. dpll_mgr = &skl_pll_mgr;
  1661. else if (IS_GEN9_LP(dev_priv))
  1662. dpll_mgr = &bxt_pll_mgr;
  1663. else if (HAS_DDI(dev_priv))
  1664. dpll_mgr = &hsw_pll_mgr;
  1665. else if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  1666. dpll_mgr = &pch_pll_mgr;
  1667. if (!dpll_mgr) {
  1668. dev_priv->num_shared_dpll = 0;
  1669. return;
  1670. }
  1671. dpll_info = dpll_mgr->dpll_info;
  1672. for (i = 0; dpll_info[i].id >= 0; i++) {
  1673. WARN_ON(i != dpll_info[i].id);
  1674. dev_priv->shared_dplls[i].id = dpll_info[i].id;
  1675. dev_priv->shared_dplls[i].name = dpll_info[i].name;
  1676. dev_priv->shared_dplls[i].funcs = *dpll_info[i].funcs;
  1677. dev_priv->shared_dplls[i].flags = dpll_info[i].flags;
  1678. }
  1679. dev_priv->dpll_mgr = dpll_mgr;
  1680. dev_priv->num_shared_dpll = i;
  1681. mutex_init(&dev_priv->dpll_lock);
  1682. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  1683. /* FIXME: Move this to a more suitable place */
  1684. if (HAS_DDI(dev_priv))
  1685. intel_ddi_pll_init(dev);
  1686. }
  1687. /**
  1688. * intel_get_shared_dpll - get a shared DPLL for CRTC and encoder combination
  1689. * @crtc: CRTC
  1690. * @crtc_state: atomic state for @crtc
  1691. * @encoder: encoder
  1692. *
  1693. * Find an appropriate DPLL for the given CRTC and encoder combination. A
  1694. * reference from the @crtc to the returned pll is registered in the atomic
  1695. * state. That configuration is made effective by calling
  1696. * intel_shared_dpll_swap_state(). The reference should be released by calling
  1697. * intel_release_shared_dpll().
  1698. *
  1699. * Returns:
  1700. * A shared DPLL to be used by @crtc and @encoder with the given @crtc_state.
  1701. */
  1702. struct intel_shared_dpll *
  1703. intel_get_shared_dpll(struct intel_crtc *crtc,
  1704. struct intel_crtc_state *crtc_state,
  1705. struct intel_encoder *encoder)
  1706. {
  1707. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1708. const struct intel_dpll_mgr *dpll_mgr = dev_priv->dpll_mgr;
  1709. if (WARN_ON(!dpll_mgr))
  1710. return NULL;
  1711. return dpll_mgr->get_dpll(crtc, crtc_state, encoder);
  1712. }
  1713. /**
  1714. * intel_release_shared_dpll - end use of DPLL by CRTC in atomic state
  1715. * @dpll: dpll in use by @crtc
  1716. * @crtc: crtc
  1717. * @state: atomic state
  1718. *
  1719. * This function releases the reference from @crtc to @dpll from the
  1720. * atomic @state. The new configuration is made effective by calling
  1721. * intel_shared_dpll_swap_state().
  1722. */
  1723. void intel_release_shared_dpll(struct intel_shared_dpll *dpll,
  1724. struct intel_crtc *crtc,
  1725. struct drm_atomic_state *state)
  1726. {
  1727. struct intel_shared_dpll_state *shared_dpll_state;
  1728. shared_dpll_state = intel_atomic_get_shared_dpll_state(state);
  1729. shared_dpll_state[dpll->id].crtc_mask &= ~(1 << crtc->pipe);
  1730. }
  1731. /**
  1732. * intel_shared_dpll_dump_hw_state - write hw_state to dmesg
  1733. * @dev_priv: i915 drm device
  1734. * @hw_state: hw state to be written to the log
  1735. *
  1736. * Write the relevant values in @hw_state to dmesg using DRM_DEBUG_KMS.
  1737. */
  1738. void intel_dpll_dump_hw_state(struct drm_i915_private *dev_priv,
  1739. struct intel_dpll_hw_state *hw_state)
  1740. {
  1741. if (dev_priv->dpll_mgr) {
  1742. dev_priv->dpll_mgr->dump_hw_state(dev_priv, hw_state);
  1743. } else {
  1744. /* fallback for platforms that don't use the shared dpll
  1745. * infrastructure
  1746. */
  1747. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  1748. "fp0: 0x%x, fp1: 0x%x\n",
  1749. hw_state->dpll,
  1750. hw_state->dpll_md,
  1751. hw_state->fp0,
  1752. hw_state->fp1);
  1753. }
  1754. }