atombios_crtc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/drm_crtc_helper.h>
  28. #include <drm/amdgpu_drm.h>
  29. #include <drm/drm_fixed.h>
  30. #include "amdgpu.h"
  31. #include "atom.h"
  32. #include "atom-bits.h"
  33. #include "atombios_encoders.h"
  34. #include "amdgpu_atombios.h"
  35. #include "amdgpu_pll.h"
  36. #include "amdgpu_connectors.h"
  37. void amdgpu_atombios_crtc_overscan_setup(struct drm_crtc *crtc,
  38. struct drm_display_mode *mode,
  39. struct drm_display_mode *adjusted_mode)
  40. {
  41. struct drm_device *dev = crtc->dev;
  42. struct amdgpu_device *adev = dev->dev_private;
  43. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  44. SET_CRTC_OVERSCAN_PS_ALLOCATION args;
  45. int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_OverScan);
  46. int a1, a2;
  47. memset(&args, 0, sizeof(args));
  48. args.ucCRTC = amdgpu_crtc->crtc_id;
  49. switch (amdgpu_crtc->rmx_type) {
  50. case RMX_CENTER:
  51. args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
  52. args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
  53. args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
  54. args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
  55. break;
  56. case RMX_ASPECT:
  57. a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
  58. a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
  59. if (a1 > a2) {
  60. args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
  61. args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
  62. } else if (a2 > a1) {
  63. args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
  64. args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
  65. }
  66. break;
  67. case RMX_FULL:
  68. default:
  69. args.usOverscanRight = cpu_to_le16(amdgpu_crtc->h_border);
  70. args.usOverscanLeft = cpu_to_le16(amdgpu_crtc->h_border);
  71. args.usOverscanBottom = cpu_to_le16(amdgpu_crtc->v_border);
  72. args.usOverscanTop = cpu_to_le16(amdgpu_crtc->v_border);
  73. break;
  74. }
  75. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  76. }
  77. void amdgpu_atombios_crtc_scaler_setup(struct drm_crtc *crtc)
  78. {
  79. struct drm_device *dev = crtc->dev;
  80. struct amdgpu_device *adev = dev->dev_private;
  81. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  82. ENABLE_SCALER_PS_ALLOCATION args;
  83. int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);
  84. memset(&args, 0, sizeof(args));
  85. args.ucScaler = amdgpu_crtc->crtc_id;
  86. switch (amdgpu_crtc->rmx_type) {
  87. case RMX_FULL:
  88. args.ucEnable = ATOM_SCALER_EXPANSION;
  89. break;
  90. case RMX_CENTER:
  91. args.ucEnable = ATOM_SCALER_CENTER;
  92. break;
  93. case RMX_ASPECT:
  94. args.ucEnable = ATOM_SCALER_EXPANSION;
  95. break;
  96. default:
  97. args.ucEnable = ATOM_SCALER_DISABLE;
  98. break;
  99. }
  100. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  101. }
  102. void amdgpu_atombios_crtc_lock(struct drm_crtc *crtc, int lock)
  103. {
  104. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  105. struct drm_device *dev = crtc->dev;
  106. struct amdgpu_device *adev = dev->dev_private;
  107. int index =
  108. GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
  109. ENABLE_CRTC_PS_ALLOCATION args;
  110. memset(&args, 0, sizeof(args));
  111. args.ucCRTC = amdgpu_crtc->crtc_id;
  112. args.ucEnable = lock;
  113. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  114. }
  115. void amdgpu_atombios_crtc_enable(struct drm_crtc *crtc, int state)
  116. {
  117. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  118. struct drm_device *dev = crtc->dev;
  119. struct amdgpu_device *adev = dev->dev_private;
  120. int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
  121. ENABLE_CRTC_PS_ALLOCATION args;
  122. memset(&args, 0, sizeof(args));
  123. args.ucCRTC = amdgpu_crtc->crtc_id;
  124. args.ucEnable = state;
  125. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  126. }
  127. void amdgpu_atombios_crtc_blank(struct drm_crtc *crtc, int state)
  128. {
  129. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  130. struct drm_device *dev = crtc->dev;
  131. struct amdgpu_device *adev = dev->dev_private;
  132. int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
  133. BLANK_CRTC_PS_ALLOCATION args;
  134. memset(&args, 0, sizeof(args));
  135. args.ucCRTC = amdgpu_crtc->crtc_id;
  136. args.ucBlanking = state;
  137. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  138. }
  139. void amdgpu_atombios_crtc_powergate(struct drm_crtc *crtc, int state)
  140. {
  141. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  142. struct drm_device *dev = crtc->dev;
  143. struct amdgpu_device *adev = dev->dev_private;
  144. int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
  145. ENABLE_DISP_POWER_GATING_PS_ALLOCATION args;
  146. memset(&args, 0, sizeof(args));
  147. args.ucDispPipeId = amdgpu_crtc->crtc_id;
  148. args.ucEnable = state;
  149. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  150. }
  151. void amdgpu_atombios_crtc_powergate_init(struct amdgpu_device *adev)
  152. {
  153. int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
  154. ENABLE_DISP_POWER_GATING_PS_ALLOCATION args;
  155. memset(&args, 0, sizeof(args));
  156. args.ucEnable = ATOM_INIT;
  157. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  158. }
  159. void amdgpu_atombios_crtc_set_dtd_timing(struct drm_crtc *crtc,
  160. struct drm_display_mode *mode)
  161. {
  162. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  163. struct drm_device *dev = crtc->dev;
  164. struct amdgpu_device *adev = dev->dev_private;
  165. SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
  166. int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
  167. u16 misc = 0;
  168. memset(&args, 0, sizeof(args));
  169. args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (amdgpu_crtc->h_border * 2));
  170. args.usH_Blanking_Time =
  171. cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (amdgpu_crtc->h_border * 2));
  172. args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (amdgpu_crtc->v_border * 2));
  173. args.usV_Blanking_Time =
  174. cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (amdgpu_crtc->v_border * 2));
  175. args.usH_SyncOffset =
  176. cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + amdgpu_crtc->h_border);
  177. args.usH_SyncWidth =
  178. cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
  179. args.usV_SyncOffset =
  180. cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + amdgpu_crtc->v_border);
  181. args.usV_SyncWidth =
  182. cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
  183. args.ucH_Border = amdgpu_crtc->h_border;
  184. args.ucV_Border = amdgpu_crtc->v_border;
  185. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  186. misc |= ATOM_VSYNC_POLARITY;
  187. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  188. misc |= ATOM_HSYNC_POLARITY;
  189. if (mode->flags & DRM_MODE_FLAG_CSYNC)
  190. misc |= ATOM_COMPOSITESYNC;
  191. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  192. misc |= ATOM_INTERLACE;
  193. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  194. misc |= ATOM_DOUBLE_CLOCK_MODE;
  195. args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
  196. args.ucCRTC = amdgpu_crtc->crtc_id;
  197. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  198. }
  199. union atom_enable_ss {
  200. ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
  201. ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
  202. ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
  203. };
  204. static void amdgpu_atombios_crtc_program_ss(struct amdgpu_device *adev,
  205. int enable,
  206. int pll_id,
  207. int crtc_id,
  208. struct amdgpu_atom_ss *ss)
  209. {
  210. unsigned i;
  211. int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
  212. union atom_enable_ss args;
  213. if (enable) {
  214. /* Don't mess with SS if percentage is 0 or external ss.
  215. * SS is already disabled previously, and disabling it
  216. * again can cause display problems if the pll is already
  217. * programmed.
  218. */
  219. if (ss->percentage == 0)
  220. return;
  221. if (ss->type & ATOM_EXTERNAL_SS_MASK)
  222. return;
  223. } else {
  224. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  225. if (adev->mode_info.crtcs[i] &&
  226. adev->mode_info.crtcs[i]->enabled &&
  227. i != crtc_id &&
  228. pll_id == adev->mode_info.crtcs[i]->pll_id) {
  229. /* one other crtc is using this pll don't turn
  230. * off spread spectrum as it might turn off
  231. * display on active crtc
  232. */
  233. return;
  234. }
  235. }
  236. }
  237. memset(&args, 0, sizeof(args));
  238. args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
  239. args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
  240. switch (pll_id) {
  241. case ATOM_PPLL1:
  242. args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
  243. break;
  244. case ATOM_PPLL2:
  245. args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
  246. break;
  247. case ATOM_DCPLL:
  248. args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
  249. break;
  250. case ATOM_PPLL_INVALID:
  251. return;
  252. }
  253. args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
  254. args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
  255. args.v3.ucEnable = enable;
  256. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  257. }
  258. union adjust_pixel_clock {
  259. ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
  260. ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
  261. };
  262. static u32 amdgpu_atombios_crtc_adjust_pll(struct drm_crtc *crtc,
  263. struct drm_display_mode *mode)
  264. {
  265. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  266. struct drm_device *dev = crtc->dev;
  267. struct amdgpu_device *adev = dev->dev_private;
  268. struct drm_encoder *encoder = amdgpu_crtc->encoder;
  269. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  270. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  271. u32 adjusted_clock = mode->clock;
  272. int encoder_mode = amdgpu_atombios_encoder_get_encoder_mode(encoder);
  273. u32 dp_clock = mode->clock;
  274. u32 clock = mode->clock;
  275. int bpc = amdgpu_crtc->bpc;
  276. bool is_duallink = amdgpu_dig_monitor_is_duallink(encoder, mode->clock);
  277. union adjust_pixel_clock args;
  278. u8 frev, crev;
  279. int index;
  280. amdgpu_crtc->pll_flags = AMDGPU_PLL_USE_FRAC_FB_DIV;
  281. if ((amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
  282. (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
  283. if (connector) {
  284. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  285. struct amdgpu_connector_atom_dig *dig_connector =
  286. amdgpu_connector->con_priv;
  287. dp_clock = dig_connector->dp_clock;
  288. }
  289. }
  290. /* use recommended ref_div for ss */
  291. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
  292. if (amdgpu_crtc->ss_enabled) {
  293. if (amdgpu_crtc->ss.refdiv) {
  294. amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_REF_DIV;
  295. amdgpu_crtc->pll_reference_div = amdgpu_crtc->ss.refdiv;
  296. amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_FRAC_FB_DIV;
  297. }
  298. }
  299. }
  300. /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
  301. if (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
  302. adjusted_clock = mode->clock * 2;
  303. if (amdgpu_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
  304. amdgpu_crtc->pll_flags |= AMDGPU_PLL_PREFER_CLOSEST_LOWER;
  305. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  306. amdgpu_crtc->pll_flags |= AMDGPU_PLL_IS_LCD;
  307. /* adjust pll for deep color modes */
  308. if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
  309. switch (bpc) {
  310. case 8:
  311. default:
  312. break;
  313. case 10:
  314. clock = (clock * 5) / 4;
  315. break;
  316. case 12:
  317. clock = (clock * 3) / 2;
  318. break;
  319. case 16:
  320. clock = clock * 2;
  321. break;
  322. }
  323. }
  324. /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
  325. * accordingly based on the encoder/transmitter to work around
  326. * special hw requirements.
  327. */
  328. index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
  329. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev,
  330. &crev))
  331. return adjusted_clock;
  332. memset(&args, 0, sizeof(args));
  333. switch (frev) {
  334. case 1:
  335. switch (crev) {
  336. case 1:
  337. case 2:
  338. args.v1.usPixelClock = cpu_to_le16(clock / 10);
  339. args.v1.ucTransmitterID = amdgpu_encoder->encoder_id;
  340. args.v1.ucEncodeMode = encoder_mode;
  341. if (amdgpu_crtc->ss_enabled && amdgpu_crtc->ss.percentage)
  342. args.v1.ucConfig |=
  343. ADJUST_DISPLAY_CONFIG_SS_ENABLE;
  344. amdgpu_atom_execute_table(adev->mode_info.atom_context,
  345. index, (uint32_t *)&args);
  346. adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
  347. break;
  348. case 3:
  349. args.v3.sInput.usPixelClock = cpu_to_le16(clock / 10);
  350. args.v3.sInput.ucTransmitterID = amdgpu_encoder->encoder_id;
  351. args.v3.sInput.ucEncodeMode = encoder_mode;
  352. args.v3.sInput.ucDispPllConfig = 0;
  353. if (amdgpu_crtc->ss_enabled && amdgpu_crtc->ss.percentage)
  354. args.v3.sInput.ucDispPllConfig |=
  355. DISPPLL_CONFIG_SS_ENABLE;
  356. if (ENCODER_MODE_IS_DP(encoder_mode)) {
  357. args.v3.sInput.ucDispPllConfig |=
  358. DISPPLL_CONFIG_COHERENT_MODE;
  359. /* 16200 or 27000 */
  360. args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
  361. } else if (amdgpu_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
  362. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  363. if (dig->coherent_mode)
  364. args.v3.sInput.ucDispPllConfig |=
  365. DISPPLL_CONFIG_COHERENT_MODE;
  366. if (is_duallink)
  367. args.v3.sInput.ucDispPllConfig |=
  368. DISPPLL_CONFIG_DUAL_LINK;
  369. }
  370. if (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
  371. ENCODER_OBJECT_ID_NONE)
  372. args.v3.sInput.ucExtTransmitterID =
  373. amdgpu_encoder_get_dp_bridge_encoder_id(encoder);
  374. else
  375. args.v3.sInput.ucExtTransmitterID = 0;
  376. amdgpu_atom_execute_table(adev->mode_info.atom_context,
  377. index, (uint32_t *)&args);
  378. adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
  379. if (args.v3.sOutput.ucRefDiv) {
  380. amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_FRAC_FB_DIV;
  381. amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_REF_DIV;
  382. amdgpu_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
  383. }
  384. if (args.v3.sOutput.ucPostDiv) {
  385. amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_FRAC_FB_DIV;
  386. amdgpu_crtc->pll_flags |= AMDGPU_PLL_USE_POST_DIV;
  387. amdgpu_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
  388. }
  389. break;
  390. default:
  391. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  392. return adjusted_clock;
  393. }
  394. break;
  395. default:
  396. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  397. return adjusted_clock;
  398. }
  399. return adjusted_clock;
  400. }
  401. union set_pixel_clock {
  402. SET_PIXEL_CLOCK_PS_ALLOCATION base;
  403. PIXEL_CLOCK_PARAMETERS v1;
  404. PIXEL_CLOCK_PARAMETERS_V2 v2;
  405. PIXEL_CLOCK_PARAMETERS_V3 v3;
  406. PIXEL_CLOCK_PARAMETERS_V5 v5;
  407. PIXEL_CLOCK_PARAMETERS_V6 v6;
  408. };
  409. /* on DCE5, make sure the voltage is high enough to support the
  410. * required disp clk.
  411. */
  412. void amdgpu_atombios_crtc_set_disp_eng_pll(struct amdgpu_device *adev,
  413. u32 dispclk)
  414. {
  415. u8 frev, crev;
  416. int index;
  417. union set_pixel_clock args;
  418. memset(&args, 0, sizeof(args));
  419. index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
  420. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev,
  421. &crev))
  422. return;
  423. switch (frev) {
  424. case 1:
  425. switch (crev) {
  426. case 5:
  427. /* if the default dcpll clock is specified,
  428. * SetPixelClock provides the dividers
  429. */
  430. args.v5.ucCRTC = ATOM_CRTC_INVALID;
  431. args.v5.usPixelClock = cpu_to_le16(dispclk);
  432. args.v5.ucPpll = ATOM_DCPLL;
  433. break;
  434. case 6:
  435. /* if the default dcpll clock is specified,
  436. * SetPixelClock provides the dividers
  437. */
  438. args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
  439. args.v6.ucPpll = ATOM_EXT_PLL1;
  440. break;
  441. default:
  442. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  443. return;
  444. }
  445. break;
  446. default:
  447. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  448. return;
  449. }
  450. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  451. }
  452. static bool is_pixel_clock_source_from_pll(u32 encoder_mode, int pll_id)
  453. {
  454. if (ENCODER_MODE_IS_DP(encoder_mode)) {
  455. if (pll_id < ATOM_EXT_PLL1)
  456. return true;
  457. else
  458. return false;
  459. } else {
  460. return true;
  461. }
  462. }
  463. void amdgpu_atombios_crtc_program_pll(struct drm_crtc *crtc,
  464. u32 crtc_id,
  465. int pll_id,
  466. u32 encoder_mode,
  467. u32 encoder_id,
  468. u32 clock,
  469. u32 ref_div,
  470. u32 fb_div,
  471. u32 frac_fb_div,
  472. u32 post_div,
  473. int bpc,
  474. bool ss_enabled,
  475. struct amdgpu_atom_ss *ss)
  476. {
  477. struct drm_device *dev = crtc->dev;
  478. struct amdgpu_device *adev = dev->dev_private;
  479. u8 frev, crev;
  480. int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
  481. union set_pixel_clock args;
  482. memset(&args, 0, sizeof(args));
  483. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev,
  484. &crev))
  485. return;
  486. switch (frev) {
  487. case 1:
  488. switch (crev) {
  489. case 1:
  490. if (clock == ATOM_DISABLE)
  491. return;
  492. args.v1.usPixelClock = cpu_to_le16(clock / 10);
  493. args.v1.usRefDiv = cpu_to_le16(ref_div);
  494. args.v1.usFbDiv = cpu_to_le16(fb_div);
  495. args.v1.ucFracFbDiv = frac_fb_div;
  496. args.v1.ucPostDiv = post_div;
  497. args.v1.ucPpll = pll_id;
  498. args.v1.ucCRTC = crtc_id;
  499. args.v1.ucRefDivSrc = 1;
  500. break;
  501. case 2:
  502. args.v2.usPixelClock = cpu_to_le16(clock / 10);
  503. args.v2.usRefDiv = cpu_to_le16(ref_div);
  504. args.v2.usFbDiv = cpu_to_le16(fb_div);
  505. args.v2.ucFracFbDiv = frac_fb_div;
  506. args.v2.ucPostDiv = post_div;
  507. args.v2.ucPpll = pll_id;
  508. args.v2.ucCRTC = crtc_id;
  509. args.v2.ucRefDivSrc = 1;
  510. break;
  511. case 3:
  512. args.v3.usPixelClock = cpu_to_le16(clock / 10);
  513. args.v3.usRefDiv = cpu_to_le16(ref_div);
  514. args.v3.usFbDiv = cpu_to_le16(fb_div);
  515. args.v3.ucFracFbDiv = frac_fb_div;
  516. args.v3.ucPostDiv = post_div;
  517. args.v3.ucPpll = pll_id;
  518. if (crtc_id == ATOM_CRTC2)
  519. args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
  520. else
  521. args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
  522. if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
  523. args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
  524. args.v3.ucTransmitterId = encoder_id;
  525. args.v3.ucEncoderMode = encoder_mode;
  526. break;
  527. case 5:
  528. args.v5.ucCRTC = crtc_id;
  529. args.v5.usPixelClock = cpu_to_le16(clock / 10);
  530. args.v5.ucRefDiv = ref_div;
  531. args.v5.usFbDiv = cpu_to_le16(fb_div);
  532. args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
  533. args.v5.ucPostDiv = post_div;
  534. args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
  535. if ((ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK)) &&
  536. (pll_id < ATOM_EXT_PLL1))
  537. args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
  538. if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
  539. switch (bpc) {
  540. case 8:
  541. default:
  542. args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
  543. break;
  544. case 10:
  545. /* yes this is correct, the atom define is wrong */
  546. args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_32BPP;
  547. break;
  548. case 12:
  549. /* yes this is correct, the atom define is wrong */
  550. args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
  551. break;
  552. }
  553. }
  554. args.v5.ucTransmitterID = encoder_id;
  555. args.v5.ucEncoderMode = encoder_mode;
  556. args.v5.ucPpll = pll_id;
  557. break;
  558. case 6:
  559. args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
  560. args.v6.ucRefDiv = ref_div;
  561. args.v6.usFbDiv = cpu_to_le16(fb_div);
  562. args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
  563. args.v6.ucPostDiv = post_div;
  564. args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
  565. if ((ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK)) &&
  566. (pll_id < ATOM_EXT_PLL1) &&
  567. !is_pixel_clock_source_from_pll(encoder_mode, pll_id))
  568. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
  569. if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
  570. switch (bpc) {
  571. case 8:
  572. default:
  573. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
  574. break;
  575. case 10:
  576. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP_V6;
  577. break;
  578. case 12:
  579. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP_V6;
  580. break;
  581. case 16:
  582. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
  583. break;
  584. }
  585. }
  586. args.v6.ucTransmitterID = encoder_id;
  587. args.v6.ucEncoderMode = encoder_mode;
  588. args.v6.ucPpll = pll_id;
  589. break;
  590. default:
  591. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  592. return;
  593. }
  594. break;
  595. default:
  596. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  597. return;
  598. }
  599. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  600. }
  601. int amdgpu_atombios_crtc_prepare_pll(struct drm_crtc *crtc,
  602. struct drm_display_mode *mode)
  603. {
  604. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  605. struct drm_device *dev = crtc->dev;
  606. struct amdgpu_device *adev = dev->dev_private;
  607. struct amdgpu_encoder *amdgpu_encoder =
  608. to_amdgpu_encoder(amdgpu_crtc->encoder);
  609. int encoder_mode = amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder);
  610. amdgpu_crtc->bpc = 8;
  611. amdgpu_crtc->ss_enabled = false;
  612. if ((amdgpu_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
  613. (amdgpu_encoder_get_dp_bridge_encoder_id(amdgpu_crtc->encoder) != ENCODER_OBJECT_ID_NONE)) {
  614. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  615. struct drm_connector *connector =
  616. amdgpu_get_connector_for_encoder(amdgpu_crtc->encoder);
  617. struct amdgpu_connector *amdgpu_connector =
  618. to_amdgpu_connector(connector);
  619. struct amdgpu_connector_atom_dig *dig_connector =
  620. amdgpu_connector->con_priv;
  621. int dp_clock;
  622. /* Assign mode clock for hdmi deep color max clock limit check */
  623. amdgpu_connector->pixelclock_for_modeset = mode->clock;
  624. amdgpu_crtc->bpc = amdgpu_connector_get_monitor_bpc(connector);
  625. switch (encoder_mode) {
  626. case ATOM_ENCODER_MODE_DP_MST:
  627. case ATOM_ENCODER_MODE_DP:
  628. /* DP/eDP */
  629. dp_clock = dig_connector->dp_clock / 10;
  630. amdgpu_crtc->ss_enabled =
  631. amdgpu_atombios_get_asic_ss_info(adev, &amdgpu_crtc->ss,
  632. ASIC_INTERNAL_SS_ON_DP,
  633. dp_clock);
  634. break;
  635. case ATOM_ENCODER_MODE_LVDS:
  636. amdgpu_crtc->ss_enabled =
  637. amdgpu_atombios_get_asic_ss_info(adev,
  638. &amdgpu_crtc->ss,
  639. dig->lcd_ss_id,
  640. mode->clock / 10);
  641. break;
  642. case ATOM_ENCODER_MODE_DVI:
  643. amdgpu_crtc->ss_enabled =
  644. amdgpu_atombios_get_asic_ss_info(adev,
  645. &amdgpu_crtc->ss,
  646. ASIC_INTERNAL_SS_ON_TMDS,
  647. mode->clock / 10);
  648. break;
  649. case ATOM_ENCODER_MODE_HDMI:
  650. amdgpu_crtc->ss_enabled =
  651. amdgpu_atombios_get_asic_ss_info(adev,
  652. &amdgpu_crtc->ss,
  653. ASIC_INTERNAL_SS_ON_HDMI,
  654. mode->clock / 10);
  655. break;
  656. default:
  657. break;
  658. }
  659. }
  660. /* adjust pixel clock as needed */
  661. amdgpu_crtc->adjusted_clock = amdgpu_atombios_crtc_adjust_pll(crtc, mode);
  662. return 0;
  663. }
  664. void amdgpu_atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
  665. {
  666. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  667. struct drm_device *dev = crtc->dev;
  668. struct amdgpu_device *adev = dev->dev_private;
  669. struct amdgpu_encoder *amdgpu_encoder =
  670. to_amdgpu_encoder(amdgpu_crtc->encoder);
  671. u32 pll_clock = mode->clock;
  672. u32 clock = mode->clock;
  673. u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
  674. struct amdgpu_pll *pll;
  675. int encoder_mode = amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder);
  676. /* pass the actual clock to amdgpu_atombios_crtc_program_pll for HDMI */
  677. if ((encoder_mode == ATOM_ENCODER_MODE_HDMI) &&
  678. (amdgpu_crtc->bpc > 8))
  679. clock = amdgpu_crtc->adjusted_clock;
  680. switch (amdgpu_crtc->pll_id) {
  681. case ATOM_PPLL1:
  682. pll = &adev->clock.ppll[0];
  683. break;
  684. case ATOM_PPLL2:
  685. pll = &adev->clock.ppll[1];
  686. break;
  687. case ATOM_PPLL0:
  688. case ATOM_PPLL_INVALID:
  689. default:
  690. pll = &adev->clock.ppll[2];
  691. break;
  692. }
  693. /* update pll params */
  694. pll->flags = amdgpu_crtc->pll_flags;
  695. pll->reference_div = amdgpu_crtc->pll_reference_div;
  696. pll->post_div = amdgpu_crtc->pll_post_div;
  697. amdgpu_pll_compute(pll, amdgpu_crtc->adjusted_clock, &pll_clock,
  698. &fb_div, &frac_fb_div, &ref_div, &post_div);
  699. amdgpu_atombios_crtc_program_ss(adev, ATOM_DISABLE, amdgpu_crtc->pll_id,
  700. amdgpu_crtc->crtc_id, &amdgpu_crtc->ss);
  701. amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
  702. encoder_mode, amdgpu_encoder->encoder_id, clock,
  703. ref_div, fb_div, frac_fb_div, post_div,
  704. amdgpu_crtc->bpc, amdgpu_crtc->ss_enabled, &amdgpu_crtc->ss);
  705. if (amdgpu_crtc->ss_enabled) {
  706. /* calculate ss amount and step size */
  707. u32 step_size;
  708. u32 amount = (((fb_div * 10) + frac_fb_div) *
  709. (u32)amdgpu_crtc->ss.percentage) /
  710. (100 * (u32)amdgpu_crtc->ss.percentage_divider);
  711. amdgpu_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
  712. amdgpu_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
  713. ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
  714. if (amdgpu_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
  715. step_size = (4 * amount * ref_div * ((u32)amdgpu_crtc->ss.rate * 2048)) /
  716. (125 * 25 * pll->reference_freq / 100);
  717. else
  718. step_size = (2 * amount * ref_div * ((u32)amdgpu_crtc->ss.rate * 2048)) /
  719. (125 * 25 * pll->reference_freq / 100);
  720. amdgpu_crtc->ss.step = step_size;
  721. amdgpu_atombios_crtc_program_ss(adev, ATOM_ENABLE, amdgpu_crtc->pll_id,
  722. amdgpu_crtc->crtc_id, &amdgpu_crtc->ss);
  723. }
  724. }