Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: MIT
0002 /*
0003  * Copyright © 2020 Intel Corporation
0004  */
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/string_helpers.h>
0008 
0009 #include "intel_crtc.h"
0010 #include "intel_de.h"
0011 #include "intel_display.h"
0012 #include "intel_display_types.h"
0013 #include "intel_dpll.h"
0014 #include "intel_lvds.h"
0015 #include "intel_panel.h"
0016 #include "intel_pps.h"
0017 #include "intel_snps_phy.h"
0018 #include "vlv_sideband.h"
0019 
0020 struct intel_dpll_funcs {
0021     int (*crtc_compute_clock)(struct intel_atomic_state *state,
0022                   struct intel_crtc *crtc);
0023     int (*crtc_get_shared_dpll)(struct intel_atomic_state *state,
0024                     struct intel_crtc *crtc);
0025 };
0026 
0027 struct intel_limit {
0028     struct {
0029         int min, max;
0030     } dot, vco, n, m, m1, m2, p, p1;
0031 
0032     struct {
0033         int dot_limit;
0034         int p2_slow, p2_fast;
0035     } p2;
0036 };
0037 static const struct intel_limit intel_limits_i8xx_dac = {
0038     .dot = { .min = 25000, .max = 350000 },
0039     .vco = { .min = 908000, .max = 1512000 },
0040     .n = { .min = 2, .max = 16 },
0041     .m = { .min = 96, .max = 140 },
0042     .m1 = { .min = 18, .max = 26 },
0043     .m2 = { .min = 6, .max = 16 },
0044     .p = { .min = 4, .max = 128 },
0045     .p1 = { .min = 2, .max = 33 },
0046     .p2 = { .dot_limit = 165000,
0047         .p2_slow = 4, .p2_fast = 2 },
0048 };
0049 
0050 static const struct intel_limit intel_limits_i8xx_dvo = {
0051     .dot = { .min = 25000, .max = 350000 },
0052     .vco = { .min = 908000, .max = 1512000 },
0053     .n = { .min = 2, .max = 16 },
0054     .m = { .min = 96, .max = 140 },
0055     .m1 = { .min = 18, .max = 26 },
0056     .m2 = { .min = 6, .max = 16 },
0057     .p = { .min = 4, .max = 128 },
0058     .p1 = { .min = 2, .max = 33 },
0059     .p2 = { .dot_limit = 165000,
0060         .p2_slow = 4, .p2_fast = 4 },
0061 };
0062 
0063 static const struct intel_limit intel_limits_i8xx_lvds = {
0064     .dot = { .min = 25000, .max = 350000 },
0065     .vco = { .min = 908000, .max = 1512000 },
0066     .n = { .min = 2, .max = 16 },
0067     .m = { .min = 96, .max = 140 },
0068     .m1 = { .min = 18, .max = 26 },
0069     .m2 = { .min = 6, .max = 16 },
0070     .p = { .min = 4, .max = 128 },
0071     .p1 = { .min = 1, .max = 6 },
0072     .p2 = { .dot_limit = 165000,
0073         .p2_slow = 14, .p2_fast = 7 },
0074 };
0075 
0076 static const struct intel_limit intel_limits_i9xx_sdvo = {
0077     .dot = { .min = 20000, .max = 400000 },
0078     .vco = { .min = 1400000, .max = 2800000 },
0079     .n = { .min = 1, .max = 6 },
0080     .m = { .min = 70, .max = 120 },
0081     .m1 = { .min = 8, .max = 18 },
0082     .m2 = { .min = 3, .max = 7 },
0083     .p = { .min = 5, .max = 80 },
0084     .p1 = { .min = 1, .max = 8 },
0085     .p2 = { .dot_limit = 200000,
0086         .p2_slow = 10, .p2_fast = 5 },
0087 };
0088 
0089 static const struct intel_limit intel_limits_i9xx_lvds = {
0090     .dot = { .min = 20000, .max = 400000 },
0091     .vco = { .min = 1400000, .max = 2800000 },
0092     .n = { .min = 1, .max = 6 },
0093     .m = { .min = 70, .max = 120 },
0094     .m1 = { .min = 8, .max = 18 },
0095     .m2 = { .min = 3, .max = 7 },
0096     .p = { .min = 7, .max = 98 },
0097     .p1 = { .min = 1, .max = 8 },
0098     .p2 = { .dot_limit = 112000,
0099         .p2_slow = 14, .p2_fast = 7 },
0100 };
0101 
0102 
0103 static const struct intel_limit intel_limits_g4x_sdvo = {
0104     .dot = { .min = 25000, .max = 270000 },
0105     .vco = { .min = 1750000, .max = 3500000},
0106     .n = { .min = 1, .max = 4 },
0107     .m = { .min = 104, .max = 138 },
0108     .m1 = { .min = 17, .max = 23 },
0109     .m2 = { .min = 5, .max = 11 },
0110     .p = { .min = 10, .max = 30 },
0111     .p1 = { .min = 1, .max = 3},
0112     .p2 = { .dot_limit = 270000,
0113         .p2_slow = 10,
0114         .p2_fast = 10
0115     },
0116 };
0117 
0118 static const struct intel_limit intel_limits_g4x_hdmi = {
0119     .dot = { .min = 22000, .max = 400000 },
0120     .vco = { .min = 1750000, .max = 3500000},
0121     .n = { .min = 1, .max = 4 },
0122     .m = { .min = 104, .max = 138 },
0123     .m1 = { .min = 16, .max = 23 },
0124     .m2 = { .min = 5, .max = 11 },
0125     .p = { .min = 5, .max = 80 },
0126     .p1 = { .min = 1, .max = 8},
0127     .p2 = { .dot_limit = 165000,
0128         .p2_slow = 10, .p2_fast = 5 },
0129 };
0130 
0131 static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
0132     .dot = { .min = 20000, .max = 115000 },
0133     .vco = { .min = 1750000, .max = 3500000 },
0134     .n = { .min = 1, .max = 3 },
0135     .m = { .min = 104, .max = 138 },
0136     .m1 = { .min = 17, .max = 23 },
0137     .m2 = { .min = 5, .max = 11 },
0138     .p = { .min = 28, .max = 112 },
0139     .p1 = { .min = 2, .max = 8 },
0140     .p2 = { .dot_limit = 0,
0141         .p2_slow = 14, .p2_fast = 14
0142     },
0143 };
0144 
0145 static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
0146     .dot = { .min = 80000, .max = 224000 },
0147     .vco = { .min = 1750000, .max = 3500000 },
0148     .n = { .min = 1, .max = 3 },
0149     .m = { .min = 104, .max = 138 },
0150     .m1 = { .min = 17, .max = 23 },
0151     .m2 = { .min = 5, .max = 11 },
0152     .p = { .min = 14, .max = 42 },
0153     .p1 = { .min = 2, .max = 6 },
0154     .p2 = { .dot_limit = 0,
0155         .p2_slow = 7, .p2_fast = 7
0156     },
0157 };
0158 
0159 static const struct intel_limit pnv_limits_sdvo = {
0160     .dot = { .min = 20000, .max = 400000},
0161     .vco = { .min = 1700000, .max = 3500000 },
0162     /* Pineview's Ncounter is a ring counter */
0163     .n = { .min = 3, .max = 6 },
0164     .m = { .min = 2, .max = 256 },
0165     /* Pineview only has one combined m divider, which we treat as m2. */
0166     .m1 = { .min = 0, .max = 0 },
0167     .m2 = { .min = 0, .max = 254 },
0168     .p = { .min = 5, .max = 80 },
0169     .p1 = { .min = 1, .max = 8 },
0170     .p2 = { .dot_limit = 200000,
0171         .p2_slow = 10, .p2_fast = 5 },
0172 };
0173 
0174 static const struct intel_limit pnv_limits_lvds = {
0175     .dot = { .min = 20000, .max = 400000 },
0176     .vco = { .min = 1700000, .max = 3500000 },
0177     .n = { .min = 3, .max = 6 },
0178     .m = { .min = 2, .max = 256 },
0179     .m1 = { .min = 0, .max = 0 },
0180     .m2 = { .min = 0, .max = 254 },
0181     .p = { .min = 7, .max = 112 },
0182     .p1 = { .min = 1, .max = 8 },
0183     .p2 = { .dot_limit = 112000,
0184         .p2_slow = 14, .p2_fast = 14 },
0185 };
0186 
0187 /* Ironlake / Sandybridge
0188  *
0189  * We calculate clock using (register_value + 2) for N/M1/M2, so here
0190  * the range value for them is (actual_value - 2).
0191  */
0192 static const struct intel_limit ilk_limits_dac = {
0193     .dot = { .min = 25000, .max = 350000 },
0194     .vco = { .min = 1760000, .max = 3510000 },
0195     .n = { .min = 1, .max = 5 },
0196     .m = { .min = 79, .max = 127 },
0197     .m1 = { .min = 12, .max = 22 },
0198     .m2 = { .min = 5, .max = 9 },
0199     .p = { .min = 5, .max = 80 },
0200     .p1 = { .min = 1, .max = 8 },
0201     .p2 = { .dot_limit = 225000,
0202         .p2_slow = 10, .p2_fast = 5 },
0203 };
0204 
0205 static const struct intel_limit ilk_limits_single_lvds = {
0206     .dot = { .min = 25000, .max = 350000 },
0207     .vco = { .min = 1760000, .max = 3510000 },
0208     .n = { .min = 1, .max = 3 },
0209     .m = { .min = 79, .max = 118 },
0210     .m1 = { .min = 12, .max = 22 },
0211     .m2 = { .min = 5, .max = 9 },
0212     .p = { .min = 28, .max = 112 },
0213     .p1 = { .min = 2, .max = 8 },
0214     .p2 = { .dot_limit = 225000,
0215         .p2_slow = 14, .p2_fast = 14 },
0216 };
0217 
0218 static const struct intel_limit ilk_limits_dual_lvds = {
0219     .dot = { .min = 25000, .max = 350000 },
0220     .vco = { .min = 1760000, .max = 3510000 },
0221     .n = { .min = 1, .max = 3 },
0222     .m = { .min = 79, .max = 127 },
0223     .m1 = { .min = 12, .max = 22 },
0224     .m2 = { .min = 5, .max = 9 },
0225     .p = { .min = 14, .max = 56 },
0226     .p1 = { .min = 2, .max = 8 },
0227     .p2 = { .dot_limit = 225000,
0228         .p2_slow = 7, .p2_fast = 7 },
0229 };
0230 
0231 /* LVDS 100mhz refclk limits. */
0232 static const struct intel_limit ilk_limits_single_lvds_100m = {
0233     .dot = { .min = 25000, .max = 350000 },
0234     .vco = { .min = 1760000, .max = 3510000 },
0235     .n = { .min = 1, .max = 2 },
0236     .m = { .min = 79, .max = 126 },
0237     .m1 = { .min = 12, .max = 22 },
0238     .m2 = { .min = 5, .max = 9 },
0239     .p = { .min = 28, .max = 112 },
0240     .p1 = { .min = 2, .max = 8 },
0241     .p2 = { .dot_limit = 225000,
0242         .p2_slow = 14, .p2_fast = 14 },
0243 };
0244 
0245 static const struct intel_limit ilk_limits_dual_lvds_100m = {
0246     .dot = { .min = 25000, .max = 350000 },
0247     .vco = { .min = 1760000, .max = 3510000 },
0248     .n = { .min = 1, .max = 3 },
0249     .m = { .min = 79, .max = 126 },
0250     .m1 = { .min = 12, .max = 22 },
0251     .m2 = { .min = 5, .max = 9 },
0252     .p = { .min = 14, .max = 42 },
0253     .p1 = { .min = 2, .max = 6 },
0254     .p2 = { .dot_limit = 225000,
0255         .p2_slow = 7, .p2_fast = 7 },
0256 };
0257 
0258 static const struct intel_limit intel_limits_vlv = {
0259      /*
0260       * These are based on the data rate limits (measured in fast clocks)
0261       * since those are the strictest limits we have. The fast
0262       * clock and actual rate limits are more relaxed, so checking
0263       * them would make no difference.
0264       */
0265     .dot = { .min = 25000, .max = 270000 },
0266     .vco = { .min = 4000000, .max = 6000000 },
0267     .n = { .min = 1, .max = 7 },
0268     .m1 = { .min = 2, .max = 3 },
0269     .m2 = { .min = 11, .max = 156 },
0270     .p1 = { .min = 2, .max = 3 },
0271     .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
0272 };
0273 
0274 static const struct intel_limit intel_limits_chv = {
0275     /*
0276      * These are based on the data rate limits (measured in fast clocks)
0277      * since those are the strictest limits we have.  The fast
0278      * clock and actual rate limits are more relaxed, so checking
0279      * them would make no difference.
0280      */
0281     .dot = { .min = 25000, .max = 540000 },
0282     .vco = { .min = 4800000, .max = 6480000 },
0283     .n = { .min = 1, .max = 1 },
0284     .m1 = { .min = 2, .max = 2 },
0285     .m2 = { .min = 24 << 22, .max = 175 << 22 },
0286     .p1 = { .min = 2, .max = 4 },
0287     .p2 = { .p2_slow = 1, .p2_fast = 14 },
0288 };
0289 
0290 static const struct intel_limit intel_limits_bxt = {
0291     .dot = { .min = 25000, .max = 594000 },
0292     .vco = { .min = 4800000, .max = 6700000 },
0293     .n = { .min = 1, .max = 1 },
0294     .m1 = { .min = 2, .max = 2 },
0295     /* FIXME: find real m2 limits */
0296     .m2 = { .min = 2 << 22, .max = 255 << 22 },
0297     .p1 = { .min = 2, .max = 4 },
0298     .p2 = { .p2_slow = 1, .p2_fast = 20 },
0299 };
0300 
0301 /*
0302  * Platform specific helpers to calculate the port PLL loopback- (clock.m),
0303  * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
0304  * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
0305  * The helpers' return value is the rate of the clock that is fed to the
0306  * display engine's pipe which can be the above fast dot clock rate or a
0307  * divided-down version of it.
0308  */
0309 /* m1 is reserved as 0 in Pineview, n is a ring counter */
0310 int pnv_calc_dpll_params(int refclk, struct dpll *clock)
0311 {
0312     clock->m = clock->m2 + 2;
0313     clock->p = clock->p1 * clock->p2;
0314     if (WARN_ON(clock->n == 0 || clock->p == 0))
0315         return 0;
0316     clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
0317     clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
0318 
0319     return clock->dot;
0320 }
0321 
0322 static u32 i9xx_dpll_compute_m(const struct dpll *dpll)
0323 {
0324     return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
0325 }
0326 
0327 int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
0328 {
0329     clock->m = i9xx_dpll_compute_m(clock);
0330     clock->p = clock->p1 * clock->p2;
0331     if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
0332         return 0;
0333     clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
0334     clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
0335 
0336     return clock->dot;
0337 }
0338 
0339 int vlv_calc_dpll_params(int refclk, struct dpll *clock)
0340 {
0341     clock->m = clock->m1 * clock->m2;
0342     clock->p = clock->p1 * clock->p2 * 5;
0343     if (WARN_ON(clock->n == 0 || clock->p == 0))
0344         return 0;
0345     clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
0346     clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
0347 
0348     return clock->dot;
0349 }
0350 
0351 int chv_calc_dpll_params(int refclk, struct dpll *clock)
0352 {
0353     clock->m = clock->m1 * clock->m2;
0354     clock->p = clock->p1 * clock->p2 * 5;
0355     if (WARN_ON(clock->n == 0 || clock->p == 0))
0356         return 0;
0357     clock->vco = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(refclk, clock->m),
0358                        clock->n << 22);
0359     clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
0360 
0361     return clock->dot;
0362 }
0363 
0364 /*
0365  * Returns whether the given set of divisors are valid for a given refclk with
0366  * the given connectors.
0367  */
0368 static bool intel_pll_is_valid(struct drm_i915_private *dev_priv,
0369                    const struct intel_limit *limit,
0370                    const struct dpll *clock)
0371 {
0372     if (clock->n < limit->n.min || limit->n.max < clock->n)
0373         return false;
0374     if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0375         return false;
0376     if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0377         return false;
0378     if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0379         return false;
0380 
0381     if (!IS_PINEVIEW(dev_priv) && !IS_LP(dev_priv))
0382         if (clock->m1 <= clock->m2)
0383             return false;
0384 
0385     if (!IS_LP(dev_priv)) {
0386         if (clock->p < limit->p.min || limit->p.max < clock->p)
0387             return false;
0388         if (clock->m < limit->m.min || limit->m.max < clock->m)
0389             return false;
0390     }
0391 
0392     if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0393         return false;
0394     /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
0395      * connector, etc., rather than just a single range.
0396      */
0397     if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0398         return false;
0399 
0400     return true;
0401 }
0402 
0403 static int
0404 i9xx_select_p2_div(const struct intel_limit *limit,
0405            const struct intel_crtc_state *crtc_state,
0406            int target)
0407 {
0408     struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
0409 
0410     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
0411         /*
0412          * For LVDS just rely on its current settings for dual-channel.
0413          * We haven't figured out how to reliably set up different
0414          * single/dual channel state, if we even can.
0415          */
0416         if (intel_is_dual_link_lvds(dev_priv))
0417             return limit->p2.p2_fast;
0418         else
0419             return limit->p2.p2_slow;
0420     } else {
0421         if (target < limit->p2.dot_limit)
0422             return limit->p2.p2_slow;
0423         else
0424             return limit->p2.p2_fast;
0425     }
0426 }
0427 
0428 /*
0429  * Returns a set of divisors for the desired target clock with the given
0430  * refclk, or FALSE.
0431  *
0432  * Target and reference clocks are specified in kHz.
0433  *
0434  * If match_clock is provided, then best_clock P divider must match the P
0435  * divider from @match_clock used for LVDS downclocking.
0436  */
0437 static bool
0438 i9xx_find_best_dpll(const struct intel_limit *limit,
0439             struct intel_crtc_state *crtc_state,
0440             int target, int refclk,
0441             const struct dpll *match_clock,
0442             struct dpll *best_clock)
0443 {
0444     struct drm_device *dev = crtc_state->uapi.crtc->dev;
0445     struct dpll clock;
0446     int err = target;
0447 
0448     memset(best_clock, 0, sizeof(*best_clock));
0449 
0450     clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
0451 
0452     for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
0453          clock.m1++) {
0454         for (clock.m2 = limit->m2.min;
0455              clock.m2 <= limit->m2.max; clock.m2++) {
0456             if (clock.m2 >= clock.m1)
0457                 break;
0458             for (clock.n = limit->n.min;
0459                  clock.n <= limit->n.max; clock.n++) {
0460                 for (clock.p1 = limit->p1.min;
0461                     clock.p1 <= limit->p1.max; clock.p1++) {
0462                     int this_err;
0463 
0464                     i9xx_calc_dpll_params(refclk, &clock);
0465                     if (!intel_pll_is_valid(to_i915(dev),
0466                                 limit,
0467                                 &clock))
0468                         continue;
0469                     if (match_clock &&
0470                         clock.p != match_clock->p)
0471                         continue;
0472 
0473                     this_err = abs(clock.dot - target);
0474                     if (this_err < err) {
0475                         *best_clock = clock;
0476                         err = this_err;
0477                     }
0478                 }
0479             }
0480         }
0481     }
0482 
0483     return (err != target);
0484 }
0485 
0486 /*
0487  * Returns a set of divisors for the desired target clock with the given
0488  * refclk, or FALSE.
0489  *
0490  * Target and reference clocks are specified in kHz.
0491  *
0492  * If match_clock is provided, then best_clock P divider must match the P
0493  * divider from @match_clock used for LVDS downclocking.
0494  */
0495 static bool
0496 pnv_find_best_dpll(const struct intel_limit *limit,
0497            struct intel_crtc_state *crtc_state,
0498            int target, int refclk,
0499            const struct dpll *match_clock,
0500            struct dpll *best_clock)
0501 {
0502     struct drm_device *dev = crtc_state->uapi.crtc->dev;
0503     struct dpll clock;
0504     int err = target;
0505 
0506     memset(best_clock, 0, sizeof(*best_clock));
0507 
0508     clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
0509 
0510     for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
0511          clock.m1++) {
0512         for (clock.m2 = limit->m2.min;
0513              clock.m2 <= limit->m2.max; clock.m2++) {
0514             for (clock.n = limit->n.min;
0515                  clock.n <= limit->n.max; clock.n++) {
0516                 for (clock.p1 = limit->p1.min;
0517                     clock.p1 <= limit->p1.max; clock.p1++) {
0518                     int this_err;
0519 
0520                     pnv_calc_dpll_params(refclk, &clock);
0521                     if (!intel_pll_is_valid(to_i915(dev),
0522                                 limit,
0523                                 &clock))
0524                         continue;
0525                     if (match_clock &&
0526                         clock.p != match_clock->p)
0527                         continue;
0528 
0529                     this_err = abs(clock.dot - target);
0530                     if (this_err < err) {
0531                         *best_clock = clock;
0532                         err = this_err;
0533                     }
0534                 }
0535             }
0536         }
0537     }
0538 
0539     return (err != target);
0540 }
0541 
0542 /*
0543  * Returns a set of divisors for the desired target clock with the given
0544  * refclk, or FALSE.
0545  *
0546  * Target and reference clocks are specified in kHz.
0547  *
0548  * If match_clock is provided, then best_clock P divider must match the P
0549  * divider from @match_clock used for LVDS downclocking.
0550  */
0551 static bool
0552 g4x_find_best_dpll(const struct intel_limit *limit,
0553            struct intel_crtc_state *crtc_state,
0554            int target, int refclk,
0555            const struct dpll *match_clock,
0556            struct dpll *best_clock)
0557 {
0558     struct drm_device *dev = crtc_state->uapi.crtc->dev;
0559     struct dpll clock;
0560     int max_n;
0561     bool found = false;
0562     /* approximately equals target * 0.00585 */
0563     int err_most = (target >> 8) + (target >> 9);
0564 
0565     memset(best_clock, 0, sizeof(*best_clock));
0566 
0567     clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
0568 
0569     max_n = limit->n.max;
0570     /* based on hardware requirement, prefer smaller n to precision */
0571     for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
0572         /* based on hardware requirement, prefere larger m1,m2 */
0573         for (clock.m1 = limit->m1.max;
0574              clock.m1 >= limit->m1.min; clock.m1--) {
0575             for (clock.m2 = limit->m2.max;
0576                  clock.m2 >= limit->m2.min; clock.m2--) {
0577                 for (clock.p1 = limit->p1.max;
0578                      clock.p1 >= limit->p1.min; clock.p1--) {
0579                     int this_err;
0580 
0581                     i9xx_calc_dpll_params(refclk, &clock);
0582                     if (!intel_pll_is_valid(to_i915(dev),
0583                                 limit,
0584                                 &clock))
0585                         continue;
0586 
0587                     this_err = abs(clock.dot - target);
0588                     if (this_err < err_most) {
0589                         *best_clock = clock;
0590                         err_most = this_err;
0591                         max_n = clock.n;
0592                         found = true;
0593                     }
0594                 }
0595             }
0596         }
0597     }
0598     return found;
0599 }
0600 
0601 /*
0602  * Check if the calculated PLL configuration is more optimal compared to the
0603  * best configuration and error found so far. Return the calculated error.
0604  */
0605 static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
0606                    const struct dpll *calculated_clock,
0607                    const struct dpll *best_clock,
0608                    unsigned int best_error_ppm,
0609                    unsigned int *error_ppm)
0610 {
0611     /*
0612      * For CHV ignore the error and consider only the P value.
0613      * Prefer a bigger P value based on HW requirements.
0614      */
0615     if (IS_CHERRYVIEW(to_i915(dev))) {
0616         *error_ppm = 0;
0617 
0618         return calculated_clock->p > best_clock->p;
0619     }
0620 
0621     if (drm_WARN_ON_ONCE(dev, !target_freq))
0622         return false;
0623 
0624     *error_ppm = div_u64(1000000ULL *
0625                 abs(target_freq - calculated_clock->dot),
0626                  target_freq);
0627     /*
0628      * Prefer a better P value over a better (smaller) error if the error
0629      * is small. Ensure this preference for future configurations too by
0630      * setting the error to 0.
0631      */
0632     if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
0633         *error_ppm = 0;
0634 
0635         return true;
0636     }
0637 
0638     return *error_ppm + 10 < best_error_ppm;
0639 }
0640 
0641 /*
0642  * Returns a set of divisors for the desired target clock with the given
0643  * refclk, or FALSE.
0644  */
0645 static bool
0646 vlv_find_best_dpll(const struct intel_limit *limit,
0647            struct intel_crtc_state *crtc_state,
0648            int target, int refclk,
0649            const struct dpll *match_clock,
0650            struct dpll *best_clock)
0651 {
0652     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
0653     struct drm_device *dev = crtc->base.dev;
0654     struct dpll clock;
0655     unsigned int bestppm = 1000000;
0656     /* min update 19.2 MHz */
0657     int max_n = min(limit->n.max, refclk / 19200);
0658     bool found = false;
0659 
0660     memset(best_clock, 0, sizeof(*best_clock));
0661 
0662     /* based on hardware requirement, prefer smaller n to precision */
0663     for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
0664         for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
0665             for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
0666                  clock.p2 -= clock.p2 > 10 ? 2 : 1) {
0667                 clock.p = clock.p1 * clock.p2 * 5;
0668                 /* based on hardware requirement, prefer bigger m1,m2 values */
0669                 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
0670                     unsigned int ppm;
0671 
0672                     clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
0673                                      refclk * clock.m1);
0674 
0675                     vlv_calc_dpll_params(refclk, &clock);
0676 
0677                     if (!intel_pll_is_valid(to_i915(dev),
0678                                 limit,
0679                                 &clock))
0680                         continue;
0681 
0682                     if (!vlv_PLL_is_optimal(dev, target,
0683                                 &clock,
0684                                 best_clock,
0685                                 bestppm, &ppm))
0686                         continue;
0687 
0688                     *best_clock = clock;
0689                     bestppm = ppm;
0690                     found = true;
0691                 }
0692             }
0693         }
0694     }
0695 
0696     return found;
0697 }
0698 
0699 /*
0700  * Returns a set of divisors for the desired target clock with the given
0701  * refclk, or FALSE.
0702  */
0703 static bool
0704 chv_find_best_dpll(const struct intel_limit *limit,
0705            struct intel_crtc_state *crtc_state,
0706            int target, int refclk,
0707            const struct dpll *match_clock,
0708            struct dpll *best_clock)
0709 {
0710     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
0711     struct drm_device *dev = crtc->base.dev;
0712     unsigned int best_error_ppm;
0713     struct dpll clock;
0714     u64 m2;
0715     int found = false;
0716 
0717     memset(best_clock, 0, sizeof(*best_clock));
0718     best_error_ppm = 1000000;
0719 
0720     /*
0721      * Based on hardware doc, the n always set to 1, and m1 always
0722      * set to 2.  If requires to support 200Mhz refclk, we need to
0723      * revisit this because n may not 1 anymore.
0724      */
0725     clock.n = 1;
0726     clock.m1 = 2;
0727 
0728     for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
0729         for (clock.p2 = limit->p2.p2_fast;
0730                 clock.p2 >= limit->p2.p2_slow;
0731                 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
0732             unsigned int error_ppm;
0733 
0734             clock.p = clock.p1 * clock.p2 * 5;
0735 
0736             m2 = DIV_ROUND_CLOSEST_ULL(mul_u32_u32(target, clock.p * clock.n) << 22,
0737                            refclk * clock.m1);
0738 
0739             if (m2 > INT_MAX/clock.m1)
0740                 continue;
0741 
0742             clock.m2 = m2;
0743 
0744             chv_calc_dpll_params(refclk, &clock);
0745 
0746             if (!intel_pll_is_valid(to_i915(dev), limit, &clock))
0747                 continue;
0748 
0749             if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
0750                         best_error_ppm, &error_ppm))
0751                 continue;
0752 
0753             *best_clock = clock;
0754             best_error_ppm = error_ppm;
0755             found = true;
0756         }
0757     }
0758 
0759     return found;
0760 }
0761 
0762 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state,
0763             struct dpll *best_clock)
0764 {
0765     const struct intel_limit *limit = &intel_limits_bxt;
0766     int refclk = 100000;
0767 
0768     return chv_find_best_dpll(limit, crtc_state,
0769                   crtc_state->port_clock, refclk,
0770                   NULL, best_clock);
0771 }
0772 
0773 u32 i9xx_dpll_compute_fp(const struct dpll *dpll)
0774 {
0775     return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
0776 }
0777 
0778 static u32 pnv_dpll_compute_fp(const struct dpll *dpll)
0779 {
0780     return (1 << dpll->n) << 16 | dpll->m2;
0781 }
0782 
0783 static void i9xx_update_pll_dividers(struct intel_crtc_state *crtc_state,
0784                      const struct dpll *clock,
0785                      const struct dpll *reduced_clock)
0786 {
0787     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
0788     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
0789     u32 fp, fp2;
0790 
0791     if (IS_PINEVIEW(dev_priv)) {
0792         fp = pnv_dpll_compute_fp(clock);
0793         fp2 = pnv_dpll_compute_fp(reduced_clock);
0794     } else {
0795         fp = i9xx_dpll_compute_fp(clock);
0796         fp2 = i9xx_dpll_compute_fp(reduced_clock);
0797     }
0798 
0799     crtc_state->dpll_hw_state.fp0 = fp;
0800     crtc_state->dpll_hw_state.fp1 = fp2;
0801 }
0802 
0803 static void i9xx_compute_dpll(struct intel_crtc_state *crtc_state,
0804                   const struct dpll *clock,
0805                   const struct dpll *reduced_clock)
0806 {
0807     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
0808     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
0809     u32 dpll;
0810 
0811     i9xx_update_pll_dividers(crtc_state, clock, reduced_clock);
0812 
0813     dpll = DPLL_VGA_MODE_DIS;
0814 
0815     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
0816         dpll |= DPLLB_MODE_LVDS;
0817     else
0818         dpll |= DPLLB_MODE_DAC_SERIAL;
0819 
0820     if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
0821         IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
0822         dpll |= (crtc_state->pixel_multiplier - 1)
0823             << SDVO_MULTIPLIER_SHIFT_HIRES;
0824     }
0825 
0826     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
0827         intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
0828         dpll |= DPLL_SDVO_HIGH_SPEED;
0829 
0830     if (intel_crtc_has_dp_encoder(crtc_state))
0831         dpll |= DPLL_SDVO_HIGH_SPEED;
0832 
0833     /* compute bitmask from p1 value */
0834     if (IS_G4X(dev_priv)) {
0835         dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
0836         dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
0837     } else if (IS_PINEVIEW(dev_priv)) {
0838         dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
0839         WARN_ON(reduced_clock->p1 != clock->p1);
0840     } else {
0841         dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
0842         WARN_ON(reduced_clock->p1 != clock->p1);
0843     }
0844 
0845     switch (clock->p2) {
0846     case 5:
0847         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
0848         break;
0849     case 7:
0850         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
0851         break;
0852     case 10:
0853         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
0854         break;
0855     case 14:
0856         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
0857         break;
0858     }
0859     WARN_ON(reduced_clock->p2 != clock->p2);
0860 
0861     if (DISPLAY_VER(dev_priv) >= 4)
0862         dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
0863 
0864     if (crtc_state->sdvo_tv_clock)
0865         dpll |= PLL_REF_INPUT_TVCLKINBC;
0866     else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
0867          intel_panel_use_ssc(dev_priv))
0868         dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
0869     else
0870         dpll |= PLL_REF_INPUT_DREFCLK;
0871 
0872     dpll |= DPLL_VCO_ENABLE;
0873     crtc_state->dpll_hw_state.dpll = dpll;
0874 
0875     if (DISPLAY_VER(dev_priv) >= 4) {
0876         u32 dpll_md = (crtc_state->pixel_multiplier - 1)
0877             << DPLL_MD_UDI_MULTIPLIER_SHIFT;
0878         crtc_state->dpll_hw_state.dpll_md = dpll_md;
0879     }
0880 }
0881 
0882 static void i8xx_compute_dpll(struct intel_crtc_state *crtc_state,
0883                   const struct dpll *clock,
0884                   const struct dpll *reduced_clock)
0885 {
0886     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
0887     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
0888     u32 dpll;
0889 
0890     i9xx_update_pll_dividers(crtc_state, clock, reduced_clock);
0891 
0892     dpll = DPLL_VGA_MODE_DIS;
0893 
0894     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
0895         dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
0896     } else {
0897         if (clock->p1 == 2)
0898             dpll |= PLL_P1_DIVIDE_BY_TWO;
0899         else
0900             dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
0901         if (clock->p2 == 4)
0902             dpll |= PLL_P2_DIVIDE_BY_4;
0903     }
0904     WARN_ON(reduced_clock->p1 != clock->p1);
0905     WARN_ON(reduced_clock->p2 != clock->p2);
0906 
0907     /*
0908      * Bspec:
0909      * "[Almador Errata}: For the correct operation of the muxed DVO pins
0910      *  (GDEVSELB/I2Cdata, GIRDBY/I2CClk) and (GFRAMEB/DVI_Data,
0911      *  GTRDYB/DVI_Clk): Bit 31 (DPLL VCO Enable) and Bit 30 (2X Clock
0912      *  Enable) must be set to “1” in both the DPLL A Control Register
0913      *  (06014h-06017h) and DPLL B Control Register (06018h-0601Bh)."
0914      *
0915      * For simplicity We simply keep both bits always enabled in
0916      * both DPLLS. The spec says we should disable the DVO 2X clock
0917      * when not needed, but this seems to work fine in practice.
0918      */
0919     if (IS_I830(dev_priv) ||
0920         intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
0921         dpll |= DPLL_DVO_2X_MODE;
0922 
0923     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
0924         intel_panel_use_ssc(dev_priv))
0925         dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
0926     else
0927         dpll |= PLL_REF_INPUT_DREFCLK;
0928 
0929     dpll |= DPLL_VCO_ENABLE;
0930     crtc_state->dpll_hw_state.dpll = dpll;
0931 }
0932 
0933 static int hsw_crtc_compute_clock(struct intel_atomic_state *state,
0934                   struct intel_crtc *crtc)
0935 {
0936     struct drm_i915_private *dev_priv = to_i915(state->base.dev);
0937     struct intel_crtc_state *crtc_state =
0938         intel_atomic_get_new_crtc_state(state, crtc);
0939     struct intel_encoder *encoder =
0940         intel_get_crtc_new_encoder(state, crtc_state);
0941 
0942     if (DISPLAY_VER(dev_priv) < 11 &&
0943         intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
0944         return 0;
0945 
0946     return intel_compute_shared_dplls(state, crtc, encoder);
0947 }
0948 
0949 static int hsw_crtc_get_shared_dpll(struct intel_atomic_state *state,
0950                     struct intel_crtc *crtc)
0951 {
0952     struct drm_i915_private *dev_priv = to_i915(state->base.dev);
0953     struct intel_crtc_state *crtc_state =
0954         intel_atomic_get_new_crtc_state(state, crtc);
0955     struct intel_encoder *encoder =
0956         intel_get_crtc_new_encoder(state, crtc_state);
0957 
0958     if (DISPLAY_VER(dev_priv) < 11 &&
0959         intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
0960         return 0;
0961 
0962     return intel_reserve_shared_dplls(state, crtc, encoder);
0963 }
0964 
0965 static int dg2_crtc_compute_clock(struct intel_atomic_state *state,
0966                   struct intel_crtc *crtc)
0967 {
0968     struct intel_crtc_state *crtc_state =
0969         intel_atomic_get_new_crtc_state(state, crtc);
0970     struct intel_encoder *encoder =
0971         intel_get_crtc_new_encoder(state, crtc_state);
0972 
0973     return intel_mpllb_calc_state(crtc_state, encoder);
0974 }
0975 
0976 static bool ilk_needs_fb_cb_tune(const struct dpll *dpll, int factor)
0977 {
0978     return dpll->m < factor * dpll->n;
0979 }
0980 
0981 static void ilk_update_pll_dividers(struct intel_crtc_state *crtc_state,
0982                     const struct dpll *clock,
0983                     const struct dpll *reduced_clock)
0984 {
0985     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
0986     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
0987     u32 fp, fp2;
0988     int factor;
0989 
0990     /* Enable autotuning of the PLL clock (if permissible) */
0991     factor = 21;
0992     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
0993         if ((intel_panel_use_ssc(dev_priv) &&
0994              dev_priv->vbt.lvds_ssc_freq == 100000) ||
0995             (HAS_PCH_IBX(dev_priv) &&
0996              intel_is_dual_link_lvds(dev_priv)))
0997             factor = 25;
0998     } else if (crtc_state->sdvo_tv_clock) {
0999         factor = 20;
1000     }
1001 
1002     fp = i9xx_dpll_compute_fp(clock);
1003     if (ilk_needs_fb_cb_tune(clock, factor))
1004         fp |= FP_CB_TUNE;
1005 
1006     fp2 = i9xx_dpll_compute_fp(reduced_clock);
1007     if (ilk_needs_fb_cb_tune(reduced_clock, factor))
1008         fp2 |= FP_CB_TUNE;
1009 
1010     crtc_state->dpll_hw_state.fp0 = fp;
1011     crtc_state->dpll_hw_state.fp1 = fp2;
1012 }
1013 
1014 static void ilk_compute_dpll(struct intel_crtc_state *crtc_state,
1015                  const struct dpll *clock,
1016                  const struct dpll *reduced_clock)
1017 {
1018     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1019     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1020     u32 dpll;
1021 
1022     ilk_update_pll_dividers(crtc_state, clock, reduced_clock);
1023 
1024     dpll = 0;
1025 
1026     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
1027         dpll |= DPLLB_MODE_LVDS;
1028     else
1029         dpll |= DPLLB_MODE_DAC_SERIAL;
1030 
1031     dpll |= (crtc_state->pixel_multiplier - 1)
1032         << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
1033 
1034     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
1035         intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1036         dpll |= DPLL_SDVO_HIGH_SPEED;
1037 
1038     if (intel_crtc_has_dp_encoder(crtc_state))
1039         dpll |= DPLL_SDVO_HIGH_SPEED;
1040 
1041     /*
1042      * The high speed IO clock is only really required for
1043      * SDVO/HDMI/DP, but we also enable it for CRT to make it
1044      * possible to share the DPLL between CRT and HDMI. Enabling
1045      * the clock needlessly does no real harm, except use up a
1046      * bit of power potentially.
1047      *
1048      * We'll limit this to IVB with 3 pipes, since it has only two
1049      * DPLLs and so DPLL sharing is the only way to get three pipes
1050      * driving PCH ports at the same time. On SNB we could do this,
1051      * and potentially avoid enabling the second DPLL, but it's not
1052      * clear if it''s a win or loss power wise. No point in doing
1053      * this on ILK at all since it has a fixed DPLL<->pipe mapping.
1054      */
1055     if (INTEL_NUM_PIPES(dev_priv) == 3 &&
1056         intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
1057         dpll |= DPLL_SDVO_HIGH_SPEED;
1058 
1059     /* compute bitmask from p1 value */
1060     dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
1061     /* also FPA1 */
1062     dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
1063 
1064     switch (clock->p2) {
1065     case 5:
1066         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
1067         break;
1068     case 7:
1069         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
1070         break;
1071     case 10:
1072         dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
1073         break;
1074     case 14:
1075         dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
1076         break;
1077     }
1078     WARN_ON(reduced_clock->p2 != clock->p2);
1079 
1080     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
1081         intel_panel_use_ssc(dev_priv))
1082         dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
1083     else
1084         dpll |= PLL_REF_INPUT_DREFCLK;
1085 
1086     dpll |= DPLL_VCO_ENABLE;
1087 
1088     crtc_state->dpll_hw_state.dpll = dpll;
1089 }
1090 
1091 static int ilk_crtc_compute_clock(struct intel_atomic_state *state,
1092                   struct intel_crtc *crtc)
1093 {
1094     struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1095     struct intel_crtc_state *crtc_state =
1096         intel_atomic_get_new_crtc_state(state, crtc);
1097     const struct intel_limit *limit;
1098     int refclk = 120000;
1099 
1100     /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
1101     if (!crtc_state->has_pch_encoder)
1102         return 0;
1103 
1104     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1105         if (intel_panel_use_ssc(dev_priv)) {
1106             drm_dbg_kms(&dev_priv->drm,
1107                     "using SSC reference clock of %d kHz\n",
1108                     dev_priv->vbt.lvds_ssc_freq);
1109             refclk = dev_priv->vbt.lvds_ssc_freq;
1110         }
1111 
1112         if (intel_is_dual_link_lvds(dev_priv)) {
1113             if (refclk == 100000)
1114                 limit = &ilk_limits_dual_lvds_100m;
1115             else
1116                 limit = &ilk_limits_dual_lvds;
1117         } else {
1118             if (refclk == 100000)
1119                 limit = &ilk_limits_single_lvds_100m;
1120             else
1121                 limit = &ilk_limits_single_lvds;
1122         }
1123     } else {
1124         limit = &ilk_limits_dac;
1125     }
1126 
1127     if (!crtc_state->clock_set &&
1128         !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1129                 refclk, NULL, &crtc_state->dpll))
1130         return -EINVAL;
1131 
1132     ilk_compute_dpll(crtc_state, &crtc_state->dpll,
1133              &crtc_state->dpll);
1134 
1135     return intel_compute_shared_dplls(state, crtc, NULL);
1136 }
1137 
1138 static int ilk_crtc_get_shared_dpll(struct intel_atomic_state *state,
1139                     struct intel_crtc *crtc)
1140 {
1141     struct intel_crtc_state *crtc_state =
1142         intel_atomic_get_new_crtc_state(state, crtc);
1143 
1144     /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
1145     if (!crtc_state->has_pch_encoder)
1146         return 0;
1147 
1148     return intel_reserve_shared_dplls(state, crtc, NULL);
1149 }
1150 
1151 void vlv_compute_dpll(struct intel_crtc_state *crtc_state)
1152 {
1153     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1154 
1155     crtc_state->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
1156         DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1157     if (crtc->pipe != PIPE_A)
1158         crtc_state->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1159 
1160     /* DPLL not used with DSI, but still need the rest set up */
1161     if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
1162         crtc_state->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
1163             DPLL_EXT_BUFFER_ENABLE_VLV;
1164 
1165     crtc_state->dpll_hw_state.dpll_md =
1166         (crtc_state->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1167 }
1168 
1169 void chv_compute_dpll(struct intel_crtc_state *crtc_state)
1170 {
1171     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1172 
1173     crtc_state->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
1174         DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1175     if (crtc->pipe != PIPE_A)
1176         crtc_state->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1177 
1178     /* DPLL not used with DSI, but still need the rest set up */
1179     if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
1180         crtc_state->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
1181 
1182     crtc_state->dpll_hw_state.dpll_md =
1183         (crtc_state->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1184 }
1185 
1186 static int chv_crtc_compute_clock(struct intel_atomic_state *state,
1187                   struct intel_crtc *crtc)
1188 {
1189     struct intel_crtc_state *crtc_state =
1190         intel_atomic_get_new_crtc_state(state, crtc);
1191     const struct intel_limit *limit = &intel_limits_chv;
1192     int refclk = 100000;
1193 
1194     if (!crtc_state->clock_set &&
1195         !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1196                 refclk, NULL, &crtc_state->dpll))
1197         return -EINVAL;
1198 
1199     chv_compute_dpll(crtc_state);
1200 
1201     return 0;
1202 }
1203 
1204 static int vlv_crtc_compute_clock(struct intel_atomic_state *state,
1205                   struct intel_crtc *crtc)
1206 {
1207     struct intel_crtc_state *crtc_state =
1208         intel_atomic_get_new_crtc_state(state, crtc);
1209     const struct intel_limit *limit = &intel_limits_vlv;
1210     int refclk = 100000;
1211 
1212     if (!crtc_state->clock_set &&
1213         !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1214                 refclk, NULL, &crtc_state->dpll)) {
1215         return -EINVAL;
1216     }
1217 
1218     vlv_compute_dpll(crtc_state);
1219 
1220     return 0;
1221 }
1222 
1223 static int g4x_crtc_compute_clock(struct intel_atomic_state *state,
1224                   struct intel_crtc *crtc)
1225 {
1226     struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1227     struct intel_crtc_state *crtc_state =
1228         intel_atomic_get_new_crtc_state(state, crtc);
1229     const struct intel_limit *limit;
1230     int refclk = 96000;
1231 
1232     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1233         if (intel_panel_use_ssc(dev_priv)) {
1234             refclk = dev_priv->vbt.lvds_ssc_freq;
1235             drm_dbg_kms(&dev_priv->drm,
1236                     "using SSC reference clock of %d kHz\n",
1237                     refclk);
1238         }
1239 
1240         if (intel_is_dual_link_lvds(dev_priv))
1241             limit = &intel_limits_g4x_dual_channel_lvds;
1242         else
1243             limit = &intel_limits_g4x_single_channel_lvds;
1244     } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
1245            intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
1246         limit = &intel_limits_g4x_hdmi;
1247     } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
1248         limit = &intel_limits_g4x_sdvo;
1249     } else {
1250         /* The option is for other outputs */
1251         limit = &intel_limits_i9xx_sdvo;
1252     }
1253 
1254     if (!crtc_state->clock_set &&
1255         !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1256                 refclk, NULL, &crtc_state->dpll))
1257         return -EINVAL;
1258 
1259     i9xx_compute_dpll(crtc_state, &crtc_state->dpll,
1260               &crtc_state->dpll);
1261 
1262     return 0;
1263 }
1264 
1265 static int pnv_crtc_compute_clock(struct intel_atomic_state *state,
1266                   struct intel_crtc *crtc)
1267 {
1268     struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1269     struct intel_crtc_state *crtc_state =
1270         intel_atomic_get_new_crtc_state(state, crtc);
1271     const struct intel_limit *limit;
1272     int refclk = 96000;
1273 
1274     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1275         if (intel_panel_use_ssc(dev_priv)) {
1276             refclk = dev_priv->vbt.lvds_ssc_freq;
1277             drm_dbg_kms(&dev_priv->drm,
1278                     "using SSC reference clock of %d kHz\n",
1279                     refclk);
1280         }
1281 
1282         limit = &pnv_limits_lvds;
1283     } else {
1284         limit = &pnv_limits_sdvo;
1285     }
1286 
1287     if (!crtc_state->clock_set &&
1288         !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1289                 refclk, NULL, &crtc_state->dpll))
1290         return -EINVAL;
1291 
1292     i9xx_compute_dpll(crtc_state, &crtc_state->dpll,
1293               &crtc_state->dpll);
1294 
1295     return 0;
1296 }
1297 
1298 static int i9xx_crtc_compute_clock(struct intel_atomic_state *state,
1299                    struct intel_crtc *crtc)
1300 {
1301     struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1302     struct intel_crtc_state *crtc_state =
1303         intel_atomic_get_new_crtc_state(state, crtc);
1304     const struct intel_limit *limit;
1305     int refclk = 96000;
1306 
1307     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1308         if (intel_panel_use_ssc(dev_priv)) {
1309             refclk = dev_priv->vbt.lvds_ssc_freq;
1310             drm_dbg_kms(&dev_priv->drm,
1311                     "using SSC reference clock of %d kHz\n",
1312                     refclk);
1313         }
1314 
1315         limit = &intel_limits_i9xx_lvds;
1316     } else {
1317         limit = &intel_limits_i9xx_sdvo;
1318     }
1319 
1320     if (!crtc_state->clock_set &&
1321         !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1322                  refclk, NULL, &crtc_state->dpll))
1323         return -EINVAL;
1324 
1325     i9xx_compute_dpll(crtc_state, &crtc_state->dpll,
1326               &crtc_state->dpll);
1327 
1328     return 0;
1329 }
1330 
1331 static int i8xx_crtc_compute_clock(struct intel_atomic_state *state,
1332                    struct intel_crtc *crtc)
1333 {
1334     struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1335     struct intel_crtc_state *crtc_state =
1336         intel_atomic_get_new_crtc_state(state, crtc);
1337     const struct intel_limit *limit;
1338     int refclk = 48000;
1339 
1340     if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1341         if (intel_panel_use_ssc(dev_priv)) {
1342             refclk = dev_priv->vbt.lvds_ssc_freq;
1343             drm_dbg_kms(&dev_priv->drm,
1344                     "using SSC reference clock of %d kHz\n",
1345                     refclk);
1346         }
1347 
1348         limit = &intel_limits_i8xx_lvds;
1349     } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
1350         limit = &intel_limits_i8xx_dvo;
1351     } else {
1352         limit = &intel_limits_i8xx_dac;
1353     }
1354 
1355     if (!crtc_state->clock_set &&
1356         !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
1357                  refclk, NULL, &crtc_state->dpll))
1358         return -EINVAL;
1359 
1360     i8xx_compute_dpll(crtc_state, &crtc_state->dpll,
1361               &crtc_state->dpll);
1362 
1363     return 0;
1364 }
1365 
1366 static const struct intel_dpll_funcs dg2_dpll_funcs = {
1367     .crtc_compute_clock = dg2_crtc_compute_clock,
1368 };
1369 
1370 static const struct intel_dpll_funcs hsw_dpll_funcs = {
1371     .crtc_compute_clock = hsw_crtc_compute_clock,
1372     .crtc_get_shared_dpll = hsw_crtc_get_shared_dpll,
1373 };
1374 
1375 static const struct intel_dpll_funcs ilk_dpll_funcs = {
1376     .crtc_compute_clock = ilk_crtc_compute_clock,
1377     .crtc_get_shared_dpll = ilk_crtc_get_shared_dpll,
1378 };
1379 
1380 static const struct intel_dpll_funcs chv_dpll_funcs = {
1381     .crtc_compute_clock = chv_crtc_compute_clock,
1382 };
1383 
1384 static const struct intel_dpll_funcs vlv_dpll_funcs = {
1385     .crtc_compute_clock = vlv_crtc_compute_clock,
1386 };
1387 
1388 static const struct intel_dpll_funcs g4x_dpll_funcs = {
1389     .crtc_compute_clock = g4x_crtc_compute_clock,
1390 };
1391 
1392 static const struct intel_dpll_funcs pnv_dpll_funcs = {
1393     .crtc_compute_clock = pnv_crtc_compute_clock,
1394 };
1395 
1396 static const struct intel_dpll_funcs i9xx_dpll_funcs = {
1397     .crtc_compute_clock = i9xx_crtc_compute_clock,
1398 };
1399 
1400 static const struct intel_dpll_funcs i8xx_dpll_funcs = {
1401     .crtc_compute_clock = i8xx_crtc_compute_clock,
1402 };
1403 
1404 int intel_dpll_crtc_compute_clock(struct intel_atomic_state *state,
1405                   struct intel_crtc *crtc)
1406 {
1407     struct drm_i915_private *i915 = to_i915(state->base.dev);
1408     struct intel_crtc_state *crtc_state =
1409         intel_atomic_get_new_crtc_state(state, crtc);
1410     int ret;
1411 
1412     drm_WARN_ON(&i915->drm, !intel_crtc_needs_modeset(crtc_state));
1413 
1414     if (drm_WARN_ON(&i915->drm, crtc_state->shared_dpll))
1415         return 0;
1416 
1417     memset(&crtc_state->dpll_hw_state, 0,
1418            sizeof(crtc_state->dpll_hw_state));
1419 
1420     if (!crtc_state->hw.enable)
1421         return 0;
1422 
1423     ret = i915->dpll_funcs->crtc_compute_clock(state, crtc);
1424     if (ret) {
1425         drm_dbg_kms(&i915->drm, "[CRTC:%d:%s] Couldn't calculate DPLL settings\n",
1426                 crtc->base.base.id, crtc->base.name);
1427         return ret;
1428     }
1429 
1430     return 0;
1431 }
1432 
1433 int intel_dpll_crtc_get_shared_dpll(struct intel_atomic_state *state,
1434                     struct intel_crtc *crtc)
1435 {
1436     struct drm_i915_private *i915 = to_i915(state->base.dev);
1437     struct intel_crtc_state *crtc_state =
1438         intel_atomic_get_new_crtc_state(state, crtc);
1439     int ret;
1440 
1441     drm_WARN_ON(&i915->drm, !intel_crtc_needs_modeset(crtc_state));
1442 
1443     if (drm_WARN_ON(&i915->drm, crtc_state->shared_dpll))
1444         return 0;
1445 
1446     if (!crtc_state->hw.enable)
1447         return 0;
1448 
1449     if (!i915->dpll_funcs->crtc_get_shared_dpll)
1450         return 0;
1451 
1452     ret = i915->dpll_funcs->crtc_get_shared_dpll(state, crtc);
1453     if (ret) {
1454         drm_dbg_kms(&i915->drm, "[CRTC:%d:%s] Couldn't get a shared DPLL\n",
1455                 crtc->base.base.id, crtc->base.name);
1456         return ret;
1457     }
1458 
1459     return 0;
1460 }
1461 
1462 void
1463 intel_dpll_init_clock_hook(struct drm_i915_private *dev_priv)
1464 {
1465     if (IS_DG2(dev_priv))
1466         dev_priv->dpll_funcs = &dg2_dpll_funcs;
1467     else if (DISPLAY_VER(dev_priv) >= 9 || HAS_DDI(dev_priv))
1468         dev_priv->dpll_funcs = &hsw_dpll_funcs;
1469     else if (HAS_PCH_SPLIT(dev_priv))
1470         dev_priv->dpll_funcs = &ilk_dpll_funcs;
1471     else if (IS_CHERRYVIEW(dev_priv))
1472         dev_priv->dpll_funcs = &chv_dpll_funcs;
1473     else if (IS_VALLEYVIEW(dev_priv))
1474         dev_priv->dpll_funcs = &vlv_dpll_funcs;
1475     else if (IS_G4X(dev_priv))
1476         dev_priv->dpll_funcs = &g4x_dpll_funcs;
1477     else if (IS_PINEVIEW(dev_priv))
1478         dev_priv->dpll_funcs = &pnv_dpll_funcs;
1479     else if (DISPLAY_VER(dev_priv) != 2)
1480         dev_priv->dpll_funcs = &i9xx_dpll_funcs;
1481     else
1482         dev_priv->dpll_funcs = &i8xx_dpll_funcs;
1483 }
1484 
1485 static bool i9xx_has_pps(struct drm_i915_private *dev_priv)
1486 {
1487     if (IS_I830(dev_priv))
1488         return false;
1489 
1490     return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
1491 }
1492 
1493 void i9xx_enable_pll(const struct intel_crtc_state *crtc_state)
1494 {
1495     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1496     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1497     u32 dpll = crtc_state->dpll_hw_state.dpll;
1498     enum pipe pipe = crtc->pipe;
1499     int i;
1500 
1501     assert_transcoder_disabled(dev_priv, crtc_state->cpu_transcoder);
1502 
1503     /* PLL is protected by panel, make sure we can write it */
1504     if (i9xx_has_pps(dev_priv))
1505         assert_pps_unlocked(dev_priv, pipe);
1506 
1507     intel_de_write(dev_priv, FP0(pipe), crtc_state->dpll_hw_state.fp0);
1508     intel_de_write(dev_priv, FP1(pipe), crtc_state->dpll_hw_state.fp1);
1509 
1510     /*
1511      * Apparently we need to have VGA mode enabled prior to changing
1512      * the P1/P2 dividers. Otherwise the DPLL will keep using the old
1513      * dividers, even though the register value does change.
1514      */
1515     intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
1516     intel_de_write(dev_priv, DPLL(pipe), dpll);
1517 
1518     /* Wait for the clocks to stabilize. */
1519     intel_de_posting_read(dev_priv, DPLL(pipe));
1520     udelay(150);
1521 
1522     if (DISPLAY_VER(dev_priv) >= 4) {
1523         intel_de_write(dev_priv, DPLL_MD(pipe),
1524                    crtc_state->dpll_hw_state.dpll_md);
1525     } else {
1526         /* The pixel multiplier can only be updated once the
1527          * DPLL is enabled and the clocks are stable.
1528          *
1529          * So write it again.
1530          */
1531         intel_de_write(dev_priv, DPLL(pipe), dpll);
1532     }
1533 
1534     /* We do this three times for luck */
1535     for (i = 0; i < 3; i++) {
1536         intel_de_write(dev_priv, DPLL(pipe), dpll);
1537         intel_de_posting_read(dev_priv, DPLL(pipe));
1538         udelay(150); /* wait for warmup */
1539     }
1540 }
1541 
1542 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv,
1543                  enum pipe pipe)
1544 {
1545     u32 reg_val;
1546 
1547     /*
1548      * PLLB opamp always calibrates to max value of 0x3f, force enable it
1549      * and set it to a reasonable value instead.
1550      */
1551     reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
1552     reg_val &= 0xffffff00;
1553     reg_val |= 0x00000030;
1554     vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
1555 
1556     reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
1557     reg_val &= 0x00ffffff;
1558     reg_val |= 0x8c000000;
1559     vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
1560 
1561     reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
1562     reg_val &= 0xffffff00;
1563     vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
1564 
1565     reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
1566     reg_val &= 0x00ffffff;
1567     reg_val |= 0xb0000000;
1568     vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
1569 }
1570 
1571 static void vlv_prepare_pll(const struct intel_crtc_state *crtc_state)
1572 {
1573     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1574     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1575     enum pipe pipe = crtc->pipe;
1576     u32 mdiv;
1577     u32 bestn, bestm1, bestm2, bestp1, bestp2;
1578     u32 coreclk, reg_val;
1579 
1580     vlv_dpio_get(dev_priv);
1581 
1582     bestn = crtc_state->dpll.n;
1583     bestm1 = crtc_state->dpll.m1;
1584     bestm2 = crtc_state->dpll.m2;
1585     bestp1 = crtc_state->dpll.p1;
1586     bestp2 = crtc_state->dpll.p2;
1587 
1588     /* See eDP HDMI DPIO driver vbios notes doc */
1589 
1590     /* PLL B needs special handling */
1591     if (pipe == PIPE_B)
1592         vlv_pllb_recal_opamp(dev_priv, pipe);
1593 
1594     /* Set up Tx target for periodic Rcomp update */
1595     vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
1596 
1597     /* Disable target IRef on PLL */
1598     reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
1599     reg_val &= 0x00ffffff;
1600     vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
1601 
1602     /* Disable fast lock */
1603     vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
1604 
1605     /* Set idtafcrecal before PLL is enabled */
1606     mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
1607     mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
1608     mdiv |= ((bestn << DPIO_N_SHIFT));
1609     mdiv |= (1 << DPIO_K_SHIFT);
1610 
1611     /*
1612      * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
1613      * but we don't support that).
1614      * Note: don't use the DAC post divider as it seems unstable.
1615      */
1616     mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
1617     vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
1618 
1619     mdiv |= DPIO_ENABLE_CALIBRATION;
1620     vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
1621 
1622     /* Set HBR and RBR LPF coefficients */
1623     if (crtc_state->port_clock == 162000 ||
1624         intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG) ||
1625         intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1626         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
1627                  0x009f0003);
1628     else
1629         vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
1630                  0x00d0000f);
1631 
1632     if (intel_crtc_has_dp_encoder(crtc_state)) {
1633         /* Use SSC source */
1634         if (pipe == PIPE_A)
1635             vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1636                      0x0df40000);
1637         else
1638             vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1639                      0x0df70000);
1640     } else { /* HDMI or VGA */
1641         /* Use bend source */
1642         if (pipe == PIPE_A)
1643             vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1644                      0x0df70000);
1645         else
1646             vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
1647                      0x0df40000);
1648     }
1649 
1650     coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
1651     coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
1652     if (intel_crtc_has_dp_encoder(crtc_state))
1653         coreclk |= 0x01000000;
1654     vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
1655 
1656     vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
1657 
1658     vlv_dpio_put(dev_priv);
1659 }
1660 
1661 static void _vlv_enable_pll(const struct intel_crtc_state *crtc_state)
1662 {
1663     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1664     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1665     enum pipe pipe = crtc->pipe;
1666 
1667     intel_de_write(dev_priv, DPLL(pipe), crtc_state->dpll_hw_state.dpll);
1668     intel_de_posting_read(dev_priv, DPLL(pipe));
1669     udelay(150);
1670 
1671     if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1672         drm_err(&dev_priv->drm, "DPLL %d failed to lock\n", pipe);
1673 }
1674 
1675 void vlv_enable_pll(const struct intel_crtc_state *crtc_state)
1676 {
1677     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1678     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1679     enum pipe pipe = crtc->pipe;
1680 
1681     assert_transcoder_disabled(dev_priv, crtc_state->cpu_transcoder);
1682 
1683     /* PLL is protected by panel, make sure we can write it */
1684     assert_pps_unlocked(dev_priv, pipe);
1685 
1686     /* Enable Refclk */
1687     intel_de_write(dev_priv, DPLL(pipe),
1688                crtc_state->dpll_hw_state.dpll &
1689                ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
1690 
1691     if (crtc_state->dpll_hw_state.dpll & DPLL_VCO_ENABLE) {
1692         vlv_prepare_pll(crtc_state);
1693         _vlv_enable_pll(crtc_state);
1694     }
1695 
1696     intel_de_write(dev_priv, DPLL_MD(pipe),
1697                crtc_state->dpll_hw_state.dpll_md);
1698     intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1699 }
1700 
1701 static void chv_prepare_pll(const struct intel_crtc_state *crtc_state)
1702 {
1703     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1704     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1705     enum pipe pipe = crtc->pipe;
1706     enum dpio_channel port = vlv_pipe_to_channel(pipe);
1707     u32 loopfilter, tribuf_calcntr;
1708     u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
1709     u32 dpio_val;
1710     int vco;
1711 
1712     bestn = crtc_state->dpll.n;
1713     bestm2_frac = crtc_state->dpll.m2 & 0x3fffff;
1714     bestm1 = crtc_state->dpll.m1;
1715     bestm2 = crtc_state->dpll.m2 >> 22;
1716     bestp1 = crtc_state->dpll.p1;
1717     bestp2 = crtc_state->dpll.p2;
1718     vco = crtc_state->dpll.vco;
1719     dpio_val = 0;
1720     loopfilter = 0;
1721 
1722     vlv_dpio_get(dev_priv);
1723 
1724     /* p1 and p2 divider */
1725     vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
1726             5 << DPIO_CHV_S1_DIV_SHIFT |
1727             bestp1 << DPIO_CHV_P1_DIV_SHIFT |
1728             bestp2 << DPIO_CHV_P2_DIV_SHIFT |
1729             1 << DPIO_CHV_K_DIV_SHIFT);
1730 
1731     /* Feedback post-divider - m2 */
1732     vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
1733 
1734     /* Feedback refclk divider - n and m1 */
1735     vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
1736             DPIO_CHV_M1_DIV_BY_2 |
1737             1 << DPIO_CHV_N_DIV_SHIFT);
1738 
1739     /* M2 fraction division */
1740     vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
1741 
1742     /* M2 fraction division enable */
1743     dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
1744     dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
1745     dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
1746     if (bestm2_frac)
1747         dpio_val |= DPIO_CHV_FRAC_DIV_EN;
1748     vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
1749 
1750     /* Program digital lock detect threshold */
1751     dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
1752     dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
1753                     DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
1754     dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
1755     if (!bestm2_frac)
1756         dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
1757     vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
1758 
1759     /* Loop filter */
1760     if (vco == 5400000) {
1761         loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
1762         loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
1763         loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
1764         tribuf_calcntr = 0x9;
1765     } else if (vco <= 6200000) {
1766         loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
1767         loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
1768         loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
1769         tribuf_calcntr = 0x9;
1770     } else if (vco <= 6480000) {
1771         loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
1772         loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
1773         loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
1774         tribuf_calcntr = 0x8;
1775     } else {
1776         /* Not supported. Apply the same limits as in the max case */
1777         loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
1778         loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
1779         loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
1780         tribuf_calcntr = 0;
1781     }
1782     vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
1783 
1784     dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
1785     dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
1786     dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
1787     vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
1788 
1789     /* AFC Recal */
1790     vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
1791             vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
1792             DPIO_AFC_RECAL);
1793 
1794     vlv_dpio_put(dev_priv);
1795 }
1796 
1797 static void _chv_enable_pll(const struct intel_crtc_state *crtc_state)
1798 {
1799     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1800     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1801     enum pipe pipe = crtc->pipe;
1802     enum dpio_channel port = vlv_pipe_to_channel(pipe);
1803     u32 tmp;
1804 
1805     vlv_dpio_get(dev_priv);
1806 
1807     /* Enable back the 10bit clock to display controller */
1808     tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1809     tmp |= DPIO_DCLKP_EN;
1810     vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1811 
1812     vlv_dpio_put(dev_priv);
1813 
1814     /*
1815      * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1816      */
1817     udelay(1);
1818 
1819     /* Enable PLL */
1820     intel_de_write(dev_priv, DPLL(pipe), crtc_state->dpll_hw_state.dpll);
1821 
1822     /* Check PLL is locked */
1823     if (intel_de_wait_for_set(dev_priv, DPLL(pipe), DPLL_LOCK_VLV, 1))
1824         drm_err(&dev_priv->drm, "PLL %d failed to lock\n", pipe);
1825 }
1826 
1827 void chv_enable_pll(const struct intel_crtc_state *crtc_state)
1828 {
1829     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1830     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1831     enum pipe pipe = crtc->pipe;
1832 
1833     assert_transcoder_disabled(dev_priv, crtc_state->cpu_transcoder);
1834 
1835     /* PLL is protected by panel, make sure we can write it */
1836     assert_pps_unlocked(dev_priv, pipe);
1837 
1838     /* Enable Refclk and SSC */
1839     intel_de_write(dev_priv, DPLL(pipe),
1840                crtc_state->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
1841 
1842     if (crtc_state->dpll_hw_state.dpll & DPLL_VCO_ENABLE) {
1843         chv_prepare_pll(crtc_state);
1844         _chv_enable_pll(crtc_state);
1845     }
1846 
1847     if (pipe != PIPE_A) {
1848         /*
1849          * WaPixelRepeatModeFixForC0:chv
1850          *
1851          * DPLLCMD is AWOL. Use chicken bits to propagate
1852          * the value from DPLLBMD to either pipe B or C.
1853          */
1854         intel_de_write(dev_priv, CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
1855         intel_de_write(dev_priv, DPLL_MD(PIPE_B),
1856                    crtc_state->dpll_hw_state.dpll_md);
1857         intel_de_write(dev_priv, CBR4_VLV, 0);
1858         dev_priv->chv_dpll_md[pipe] = crtc_state->dpll_hw_state.dpll_md;
1859 
1860         /*
1861          * DPLLB VGA mode also seems to cause problems.
1862          * We should always have it disabled.
1863          */
1864         drm_WARN_ON(&dev_priv->drm,
1865                 (intel_de_read(dev_priv, DPLL(PIPE_B)) &
1866                  DPLL_VGA_MODE_DIS) == 0);
1867     } else {
1868         intel_de_write(dev_priv, DPLL_MD(pipe),
1869                    crtc_state->dpll_hw_state.dpll_md);
1870         intel_de_posting_read(dev_priv, DPLL_MD(pipe));
1871     }
1872 }
1873 
1874 /**
1875  * vlv_force_pll_on - forcibly enable just the PLL
1876  * @dev_priv: i915 private structure
1877  * @pipe: pipe PLL to enable
1878  * @dpll: PLL configuration
1879  *
1880  * Enable the PLL for @pipe using the supplied @dpll config. To be used
1881  * in cases where we need the PLL enabled even when @pipe is not going to
1882  * be enabled.
1883  */
1884 int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
1885              const struct dpll *dpll)
1886 {
1887     struct intel_crtc *crtc = intel_crtc_for_pipe(dev_priv, pipe);
1888     struct intel_crtc_state *crtc_state;
1889 
1890     crtc_state = intel_crtc_state_alloc(crtc);
1891     if (!crtc_state)
1892         return -ENOMEM;
1893 
1894     crtc_state->cpu_transcoder = (enum transcoder)pipe;
1895     crtc_state->pixel_multiplier = 1;
1896     crtc_state->dpll = *dpll;
1897     crtc_state->output_types = BIT(INTEL_OUTPUT_EDP);
1898 
1899     if (IS_CHERRYVIEW(dev_priv)) {
1900         chv_compute_dpll(crtc_state);
1901         chv_enable_pll(crtc_state);
1902     } else {
1903         vlv_compute_dpll(crtc_state);
1904         vlv_enable_pll(crtc_state);
1905     }
1906 
1907     kfree(crtc_state);
1908 
1909     return 0;
1910 }
1911 
1912 void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1913 {
1914     u32 val;
1915 
1916     /* Make sure the pipe isn't still relying on us */
1917     assert_transcoder_disabled(dev_priv, (enum transcoder)pipe);
1918 
1919     val = DPLL_INTEGRATED_REF_CLK_VLV |
1920         DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1921     if (pipe != PIPE_A)
1922         val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1923 
1924     intel_de_write(dev_priv, DPLL(pipe), val);
1925     intel_de_posting_read(dev_priv, DPLL(pipe));
1926 }
1927 
1928 void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1929 {
1930     enum dpio_channel port = vlv_pipe_to_channel(pipe);
1931     u32 val;
1932 
1933     /* Make sure the pipe isn't still relying on us */
1934     assert_transcoder_disabled(dev_priv, (enum transcoder)pipe);
1935 
1936     val = DPLL_SSC_REF_CLK_CHV |
1937         DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
1938     if (pipe != PIPE_A)
1939         val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1940 
1941     intel_de_write(dev_priv, DPLL(pipe), val);
1942     intel_de_posting_read(dev_priv, DPLL(pipe));
1943 
1944     vlv_dpio_get(dev_priv);
1945 
1946     /* Disable 10bit clock to display controller */
1947     val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1948     val &= ~DPIO_DCLKP_EN;
1949     vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1950 
1951     vlv_dpio_put(dev_priv);
1952 }
1953 
1954 void i9xx_disable_pll(const struct intel_crtc_state *crtc_state)
1955 {
1956     struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1957     struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1958     enum pipe pipe = crtc->pipe;
1959 
1960     /* Don't disable pipe or pipe PLLs if needed */
1961     if (IS_I830(dev_priv))
1962         return;
1963 
1964     /* Make sure the pipe isn't still relying on us */
1965     assert_transcoder_disabled(dev_priv, crtc_state->cpu_transcoder);
1966 
1967     intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
1968     intel_de_posting_read(dev_priv, DPLL(pipe));
1969 }
1970 
1971 
1972 /**
1973  * vlv_force_pll_off - forcibly disable just the PLL
1974  * @dev_priv: i915 private structure
1975  * @pipe: pipe PLL to disable
1976  *
1977  * Disable the PLL for @pipe. To be used in cases where we need
1978  * the PLL enabled even when @pipe is not going to be enabled.
1979  */
1980 void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
1981 {
1982     if (IS_CHERRYVIEW(dev_priv))
1983         chv_disable_pll(dev_priv, pipe);
1984     else
1985         vlv_disable_pll(dev_priv, pipe);
1986 }
1987 
1988 /* Only for pre-ILK configs */
1989 static void assert_pll(struct drm_i915_private *dev_priv,
1990                enum pipe pipe, bool state)
1991 {
1992     bool cur_state;
1993 
1994     cur_state = intel_de_read(dev_priv, DPLL(pipe)) & DPLL_VCO_ENABLE;
1995     I915_STATE_WARN(cur_state != state,
1996             "PLL state assertion failure (expected %s, current %s)\n",
1997             str_on_off(state), str_on_off(cur_state));
1998 }
1999 
2000 void assert_pll_enabled(struct drm_i915_private *i915, enum pipe pipe)
2001 {
2002     assert_pll(i915, pipe, true);
2003 }
2004 
2005 void assert_pll_disabled(struct drm_i915_private *i915, enum pipe pipe)
2006 {
2007     assert_pll(i915, pipe, false);
2008 }