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