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 * modest interface. During an atomic operation, a PLL can be requested for a
40 * given CRTC and encoder configuration by calling intel_get_shared_dpll() and
41 * a previously used PLL can be released with intel_release_shared_dpll().
42 * Changes to the users are first staged in the atomic state, and then made
43 * effective by calling intel_shared_dpll_swap_state() during the atomic
48 intel_atomic_duplicate_dpll_state(struct drm_i915_private *dev_priv,
49 struct intel_shared_dpll_state *shared_dpll)
53 /* Copy shared dpll state */
54 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
55 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
57 shared_dpll[i] = pll->state;
61 static struct intel_shared_dpll_state *
62 intel_atomic_get_shared_dpll_state(struct drm_atomic_state *s)
64 struct intel_atomic_state *state = to_intel_atomic_state(s);
66 WARN_ON(!drm_modeset_is_locked(&s->dev->mode_config.connection_mutex));
68 if (!state->dpll_set) {
69 state->dpll_set = true;
71 intel_atomic_duplicate_dpll_state(to_i915(s->dev),
75 return state->shared_dpll;
79 * intel_get_shared_dpll_by_id - get a DPLL given its id
80 * @dev_priv: i915 device instance
84 * A pointer to the DPLL with @id
86 struct intel_shared_dpll *
87 intel_get_shared_dpll_by_id(struct drm_i915_private *dev_priv,
88 enum intel_dpll_id id)
90 return &dev_priv->shared_dplls[id];
94 * intel_get_shared_dpll_id - get the id of a DPLL
95 * @dev_priv: i915 device instance
102 intel_get_shared_dpll_id(struct drm_i915_private *dev_priv,
103 struct intel_shared_dpll *pll)
105 if (WARN_ON(pll < dev_priv->shared_dplls||
106 pll > &dev_priv->shared_dplls[dev_priv->num_shared_dpll]))
109 return (enum intel_dpll_id) (pll - dev_priv->shared_dplls);
113 void assert_shared_dpll(struct drm_i915_private *dev_priv,
114 struct intel_shared_dpll *pll,
118 struct intel_dpll_hw_state hw_state;
120 if (WARN(!pll, "asserting DPLL %s with no DPLL\n", onoff(state)))
123 cur_state = pll->info->funcs->get_hw_state(dev_priv, pll, &hw_state);
124 I915_STATE_WARN(cur_state != state,
125 "%s assertion failure (expected %s, current %s)\n",
126 pll->info->name, onoff(state), onoff(cur_state));
130 * intel_prepare_shared_dpll - call a dpll's prepare hook
131 * @crtc_state: CRTC, and its state, which has a shared dpll
133 * This calls the PLL's prepare hook if it has one and if the PLL is not
134 * already enabled. The prepare hook is platform specific.
136 void intel_prepare_shared_dpll(const struct intel_crtc_state *crtc_state)
138 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
139 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
140 struct intel_shared_dpll *pll = crtc_state->shared_dpll;
142 if (WARN_ON(pll == NULL))
145 mutex_lock(&dev_priv->dpll_lock);
146 WARN_ON(!pll->state.crtc_mask);
147 if (!pll->active_mask) {
148 DRM_DEBUG_DRIVER("setting up %s\n", pll->info->name);
150 assert_shared_dpll_disabled(dev_priv, pll);
152 pll->info->funcs->prepare(dev_priv, pll);
154 mutex_unlock(&dev_priv->dpll_lock);
158 * intel_enable_shared_dpll - enable a CRTC's shared DPLL
159 * @crtc_state: CRTC, and its state, which has a shared DPLL
161 * Enable the shared DPLL used by @crtc.
163 void intel_enable_shared_dpll(const struct intel_crtc_state *crtc_state)
165 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
166 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
167 struct intel_shared_dpll *pll = crtc_state->shared_dpll;
168 unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
169 unsigned int old_mask;
171 if (WARN_ON(pll == NULL))
174 mutex_lock(&dev_priv->dpll_lock);
175 old_mask = pll->active_mask;
177 if (WARN_ON(!(pll->state.crtc_mask & crtc_mask)) ||
178 WARN_ON(pll->active_mask & crtc_mask))
181 pll->active_mask |= crtc_mask;
183 DRM_DEBUG_KMS("enable %s (active %x, on? %d) for crtc %d\n",
184 pll->info->name, pll->active_mask, pll->on,
189 assert_shared_dpll_enabled(dev_priv, pll);
194 DRM_DEBUG_KMS("enabling %s\n", pll->info->name);
195 pll->info->funcs->enable(dev_priv, pll);
199 mutex_unlock(&dev_priv->dpll_lock);
203 * intel_disable_shared_dpll - disable a CRTC's shared DPLL
204 * @crtc_state: CRTC, and its state, which has a shared DPLL
206 * Disable the shared DPLL used by @crtc.
208 void intel_disable_shared_dpll(const struct intel_crtc_state *crtc_state)
210 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
211 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
212 struct intel_shared_dpll *pll = crtc_state->shared_dpll;
213 unsigned int crtc_mask = drm_crtc_mask(&crtc->base);
215 /* PCH only available on ILK+ */
216 if (INTEL_GEN(dev_priv) < 5)
222 mutex_lock(&dev_priv->dpll_lock);
223 if (WARN_ON(!(pll->active_mask & crtc_mask)))
226 DRM_DEBUG_KMS("disable %s (active %x, on? %d) for crtc %d\n",
227 pll->info->name, pll->active_mask, pll->on,
230 assert_shared_dpll_enabled(dev_priv, pll);
233 pll->active_mask &= ~crtc_mask;
234 if (pll->active_mask)
237 DRM_DEBUG_KMS("disabling %s\n", pll->info->name);
238 pll->info->funcs->disable(dev_priv, pll);
242 mutex_unlock(&dev_priv->dpll_lock);
245 static struct intel_shared_dpll *
246 intel_find_shared_dpll(struct intel_crtc_state *crtc_state,
247 enum intel_dpll_id range_min,
248 enum intel_dpll_id range_max)
250 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
251 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
252 struct intel_shared_dpll *pll, *unused_pll = NULL;
253 struct intel_shared_dpll_state *shared_dpll;
254 enum intel_dpll_id i;
256 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
258 for (i = range_min; i <= range_max; i++) {
259 pll = &dev_priv->shared_dplls[i];
261 /* Only want to check enabled timings first */
262 if (shared_dpll[i].crtc_mask == 0) {
268 if (memcmp(&crtc_state->dpll_hw_state,
269 &shared_dpll[i].hw_state,
270 sizeof(crtc_state->dpll_hw_state)) == 0) {
271 DRM_DEBUG_KMS("[CRTC:%d:%s] sharing existing %s (crtc mask 0x%08x, active %x)\n",
272 crtc->base.base.id, crtc->base.name,
274 shared_dpll[i].crtc_mask,
280 /* Ok no matching timings, maybe there's a free one? */
282 DRM_DEBUG_KMS("[CRTC:%d:%s] allocated %s\n",
283 crtc->base.base.id, crtc->base.name,
284 unused_pll->info->name);
292 intel_reference_shared_dpll(struct intel_shared_dpll *pll,
293 struct intel_crtc_state *crtc_state)
295 struct intel_shared_dpll_state *shared_dpll;
296 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
297 const enum intel_dpll_id id = pll->info->id;
299 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
301 if (shared_dpll[id].crtc_mask == 0)
302 shared_dpll[id].hw_state =
303 crtc_state->dpll_hw_state;
305 crtc_state->shared_dpll = pll;
306 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->info->name,
307 pipe_name(crtc->pipe));
309 shared_dpll[id].crtc_mask |= 1 << crtc->pipe;
313 * intel_shared_dpll_swap_state - make atomic DPLL configuration effective
314 * @state: atomic state
316 * This is the dpll version of drm_atomic_helper_swap_state() since the
317 * helper does not handle driver-specific global state.
319 * For consistency with atomic helpers this function does a complete swap,
320 * i.e. it also puts the current state into @state, even though there is no
321 * need for that at this moment.
323 void intel_shared_dpll_swap_state(struct drm_atomic_state *state)
325 struct drm_i915_private *dev_priv = to_i915(state->dev);
326 struct intel_shared_dpll_state *shared_dpll;
327 struct intel_shared_dpll *pll;
328 enum intel_dpll_id i;
330 if (!to_intel_atomic_state(state)->dpll_set)
333 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
334 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
335 struct intel_shared_dpll_state tmp;
337 pll = &dev_priv->shared_dplls[i];
340 pll->state = shared_dpll[i];
341 shared_dpll[i] = tmp;
345 static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
346 struct intel_shared_dpll *pll,
347 struct intel_dpll_hw_state *hw_state)
349 const enum intel_dpll_id id = pll->info->id;
350 intel_wakeref_t wakeref;
353 wakeref = intel_display_power_get_if_enabled(dev_priv,
354 POWER_DOMAIN_DISPLAY_CORE);
358 val = I915_READ(PCH_DPLL(id));
359 hw_state->dpll = val;
360 hw_state->fp0 = I915_READ(PCH_FP0(id));
361 hw_state->fp1 = I915_READ(PCH_FP1(id));
363 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
365 return val & DPLL_VCO_ENABLE;
368 static void ibx_pch_dpll_prepare(struct drm_i915_private *dev_priv,
369 struct intel_shared_dpll *pll)
371 const enum intel_dpll_id id = pll->info->id;
373 I915_WRITE(PCH_FP0(id), pll->state.hw_state.fp0);
374 I915_WRITE(PCH_FP1(id), pll->state.hw_state.fp1);
377 static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
382 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)));
384 val = I915_READ(PCH_DREF_CONTROL);
385 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
386 DREF_SUPERSPREAD_SOURCE_MASK));
387 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
390 static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
391 struct intel_shared_dpll *pll)
393 const enum intel_dpll_id id = pll->info->id;
395 /* PCH refclock must be enabled first */
396 ibx_assert_pch_refclk_enabled(dev_priv);
398 I915_WRITE(PCH_DPLL(id), pll->state.hw_state.dpll);
400 /* Wait for the clocks to stabilize. */
401 POSTING_READ(PCH_DPLL(id));
404 /* The pixel multiplier can only be updated once the
405 * DPLL is enabled and the clocks are stable.
409 I915_WRITE(PCH_DPLL(id), pll->state.hw_state.dpll);
410 POSTING_READ(PCH_DPLL(id));
414 static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
415 struct intel_shared_dpll *pll)
417 const enum intel_dpll_id id = pll->info->id;
419 I915_WRITE(PCH_DPLL(id), 0);
420 POSTING_READ(PCH_DPLL(id));
424 static struct intel_shared_dpll *
425 ibx_get_dpll(struct intel_crtc_state *crtc_state,
426 struct intel_encoder *encoder)
428 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
429 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
430 struct intel_shared_dpll *pll;
431 enum intel_dpll_id i;
433 if (HAS_PCH_IBX(dev_priv)) {
434 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
435 i = (enum intel_dpll_id) crtc->pipe;
436 pll = &dev_priv->shared_dplls[i];
438 DRM_DEBUG_KMS("[CRTC:%d:%s] using pre-allocated %s\n",
439 crtc->base.base.id, crtc->base.name,
442 pll = intel_find_shared_dpll(crtc_state,
450 /* reference the pll */
451 intel_reference_shared_dpll(pll, crtc_state);
456 static void ibx_dump_hw_state(struct drm_i915_private *dev_priv,
457 const struct intel_dpll_hw_state *hw_state)
459 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
460 "fp0: 0x%x, fp1: 0x%x\n",
467 static const struct intel_shared_dpll_funcs ibx_pch_dpll_funcs = {
468 .prepare = ibx_pch_dpll_prepare,
469 .enable = ibx_pch_dpll_enable,
470 .disable = ibx_pch_dpll_disable,
471 .get_hw_state = ibx_pch_dpll_get_hw_state,
474 static void hsw_ddi_wrpll_enable(struct drm_i915_private *dev_priv,
475 struct intel_shared_dpll *pll)
477 const enum intel_dpll_id id = pll->info->id;
479 I915_WRITE(WRPLL_CTL(id), pll->state.hw_state.wrpll);
480 POSTING_READ(WRPLL_CTL(id));
484 static void hsw_ddi_spll_enable(struct drm_i915_private *dev_priv,
485 struct intel_shared_dpll *pll)
487 I915_WRITE(SPLL_CTL, pll->state.hw_state.spll);
488 POSTING_READ(SPLL_CTL);
492 static void hsw_ddi_wrpll_disable(struct drm_i915_private *dev_priv,
493 struct intel_shared_dpll *pll)
495 const enum intel_dpll_id id = pll->info->id;
498 val = I915_READ(WRPLL_CTL(id));
499 I915_WRITE(WRPLL_CTL(id), val & ~WRPLL_PLL_ENABLE);
500 POSTING_READ(WRPLL_CTL(id));
503 static void hsw_ddi_spll_disable(struct drm_i915_private *dev_priv,
504 struct intel_shared_dpll *pll)
508 val = I915_READ(SPLL_CTL);
509 I915_WRITE(SPLL_CTL, val & ~SPLL_PLL_ENABLE);
510 POSTING_READ(SPLL_CTL);
513 static bool hsw_ddi_wrpll_get_hw_state(struct drm_i915_private *dev_priv,
514 struct intel_shared_dpll *pll,
515 struct intel_dpll_hw_state *hw_state)
517 const enum intel_dpll_id id = pll->info->id;
518 intel_wakeref_t wakeref;
521 wakeref = intel_display_power_get_if_enabled(dev_priv,
522 POWER_DOMAIN_DISPLAY_CORE);
526 val = I915_READ(WRPLL_CTL(id));
527 hw_state->wrpll = val;
529 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
531 return val & WRPLL_PLL_ENABLE;
534 static bool hsw_ddi_spll_get_hw_state(struct drm_i915_private *dev_priv,
535 struct intel_shared_dpll *pll,
536 struct intel_dpll_hw_state *hw_state)
538 intel_wakeref_t wakeref;
541 wakeref = intel_display_power_get_if_enabled(dev_priv,
542 POWER_DOMAIN_DISPLAY_CORE);
546 val = I915_READ(SPLL_CTL);
547 hw_state->spll = val;
549 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
551 return val & SPLL_PLL_ENABLE;
555 #define LC_FREQ_2K U64_C(LC_FREQ * 2000)
561 /* Constraints for PLL good behavior */
567 struct hsw_wrpll_rnp {
571 static unsigned hsw_wrpll_get_budget_for_freq(int clock)
645 static void hsw_wrpll_update_rnp(u64 freq2k, unsigned int budget,
646 unsigned int r2, unsigned int n2,
648 struct hsw_wrpll_rnp *best)
650 u64 a, b, c, d, diff, diff_best;
652 /* No best (r,n,p) yet */
661 * Output clock is (LC_FREQ_2K / 2000) * N / (P * R), which compares to
665 * abs(freq2k - (LC_FREQ_2K * n2/(p * r2))) /
668 * and we would like delta <= budget.
670 * If the discrepancy is above the PPM-based budget, always prefer to
671 * improve upon the previous solution. However, if you're within the
672 * budget, try to maximize Ref * VCO, that is N / (P * R^2).
674 a = freq2k * budget * p * r2;
675 b = freq2k * budget * best->p * best->r2;
676 diff = abs_diff(freq2k * p * r2, LC_FREQ_2K * n2);
677 diff_best = abs_diff(freq2k * best->p * best->r2,
678 LC_FREQ_2K * best->n2);
680 d = 1000000 * diff_best;
682 if (a < c && b < d) {
683 /* If both are above the budget, pick the closer */
684 if (best->p * best->r2 * diff < p * r2 * diff_best) {
689 } else if (a >= c && b < d) {
690 /* If A is below the threshold but B is above it? Update. */
694 } else if (a >= c && b >= d) {
695 /* Both are below the limit, so pick the higher n2/(r2*r2) */
696 if (n2 * best->r2 * best->r2 > best->n2 * r2 * r2) {
702 /* Otherwise a < c && b >= d, do nothing */
706 hsw_ddi_calculate_wrpll(int clock /* in Hz */,
707 unsigned *r2_out, unsigned *n2_out, unsigned *p_out)
711 struct hsw_wrpll_rnp best = { 0, 0, 0 };
714 freq2k = clock / 100;
716 budget = hsw_wrpll_get_budget_for_freq(clock);
718 /* Special case handling for 540 pixel clock: bypass WR PLL entirely
719 * and directly pass the LC PLL to it. */
720 if (freq2k == 5400000) {
728 * Ref = LC_FREQ / R, where Ref is the actual reference input seen by
731 * We want R so that REF_MIN <= Ref <= REF_MAX.
732 * Injecting R2 = 2 * R gives:
733 * REF_MAX * r2 > LC_FREQ * 2 and
734 * REF_MIN * r2 < LC_FREQ * 2
736 * Which means the desired boundaries for r2 are:
737 * LC_FREQ * 2 / REF_MAX < r2 < LC_FREQ * 2 / REF_MIN
740 for (r2 = LC_FREQ * 2 / REF_MAX + 1;
741 r2 <= LC_FREQ * 2 / REF_MIN;
745 * VCO = N * Ref, that is: VCO = N * LC_FREQ / R
747 * Once again we want VCO_MIN <= VCO <= VCO_MAX.
748 * Injecting R2 = 2 * R and N2 = 2 * N, we get:
749 * VCO_MAX * r2 > n2 * LC_FREQ and
750 * VCO_MIN * r2 < n2 * LC_FREQ)
752 * Which means the desired boundaries for n2 are:
753 * VCO_MIN * r2 / LC_FREQ < n2 < VCO_MAX * r2 / LC_FREQ
755 for (n2 = VCO_MIN * r2 / LC_FREQ + 1;
756 n2 <= VCO_MAX * r2 / LC_FREQ;
759 for (p = P_MIN; p <= P_MAX; p += P_INC)
760 hsw_wrpll_update_rnp(freq2k, budget,
770 static struct intel_shared_dpll *hsw_ddi_hdmi_get_dpll(struct intel_crtc_state *crtc_state)
772 struct intel_shared_dpll *pll;
774 unsigned int p, n2, r2;
776 hsw_ddi_calculate_wrpll(crtc_state->port_clock * 1000, &r2, &n2, &p);
778 val = WRPLL_PLL_ENABLE | WRPLL_PLL_LCPLL |
779 WRPLL_DIVIDER_REFERENCE(r2) | WRPLL_DIVIDER_FEEDBACK(n2) |
780 WRPLL_DIVIDER_POST(p);
782 crtc_state->dpll_hw_state.wrpll = val;
784 pll = intel_find_shared_dpll(crtc_state,
785 DPLL_ID_WRPLL1, DPLL_ID_WRPLL2);
793 static struct intel_shared_dpll *
794 hsw_ddi_dp_get_dpll(struct intel_crtc_state *crtc_state)
796 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
797 struct intel_shared_dpll *pll;
798 enum intel_dpll_id pll_id;
799 int clock = crtc_state->port_clock;
803 pll_id = DPLL_ID_LCPLL_810;
806 pll_id = DPLL_ID_LCPLL_1350;
809 pll_id = DPLL_ID_LCPLL_2700;
812 DRM_DEBUG_KMS("Invalid clock for DP: %d\n", clock);
816 pll = intel_get_shared_dpll_by_id(dev_priv, pll_id);
824 static struct intel_shared_dpll *
825 hsw_get_dpll(struct intel_crtc_state *crtc_state,
826 struct intel_encoder *encoder)
828 struct intel_shared_dpll *pll;
830 memset(&crtc_state->dpll_hw_state, 0,
831 sizeof(crtc_state->dpll_hw_state));
833 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
834 pll = hsw_ddi_hdmi_get_dpll(crtc_state);
835 } else if (intel_crtc_has_dp_encoder(crtc_state)) {
836 pll = hsw_ddi_dp_get_dpll(crtc_state);
837 } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
838 if (WARN_ON(crtc_state->port_clock / 2 != 135000))
841 crtc_state->dpll_hw_state.spll =
842 SPLL_PLL_ENABLE | SPLL_PLL_FREQ_1350MHz | SPLL_PLL_SSC;
844 pll = intel_find_shared_dpll(crtc_state,
845 DPLL_ID_SPLL, DPLL_ID_SPLL);
853 intel_reference_shared_dpll(pll, crtc_state);
858 static void hsw_dump_hw_state(struct drm_i915_private *dev_priv,
859 const struct intel_dpll_hw_state *hw_state)
861 DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
862 hw_state->wrpll, hw_state->spll);
865 static const struct intel_shared_dpll_funcs hsw_ddi_wrpll_funcs = {
866 .enable = hsw_ddi_wrpll_enable,
867 .disable = hsw_ddi_wrpll_disable,
868 .get_hw_state = hsw_ddi_wrpll_get_hw_state,
871 static const struct intel_shared_dpll_funcs hsw_ddi_spll_funcs = {
872 .enable = hsw_ddi_spll_enable,
873 .disable = hsw_ddi_spll_disable,
874 .get_hw_state = hsw_ddi_spll_get_hw_state,
877 static void hsw_ddi_lcpll_enable(struct drm_i915_private *dev_priv,
878 struct intel_shared_dpll *pll)
882 static void hsw_ddi_lcpll_disable(struct drm_i915_private *dev_priv,
883 struct intel_shared_dpll *pll)
887 static bool hsw_ddi_lcpll_get_hw_state(struct drm_i915_private *dev_priv,
888 struct intel_shared_dpll *pll,
889 struct intel_dpll_hw_state *hw_state)
894 static const struct intel_shared_dpll_funcs hsw_ddi_lcpll_funcs = {
895 .enable = hsw_ddi_lcpll_enable,
896 .disable = hsw_ddi_lcpll_disable,
897 .get_hw_state = hsw_ddi_lcpll_get_hw_state,
900 struct skl_dpll_regs {
901 i915_reg_t ctl, cfgcr1, cfgcr2;
904 /* this array is indexed by the *shared* pll id */
905 static const struct skl_dpll_regs skl_dpll_regs[4] = {
909 /* DPLL 0 doesn't support HDMI mode */
914 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL1),
915 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL1),
920 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL2),
921 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL2),
926 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL3),
927 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL3),
931 static void skl_ddi_pll_write_ctrl1(struct drm_i915_private *dev_priv,
932 struct intel_shared_dpll *pll)
934 const enum intel_dpll_id id = pll->info->id;
937 val = I915_READ(DPLL_CTRL1);
939 val &= ~(DPLL_CTRL1_HDMI_MODE(id) |
941 DPLL_CTRL1_LINK_RATE_MASK(id));
942 val |= pll->state.hw_state.ctrl1 << (id * 6);
944 I915_WRITE(DPLL_CTRL1, val);
945 POSTING_READ(DPLL_CTRL1);
948 static void skl_ddi_pll_enable(struct drm_i915_private *dev_priv,
949 struct intel_shared_dpll *pll)
951 const struct skl_dpll_regs *regs = skl_dpll_regs;
952 const enum intel_dpll_id id = pll->info->id;
954 skl_ddi_pll_write_ctrl1(dev_priv, pll);
956 I915_WRITE(regs[id].cfgcr1, pll->state.hw_state.cfgcr1);
957 I915_WRITE(regs[id].cfgcr2, pll->state.hw_state.cfgcr2);
958 POSTING_READ(regs[id].cfgcr1);
959 POSTING_READ(regs[id].cfgcr2);
961 /* the enable bit is always bit 31 */
962 I915_WRITE(regs[id].ctl,
963 I915_READ(regs[id].ctl) | LCPLL_PLL_ENABLE);
965 if (intel_wait_for_register(&dev_priv->uncore,
970 DRM_ERROR("DPLL %d not locked\n", id);
973 static void skl_ddi_dpll0_enable(struct drm_i915_private *dev_priv,
974 struct intel_shared_dpll *pll)
976 skl_ddi_pll_write_ctrl1(dev_priv, pll);
979 static void skl_ddi_pll_disable(struct drm_i915_private *dev_priv,
980 struct intel_shared_dpll *pll)
982 const struct skl_dpll_regs *regs = skl_dpll_regs;
983 const enum intel_dpll_id id = pll->info->id;
985 /* the enable bit is always bit 31 */
986 I915_WRITE(regs[id].ctl,
987 I915_READ(regs[id].ctl) & ~LCPLL_PLL_ENABLE);
988 POSTING_READ(regs[id].ctl);
991 static void skl_ddi_dpll0_disable(struct drm_i915_private *dev_priv,
992 struct intel_shared_dpll *pll)
996 static bool skl_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
997 struct intel_shared_dpll *pll,
998 struct intel_dpll_hw_state *hw_state)
1001 const struct skl_dpll_regs *regs = skl_dpll_regs;
1002 const enum intel_dpll_id id = pll->info->id;
1003 intel_wakeref_t wakeref;
1006 wakeref = intel_display_power_get_if_enabled(dev_priv,
1007 POWER_DOMAIN_DISPLAY_CORE);
1013 val = I915_READ(regs[id].ctl);
1014 if (!(val & LCPLL_PLL_ENABLE))
1017 val = I915_READ(DPLL_CTRL1);
1018 hw_state->ctrl1 = (val >> (id * 6)) & 0x3f;
1020 /* avoid reading back stale values if HDMI mode is not enabled */
1021 if (val & DPLL_CTRL1_HDMI_MODE(id)) {
1022 hw_state->cfgcr1 = I915_READ(regs[id].cfgcr1);
1023 hw_state->cfgcr2 = I915_READ(regs[id].cfgcr2);
1028 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
1033 static bool skl_ddi_dpll0_get_hw_state(struct drm_i915_private *dev_priv,
1034 struct intel_shared_dpll *pll,
1035 struct intel_dpll_hw_state *hw_state)
1037 const struct skl_dpll_regs *regs = skl_dpll_regs;
1038 const enum intel_dpll_id id = pll->info->id;
1039 intel_wakeref_t wakeref;
1043 wakeref = intel_display_power_get_if_enabled(dev_priv,
1044 POWER_DOMAIN_DISPLAY_CORE);
1050 /* DPLL0 is always enabled since it drives CDCLK */
1051 val = I915_READ(regs[id].ctl);
1052 if (WARN_ON(!(val & LCPLL_PLL_ENABLE)))
1055 val = I915_READ(DPLL_CTRL1);
1056 hw_state->ctrl1 = (val >> (id * 6)) & 0x3f;
1061 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
1066 struct skl_wrpll_context {
1067 u64 min_deviation; /* current minimal deviation */
1068 u64 central_freq; /* chosen central freq */
1069 u64 dco_freq; /* chosen dco freq */
1070 unsigned int p; /* chosen divider */
1073 static void skl_wrpll_context_init(struct skl_wrpll_context *ctx)
1075 memset(ctx, 0, sizeof(*ctx));
1077 ctx->min_deviation = U64_MAX;
1080 /* DCO freq must be within +1%/-6% of the DCO central freq */
1081 #define SKL_DCO_MAX_PDEVIATION 100
1082 #define SKL_DCO_MAX_NDEVIATION 600
1084 static void skl_wrpll_try_divider(struct skl_wrpll_context *ctx,
1087 unsigned int divider)
1091 deviation = div64_u64(10000 * abs_diff(dco_freq, central_freq),
1094 /* positive deviation */
1095 if (dco_freq >= central_freq) {
1096 if (deviation < SKL_DCO_MAX_PDEVIATION &&
1097 deviation < ctx->min_deviation) {
1098 ctx->min_deviation = deviation;
1099 ctx->central_freq = central_freq;
1100 ctx->dco_freq = dco_freq;
1103 /* negative deviation */
1104 } else if (deviation < SKL_DCO_MAX_NDEVIATION &&
1105 deviation < ctx->min_deviation) {
1106 ctx->min_deviation = deviation;
1107 ctx->central_freq = central_freq;
1108 ctx->dco_freq = dco_freq;
1113 static void skl_wrpll_get_multipliers(unsigned int p,
1114 unsigned int *p0 /* out */,
1115 unsigned int *p1 /* out */,
1116 unsigned int *p2 /* out */)
1120 unsigned int half = p / 2;
1122 if (half == 1 || half == 2 || half == 3 || half == 5) {
1126 } else if (half % 2 == 0) {
1130 } else if (half % 3 == 0) {
1134 } else if (half % 7 == 0) {
1139 } else if (p == 3 || p == 9) { /* 3, 5, 7, 9, 15, 21, 35 */
1143 } else if (p == 5 || p == 7) {
1147 } else if (p == 15) {
1151 } else if (p == 21) {
1155 } else if (p == 35) {
1162 struct skl_wrpll_params {
1172 static void skl_wrpll_params_populate(struct skl_wrpll_params *params,
1175 u32 p0, u32 p1, u32 p2)
1179 switch (central_freq) {
1181 params->central_freq = 0;
1184 params->central_freq = 1;
1187 params->central_freq = 3;
1204 WARN(1, "Incorrect PDiv\n");
1221 WARN(1, "Incorrect KDiv\n");
1224 params->qdiv_ratio = p1;
1225 params->qdiv_mode = (params->qdiv_ratio == 1) ? 0 : 1;
1227 dco_freq = p0 * p1 * p2 * afe_clock;
1230 * Intermediate values are in Hz.
1231 * Divide by MHz to match bsepc
1233 params->dco_integer = div_u64(dco_freq, 24 * MHz(1));
1234 params->dco_fraction =
1235 div_u64((div_u64(dco_freq, 24) -
1236 params->dco_integer * MHz(1)) * 0x8000, MHz(1));
1240 skl_ddi_calculate_wrpll(int clock /* in Hz */,
1241 struct skl_wrpll_params *wrpll_params)
1243 u64 afe_clock = clock * 5; /* AFE Clock is 5x Pixel clock */
1244 u64 dco_central_freq[3] = { 8400000000ULL,
1247 static const int even_dividers[] = { 4, 6, 8, 10, 12, 14, 16, 18, 20,
1248 24, 28, 30, 32, 36, 40, 42, 44,
1249 48, 52, 54, 56, 60, 64, 66, 68,
1250 70, 72, 76, 78, 80, 84, 88, 90,
1252 static const int odd_dividers[] = { 3, 5, 7, 9, 15, 21, 35 };
1253 static const struct {
1257 { even_dividers, ARRAY_SIZE(even_dividers) },
1258 { odd_dividers, ARRAY_SIZE(odd_dividers) },
1260 struct skl_wrpll_context ctx;
1261 unsigned int dco, d, i;
1262 unsigned int p0, p1, p2;
1264 skl_wrpll_context_init(&ctx);
1266 for (d = 0; d < ARRAY_SIZE(dividers); d++) {
1267 for (dco = 0; dco < ARRAY_SIZE(dco_central_freq); dco++) {
1268 for (i = 0; i < dividers[d].n_dividers; i++) {
1269 unsigned int p = dividers[d].list[i];
1270 u64 dco_freq = p * afe_clock;
1272 skl_wrpll_try_divider(&ctx,
1273 dco_central_freq[dco],
1277 * Skip the remaining dividers if we're sure to
1278 * have found the definitive divider, we can't
1279 * improve a 0 deviation.
1281 if (ctx.min_deviation == 0)
1282 goto skip_remaining_dividers;
1286 skip_remaining_dividers:
1288 * If a solution is found with an even divider, prefer
1291 if (d == 0 && ctx.p)
1296 DRM_DEBUG_DRIVER("No valid divider found for %dHz\n", clock);
1301 * gcc incorrectly analyses that these can be used without being
1302 * initialized. To be fair, it's hard to guess.
1305 skl_wrpll_get_multipliers(ctx.p, &p0, &p1, &p2);
1306 skl_wrpll_params_populate(wrpll_params, afe_clock, ctx.central_freq,
1312 static bool skl_ddi_hdmi_pll_dividers(struct intel_crtc_state *crtc_state)
1314 u32 ctrl1, cfgcr1, cfgcr2;
1315 struct skl_wrpll_params wrpll_params = { 0, };
1318 * See comment in intel_dpll_hw_state to understand why we always use 0
1319 * as the DPLL id in this function.
1321 ctrl1 = DPLL_CTRL1_OVERRIDE(0);
1323 ctrl1 |= DPLL_CTRL1_HDMI_MODE(0);
1325 if (!skl_ddi_calculate_wrpll(crtc_state->port_clock * 1000,
1329 cfgcr1 = DPLL_CFGCR1_FREQ_ENABLE |
1330 DPLL_CFGCR1_DCO_FRACTION(wrpll_params.dco_fraction) |
1331 wrpll_params.dco_integer;
1333 cfgcr2 = DPLL_CFGCR2_QDIV_RATIO(wrpll_params.qdiv_ratio) |
1334 DPLL_CFGCR2_QDIV_MODE(wrpll_params.qdiv_mode) |
1335 DPLL_CFGCR2_KDIV(wrpll_params.kdiv) |
1336 DPLL_CFGCR2_PDIV(wrpll_params.pdiv) |
1337 wrpll_params.central_freq;
1339 memset(&crtc_state->dpll_hw_state, 0,
1340 sizeof(crtc_state->dpll_hw_state));
1342 crtc_state->dpll_hw_state.ctrl1 = ctrl1;
1343 crtc_state->dpll_hw_state.cfgcr1 = cfgcr1;
1344 crtc_state->dpll_hw_state.cfgcr2 = cfgcr2;
1349 skl_ddi_dp_set_dpll_hw_state(struct intel_crtc_state *crtc_state)
1354 * See comment in intel_dpll_hw_state to understand why we always use 0
1355 * as the DPLL id in this function.
1357 ctrl1 = DPLL_CTRL1_OVERRIDE(0);
1358 switch (crtc_state->port_clock / 2) {
1360 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, 0);
1363 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, 0);
1366 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, 0);
1370 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, 0);
1373 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, 0);
1376 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, 0);
1380 memset(&crtc_state->dpll_hw_state, 0,
1381 sizeof(crtc_state->dpll_hw_state));
1383 crtc_state->dpll_hw_state.ctrl1 = ctrl1;
1388 static struct intel_shared_dpll *
1389 skl_get_dpll(struct intel_crtc_state *crtc_state,
1390 struct intel_encoder *encoder)
1392 struct intel_shared_dpll *pll;
1395 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
1396 bret = skl_ddi_hdmi_pll_dividers(crtc_state);
1398 DRM_DEBUG_KMS("Could not get HDMI pll dividers.\n");
1401 } else if (intel_crtc_has_dp_encoder(crtc_state)) {
1402 bret = skl_ddi_dp_set_dpll_hw_state(crtc_state);
1404 DRM_DEBUG_KMS("Could not set DP dpll HW state.\n");
1411 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
1412 pll = intel_find_shared_dpll(crtc_state,
1416 pll = intel_find_shared_dpll(crtc_state,
1422 intel_reference_shared_dpll(pll, crtc_state);
1427 static void skl_dump_hw_state(struct drm_i915_private *dev_priv,
1428 const struct intel_dpll_hw_state *hw_state)
1430 DRM_DEBUG_KMS("dpll_hw_state: "
1431 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
1437 static const struct intel_shared_dpll_funcs skl_ddi_pll_funcs = {
1438 .enable = skl_ddi_pll_enable,
1439 .disable = skl_ddi_pll_disable,
1440 .get_hw_state = skl_ddi_pll_get_hw_state,
1443 static const struct intel_shared_dpll_funcs skl_ddi_dpll0_funcs = {
1444 .enable = skl_ddi_dpll0_enable,
1445 .disable = skl_ddi_dpll0_disable,
1446 .get_hw_state = skl_ddi_dpll0_get_hw_state,
1449 static void bxt_ddi_pll_enable(struct drm_i915_private *dev_priv,
1450 struct intel_shared_dpll *pll)
1453 enum port port = (enum port)pll->info->id; /* 1:1 port->PLL mapping */
1455 enum dpio_channel ch;
1457 bxt_port_to_phy_channel(dev_priv, port, &phy, &ch);
1459 /* Non-SSC reference */
1460 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1461 temp |= PORT_PLL_REF_SEL;
1462 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1464 if (IS_GEMINILAKE(dev_priv)) {
1465 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1466 temp |= PORT_PLL_POWER_ENABLE;
1467 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1469 if (wait_for_us((I915_READ(BXT_PORT_PLL_ENABLE(port)) &
1470 PORT_PLL_POWER_STATE), 200))
1471 DRM_ERROR("Power state not set for PLL:%d\n", port);
1474 /* Disable 10 bit clock */
1475 temp = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
1476 temp &= ~PORT_PLL_10BIT_CLK_ENABLE;
1477 I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
1480 temp = I915_READ(BXT_PORT_PLL_EBB_0(phy, ch));
1481 temp &= ~(PORT_PLL_P1_MASK | PORT_PLL_P2_MASK);
1482 temp |= pll->state.hw_state.ebb0;
1483 I915_WRITE(BXT_PORT_PLL_EBB_0(phy, ch), temp);
1485 /* Write M2 integer */
1486 temp = I915_READ(BXT_PORT_PLL(phy, ch, 0));
1487 temp &= ~PORT_PLL_M2_MASK;
1488 temp |= pll->state.hw_state.pll0;
1489 I915_WRITE(BXT_PORT_PLL(phy, ch, 0), temp);
1492 temp = I915_READ(BXT_PORT_PLL(phy, ch, 1));
1493 temp &= ~PORT_PLL_N_MASK;
1494 temp |= pll->state.hw_state.pll1;
1495 I915_WRITE(BXT_PORT_PLL(phy, ch, 1), temp);
1497 /* Write M2 fraction */
1498 temp = I915_READ(BXT_PORT_PLL(phy, ch, 2));
1499 temp &= ~PORT_PLL_M2_FRAC_MASK;
1500 temp |= pll->state.hw_state.pll2;
1501 I915_WRITE(BXT_PORT_PLL(phy, ch, 2), temp);
1503 /* Write M2 fraction enable */
1504 temp = I915_READ(BXT_PORT_PLL(phy, ch, 3));
1505 temp &= ~PORT_PLL_M2_FRAC_ENABLE;
1506 temp |= pll->state.hw_state.pll3;
1507 I915_WRITE(BXT_PORT_PLL(phy, ch, 3), temp);
1510 temp = I915_READ(BXT_PORT_PLL(phy, ch, 6));
1511 temp &= ~PORT_PLL_PROP_COEFF_MASK;
1512 temp &= ~PORT_PLL_INT_COEFF_MASK;
1513 temp &= ~PORT_PLL_GAIN_CTL_MASK;
1514 temp |= pll->state.hw_state.pll6;
1515 I915_WRITE(BXT_PORT_PLL(phy, ch, 6), temp);
1517 /* Write calibration val */
1518 temp = I915_READ(BXT_PORT_PLL(phy, ch, 8));
1519 temp &= ~PORT_PLL_TARGET_CNT_MASK;
1520 temp |= pll->state.hw_state.pll8;
1521 I915_WRITE(BXT_PORT_PLL(phy, ch, 8), temp);
1523 temp = I915_READ(BXT_PORT_PLL(phy, ch, 9));
1524 temp &= ~PORT_PLL_LOCK_THRESHOLD_MASK;
1525 temp |= pll->state.hw_state.pll9;
1526 I915_WRITE(BXT_PORT_PLL(phy, ch, 9), temp);
1528 temp = I915_READ(BXT_PORT_PLL(phy, ch, 10));
1529 temp &= ~PORT_PLL_DCO_AMP_OVR_EN_H;
1530 temp &= ~PORT_PLL_DCO_AMP_MASK;
1531 temp |= pll->state.hw_state.pll10;
1532 I915_WRITE(BXT_PORT_PLL(phy, ch, 10), temp);
1534 /* Recalibrate with new settings */
1535 temp = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
1536 temp |= PORT_PLL_RECALIBRATE;
1537 I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
1538 temp &= ~PORT_PLL_10BIT_CLK_ENABLE;
1539 temp |= pll->state.hw_state.ebb4;
1540 I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
1543 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1544 temp |= PORT_PLL_ENABLE;
1545 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1546 POSTING_READ(BXT_PORT_PLL_ENABLE(port));
1548 if (wait_for_us((I915_READ(BXT_PORT_PLL_ENABLE(port)) & PORT_PLL_LOCK),
1550 DRM_ERROR("PLL %d not locked\n", port);
1552 if (IS_GEMINILAKE(dev_priv)) {
1553 temp = I915_READ(BXT_PORT_TX_DW5_LN0(phy, ch));
1554 temp |= DCC_DELAY_RANGE_2;
1555 I915_WRITE(BXT_PORT_TX_DW5_GRP(phy, ch), temp);
1559 * While we write to the group register to program all lanes at once we
1560 * can read only lane registers and we pick lanes 0/1 for that.
1562 temp = I915_READ(BXT_PORT_PCS_DW12_LN01(phy, ch));
1563 temp &= ~LANE_STAGGER_MASK;
1564 temp &= ~LANESTAGGER_STRAP_OVRD;
1565 temp |= pll->state.hw_state.pcsdw12;
1566 I915_WRITE(BXT_PORT_PCS_DW12_GRP(phy, ch), temp);
1569 static void bxt_ddi_pll_disable(struct drm_i915_private *dev_priv,
1570 struct intel_shared_dpll *pll)
1572 enum port port = (enum port)pll->info->id; /* 1:1 port->PLL mapping */
1575 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1576 temp &= ~PORT_PLL_ENABLE;
1577 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1578 POSTING_READ(BXT_PORT_PLL_ENABLE(port));
1580 if (IS_GEMINILAKE(dev_priv)) {
1581 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1582 temp &= ~PORT_PLL_POWER_ENABLE;
1583 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1585 if (wait_for_us(!(I915_READ(BXT_PORT_PLL_ENABLE(port)) &
1586 PORT_PLL_POWER_STATE), 200))
1587 DRM_ERROR("Power state not reset for PLL:%d\n", port);
1591 static bool bxt_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
1592 struct intel_shared_dpll *pll,
1593 struct intel_dpll_hw_state *hw_state)
1595 enum port port = (enum port)pll->info->id; /* 1:1 port->PLL mapping */
1596 intel_wakeref_t wakeref;
1598 enum dpio_channel ch;
1602 bxt_port_to_phy_channel(dev_priv, port, &phy, &ch);
1604 wakeref = intel_display_power_get_if_enabled(dev_priv,
1605 POWER_DOMAIN_DISPLAY_CORE);
1611 val = I915_READ(BXT_PORT_PLL_ENABLE(port));
1612 if (!(val & PORT_PLL_ENABLE))
1615 hw_state->ebb0 = I915_READ(BXT_PORT_PLL_EBB_0(phy, ch));
1616 hw_state->ebb0 &= PORT_PLL_P1_MASK | PORT_PLL_P2_MASK;
1618 hw_state->ebb4 = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
1619 hw_state->ebb4 &= PORT_PLL_10BIT_CLK_ENABLE;
1621 hw_state->pll0 = I915_READ(BXT_PORT_PLL(phy, ch, 0));
1622 hw_state->pll0 &= PORT_PLL_M2_MASK;
1624 hw_state->pll1 = I915_READ(BXT_PORT_PLL(phy, ch, 1));
1625 hw_state->pll1 &= PORT_PLL_N_MASK;
1627 hw_state->pll2 = I915_READ(BXT_PORT_PLL(phy, ch, 2));
1628 hw_state->pll2 &= PORT_PLL_M2_FRAC_MASK;
1630 hw_state->pll3 = I915_READ(BXT_PORT_PLL(phy, ch, 3));
1631 hw_state->pll3 &= PORT_PLL_M2_FRAC_ENABLE;
1633 hw_state->pll6 = I915_READ(BXT_PORT_PLL(phy, ch, 6));
1634 hw_state->pll6 &= PORT_PLL_PROP_COEFF_MASK |
1635 PORT_PLL_INT_COEFF_MASK |
1636 PORT_PLL_GAIN_CTL_MASK;
1638 hw_state->pll8 = I915_READ(BXT_PORT_PLL(phy, ch, 8));
1639 hw_state->pll8 &= PORT_PLL_TARGET_CNT_MASK;
1641 hw_state->pll9 = I915_READ(BXT_PORT_PLL(phy, ch, 9));
1642 hw_state->pll9 &= PORT_PLL_LOCK_THRESHOLD_MASK;
1644 hw_state->pll10 = I915_READ(BXT_PORT_PLL(phy, ch, 10));
1645 hw_state->pll10 &= PORT_PLL_DCO_AMP_OVR_EN_H |
1646 PORT_PLL_DCO_AMP_MASK;
1649 * While we write to the group register to program all lanes at once we
1650 * can read only lane registers. We configure all lanes the same way, so
1651 * here just read out lanes 0/1 and output a note if lanes 2/3 differ.
1653 hw_state->pcsdw12 = I915_READ(BXT_PORT_PCS_DW12_LN01(phy, ch));
1654 if (I915_READ(BXT_PORT_PCS_DW12_LN23(phy, ch)) != hw_state->pcsdw12)
1655 DRM_DEBUG_DRIVER("lane stagger config different for lane 01 (%08x) and 23 (%08x)\n",
1657 I915_READ(BXT_PORT_PCS_DW12_LN23(phy, ch)));
1658 hw_state->pcsdw12 &= LANE_STAGGER_MASK | LANESTAGGER_STRAP_OVRD;
1663 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
1668 /* bxt clock parameters */
1669 struct bxt_clk_div {
1681 /* pre-calculated values for DP linkrates */
1682 static const struct bxt_clk_div bxt_dp_clk_val[] = {
1683 {162000, 4, 2, 32, 1677722, 1, 1},
1684 {270000, 4, 1, 27, 0, 0, 1},
1685 {540000, 2, 1, 27, 0, 0, 1},
1686 {216000, 3, 2, 32, 1677722, 1, 1},
1687 {243000, 4, 1, 24, 1258291, 1, 1},
1688 {324000, 4, 1, 32, 1677722, 1, 1},
1689 {432000, 3, 1, 32, 1677722, 1, 1}
1693 bxt_ddi_hdmi_pll_dividers(struct intel_crtc_state *crtc_state,
1694 struct bxt_clk_div *clk_div)
1696 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1697 struct dpll best_clock;
1699 /* Calculate HDMI div */
1701 * FIXME: tie the following calculation into
1702 * i9xx_crtc_compute_clock
1704 if (!bxt_find_best_dpll(crtc_state, &best_clock)) {
1705 DRM_DEBUG_DRIVER("no PLL dividers found for clock %d pipe %c\n",
1706 crtc_state->port_clock,
1707 pipe_name(crtc->pipe));
1711 clk_div->p1 = best_clock.p1;
1712 clk_div->p2 = best_clock.p2;
1713 WARN_ON(best_clock.m1 != 2);
1714 clk_div->n = best_clock.n;
1715 clk_div->m2_int = best_clock.m2 >> 22;
1716 clk_div->m2_frac = best_clock.m2 & ((1 << 22) - 1);
1717 clk_div->m2_frac_en = clk_div->m2_frac != 0;
1719 clk_div->vco = best_clock.vco;
1724 static void bxt_ddi_dp_pll_dividers(struct intel_crtc_state *crtc_state,
1725 struct bxt_clk_div *clk_div)
1727 int clock = crtc_state->port_clock;
1730 *clk_div = bxt_dp_clk_val[0];
1731 for (i = 0; i < ARRAY_SIZE(bxt_dp_clk_val); ++i) {
1732 if (bxt_dp_clk_val[i].clock == clock) {
1733 *clk_div = bxt_dp_clk_val[i];
1738 clk_div->vco = clock * 10 / 2 * clk_div->p1 * clk_div->p2;
1741 static bool bxt_ddi_set_dpll_hw_state(struct intel_crtc_state *crtc_state,
1742 const struct bxt_clk_div *clk_div)
1744 struct intel_dpll_hw_state *dpll_hw_state = &crtc_state->dpll_hw_state;
1745 int clock = crtc_state->port_clock;
1746 int vco = clk_div->vco;
1747 u32 prop_coef, int_coef, gain_ctl, targ_cnt;
1750 memset(dpll_hw_state, 0, sizeof(*dpll_hw_state));
1752 if (vco >= 6200000 && vco <= 6700000) {
1757 } else if ((vco > 5400000 && vco < 6200000) ||
1758 (vco >= 4800000 && vco < 5400000)) {
1763 } else if (vco == 5400000) {
1769 DRM_ERROR("Invalid VCO\n");
1775 else if (clock > 135000)
1777 else if (clock > 67000)
1779 else if (clock > 33000)
1784 dpll_hw_state->ebb0 = PORT_PLL_P1(clk_div->p1) | PORT_PLL_P2(clk_div->p2);
1785 dpll_hw_state->pll0 = clk_div->m2_int;
1786 dpll_hw_state->pll1 = PORT_PLL_N(clk_div->n);
1787 dpll_hw_state->pll2 = clk_div->m2_frac;
1789 if (clk_div->m2_frac_en)
1790 dpll_hw_state->pll3 = PORT_PLL_M2_FRAC_ENABLE;
1792 dpll_hw_state->pll6 = prop_coef | PORT_PLL_INT_COEFF(int_coef);
1793 dpll_hw_state->pll6 |= PORT_PLL_GAIN_CTL(gain_ctl);
1795 dpll_hw_state->pll8 = targ_cnt;
1797 dpll_hw_state->pll9 = 5 << PORT_PLL_LOCK_THRESHOLD_SHIFT;
1799 dpll_hw_state->pll10 =
1800 PORT_PLL_DCO_AMP(PORT_PLL_DCO_AMP_DEFAULT)
1801 | PORT_PLL_DCO_AMP_OVR_EN_H;
1803 dpll_hw_state->ebb4 = PORT_PLL_10BIT_CLK_ENABLE;
1805 dpll_hw_state->pcsdw12 = LANESTAGGER_STRAP_OVRD | lanestagger;
1811 bxt_ddi_dp_set_dpll_hw_state(struct intel_crtc_state *crtc_state)
1813 struct bxt_clk_div clk_div = {};
1815 bxt_ddi_dp_pll_dividers(crtc_state, &clk_div);
1817 return bxt_ddi_set_dpll_hw_state(crtc_state, &clk_div);
1821 bxt_ddi_hdmi_set_dpll_hw_state(struct intel_crtc_state *crtc_state)
1823 struct bxt_clk_div clk_div = {};
1825 bxt_ddi_hdmi_pll_dividers(crtc_state, &clk_div);
1827 return bxt_ddi_set_dpll_hw_state(crtc_state, &clk_div);
1830 static struct intel_shared_dpll *
1831 bxt_get_dpll(struct intel_crtc_state *crtc_state,
1832 struct intel_encoder *encoder)
1834 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1835 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1836 struct intel_shared_dpll *pll;
1837 enum intel_dpll_id id;
1839 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) &&
1840 !bxt_ddi_hdmi_set_dpll_hw_state(crtc_state))
1843 if (intel_crtc_has_dp_encoder(crtc_state) &&
1844 !bxt_ddi_dp_set_dpll_hw_state(crtc_state))
1847 /* 1:1 mapping between ports and PLLs */
1848 id = (enum intel_dpll_id) encoder->port;
1849 pll = intel_get_shared_dpll_by_id(dev_priv, id);
1851 DRM_DEBUG_KMS("[CRTC:%d:%s] using pre-allocated %s\n",
1852 crtc->base.base.id, crtc->base.name, pll->info->name);
1854 intel_reference_shared_dpll(pll, crtc_state);
1859 static void bxt_dump_hw_state(struct drm_i915_private *dev_priv,
1860 const struct intel_dpll_hw_state *hw_state)
1862 DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
1863 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
1864 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
1878 static const struct intel_shared_dpll_funcs bxt_ddi_pll_funcs = {
1879 .enable = bxt_ddi_pll_enable,
1880 .disable = bxt_ddi_pll_disable,
1881 .get_hw_state = bxt_ddi_pll_get_hw_state,
1884 struct intel_dpll_mgr {
1885 const struct dpll_info *dpll_info;
1887 struct intel_shared_dpll *(*get_dpll)(struct intel_crtc_state *crtc_state,
1888 struct intel_encoder *encoder);
1890 void (*dump_hw_state)(struct drm_i915_private *dev_priv,
1891 const struct intel_dpll_hw_state *hw_state);
1894 static const struct dpll_info pch_plls[] = {
1895 { "PCH DPLL A", &ibx_pch_dpll_funcs, DPLL_ID_PCH_PLL_A, 0 },
1896 { "PCH DPLL B", &ibx_pch_dpll_funcs, DPLL_ID_PCH_PLL_B, 0 },
1900 static const struct intel_dpll_mgr pch_pll_mgr = {
1901 .dpll_info = pch_plls,
1902 .get_dpll = ibx_get_dpll,
1903 .dump_hw_state = ibx_dump_hw_state,
1906 static const struct dpll_info hsw_plls[] = {
1907 { "WRPLL 1", &hsw_ddi_wrpll_funcs, DPLL_ID_WRPLL1, 0 },
1908 { "WRPLL 2", &hsw_ddi_wrpll_funcs, DPLL_ID_WRPLL2, 0 },
1909 { "SPLL", &hsw_ddi_spll_funcs, DPLL_ID_SPLL, 0 },
1910 { "LCPLL 810", &hsw_ddi_lcpll_funcs, DPLL_ID_LCPLL_810, INTEL_DPLL_ALWAYS_ON },
1911 { "LCPLL 1350", &hsw_ddi_lcpll_funcs, DPLL_ID_LCPLL_1350, INTEL_DPLL_ALWAYS_ON },
1912 { "LCPLL 2700", &hsw_ddi_lcpll_funcs, DPLL_ID_LCPLL_2700, INTEL_DPLL_ALWAYS_ON },
1916 static const struct intel_dpll_mgr hsw_pll_mgr = {
1917 .dpll_info = hsw_plls,
1918 .get_dpll = hsw_get_dpll,
1919 .dump_hw_state = hsw_dump_hw_state,
1922 static const struct dpll_info skl_plls[] = {
1923 { "DPLL 0", &skl_ddi_dpll0_funcs, DPLL_ID_SKL_DPLL0, INTEL_DPLL_ALWAYS_ON },
1924 { "DPLL 1", &skl_ddi_pll_funcs, DPLL_ID_SKL_DPLL1, 0 },
1925 { "DPLL 2", &skl_ddi_pll_funcs, DPLL_ID_SKL_DPLL2, 0 },
1926 { "DPLL 3", &skl_ddi_pll_funcs, DPLL_ID_SKL_DPLL3, 0 },
1930 static const struct intel_dpll_mgr skl_pll_mgr = {
1931 .dpll_info = skl_plls,
1932 .get_dpll = skl_get_dpll,
1933 .dump_hw_state = skl_dump_hw_state,
1936 static const struct dpll_info bxt_plls[] = {
1937 { "PORT PLL A", &bxt_ddi_pll_funcs, DPLL_ID_SKL_DPLL0, 0 },
1938 { "PORT PLL B", &bxt_ddi_pll_funcs, DPLL_ID_SKL_DPLL1, 0 },
1939 { "PORT PLL C", &bxt_ddi_pll_funcs, DPLL_ID_SKL_DPLL2, 0 },
1943 static const struct intel_dpll_mgr bxt_pll_mgr = {
1944 .dpll_info = bxt_plls,
1945 .get_dpll = bxt_get_dpll,
1946 .dump_hw_state = bxt_dump_hw_state,
1949 static void cnl_ddi_pll_enable(struct drm_i915_private *dev_priv,
1950 struct intel_shared_dpll *pll)
1952 const enum intel_dpll_id id = pll->info->id;
1955 /* 1. Enable DPLL power in DPLL_ENABLE. */
1956 val = I915_READ(CNL_DPLL_ENABLE(id));
1957 val |= PLL_POWER_ENABLE;
1958 I915_WRITE(CNL_DPLL_ENABLE(id), val);
1960 /* 2. Wait for DPLL power state enabled in DPLL_ENABLE. */
1961 if (intel_wait_for_register(&dev_priv->uncore,
1962 CNL_DPLL_ENABLE(id),
1966 DRM_ERROR("PLL %d Power not enabled\n", id);
1969 * 3. Configure DPLL_CFGCR0 to set SSC enable/disable,
1970 * select DP mode, and set DP link rate.
1972 val = pll->state.hw_state.cfgcr0;
1973 I915_WRITE(CNL_DPLL_CFGCR0(id), val);
1975 /* 4. Reab back to ensure writes completed */
1976 POSTING_READ(CNL_DPLL_CFGCR0(id));
1978 /* 3. Configure DPLL_CFGCR0 */
1979 /* Avoid touch CFGCR1 if HDMI mode is not enabled */
1980 if (pll->state.hw_state.cfgcr0 & DPLL_CFGCR0_HDMI_MODE) {
1981 val = pll->state.hw_state.cfgcr1;
1982 I915_WRITE(CNL_DPLL_CFGCR1(id), val);
1983 /* 4. Reab back to ensure writes completed */
1984 POSTING_READ(CNL_DPLL_CFGCR1(id));
1988 * 5. If the frequency will result in a change to the voltage
1989 * requirement, follow the Display Voltage Frequency Switching
1990 * Sequence Before Frequency Change
1992 * Note: DVFS is actually handled via the cdclk code paths,
1993 * hence we do nothing here.
1996 /* 6. Enable DPLL in DPLL_ENABLE. */
1997 val = I915_READ(CNL_DPLL_ENABLE(id));
1999 I915_WRITE(CNL_DPLL_ENABLE(id), val);
2001 /* 7. Wait for PLL lock status in DPLL_ENABLE. */
2002 if (intel_wait_for_register(&dev_priv->uncore,
2003 CNL_DPLL_ENABLE(id),
2007 DRM_ERROR("PLL %d not locked\n", id);
2010 * 8. If the frequency will result in a change to the voltage
2011 * requirement, follow the Display Voltage Frequency Switching
2012 * Sequence After Frequency Change
2014 * Note: DVFS is actually handled via the cdclk code paths,
2015 * hence we do nothing here.
2019 * 9. turn on the clock for the DDI and map the DPLL to the DDI
2020 * Done at intel_ddi_clk_select
2024 static void cnl_ddi_pll_disable(struct drm_i915_private *dev_priv,
2025 struct intel_shared_dpll *pll)
2027 const enum intel_dpll_id id = pll->info->id;
2031 * 1. Configure DPCLKA_CFGCR0 to turn off the clock for the DDI.
2032 * Done at intel_ddi_post_disable
2036 * 2. If the frequency will result in a change to the voltage
2037 * requirement, follow the Display Voltage Frequency Switching
2038 * Sequence Before Frequency Change
2040 * Note: DVFS is actually handled via the cdclk code paths,
2041 * hence we do nothing here.
2044 /* 3. Disable DPLL through DPLL_ENABLE. */
2045 val = I915_READ(CNL_DPLL_ENABLE(id));
2047 I915_WRITE(CNL_DPLL_ENABLE(id), val);
2049 /* 4. Wait for PLL not locked status in DPLL_ENABLE. */
2050 if (intel_wait_for_register(&dev_priv->uncore,
2051 CNL_DPLL_ENABLE(id),
2055 DRM_ERROR("PLL %d locked\n", id);
2058 * 5. If the frequency will result in a change to the voltage
2059 * requirement, follow the Display Voltage Frequency Switching
2060 * Sequence After Frequency Change
2062 * Note: DVFS is actually handled via the cdclk code paths,
2063 * hence we do nothing here.
2066 /* 6. Disable DPLL power in DPLL_ENABLE. */
2067 val = I915_READ(CNL_DPLL_ENABLE(id));
2068 val &= ~PLL_POWER_ENABLE;
2069 I915_WRITE(CNL_DPLL_ENABLE(id), val);
2071 /* 7. Wait for DPLL power state disabled in DPLL_ENABLE. */
2072 if (intel_wait_for_register(&dev_priv->uncore,
2073 CNL_DPLL_ENABLE(id),
2077 DRM_ERROR("PLL %d Power not disabled\n", id);
2080 static bool cnl_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
2081 struct intel_shared_dpll *pll,
2082 struct intel_dpll_hw_state *hw_state)
2084 const enum intel_dpll_id id = pll->info->id;
2085 intel_wakeref_t wakeref;
2089 wakeref = intel_display_power_get_if_enabled(dev_priv,
2090 POWER_DOMAIN_DISPLAY_CORE);
2096 val = I915_READ(CNL_DPLL_ENABLE(id));
2097 if (!(val & PLL_ENABLE))
2100 val = I915_READ(CNL_DPLL_CFGCR0(id));
2101 hw_state->cfgcr0 = val;
2103 /* avoid reading back stale values if HDMI mode is not enabled */
2104 if (val & DPLL_CFGCR0_HDMI_MODE) {
2105 hw_state->cfgcr1 = I915_READ(CNL_DPLL_CFGCR1(id));
2110 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
2115 static void cnl_wrpll_get_multipliers(int bestdiv, int *pdiv,
2116 int *qdiv, int *kdiv)
2119 if (bestdiv % 2 == 0) {
2124 } else if (bestdiv % 4 == 0) {
2126 *qdiv = bestdiv / 4;
2128 } else if (bestdiv % 6 == 0) {
2130 *qdiv = bestdiv / 6;
2132 } else if (bestdiv % 5 == 0) {
2134 *qdiv = bestdiv / 10;
2136 } else if (bestdiv % 14 == 0) {
2138 *qdiv = bestdiv / 14;
2142 if (bestdiv == 3 || bestdiv == 5 || bestdiv == 7) {
2146 } else { /* 9, 15, 21 */
2147 *pdiv = bestdiv / 3;
2154 static void cnl_wrpll_params_populate(struct skl_wrpll_params *params,
2155 u32 dco_freq, u32 ref_freq,
2156 int pdiv, int qdiv, int kdiv)
2171 WARN(1, "Incorrect KDiv\n");
2188 WARN(1, "Incorrect PDiv\n");
2191 WARN_ON(kdiv != 2 && qdiv != 1);
2193 params->qdiv_ratio = qdiv;
2194 params->qdiv_mode = (qdiv == 1) ? 0 : 1;
2196 dco = div_u64((u64)dco_freq << 15, ref_freq);
2198 params->dco_integer = dco >> 15;
2199 params->dco_fraction = dco & 0x7fff;
2202 int cnl_hdmi_pll_ref_clock(struct drm_i915_private *dev_priv)
2204 int ref_clock = dev_priv->cdclk.hw.ref;
2207 * For ICL+, the spec states: if reference frequency is 38.4,
2208 * use 19.2 because the DPLL automatically divides that by 2.
2210 if (INTEL_GEN(dev_priv) >= 11 && ref_clock == 38400)
2217 cnl_ddi_calculate_wrpll(struct intel_crtc_state *crtc_state,
2218 struct skl_wrpll_params *wrpll_params)
2220 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2221 u32 afe_clock = crtc_state->port_clock * 5;
2223 u32 dco_min = 7998000;
2224 u32 dco_max = 10000000;
2225 u32 dco_mid = (dco_min + dco_max) / 2;
2226 static const int dividers[] = { 2, 4, 6, 8, 10, 12, 14, 16,
2227 18, 20, 24, 28, 30, 32, 36, 40,
2228 42, 44, 48, 50, 52, 54, 56, 60,
2229 64, 66, 68, 70, 72, 76, 78, 80,
2230 84, 88, 90, 92, 96, 98, 100, 102,
2231 3, 5, 7, 9, 15, 21 };
2232 u32 dco, best_dco = 0, dco_centrality = 0;
2233 u32 best_dco_centrality = U32_MAX; /* Spec meaning of 999999 MHz */
2234 int d, best_div = 0, pdiv = 0, qdiv = 0, kdiv = 0;
2236 for (d = 0; d < ARRAY_SIZE(dividers); d++) {
2237 dco = afe_clock * dividers[d];
2239 if ((dco <= dco_max) && (dco >= dco_min)) {
2240 dco_centrality = abs(dco - dco_mid);
2242 if (dco_centrality < best_dco_centrality) {
2243 best_dco_centrality = dco_centrality;
2244 best_div = dividers[d];
2253 cnl_wrpll_get_multipliers(best_div, &pdiv, &qdiv, &kdiv);
2255 ref_clock = cnl_hdmi_pll_ref_clock(dev_priv);
2257 cnl_wrpll_params_populate(wrpll_params, best_dco, ref_clock,
2263 static bool cnl_ddi_hdmi_pll_dividers(struct intel_crtc_state *crtc_state)
2266 struct skl_wrpll_params wrpll_params = { 0, };
2268 cfgcr0 = DPLL_CFGCR0_HDMI_MODE;
2270 if (!cnl_ddi_calculate_wrpll(crtc_state, &wrpll_params))
2273 cfgcr0 |= DPLL_CFGCR0_DCO_FRACTION(wrpll_params.dco_fraction) |
2274 wrpll_params.dco_integer;
2276 cfgcr1 = DPLL_CFGCR1_QDIV_RATIO(wrpll_params.qdiv_ratio) |
2277 DPLL_CFGCR1_QDIV_MODE(wrpll_params.qdiv_mode) |
2278 DPLL_CFGCR1_KDIV(wrpll_params.kdiv) |
2279 DPLL_CFGCR1_PDIV(wrpll_params.pdiv) |
2280 DPLL_CFGCR1_CENTRAL_FREQ;
2282 memset(&crtc_state->dpll_hw_state, 0,
2283 sizeof(crtc_state->dpll_hw_state));
2285 crtc_state->dpll_hw_state.cfgcr0 = cfgcr0;
2286 crtc_state->dpll_hw_state.cfgcr1 = cfgcr1;
2291 cnl_ddi_dp_set_dpll_hw_state(struct intel_crtc_state *crtc_state)
2295 cfgcr0 = DPLL_CFGCR0_SSC_ENABLE;
2297 switch (crtc_state->port_clock / 2) {
2299 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_810;
2302 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_1350;
2305 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_2700;
2309 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_1620;
2312 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_1080;
2315 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_2160;
2318 /* Some SKUs may require elevated I/O voltage to support this */
2319 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_3240;
2322 /* Some SKUs may require elevated I/O voltage to support this */
2323 cfgcr0 |= DPLL_CFGCR0_LINK_RATE_4050;
2327 memset(&crtc_state->dpll_hw_state, 0,
2328 sizeof(crtc_state->dpll_hw_state));
2330 crtc_state->dpll_hw_state.cfgcr0 = cfgcr0;
2335 static struct intel_shared_dpll *
2336 cnl_get_dpll(struct intel_crtc_state *crtc_state,
2337 struct intel_encoder *encoder)
2339 struct intel_shared_dpll *pll;
2342 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
2343 bret = cnl_ddi_hdmi_pll_dividers(crtc_state);
2345 DRM_DEBUG_KMS("Could not get HDMI pll dividers.\n");
2348 } else if (intel_crtc_has_dp_encoder(crtc_state)) {
2349 bret = cnl_ddi_dp_set_dpll_hw_state(crtc_state);
2351 DRM_DEBUG_KMS("Could not set DP dpll HW state.\n");
2355 DRM_DEBUG_KMS("Skip DPLL setup for output_types 0x%x\n",
2356 crtc_state->output_types);
2360 pll = intel_find_shared_dpll(crtc_state,
2364 DRM_DEBUG_KMS("No PLL selected\n");
2368 intel_reference_shared_dpll(pll, crtc_state);
2373 static void cnl_dump_hw_state(struct drm_i915_private *dev_priv,
2374 const struct intel_dpll_hw_state *hw_state)
2376 DRM_DEBUG_KMS("dpll_hw_state: "
2377 "cfgcr0: 0x%x, cfgcr1: 0x%x\n",
2382 static const struct intel_shared_dpll_funcs cnl_ddi_pll_funcs = {
2383 .enable = cnl_ddi_pll_enable,
2384 .disable = cnl_ddi_pll_disable,
2385 .get_hw_state = cnl_ddi_pll_get_hw_state,
2388 static const struct dpll_info cnl_plls[] = {
2389 { "DPLL 0", &cnl_ddi_pll_funcs, DPLL_ID_SKL_DPLL0, 0 },
2390 { "DPLL 1", &cnl_ddi_pll_funcs, DPLL_ID_SKL_DPLL1, 0 },
2391 { "DPLL 2", &cnl_ddi_pll_funcs, DPLL_ID_SKL_DPLL2, 0 },
2395 static const struct intel_dpll_mgr cnl_pll_mgr = {
2396 .dpll_info = cnl_plls,
2397 .get_dpll = cnl_get_dpll,
2398 .dump_hw_state = cnl_dump_hw_state,
2401 struct icl_combo_pll_params {
2403 struct skl_wrpll_params wrpll;
2407 * These values alrea already adjusted: they're the bits we write to the
2408 * registers, not the logical values.
2410 static const struct icl_combo_pll_params icl_dp_combo_pll_24MHz_values[] = {
2412 { .dco_integer = 0x151, .dco_fraction = 0x4000, /* [0]: 5.4 */
2413 .pdiv = 0x2 /* 3 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2415 { .dco_integer = 0x151, .dco_fraction = 0x4000, /* [1]: 2.7 */
2416 .pdiv = 0x2 /* 3 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2418 { .dco_integer = 0x151, .dco_fraction = 0x4000, /* [2]: 1.62 */
2419 .pdiv = 0x4 /* 5 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2421 { .dco_integer = 0x151, .dco_fraction = 0x4000, /* [3]: 3.24 */
2422 .pdiv = 0x4 /* 5 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2424 { .dco_integer = 0x168, .dco_fraction = 0x0000, /* [4]: 2.16 */
2425 .pdiv = 0x1 /* 2 */, .kdiv = 2, .qdiv_mode = 1, .qdiv_ratio = 2, }, },
2427 { .dco_integer = 0x168, .dco_fraction = 0x0000, /* [5]: 4.32 */
2428 .pdiv = 0x1 /* 2 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2430 { .dco_integer = 0x195, .dco_fraction = 0x0000, /* [6]: 6.48 */
2431 .pdiv = 0x2 /* 3 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2433 { .dco_integer = 0x151, .dco_fraction = 0x4000, /* [7]: 8.1 */
2434 .pdiv = 0x1 /* 2 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2438 /* Also used for 38.4 MHz values. */
2439 static const struct icl_combo_pll_params icl_dp_combo_pll_19_2MHz_values[] = {
2441 { .dco_integer = 0x1A5, .dco_fraction = 0x7000, /* [0]: 5.4 */
2442 .pdiv = 0x2 /* 3 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2444 { .dco_integer = 0x1A5, .dco_fraction = 0x7000, /* [1]: 2.7 */
2445 .pdiv = 0x2 /* 3 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2447 { .dco_integer = 0x1A5, .dco_fraction = 0x7000, /* [2]: 1.62 */
2448 .pdiv = 0x4 /* 5 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2450 { .dco_integer = 0x1A5, .dco_fraction = 0x7000, /* [3]: 3.24 */
2451 .pdiv = 0x4 /* 5 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2453 { .dco_integer = 0x1C2, .dco_fraction = 0x0000, /* [4]: 2.16 */
2454 .pdiv = 0x1 /* 2 */, .kdiv = 2, .qdiv_mode = 1, .qdiv_ratio = 2, }, },
2456 { .dco_integer = 0x1C2, .dco_fraction = 0x0000, /* [5]: 4.32 */
2457 .pdiv = 0x1 /* 2 */, .kdiv = 2, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2459 { .dco_integer = 0x1FA, .dco_fraction = 0x2000, /* [6]: 6.48 */
2460 .pdiv = 0x2 /* 3 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2462 { .dco_integer = 0x1A5, .dco_fraction = 0x7000, /* [7]: 8.1 */
2463 .pdiv = 0x1 /* 2 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0, }, },
2466 static const struct skl_wrpll_params icl_tbt_pll_24MHz_values = {
2467 .dco_integer = 0x151, .dco_fraction = 0x4000,
2468 .pdiv = 0x4 /* 5 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0,
2471 static const struct skl_wrpll_params icl_tbt_pll_19_2MHz_values = {
2472 .dco_integer = 0x1A5, .dco_fraction = 0x7000,
2473 .pdiv = 0x4 /* 5 */, .kdiv = 1, .qdiv_mode = 0, .qdiv_ratio = 0,
2476 static bool icl_calc_dp_combo_pll(struct intel_crtc_state *crtc_state,
2477 struct skl_wrpll_params *pll_params)
2479 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2480 const struct icl_combo_pll_params *params =
2481 dev_priv->cdclk.hw.ref == 24000 ?
2482 icl_dp_combo_pll_24MHz_values :
2483 icl_dp_combo_pll_19_2MHz_values;
2484 int clock = crtc_state->port_clock;
2487 for (i = 0; i < ARRAY_SIZE(icl_dp_combo_pll_24MHz_values); i++) {
2488 if (clock == params[i].clock) {
2489 *pll_params = params[i].wrpll;
2494 MISSING_CASE(clock);
2498 static bool icl_calc_tbt_pll(struct intel_crtc_state *crtc_state,
2499 struct skl_wrpll_params *pll_params)
2501 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2503 *pll_params = dev_priv->cdclk.hw.ref == 24000 ?
2504 icl_tbt_pll_24MHz_values : icl_tbt_pll_19_2MHz_values;
2508 static bool icl_calc_dpll_state(struct intel_crtc_state *crtc_state,
2509 struct intel_encoder *encoder)
2511 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2513 struct skl_wrpll_params pll_params = { 0 };
2516 if (intel_port_is_tc(dev_priv, encoder->port))
2517 ret = icl_calc_tbt_pll(crtc_state, &pll_params);
2518 else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
2519 intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
2520 ret = cnl_ddi_calculate_wrpll(crtc_state, &pll_params);
2522 ret = icl_calc_dp_combo_pll(crtc_state, &pll_params);
2527 cfgcr0 = DPLL_CFGCR0_DCO_FRACTION(pll_params.dco_fraction) |
2528 pll_params.dco_integer;
2530 cfgcr1 = DPLL_CFGCR1_QDIV_RATIO(pll_params.qdiv_ratio) |
2531 DPLL_CFGCR1_QDIV_MODE(pll_params.qdiv_mode) |
2532 DPLL_CFGCR1_KDIV(pll_params.kdiv) |
2533 DPLL_CFGCR1_PDIV(pll_params.pdiv) |
2534 DPLL_CFGCR1_CENTRAL_FREQ_8400;
2536 memset(&crtc_state->dpll_hw_state, 0,
2537 sizeof(crtc_state->dpll_hw_state));
2539 crtc_state->dpll_hw_state.cfgcr0 = cfgcr0;
2540 crtc_state->dpll_hw_state.cfgcr1 = cfgcr1;
2546 static enum tc_port icl_pll_id_to_tc_port(enum intel_dpll_id id)
2548 return id - DPLL_ID_ICL_MGPLL1;
2551 enum intel_dpll_id icl_tc_port_to_pll_id(enum tc_port tc_port)
2553 return tc_port + DPLL_ID_ICL_MGPLL1;
2556 static bool icl_mg_pll_find_divisors(int clock_khz, bool is_dp, bool use_ssc,
2557 u32 *target_dco_khz,
2558 struct intel_dpll_hw_state *state)
2560 u32 dco_min_freq, dco_max_freq;
2561 int div1_vals[] = {7, 5, 3, 2};
2565 dco_min_freq = is_dp ? 8100000 : use_ssc ? 8000000 : 7992000;
2566 dco_max_freq = is_dp ? 8100000 : 10000000;
2568 for (i = 0; i < ARRAY_SIZE(div1_vals); i++) {
2569 int div1 = div1_vals[i];
2571 for (div2 = 10; div2 > 0; div2--) {
2572 int dco = div1 * div2 * clock_khz * 5;
2573 int a_divratio, tlinedrv, inputsel;
2576 if (dco < dco_min_freq || dco > dco_max_freq)
2580 a_divratio = is_dp ? 10 : 5;
2586 inputsel = is_dp ? 0 : 1;
2593 hsdiv = MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_2;
2596 hsdiv = MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_3;
2599 hsdiv = MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_5;
2602 hsdiv = MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_7;
2606 *target_dco_khz = dco;
2608 state->mg_refclkin_ctl = MG_REFCLKIN_CTL_OD_2_MUX(1);
2610 state->mg_clktop2_coreclkctl1 =
2611 MG_CLKTOP2_CORECLKCTL1_A_DIVRATIO(a_divratio);
2613 state->mg_clktop2_hsclkctl =
2614 MG_CLKTOP2_HSCLKCTL_TLINEDRV_CLKSEL(tlinedrv) |
2615 MG_CLKTOP2_HSCLKCTL_CORE_INPUTSEL(inputsel) |
2617 MG_CLKTOP2_HSCLKCTL_DSDIV_RATIO(div2);
2627 * The specification for this function uses real numbers, so the math had to be
2628 * adapted to integer-only calculation, that's why it looks so different.
2630 static bool icl_calc_mg_pll_state(struct intel_crtc_state *crtc_state)
2632 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2633 struct intel_dpll_hw_state *pll_state = &crtc_state->dpll_hw_state;
2634 int refclk_khz = dev_priv->cdclk.hw.ref;
2635 int clock = crtc_state->port_clock;
2636 u32 dco_khz, m1div, m2div_int, m2div_rem, m2div_frac;
2637 u32 iref_ndiv, iref_trim, iref_pulse_w;
2638 u32 prop_coeff, int_coeff;
2639 u32 tdc_targetcnt, feedfwgain;
2640 u64 ssc_stepsize, ssc_steplen, ssc_steplog;
2642 bool use_ssc = false;
2643 bool is_dp = !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI);
2645 memset(pll_state, 0, sizeof(*pll_state));
2647 if (!icl_mg_pll_find_divisors(clock, is_dp, use_ssc, &dco_khz,
2649 DRM_DEBUG_KMS("Failed to find divisors for clock %d\n", clock);
2654 m2div_int = dco_khz / (refclk_khz * m1div);
2655 if (m2div_int > 255) {
2657 m2div_int = dco_khz / (refclk_khz * m1div);
2658 if (m2div_int > 255) {
2659 DRM_DEBUG_KMS("Failed to find mdiv for clock %d\n",
2664 m2div_rem = dco_khz % (refclk_khz * m1div);
2666 tmp = (u64)m2div_rem * (1 << 22);
2667 do_div(tmp, refclk_khz * m1div);
2670 switch (refclk_khz) {
2687 MISSING_CASE(refclk_khz);
2692 * tdc_res = 0.000003
2693 * tdc_targetcnt = int(2 / (tdc_res * 8 * 50 * 1.1) / refclk_mhz + 0.5)
2695 * The multiplication by 1000 is due to refclk MHz to KHz conversion. It
2696 * was supposed to be a division, but we rearranged the operations of
2697 * the formula to avoid early divisions so we don't multiply the
2700 * 0.000003 * 8 * 50 * 1.1 = 0.00132, also known as 132 / 100000, which
2701 * we also rearrange to work with integers.
2703 * The 0.5 transformed to 5 results in a multiplication by 10 and the
2704 * last division by 10.
2706 tdc_targetcnt = (2 * 1000 * 100000 * 10 / (132 * refclk_khz) + 5) / 10;
2709 * Here we divide dco_khz by 10 in order to allow the dividend to fit in
2710 * 32 bits. That's not a problem since we round the division down
2713 feedfwgain = (use_ssc || m2div_rem > 0) ?
2714 m1div * 1000000 * 100 / (dco_khz * 3 / 10) : 0;
2716 if (dco_khz >= 9000000) {
2725 tmp = mul_u32_u32(dco_khz, 47 * 32);
2726 do_div(tmp, refclk_khz * m1div * 10000);
2729 tmp = mul_u32_u32(dco_khz, 1000);
2730 ssc_steplen = DIV_ROUND_UP_ULL(tmp, 32 * 2 * 32);
2737 pll_state->mg_pll_div0 = (m2div_rem > 0 ? MG_PLL_DIV0_FRACNEN_H : 0) |
2738 MG_PLL_DIV0_FBDIV_FRAC(m2div_frac) |
2739 MG_PLL_DIV0_FBDIV_INT(m2div_int);
2741 pll_state->mg_pll_div1 = MG_PLL_DIV1_IREF_NDIVRATIO(iref_ndiv) |
2742 MG_PLL_DIV1_DITHER_DIV_2 |
2743 MG_PLL_DIV1_NDIVRATIO(1) |
2744 MG_PLL_DIV1_FBPREDIV(m1div);
2746 pll_state->mg_pll_lf = MG_PLL_LF_TDCTARGETCNT(tdc_targetcnt) |
2747 MG_PLL_LF_AFCCNTSEL_512 |
2748 MG_PLL_LF_GAINCTRL(1) |
2749 MG_PLL_LF_INT_COEFF(int_coeff) |
2750 MG_PLL_LF_PROP_COEFF(prop_coeff);
2752 pll_state->mg_pll_frac_lock = MG_PLL_FRAC_LOCK_TRUELOCK_CRIT_32 |
2753 MG_PLL_FRAC_LOCK_EARLYLOCK_CRIT_32 |
2754 MG_PLL_FRAC_LOCK_LOCKTHRESH(10) |
2755 MG_PLL_FRAC_LOCK_DCODITHEREN |
2756 MG_PLL_FRAC_LOCK_FEEDFWRDGAIN(feedfwgain);
2757 if (use_ssc || m2div_rem > 0)
2758 pll_state->mg_pll_frac_lock |= MG_PLL_FRAC_LOCK_FEEDFWRDCAL_EN;
2760 pll_state->mg_pll_ssc = (use_ssc ? MG_PLL_SSC_EN : 0) |
2761 MG_PLL_SSC_TYPE(2) |
2762 MG_PLL_SSC_STEPLENGTH(ssc_steplen) |
2763 MG_PLL_SSC_STEPNUM(ssc_steplog) |
2765 MG_PLL_SSC_STEPSIZE(ssc_stepsize);
2767 pll_state->mg_pll_tdc_coldst_bias = MG_PLL_TDC_COLDST_COLDSTART |
2768 MG_PLL_TDC_COLDST_IREFINT_EN |
2769 MG_PLL_TDC_COLDST_REFBIAS_START_PULSE_W(iref_pulse_w) |
2770 MG_PLL_TDC_TDCOVCCORR_EN |
2771 MG_PLL_TDC_TDCSEL(3);
2773 pll_state->mg_pll_bias = MG_PLL_BIAS_BIAS_GB_SEL(3) |
2774 MG_PLL_BIAS_INIT_DCOAMP(0x3F) |
2775 MG_PLL_BIAS_BIAS_BONUS(10) |
2776 MG_PLL_BIAS_BIASCAL_EN |
2777 MG_PLL_BIAS_CTRIM(12) |
2778 MG_PLL_BIAS_VREF_RDAC(4) |
2779 MG_PLL_BIAS_IREFTRIM(iref_trim);
2781 if (refclk_khz == 38400) {
2782 pll_state->mg_pll_tdc_coldst_bias_mask = MG_PLL_TDC_COLDST_COLDSTART;
2783 pll_state->mg_pll_bias_mask = 0;
2785 pll_state->mg_pll_tdc_coldst_bias_mask = -1U;
2786 pll_state->mg_pll_bias_mask = -1U;
2789 pll_state->mg_pll_tdc_coldst_bias &= pll_state->mg_pll_tdc_coldst_bias_mask;
2790 pll_state->mg_pll_bias &= pll_state->mg_pll_bias_mask;
2795 static struct intel_shared_dpll *
2796 icl_get_dpll(struct intel_crtc_state *crtc_state,
2797 struct intel_encoder *encoder)
2799 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2800 struct intel_digital_port *intel_dig_port;
2801 struct intel_shared_dpll *pll;
2802 enum port port = encoder->port;
2803 enum intel_dpll_id min, max;
2806 if (intel_port_is_combophy(dev_priv, port)) {
2807 min = DPLL_ID_ICL_DPLL0;
2808 max = DPLL_ID_ICL_DPLL1;
2809 ret = icl_calc_dpll_state(crtc_state, encoder);
2810 } else if (intel_port_is_tc(dev_priv, port)) {
2811 if (encoder->type == INTEL_OUTPUT_DP_MST) {
2812 struct intel_dp_mst_encoder *mst_encoder;
2814 mst_encoder = enc_to_mst(&encoder->base);
2815 intel_dig_port = mst_encoder->primary;
2817 intel_dig_port = enc_to_dig_port(&encoder->base);
2820 if (intel_dig_port->tc_type == TC_PORT_TBT) {
2821 min = DPLL_ID_ICL_TBTPLL;
2823 ret = icl_calc_dpll_state(crtc_state, encoder);
2825 enum tc_port tc_port;
2827 tc_port = intel_port_to_tc(dev_priv, port);
2828 min = icl_tc_port_to_pll_id(tc_port);
2830 ret = icl_calc_mg_pll_state(crtc_state);
2838 DRM_DEBUG_KMS("Could not calculate PLL state.\n");
2843 pll = intel_find_shared_dpll(crtc_state, min, max);
2845 DRM_DEBUG_KMS("No PLL selected\n");
2849 intel_reference_shared_dpll(pll, crtc_state);
2854 static bool mg_pll_get_hw_state(struct drm_i915_private *dev_priv,
2855 struct intel_shared_dpll *pll,
2856 struct intel_dpll_hw_state *hw_state)
2858 const enum intel_dpll_id id = pll->info->id;
2859 enum tc_port tc_port = icl_pll_id_to_tc_port(id);
2860 intel_wakeref_t wakeref;
2864 wakeref = intel_display_power_get_if_enabled(dev_priv,
2865 POWER_DOMAIN_DISPLAY_CORE);
2869 val = I915_READ(MG_PLL_ENABLE(tc_port));
2870 if (!(val & PLL_ENABLE))
2873 hw_state->mg_refclkin_ctl = I915_READ(MG_REFCLKIN_CTL(tc_port));
2874 hw_state->mg_refclkin_ctl &= MG_REFCLKIN_CTL_OD_2_MUX_MASK;
2876 hw_state->mg_clktop2_coreclkctl1 =
2877 I915_READ(MG_CLKTOP2_CORECLKCTL1(tc_port));
2878 hw_state->mg_clktop2_coreclkctl1 &=
2879 MG_CLKTOP2_CORECLKCTL1_A_DIVRATIO_MASK;
2881 hw_state->mg_clktop2_hsclkctl =
2882 I915_READ(MG_CLKTOP2_HSCLKCTL(tc_port));
2883 hw_state->mg_clktop2_hsclkctl &=
2884 MG_CLKTOP2_HSCLKCTL_TLINEDRV_CLKSEL_MASK |
2885 MG_CLKTOP2_HSCLKCTL_CORE_INPUTSEL_MASK |
2886 MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_MASK |
2887 MG_CLKTOP2_HSCLKCTL_DSDIV_RATIO_MASK;
2889 hw_state->mg_pll_div0 = I915_READ(MG_PLL_DIV0(tc_port));
2890 hw_state->mg_pll_div1 = I915_READ(MG_PLL_DIV1(tc_port));
2891 hw_state->mg_pll_lf = I915_READ(MG_PLL_LF(tc_port));
2892 hw_state->mg_pll_frac_lock = I915_READ(MG_PLL_FRAC_LOCK(tc_port));
2893 hw_state->mg_pll_ssc = I915_READ(MG_PLL_SSC(tc_port));
2895 hw_state->mg_pll_bias = I915_READ(MG_PLL_BIAS(tc_port));
2896 hw_state->mg_pll_tdc_coldst_bias =
2897 I915_READ(MG_PLL_TDC_COLDST_BIAS(tc_port));
2899 if (dev_priv->cdclk.hw.ref == 38400) {
2900 hw_state->mg_pll_tdc_coldst_bias_mask = MG_PLL_TDC_COLDST_COLDSTART;
2901 hw_state->mg_pll_bias_mask = 0;
2903 hw_state->mg_pll_tdc_coldst_bias_mask = -1U;
2904 hw_state->mg_pll_bias_mask = -1U;
2907 hw_state->mg_pll_tdc_coldst_bias &= hw_state->mg_pll_tdc_coldst_bias_mask;
2908 hw_state->mg_pll_bias &= hw_state->mg_pll_bias_mask;
2912 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
2916 static bool icl_pll_get_hw_state(struct drm_i915_private *dev_priv,
2917 struct intel_shared_dpll *pll,
2918 struct intel_dpll_hw_state *hw_state,
2919 i915_reg_t enable_reg)
2921 const enum intel_dpll_id id = pll->info->id;
2922 intel_wakeref_t wakeref;
2926 wakeref = intel_display_power_get_if_enabled(dev_priv,
2927 POWER_DOMAIN_DISPLAY_CORE);
2931 val = I915_READ(enable_reg);
2932 if (!(val & PLL_ENABLE))
2935 hw_state->cfgcr0 = I915_READ(ICL_DPLL_CFGCR0(id));
2936 hw_state->cfgcr1 = I915_READ(ICL_DPLL_CFGCR1(id));
2940 intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
2944 static bool combo_pll_get_hw_state(struct drm_i915_private *dev_priv,
2945 struct intel_shared_dpll *pll,
2946 struct intel_dpll_hw_state *hw_state)
2948 return icl_pll_get_hw_state(dev_priv, pll, hw_state,
2949 CNL_DPLL_ENABLE(pll->info->id));
2952 static bool tbt_pll_get_hw_state(struct drm_i915_private *dev_priv,
2953 struct intel_shared_dpll *pll,
2954 struct intel_dpll_hw_state *hw_state)
2956 return icl_pll_get_hw_state(dev_priv, pll, hw_state, TBT_PLL_ENABLE);
2959 static void icl_dpll_write(struct drm_i915_private *dev_priv,
2960 struct intel_shared_dpll *pll)
2962 struct intel_dpll_hw_state *hw_state = &pll->state.hw_state;
2963 const enum intel_dpll_id id = pll->info->id;
2965 I915_WRITE(ICL_DPLL_CFGCR0(id), hw_state->cfgcr0);
2966 I915_WRITE(ICL_DPLL_CFGCR1(id), hw_state->cfgcr1);
2967 POSTING_READ(ICL_DPLL_CFGCR1(id));
2970 static void icl_mg_pll_write(struct drm_i915_private *dev_priv,
2971 struct intel_shared_dpll *pll)
2973 struct intel_dpll_hw_state *hw_state = &pll->state.hw_state;
2974 enum tc_port tc_port = icl_pll_id_to_tc_port(pll->info->id);
2978 * Some of the following registers have reserved fields, so program
2979 * these with RMW based on a mask. The mask can be fixed or generated
2980 * during the calc/readout phase if the mask depends on some other HW
2981 * state like refclk, see icl_calc_mg_pll_state().
2983 val = I915_READ(MG_REFCLKIN_CTL(tc_port));
2984 val &= ~MG_REFCLKIN_CTL_OD_2_MUX_MASK;
2985 val |= hw_state->mg_refclkin_ctl;
2986 I915_WRITE(MG_REFCLKIN_CTL(tc_port), val);
2988 val = I915_READ(MG_CLKTOP2_CORECLKCTL1(tc_port));
2989 val &= ~MG_CLKTOP2_CORECLKCTL1_A_DIVRATIO_MASK;
2990 val |= hw_state->mg_clktop2_coreclkctl1;
2991 I915_WRITE(MG_CLKTOP2_CORECLKCTL1(tc_port), val);
2993 val = I915_READ(MG_CLKTOP2_HSCLKCTL(tc_port));
2994 val &= ~(MG_CLKTOP2_HSCLKCTL_TLINEDRV_CLKSEL_MASK |
2995 MG_CLKTOP2_HSCLKCTL_CORE_INPUTSEL_MASK |
2996 MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_MASK |
2997 MG_CLKTOP2_HSCLKCTL_DSDIV_RATIO_MASK);
2998 val |= hw_state->mg_clktop2_hsclkctl;
2999 I915_WRITE(MG_CLKTOP2_HSCLKCTL(tc_port), val);
3001 I915_WRITE(MG_PLL_DIV0(tc_port), hw_state->mg_pll_div0);
3002 I915_WRITE(MG_PLL_DIV1(tc_port), hw_state->mg_pll_div1);
3003 I915_WRITE(MG_PLL_LF(tc_port), hw_state->mg_pll_lf);
3004 I915_WRITE(MG_PLL_FRAC_LOCK(tc_port), hw_state->mg_pll_frac_lock);
3005 I915_WRITE(MG_PLL_SSC(tc_port), hw_state->mg_pll_ssc);
3007 val = I915_READ(MG_PLL_BIAS(tc_port));
3008 val &= ~hw_state->mg_pll_bias_mask;
3009 val |= hw_state->mg_pll_bias;
3010 I915_WRITE(MG_PLL_BIAS(tc_port), val);
3012 val = I915_READ(MG_PLL_TDC_COLDST_BIAS(tc_port));
3013 val &= ~hw_state->mg_pll_tdc_coldst_bias_mask;
3014 val |= hw_state->mg_pll_tdc_coldst_bias;
3015 I915_WRITE(MG_PLL_TDC_COLDST_BIAS(tc_port), val);
3017 POSTING_READ(MG_PLL_TDC_COLDST_BIAS(tc_port));
3020 static void icl_pll_power_enable(struct drm_i915_private *dev_priv,
3021 struct intel_shared_dpll *pll,
3022 i915_reg_t enable_reg)
3026 val = I915_READ(enable_reg);
3027 val |= PLL_POWER_ENABLE;
3028 I915_WRITE(enable_reg, val);
3031 * The spec says we need to "wait" but it also says it should be
3034 if (intel_wait_for_register(&dev_priv->uncore, enable_reg,
3035 PLL_POWER_STATE, PLL_POWER_STATE, 1))
3036 DRM_ERROR("PLL %d Power not enabled\n", pll->info->id);
3039 static void icl_pll_enable(struct drm_i915_private *dev_priv,
3040 struct intel_shared_dpll *pll,
3041 i915_reg_t enable_reg)
3045 val = I915_READ(enable_reg);
3047 I915_WRITE(enable_reg, val);
3049 /* Timeout is actually 600us. */
3050 if (intel_wait_for_register(&dev_priv->uncore, enable_reg,
3051 PLL_LOCK, PLL_LOCK, 1))
3052 DRM_ERROR("PLL %d not locked\n", pll->info->id);
3055 static void combo_pll_enable(struct drm_i915_private *dev_priv,
3056 struct intel_shared_dpll *pll)
3058 i915_reg_t enable_reg = CNL_DPLL_ENABLE(pll->info->id);
3060 icl_pll_power_enable(dev_priv, pll, enable_reg);
3062 icl_dpll_write(dev_priv, pll);
3065 * DVFS pre sequence would be here, but in our driver the cdclk code
3066 * paths should already be setting the appropriate voltage, hence we do
3070 icl_pll_enable(dev_priv, pll, enable_reg);
3072 /* DVFS post sequence would be here. See the comment above. */
3075 static void tbt_pll_enable(struct drm_i915_private *dev_priv,
3076 struct intel_shared_dpll *pll)
3078 icl_pll_power_enable(dev_priv, pll, TBT_PLL_ENABLE);
3080 icl_dpll_write(dev_priv, pll);
3083 * DVFS pre sequence would be here, but in our driver the cdclk code
3084 * paths should already be setting the appropriate voltage, hence we do
3088 icl_pll_enable(dev_priv, pll, TBT_PLL_ENABLE);
3090 /* DVFS post sequence would be here. See the comment above. */
3093 static void mg_pll_enable(struct drm_i915_private *dev_priv,
3094 struct intel_shared_dpll *pll)
3096 i915_reg_t enable_reg =
3097 MG_PLL_ENABLE(icl_pll_id_to_tc_port(pll->info->id));
3099 icl_pll_power_enable(dev_priv, pll, enable_reg);
3101 icl_mg_pll_write(dev_priv, pll);
3104 * DVFS pre sequence would be here, but in our driver the cdclk code
3105 * paths should already be setting the appropriate voltage, hence we do
3109 icl_pll_enable(dev_priv, pll, enable_reg);
3111 /* DVFS post sequence would be here. See the comment above. */
3114 static void icl_pll_disable(struct drm_i915_private *dev_priv,
3115 struct intel_shared_dpll *pll,
3116 i915_reg_t enable_reg)
3120 /* The first steps are done by intel_ddi_post_disable(). */
3123 * DVFS pre sequence would be here, but in our driver the cdclk code
3124 * paths should already be setting the appropriate voltage, hence we do
3128 val = I915_READ(enable_reg);
3130 I915_WRITE(enable_reg, val);
3132 /* Timeout is actually 1us. */
3133 if (intel_wait_for_register(&dev_priv->uncore,
3134 enable_reg, PLL_LOCK, 0, 1))
3135 DRM_ERROR("PLL %d locked\n", pll->info->id);
3137 /* DVFS post sequence would be here. See the comment above. */
3139 val = I915_READ(enable_reg);
3140 val &= ~PLL_POWER_ENABLE;
3141 I915_WRITE(enable_reg, val);
3144 * The spec says we need to "wait" but it also says it should be
3147 if (intel_wait_for_register(&dev_priv->uncore,
3148 enable_reg, PLL_POWER_STATE, 0, 1))
3149 DRM_ERROR("PLL %d Power not disabled\n", pll->info->id);
3152 static void combo_pll_disable(struct drm_i915_private *dev_priv,
3153 struct intel_shared_dpll *pll)
3155 icl_pll_disable(dev_priv, pll, CNL_DPLL_ENABLE(pll->info->id));
3158 static void tbt_pll_disable(struct drm_i915_private *dev_priv,
3159 struct intel_shared_dpll *pll)
3161 icl_pll_disable(dev_priv, pll, TBT_PLL_ENABLE);
3164 static void mg_pll_disable(struct drm_i915_private *dev_priv,
3165 struct intel_shared_dpll *pll)
3167 i915_reg_t enable_reg =
3168 MG_PLL_ENABLE(icl_pll_id_to_tc_port(pll->info->id));
3170 icl_pll_disable(dev_priv, pll, enable_reg);
3173 static void icl_dump_hw_state(struct drm_i915_private *dev_priv,
3174 const struct intel_dpll_hw_state *hw_state)
3176 DRM_DEBUG_KMS("dpll_hw_state: cfgcr0: 0x%x, cfgcr1: 0x%x, "
3177 "mg_refclkin_ctl: 0x%x, hg_clktop2_coreclkctl1: 0x%x, "
3178 "mg_clktop2_hsclkctl: 0x%x, mg_pll_div0: 0x%x, "
3179 "mg_pll_div2: 0x%x, mg_pll_lf: 0x%x, "
3180 "mg_pll_frac_lock: 0x%x, mg_pll_ssc: 0x%x, "
3181 "mg_pll_bias: 0x%x, mg_pll_tdc_coldst_bias: 0x%x\n",
3182 hw_state->cfgcr0, hw_state->cfgcr1,
3183 hw_state->mg_refclkin_ctl,
3184 hw_state->mg_clktop2_coreclkctl1,
3185 hw_state->mg_clktop2_hsclkctl,
3186 hw_state->mg_pll_div0,
3187 hw_state->mg_pll_div1,
3188 hw_state->mg_pll_lf,
3189 hw_state->mg_pll_frac_lock,
3190 hw_state->mg_pll_ssc,
3191 hw_state->mg_pll_bias,
3192 hw_state->mg_pll_tdc_coldst_bias);
3195 static const struct intel_shared_dpll_funcs combo_pll_funcs = {
3196 .enable = combo_pll_enable,
3197 .disable = combo_pll_disable,
3198 .get_hw_state = combo_pll_get_hw_state,
3201 static const struct intel_shared_dpll_funcs tbt_pll_funcs = {
3202 .enable = tbt_pll_enable,
3203 .disable = tbt_pll_disable,
3204 .get_hw_state = tbt_pll_get_hw_state,
3207 static const struct intel_shared_dpll_funcs mg_pll_funcs = {
3208 .enable = mg_pll_enable,
3209 .disable = mg_pll_disable,
3210 .get_hw_state = mg_pll_get_hw_state,
3213 static const struct dpll_info icl_plls[] = {
3214 { "DPLL 0", &combo_pll_funcs, DPLL_ID_ICL_DPLL0, 0 },
3215 { "DPLL 1", &combo_pll_funcs, DPLL_ID_ICL_DPLL1, 0 },
3216 { "TBT PLL", &tbt_pll_funcs, DPLL_ID_ICL_TBTPLL, 0 },
3217 { "MG PLL 1", &mg_pll_funcs, DPLL_ID_ICL_MGPLL1, 0 },
3218 { "MG PLL 2", &mg_pll_funcs, DPLL_ID_ICL_MGPLL2, 0 },
3219 { "MG PLL 3", &mg_pll_funcs, DPLL_ID_ICL_MGPLL3, 0 },
3220 { "MG PLL 4", &mg_pll_funcs, DPLL_ID_ICL_MGPLL4, 0 },
3224 static const struct intel_dpll_mgr icl_pll_mgr = {
3225 .dpll_info = icl_plls,
3226 .get_dpll = icl_get_dpll,
3227 .dump_hw_state = icl_dump_hw_state,
3230 static const struct dpll_info ehl_plls[] = {
3231 { "DPLL 0", &combo_pll_funcs, DPLL_ID_ICL_DPLL0, 0 },
3232 { "DPLL 1", &combo_pll_funcs, DPLL_ID_ICL_DPLL1, 0 },
3236 static const struct intel_dpll_mgr ehl_pll_mgr = {
3237 .dpll_info = ehl_plls,
3238 .get_dpll = icl_get_dpll,
3239 .dump_hw_state = icl_dump_hw_state,
3243 * intel_shared_dpll_init - Initialize shared DPLLs
3246 * Initialize shared DPLLs for @dev.
3248 void intel_shared_dpll_init(struct drm_device *dev)
3250 struct drm_i915_private *dev_priv = to_i915(dev);
3251 const struct intel_dpll_mgr *dpll_mgr = NULL;
3252 const struct dpll_info *dpll_info;
3255 if (IS_ELKHARTLAKE(dev_priv))
3256 dpll_mgr = &ehl_pll_mgr;
3257 else if (INTEL_GEN(dev_priv) >= 11)
3258 dpll_mgr = &icl_pll_mgr;
3259 else if (IS_CANNONLAKE(dev_priv))
3260 dpll_mgr = &cnl_pll_mgr;
3261 else if (IS_GEN9_BC(dev_priv))
3262 dpll_mgr = &skl_pll_mgr;
3263 else if (IS_GEN9_LP(dev_priv))
3264 dpll_mgr = &bxt_pll_mgr;
3265 else if (HAS_DDI(dev_priv))
3266 dpll_mgr = &hsw_pll_mgr;
3267 else if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
3268 dpll_mgr = &pch_pll_mgr;
3271 dev_priv->num_shared_dpll = 0;
3275 dpll_info = dpll_mgr->dpll_info;
3277 for (i = 0; dpll_info[i].name; i++) {
3278 WARN_ON(i != dpll_info[i].id);
3279 dev_priv->shared_dplls[i].info = &dpll_info[i];
3282 dev_priv->dpll_mgr = dpll_mgr;
3283 dev_priv->num_shared_dpll = i;
3284 mutex_init(&dev_priv->dpll_lock);
3286 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
3290 * intel_get_shared_dpll - get a shared DPLL for CRTC and encoder combination
3291 * @crtc_state: atomic state for the crtc
3294 * Find an appropriate DPLL for the given CRTC and encoder combination. A
3295 * reference from the @crtc_state to the returned pll is registered in the
3296 * atomic state. That configuration is made effective by calling
3297 * intel_shared_dpll_swap_state(). The reference should be released by calling
3298 * intel_release_shared_dpll().
3301 * A shared DPLL to be used by @crtc_state and @encoder.
3303 struct intel_shared_dpll *
3304 intel_get_shared_dpll(struct intel_crtc_state *crtc_state,
3305 struct intel_encoder *encoder)
3307 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
3308 const struct intel_dpll_mgr *dpll_mgr = dev_priv->dpll_mgr;
3310 if (WARN_ON(!dpll_mgr))
3313 return dpll_mgr->get_dpll(crtc_state, encoder);
3317 * intel_release_shared_dpll - end use of DPLL by CRTC in atomic state
3318 * @dpll: dpll in use by @crtc
3320 * @state: atomic state
3322 * This function releases the reference from @crtc to @dpll from the
3323 * atomic @state. The new configuration is made effective by calling
3324 * intel_shared_dpll_swap_state().
3326 void intel_release_shared_dpll(struct intel_shared_dpll *dpll,
3327 struct intel_crtc *crtc,
3328 struct drm_atomic_state *state)
3330 struct intel_shared_dpll_state *shared_dpll_state;
3332 shared_dpll_state = intel_atomic_get_shared_dpll_state(state);
3333 shared_dpll_state[dpll->info->id].crtc_mask &= ~(1 << crtc->pipe);
3337 * intel_shared_dpll_dump_hw_state - write hw_state to dmesg
3338 * @dev_priv: i915 drm device
3339 * @hw_state: hw state to be written to the log
3341 * Write the relevant values in @hw_state to dmesg using DRM_DEBUG_KMS.
3343 void intel_dpll_dump_hw_state(struct drm_i915_private *dev_priv,
3344 const struct intel_dpll_hw_state *hw_state)
3346 if (dev_priv->dpll_mgr) {
3347 dev_priv->dpll_mgr->dump_hw_state(dev_priv, hw_state);
3349 /* fallback for platforms that don't use the shared dpll
3352 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
3353 "fp0: 0x%x, fp1: 0x%x\n",