2 * Copyright © 2006-2016 Intel Corporation
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:
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
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.
24 #include "intel_dpio_phy.h"
25 #include "intel_dpll_mgr.h"
26 #include "intel_drv.h"
31 * Display PLLs used for driving outputs vary by platform. While some have
32 * per-pipe or per-encoder dedicated PLLs, others allow the use of any PLL
33 * from a pool. In the latter scenario, it is possible that multiple pipes
34 * share a PLL if their configurations match.
36 * This file provides an abstraction over display PLLs. The function
37 * intel_shared_dpll_init() initializes the PLLs for the given platform. The
38 * users of a PLL are tracked and that tracking is integrated with the atomic
39 * modset interface. During an atomic operation, required PLLs can be reserved
40 * for a given CRTC and encoder configuration by calling
41 * intel_reserve_shared_dplls() and previously reserved PLLs can be released
42 * with intel_release_shared_dplls().
43 * Changes to the users are first staged in the atomic state, and then made
44 * effective by calling intel_shared_dpll_swap_state() during the atomic
49 intel_atomic_duplicate_dpll_state(struct drm_i915_private *dev_priv,
50 struct intel_shared_dpll_state *shared_dpll)
54 /* Copy shared dpll state */
55 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
56 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
58 shared_dpll[i] = pll->state;
62 static struct intel_shared_dpll_state *
63 intel_atomic_get_shared_dpll_state(struct drm_atomic_state *s)
65 struct intel_atomic_state *state = to_intel_atomic_state(s);
67 WARN_ON(!drm_modeset_is_locked(&s->dev->mode_config.connection_mutex));
69 if (!state->dpll_set) {
70 state->dpll_set = true;
72 intel_atomic_duplicate_dpll_state(to_i915(s->dev),
76 return state->shared_dpll;
80 * intel_get_shared_dpll_by_id - get a DPLL given its id
81 * @dev_priv: i915 device instance
85 * A pointer to the DPLL with @id
87 struct intel_shared_dpll *
88 intel_get_shared_dpll_by_id(struct drm_i915_private *dev_priv,
89 enum intel_dpll_id id)
91 return &dev_priv->shared_dplls[id];
95 * intel_get_shared_dpll_id - get the id of a DPLL
96 * @dev_priv: i915 device instance
103 intel_get_shared_dpll_id(struct drm_i915_private *dev_priv,
104 struct intel_shared_dpll *pll)
106 if (WARN_ON(pll < dev_priv->shared_dplls||
107 pll > &dev_priv->shared_dplls[dev_priv->num_shared_dpll]))
110 return (enum intel_dpll_id) (pll - dev_priv->shared_dplls);
114 void assert_shared_dpll(struct drm_i915_private *dev_priv,
115 struct intel_shared_dpll *pll,
119 struct intel_dpll_hw_state hw_state;
121 if (WARN(!pll, "asserting DPLL %s with no DPLL\n", onoff(state)))
124 cur_state = pll->info->funcs->get_hw_state(dev_priv, pll, &hw_state);
125 I915_STATE_WARN(cur_state != state,
126 "%s assertion failure (expected %s, current %s)\n",
127 pll->info->name, onoff(state), onoff(cur_state));
131 * intel_prepare_shared_dpll - call a dpll's prepare hook
132 * @crtc_state: CRTC, and its state, which has a shared dpll
134 * This calls the PLL's prepare hook if it has one and if the PLL is not
135 * already enabled. The prepare hook is platform specific.
137 void intel_prepare_shared_dpll(const struct intel_crtc_state *crtc_state)
139 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
140 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
141 struct intel_shared_dpll *pll = crtc_state->shared_dpll;
143 if (WARN_ON(pll == NULL))
146 mutex_lock(&dev_priv->dpll_lock);
147 WARN_ON(!pll->state.crtc_mask);
148 if (!pll->active_mask) {
149 DRM_DEBUG_DRIVER("setting up %s\n", pll->info->name);
151 assert_shared_dpll_disabled(dev_priv, pll);
153 pll->info->funcs->prepare(dev_priv, pll);
155 mutex_unlock(&dev_priv->dpll_lock);
159 * intel_enable_shared_dpll - enable a CRTC's shared DPLL
160 * @crtc_state: CRTC, and its state, which has a shared DPLL
162 * Enable the shared DPLL used by @crtc.
164 void intel_enable_shared_dpll(const struct intel_crtc_state *crtc_state)
166 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
167 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
168 struct intel_shared_dpll *pll = crtc_state->shared_dpll;
169 unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
170 unsigned int old_mask;
172 if (WARN_ON(pll == NULL))
175 mutex_lock(&dev_priv->dpll_lock);
176 old_mask = pll->active_mask;
178 if (WARN_ON(!(pll->state.crtc_mask & crtc_mask)) ||
179 WARN_ON(pll->active_mask & crtc_mask))
182 pll->active_mask |= crtc_mask;
184 DRM_DEBUG_KMS("enable %s (active %x, on? %d) for crtc %d\n",
185 pll->info->name, pll->active_mask, pll->on,
190 assert_shared_dpll_enabled(dev_priv, pll);
195 DRM_DEBUG_KMS("enabling %s\n", pll->info->name);
196 pll->info->funcs->enable(dev_priv, pll);
200 mutex_unlock(&dev_priv->dpll_lock);
204 * intel_disable_shared_dpll - disable a CRTC's shared DPLL
205 * @crtc_state: CRTC, and its state, which has a shared DPLL
207 * Disable the shared DPLL used by @crtc.
209 void intel_disable_shared_dpll(const struct intel_crtc_state *crtc_state)
211 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
212 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
213 struct intel_shared_dpll *pll = crtc_state->shared_dpll;
214 unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
216 /* PCH only available on ILK+ */
217 if (INTEL_GEN(dev_priv) < 5)
223 mutex_lock(&dev_priv->dpll_lock);
224 if (WARN_ON(!(pll->active_mask & crtc_mask)))
227 DRM_DEBUG_KMS("disable %s (active %x, on? %d) for crtc %d\n",
228 pll->info->name, pll->active_mask, pll->on,
231 assert_shared_dpll_enabled(dev_priv, pll);
234 pll->active_mask &= ~crtc_mask;
235 if (pll->active_mask)
238 DRM_DEBUG_KMS("disabling %s\n", pll->info->name);
239 pll->info->funcs->disable(dev_priv, pll);
243 mutex_unlock(&dev_priv->dpll_lock);
246 static struct intel_shared_dpll *
247 intel_find_shared_dpll(struct intel_atomic_state *state,
248 const struct intel_crtc *crtc,
249 const struct intel_dpll_hw_state *pll_state,
250 enum intel_dpll_id range_min,
251 enum intel_dpll_id range_max)
253 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
254 struct intel_shared_dpll *pll, *unused_pll = NULL;
255 struct intel_shared_dpll_state *shared_dpll;
256 enum intel_dpll_id i;
258 shared_dpll = intel_atomic_get_shared_dpll_state(&state->base);
260 for (i = range_min; i <= range_max; i++) {
261 pll = &dev_priv->shared_dplls[i];
263 /* Only want to check enabled timings first */
264 if (shared_dpll[i].crtc_mask == 0) {
270 if (memcmp(pll_state,
271 &shared_dpll[i].hw_state,
272 sizeof(*pll_state)) == 0) {
273 DRM_DEBUG_KMS("[CRTC:%d:%s] sharing existing %s (crtc mask 0x%08x, active %x)\n",
274 crtc->base.base.id, crtc->base.name,
276 shared_dpll[i].crtc_mask,
282 /* Ok no matching timings, maybe there's a free one? */
284 DRM_DEBUG_KMS("[CRTC:%d:%s] allocated %s\n",
285 crtc->base.base.id, crtc->base.name,
286 unused_pll->info->name);
294 intel_reference_shared_dpll(struct intel_atomic_state *state,
295 const struct intel_crtc *crtc,
296 const struct intel_shared_dpll *pll,
297 const struct intel_dpll_hw_state *pll_state)
299 struct intel_shared_dpll_state *shared_dpll;
300 const enum intel_dpll_id id = pll->info->id;
302 shared_dpll = intel_atomic_get_shared_dpll_state(&state->base);
304 if (shared_dpll[id].crtc_mask == 0)
305 shared_dpll[id].hw_state = *pll_state;
307 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->info->name,
308 pipe_name(crtc->pipe));
310 shared_dpll[id].crtc_mask |= 1 << crtc->pipe;
313 static void intel_unreference_shared_dpll(struct intel_atomic_state *state,
314 const struct intel_crtc *crtc,
315 const struct intel_shared_dpll *pll)
317 struct intel_shared_dpll_state *shared_dpll;
319 shared_dpll = intel_atomic_get_shared_dpll_state(&state->base);
320 shared_dpll[pll->info->id].crtc_mask &= ~(1 << crtc->pipe);
323 static void intel_put_dpll(struct intel_atomic_state *state,
324 struct intel_crtc *crtc)
326 const struct intel_crtc_state *old_crtc_state =
327 intel_atomic_get_old_crtc_state(state, crtc);
328 struct intel_crtc_state *new_crtc_state =
329 intel_atomic_get_new_crtc_state(state, crtc);
331 new_crtc_state->shared_dpll = NULL;
333 if (!old_crtc_state->shared_dpll)
336 intel_unreference_shared_dpll(state, crtc, old_crtc_state->shared_dpll);
340 * intel_shared_dpll_swap_state - make atomic DPLL configuration effective
341 * @state: atomic state
343 * This is the dpll version of drm_atomic_helper_swap_state() since the
344 * helper does not handle driver-specific global state.
346 * For consistency with atomic helpers this function does a complete swap,
347 * i.e. it also puts the current state into @state, even though there is no
348 * need for that at this moment.
350 void intel_shared_dpll_swap_state(struct intel_atomic_state *state)
352 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
353 struct intel_shared_dpll_state *shared_dpll = state->shared_dpll;
354 enum intel_dpll_id i;
356 if (!state->dpll_set)
359 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
360 struct intel_shared_dpll *pll =
361 &dev_priv->shared_dplls[i];
363 swap(pll->state, shared_dpll[i]);
367 static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
368 struct intel_shared_dpll *pll,
369 struct intel_dpll_hw_state *hw_state)
371 const enum intel_dpll_id id = pll->info->id;
372 intel_wakeref_t wakeref;
375 wakeref = intel_display_power_get_if_enabled(dev_priv,
376 POWER_DOMAIN_DISPLAY_CORE);
380 val = I915_READ(PCH_DPLL(id));
381 hw_state->dpll = val;
382 hw_state->fp0 = I915_READ(PCH_FP0(id));
383 hw_state->fp1 = I915_READ(PCH_FP1(id));
385 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
387 return val & DPLL_VCO_ENABLE;
390 static void ibx_pch_dpll_prepare(struct drm_i915_private *dev_priv,
391 struct intel_shared_dpll *pll)
393 const enum intel_dpll_id id = pll->info->id;
395 I915_WRITE(PCH_FP0(id), pll->state.hw_state.fp0);
396 I915_WRITE(PCH_FP1(id), pll->state.hw_state.fp1);
399 static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
404 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)));
406 val = I915_READ(PCH_DREF_CONTROL);
407 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
408 DREF_SUPERSPREAD_SOURCE_MASK));
409 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
412 static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
413 struct intel_shared_dpll *pll)
415 const enum intel_dpll_id id = pll->info->id;
417 /* PCH refclock must be enabled first */
418 ibx_assert_pch_refclk_enabled(dev_priv);
420 I915_WRITE(PCH_DPLL(id), pll->state.hw_state.dpll);
422 /* Wait for the clocks to stabilize. */
423 POSTING_READ(PCH_DPLL(id));
426 /* The pixel multiplier can only be updated once the
427 * DPLL is enabled and the clocks are stable.
431 I915_WRITE(PCH_DPLL(id), pll->state.hw_state.dpll);
432 POSTING_READ(PCH_DPLL(id));
436 static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
437 struct intel_shared_dpll *pll)
439 const enum intel_dpll_id id = pll->info->id;
441 I915_WRITE(PCH_DPLL(id), 0);
442 POSTING_READ(PCH_DPLL(id));
446 static bool ibx_get_dpll(struct intel_atomic_state *state,
447 struct intel_crtc *crtc,
448 struct intel_encoder *encoder)
450 struct intel_crtc_state *crtc_state =
451 intel_atomic_get_new_crtc_state(state, crtc);
452 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
453 struct intel_shared_dpll *pll;
454 enum intel_dpll_id i;
456 if (HAS_PCH_IBX(dev_priv)) {
457 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
458 i = (enum intel_dpll_id) crtc->pipe;
459 pll = &dev_priv->shared_dplls[i];
461 DRM_DEBUG_KMS("[CRTC:%d:%s] using pre-allocated %s\n",
462 crtc->base.base.id, crtc->base.name,
465 pll = intel_find_shared_dpll(state, crtc,
466 &crtc_state->dpll_hw_state,
474 /* reference the pll */
475 intel_reference_shared_dpll(state, crtc,
476 pll, &crtc_state->dpll_hw_state);
478 crtc_state->shared_dpll = pll;
483 static void ibx_dump_hw_state(struct drm_i915_private *dev_priv,
484 const struct intel_dpll_hw_state *hw_state)
486 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
487 "fp0: 0x%x, fp1: 0x%x\n",
494 static const struct intel_shared_dpll_funcs ibx_pch_dpll_funcs = {
495 .prepare = ibx_pch_dpll_prepare,
496 .enable = ibx_pch_dpll_enable,
497 .disable = ibx_pch_dpll_disable,
498 .get_hw_state = ibx_pch_dpll_get_hw_state,
501 static void hsw_ddi_wrpll_enable(struct drm_i915_private *dev_priv,
502 struct intel_shared_dpll *pll)
504 const enum intel_dpll_id id = pll->info->id;
506 I915_WRITE(WRPLL_CTL(id), pll->state.hw_state.wrpll);
507 POSTING_READ(WRPLL_CTL(id));
511 static void hsw_ddi_spll_enable(struct drm_i915_private *dev_priv,
512 struct intel_shared_dpll *pll)
514 I915_WRITE(SPLL_CTL, pll->state.hw_state.spll);
515 POSTING_READ(SPLL_CTL);
519 static void hsw_ddi_wrpll_disable(struct drm_i915_private *dev_priv,
520 struct intel_shared_dpll *pll)
522 const enum intel_dpll_id id = pll->info->id;
525 val = I915_READ(WRPLL_CTL(id));
526 I915_WRITE(WRPLL_CTL(id), val & ~WRPLL_PLL_ENABLE);
527 POSTING_READ(WRPLL_CTL(id));
530 static void hsw_ddi_spll_disable(struct drm_i915_private *dev_priv,
531 struct intel_shared_dpll *pll)
535 val = I915_READ(SPLL_CTL);
536 I915_WRITE(SPLL_CTL, val & ~SPLL_PLL_ENABLE);
537 POSTING_READ(SPLL_CTL);
540 static bool hsw_ddi_wrpll_get_hw_state(struct drm_i915_private *dev_priv,
541 struct intel_shared_dpll *pll,
542 struct intel_dpll_hw_state *hw_state)
544 const enum intel_dpll_id id = pll->info->id;
545 intel_wakeref_t wakeref;
548 wakeref = intel_display_power_get_if_enabled(dev_priv,
549 POWER_DOMAIN_DISPLAY_CORE);
553 val = I915_READ(WRPLL_CTL(id));
554 hw_state->wrpll = val;
556 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
558 return val & WRPLL_PLL_ENABLE;
561 static bool hsw_ddi_spll_get_hw_state(struct drm_i915_private *dev_priv,
562 struct intel_shared_dpll *pll,
563 struct intel_dpll_hw_state *hw_state)
565 intel_wakeref_t wakeref;
568 wakeref = intel_display_power_get_if_enabled(dev_priv,
569 POWER_DOMAIN_DISPLAY_CORE);
573 val = I915_READ(SPLL_CTL);
574 hw_state->spll = val;
576 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
578 return val & SPLL_PLL_ENABLE;
582 #define LC_FREQ_2K U64_C(LC_FREQ * 2000)
588 /* Constraints for PLL good behavior */
594 struct hsw_wrpll_rnp {
598 static unsigned hsw_wrpll_get_budget_for_freq(int clock)
672 static void hsw_wrpll_update_rnp(u64 freq2k, unsigned int budget,
673 unsigned int r2, unsigned int n2,
675 struct hsw_wrpll_rnp *best)
677 u64 a, b, c, d, diff, diff_best;
679 /* No best (r,n,p) yet */
688 * Output clock is (LC_FREQ_2K / 2000) * N / (P * R), which compares to
692 * abs(freq2k - (LC_FREQ_2K * n2/(p * r2))) /
695 * and we would like delta <= budget.
697 * If the discrepancy is above the PPM-based budget, always prefer to
698 * improve upon the previous solution. However, if you're within the
699 * budget, try to maximize Ref * VCO, that is N / (P * R^2).
701 a = freq2k * budget * p * r2;
702 b = freq2k * budget * best->p * best->r2;
703 diff = abs_diff(freq2k * p * r2, LC_FREQ_2K * n2);
704 diff_best = abs_diff(freq2k * best->p * best->r2,
705 LC_FREQ_2K * best->n2);
707 d = 1000000 * diff_best;
709 if (a < c && b < d) {
710 /* If both are above the budget, pick the closer */
711 if (best->p * best->r2 * diff < p * r2 * diff_best) {
716 } else if (a >= c && b < d) {
717 /* If A is below the threshold but B is above it? Update. */
721 } else if (a >= c && b >= d) {
722 /* Both are below the limit, so pick the higher n2/(r2*r2) */
723 if (n2 * best->r2 * best->r2 > best->n2 * r2 * r2) {
729 /* Otherwise a < c && b >= d, do nothing */
733 hsw_ddi_calculate_wrpll(int clock /* in Hz */,
734 unsigned *r2_out, unsigned *n2_out, unsigned *p_out)
738 struct hsw_wrpll_rnp best = { 0, 0, 0 };
741 freq2k = clock / 100;
743 budget = hsw_wrpll_get_budget_for_freq(clock);
745 /* Special case handling for 540 pixel clock: bypass WR PLL entirely
746 * and directly pass the LC PLL to it. */
747 if (freq2k == 5400000) {
755 * Ref = LC_FREQ / R, where Ref is the actual reference input seen by
758 * We want R so that REF_MIN <= Ref <= REF_MAX.
759 * Injecting R2 = 2 * R gives:
760 * REF_MAX * r2 > LC_FREQ * 2 and
761 * REF_MIN * r2 < LC_FREQ * 2
763 * Which means the desired boundaries for r2 are:
764 * LC_FREQ * 2 / REF_MAX < r2 < LC_FREQ * 2 / REF_MIN
767 for (r2 = LC_FREQ * 2 / REF_MAX + 1;
768 r2 <= LC_FREQ * 2 / REF_MIN;
772 * VCO = N * Ref, that is: VCO = N * LC_FREQ / R
774 * Once again we want VCO_MIN <= VCO <= VCO_MAX.
775 * Injecting R2 = 2 * R and N2 = 2 * N, we get:
776 * VCO_MAX * r2 > n2 * LC_FREQ and
777 * VCO_MIN * r2 < n2 * LC_FREQ)
779 * Which means the desired boundaries for n2 are:
780 * VCO_MIN * r2 / LC_FREQ < n2 < VCO_MAX * r2 / LC_FREQ
782 for (n2 = VCO_MIN * r2 / LC_FREQ + 1;
783 n2 <= VCO_MAX * r2 / LC_FREQ;
786 for (p = P_MIN; p <= P_MAX; p += P_INC)
787 hsw_wrpll_update_rnp(freq2k, budget,
797 static struct intel_shared_dpll *
798 hsw_ddi_hdmi_get_dpll(struct intel_atomic_state *state,
799 struct intel_crtc *crtc)
801 struct intel_crtc_state *crtc_state =
802 intel_atomic_get_new_crtc_state(state, crtc);
803 struct intel_shared_dpll *pll;
805 unsigned int p, n2, r2;
807 hsw_ddi_calculate_wrpll(crtc_state->port_clock * 1000, &r2, &n2, &p);
809 val = WRPLL_PLL_ENABLE | WRPLL_REF_LCPLL |
810 WRPLL_DIVIDER_REFERENCE(r2) | WRPLL_DIVIDER_FEEDBACK(n2) |
811 WRPLL_DIVIDER_POST(p);
813 crtc_state->dpll_hw_state.wrpll = val;
815 pll = intel_find_shared_dpll(state, crtc,
816 &crtc_state->dpll_hw_state,
817 DPLL_ID_WRPLL1, DPLL_ID_WRPLL2);
825 static struct intel_shared_dpll *
826 hsw_ddi_dp_get_dpll(struct intel_crtc_state *crtc_state)
828 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
829 struct intel_shared_dpll *pll;
830 enum intel_dpll_id pll_id;
831 int clock = crtc_state->port_clock;
835 pll_id = DPLL_ID_LCPLL_810;
838 pll_id = DPLL_ID_LCPLL_1350;
841 pll_id = DPLL_ID_LCPLL_2700;
844 DRM_DEBUG_KMS("Invalid clock for DP: %d\n", clock);
848 pll = intel_get_shared_dpll_by_id(dev_priv, pll_id);
856 static bool hsw_get_dpll(struct intel_atomic_state *state,
857 struct intel_crtc *crtc,
858 struct intel_encoder *encoder)
860 struct intel_crtc_state *crtc_state =
861 intel_atomic_get_new_crtc_state(state, crtc);
862 struct intel_shared_dpll *pll;
864 memset(&crtc_state->dpll_hw_state, 0,
865 sizeof(crtc_state->dpll_hw_state));
867 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
868 pll = hsw_ddi_hdmi_get_dpll(state, crtc);
869 } else if (intel_crtc_has_dp_encoder(crtc_state)) {
870 pll = hsw_ddi_dp_get_dpll(crtc_state);
871 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
872 if (WARN_ON(crtc_state->port_clock / 2 != 135000))
875 crtc_state->dpll_hw_state.spll =
876 SPLL_PLL_ENABLE | SPLL_FREQ_1350MHz | SPLL_REF_MUXED_SSC;
878 pll = intel_find_shared_dpll(state, crtc,
879 &crtc_state->dpll_hw_state,
880 DPLL_ID_SPLL, DPLL_ID_SPLL);
888 intel_reference_shared_dpll(state, crtc,
889 pll, &crtc_state->dpll_hw_state);
891 crtc_state->shared_dpll = pll;
896 static void hsw_dump_hw_state(struct drm_i915_private *dev_priv,
897 const struct intel_dpll_hw_state *hw_state)
899 DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
900 hw_state->wrpll, hw_state->spll);
903 static const struct intel_shared_dpll_funcs hsw_ddi_wrpll_funcs = {
904 .enable = hsw_ddi_wrpll_enable,
905 .disable = hsw_ddi_wrpll_disable,
906 .get_hw_state = hsw_ddi_wrpll_get_hw_state,
909 static const struct intel_shared_dpll_funcs hsw_ddi_spll_funcs = {
910 .enable = hsw_ddi_spll_enable,
911 .disable = hsw_ddi_spll_disable,
912 .get_hw_state = hsw_ddi_spll_get_hw_state,
915 static void hsw_ddi_lcpll_enable(struct drm_i915_private *dev_priv,
916 struct intel_shared_dpll *pll)
920 static void hsw_ddi_lcpll_disable(struct drm_i915_private *dev_priv,
921 struct intel_shared_dpll *pll)
925 static bool hsw_ddi_lcpll_get_hw_state(struct drm_i915_private *dev_priv,
926 struct intel_shared_dpll *pll,
927 struct intel_dpll_hw_state *hw_state)
932 static const struct intel_shared_dpll_funcs hsw_ddi_lcpll_funcs = {
933 .enable = hsw_ddi_lcpll_enable,
934 .disable = hsw_ddi_lcpll_disable,
935 .get_hw_state = hsw_ddi_lcpll_get_hw_state,
938 struct skl_dpll_regs {
939 i915_reg_t ctl, cfgcr1, cfgcr2;
942 /* this array is indexed by the *shared* pll id */
943 static const struct skl_dpll_regs skl_dpll_regs[4] = {
947 /* DPLL 0 doesn't support HDMI mode */
952 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL1),
953 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL1),
958 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL2),
959 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL2),
964 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL3),
965 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL3),
969 static void skl_ddi_pll_write_ctrl1(struct drm_i915_private *dev_priv,
970 struct intel_shared_dpll *pll)
972 const enum intel_dpll_id id = pll->info->id;
975 val = I915_READ(DPLL_CTRL1);
977 val &= ~(DPLL_CTRL1_HDMI_MODE(id) |
979 DPLL_CTRL1_LINK_RATE_MASK(id));
980 val |= pll->state.hw_state.ctrl1 << (id * 6);
982 I915_WRITE(DPLL_CTRL1, val);
983 POSTING_READ(DPLL_CTRL1);
986 static void skl_ddi_pll_enable(struct drm_i915_private *dev_priv,
987 struct intel_shared_dpll *pll)
989 const struct skl_dpll_regs *regs = skl_dpll_regs;
990 const enum intel_dpll_id id = pll->info->id;
992 skl_ddi_pll_write_ctrl1(dev_priv, pll);
994 I915_WRITE(regs[id].cfgcr1, pll->state.hw_state.cfgcr1);
995 I915_WRITE(regs[id].cfgcr2, pll->state.hw_state.cfgcr2);
996 POSTING_READ(regs[id].cfgcr1);
997 POSTING_READ(regs[id].cfgcr2);
999 /* the enable bit is always bit 31 */
1000 I915_WRITE(regs[id].ctl,
1001 I915_READ(regs[id].ctl) | LCPLL_PLL_ENABLE);
1003 if (intel_wait_for_register(&dev_priv->uncore,
1008 DRM_ERROR("DPLL %d not locked\n", id);
1011 static void skl_ddi_dpll0_enable(struct drm_i915_private *dev_priv,
1012 struct intel_shared_dpll *pll)
1014 skl_ddi_pll_write_ctrl1(dev_priv, pll);
1017 static void skl_ddi_pll_disable(struct drm_i915_private *dev_priv,
1018 struct intel_shared_dpll *pll)
1020 const struct skl_dpll_regs *regs = skl_dpll_regs;
1021 const enum intel_dpll_id id = pll->info->id;
1023 /* the enable bit is always bit 31 */
1024 I915_WRITE(regs[id].ctl,
1025 I915_READ(regs[id].ctl) & ~LCPLL_PLL_ENABLE);
1026 POSTING_READ(regs[id].ctl);
1029 static void skl_ddi_dpll0_disable(struct drm_i915_private *dev_priv,
1030 struct intel_shared_dpll *pll)
1034 static bool skl_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
1035 struct intel_shared_dpll *pll,
1036 struct intel_dpll_hw_state *hw_state)
1039 const struct skl_dpll_regs *regs = skl_dpll_regs;
1040 const enum intel_dpll_id id = pll->info->id;
1041 intel_wakeref_t wakeref;
1044 wakeref = intel_display_power_get_if_enabled(dev_priv,
1045 POWER_DOMAIN_DISPLAY_CORE);
1051 val = I915_READ(regs[id].ctl);
1052 if (!(val & LCPLL_PLL_ENABLE))
1055 val = I915_READ(DPLL_CTRL1);
1056 hw_state->ctrl1 = (val >> (id * 6)) & 0x3f;
1058 /* avoid reading back stale values if HDMI mode is not enabled */
1059 if (val & DPLL_CTRL1_HDMI_MODE(id)) {
1060 hw_state->cfgcr1 = I915_READ(regs[id].cfgcr1);
1061 hw_state->cfgcr2 = I915_READ(regs[id].cfgcr2);
1066 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
1071 static bool skl_ddi_dpll0_get_hw_state(struct drm_i915_private *dev_priv,
1072 struct intel_shared_dpll *pll,
1073 struct intel_dpll_hw_state *hw_state)
1075 const struct skl_dpll_regs *regs = skl_dpll_regs;
1076 const enum intel_dpll_id id = pll->info->id;
1077 intel_wakeref_t wakeref;
1081 wakeref = intel_display_power_get_if_enabled(dev_priv,
1082 POWER_DOMAIN_DISPLAY_CORE);
1088 /* DPLL0 is always enabled since it drives CDCLK */
1089 val = I915_READ(regs[id].ctl);
1090 if (WARN_ON(!(val & LCPLL_PLL_ENABLE)))
1093 val = I915_READ(DPLL_CTRL1);
1094 hw_state->ctrl1 = (val >> (id * 6)) & 0x3f;
1099 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
1104 struct skl_wrpll_context {
1105 u64 min_deviation; /* current minimal deviation */
1106 u64 central_freq; /* chosen central freq */
1107 u64 dco_freq; /* chosen dco freq */
1108 unsigned int p; /* chosen divider */
1111 static void skl_wrpll_context_init(struct skl_wrpll_context *ctx)
1113 memset(ctx, 0, sizeof(*ctx));
1115 ctx->min_deviation = U64_MAX;
1118 /* DCO freq must be within +1%/-6% of the DCO central freq */
1119 #define SKL_DCO_MAX_PDEVIATION 100
1120 #define SKL_DCO_MAX_NDEVIATION 600
1122 static void skl_wrpll_try_divider(struct skl_wrpll_context *ctx,
1125 unsigned int divider)
1129 deviation = div64_u64(10000 * abs_diff(dco_freq, central_freq),
1132 /* positive deviation */
1133 if (dco_freq >= central_freq) {
1134 if (deviation < SKL_DCO_MAX_PDEVIATION &&
1135 deviation < ctx->min_deviation) {
1136 ctx->min_deviation = deviation;
1137 ctx->central_freq = central_freq;
1138 ctx->dco_freq = dco_freq;
1141 /* negative deviation */
1142 } else if (deviation < SKL_DCO_MAX_NDEVIATION &&
1143 deviation < ctx->min_deviation) {
1144 ctx->min_deviation = deviation;
1145 ctx->central_freq = central_freq;
1146 ctx->dco_freq = dco_freq;
1151 static void skl_wrpll_get_multipliers(unsigned int p,
1152 unsigned int *p0 /* out */,
1153 unsigned int *p1 /* out */,
1154 unsigned int *p2 /* out */)
1158 unsigned int half = p / 2;
1160 if (half == 1 || half == 2 || half == 3 || half == 5) {
1164 } else if (half % 2 == 0) {
1168 } else if (half % 3 == 0) {
1172 } else if (half % 7 == 0) {
1177 } else if (p == 3 || p == 9) { /* 3, 5, 7, 9, 15, 21, 35 */
1181 } else if (p == 5 || p == 7) {
1185 } else if (p == 15) {
1189 } else if (p == 21) {
1193 } else if (p == 35) {
1200 struct skl_wrpll_params {
1210 static void skl_wrpll_params_populate(struct skl_wrpll_params *params,
1213 u32 p0, u32 p1, u32 p2)
1217 switch (central_freq) {
1219 params->central_freq = 0;
1222 params->central_freq = 1;
1225 params->central_freq = 3;
1242 WARN(1, "Incorrect PDiv\n");
1259 WARN(1, "Incorrect KDiv\n");
1262 params->qdiv_ratio = p1;
1263 params->qdiv_mode = (params->qdiv_ratio == 1) ? 0 : 1;
1265 dco_freq = p0 * p1 * p2 * afe_clock;
1268 * Intermediate values are in Hz.
1269 * Divide by MHz to match bsepc
1271 params->dco_integer = div_u64(dco_freq, 24 * MHz(1));
1272 params->dco_fraction =
1273 div_u64((div_u64(dco_freq, 24) -
1274 params->dco_integer * MHz(1)) * 0x8000, MHz(1));
1278 skl_ddi_calculate_wrpll(int clock /* in Hz */,
1279 struct skl_wrpll_params *wrpll_params)
1281 u64 afe_clock = clock * 5; /* AFE Clock is 5x Pixel clock */
1282 u64 dco_central_freq[3] = { 8400000000ULL,
1285 static const int even_dividers[] = { 4, 6, 8, 10, 12, 14, 16, 18, 20,
1286 24, 28, 30, 32, 36, 40, 42, 44,
1287 48, 52, 54, 56, 60, 64, 66, 68,
1288 70, 72, 76, 78, 80, 84, 88, 90,
1290 static const int odd_dividers[] = { 3, 5, 7, 9, 15, 21, 35 };
1291 static const struct {
1295 { even_dividers, ARRAY_SIZE(even_dividers) },
1296 { odd_dividers, ARRAY_SIZE(odd_dividers) },
1298 struct skl_wrpll_context ctx;
1299 unsigned int dco, d, i;
1300 unsigned int p0, p1, p2;
1302 skl_wrpll_context_init(&ctx);
1304 for (d = 0; d < ARRAY_SIZE(dividers); d++) {
1305 for (dco = 0; dco < ARRAY_SIZE(dco_central_freq); dco++) {
1306 for (i = 0; i < dividers[d].n_dividers; i++) {
1307 unsigned int p = dividers[d].list[i];
1308 u64 dco_freq = p * afe_clock;
1310 skl_wrpll_try_divider(&ctx,
1311 dco_central_freq[dco],
1315 * Skip the remaining dividers if we're sure to
1316 * have found the definitive divider, we can't
1317 * improve a 0 deviation.
1319 if (ctx.min_deviation == 0)
1320 goto skip_remaining_dividers;
1324 skip_remaining_dividers:
1326 * If a solution is found with an even divider, prefer
1329 if (d == 0 && ctx.p)
1334 DRM_DEBUG_DRIVER("No valid divider found for %dHz\n", clock);
1339 * gcc incorrectly analyses that these can be used without being
1340 * initialized. To be fair, it's hard to guess.
1343 skl_wrpll_get_multipliers(ctx.p, &p0, &p1, &p2);
1344 skl_wrpll_params_populate(wrpll_params, afe_clock, ctx.central_freq,
1350 static bool skl_ddi_hdmi_pll_dividers(struct intel_crtc_state *crtc_state)
1352 u32 ctrl1, cfgcr1, cfgcr2;
1353 struct skl_wrpll_params wrpll_params = { 0, };
1356 * See comment in intel_dpll_hw_state to understand why we always use 0
1357 * as the DPLL id in this function.
1359 ctrl1 = DPLL_CTRL1_OVERRIDE(0);
1361 ctrl1 |= DPLL_CTRL1_HDMI_MODE(0);
1363 if (!skl_ddi_calculate_wrpll(crtc_state->port_clock * 1000,
1367 cfgcr1 = DPLL_CFGCR1_FREQ_ENABLE |
1368 DPLL_CFGCR1_DCO_FRACTION(wrpll_params.dco_fraction) |
1369 wrpll_params.dco_integer;
1371 cfgcr2 = DPLL_CFGCR2_QDIV_RATIO(wrpll_params.qdiv_ratio) |
1372 DPLL_CFGCR2_QDIV_MODE(wrpll_params.qdiv_mode) |
1373 DPLL_CFGCR2_KDIV(wrpll_params.kdiv) |
1374 DPLL_CFGCR2_PDIV(wrpll_params.pdiv) |
1375 wrpll_params.central_freq;
1377 memset(&crtc_state->dpll_hw_state, 0,
1378 sizeof(crtc_state->dpll_hw_state));
1380 crtc_state->dpll_hw_state.ctrl1 = ctrl1;
1381 crtc_state->dpll_hw_state.cfgcr1 = cfgcr1;
1382 crtc_state->dpll_hw_state.cfgcr2 = cfgcr2;
1387 skl_ddi_dp_set_dpll_hw_state(struct intel_crtc_state *crtc_state)
1392 * See comment in intel_dpll_hw_state to understand why we always use 0
1393 * as the DPLL id in this function.
1395 ctrl1 = DPLL_CTRL1_OVERRIDE(0);
1396 switch (crtc_state->port_clock / 2) {
1398 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, 0);
1401 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, 0);
1404 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, 0);
1408 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, 0);
1411 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, 0);
1414 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, 0);
1418 memset(&crtc_state->dpll_hw_state, 0,
1419 sizeof(crtc_state->dpll_hw_state));
1421 crtc_state->dpll_hw_state.ctrl1 = ctrl1;
1426 static bool skl_get_dpll(struct intel_atomic_state *state,
1427 struct intel_crtc *crtc,
1428 struct intel_encoder *encoder)
1430 struct intel_crtc_state *crtc_state =
1431 intel_atomic_get_new_crtc_state(state, crtc);
1432 struct intel_shared_dpll *pll;
1435 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
1436 bret = skl_ddi_hdmi_pll_dividers(crtc_state);
1438 DRM_DEBUG_KMS("Could not get HDMI pll dividers.\n");
1441 } else if (intel_crtc_has_dp_encoder(crtc_state)) {
1442 bret = skl_ddi_dp_set_dpll_hw_state(crtc_state);
1444 DRM_DEBUG_KMS("Could not set DP dpll HW state.\n");
1451 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
1452 pll = intel_find_shared_dpll(state, crtc,
1453 &crtc_state->dpll_hw_state,
1457 pll = intel_find_shared_dpll(state, crtc,
1458 &crtc_state->dpll_hw_state,
1464 intel_reference_shared_dpll(state, crtc,
1465 pll, &crtc_state->dpll_hw_state);
1467 crtc_state->shared_dpll = pll;
1472 static void skl_dump_hw_state(struct drm_i915_private *dev_priv,
1473 const struct intel_dpll_hw_state *hw_state)
1475 DRM_DEBUG_KMS("dpll_hw_state: "
1476 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
1482 static const struct intel_shared_dpll_funcs skl_ddi_pll_funcs = {
1483 .enable = skl_ddi_pll_enable,
1484 .disable = skl_ddi_pll_disable,
1485 .get_hw_state = skl_ddi_pll_get_hw_state,
1488 static const struct intel_shared_dpll_funcs skl_ddi_dpll0_funcs = {
1489 .enable = skl_ddi_dpll0_enable,
1490 .disable = skl_ddi_dpll0_disable,
1491 .get_hw_state = skl_ddi_dpll0_get_hw_state,
1494 static void bxt_ddi_pll_enable(struct drm_i915_private *dev_priv,
1495 struct intel_shared_dpll *pll)
1498 enum port port = (enum port)pll->info->id; /* 1:1 port->PLL mapping */
1500 enum dpio_channel ch;
1502 bxt_port_to_phy_channel(dev_priv, port, &phy, &ch);
1504 /* Non-SSC reference */
1505 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1506 temp |= PORT_PLL_REF_SEL;
1507 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1509 if (IS_GEMINILAKE(dev_priv)) {
1510 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1511 temp |= PORT_PLL_POWER_ENABLE;
1512 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1514 if (wait_for_us((I915_READ(BXT_PORT_PLL_ENABLE(port)) &
1515 PORT_PLL_POWER_STATE), 200))
1516 DRM_ERROR("Power state not set for PLL:%d\n", port);
1519 /* Disable 10 bit clock */
1520 temp = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
1521 temp &= ~PORT_PLL_10BIT_CLK_ENABLE;
1522 I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
1525 temp = I915_READ(BXT_PORT_PLL_EBB_0(phy, ch));
1526 temp &= ~(PORT_PLL_P1_MASK | PORT_PLL_P2_MASK);
1527 temp |= pll->state.hw_state.ebb0;
1528 I915_WRITE(BXT_PORT_PLL_EBB_0(phy, ch), temp);
1530 /* Write M2 integer */
1531 temp = I915_READ(BXT_PORT_PLL(phy, ch, 0));
1532 temp &= ~PORT_PLL_M2_MASK;
1533 temp |= pll->state.hw_state.pll0;
1534 I915_WRITE(BXT_PORT_PLL(phy, ch, 0), temp);
1537 temp = I915_READ(BXT_PORT_PLL(phy, ch, 1));
1538 temp &= ~PORT_PLL_N_MASK;
1539 temp |= pll->state.hw_state.pll1;
1540 I915_WRITE(BXT_PORT_PLL(phy, ch, 1), temp);
1542 /* Write M2 fraction */
1543 temp = I915_READ(BXT_PORT_PLL(phy, ch, 2));
1544 temp &= ~PORT_PLL_M2_FRAC_MASK;
1545 temp |= pll->state.hw_state.pll2;
1546 I915_WRITE(BXT_PORT_PLL(phy, ch, 2), temp);
1548 /* Write M2 fraction enable */
1549 temp = I915_READ(BXT_PORT_PLL(phy, ch, 3));
1550 temp &= ~PORT_PLL_M2_FRAC_ENABLE;
1551 temp |= pll->state.hw_state.pll3;
1552 I915_WRITE(BXT_PORT_PLL(phy, ch, 3), temp);
1555 temp = I915_READ(BXT_PORT_PLL(phy, ch, 6));
1556 temp &= ~PORT_PLL_PROP_COEFF_MASK;
1557 temp &= ~PORT_PLL_INT_COEFF_MASK;
1558 temp &= ~PORT_PLL_GAIN_CTL_MASK;
1559 temp |= pll->state.hw_state.pll6;
1560 I915_WRITE(BXT_PORT_PLL(phy, ch, 6), temp);
1562 /* Write calibration val */
1563 temp = I915_READ(BXT_PORT_PLL(phy, ch, 8));
1564 temp &= ~PORT_PLL_TARGET_CNT_MASK;
1565 temp |= pll->state.hw_state.pll8;
1566 I915_WRITE(BXT_PORT_PLL(phy, ch, 8), temp);
1568 temp = I915_READ(BXT_PORT_PLL(phy, ch, 9));
1569 temp &= ~PORT_PLL_LOCK_THRESHOLD_MASK;
1570 temp |= pll->state.hw_state.pll9;
1571 I915_WRITE(BXT_PORT_PLL(phy, ch, 9), temp);
1573 temp = I915_READ(BXT_PORT_PLL(phy, ch, 10));
1574 temp &= ~PORT_PLL_DCO_AMP_OVR_EN_H;
1575 temp &= ~PORT_PLL_DCO_AMP_MASK;
1576 temp |= pll->state.hw_state.pll10;
1577 I915_WRITE(BXT_PORT_PLL(phy, ch, 10), temp);
1579 /* Recalibrate with new settings */
1580 temp = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
1581 temp |= PORT_PLL_RECALIBRATE;
1582 I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
1583 temp &= ~PORT_PLL_10BIT_CLK_ENABLE;
1584 temp |= pll->state.hw_state.ebb4;
1585 I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
1588 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1589 temp |= PORT_PLL_ENABLE;
1590 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1591 POSTING_READ(BXT_PORT_PLL_ENABLE(port));
1593 if (wait_for_us((I915_READ(BXT_PORT_PLL_ENABLE(port)) & PORT_PLL_LOCK),
1595 DRM_ERROR("PLL %d not locked\n", port);
1597 if (IS_GEMINILAKE(dev_priv)) {
1598 temp = I915_READ(BXT_PORT_TX_DW5_LN0(phy, ch));
1599 temp |= DCC_DELAY_RANGE_2;
1600 I915_WRITE(BXT_PORT_TX_DW5_GRP(phy, ch), temp);
1604 * While we write to the group register to program all lanes at once we
1605 * can read only lane registers and we pick lanes 0/1 for that.
1607 temp = I915_READ(BXT_PORT_PCS_DW12_LN01(phy, ch));
1608 temp &= ~LANE_STAGGER_MASK;
1609 temp &= ~LANESTAGGER_STRAP_OVRD;
1610 temp |= pll->state.hw_state.pcsdw12;
1611 I915_WRITE(BXT_PORT_PCS_DW12_GRP(phy, ch), temp);
1614 static void bxt_ddi_pll_disable(struct drm_i915_private *dev_priv,
1615 struct intel_shared_dpll *pll)
1617 enum port port = (enum port)pll->info->id; /* 1:1 port->PLL mapping */
1620 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1621 temp &= ~PORT_PLL_ENABLE;
1622 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1623 POSTING_READ(BXT_PORT_PLL_ENABLE(port));
1625 if (IS_GEMINILAKE(dev_priv)) {
1626 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1627 temp &= ~PORT_PLL_POWER_ENABLE;
1628 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1630 if (wait_for_us(!(I915_READ(BXT_PORT_PLL_ENABLE(port)) &
1631 PORT_PLL_POWER_STATE), 200))
1632 DRM_ERROR("Power state not reset for PLL:%d\n", port);
1636 static bool bxt_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
1637 struct intel_shared_dpll *pll,
1638 struct intel_dpll_hw_state *hw_state)
1640 enum port port = (enum port)pll->info->id; /* 1:1 port->PLL mapping */
1641 intel_wakeref_t wakeref;
1643 enum dpio_channel ch;
1647 bxt_port_to_phy_channel(dev_priv, port, &phy, &ch);
1649 wakeref = intel_display_power_get_if_enabled(dev_priv,
1650 POWER_DOMAIN_DISPLAY_CORE);
1656 val = I915_READ(BXT_PORT_PLL_ENABLE(port));
1657 if (!(val & PORT_PLL_ENABLE))
1660 hw_state->ebb0 = I915_READ(BXT_PORT_PLL_EBB_0(phy, ch));
1661 hw_state->ebb0 &= PORT_PLL_P1_MASK | PORT_PLL_P2_MASK;
1663 hw_state->ebb4 = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
1664 hw_state->ebb4 &= PORT_PLL_10BIT_CLK_ENABLE;
1666 hw_state->pll0 = I915_READ(BXT_PORT_PLL(phy, ch, 0));
1667 hw_state->pll0 &= PORT_PLL_M2_MASK;
1669 hw_state->pll1 = I915_READ(BXT_PORT_PLL(phy, ch, 1));
1670 hw_state->pll1 &= PORT_PLL_N_MASK;
1672 hw_state->pll2 = I915_READ(BXT_PORT_PLL(phy, ch, 2));
1673 hw_state->pll2 &= PORT_PLL_M2_FRAC_MASK;
1675 hw_state->pll3 = I915_READ(BXT_PORT_PLL(phy, ch, 3));
1676 hw_state->pll3 &= PORT_PLL_M2_FRAC_ENABLE;
1678 hw_state->pll6 = I915_READ(BXT_PORT_PLL(phy, ch, 6));
1679 hw_state->pll6 &= PORT_PLL_PROP_COEFF_MASK |
1680 PORT_PLL_INT_COEFF_MASK |
1681 PORT_PLL_GAIN_CTL_MASK;
1683 hw_state->pll8 = I915_READ(BXT_PORT_PLL(phy, ch, 8));
1684 hw_state->pll8 &= PORT_PLL_TARGET_CNT_MASK;
1686 hw_state->pll9 = I915_READ(BXT_PORT_PLL(phy, ch, 9));
1687 hw_state->pll9 &= PORT_PLL_LOCK_THRESHOLD_MASK;
1689 hw_state->pll10 = I915_READ(BXT_PORT_PLL(phy, ch, 10));
1690 hw_state->pll10 &= PORT_PLL_DCO_AMP_OVR_EN_H |
1691 PORT_PLL_DCO_AMP_MASK;
1694 * While we write to the group register to program all lanes at once we
1695 * can read only lane registers. We configure all lanes the same way, so
1696 * here just read out lanes 0/1 and output a note if lanes 2/3 differ.
1698 hw_state->pcsdw12 = I915_READ(BXT_PORT_PCS_DW12_LN01(phy, ch));
1699 if (I915_READ(BXT_PORT_PCS_DW12_LN23(phy, ch)) != hw_state->pcsdw12)
1700 DRM_DEBUG_DRIVER("lane stagger config different for lane 01 (%08x) and 23 (%08x)\n",
1702 I915_READ(BXT_PORT_PCS_DW12_LN23(phy, ch)));
1703 hw_state->pcsdw12 &= LANE_STAGGER_MASK | LANESTAGGER_STRAP_OVRD;
1708 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
1713 /* bxt clock parameters */
1714 struct bxt_clk_div {
1726 /* pre-calculated values for DP linkrates */
1727 static const struct bxt_clk_div bxt_dp_clk_val[] = {
1728 {162000, 4, 2, 32, 1677722, 1, 1},
1729 {270000, 4, 1, 27, 0, 0, 1},
1730 {540000, 2, 1, 27, 0, 0, 1},
1731 {216000, 3, 2, 32, 1677722, 1, 1},
1732 {243000, 4, 1, 24, 1258291, 1, 1},
1733 {324000, 4, 1, 32, 1677722, 1, 1},
1734 {432000, 3, 1, 32, 1677722, 1, 1}
1738 bxt_ddi_hdmi_pll_dividers(struct intel_crtc_state *crtc_state,
1739 struct bxt_clk_div *clk_div)
1741 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1742 struct dpll best_clock;
1744 /* Calculate HDMI div */
1746 * FIXME: tie the following calculation into
1747 * i9xx_crtc_compute_clock
1749 if (!bxt_find_best_dpll(crtc_state, &best_clock)) {
1750 DRM_DEBUG_DRIVER("no PLL dividers found for clock %d pipe %c\n",
1751 crtc_state->port_clock,
1752 pipe_name(crtc->pipe));
1756 clk_div->p1 = best_clock.p1;
1757 clk_div->p2 = best_clock.p2;
1758 WARN_ON(best_clock.m1 != 2);
1759 clk_div->n = best_clock.n;
1760 clk_div->m2_int = best_clock.m2 >> 22;
1761 clk_div->m2_frac = best_clock.m2 & ((1 << 22) - 1);
1762 clk_div->m2_frac_en = clk_div->m2_frac != 0;
1764 clk_div->vco = best_clock.vco;
1769 static void bxt_ddi_dp_pll_dividers(struct intel_crtc_state *crtc_state,
1770 struct bxt_clk_div *clk_div)
1772 int clock = crtc_state->port_clock;
1775 *clk_div = bxt_dp_clk_val[0];
1776 for (i = 0; i < ARRAY_SIZE(bxt_dp_clk_val); ++i) {
1777 if (bxt_dp_clk_val[i].clock == clock) {
1778 *clk_div = bxt_dp_clk_val[i];
1783 clk_div->vco = clock * 10 / 2 * clk_div->p1 * clk_div->p2;
1786 static bool bxt_ddi_set_dpll_hw_state(struct intel_crtc_state *crtc_state,
1787 const struct bxt_clk_div *clk_div)
1789 struct intel_dpll_hw_state *dpll_hw_state = &crtc_state->dpll_hw_state;
1790 int clock = crtc_state->port_clock;
1791 int vco = clk_div->vco;
1792 u32 prop_coef, int_coef, gain_ctl, targ_cnt;
1795 memset(dpll_hw_state, 0, sizeof(*dpll_hw_state));
1797 if (vco >= 6200000 && vco <= 6700000) {
1802 } else if ((vco > 5400000 && vco < 6200000) ||
1803 (vco >= 4800000 && vco < 5400000)) {
1808 } else if (vco == 5400000) {
1814 DRM_ERROR("Invalid VCO\n");
1820 else if (clock > 135000)
1822 else if (clock > 67000)
1824 else if (clock > 33000)
1829 dpll_hw_state->ebb0 = PORT_PLL_P1(clk_div->p1) | PORT_PLL_P2(clk_div->p2);
1830 dpll_hw_state->pll0 = clk_div->m2_int;
1831 dpll_hw_state->pll1 = PORT_PLL_N(clk_div->n);
1832 dpll_hw_state->pll2 = clk_div->m2_frac;
1834 if (clk_div->m2_frac_en)
1835 dpll_hw_state->pll3 = PORT_PLL_M2_FRAC_ENABLE;
1837 dpll_hw_state->pll6 = prop_coef | PORT_PLL_INT_COEFF(int_coef);
1838 dpll_hw_state->pll6 |= PORT_PLL_GAIN_CTL(gain_ctl);
1840 dpll_hw_state->pll8 = targ_cnt;
1842 dpll_hw_state->pll9 = 5 << PORT_PLL_LOCK_THRESHOLD_SHIFT;
1844 dpll_hw_state->pll10 =
1845 PORT_PLL_DCO_AMP(PORT_PLL_DCO_AMP_DEFAULT)
1846 | PORT_PLL_DCO_AMP_OVR_EN_H;
1848 dpll_hw_state->ebb4 = PORT_PLL_10BIT_CLK_ENABLE;
1850 dpll_hw_state->pcsdw12 = LANESTAGGER_STRAP_OVRD | lanestagger;
1856 bxt_ddi_dp_set_dpll_hw_state(struct intel_crtc_state *crtc_state)
1858 struct bxt_clk_div clk_div = {};
1860 bxt_ddi_dp_pll_dividers(crtc_state, &clk_div);
1862 return bxt_ddi_set_dpll_hw_state(crtc_state, &clk_div);
1866 bxt_ddi_hdmi_set_dpll_hw_state(struct intel_crtc_state *crtc_state)
1868 struct bxt_clk_div clk_div = {};
1870 bxt_ddi_hdmi_pll_dividers(crtc_state, &clk_div);
1872 return bxt_ddi_set_dpll_hw_state(crtc_state, &clk_div);
1875 static bool bxt_get_dpll(struct intel_atomic_state *state,
1876 struct intel_crtc *crtc,
1877 struct intel_encoder *encoder)
1879 struct intel_crtc_state *crtc_state =
1880 intel_atomic_get_new_crtc_state(state, crtc);
1881 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1882 struct intel_shared_dpll *pll;
1883 enum intel_dpll_id id;
1885 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) &&
1886 !bxt_ddi_hdmi_set_dpll_hw_state(crtc_state))
1889 if (intel_crtc_has_dp_encoder(crtc_state) &&
1890 !bxt_ddi_dp_set_dpll_hw_state(crtc_state))
1893 /* 1:1 mapping between ports and PLLs */
1894 id = (enum intel_dpll_id) encoder->port;
1895 pll = intel_get_shared_dpll_by_id(dev_priv, id);
1897 DRM_DEBUG_KMS("[CRTC:%d:%s] using pre-allocated %s\n",
1898 crtc->base.base.id, crtc->base.name, pll->info->name);
1900 intel_reference_shared_dpll(state, crtc,
1901 pll, &crtc_state->dpll_hw_state);
1903 crtc_state->shared_dpll = pll;
1908 static void bxt_dump_hw_state(struct drm_i915_private *dev_priv,
1909 const struct intel_dpll_hw_state *hw_state)
1911 DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
1912 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
1913 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
1927 static const struct intel_shared_dpll_funcs bxt_ddi_pll_funcs = {
1928 .enable = bxt_ddi_pll_enable,
1929 .disable = bxt_ddi_pll_disable,
1930 .get_hw_state = bxt_ddi_pll_get_hw_state,
1933 struct intel_dpll_mgr {
1934 const struct dpll_info *dpll_info;
1936 bool (*get_dplls)(struct intel_atomic_state *state,
1937 struct intel_crtc *crtc,
1938 struct intel_encoder *encoder);
1939 void (*put_dplls)(struct intel_atomic_state *state,
1940 struct intel_crtc *crtc);
1941 void (*update_active_dpll)(struct intel_atomic_state *state,
1942 struct intel_crtc *crtc,
1943 struct intel_encoder *encoder);
1944 void (*dump_hw_state)(struct drm_i915_private *dev_priv,
1945 const struct intel_dpll_hw_state *hw_state);
1948 static const struct dpll_info pch_plls[] = {
1949 { "PCH DPLL A", &ibx_pch_dpll_funcs, DPLL_ID_PCH_PLL_A, 0 },
1950 { "PCH DPLL B", &ibx_pch_dpll_funcs, DPLL_ID_PCH_PLL_B, 0 },
1954 static const struct intel_dpll_mgr pch_pll_mgr = {
1955 .dpll_info = pch_plls,
1956 .get_dplls = ibx_get_dpll,
1957 .put_dplls = intel_put_dpll,
1958 .dump_hw_state = ibx_dump_hw_state,
1961 static const struct dpll_info hsw_plls[] = {
1962 { "WRPLL 1", &hsw_ddi_wrpll_funcs, DPLL_ID_WRPLL1, 0 },
1963 { "WRPLL 2", &hsw_ddi_wrpll_funcs, DPLL_ID_WRPLL2, 0 },
1964 { "SPLL", &hsw_ddi_spll_funcs, DPLL_ID_SPLL, 0 },
1965 { "LCPLL 810", &hsw_ddi_lcpll_funcs, DPLL_ID_LCPLL_810, INTEL_DPLL_ALWAYS_ON },
1966 { "LCPLL 1350", &hsw_ddi_lcpll_funcs, DPLL_ID_LCPLL_1350, INTEL_DPLL_ALWAYS_ON },
1967 { "LCPLL 2700", &hsw_ddi_lcpll_funcs, DPLL_ID_LCPLL_2700, INTEL_DPLL_ALWAYS_ON },
1971 static const struct intel_dpll_mgr hsw_pll_mgr = {
1972 .dpll_info = hsw_plls,
1973 .get_dplls = hsw_get_dpll,
1974 .put_dplls = intel_put_dpll,
1975 .dump_hw_state = hsw_dump_hw_state,
1978 static const struct dpll_info skl_plls[] = {
1979 { "DPLL 0", &skl_ddi_dpll0_funcs, DPLL_ID_SKL_DPLL0, INTEL_DPLL_ALWAYS_ON },
1980 { "DPLL 1", &skl_ddi_pll_funcs, DPLL_ID_SKL_DPLL1, 0 },
1981 { "DPLL 2", &skl_ddi_pll_funcs, DPLL_ID_SKL_DPLL2, 0 },
1982 { "DPLL 3", &skl_ddi_pll_funcs, DPLL_ID_SKL_DPLL3, 0 },
1986 static const struct intel_dpll_mgr skl_pll_mgr = {
1987 .dpll_info = skl_plls,
1988 .get_dplls = skl_get_dpll,
1989 .put_dplls = intel_put_dpll,
1990 .dump_hw_state = skl_dump_hw_state,
1993 static const struct dpll_info bxt_plls[] = {
1994 { "PORT PLL A", &bxt_ddi_pll_funcs, DPLL_ID_SKL_DPLL0, 0 },
1995 { "PORT PLL B", &bxt_ddi_pll_funcs, DPLL_ID_SKL_DPLL1, 0 },
1996 { "PORT PLL C", &bxt_ddi_pll_funcs, DPLL_ID_SKL_DPLL2, 0 },
2000 static const struct intel_dpll_mgr bxt_pll_mgr = {
2001 .dpll_info = bxt_plls,
2002 .get_dplls = bxt_get_dpll,
2003 .put_dplls = intel_put_dpll,
2004 .dump_hw_state = bxt_dump_hw_state,
2007 static void cnl_ddi_pll_enable(struct drm_i915_private *dev_priv,
2008 struct intel_shared_dpll *pll)
2010 const enum intel_dpll_id id = pll->info->id;
2013 /* 1. Enable DPLL power in DPLL_ENABLE. */
2014 val = I915_READ(CNL_DPLL_ENABLE(id));
2015 val |= PLL_POWER_ENABLE;
2016 I915_WRITE(CNL_DPLL_ENABLE(id), val);
2018 /* 2. Wait for DPLL power state enabled in DPLL_ENABLE. */
2019 if (intel_wait_for_register(&dev_priv->uncore,
2020 CNL_DPLL_ENABLE(id),
2024 DRM_ERROR("PLL %d Power not enabled\n", id);
2027 * 3. Configure DPLL_CFGCR0 to set SSC enable/disable,
2028 * select DP mode, and set DP link rate.
2030 val = pll->state.hw_state.cfgcr0;
2031 I915_WRITE(CNL_DPLL_CFGCR0(id), val);
2033 /* 4. Reab back to ensure writes completed */
2034 POSTING_READ(CNL_DPLL_CFGCR0(id));
2036 /* 3. Configure DPLL_CFGCR0 */
2037 /* Avoid touch CFGCR1 if HDMI mode is not enabled */
2038 if (pll->state.hw_state.cfgcr0 & DPLL_CFGCR0_HDMI_MODE) {
2039 val = pll->state.hw_state.cfgcr1;
2040 I915_WRITE(CNL_DPLL_CFGCR1(id), val);
2041 /* 4. Reab back to ensure writes completed */
2042 POSTING_READ(CNL_DPLL_CFGCR1(id));
2046 * 5. If the frequency will result in a change to the voltage
2047 * requirement, follow the Display Voltage Frequency Switching
2048 * Sequence Before Frequency Change
2050 * Note: DVFS is actually handled via the cdclk code paths,
2051 * hence we do nothing here.
2054 /* 6. Enable DPLL in DPLL_ENABLE. */
2055 val = I915_READ(CNL_DPLL_ENABLE(id));
2057 I915_WRITE(CNL_DPLL_ENABLE(id), val);
2059 /* 7. Wait for PLL lock status in DPLL_ENABLE. */
2060 if (intel_wait_for_register(&dev_priv->uncore,
2061 CNL_DPLL_ENABLE(id),
2065 DRM_ERROR("PLL %d not locked\n", id);
2068 * 8. If the frequency will result in a change to the voltage
2069 * requirement, follow the Display Voltage Frequency Switching
2070 * Sequence After Frequency Change
2072 * Note: DVFS is actually handled via the cdclk code paths,
2073 * hence we do nothing here.
2077 * 9. turn on the clock for the DDI and map the DPLL to the DDI
2078 * Done at intel_ddi_clk_select
2082 static void cnl_ddi_pll_disable(struct drm_i915_private *dev_priv,
2083 struct intel_shared_dpll *pll)
2085 const enum intel_dpll_id id = pll->info->id;
2089 * 1. Configure DPCLKA_CFGCR0 to turn off the clock for the DDI.
2090 * Done at intel_ddi_post_disable
2094 * 2. If the frequency will result in a change to the voltage
2095 * requirement, follow the Display Voltage Frequency Switching
2096 * Sequence Before Frequency Change
2098 * Note: DVFS is actually handled via the cdclk code paths,
2099 * hence we do nothing here.
2102 /* 3. Disable DPLL through DPLL_ENABLE. */
2103 val = I915_READ(CNL_DPLL_ENABLE(id));
2105 I915_WRITE(CNL_DPLL_ENABLE(id), val);
2107 /* 4. Wait for PLL not locked status in DPLL_ENABLE. */
2108 if (intel_wait_for_register(&dev_priv->uncore,
2109 CNL_DPLL_ENABLE(id),
2113 DRM_ERROR("PLL %d locked\n", id);
2116 * 5. If the frequency will result in a change to the voltage
2117 * requirement, follow the Display Voltage Frequency Switching
2118 * Sequence After Frequency Change
2120 * Note: DVFS is actually handled via the cdclk code paths,
2121 * hence we do nothing here.
2124 /* 6. Disable DPLL power in DPLL_ENABLE. */
2125 val = I915_READ(CNL_DPLL_ENABLE(id));
2126 val &= ~PLL_POWER_ENABLE;
2127 I915_WRITE(CNL_DPLL_ENABLE(id), val);
2129 /* 7. Wait for DPLL power state disabled in DPLL_ENABLE. */
2130 if (intel_wait_for_register(&dev_priv->uncore,
2131 CNL_DPLL_ENABLE(id),
2135 DRM_ERROR("PLL %d Power not disabled\n", id);
2138 static bool cnl_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
2139 struct intel_shared_dpll *pll,
2140 struct intel_dpll_hw_state *hw_state)
2142 const enum intel_dpll_id id = pll->info->id;
2143 intel_wakeref_t wakeref;
2147 wakeref = intel_display_power_get_if_enabled(dev_priv,
2148 POWER_DOMAIN_DISPLAY_CORE);
2154 val = I915_READ(CNL_DPLL_ENABLE(id));
2155 if (!(val & PLL_ENABLE))
2158 val = I915_READ(CNL_DPLL_CFGCR0(id));
2159 hw_state->cfgcr0 = val;
2161 /* avoid reading back stale values if HDMI mode is not enabled */
2162 if (val & DPLL_CFGCR0_HDMI_MODE) {
2163 hw_state->cfgcr1 = I915_READ(CNL_DPLL_CFGCR1(id));
2168 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
2173 static void cnl_wrpll_get_multipliers(int bestdiv, int *pdiv,
2174 int *qdiv, int *kdiv)
2177 if (bestdiv % 2 == 0) {
2182 } else if (bestdiv % 4 == 0) {
2184 *qdiv = bestdiv / 4;
2186 } else if (bestdiv % 6 == 0) {
2188 *qdiv = bestdiv / 6;
2190 } else if (bestdiv % 5 == 0) {
2192 *qdiv = bestdiv / 10;
2194 } else if (bestdiv % 14 == 0) {
2196 *qdiv = bestdiv / 14;
2200 if (bestdiv == 3 || bestdiv == 5 || bestdiv == 7) {
2204 } else { /* 9, 15, 21 */
2205 *pdiv = bestdiv / 3;
2212 static void cnl_wrpll_params_populate(struct skl_wrpll_params *params,
2213 u32 dco_freq, u32 ref_freq,
2214 int pdiv, int qdiv, int kdiv)
2229 WARN(1, "Incorrect KDiv\n");
2246 WARN(1, "Incorrect PDiv\n");
2249 WARN_ON(kdiv != 2 && qdiv != 1);
2251 params->qdiv_ratio = qdiv;
2252 params->qdiv_mode = (qdiv == 1) ? 0 : 1;
2254 dco = div_u64((u64)dco_freq << 15, ref_freq);
2256 params->dco_integer = dco >> 15;
2257 params->dco_fraction = dco & 0x7fff;
2260 int cnl_hdmi_pll_ref_clock(struct drm_i915_private *dev_priv)
2262 int ref_clock = dev_priv->cdclk.hw.ref;
2265 * For ICL+, the spec states: if reference frequency is 38.4,
2266 * use 19.2 because the DPLL automatically divides that by 2.
2268 if (INTEL_GEN(dev_priv) >= 11 && ref_clock == 38400)
2275 cnl_ddi_calculate_wrpll(struct intel_crtc_state *crtc_state,
2276 struct skl_wrpll_params *wrpll_params)
2278 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2279 u32 afe_clock = crtc_state->port_clock * 5;
2281 u32 dco_min = 7998000;
2282 u32 dco_max = 10000000;
2283 u32 dco_mid = (dco_min + dco_max) / 2;
2284 static const int dividers[] = { 2, 4, 6, 8, 10, 12, 14, 16,
2285 18, 20, 24, 28, 30, 32, 36, 40,
2286 42, 44, 48, 50, 52, 54, 56, 60,
2287 64, 66, 68, 70, 72, 76, 78, 80,
2288 84, 88, 90, 92, 96, 98, 100, 102,
2289 3, 5, 7, 9, 15, 21 };
2290 u32 dco, best_dco = 0, dco_centrality = 0;
2291 u32 best_dco_centrality = U32_MAX; /* Spec meaning of 999999 MHz */
2292 int d, best_div = 0, pdiv = 0, qdiv = 0, kdiv = 0;
2294 for (d = 0; d < ARRAY_SIZE(dividers); d++) {
2295 dco = afe_clock * dividers[d];
2297 if ((dco <= dco_max) && (dco >= dco_min)) {
2298 dco_centrality = abs(dco - dco_mid);
2300 if (dco_centrality < best_dco_centrality) {
2301 best_dco_centrality = dco_centrality;
2302 best_div = dividers[d];
2311 cnl_wrpll_get_multipliers(best_div, &pdiv, &qdiv, &kdiv);
2313 ref_clock = cnl_hdmi_pll_ref_clock(dev_priv);
2315 cnl_wrpll_params_populate(wrpll_params, best_dco, ref_clock,
2321 static bool cnl_ddi_hdmi_pll_dividers(struct intel_crtc_state *crtc_state)
2324 struct skl_wrpll_params wrpll_params = { 0, };
2326 cfgcr0 = DPLL_CFGCR0_HDMI_MODE;
2328 if (!cnl_ddi_calculate_wrpll(crtc_state, &wrpll_params))
2331 cfgcr0 |= DPLL_CFGCR0_DCO_FRACTION(wrpll_params.dco_fraction) |
2332 wrpll_params.dco_integer;
2334 cfgcr1 = DPLL_CFGCR1_QDIV_RATIO(wrpll_params.qdiv_ratio) |
2335 DPLL_CFGCR1_QDIV_MODE(wrpll_params.qdiv_mode) |
2336 DPLL_CFGCR1_KDIV(wrpll_params.kdiv) |
2337 DPLL_CFGCR1_PDIV(wrpll_params.pdiv) |
2338 DPLL_CFGCR1_CENTRAL_FREQ;
2340 memset(&crtc_state->dpll_hw_state, 0,
2341 sizeof(crtc_state->dpll_hw_state));
2343 crtc_state->dpll_hw_state.cfgcr0 = cfgcr0;
2344 crtc_state->dpll_hw_state.cfgcr1 = cfgcr1;
2349 cnl_ddi_dp_set_dpll_hw_state(struct intel_crtc_state *crtc_state)
2353 cfgcr0 = DPLL_CFGCR0_SSC_ENABLE;
2355 switch (crtc_state->port_clock / 2) {
2357 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_810;
2360 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_1350;
2363 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_2700;
2367 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_1620;
2370 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_1080;
2373 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_2160;
2376 /* Some SKUs may require elevated I/O voltage to support this */
2377 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_3240;
2380 /* Some SKUs may require elevated I/O voltage to support this */
2381 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_4050;
2385 memset(&crtc_state->dpll_hw_state, 0,
2386 sizeof(crtc_state->dpll_hw_state));
2388 crtc_state->dpll_hw_state.cfgcr0 = cfgcr0;
2393 static bool cnl_get_dpll(struct intel_atomic_state *state,
2394 struct intel_crtc *crtc,
2395 struct intel_encoder *encoder)
2397 struct intel_crtc_state *crtc_state =
2398 intel_atomic_get_new_crtc_state(state, crtc);
2399 struct intel_shared_dpll *pll;
2402 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
2403 bret = cnl_ddi_hdmi_pll_dividers(crtc_state);
2405 DRM_DEBUG_KMS("Could not get HDMI pll dividers.\n");
2408 } else if (intel_crtc_has_dp_encoder(crtc_state)) {
2409 bret = cnl_ddi_dp_set_dpll_hw_state(crtc_state);
2411 DRM_DEBUG_KMS("Could not set DP dpll HW state.\n");
2415 DRM_DEBUG_KMS("Skip DPLL setup for output_types 0x%x\n",
2416 crtc_state->output_types);
2420 pll = intel_find_shared_dpll(state, crtc,
2421 &crtc_state->dpll_hw_state,
2425 DRM_DEBUG_KMS("No PLL selected\n");
2429 intel_reference_shared_dpll(state, crtc,
2430 pll, &crtc_state->dpll_hw_state);
2432 crtc_state->shared_dpll = pll;
2437 static void cnl_dump_hw_state(struct drm_i915_private *dev_priv,
2438 const struct intel_dpll_hw_state *hw_state)
2440 DRM_DEBUG_KMS("dpll_hw_state: "
2441 "cfgcr0: 0x%x, cfgcr1: 0x%x\n",
2446 static const struct intel_shared_dpll_funcs cnl_ddi_pll_funcs = {
2447 .enable = cnl_ddi_pll_enable,
2448 .disable = cnl_ddi_pll_disable,
2449 .get_hw_state = cnl_ddi_pll_get_hw_state,
2452 static const struct dpll_info cnl_plls[] = {
2453 { "DPLL 0", &cnl_ddi_pll_funcs, DPLL_ID_SKL_DPLL0, 0 },
2454 { "DPLL 1", &cnl_ddi_pll_funcs, DPLL_ID_SKL_DPLL1, 0 },
2455 { "DPLL 2", &cnl_ddi_pll_funcs, DPLL_ID_SKL_DPLL2, 0 },
2459 static const struct intel_dpll_mgr cnl_pll_mgr = {
2460 .dpll_info = cnl_plls,
2461 .get_dplls = cnl_get_dpll,
2462 .put_dplls = intel_put_dpll,
2463 .dump_hw_state = cnl_dump_hw_state,
2466 struct icl_combo_pll_params {
2468 struct skl_wrpll_params wrpll;
2472 * These values alrea already adjusted: they're the bits we write to the
2473 * registers, not the logical values.
2475 static const struct icl_combo_pll_params icl_dp_combo_pll_24MHz_values[] = {
2477 { .dco_integer = 0x151, .dco_fraction = 0x4000, /* [0]: 5.4 */
2478 .pdiv = 0x2 /* 3 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2480 { .dco_integer = 0x151, .dco_fraction = 0x4000, /* [1]: 2.7 */
2481 .pdiv = 0x2 /* 3 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2483 { .dco_integer = 0x151, .dco_fraction = 0x4000, /* [2]: 1.62 */
2484 .pdiv = 0x4 /* 5 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2486 { .dco_integer = 0x151, .dco_fraction = 0x4000, /* [3]: 3.24 */
2487 .pdiv = 0x4 /* 5 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2489 { .dco_integer = 0x168, .dco_fraction = 0x0000, /* [4]: 2.16 */
2490 .pdiv = 0x1 /* 2 */, .kdiv = 2, .qdiv_mode = 1, .qdiv_ratio = 2, }, },
2492 { .dco_integer = 0x168, .dco_fraction = 0x0000, /* [5]: 4.32 */
2493 .pdiv = 0x1 /* 2 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2495 { .dco_integer = 0x195, .dco_fraction = 0x0000, /* [6]: 6.48 */
2496 .pdiv = 0x2 /* 3 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2498 { .dco_integer = 0x151, .dco_fraction = 0x4000, /* [7]: 8.1 */
2499 .pdiv = 0x1 /* 2 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2503 /* Also used for 38.4 MHz values. */
2504 static const struct icl_combo_pll_params icl_dp_combo_pll_19_2MHz_values[] = {
2506 { .dco_integer = 0x1A5, .dco_fraction = 0x7000, /* [0]: 5.4 */
2507 .pdiv = 0x2 /* 3 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2509 { .dco_integer = 0x1A5, .dco_fraction = 0x7000, /* [1]: 2.7 */
2510 .pdiv = 0x2 /* 3 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2512 { .dco_integer = 0x1A5, .dco_fraction = 0x7000, /* [2]: 1.62 */
2513 .pdiv = 0x4 /* 5 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2515 { .dco_integer = 0x1A5, .dco_fraction = 0x7000, /* [3]: 3.24 */
2516 .pdiv = 0x4 /* 5 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2518 { .dco_integer = 0x1C2, .dco_fraction = 0x0000, /* [4]: 2.16 */
2519 .pdiv = 0x1 /* 2 */, .kdiv = 2, .qdiv_mode = 1, .qdiv_ratio = 2, }, },
2521 { .dco_integer = 0x1C2, .dco_fraction = 0x0000, /* [5]: 4.32 */
2522 .pdiv = 0x1 /* 2 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2524 { .dco_integer = 0x1FA, .dco_fraction = 0x2000, /* [6]: 6.48 */
2525 .pdiv = 0x2 /* 3 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2527 { .dco_integer = 0x1A5, .dco_fraction = 0x7000, /* [7]: 8.1 */
2528 .pdiv = 0x1 /* 2 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2531 static const struct skl_wrpll_params icl_tbt_pll_24MHz_values = {
2532 .dco_integer = 0x151, .dco_fraction = 0x4000,
2533 .pdiv = 0x4 /* 5 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0,
2536 static const struct skl_wrpll_params icl_tbt_pll_19_2MHz_values = {
2537 .dco_integer = 0x1A5, .dco_fraction = 0x7000,
2538 .pdiv = 0x4 /* 5 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0,
2541 static bool icl_calc_dp_combo_pll(struct intel_crtc_state *crtc_state,
2542 struct skl_wrpll_params *pll_params)
2544 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2545 const struct icl_combo_pll_params *params =
2546 dev_priv->cdclk.hw.ref == 24000 ?
2547 icl_dp_combo_pll_24MHz_values :
2548 icl_dp_combo_pll_19_2MHz_values;
2549 int clock = crtc_state->port_clock;
2552 for (i = 0; i < ARRAY_SIZE(icl_dp_combo_pll_24MHz_values); i++) {
2553 if (clock == params[i].clock) {
2554 *pll_params = params[i].wrpll;
2559 MISSING_CASE(clock);
2563 static bool icl_calc_tbt_pll(struct intel_crtc_state *crtc_state,
2564 struct skl_wrpll_params *pll_params)
2566 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2568 *pll_params = dev_priv->cdclk.hw.ref == 24000 ?
2569 icl_tbt_pll_24MHz_values : icl_tbt_pll_19_2MHz_values;
2573 static bool icl_calc_dpll_state(struct intel_crtc_state *crtc_state,
2574 struct intel_encoder *encoder,
2575 struct intel_dpll_hw_state *pll_state)
2577 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2579 struct skl_wrpll_params pll_params = { 0 };
2582 if (intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv,
2584 ret = icl_calc_tbt_pll(crtc_state, &pll_params);
2585 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
2586 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
2587 ret = cnl_ddi_calculate_wrpll(crtc_state, &pll_params);
2589 ret = icl_calc_dp_combo_pll(crtc_state, &pll_params);
2594 cfgcr0 = DPLL_CFGCR0_DCO_FRACTION(pll_params.dco_fraction) |
2595 pll_params.dco_integer;
2597 cfgcr1 = DPLL_CFGCR1_QDIV_RATIO(pll_params.qdiv_ratio) |
2598 DPLL_CFGCR1_QDIV_MODE(pll_params.qdiv_mode) |
2599 DPLL_CFGCR1_KDIV(pll_params.kdiv) |
2600 DPLL_CFGCR1_PDIV(pll_params.pdiv);
2602 if (INTEL_GEN(dev_priv) >= 12)
2603 cfgcr1 |= TGL_DPLL_CFGCR1_CFSELOVRD_NORMAL_XTAL;
2605 cfgcr1 |= DPLL_CFGCR1_CENTRAL_FREQ_8400;
2607 memset(pll_state, 0, sizeof(*pll_state));
2609 pll_state->cfgcr0 = cfgcr0;
2610 pll_state->cfgcr1 = cfgcr1;
2616 static enum tc_port icl_pll_id_to_tc_port(enum intel_dpll_id id)
2618 return id - DPLL_ID_ICL_MGPLL1;
2621 enum intel_dpll_id icl_tc_port_to_pll_id(enum tc_port tc_port)
2623 return tc_port + DPLL_ID_ICL_MGPLL1;
2626 static bool icl_mg_pll_find_divisors(int clock_khz, bool is_dp, bool use_ssc,
2627 u32 *target_dco_khz,
2628 struct intel_dpll_hw_state *state)
2630 u32 dco_min_freq, dco_max_freq;
2631 int div1_vals[] = {7, 5, 3, 2};
2635 dco_min_freq = is_dp ? 8100000 : use_ssc ? 8000000 : 7992000;
2636 dco_max_freq = is_dp ? 8100000 : 10000000;
2638 for (i = 0; i < ARRAY_SIZE(div1_vals); i++) {
2639 int div1 = div1_vals[i];
2641 for (div2 = 10; div2 > 0; div2--) {
2642 int dco = div1 * div2 * clock_khz * 5;
2643 int a_divratio, tlinedrv, inputsel;
2646 if (dco < dco_min_freq || dco > dco_max_freq)
2650 a_divratio = is_dp ? 10 : 5;
2656 inputsel = is_dp ? 0 : 1;
2663 hsdiv = MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_2;
2666 hsdiv = MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_3;
2669 hsdiv = MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_5;
2672 hsdiv = MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_7;
2676 *target_dco_khz = dco;
2678 state->mg_refclkin_ctl = MG_REFCLKIN_CTL_OD_2_MUX(1);
2680 state->mg_clktop2_coreclkctl1 =
2681 MG_CLKTOP2_CORECLKCTL1_A_DIVRATIO(a_divratio);
2683 state->mg_clktop2_hsclkctl =
2684 MG_CLKTOP2_HSCLKCTL_TLINEDRV_CLKSEL(tlinedrv) |
2685 MG_CLKTOP2_HSCLKCTL_CORE_INPUTSEL(inputsel) |
2687 MG_CLKTOP2_HSCLKCTL_DSDIV_RATIO(div2);
2697 * The specification for this function uses real numbers, so the math had to be
2698 * adapted to integer-only calculation, that's why it looks so different.
2700 static bool icl_calc_mg_pll_state(struct intel_crtc_state *crtc_state,
2701 struct intel_dpll_hw_state *pll_state)
2703 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2704 int refclk_khz = dev_priv->cdclk.hw.ref;
2705 int clock = crtc_state->port_clock;
2706 u32 dco_khz, m1div, m2div_int, m2div_rem, m2div_frac;
2707 u32 iref_ndiv, iref_trim, iref_pulse_w;
2708 u32 prop_coeff, int_coeff;
2709 u32 tdc_targetcnt, feedfwgain;
2710 u64 ssc_stepsize, ssc_steplen, ssc_steplog;
2712 bool use_ssc = false;
2713 bool is_dp = !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI);
2715 memset(pll_state, 0, sizeof(*pll_state));
2717 if (!icl_mg_pll_find_divisors(clock, is_dp, use_ssc, &dco_khz,
2719 DRM_DEBUG_KMS("Failed to find divisors for clock %d\n", clock);
2724 m2div_int = dco_khz / (refclk_khz * m1div);
2725 if (m2div_int > 255) {
2727 m2div_int = dco_khz / (refclk_khz * m1div);
2728 if (m2div_int > 255) {
2729 DRM_DEBUG_KMS("Failed to find mdiv for clock %d\n",
2734 m2div_rem = dco_khz % (refclk_khz * m1div);
2736 tmp = (u64)m2div_rem * (1 << 22);
2737 do_div(tmp, refclk_khz * m1div);
2740 switch (refclk_khz) {
2757 MISSING_CASE(refclk_khz);
2762 * tdc_res = 0.000003
2763 * tdc_targetcnt = int(2 / (tdc_res * 8 * 50 * 1.1) / refclk_mhz + 0.5)
2765 * The multiplication by 1000 is due to refclk MHz to KHz conversion. It
2766 * was supposed to be a division, but we rearranged the operations of
2767 * the formula to avoid early divisions so we don't multiply the
2770 * 0.000003 * 8 * 50 * 1.1 = 0.00132, also known as 132 / 100000, which
2771 * we also rearrange to work with integers.
2773 * The 0.5 transformed to 5 results in a multiplication by 10 and the
2774 * last division by 10.
2776 tdc_targetcnt = (2 * 1000 * 100000 * 10 / (132 * refclk_khz) + 5) / 10;
2779 * Here we divide dco_khz by 10 in order to allow the dividend to fit in
2780 * 32 bits. That's not a problem since we round the division down
2783 feedfwgain = (use_ssc || m2div_rem > 0) ?
2784 m1div * 1000000 * 100 / (dco_khz * 3 / 10) : 0;
2786 if (dco_khz >= 9000000) {
2795 tmp = mul_u32_u32(dco_khz, 47 * 32);
2796 do_div(tmp, refclk_khz * m1div * 10000);
2799 tmp = mul_u32_u32(dco_khz, 1000);
2800 ssc_steplen = DIV_ROUND_UP_ULL(tmp, 32 * 2 * 32);
2807 pll_state->mg_pll_div0 = (m2div_rem > 0 ? MG_PLL_DIV0_FRACNEN_H : 0) |
2808 MG_PLL_DIV0_FBDIV_FRAC(m2div_frac) |
2809 MG_PLL_DIV0_FBDIV_INT(m2div_int);
2811 pll_state->mg_pll_div1 = MG_PLL_DIV1_IREF_NDIVRATIO(iref_ndiv) |
2812 MG_PLL_DIV1_DITHER_DIV_2 |
2813 MG_PLL_DIV1_NDIVRATIO(1) |
2814 MG_PLL_DIV1_FBPREDIV(m1div);
2816 pll_state->mg_pll_lf = MG_PLL_LF_TDCTARGETCNT(tdc_targetcnt) |
2817 MG_PLL_LF_AFCCNTSEL_512 |
2818 MG_PLL_LF_GAINCTRL(1) |
2819 MG_PLL_LF_INT_COEFF(int_coeff) |
2820 MG_PLL_LF_PROP_COEFF(prop_coeff);
2822 pll_state->mg_pll_frac_lock = MG_PLL_FRAC_LOCK_TRUELOCK_CRIT_32 |
2823 MG_PLL_FRAC_LOCK_EARLYLOCK_CRIT_32 |
2824 MG_PLL_FRAC_LOCK_LOCKTHRESH(10) |
2825 MG_PLL_FRAC_LOCK_DCODITHEREN |
2826 MG_PLL_FRAC_LOCK_FEEDFWRDGAIN(feedfwgain);
2827 if (use_ssc || m2div_rem > 0)
2828 pll_state->mg_pll_frac_lock |= MG_PLL_FRAC_LOCK_FEEDFWRDCAL_EN;
2830 pll_state->mg_pll_ssc = (use_ssc ? MG_PLL_SSC_EN : 0) |
2831 MG_PLL_SSC_TYPE(2) |
2832 MG_PLL_SSC_STEPLENGTH(ssc_steplen) |
2833 MG_PLL_SSC_STEPNUM(ssc_steplog) |
2835 MG_PLL_SSC_STEPSIZE(ssc_stepsize);
2837 pll_state->mg_pll_tdc_coldst_bias = MG_PLL_TDC_COLDST_COLDSTART |
2838 MG_PLL_TDC_COLDST_IREFINT_EN |
2839 MG_PLL_TDC_COLDST_REFBIAS_START_PULSE_W(iref_pulse_w) |
2840 MG_PLL_TDC_TDCOVCCORR_EN |
2841 MG_PLL_TDC_TDCSEL(3);
2843 pll_state->mg_pll_bias = MG_PLL_BIAS_BIAS_GB_SEL(3) |
2844 MG_PLL_BIAS_INIT_DCOAMP(0x3F) |
2845 MG_PLL_BIAS_BIAS_BONUS(10) |
2846 MG_PLL_BIAS_BIASCAL_EN |
2847 MG_PLL_BIAS_CTRIM(12) |
2848 MG_PLL_BIAS_VREF_RDAC(4) |
2849 MG_PLL_BIAS_IREFTRIM(iref_trim);
2851 if (refclk_khz == 38400) {
2852 pll_state->mg_pll_tdc_coldst_bias_mask = MG_PLL_TDC_COLDST_COLDSTART;
2853 pll_state->mg_pll_bias_mask = 0;
2855 pll_state->mg_pll_tdc_coldst_bias_mask = -1U;
2856 pll_state->mg_pll_bias_mask = -1U;
2859 pll_state->mg_pll_tdc_coldst_bias &= pll_state->mg_pll_tdc_coldst_bias_mask;
2860 pll_state->mg_pll_bias &= pll_state->mg_pll_bias_mask;
2866 * icl_set_active_port_dpll - select the active port DPLL for a given CRTC
2867 * @crtc_state: state for the CRTC to select the DPLL for
2868 * @port_dpll_id: the active @port_dpll_id to select
2870 * Select the given @port_dpll_id instance from the DPLLs reserved for the
2873 void icl_set_active_port_dpll(struct intel_crtc_state *crtc_state,
2874 enum icl_port_dpll_id port_dpll_id)
2876 struct icl_port_dpll *port_dpll =
2877 &crtc_state->icl_port_dplls[port_dpll_id];
2879 crtc_state->shared_dpll = port_dpll->pll;
2880 crtc_state->dpll_hw_state = port_dpll->hw_state;
2883 static void icl_update_active_dpll(struct intel_atomic_state *state,
2884 struct intel_crtc *crtc,
2885 struct intel_encoder *encoder)
2887 struct intel_crtc_state *crtc_state =
2888 intel_atomic_get_new_crtc_state(state, crtc);
2889 struct intel_digital_port *primary_port;
2890 enum icl_port_dpll_id port_dpll_id = ICL_PORT_DPLL_DEFAULT;
2892 primary_port = encoder->type == INTEL_OUTPUT_DP_MST ?
2893 enc_to_mst(&encoder->base)->primary :
2894 enc_to_dig_port(&encoder->base);
2897 (primary_port->tc_mode == TC_PORT_DP_ALT ||
2898 primary_port->tc_mode == TC_PORT_LEGACY))
2899 port_dpll_id = ICL_PORT_DPLL_MG_PHY;
2901 icl_set_active_port_dpll(crtc_state, port_dpll_id);
2904 static bool icl_get_combo_phy_dpll(struct intel_atomic_state *state,
2905 struct intel_crtc *crtc,
2906 struct intel_encoder *encoder)
2908 struct intel_crtc_state *crtc_state =
2909 intel_atomic_get_new_crtc_state(state, crtc);
2910 struct icl_port_dpll *port_dpll =
2911 &crtc_state->icl_port_dplls[ICL_PORT_DPLL_DEFAULT];
2912 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2913 enum port port = encoder->port;
2914 bool has_dpll4 = false;
2916 if (!icl_calc_dpll_state(crtc_state, encoder, &port_dpll->hw_state)) {
2917 DRM_DEBUG_KMS("Could not calculate combo PHY PLL state.\n");
2922 if (IS_ELKHARTLAKE(dev_priv) && port != PORT_A)
2925 port_dpll->pll = intel_find_shared_dpll(state, crtc,
2926 &port_dpll->hw_state,
2928 has_dpll4 ? DPLL_ID_EHL_DPLL4
2929 : DPLL_ID_ICL_DPLL1);
2930 if (!port_dpll->pll) {
2931 DRM_DEBUG_KMS("No combo PHY PLL found for port %c\n",
2932 port_name(encoder->port));
2936 intel_reference_shared_dpll(state, crtc,
2937 port_dpll->pll, &port_dpll->hw_state);
2939 icl_update_active_dpll(state, crtc, encoder);
2944 static bool icl_get_tc_phy_dplls(struct intel_atomic_state *state,
2945 struct intel_crtc *crtc,
2946 struct intel_encoder *encoder)
2948 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2949 struct intel_crtc_state *crtc_state =
2950 intel_atomic_get_new_crtc_state(state, crtc);
2951 struct icl_port_dpll *port_dpll;
2952 enum intel_dpll_id dpll_id;
2954 port_dpll = &crtc_state->icl_port_dplls[ICL_PORT_DPLL_DEFAULT];
2955 if (!icl_calc_dpll_state(crtc_state, encoder, &port_dpll->hw_state)) {
2956 DRM_DEBUG_KMS("Could not calculate TBT PLL state.\n");
2960 port_dpll->pll = intel_find_shared_dpll(state, crtc,
2961 &port_dpll->hw_state,
2963 DPLL_ID_ICL_TBTPLL);
2964 if (!port_dpll->pll) {
2965 DRM_DEBUG_KMS("No TBT-ALT PLL found\n");
2968 intel_reference_shared_dpll(state, crtc,
2969 port_dpll->pll, &port_dpll->hw_state);
2972 port_dpll = &crtc_state->icl_port_dplls[ICL_PORT_DPLL_MG_PHY];
2973 if (!icl_calc_mg_pll_state(crtc_state, &port_dpll->hw_state)) {
2974 DRM_DEBUG_KMS("Could not calculate MG PHY PLL state.\n");
2975 goto err_unreference_tbt_pll;
2978 dpll_id = icl_tc_port_to_pll_id(intel_port_to_tc(dev_priv,
2980 port_dpll->pll = intel_find_shared_dpll(state, crtc,
2981 &port_dpll->hw_state,
2984 if (!port_dpll->pll) {
2985 DRM_DEBUG_KMS("No MG PHY PLL found\n");
2986 goto err_unreference_tbt_pll;
2988 intel_reference_shared_dpll(state, crtc,
2989 port_dpll->pll, &port_dpll->hw_state);
2991 icl_update_active_dpll(state, crtc, encoder);
2995 err_unreference_tbt_pll:
2996 port_dpll = &crtc_state->icl_port_dplls[ICL_PORT_DPLL_DEFAULT];
2997 intel_unreference_shared_dpll(state, crtc, port_dpll->pll);
3002 static bool icl_get_dplls(struct intel_atomic_state *state,
3003 struct intel_crtc *crtc,
3004 struct intel_encoder *encoder)
3006 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3007 enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3009 if (intel_phy_is_combo(dev_priv, phy))
3010 return icl_get_combo_phy_dpll(state, crtc, encoder);
3011 else if (intel_phy_is_tc(dev_priv, phy))
3012 return icl_get_tc_phy_dplls(state, crtc, encoder);
3019 static void icl_put_dplls(struct intel_atomic_state *state,
3020 struct intel_crtc *crtc)
3022 const struct intel_crtc_state *old_crtc_state =
3023 intel_atomic_get_old_crtc_state(state, crtc);
3024 struct intel_crtc_state *new_crtc_state =
3025 intel_atomic_get_new_crtc_state(state, crtc);
3026 enum icl_port_dpll_id id;
3028 new_crtc_state->shared_dpll = NULL;
3030 for (id = ICL_PORT_DPLL_DEFAULT; id < ICL_PORT_DPLL_COUNT; id++) {
3031 const struct icl_port_dpll *old_port_dpll =
3032 &old_crtc_state->icl_port_dplls[id];
3033 struct icl_port_dpll *new_port_dpll =
3034 &new_crtc_state->icl_port_dplls[id];
3036 new_port_dpll->pll = NULL;
3038 if (!old_port_dpll->pll)
3041 intel_unreference_shared_dpll(state, crtc, old_port_dpll->pll);
3045 static bool mg_pll_get_hw_state(struct drm_i915_private *dev_priv,
3046 struct intel_shared_dpll *pll,
3047 struct intel_dpll_hw_state *hw_state)
3049 const enum intel_dpll_id id = pll->info->id;
3050 enum tc_port tc_port = icl_pll_id_to_tc_port(id);
3051 intel_wakeref_t wakeref;
3055 wakeref = intel_display_power_get_if_enabled(dev_priv,
3056 POWER_DOMAIN_DISPLAY_CORE);
3060 val = I915_READ(MG_PLL_ENABLE(tc_port));
3061 if (!(val & PLL_ENABLE))
3064 hw_state->mg_refclkin_ctl = I915_READ(MG_REFCLKIN_CTL(tc_port));
3065 hw_state->mg_refclkin_ctl &= MG_REFCLKIN_CTL_OD_2_MUX_MASK;
3067 hw_state->mg_clktop2_coreclkctl1 =
3068 I915_READ(MG_CLKTOP2_CORECLKCTL1(tc_port));
3069 hw_state->mg_clktop2_coreclkctl1 &=
3070 MG_CLKTOP2_CORECLKCTL1_A_DIVRATIO_MASK;
3072 hw_state->mg_clktop2_hsclkctl =
3073 I915_READ(MG_CLKTOP2_HSCLKCTL(tc_port));
3074 hw_state->mg_clktop2_hsclkctl &=
3075 MG_CLKTOP2_HSCLKCTL_TLINEDRV_CLKSEL_MASK |
3076 MG_CLKTOP2_HSCLKCTL_CORE_INPUTSEL_MASK |
3077 MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_MASK |
3078 MG_CLKTOP2_HSCLKCTL_DSDIV_RATIO_MASK;
3080 hw_state->mg_pll_div0 = I915_READ(MG_PLL_DIV0(tc_port));
3081 hw_state->mg_pll_div1 = I915_READ(MG_PLL_DIV1(tc_port));
3082 hw_state->mg_pll_lf = I915_READ(MG_PLL_LF(tc_port));
3083 hw_state->mg_pll_frac_lock = I915_READ(MG_PLL_FRAC_LOCK(tc_port));
3084 hw_state->mg_pll_ssc = I915_READ(MG_PLL_SSC(tc_port));
3086 hw_state->mg_pll_bias = I915_READ(MG_PLL_BIAS(tc_port));
3087 hw_state->mg_pll_tdc_coldst_bias =
3088 I915_READ(MG_PLL_TDC_COLDST_BIAS(tc_port));
3090 if (dev_priv->cdclk.hw.ref == 38400) {
3091 hw_state->mg_pll_tdc_coldst_bias_mask = MG_PLL_TDC_COLDST_COLDSTART;
3092 hw_state->mg_pll_bias_mask = 0;
3094 hw_state->mg_pll_tdc_coldst_bias_mask = -1U;
3095 hw_state->mg_pll_bias_mask = -1U;
3098 hw_state->mg_pll_tdc_coldst_bias &= hw_state->mg_pll_tdc_coldst_bias_mask;
3099 hw_state->mg_pll_bias &= hw_state->mg_pll_bias_mask;
3103 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
3107 static bool icl_pll_get_hw_state(struct drm_i915_private *dev_priv,
3108 struct intel_shared_dpll *pll,
3109 struct intel_dpll_hw_state *hw_state,
3110 i915_reg_t enable_reg)
3112 const enum intel_dpll_id id = pll->info->id;
3113 intel_wakeref_t wakeref;
3117 wakeref = intel_display_power_get_if_enabled(dev_priv,
3118 POWER_DOMAIN_DISPLAY_CORE);
3122 val = I915_READ(enable_reg);
3123 if (!(val & PLL_ENABLE))
3126 if (INTEL_GEN(dev_priv) >= 12) {
3127 hw_state->cfgcr0 = I915_READ(TGL_DPLL_CFGCR0(id));
3128 hw_state->cfgcr1 = I915_READ(TGL_DPLL_CFGCR1(id));
3130 if (IS_ELKHARTLAKE(dev_priv) && id == DPLL_ID_EHL_DPLL4) {
3131 hw_state->cfgcr0 = I915_READ(ICL_DPLL_CFGCR0(4));
3132 hw_state->cfgcr1 = I915_READ(ICL_DPLL_CFGCR1(4));
3134 hw_state->cfgcr0 = I915_READ(ICL_DPLL_CFGCR0(id));
3135 hw_state->cfgcr1 = I915_READ(ICL_DPLL_CFGCR1(id));
3141 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
3145 static bool combo_pll_get_hw_state(struct drm_i915_private *dev_priv,
3146 struct intel_shared_dpll *pll,
3147 struct intel_dpll_hw_state *hw_state)
3149 i915_reg_t enable_reg = CNL_DPLL_ENABLE(pll->info->id);
3151 if (IS_ELKHARTLAKE(dev_priv) &&
3152 pll->info->id == DPLL_ID_EHL_DPLL4) {
3153 enable_reg = MG_PLL_ENABLE(0);
3156 return icl_pll_get_hw_state(dev_priv, pll, hw_state, enable_reg);
3159 static bool tbt_pll_get_hw_state(struct drm_i915_private *dev_priv,
3160 struct intel_shared_dpll *pll,
3161 struct intel_dpll_hw_state *hw_state)
3163 return icl_pll_get_hw_state(dev_priv, pll, hw_state, TBT_PLL_ENABLE);
3166 static void icl_dpll_write(struct drm_i915_private *dev_priv,
3167 struct intel_shared_dpll *pll)
3169 struct intel_dpll_hw_state *hw_state = &pll->state.hw_state;
3170 const enum intel_dpll_id id = pll->info->id;
3171 i915_reg_t cfgcr0_reg, cfgcr1_reg;
3173 if (INTEL_GEN(dev_priv) >= 12) {
3174 cfgcr0_reg = TGL_DPLL_CFGCR0(id);
3175 cfgcr1_reg = TGL_DPLL_CFGCR1(id);
3177 if (IS_ELKHARTLAKE(dev_priv) && id == DPLL_ID_EHL_DPLL4) {
3178 cfgcr0_reg = ICL_DPLL_CFGCR0(4);
3179 cfgcr1_reg = ICL_DPLL_CFGCR1(4);
3181 cfgcr0_reg = ICL_DPLL_CFGCR0(id);
3182 cfgcr1_reg = ICL_DPLL_CFGCR1(id);
3186 I915_WRITE(cfgcr0_reg, hw_state->cfgcr0);
3187 I915_WRITE(cfgcr1_reg, hw_state->cfgcr1);
3188 POSTING_READ(cfgcr1_reg);
3191 static void icl_mg_pll_write(struct drm_i915_private *dev_priv,
3192 struct intel_shared_dpll *pll)
3194 struct intel_dpll_hw_state *hw_state = &pll->state.hw_state;
3195 enum tc_port tc_port = icl_pll_id_to_tc_port(pll->info->id);
3199 * Some of the following registers have reserved fields, so program
3200 * these with RMW based on a mask. The mask can be fixed or generated
3201 * during the calc/readout phase if the mask depends on some other HW
3202 * state like refclk, see icl_calc_mg_pll_state().
3204 val = I915_READ(MG_REFCLKIN_CTL(tc_port));
3205 val &= ~MG_REFCLKIN_CTL_OD_2_MUX_MASK;
3206 val |= hw_state->mg_refclkin_ctl;
3207 I915_WRITE(MG_REFCLKIN_CTL(tc_port), val);
3209 val = I915_READ(MG_CLKTOP2_CORECLKCTL1(tc_port));
3210 val &= ~MG_CLKTOP2_CORECLKCTL1_A_DIVRATIO_MASK;
3211 val |= hw_state->mg_clktop2_coreclkctl1;
3212 I915_WRITE(MG_CLKTOP2_CORECLKCTL1(tc_port), val);
3214 val = I915_READ(MG_CLKTOP2_HSCLKCTL(tc_port));
3215 val &= ~(MG_CLKTOP2_HSCLKCTL_TLINEDRV_CLKSEL_MASK |
3216 MG_CLKTOP2_HSCLKCTL_CORE_INPUTSEL_MASK |
3217 MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_MASK |
3218 MG_CLKTOP2_HSCLKCTL_DSDIV_RATIO_MASK);
3219 val |= hw_state->mg_clktop2_hsclkctl;
3220 I915_WRITE(MG_CLKTOP2_HSCLKCTL(tc_port), val);
3222 I915_WRITE(MG_PLL_DIV0(tc_port), hw_state->mg_pll_div0);
3223 I915_WRITE(MG_PLL_DIV1(tc_port), hw_state->mg_pll_div1);
3224 I915_WRITE(MG_PLL_LF(tc_port), hw_state->mg_pll_lf);
3225 I915_WRITE(MG_PLL_FRAC_LOCK(tc_port), hw_state->mg_pll_frac_lock);
3226 I915_WRITE(MG_PLL_SSC(tc_port), hw_state->mg_pll_ssc);
3228 val = I915_READ(MG_PLL_BIAS(tc_port));
3229 val &= ~hw_state->mg_pll_bias_mask;
3230 val |= hw_state->mg_pll_bias;
3231 I915_WRITE(MG_PLL_BIAS(tc_port), val);
3233 val = I915_READ(MG_PLL_TDC_COLDST_BIAS(tc_port));
3234 val &= ~hw_state->mg_pll_tdc_coldst_bias_mask;
3235 val |= hw_state->mg_pll_tdc_coldst_bias;
3236 I915_WRITE(MG_PLL_TDC_COLDST_BIAS(tc_port), val);
3238 POSTING_READ(MG_PLL_TDC_COLDST_BIAS(tc_port));
3241 static void icl_pll_power_enable(struct drm_i915_private *dev_priv,
3242 struct intel_shared_dpll *pll,
3243 i915_reg_t enable_reg)
3247 val = I915_READ(enable_reg);
3248 val |= PLL_POWER_ENABLE;
3249 I915_WRITE(enable_reg, val);
3252 * The spec says we need to "wait" but it also says it should be
3255 if (intel_wait_for_register(&dev_priv->uncore, enable_reg,
3256 PLL_POWER_STATE, PLL_POWER_STATE, 1))
3257 DRM_ERROR("PLL %d Power not enabled\n", pll->info->id);
3260 static void icl_pll_enable(struct drm_i915_private *dev_priv,
3261 struct intel_shared_dpll *pll,
3262 i915_reg_t enable_reg)
3266 val = I915_READ(enable_reg);
3268 I915_WRITE(enable_reg, val);
3270 /* Timeout is actually 600us. */
3271 if (intel_wait_for_register(&dev_priv->uncore, enable_reg,
3272 PLL_LOCK, PLL_LOCK, 1))
3273 DRM_ERROR("PLL %d not locked\n", pll->info->id);
3276 static void combo_pll_enable(struct drm_i915_private *dev_priv,
3277 struct intel_shared_dpll *pll)
3279 i915_reg_t enable_reg = CNL_DPLL_ENABLE(pll->info->id);
3281 if (IS_ELKHARTLAKE(dev_priv) &&
3282 pll->info->id == DPLL_ID_EHL_DPLL4) {
3283 enable_reg = MG_PLL_ENABLE(0);
3286 * We need to disable DC states when this DPLL is enabled.
3287 * This can be done by taking a reference on DPLL4 power
3290 pll->wakeref = intel_display_power_get(dev_priv,
3291 POWER_DOMAIN_DPLL_DC_OFF);
3294 icl_pll_power_enable(dev_priv, pll, enable_reg);
3296 icl_dpll_write(dev_priv, pll);
3299 * DVFS pre sequence would be here, but in our driver the cdclk code
3300 * paths should already be setting the appropriate voltage, hence we do
3304 icl_pll_enable(dev_priv, pll, enable_reg);
3306 /* DVFS post sequence would be here. See the comment above. */
3309 static void tbt_pll_enable(struct drm_i915_private *dev_priv,
3310 struct intel_shared_dpll *pll)
3312 icl_pll_power_enable(dev_priv, pll, TBT_PLL_ENABLE);
3314 icl_dpll_write(dev_priv, pll);
3317 * DVFS pre sequence would be here, but in our driver the cdclk code
3318 * paths should already be setting the appropriate voltage, hence we do
3322 icl_pll_enable(dev_priv, pll, TBT_PLL_ENABLE);
3324 /* DVFS post sequence would be here. See the comment above. */
3327 static void mg_pll_enable(struct drm_i915_private *dev_priv,
3328 struct intel_shared_dpll *pll)
3330 i915_reg_t enable_reg =
3331 MG_PLL_ENABLE(icl_pll_id_to_tc_port(pll->info->id));
3333 icl_pll_power_enable(dev_priv, pll, enable_reg);
3335 icl_mg_pll_write(dev_priv, pll);
3338 * DVFS pre sequence would be here, but in our driver the cdclk code
3339 * paths should already be setting the appropriate voltage, hence we do
3343 icl_pll_enable(dev_priv, pll, enable_reg);
3345 /* DVFS post sequence would be here. See the comment above. */
3348 static void icl_pll_disable(struct drm_i915_private *dev_priv,
3349 struct intel_shared_dpll *pll,
3350 i915_reg_t enable_reg)
3354 /* The first steps are done by intel_ddi_post_disable(). */
3357 * DVFS pre sequence would be here, but in our driver the cdclk code
3358 * paths should already be setting the appropriate voltage, hence we do
3362 val = I915_READ(enable_reg);
3364 I915_WRITE(enable_reg, val);
3366 /* Timeout is actually 1us. */
3367 if (intel_wait_for_register(&dev_priv->uncore,
3368 enable_reg, PLL_LOCK, 0, 1))
3369 DRM_ERROR("PLL %d locked\n", pll->info->id);
3371 /* DVFS post sequence would be here. See the comment above. */
3373 val = I915_READ(enable_reg);
3374 val &= ~PLL_POWER_ENABLE;
3375 I915_WRITE(enable_reg, val);
3378 * The spec says we need to "wait" but it also says it should be
3381 if (intel_wait_for_register(&dev_priv->uncore,
3382 enable_reg, PLL_POWER_STATE, 0, 1))
3383 DRM_ERROR("PLL %d Power not disabled\n", pll->info->id);
3386 static void combo_pll_disable(struct drm_i915_private *dev_priv,
3387 struct intel_shared_dpll *pll)
3389 i915_reg_t enable_reg = CNL_DPLL_ENABLE(pll->info->id);
3391 if (IS_ELKHARTLAKE(dev_priv) &&
3392 pll->info->id == DPLL_ID_EHL_DPLL4) {
3393 enable_reg = MG_PLL_ENABLE(0);
3394 icl_pll_disable(dev_priv, pll, enable_reg);
3396 intel_display_power_put(dev_priv, POWER_DOMAIN_DPLL_DC_OFF,
3401 icl_pll_disable(dev_priv, pll, enable_reg);
3404 static void tbt_pll_disable(struct drm_i915_private *dev_priv,
3405 struct intel_shared_dpll *pll)
3407 icl_pll_disable(dev_priv, pll, TBT_PLL_ENABLE);
3410 static void mg_pll_disable(struct drm_i915_private *dev_priv,
3411 struct intel_shared_dpll *pll)
3413 i915_reg_t enable_reg =
3414 MG_PLL_ENABLE(icl_pll_id_to_tc_port(pll->info->id));
3416 icl_pll_disable(dev_priv, pll, enable_reg);
3419 static void icl_dump_hw_state(struct drm_i915_private *dev_priv,
3420 const struct intel_dpll_hw_state *hw_state)
3422 DRM_DEBUG_KMS("dpll_hw_state: cfgcr0: 0x%x, cfgcr1: 0x%x, "
3423 "mg_refclkin_ctl: 0x%x, hg_clktop2_coreclkctl1: 0x%x, "
3424 "mg_clktop2_hsclkctl: 0x%x, mg_pll_div0: 0x%x, "
3425 "mg_pll_div2: 0x%x, mg_pll_lf: 0x%x, "
3426 "mg_pll_frac_lock: 0x%x, mg_pll_ssc: 0x%x, "
3427 "mg_pll_bias: 0x%x, mg_pll_tdc_coldst_bias: 0x%x\n",
3428 hw_state->cfgcr0, hw_state->cfgcr1,
3429 hw_state->mg_refclkin_ctl,
3430 hw_state->mg_clktop2_coreclkctl1,
3431 hw_state->mg_clktop2_hsclkctl,
3432 hw_state->mg_pll_div0,
3433 hw_state->mg_pll_div1,
3434 hw_state->mg_pll_lf,
3435 hw_state->mg_pll_frac_lock,
3436 hw_state->mg_pll_ssc,
3437 hw_state->mg_pll_bias,
3438 hw_state->mg_pll_tdc_coldst_bias);
3441 static const struct intel_shared_dpll_funcs combo_pll_funcs = {
3442 .enable = combo_pll_enable,
3443 .disable = combo_pll_disable,
3444 .get_hw_state = combo_pll_get_hw_state,
3447 static const struct intel_shared_dpll_funcs tbt_pll_funcs = {
3448 .enable = tbt_pll_enable,
3449 .disable = tbt_pll_disable,
3450 .get_hw_state = tbt_pll_get_hw_state,
3453 static const struct intel_shared_dpll_funcs mg_pll_funcs = {
3454 .enable = mg_pll_enable,
3455 .disable = mg_pll_disable,
3456 .get_hw_state = mg_pll_get_hw_state,
3459 static const struct dpll_info icl_plls[] = {
3460 { "DPLL 0", &combo_pll_funcs, DPLL_ID_ICL_DPLL0, 0 },
3461 { "DPLL 1", &combo_pll_funcs, DPLL_ID_ICL_DPLL1, 0 },
3462 { "TBT PLL", &tbt_pll_funcs, DPLL_ID_ICL_TBTPLL, 0 },
3463 { "MG PLL 1", &mg_pll_funcs, DPLL_ID_ICL_MGPLL1, 0 },
3464 { "MG PLL 2", &mg_pll_funcs, DPLL_ID_ICL_MGPLL2, 0 },
3465 { "MG PLL 3", &mg_pll_funcs, DPLL_ID_ICL_MGPLL3, 0 },
3466 { "MG PLL 4", &mg_pll_funcs, DPLL_ID_ICL_MGPLL4, 0 },
3470 static const struct intel_dpll_mgr icl_pll_mgr = {
3471 .dpll_info = icl_plls,
3472 .get_dplls = icl_get_dplls,
3473 .put_dplls = icl_put_dplls,
3474 .update_active_dpll = icl_update_active_dpll,
3475 .dump_hw_state = icl_dump_hw_state,
3478 static const struct dpll_info ehl_plls[] = {
3479 { "DPLL 0", &combo_pll_funcs, DPLL_ID_ICL_DPLL0, 0 },
3480 { "DPLL 1", &combo_pll_funcs, DPLL_ID_ICL_DPLL1, 0 },
3481 { "DPLL 4", &combo_pll_funcs, DPLL_ID_EHL_DPLL4, 0 },
3485 static const struct intel_dpll_mgr ehl_pll_mgr = {
3486 .dpll_info = ehl_plls,
3487 .get_dplls = icl_get_dplls,
3488 .put_dplls = icl_put_dplls,
3489 .dump_hw_state = icl_dump_hw_state,
3492 static const struct dpll_info tgl_plls[] = {
3493 { "DPLL 0", &combo_pll_funcs, DPLL_ID_ICL_DPLL0, 0 },
3494 { "DPLL 1", &combo_pll_funcs, DPLL_ID_ICL_DPLL1, 0 },
3495 { "TBT PLL", &tbt_pll_funcs, DPLL_ID_ICL_TBTPLL, 0 },
3496 /* TODO: Add typeC plls */
3500 static const struct intel_dpll_mgr tgl_pll_mgr = {
3501 .dpll_info = tgl_plls,
3502 .get_dplls = icl_get_dplls,
3503 .put_dplls = icl_put_dplls,
3504 .dump_hw_state = icl_dump_hw_state,
3508 * intel_shared_dpll_init - Initialize shared DPLLs
3511 * Initialize shared DPLLs for @dev.
3513 void intel_shared_dpll_init(struct drm_device *dev)
3515 struct drm_i915_private *dev_priv = to_i915(dev);
3516 const struct intel_dpll_mgr *dpll_mgr = NULL;
3517 const struct dpll_info *dpll_info;
3520 if (INTEL_GEN(dev_priv) >= 12)
3521 dpll_mgr = &tgl_pll_mgr;
3522 else if (IS_ELKHARTLAKE(dev_priv))
3523 dpll_mgr = &ehl_pll_mgr;
3524 else if (INTEL_GEN(dev_priv) >= 11)
3525 dpll_mgr = &icl_pll_mgr;
3526 else if (IS_CANNONLAKE(dev_priv))
3527 dpll_mgr = &cnl_pll_mgr;
3528 else if (IS_GEN9_BC(dev_priv))
3529 dpll_mgr = &skl_pll_mgr;
3530 else if (IS_GEN9_LP(dev_priv))
3531 dpll_mgr = &bxt_pll_mgr;
3532 else if (HAS_DDI(dev_priv))
3533 dpll_mgr = &hsw_pll_mgr;
3534 else if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
3535 dpll_mgr = &pch_pll_mgr;
3538 dev_priv->num_shared_dpll = 0;
3542 dpll_info = dpll_mgr->dpll_info;
3544 for (i = 0; dpll_info[i].name; i++) {
3545 WARN_ON(i != dpll_info[i].id);
3546 dev_priv->shared_dplls[i].info = &dpll_info[i];
3549 dev_priv->dpll_mgr = dpll_mgr;
3550 dev_priv->num_shared_dpll = i;
3551 mutex_init(&dev_priv->dpll_lock);
3553 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
3557 * intel_reserve_shared_dplls - reserve DPLLs for CRTC and encoder combination
3558 * @state: atomic state
3559 * @crtc: CRTC to reserve DPLLs for
3562 * This function reserves all required DPLLs for the given CRTC and encoder
3563 * combination in the current atomic commit @state and the new @crtc atomic
3566 * The new configuration in the atomic commit @state is made effective by
3567 * calling intel_shared_dpll_swap_state().
3569 * The reserved DPLLs should be released by calling
3570 * intel_release_shared_dplls().
3573 * True if all required DPLLs were successfully reserved.
3575 bool intel_reserve_shared_dplls(struct intel_atomic_state *state,
3576 struct intel_crtc *crtc,
3577 struct intel_encoder *encoder)
3579 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3580 const struct intel_dpll_mgr *dpll_mgr = dev_priv->dpll_mgr;
3582 if (WARN_ON(!dpll_mgr))
3585 return dpll_mgr->get_dplls(state, crtc, encoder);
3589 * intel_release_shared_dplls - end use of DPLLs by CRTC in atomic state
3590 * @state: atomic state
3591 * @crtc: crtc from which the DPLLs are to be released
3593 * This function releases all DPLLs reserved by intel_reserve_shared_dplls()
3594 * from the current atomic commit @state and the old @crtc atomic state.
3596 * The new configuration in the atomic commit @state is made effective by
3597 * calling intel_shared_dpll_swap_state().
3599 void intel_release_shared_dplls(struct intel_atomic_state *state,
3600 struct intel_crtc *crtc)
3602 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3603 const struct intel_dpll_mgr *dpll_mgr = dev_priv->dpll_mgr;
3606 * FIXME: this function is called for every platform having a
3607 * compute_clock hook, even though the platform doesn't yet support
3608 * the shared DPLL framework and intel_reserve_shared_dplls() is not
3614 dpll_mgr->put_dplls(state, crtc);
3618 * intel_update_active_dpll - update the active DPLL for a CRTC/encoder
3619 * @state: atomic state
3620 * @crtc: the CRTC for which to update the active DPLL
3621 * @encoder: encoder determining the type of port DPLL
3623 * Update the active DPLL for the given @crtc/@encoder in @crtc's atomic state,
3624 * from the port DPLLs reserved previously by intel_reserve_shared_dplls(). The
3625 * DPLL selected will be based on the current mode of the encoder's port.
3627 void intel_update_active_dpll(struct intel_atomic_state *state,
3628 struct intel_crtc *crtc,
3629 struct intel_encoder *encoder)
3631 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3632 const struct intel_dpll_mgr *dpll_mgr = dev_priv->dpll_mgr;
3634 if (WARN_ON(!dpll_mgr))
3637 dpll_mgr->update_active_dpll(state, crtc, encoder);
3641 * intel_shared_dpll_dump_hw_state - write hw_state to dmesg
3642 * @dev_priv: i915 drm device
3643 * @hw_state: hw state to be written to the log
3645 * Write the relevant values in @hw_state to dmesg using DRM_DEBUG_KMS.
3647 void intel_dpll_dump_hw_state(struct drm_i915_private *dev_priv,
3648 const struct intel_dpll_hw_state *hw_state)
3650 if (dev_priv->dpll_mgr) {
3651 dev_priv->dpll_mgr->dump_hw_state(dev_priv, hw_state);
3653 /* fallback for platforms that don't use the shared dpll
3656 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
3657 "fp0: 0x%x, fp1: 0x%x\n",