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