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_drv.h"
26 struct intel_shared_dpll *
27 skl_find_link_pll(struct drm_i915_private *dev_priv, int clock)
29 struct intel_shared_dpll *pll = NULL;
30 struct intel_dpll_hw_state dpll_hw_state;
34 if (!skl_ddi_dp_set_dpll_hw_state(clock, &dpll_hw_state))
37 for (i = DPLL_ID_SKL_DPLL1; i <= DPLL_ID_SKL_DPLL3; i++) {
38 pll = &dev_priv->shared_dplls[i];
40 /* Only want to check enabled timings first */
41 if (pll->config.crtc_mask == 0)
44 if (memcmp(&dpll_hw_state, &pll->config.hw_state,
45 sizeof(pll->config.hw_state)) == 0) {
51 /* Ok no matching timings, maybe there's a free one? */
52 for (i = DPLL_ID_SKL_DPLL1;
53 ((found == false) && (i <= DPLL_ID_SKL_DPLL3)); i++) {
54 pll = &dev_priv->shared_dplls[i];
55 if (pll->config.crtc_mask == 0) {
56 pll->config.hw_state = dpll_hw_state;
64 struct intel_shared_dpll *
65 intel_get_shared_dpll_by_id(struct drm_i915_private *dev_priv,
66 enum intel_dpll_id id)
68 return &dev_priv->shared_dplls[id];
72 intel_get_shared_dpll_id(struct drm_i915_private *dev_priv,
73 struct intel_shared_dpll *pll)
75 if (WARN_ON(pll < dev_priv->shared_dplls||
76 pll > &dev_priv->shared_dplls[dev_priv->num_shared_dpll]))
79 return (enum intel_dpll_id) (pll - dev_priv->shared_dplls);
83 intel_shared_dpll_config_get(struct intel_shared_dpll_config *config,
84 struct intel_shared_dpll *pll,
85 struct intel_crtc *crtc)
87 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
88 enum intel_dpll_id id = intel_get_shared_dpll_id(dev_priv, pll);
90 config[id].crtc_mask |= 1 << crtc->pipe;
94 intel_shared_dpll_config_put(struct intel_shared_dpll_config *config,
95 struct intel_shared_dpll *pll,
96 struct intel_crtc *crtc)
98 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
99 enum intel_dpll_id id = intel_get_shared_dpll_id(dev_priv, pll);
101 config[id].crtc_mask &= ~(1 << crtc->pipe);
105 void assert_shared_dpll(struct drm_i915_private *dev_priv,
106 struct intel_shared_dpll *pll,
110 struct intel_dpll_hw_state hw_state;
112 if (WARN(!pll, "asserting DPLL %s with no DPLL\n", onoff(state)))
115 cur_state = pll->funcs.get_hw_state(dev_priv, pll, &hw_state);
116 I915_STATE_WARN(cur_state != state,
117 "%s assertion failure (expected %s, current %s)\n",
118 pll->name, onoff(state), onoff(cur_state));
121 void intel_prepare_shared_dpll(struct intel_crtc *crtc)
123 struct drm_device *dev = crtc->base.dev;
124 struct drm_i915_private *dev_priv = to_i915(dev);
125 struct intel_shared_dpll *pll = crtc->config->shared_dpll;
127 if (WARN_ON(pll == NULL))
130 mutex_lock(&dev_priv->dpll_lock);
131 WARN_ON(!pll->config.crtc_mask);
132 if (!pll->active_mask) {
133 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
135 assert_shared_dpll_disabled(dev_priv, pll);
137 pll->funcs.mode_set(dev_priv, pll);
139 mutex_unlock(&dev_priv->dpll_lock);
143 * intel_enable_shared_dpll - enable PCH PLL
144 * @dev_priv: i915 private structure
145 * @pipe: pipe PLL to enable
147 * The PCH PLL needs to be enabled before the PCH transcoder, since it
148 * drives the transcoder clock.
150 void intel_enable_shared_dpll(struct intel_crtc *crtc)
152 struct drm_device *dev = crtc->base.dev;
153 struct drm_i915_private *dev_priv = to_i915(dev);
154 struct intel_shared_dpll *pll = crtc->config->shared_dpll;
155 unsigned crtc_mask = 1 << drm_crtc_index(&crtc->base);
158 if (WARN_ON(pll == NULL))
161 mutex_lock(&dev_priv->dpll_lock);
162 old_mask = pll->active_mask;
164 if (WARN_ON(!(pll->config.crtc_mask & crtc_mask)) ||
165 WARN_ON(pll->active_mask & crtc_mask))
168 pll->active_mask |= crtc_mask;
170 DRM_DEBUG_KMS("enable %s (active %x, on? %d) for crtc %d\n",
171 pll->name, pll->active_mask, pll->on,
176 assert_shared_dpll_enabled(dev_priv, pll);
181 DRM_DEBUG_KMS("enabling %s\n", pll->name);
182 pll->funcs.enable(dev_priv, pll);
186 mutex_unlock(&dev_priv->dpll_lock);
189 void intel_disable_shared_dpll(struct intel_crtc *crtc)
191 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
192 struct intel_shared_dpll *pll = crtc->config->shared_dpll;
193 unsigned crtc_mask = 1 << drm_crtc_index(&crtc->base);
195 /* PCH only available on ILK+ */
196 if (INTEL_GEN(dev_priv) < 5)
202 mutex_lock(&dev_priv->dpll_lock);
203 if (WARN_ON(!(pll->active_mask & crtc_mask)))
206 DRM_DEBUG_KMS("disable %s (active %x, on? %d) for crtc %d\n",
207 pll->name, pll->active_mask, pll->on,
210 assert_shared_dpll_enabled(dev_priv, pll);
213 pll->active_mask &= ~crtc_mask;
214 if (pll->active_mask)
217 DRM_DEBUG_KMS("disabling %s\n", pll->name);
218 pll->funcs.disable(dev_priv, pll);
222 mutex_unlock(&dev_priv->dpll_lock);
225 static struct intel_shared_dpll *
226 intel_find_shared_dpll(struct intel_crtc *crtc,
227 struct intel_crtc_state *crtc_state,
228 enum intel_dpll_id range_min,
229 enum intel_dpll_id range_max)
231 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
232 struct intel_shared_dpll *pll;
233 struct intel_shared_dpll_config *shared_dpll;
234 enum intel_dpll_id i;
236 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
238 for (i = range_min; i <= range_max; i++) {
239 pll = &dev_priv->shared_dplls[i];
241 /* Only want to check enabled timings first */
242 if (shared_dpll[i].crtc_mask == 0)
245 if (memcmp(&crtc_state->dpll_hw_state,
246 &shared_dpll[i].hw_state,
247 sizeof(crtc_state->dpll_hw_state)) == 0) {
248 DRM_DEBUG_KMS("[CRTC:%d:%s] sharing existing %s (crtc mask 0x%08x, active %x)\n",
249 crtc->base.base.id, crtc->base.name, pll->name,
250 shared_dpll[i].crtc_mask,
256 /* Ok no matching timings, maybe there's a free one? */
257 for (i = range_min; i <= range_max; i++) {
258 pll = &dev_priv->shared_dplls[i];
259 if (shared_dpll[i].crtc_mask == 0) {
260 DRM_DEBUG_KMS("[CRTC:%d:%s] allocated %s\n",
261 crtc->base.base.id, crtc->base.name, pll->name);
270 intel_reference_shared_dpll(struct intel_shared_dpll *pll,
271 struct intel_crtc_state *crtc_state)
273 struct intel_shared_dpll_config *shared_dpll;
274 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
275 enum intel_dpll_id i = pll->id;
277 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
279 if (shared_dpll[i].crtc_mask == 0)
280 shared_dpll[i].hw_state =
281 crtc_state->dpll_hw_state;
283 crtc_state->shared_dpll = pll;
284 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
285 pipe_name(crtc->pipe));
287 intel_shared_dpll_config_get(shared_dpll, pll, crtc);
290 void intel_shared_dpll_commit(struct drm_atomic_state *state)
292 struct drm_i915_private *dev_priv = to_i915(state->dev);
293 struct intel_shared_dpll_config *shared_dpll;
294 struct intel_shared_dpll *pll;
295 enum intel_dpll_id i;
297 if (!to_intel_atomic_state(state)->dpll_set)
300 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
301 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
302 pll = &dev_priv->shared_dplls[i];
303 pll->config = shared_dpll[i];
307 static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
308 struct intel_shared_dpll *pll,
309 struct intel_dpll_hw_state *hw_state)
313 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
316 val = I915_READ(PCH_DPLL(pll->id));
317 hw_state->dpll = val;
318 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
319 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
321 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
323 return val & DPLL_VCO_ENABLE;
326 static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
327 struct intel_shared_dpll *pll)
329 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
330 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
333 static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
338 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)));
340 val = I915_READ(PCH_DREF_CONTROL);
341 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
342 DREF_SUPERSPREAD_SOURCE_MASK));
343 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
346 static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
347 struct intel_shared_dpll *pll)
349 /* PCH refclock must be enabled first */
350 ibx_assert_pch_refclk_enabled(dev_priv);
352 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
354 /* Wait for the clocks to stabilize. */
355 POSTING_READ(PCH_DPLL(pll->id));
358 /* The pixel multiplier can only be updated once the
359 * DPLL is enabled and the clocks are stable.
363 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
364 POSTING_READ(PCH_DPLL(pll->id));
368 static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
369 struct intel_shared_dpll *pll)
371 struct drm_device *dev = &dev_priv->drm;
372 struct intel_crtc *crtc;
374 /* Make sure no transcoder isn't still depending on us. */
375 for_each_intel_crtc(dev, crtc) {
376 if (crtc->config->shared_dpll == pll)
377 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
380 I915_WRITE(PCH_DPLL(pll->id), 0);
381 POSTING_READ(PCH_DPLL(pll->id));
385 static struct intel_shared_dpll *
386 ibx_get_dpll(struct intel_crtc *crtc, struct intel_crtc_state *crtc_state,
387 struct intel_encoder *encoder)
389 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
390 struct intel_shared_dpll *pll;
391 enum intel_dpll_id i;
393 if (HAS_PCH_IBX(dev_priv)) {
394 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
395 i = (enum intel_dpll_id) crtc->pipe;
396 pll = &dev_priv->shared_dplls[i];
398 DRM_DEBUG_KMS("[CRTC:%d:%s] using pre-allocated %s\n",
399 crtc->base.base.id, crtc->base.name, pll->name);
401 pll = intel_find_shared_dpll(crtc, crtc_state,
409 /* reference the pll */
410 intel_reference_shared_dpll(pll, crtc_state);
415 static const struct intel_shared_dpll_funcs ibx_pch_dpll_funcs = {
416 .mode_set = ibx_pch_dpll_mode_set,
417 .enable = ibx_pch_dpll_enable,
418 .disable = ibx_pch_dpll_disable,
419 .get_hw_state = ibx_pch_dpll_get_hw_state,
422 static void hsw_ddi_wrpll_enable(struct drm_i915_private *dev_priv,
423 struct intel_shared_dpll *pll)
425 I915_WRITE(WRPLL_CTL(pll->id), pll->config.hw_state.wrpll);
426 POSTING_READ(WRPLL_CTL(pll->id));
430 static void hsw_ddi_spll_enable(struct drm_i915_private *dev_priv,
431 struct intel_shared_dpll *pll)
433 I915_WRITE(SPLL_CTL, pll->config.hw_state.spll);
434 POSTING_READ(SPLL_CTL);
438 static void hsw_ddi_wrpll_disable(struct drm_i915_private *dev_priv,
439 struct intel_shared_dpll *pll)
443 val = I915_READ(WRPLL_CTL(pll->id));
444 I915_WRITE(WRPLL_CTL(pll->id), val & ~WRPLL_PLL_ENABLE);
445 POSTING_READ(WRPLL_CTL(pll->id));
448 static void hsw_ddi_spll_disable(struct drm_i915_private *dev_priv,
449 struct intel_shared_dpll *pll)
453 val = I915_READ(SPLL_CTL);
454 I915_WRITE(SPLL_CTL, val & ~SPLL_PLL_ENABLE);
455 POSTING_READ(SPLL_CTL);
458 static bool hsw_ddi_wrpll_get_hw_state(struct drm_i915_private *dev_priv,
459 struct intel_shared_dpll *pll,
460 struct intel_dpll_hw_state *hw_state)
464 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
467 val = I915_READ(WRPLL_CTL(pll->id));
468 hw_state->wrpll = val;
470 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
472 return val & WRPLL_PLL_ENABLE;
475 static bool hsw_ddi_spll_get_hw_state(struct drm_i915_private *dev_priv,
476 struct intel_shared_dpll *pll,
477 struct intel_dpll_hw_state *hw_state)
481 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
484 val = I915_READ(SPLL_CTL);
485 hw_state->spll = val;
487 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
489 return val & SPLL_PLL_ENABLE;
493 #define LC_FREQ_2K U64_C(LC_FREQ * 2000)
499 /* Constraints for PLL good behavior */
505 struct hsw_wrpll_rnp {
509 static unsigned hsw_wrpll_get_budget_for_freq(int clock)
583 static void hsw_wrpll_update_rnp(uint64_t freq2k, unsigned budget,
584 unsigned r2, unsigned n2, unsigned p,
585 struct hsw_wrpll_rnp *best)
587 uint64_t a, b, c, d, diff, diff_best;
589 /* No best (r,n,p) yet */
598 * Output clock is (LC_FREQ_2K / 2000) * N / (P * R), which compares to
602 * abs(freq2k - (LC_FREQ_2K * n2/(p * r2))) /
605 * and we would like delta <= budget.
607 * If the discrepancy is above the PPM-based budget, always prefer to
608 * improve upon the previous solution. However, if you're within the
609 * budget, try to maximize Ref * VCO, that is N / (P * R^2).
611 a = freq2k * budget * p * r2;
612 b = freq2k * budget * best->p * best->r2;
613 diff = abs_diff(freq2k * p * r2, LC_FREQ_2K * n2);
614 diff_best = abs_diff(freq2k * best->p * best->r2,
615 LC_FREQ_2K * best->n2);
617 d = 1000000 * diff_best;
619 if (a < c && b < d) {
620 /* If both are above the budget, pick the closer */
621 if (best->p * best->r2 * diff < p * r2 * diff_best) {
626 } else if (a >= c && b < d) {
627 /* If A is below the threshold but B is above it? Update. */
631 } else if (a >= c && b >= d) {
632 /* Both are below the limit, so pick the higher n2/(r2*r2) */
633 if (n2 * best->r2 * best->r2 > best->n2 * r2 * r2) {
639 /* Otherwise a < c && b >= d, do nothing */
643 hsw_ddi_calculate_wrpll(int clock /* in Hz */,
644 unsigned *r2_out, unsigned *n2_out, unsigned *p_out)
648 struct hsw_wrpll_rnp best = { 0, 0, 0 };
651 freq2k = clock / 100;
653 budget = hsw_wrpll_get_budget_for_freq(clock);
655 /* Special case handling for 540 pixel clock: bypass WR PLL entirely
656 * and directly pass the LC PLL to it. */
657 if (freq2k == 5400000) {
665 * Ref = LC_FREQ / R, where Ref is the actual reference input seen by
668 * We want R so that REF_MIN <= Ref <= REF_MAX.
669 * Injecting R2 = 2 * R gives:
670 * REF_MAX * r2 > LC_FREQ * 2 and
671 * REF_MIN * r2 < LC_FREQ * 2
673 * Which means the desired boundaries for r2 are:
674 * LC_FREQ * 2 / REF_MAX < r2 < LC_FREQ * 2 / REF_MIN
677 for (r2 = LC_FREQ * 2 / REF_MAX + 1;
678 r2 <= LC_FREQ * 2 / REF_MIN;
682 * VCO = N * Ref, that is: VCO = N * LC_FREQ / R
684 * Once again we want VCO_MIN <= VCO <= VCO_MAX.
685 * Injecting R2 = 2 * R and N2 = 2 * N, we get:
686 * VCO_MAX * r2 > n2 * LC_FREQ and
687 * VCO_MIN * r2 < n2 * LC_FREQ)
689 * Which means the desired boundaries for n2 are:
690 * VCO_MIN * r2 / LC_FREQ < n2 < VCO_MAX * r2 / LC_FREQ
692 for (n2 = VCO_MIN * r2 / LC_FREQ + 1;
693 n2 <= VCO_MAX * r2 / LC_FREQ;
696 for (p = P_MIN; p <= P_MAX; p += P_INC)
697 hsw_wrpll_update_rnp(freq2k, budget,
707 static struct intel_shared_dpll *hsw_ddi_hdmi_get_dpll(int clock,
708 struct intel_crtc *crtc,
709 struct intel_crtc_state *crtc_state)
711 struct intel_shared_dpll *pll;
713 unsigned int p, n2, r2;
715 hsw_ddi_calculate_wrpll(clock * 1000, &r2, &n2, &p);
717 val = WRPLL_PLL_ENABLE | WRPLL_PLL_LCPLL |
718 WRPLL_DIVIDER_REFERENCE(r2) | WRPLL_DIVIDER_FEEDBACK(n2) |
719 WRPLL_DIVIDER_POST(p);
721 crtc_state->dpll_hw_state.wrpll = val;
723 pll = intel_find_shared_dpll(crtc, crtc_state,
724 DPLL_ID_WRPLL1, DPLL_ID_WRPLL2);
732 struct intel_shared_dpll *hsw_ddi_dp_get_dpll(struct intel_encoder *encoder,
735 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
736 struct intel_shared_dpll *pll;
737 enum intel_dpll_id pll_id;
741 pll_id = DPLL_ID_LCPLL_810;
744 pll_id = DPLL_ID_LCPLL_1350;
747 pll_id = DPLL_ID_LCPLL_2700;
750 DRM_DEBUG_KMS("Invalid clock for DP: %d\n", clock);
754 pll = intel_get_shared_dpll_by_id(dev_priv, pll_id);
762 static struct intel_shared_dpll *
763 hsw_get_dpll(struct intel_crtc *crtc, struct intel_crtc_state *crtc_state,
764 struct intel_encoder *encoder)
766 struct intel_shared_dpll *pll;
767 int clock = crtc_state->port_clock;
769 memset(&crtc_state->dpll_hw_state, 0,
770 sizeof(crtc_state->dpll_hw_state));
772 if (encoder->type == INTEL_OUTPUT_HDMI) {
773 pll = hsw_ddi_hdmi_get_dpll(clock, crtc, crtc_state);
775 } else if (encoder->type == INTEL_OUTPUT_DP ||
776 encoder->type == INTEL_OUTPUT_DP_MST ||
777 encoder->type == INTEL_OUTPUT_EDP) {
778 pll = hsw_ddi_dp_get_dpll(encoder, clock);
780 } else if (encoder->type == INTEL_OUTPUT_ANALOG) {
781 if (WARN_ON(crtc_state->port_clock / 2 != 135000))
784 crtc_state->dpll_hw_state.spll =
785 SPLL_PLL_ENABLE | SPLL_PLL_FREQ_1350MHz | SPLL_PLL_SSC;
787 pll = intel_find_shared_dpll(crtc, crtc_state,
788 DPLL_ID_SPLL, DPLL_ID_SPLL);
796 intel_reference_shared_dpll(pll, crtc_state);
801 static const struct intel_shared_dpll_funcs hsw_ddi_wrpll_funcs = {
802 .enable = hsw_ddi_wrpll_enable,
803 .disable = hsw_ddi_wrpll_disable,
804 .get_hw_state = hsw_ddi_wrpll_get_hw_state,
807 static const struct intel_shared_dpll_funcs hsw_ddi_spll_funcs = {
808 .enable = hsw_ddi_spll_enable,
809 .disable = hsw_ddi_spll_disable,
810 .get_hw_state = hsw_ddi_spll_get_hw_state,
813 static void hsw_ddi_lcpll_enable(struct drm_i915_private *dev_priv,
814 struct intel_shared_dpll *pll)
818 static void hsw_ddi_lcpll_disable(struct drm_i915_private *dev_priv,
819 struct intel_shared_dpll *pll)
823 static bool hsw_ddi_lcpll_get_hw_state(struct drm_i915_private *dev_priv,
824 struct intel_shared_dpll *pll,
825 struct intel_dpll_hw_state *hw_state)
830 static const struct intel_shared_dpll_funcs hsw_ddi_lcpll_funcs = {
831 .enable = hsw_ddi_lcpll_enable,
832 .disable = hsw_ddi_lcpll_disable,
833 .get_hw_state = hsw_ddi_lcpll_get_hw_state,
836 struct skl_dpll_regs {
837 i915_reg_t ctl, cfgcr1, cfgcr2;
840 /* this array is indexed by the *shared* pll id */
841 static const struct skl_dpll_regs skl_dpll_regs[4] = {
845 /* DPLL 0 doesn't support HDMI mode */
850 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL1),
851 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL1),
856 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL2),
857 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL2),
862 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL3),
863 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL3),
867 static void skl_ddi_pll_write_ctrl1(struct drm_i915_private *dev_priv,
868 struct intel_shared_dpll *pll)
872 val = I915_READ(DPLL_CTRL1);
874 val &= ~(DPLL_CTRL1_HDMI_MODE(pll->id) | DPLL_CTRL1_SSC(pll->id) |
875 DPLL_CTRL1_LINK_RATE_MASK(pll->id));
876 val |= pll->config.hw_state.ctrl1 << (pll->id * 6);
878 I915_WRITE(DPLL_CTRL1, val);
879 POSTING_READ(DPLL_CTRL1);
882 static void skl_ddi_pll_enable(struct drm_i915_private *dev_priv,
883 struct intel_shared_dpll *pll)
885 const struct skl_dpll_regs *regs = skl_dpll_regs;
887 skl_ddi_pll_write_ctrl1(dev_priv, pll);
889 I915_WRITE(regs[pll->id].cfgcr1, pll->config.hw_state.cfgcr1);
890 I915_WRITE(regs[pll->id].cfgcr2, pll->config.hw_state.cfgcr2);
891 POSTING_READ(regs[pll->id].cfgcr1);
892 POSTING_READ(regs[pll->id].cfgcr2);
894 /* the enable bit is always bit 31 */
895 I915_WRITE(regs[pll->id].ctl,
896 I915_READ(regs[pll->id].ctl) | LCPLL_PLL_ENABLE);
898 if (intel_wait_for_register(dev_priv,
903 DRM_ERROR("DPLL %d not locked\n", pll->id);
906 static void skl_ddi_dpll0_enable(struct drm_i915_private *dev_priv,
907 struct intel_shared_dpll *pll)
909 skl_ddi_pll_write_ctrl1(dev_priv, pll);
912 static void skl_ddi_pll_disable(struct drm_i915_private *dev_priv,
913 struct intel_shared_dpll *pll)
915 const struct skl_dpll_regs *regs = skl_dpll_regs;
917 /* the enable bit is always bit 31 */
918 I915_WRITE(regs[pll->id].ctl,
919 I915_READ(regs[pll->id].ctl) & ~LCPLL_PLL_ENABLE);
920 POSTING_READ(regs[pll->id].ctl);
923 static void skl_ddi_dpll0_disable(struct drm_i915_private *dev_priv,
924 struct intel_shared_dpll *pll)
928 static bool skl_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
929 struct intel_shared_dpll *pll,
930 struct intel_dpll_hw_state *hw_state)
933 const struct skl_dpll_regs *regs = skl_dpll_regs;
936 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
941 val = I915_READ(regs[pll->id].ctl);
942 if (!(val & LCPLL_PLL_ENABLE))
945 val = I915_READ(DPLL_CTRL1);
946 hw_state->ctrl1 = (val >> (pll->id * 6)) & 0x3f;
948 /* avoid reading back stale values if HDMI mode is not enabled */
949 if (val & DPLL_CTRL1_HDMI_MODE(pll->id)) {
950 hw_state->cfgcr1 = I915_READ(regs[pll->id].cfgcr1);
951 hw_state->cfgcr2 = I915_READ(regs[pll->id].cfgcr2);
956 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
961 static bool skl_ddi_dpll0_get_hw_state(struct drm_i915_private *dev_priv,
962 struct intel_shared_dpll *pll,
963 struct intel_dpll_hw_state *hw_state)
966 const struct skl_dpll_regs *regs = skl_dpll_regs;
969 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
974 /* DPLL0 is always enabled since it drives CDCLK */
975 val = I915_READ(regs[pll->id].ctl);
976 if (WARN_ON(!(val & LCPLL_PLL_ENABLE)))
979 val = I915_READ(DPLL_CTRL1);
980 hw_state->ctrl1 = (val >> (pll->id * 6)) & 0x3f;
985 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
990 struct skl_wrpll_context {
991 uint64_t min_deviation; /* current minimal deviation */
992 uint64_t central_freq; /* chosen central freq */
993 uint64_t dco_freq; /* chosen dco freq */
994 unsigned int p; /* chosen divider */
997 static void skl_wrpll_context_init(struct skl_wrpll_context *ctx)
999 memset(ctx, 0, sizeof(*ctx));
1001 ctx->min_deviation = U64_MAX;
1004 /* DCO freq must be within +1%/-6% of the DCO central freq */
1005 #define SKL_DCO_MAX_PDEVIATION 100
1006 #define SKL_DCO_MAX_NDEVIATION 600
1008 static void skl_wrpll_try_divider(struct skl_wrpll_context *ctx,
1009 uint64_t central_freq,
1011 unsigned int divider)
1015 deviation = div64_u64(10000 * abs_diff(dco_freq, central_freq),
1018 /* positive deviation */
1019 if (dco_freq >= central_freq) {
1020 if (deviation < SKL_DCO_MAX_PDEVIATION &&
1021 deviation < ctx->min_deviation) {
1022 ctx->min_deviation = deviation;
1023 ctx->central_freq = central_freq;
1024 ctx->dco_freq = dco_freq;
1027 /* negative deviation */
1028 } else if (deviation < SKL_DCO_MAX_NDEVIATION &&
1029 deviation < ctx->min_deviation) {
1030 ctx->min_deviation = deviation;
1031 ctx->central_freq = central_freq;
1032 ctx->dco_freq = dco_freq;
1037 static void skl_wrpll_get_multipliers(unsigned int p,
1038 unsigned int *p0 /* out */,
1039 unsigned int *p1 /* out */,
1040 unsigned int *p2 /* out */)
1044 unsigned int half = p / 2;
1046 if (half == 1 || half == 2 || half == 3 || half == 5) {
1050 } else if (half % 2 == 0) {
1054 } else if (half % 3 == 0) {
1058 } else if (half % 7 == 0) {
1063 } else if (p == 3 || p == 9) { /* 3, 5, 7, 9, 15, 21, 35 */
1067 } else if (p == 5 || p == 7) {
1071 } else if (p == 15) {
1075 } else if (p == 21) {
1079 } else if (p == 35) {
1086 struct skl_wrpll_params {
1087 uint32_t dco_fraction;
1088 uint32_t dco_integer;
1089 uint32_t qdiv_ratio;
1093 uint32_t central_freq;
1096 static void skl_wrpll_params_populate(struct skl_wrpll_params *params,
1098 uint64_t central_freq,
1099 uint32_t p0, uint32_t p1, uint32_t p2)
1103 switch (central_freq) {
1105 params->central_freq = 0;
1108 params->central_freq = 1;
1111 params->central_freq = 3;
1128 WARN(1, "Incorrect PDiv\n");
1145 WARN(1, "Incorrect KDiv\n");
1148 params->qdiv_ratio = p1;
1149 params->qdiv_mode = (params->qdiv_ratio == 1) ? 0 : 1;
1151 dco_freq = p0 * p1 * p2 * afe_clock;
1154 * Intermediate values are in Hz.
1155 * Divide by MHz to match bsepc
1157 params->dco_integer = div_u64(dco_freq, 24 * MHz(1));
1158 params->dco_fraction =
1159 div_u64((div_u64(dco_freq, 24) -
1160 params->dco_integer * MHz(1)) * 0x8000, MHz(1));
1164 skl_ddi_calculate_wrpll(int clock /* in Hz */,
1165 struct skl_wrpll_params *wrpll_params)
1167 uint64_t afe_clock = clock * 5; /* AFE Clock is 5x Pixel clock */
1168 uint64_t dco_central_freq[3] = {8400000000ULL,
1171 static const int even_dividers[] = { 4, 6, 8, 10, 12, 14, 16, 18, 20,
1172 24, 28, 30, 32, 36, 40, 42, 44,
1173 48, 52, 54, 56, 60, 64, 66, 68,
1174 70, 72, 76, 78, 80, 84, 88, 90,
1176 static const int odd_dividers[] = { 3, 5, 7, 9, 15, 21, 35 };
1177 static const struct {
1181 { even_dividers, ARRAY_SIZE(even_dividers) },
1182 { odd_dividers, ARRAY_SIZE(odd_dividers) },
1184 struct skl_wrpll_context ctx;
1185 unsigned int dco, d, i;
1186 unsigned int p0, p1, p2;
1188 skl_wrpll_context_init(&ctx);
1190 for (d = 0; d < ARRAY_SIZE(dividers); d++) {
1191 for (dco = 0; dco < ARRAY_SIZE(dco_central_freq); dco++) {
1192 for (i = 0; i < dividers[d].n_dividers; i++) {
1193 unsigned int p = dividers[d].list[i];
1194 uint64_t dco_freq = p * afe_clock;
1196 skl_wrpll_try_divider(&ctx,
1197 dco_central_freq[dco],
1201 * Skip the remaining dividers if we're sure to
1202 * have found the definitive divider, we can't
1203 * improve a 0 deviation.
1205 if (ctx.min_deviation == 0)
1206 goto skip_remaining_dividers;
1210 skip_remaining_dividers:
1212 * If a solution is found with an even divider, prefer
1215 if (d == 0 && ctx.p)
1220 DRM_DEBUG_DRIVER("No valid divider found for %dHz\n", clock);
1225 * gcc incorrectly analyses that these can be used without being
1226 * initialized. To be fair, it's hard to guess.
1229 skl_wrpll_get_multipliers(ctx.p, &p0, &p1, &p2);
1230 skl_wrpll_params_populate(wrpll_params, afe_clock, ctx.central_freq,
1236 static bool skl_ddi_hdmi_pll_dividers(struct intel_crtc *crtc,
1237 struct intel_crtc_state *crtc_state,
1240 uint32_t ctrl1, cfgcr1, cfgcr2;
1241 struct skl_wrpll_params wrpll_params = { 0, };
1244 * See comment in intel_dpll_hw_state to understand why we always use 0
1245 * as the DPLL id in this function.
1247 ctrl1 = DPLL_CTRL1_OVERRIDE(0);
1249 ctrl1 |= DPLL_CTRL1_HDMI_MODE(0);
1251 if (!skl_ddi_calculate_wrpll(clock * 1000, &wrpll_params))
1254 cfgcr1 = DPLL_CFGCR1_FREQ_ENABLE |
1255 DPLL_CFGCR1_DCO_FRACTION(wrpll_params.dco_fraction) |
1256 wrpll_params.dco_integer;
1258 cfgcr2 = DPLL_CFGCR2_QDIV_RATIO(wrpll_params.qdiv_ratio) |
1259 DPLL_CFGCR2_QDIV_MODE(wrpll_params.qdiv_mode) |
1260 DPLL_CFGCR2_KDIV(wrpll_params.kdiv) |
1261 DPLL_CFGCR2_PDIV(wrpll_params.pdiv) |
1262 wrpll_params.central_freq;
1264 memset(&crtc_state->dpll_hw_state, 0,
1265 sizeof(crtc_state->dpll_hw_state));
1267 crtc_state->dpll_hw_state.ctrl1 = ctrl1;
1268 crtc_state->dpll_hw_state.cfgcr1 = cfgcr1;
1269 crtc_state->dpll_hw_state.cfgcr2 = cfgcr2;
1274 bool skl_ddi_dp_set_dpll_hw_state(int clock,
1275 struct intel_dpll_hw_state *dpll_hw_state)
1280 * See comment in intel_dpll_hw_state to understand why we always use 0
1281 * as the DPLL id in this function.
1283 ctrl1 = DPLL_CTRL1_OVERRIDE(0);
1284 switch (clock / 2) {
1286 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, 0);
1289 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, 0);
1292 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, 0);
1296 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, 0);
1299 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, 0);
1302 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, 0);
1306 dpll_hw_state->ctrl1 = ctrl1;
1310 static struct intel_shared_dpll *
1311 skl_get_dpll(struct intel_crtc *crtc, struct intel_crtc_state *crtc_state,
1312 struct intel_encoder *encoder)
1314 struct intel_shared_dpll *pll;
1315 int clock = crtc_state->port_clock;
1317 struct intel_dpll_hw_state dpll_hw_state;
1319 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
1321 if (encoder->type == INTEL_OUTPUT_HDMI) {
1322 bret = skl_ddi_hdmi_pll_dividers(crtc, crtc_state, clock);
1324 DRM_DEBUG_KMS("Could not get HDMI pll dividers.\n");
1327 } else if (encoder->type == INTEL_OUTPUT_DP ||
1328 encoder->type == INTEL_OUTPUT_DP_MST ||
1329 encoder->type == INTEL_OUTPUT_EDP) {
1330 bret = skl_ddi_dp_set_dpll_hw_state(clock, &dpll_hw_state);
1332 DRM_DEBUG_KMS("Could not set DP dpll HW state.\n");
1335 crtc_state->dpll_hw_state = dpll_hw_state;
1340 if (encoder->type == INTEL_OUTPUT_EDP)
1341 pll = intel_find_shared_dpll(crtc, crtc_state,
1345 pll = intel_find_shared_dpll(crtc, crtc_state,
1351 intel_reference_shared_dpll(pll, crtc_state);
1356 static const struct intel_shared_dpll_funcs skl_ddi_pll_funcs = {
1357 .enable = skl_ddi_pll_enable,
1358 .disable = skl_ddi_pll_disable,
1359 .get_hw_state = skl_ddi_pll_get_hw_state,
1362 static const struct intel_shared_dpll_funcs skl_ddi_dpll0_funcs = {
1363 .enable = skl_ddi_dpll0_enable,
1364 .disable = skl_ddi_dpll0_disable,
1365 .get_hw_state = skl_ddi_dpll0_get_hw_state,
1368 static void bxt_ddi_pll_enable(struct drm_i915_private *dev_priv,
1369 struct intel_shared_dpll *pll)
1372 enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
1374 enum dpio_channel ch;
1376 bxt_port_to_phy_channel(port, &phy, &ch);
1378 /* Non-SSC reference */
1379 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1380 temp |= PORT_PLL_REF_SEL;
1381 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1383 /* Disable 10 bit clock */
1384 temp = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
1385 temp &= ~PORT_PLL_10BIT_CLK_ENABLE;
1386 I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
1389 temp = I915_READ(BXT_PORT_PLL_EBB_0(phy, ch));
1390 temp &= ~(PORT_PLL_P1_MASK | PORT_PLL_P2_MASK);
1391 temp |= pll->config.hw_state.ebb0;
1392 I915_WRITE(BXT_PORT_PLL_EBB_0(phy, ch), temp);
1394 /* Write M2 integer */
1395 temp = I915_READ(BXT_PORT_PLL(phy, ch, 0));
1396 temp &= ~PORT_PLL_M2_MASK;
1397 temp |= pll->config.hw_state.pll0;
1398 I915_WRITE(BXT_PORT_PLL(phy, ch, 0), temp);
1401 temp = I915_READ(BXT_PORT_PLL(phy, ch, 1));
1402 temp &= ~PORT_PLL_N_MASK;
1403 temp |= pll->config.hw_state.pll1;
1404 I915_WRITE(BXT_PORT_PLL(phy, ch, 1), temp);
1406 /* Write M2 fraction */
1407 temp = I915_READ(BXT_PORT_PLL(phy, ch, 2));
1408 temp &= ~PORT_PLL_M2_FRAC_MASK;
1409 temp |= pll->config.hw_state.pll2;
1410 I915_WRITE(BXT_PORT_PLL(phy, ch, 2), temp);
1412 /* Write M2 fraction enable */
1413 temp = I915_READ(BXT_PORT_PLL(phy, ch, 3));
1414 temp &= ~PORT_PLL_M2_FRAC_ENABLE;
1415 temp |= pll->config.hw_state.pll3;
1416 I915_WRITE(BXT_PORT_PLL(phy, ch, 3), temp);
1419 temp = I915_READ(BXT_PORT_PLL(phy, ch, 6));
1420 temp &= ~PORT_PLL_PROP_COEFF_MASK;
1421 temp &= ~PORT_PLL_INT_COEFF_MASK;
1422 temp &= ~PORT_PLL_GAIN_CTL_MASK;
1423 temp |= pll->config.hw_state.pll6;
1424 I915_WRITE(BXT_PORT_PLL(phy, ch, 6), temp);
1426 /* Write calibration val */
1427 temp = I915_READ(BXT_PORT_PLL(phy, ch, 8));
1428 temp &= ~PORT_PLL_TARGET_CNT_MASK;
1429 temp |= pll->config.hw_state.pll8;
1430 I915_WRITE(BXT_PORT_PLL(phy, ch, 8), temp);
1432 temp = I915_READ(BXT_PORT_PLL(phy, ch, 9));
1433 temp &= ~PORT_PLL_LOCK_THRESHOLD_MASK;
1434 temp |= pll->config.hw_state.pll9;
1435 I915_WRITE(BXT_PORT_PLL(phy, ch, 9), temp);
1437 temp = I915_READ(BXT_PORT_PLL(phy, ch, 10));
1438 temp &= ~PORT_PLL_DCO_AMP_OVR_EN_H;
1439 temp &= ~PORT_PLL_DCO_AMP_MASK;
1440 temp |= pll->config.hw_state.pll10;
1441 I915_WRITE(BXT_PORT_PLL(phy, ch, 10), temp);
1443 /* Recalibrate with new settings */
1444 temp = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
1445 temp |= PORT_PLL_RECALIBRATE;
1446 I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
1447 temp &= ~PORT_PLL_10BIT_CLK_ENABLE;
1448 temp |= pll->config.hw_state.ebb4;
1449 I915_WRITE(BXT_PORT_PLL_EBB_4(phy, ch), temp);
1452 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1453 temp |= PORT_PLL_ENABLE;
1454 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1455 POSTING_READ(BXT_PORT_PLL_ENABLE(port));
1457 if (wait_for_us((I915_READ(BXT_PORT_PLL_ENABLE(port)) & PORT_PLL_LOCK),
1459 DRM_ERROR("PLL %d not locked\n", port);
1462 * While we write to the group register to program all lanes at once we
1463 * can read only lane registers and we pick lanes 0/1 for that.
1465 temp = I915_READ(BXT_PORT_PCS_DW12_LN01(phy, ch));
1466 temp &= ~LANE_STAGGER_MASK;
1467 temp &= ~LANESTAGGER_STRAP_OVRD;
1468 temp |= pll->config.hw_state.pcsdw12;
1469 I915_WRITE(BXT_PORT_PCS_DW12_GRP(phy, ch), temp);
1472 static void bxt_ddi_pll_disable(struct drm_i915_private *dev_priv,
1473 struct intel_shared_dpll *pll)
1475 enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
1478 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1479 temp &= ~PORT_PLL_ENABLE;
1480 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1481 POSTING_READ(BXT_PORT_PLL_ENABLE(port));
1484 static bool bxt_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
1485 struct intel_shared_dpll *pll,
1486 struct intel_dpll_hw_state *hw_state)
1488 enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
1492 enum dpio_channel ch;
1494 bxt_port_to_phy_channel(port, &phy, &ch);
1496 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
1501 val = I915_READ(BXT_PORT_PLL_ENABLE(port));
1502 if (!(val & PORT_PLL_ENABLE))
1505 hw_state->ebb0 = I915_READ(BXT_PORT_PLL_EBB_0(phy, ch));
1506 hw_state->ebb0 &= PORT_PLL_P1_MASK | PORT_PLL_P2_MASK;
1508 hw_state->ebb4 = I915_READ(BXT_PORT_PLL_EBB_4(phy, ch));
1509 hw_state->ebb4 &= PORT_PLL_10BIT_CLK_ENABLE;
1511 hw_state->pll0 = I915_READ(BXT_PORT_PLL(phy, ch, 0));
1512 hw_state->pll0 &= PORT_PLL_M2_MASK;
1514 hw_state->pll1 = I915_READ(BXT_PORT_PLL(phy, ch, 1));
1515 hw_state->pll1 &= PORT_PLL_N_MASK;
1517 hw_state->pll2 = I915_READ(BXT_PORT_PLL(phy, ch, 2));
1518 hw_state->pll2 &= PORT_PLL_M2_FRAC_MASK;
1520 hw_state->pll3 = I915_READ(BXT_PORT_PLL(phy, ch, 3));
1521 hw_state->pll3 &= PORT_PLL_M2_FRAC_ENABLE;
1523 hw_state->pll6 = I915_READ(BXT_PORT_PLL(phy, ch, 6));
1524 hw_state->pll6 &= PORT_PLL_PROP_COEFF_MASK |
1525 PORT_PLL_INT_COEFF_MASK |
1526 PORT_PLL_GAIN_CTL_MASK;
1528 hw_state->pll8 = I915_READ(BXT_PORT_PLL(phy, ch, 8));
1529 hw_state->pll8 &= PORT_PLL_TARGET_CNT_MASK;
1531 hw_state->pll9 = I915_READ(BXT_PORT_PLL(phy, ch, 9));
1532 hw_state->pll9 &= PORT_PLL_LOCK_THRESHOLD_MASK;
1534 hw_state->pll10 = I915_READ(BXT_PORT_PLL(phy, ch, 10));
1535 hw_state->pll10 &= PORT_PLL_DCO_AMP_OVR_EN_H |
1536 PORT_PLL_DCO_AMP_MASK;
1539 * While we write to the group register to program all lanes at once we
1540 * can read only lane registers. We configure all lanes the same way, so
1541 * here just read out lanes 0/1 and output a note if lanes 2/3 differ.
1543 hw_state->pcsdw12 = I915_READ(BXT_PORT_PCS_DW12_LN01(phy, ch));
1544 if (I915_READ(BXT_PORT_PCS_DW12_LN23(phy, ch)) != hw_state->pcsdw12)
1545 DRM_DEBUG_DRIVER("lane stagger config different for lane 01 (%08x) and 23 (%08x)\n",
1547 I915_READ(BXT_PORT_PCS_DW12_LN23(phy, ch)));
1548 hw_state->pcsdw12 &= LANE_STAGGER_MASK | LANESTAGGER_STRAP_OVRD;
1553 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
1558 /* bxt clock parameters */
1559 struct bxt_clk_div {
1571 /* pre-calculated values for DP linkrates */
1572 static const struct bxt_clk_div bxt_dp_clk_val[] = {
1573 {162000, 4, 2, 32, 1677722, 1, 1},
1574 {270000, 4, 1, 27, 0, 0, 1},
1575 {540000, 2, 1, 27, 0, 0, 1},
1576 {216000, 3, 2, 32, 1677722, 1, 1},
1577 {243000, 4, 1, 24, 1258291, 1, 1},
1578 {324000, 4, 1, 32, 1677722, 1, 1},
1579 {432000, 3, 1, 32, 1677722, 1, 1}
1583 bxt_ddi_hdmi_pll_dividers(struct intel_crtc *intel_crtc,
1584 struct intel_crtc_state *crtc_state, int clock,
1585 struct bxt_clk_div *clk_div)
1587 struct dpll best_clock;
1589 /* Calculate HDMI div */
1591 * FIXME: tie the following calculation into
1592 * i9xx_crtc_compute_clock
1594 if (!bxt_find_best_dpll(crtc_state, clock, &best_clock)) {
1595 DRM_DEBUG_DRIVER("no PLL dividers found for clock %d pipe %c\n",
1596 clock, pipe_name(intel_crtc->pipe));
1600 clk_div->p1 = best_clock.p1;
1601 clk_div->p2 = best_clock.p2;
1602 WARN_ON(best_clock.m1 != 2);
1603 clk_div->n = best_clock.n;
1604 clk_div->m2_int = best_clock.m2 >> 22;
1605 clk_div->m2_frac = best_clock.m2 & ((1 << 22) - 1);
1606 clk_div->m2_frac_en = clk_div->m2_frac != 0;
1608 clk_div->vco = best_clock.vco;
1613 static void bxt_ddi_dp_pll_dividers(int clock, struct bxt_clk_div *clk_div)
1617 *clk_div = bxt_dp_clk_val[0];
1618 for (i = 0; i < ARRAY_SIZE(bxt_dp_clk_val); ++i) {
1619 if (bxt_dp_clk_val[i].clock == clock) {
1620 *clk_div = bxt_dp_clk_val[i];
1625 clk_div->vco = clock * 10 / 2 * clk_div->p1 * clk_div->p2;
1628 static bool bxt_ddi_set_dpll_hw_state(int clock,
1629 struct bxt_clk_div *clk_div,
1630 struct intel_dpll_hw_state *dpll_hw_state)
1632 int vco = clk_div->vco;
1633 uint32_t prop_coef, int_coef, gain_ctl, targ_cnt;
1634 uint32_t lanestagger;
1636 if (vco >= 6200000 && vco <= 6700000) {
1641 } else if ((vco > 5400000 && vco < 6200000) ||
1642 (vco >= 4800000 && vco < 5400000)) {
1647 } else if (vco == 5400000) {
1653 DRM_ERROR("Invalid VCO\n");
1659 else if (clock > 135000)
1661 else if (clock > 67000)
1663 else if (clock > 33000)
1668 dpll_hw_state->ebb0 = PORT_PLL_P1(clk_div->p1) | PORT_PLL_P2(clk_div->p2);
1669 dpll_hw_state->pll0 = clk_div->m2_int;
1670 dpll_hw_state->pll1 = PORT_PLL_N(clk_div->n);
1671 dpll_hw_state->pll2 = clk_div->m2_frac;
1673 if (clk_div->m2_frac_en)
1674 dpll_hw_state->pll3 = PORT_PLL_M2_FRAC_ENABLE;
1676 dpll_hw_state->pll6 = prop_coef | PORT_PLL_INT_COEFF(int_coef);
1677 dpll_hw_state->pll6 |= PORT_PLL_GAIN_CTL(gain_ctl);
1679 dpll_hw_state->pll8 = targ_cnt;
1681 dpll_hw_state->pll9 = 5 << PORT_PLL_LOCK_THRESHOLD_SHIFT;
1683 dpll_hw_state->pll10 =
1684 PORT_PLL_DCO_AMP(PORT_PLL_DCO_AMP_DEFAULT)
1685 | PORT_PLL_DCO_AMP_OVR_EN_H;
1687 dpll_hw_state->ebb4 = PORT_PLL_10BIT_CLK_ENABLE;
1689 dpll_hw_state->pcsdw12 = LANESTAGGER_STRAP_OVRD | lanestagger;
1694 bool bxt_ddi_dp_set_dpll_hw_state(int clock,
1695 struct intel_dpll_hw_state *dpll_hw_state)
1697 struct bxt_clk_div clk_div = {0};
1699 bxt_ddi_dp_pll_dividers(clock, &clk_div);
1701 return bxt_ddi_set_dpll_hw_state(clock, &clk_div, dpll_hw_state);
1705 bxt_ddi_hdmi_set_dpll_hw_state(struct intel_crtc *intel_crtc,
1706 struct intel_crtc_state *crtc_state, int clock,
1707 struct intel_dpll_hw_state *dpll_hw_state)
1709 struct bxt_clk_div clk_div = { };
1711 bxt_ddi_hdmi_pll_dividers(intel_crtc, crtc_state, clock, &clk_div);
1713 return bxt_ddi_set_dpll_hw_state(clock, &clk_div, dpll_hw_state);
1716 static struct intel_shared_dpll *
1717 bxt_get_dpll(struct intel_crtc *crtc,
1718 struct intel_crtc_state *crtc_state,
1719 struct intel_encoder *encoder)
1721 struct intel_dpll_hw_state dpll_hw_state = { };
1722 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1723 struct intel_digital_port *intel_dig_port;
1724 struct intel_shared_dpll *pll;
1725 int i, clock = crtc_state->port_clock;
1727 if (encoder->type == INTEL_OUTPUT_HDMI &&
1728 !bxt_ddi_hdmi_set_dpll_hw_state(crtc, crtc_state, clock,
1732 if ((encoder->type == INTEL_OUTPUT_DP ||
1733 encoder->type == INTEL_OUTPUT_EDP ||
1734 encoder->type == INTEL_OUTPUT_DP_MST) &&
1735 !bxt_ddi_dp_set_dpll_hw_state(clock, &dpll_hw_state))
1738 memset(&crtc_state->dpll_hw_state, 0,
1739 sizeof(crtc_state->dpll_hw_state));
1741 crtc_state->dpll_hw_state = dpll_hw_state;
1743 if (encoder->type == INTEL_OUTPUT_DP_MST) {
1744 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
1746 intel_dig_port = intel_mst->primary;
1748 intel_dig_port = enc_to_dig_port(&encoder->base);
1750 /* 1:1 mapping between ports and PLLs */
1751 i = (enum intel_dpll_id) intel_dig_port->port;
1752 pll = intel_get_shared_dpll_by_id(dev_priv, i);
1754 DRM_DEBUG_KMS("[CRTC:%d:%s] using pre-allocated %s\n",
1755 crtc->base.base.id, crtc->base.name, pll->name);
1757 intel_reference_shared_dpll(pll, crtc_state);
1762 static const struct intel_shared_dpll_funcs bxt_ddi_pll_funcs = {
1763 .enable = bxt_ddi_pll_enable,
1764 .disable = bxt_ddi_pll_disable,
1765 .get_hw_state = bxt_ddi_pll_get_hw_state,
1768 static void intel_ddi_pll_init(struct drm_device *dev)
1770 struct drm_i915_private *dev_priv = to_i915(dev);
1772 if (INTEL_GEN(dev_priv) < 9) {
1773 uint32_t val = I915_READ(LCPLL_CTL);
1776 * The LCPLL register should be turned on by the BIOS. For now
1777 * let's just check its state and print errors in case
1778 * something is wrong. Don't even try to turn it on.
1781 if (val & LCPLL_CD_SOURCE_FCLK)
1782 DRM_ERROR("CDCLK source is not LCPLL\n");
1784 if (val & LCPLL_PLL_DISABLE)
1785 DRM_ERROR("LCPLL is disabled\n");
1792 const struct intel_shared_dpll_funcs *funcs;
1796 struct intel_dpll_mgr {
1797 const struct dpll_info *dpll_info;
1799 struct intel_shared_dpll *(*get_dpll)(struct intel_crtc *crtc,
1800 struct intel_crtc_state *crtc_state,
1801 struct intel_encoder *encoder);
1804 static const struct dpll_info pch_plls[] = {
1805 { "PCH DPLL A", DPLL_ID_PCH_PLL_A, &ibx_pch_dpll_funcs, 0 },
1806 { "PCH DPLL B", DPLL_ID_PCH_PLL_B, &ibx_pch_dpll_funcs, 0 },
1807 { NULL, -1, NULL, 0 },
1810 static const struct intel_dpll_mgr pch_pll_mgr = {
1811 .dpll_info = pch_plls,
1812 .get_dpll = ibx_get_dpll,
1815 static const struct dpll_info hsw_plls[] = {
1816 { "WRPLL 1", DPLL_ID_WRPLL1, &hsw_ddi_wrpll_funcs, 0 },
1817 { "WRPLL 2", DPLL_ID_WRPLL2, &hsw_ddi_wrpll_funcs, 0 },
1818 { "SPLL", DPLL_ID_SPLL, &hsw_ddi_spll_funcs, 0 },
1819 { "LCPLL 810", DPLL_ID_LCPLL_810, &hsw_ddi_lcpll_funcs, INTEL_DPLL_ALWAYS_ON },
1820 { "LCPLL 1350", DPLL_ID_LCPLL_1350, &hsw_ddi_lcpll_funcs, INTEL_DPLL_ALWAYS_ON },
1821 { "LCPLL 2700", DPLL_ID_LCPLL_2700, &hsw_ddi_lcpll_funcs, INTEL_DPLL_ALWAYS_ON },
1822 { NULL, -1, NULL, },
1825 static const struct intel_dpll_mgr hsw_pll_mgr = {
1826 .dpll_info = hsw_plls,
1827 .get_dpll = hsw_get_dpll,
1830 static const struct dpll_info skl_plls[] = {
1831 { "DPLL 0", DPLL_ID_SKL_DPLL0, &skl_ddi_dpll0_funcs, INTEL_DPLL_ALWAYS_ON },
1832 { "DPLL 1", DPLL_ID_SKL_DPLL1, &skl_ddi_pll_funcs, 0 },
1833 { "DPLL 2", DPLL_ID_SKL_DPLL2, &skl_ddi_pll_funcs, 0 },
1834 { "DPLL 3", DPLL_ID_SKL_DPLL3, &skl_ddi_pll_funcs, 0 },
1835 { NULL, -1, NULL, },
1838 static const struct intel_dpll_mgr skl_pll_mgr = {
1839 .dpll_info = skl_plls,
1840 .get_dpll = skl_get_dpll,
1843 static const struct dpll_info bxt_plls[] = {
1844 { "PORT PLL A", DPLL_ID_SKL_DPLL0, &bxt_ddi_pll_funcs, 0 },
1845 { "PORT PLL B", DPLL_ID_SKL_DPLL1, &bxt_ddi_pll_funcs, 0 },
1846 { "PORT PLL C", DPLL_ID_SKL_DPLL2, &bxt_ddi_pll_funcs, 0 },
1847 { NULL, -1, NULL, },
1850 static const struct intel_dpll_mgr bxt_pll_mgr = {
1851 .dpll_info = bxt_plls,
1852 .get_dpll = bxt_get_dpll,
1855 void intel_shared_dpll_init(struct drm_device *dev)
1857 struct drm_i915_private *dev_priv = to_i915(dev);
1858 const struct intel_dpll_mgr *dpll_mgr = NULL;
1859 const struct dpll_info *dpll_info;
1862 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
1863 dpll_mgr = &skl_pll_mgr;
1864 else if (IS_BROXTON(dev_priv))
1865 dpll_mgr = &bxt_pll_mgr;
1866 else if (HAS_DDI(dev_priv))
1867 dpll_mgr = &hsw_pll_mgr;
1868 else if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
1869 dpll_mgr = &pch_pll_mgr;
1872 dev_priv->num_shared_dpll = 0;
1876 dpll_info = dpll_mgr->dpll_info;
1878 for (i = 0; dpll_info[i].id >= 0; i++) {
1879 WARN_ON(i != dpll_info[i].id);
1881 dev_priv->shared_dplls[i].id = dpll_info[i].id;
1882 dev_priv->shared_dplls[i].name = dpll_info[i].name;
1883 dev_priv->shared_dplls[i].funcs = *dpll_info[i].funcs;
1884 dev_priv->shared_dplls[i].flags = dpll_info[i].flags;
1887 dev_priv->dpll_mgr = dpll_mgr;
1888 dev_priv->num_shared_dpll = i;
1889 mutex_init(&dev_priv->dpll_lock);
1891 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
1893 /* FIXME: Move this to a more suitable place */
1894 if (HAS_DDI(dev_priv))
1895 intel_ddi_pll_init(dev);
1898 struct intel_shared_dpll *
1899 intel_get_shared_dpll(struct intel_crtc *crtc,
1900 struct intel_crtc_state *crtc_state,
1901 struct intel_encoder *encoder)
1903 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1904 const struct intel_dpll_mgr *dpll_mgr = dev_priv->dpll_mgr;
1906 if (WARN_ON(!dpll_mgr))
1909 return dpll_mgr->get_dpll(crtc, crtc_state, encoder);