Home | History | Annotate | Line # | Download | only in amdgpu
amdgpu_pll.c revision 1.1.1.2
      1 /*	$NetBSD: amdgpu_pll.c,v 1.1.1.2 2021/12/18 20:11:10 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2014 Advanced Micro Devices, Inc.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  *
     24  */
     25 
     26 #include <sys/cdefs.h>
     27 __KERNEL_RCSID(0, "$NetBSD: amdgpu_pll.c,v 1.1.1.2 2021/12/18 20:11:10 riastradh Exp $");
     28 
     29 #include <drm/amdgpu_drm.h>
     30 #include "amdgpu.h"
     31 #include "atom.h"
     32 #include "atombios_encoders.h"
     33 #include "amdgpu_pll.h"
     34 #include <asm/div64.h>
     35 #include <linux/gcd.h>
     36 
     37 /**
     38  * amdgpu_pll_reduce_ratio - fractional number reduction
     39  *
     40  * @nom: nominator
     41  * @den: denominator
     42  * @nom_min: minimum value for nominator
     43  * @den_min: minimum value for denominator
     44  *
     45  * Find the greatest common divisor and apply it on both nominator and
     46  * denominator, but make nominator and denominator are at least as large
     47  * as their minimum values.
     48  */
     49 static void amdgpu_pll_reduce_ratio(unsigned *nom, unsigned *den,
     50 				    unsigned nom_min, unsigned den_min)
     51 {
     52 	unsigned tmp;
     53 
     54 	/* reduce the numbers to a simpler ratio */
     55 	tmp = gcd(*nom, *den);
     56 	*nom /= tmp;
     57 	*den /= tmp;
     58 
     59 	/* make sure nominator is large enough */
     60 	if (*nom < nom_min) {
     61 		tmp = DIV_ROUND_UP(nom_min, *nom);
     62 		*nom *= tmp;
     63 		*den *= tmp;
     64 	}
     65 
     66 	/* make sure the denominator is large enough */
     67 	if (*den < den_min) {
     68 		tmp = DIV_ROUND_UP(den_min, *den);
     69 		*nom *= tmp;
     70 		*den *= tmp;
     71 	}
     72 }
     73 
     74 /**
     75  * amdgpu_pll_get_fb_ref_div - feedback and ref divider calculation
     76  *
     77  * @nom: nominator
     78  * @den: denominator
     79  * @post_div: post divider
     80  * @fb_div_max: feedback divider maximum
     81  * @ref_div_max: reference divider maximum
     82  * @fb_div: resulting feedback divider
     83  * @ref_div: resulting reference divider
     84  *
     85  * Calculate feedback and reference divider for a given post divider. Makes
     86  * sure we stay within the limits.
     87  */
     88 static void amdgpu_pll_get_fb_ref_div(unsigned nom, unsigned den, unsigned post_div,
     89 				      unsigned fb_div_max, unsigned ref_div_max,
     90 				      unsigned *fb_div, unsigned *ref_div)
     91 {
     92 	/* limit reference * post divider to a maximum */
     93 	ref_div_max = min(128 / post_div, ref_div_max);
     94 
     95 	/* get matching reference and feedback divider */
     96 	*ref_div = min(max(DIV_ROUND_CLOSEST(den, post_div), 1u), ref_div_max);
     97 	*fb_div = DIV_ROUND_CLOSEST(nom * *ref_div * post_div, den);
     98 
     99 	/* limit fb divider to its maximum */
    100 	if (*fb_div > fb_div_max) {
    101 		*ref_div = DIV_ROUND_CLOSEST(*ref_div * fb_div_max, *fb_div);
    102 		*fb_div = fb_div_max;
    103 	}
    104 }
    105 
    106 /**
    107  * amdgpu_pll_compute - compute PLL paramaters
    108  *
    109  * @pll: information about the PLL
    110  * @dot_clock_p: resulting pixel clock
    111  * fb_div_p: resulting feedback divider
    112  * frac_fb_div_p: fractional part of the feedback divider
    113  * ref_div_p: resulting reference divider
    114  * post_div_p: resulting reference divider
    115  *
    116  * Try to calculate the PLL parameters to generate the given frequency:
    117  * dot_clock = (ref_freq * feedback_div) / (ref_div * post_div)
    118  */
    119 void amdgpu_pll_compute(struct amdgpu_pll *pll,
    120 			u32 freq,
    121 			u32 *dot_clock_p,
    122 			u32 *fb_div_p,
    123 			u32 *frac_fb_div_p,
    124 			u32 *ref_div_p,
    125 			u32 *post_div_p)
    126 {
    127 	unsigned target_clock = pll->flags & AMDGPU_PLL_USE_FRAC_FB_DIV ?
    128 		freq : freq / 10;
    129 
    130 	unsigned fb_div_min, fb_div_max, fb_div;
    131 	unsigned post_div_min, post_div_max, post_div;
    132 	unsigned ref_div_min, ref_div_max, ref_div;
    133 	unsigned post_div_best, diff_best;
    134 	unsigned nom, den;
    135 
    136 	/* determine allowed feedback divider range */
    137 	fb_div_min = pll->min_feedback_div;
    138 	fb_div_max = pll->max_feedback_div;
    139 
    140 	if (pll->flags & AMDGPU_PLL_USE_FRAC_FB_DIV) {
    141 		fb_div_min *= 10;
    142 		fb_div_max *= 10;
    143 	}
    144 
    145 	/* determine allowed ref divider range */
    146 	if (pll->flags & AMDGPU_PLL_USE_REF_DIV)
    147 		ref_div_min = pll->reference_div;
    148 	else
    149 		ref_div_min = pll->min_ref_div;
    150 
    151 	if (pll->flags & AMDGPU_PLL_USE_FRAC_FB_DIV &&
    152 	    pll->flags & AMDGPU_PLL_USE_REF_DIV)
    153 		ref_div_max = pll->reference_div;
    154 	else
    155 		ref_div_max = pll->max_ref_div;
    156 
    157 	/* determine allowed post divider range */
    158 	if (pll->flags & AMDGPU_PLL_USE_POST_DIV) {
    159 		post_div_min = pll->post_div;
    160 		post_div_max = pll->post_div;
    161 	} else {
    162 		unsigned vco_min, vco_max;
    163 
    164 		if (pll->flags & AMDGPU_PLL_IS_LCD) {
    165 			vco_min = pll->lcd_pll_out_min;
    166 			vco_max = pll->lcd_pll_out_max;
    167 		} else {
    168 			vco_min = pll->pll_out_min;
    169 			vco_max = pll->pll_out_max;
    170 		}
    171 
    172 		if (pll->flags & AMDGPU_PLL_USE_FRAC_FB_DIV) {
    173 			vco_min *= 10;
    174 			vco_max *= 10;
    175 		}
    176 
    177 		post_div_min = vco_min / target_clock;
    178 		if ((target_clock * post_div_min) < vco_min)
    179 			++post_div_min;
    180 		if (post_div_min < pll->min_post_div)
    181 			post_div_min = pll->min_post_div;
    182 
    183 		post_div_max = vco_max / target_clock;
    184 		if ((target_clock * post_div_max) > vco_max)
    185 			--post_div_max;
    186 		if (post_div_max > pll->max_post_div)
    187 			post_div_max = pll->max_post_div;
    188 	}
    189 
    190 	/* represent the searched ratio as fractional number */
    191 	nom = target_clock;
    192 	den = pll->reference_freq;
    193 
    194 	/* reduce the numbers to a simpler ratio */
    195 	amdgpu_pll_reduce_ratio(&nom, &den, fb_div_min, post_div_min);
    196 
    197 	/* now search for a post divider */
    198 	if (pll->flags & AMDGPU_PLL_PREFER_MINM_OVER_MAXP)
    199 		post_div_best = post_div_min;
    200 	else
    201 		post_div_best = post_div_max;
    202 	diff_best = ~0;
    203 
    204 	for (post_div = post_div_min; post_div <= post_div_max; ++post_div) {
    205 		unsigned diff;
    206 		amdgpu_pll_get_fb_ref_div(nom, den, post_div, fb_div_max,
    207 					  ref_div_max, &fb_div, &ref_div);
    208 		diff = abs(target_clock - (pll->reference_freq * fb_div) /
    209 			(ref_div * post_div));
    210 
    211 		if (diff < diff_best || (diff == diff_best &&
    212 		    !(pll->flags & AMDGPU_PLL_PREFER_MINM_OVER_MAXP))) {
    213 
    214 			post_div_best = post_div;
    215 			diff_best = diff;
    216 		}
    217 	}
    218 	post_div = post_div_best;
    219 
    220 	/* get the feedback and reference divider for the optimal value */
    221 	amdgpu_pll_get_fb_ref_div(nom, den, post_div, fb_div_max, ref_div_max,
    222 				  &fb_div, &ref_div);
    223 
    224 	/* reduce the numbers to a simpler ratio once more */
    225 	/* this also makes sure that the reference divider is large enough */
    226 	amdgpu_pll_reduce_ratio(&fb_div, &ref_div, fb_div_min, ref_div_min);
    227 
    228 	/* avoid high jitter with small fractional dividers */
    229 	if (pll->flags & AMDGPU_PLL_USE_FRAC_FB_DIV && (fb_div % 10)) {
    230 		fb_div_min = max(fb_div_min, (9 - (fb_div % 10)) * 20 + 60);
    231 		if (fb_div < fb_div_min) {
    232 			unsigned tmp = DIV_ROUND_UP(fb_div_min, fb_div);
    233 			fb_div *= tmp;
    234 			ref_div *= tmp;
    235 		}
    236 	}
    237 
    238 	/* and finally save the result */
    239 	if (pll->flags & AMDGPU_PLL_USE_FRAC_FB_DIV) {
    240 		*fb_div_p = fb_div / 10;
    241 		*frac_fb_div_p = fb_div % 10;
    242 	} else {
    243 		*fb_div_p = fb_div;
    244 		*frac_fb_div_p = 0;
    245 	}
    246 
    247 	*dot_clock_p = ((pll->reference_freq * *fb_div_p * 10) +
    248 			(pll->reference_freq * *frac_fb_div_p)) /
    249 		       (ref_div * post_div * 10);
    250 	*ref_div_p = ref_div;
    251 	*post_div_p = post_div;
    252 
    253 	DRM_DEBUG_KMS("%d - %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
    254 		      freq, *dot_clock_p * 10, *fb_div_p, *frac_fb_div_p,
    255 		      ref_div, post_div);
    256 }
    257 
    258 /**
    259  * amdgpu_pll_get_use_mask - look up a mask of which pplls are in use
    260  *
    261  * @crtc: drm crtc
    262  *
    263  * Returns the mask of which PPLLs (Pixel PLLs) are in use.
    264  */
    265 u32 amdgpu_pll_get_use_mask(struct drm_crtc *crtc)
    266 {
    267 	struct drm_device *dev = crtc->dev;
    268 	struct drm_crtc *test_crtc;
    269 	struct amdgpu_crtc *test_amdgpu_crtc;
    270 	u32 pll_in_use = 0;
    271 
    272 	list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
    273 		if (crtc == test_crtc)
    274 			continue;
    275 
    276 		test_amdgpu_crtc = to_amdgpu_crtc(test_crtc);
    277 		if (test_amdgpu_crtc->pll_id != ATOM_PPLL_INVALID)
    278 			pll_in_use |= (1 << test_amdgpu_crtc->pll_id);
    279 	}
    280 	return pll_in_use;
    281 }
    282 
    283 /**
    284  * amdgpu_pll_get_shared_dp_ppll - return the PPLL used by another crtc for DP
    285  *
    286  * @crtc: drm crtc
    287  *
    288  * Returns the PPLL (Pixel PLL) used by another crtc/encoder which is
    289  * also in DP mode.  For DP, a single PPLL can be used for all DP
    290  * crtcs/encoders.
    291  */
    292 int amdgpu_pll_get_shared_dp_ppll(struct drm_crtc *crtc)
    293 {
    294 	struct drm_device *dev = crtc->dev;
    295 	struct drm_crtc *test_crtc;
    296 	struct amdgpu_crtc *test_amdgpu_crtc;
    297 
    298 	list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
    299 		if (crtc == test_crtc)
    300 			continue;
    301 		test_amdgpu_crtc = to_amdgpu_crtc(test_crtc);
    302 		if (test_amdgpu_crtc->encoder &&
    303 		    ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(test_amdgpu_crtc->encoder))) {
    304 			/* for DP use the same PLL for all */
    305 			if (test_amdgpu_crtc->pll_id != ATOM_PPLL_INVALID)
    306 				return test_amdgpu_crtc->pll_id;
    307 		}
    308 	}
    309 	return ATOM_PPLL_INVALID;
    310 }
    311 
    312 /**
    313  * amdgpu_pll_get_shared_nondp_ppll - return the PPLL used by another non-DP crtc
    314  *
    315  * @crtc: drm crtc
    316  * @encoder: drm encoder
    317  *
    318  * Returns the PPLL (Pixel PLL) used by another non-DP crtc/encoder which can
    319  * be shared (i.e., same clock).
    320  */
    321 int amdgpu_pll_get_shared_nondp_ppll(struct drm_crtc *crtc)
    322 {
    323 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
    324 	struct drm_device *dev = crtc->dev;
    325 	struct drm_crtc *test_crtc;
    326 	struct amdgpu_crtc *test_amdgpu_crtc;
    327 	u32 adjusted_clock, test_adjusted_clock;
    328 
    329 	adjusted_clock = amdgpu_crtc->adjusted_clock;
    330 
    331 	if (adjusted_clock == 0)
    332 		return ATOM_PPLL_INVALID;
    333 
    334 	list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
    335 		if (crtc == test_crtc)
    336 			continue;
    337 		test_amdgpu_crtc = to_amdgpu_crtc(test_crtc);
    338 		if (test_amdgpu_crtc->encoder &&
    339 		    !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(test_amdgpu_crtc->encoder))) {
    340 			/* check if we are already driving this connector with another crtc */
    341 			if (test_amdgpu_crtc->connector == amdgpu_crtc->connector) {
    342 				/* if we are, return that pll */
    343 				if (test_amdgpu_crtc->pll_id != ATOM_PPLL_INVALID)
    344 					return test_amdgpu_crtc->pll_id;
    345 			}
    346 			/* for non-DP check the clock */
    347 			test_adjusted_clock = test_amdgpu_crtc->adjusted_clock;
    348 			if ((crtc->mode.clock == test_crtc->mode.clock) &&
    349 			    (adjusted_clock == test_adjusted_clock) &&
    350 			    (amdgpu_crtc->ss_enabled == test_amdgpu_crtc->ss_enabled) &&
    351 			    (test_amdgpu_crtc->pll_id != ATOM_PPLL_INVALID))
    352 				return test_amdgpu_crtc->pll_id;
    353 		}
    354 	}
    355 	return ATOM_PPLL_INVALID;
    356 }
    357