amdgpu_pll.c 10 KB

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