atombios_crtc.c 28 KB

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