atombios_crtc.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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/radeon_drm.h>
  29. #include <drm/drm_fixed.h>
  30. #include "radeon.h"
  31. #include "atom.h"
  32. #include "atom-bits.h"
  33. static void atombios_overscan_setup(struct drm_crtc *crtc,
  34. struct drm_display_mode *mode,
  35. struct drm_display_mode *adjusted_mode)
  36. {
  37. struct drm_device *dev = crtc->dev;
  38. struct radeon_device *rdev = dev->dev_private;
  39. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  40. SET_CRTC_OVERSCAN_PS_ALLOCATION args;
  41. int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_OverScan);
  42. int a1, a2;
  43. memset(&args, 0, sizeof(args));
  44. args.ucCRTC = radeon_crtc->crtc_id;
  45. switch (radeon_crtc->rmx_type) {
  46. case RMX_CENTER:
  47. args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
  48. args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - mode->crtc_vdisplay) / 2);
  49. args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
  50. args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - mode->crtc_hdisplay) / 2);
  51. break;
  52. case RMX_ASPECT:
  53. a1 = mode->crtc_vdisplay * adjusted_mode->crtc_hdisplay;
  54. a2 = adjusted_mode->crtc_vdisplay * mode->crtc_hdisplay;
  55. if (a1 > a2) {
  56. args.usOverscanLeft = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
  57. args.usOverscanRight = cpu_to_le16((adjusted_mode->crtc_hdisplay - (a2 / mode->crtc_vdisplay)) / 2);
  58. } else if (a2 > a1) {
  59. args.usOverscanTop = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
  60. args.usOverscanBottom = cpu_to_le16((adjusted_mode->crtc_vdisplay - (a1 / mode->crtc_hdisplay)) / 2);
  61. }
  62. break;
  63. case RMX_FULL:
  64. default:
  65. args.usOverscanRight = cpu_to_le16(radeon_crtc->h_border);
  66. args.usOverscanLeft = cpu_to_le16(radeon_crtc->h_border);
  67. args.usOverscanBottom = cpu_to_le16(radeon_crtc->v_border);
  68. args.usOverscanTop = cpu_to_le16(radeon_crtc->v_border);
  69. break;
  70. }
  71. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  72. }
  73. static void atombios_scaler_setup(struct drm_crtc *crtc)
  74. {
  75. struct drm_device *dev = crtc->dev;
  76. struct radeon_device *rdev = dev->dev_private;
  77. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  78. ENABLE_SCALER_PS_ALLOCATION args;
  79. int index = GetIndexIntoMasterTable(COMMAND, EnableScaler);
  80. /* fixme - fill in enc_priv for atom dac */
  81. enum radeon_tv_std tv_std = TV_STD_NTSC;
  82. bool is_tv = false, is_cv = false;
  83. struct drm_encoder *encoder;
  84. if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
  85. return;
  86. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  87. /* find tv std */
  88. if (encoder->crtc == crtc) {
  89. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  90. if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
  91. struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
  92. tv_std = tv_dac->tv_std;
  93. is_tv = true;
  94. }
  95. }
  96. }
  97. memset(&args, 0, sizeof(args));
  98. args.ucScaler = radeon_crtc->crtc_id;
  99. if (is_tv) {
  100. switch (tv_std) {
  101. case TV_STD_NTSC:
  102. default:
  103. args.ucTVStandard = ATOM_TV_NTSC;
  104. break;
  105. case TV_STD_PAL:
  106. args.ucTVStandard = ATOM_TV_PAL;
  107. break;
  108. case TV_STD_PAL_M:
  109. args.ucTVStandard = ATOM_TV_PALM;
  110. break;
  111. case TV_STD_PAL_60:
  112. args.ucTVStandard = ATOM_TV_PAL60;
  113. break;
  114. case TV_STD_NTSC_J:
  115. args.ucTVStandard = ATOM_TV_NTSCJ;
  116. break;
  117. case TV_STD_SCART_PAL:
  118. args.ucTVStandard = ATOM_TV_PAL; /* ??? */
  119. break;
  120. case TV_STD_SECAM:
  121. args.ucTVStandard = ATOM_TV_SECAM;
  122. break;
  123. case TV_STD_PAL_CN:
  124. args.ucTVStandard = ATOM_TV_PALCN;
  125. break;
  126. }
  127. args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
  128. } else if (is_cv) {
  129. args.ucTVStandard = ATOM_TV_CV;
  130. args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
  131. } else {
  132. switch (radeon_crtc->rmx_type) {
  133. case RMX_FULL:
  134. args.ucEnable = ATOM_SCALER_EXPANSION;
  135. break;
  136. case RMX_CENTER:
  137. args.ucEnable = ATOM_SCALER_CENTER;
  138. break;
  139. case RMX_ASPECT:
  140. args.ucEnable = ATOM_SCALER_EXPANSION;
  141. break;
  142. default:
  143. if (ASIC_IS_AVIVO(rdev))
  144. args.ucEnable = ATOM_SCALER_DISABLE;
  145. else
  146. args.ucEnable = ATOM_SCALER_CENTER;
  147. break;
  148. }
  149. }
  150. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  151. if ((is_tv || is_cv)
  152. && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
  153. atom_rv515_force_tv_scaler(rdev, radeon_crtc);
  154. }
  155. }
  156. static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
  157. {
  158. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  159. struct drm_device *dev = crtc->dev;
  160. struct radeon_device *rdev = dev->dev_private;
  161. int index =
  162. GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
  163. ENABLE_CRTC_PS_ALLOCATION args;
  164. memset(&args, 0, sizeof(args));
  165. args.ucCRTC = radeon_crtc->crtc_id;
  166. args.ucEnable = lock;
  167. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  168. }
  169. static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
  170. {
  171. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  172. struct drm_device *dev = crtc->dev;
  173. struct radeon_device *rdev = dev->dev_private;
  174. int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
  175. ENABLE_CRTC_PS_ALLOCATION args;
  176. memset(&args, 0, sizeof(args));
  177. args.ucCRTC = radeon_crtc->crtc_id;
  178. args.ucEnable = state;
  179. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  180. }
  181. static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
  182. {
  183. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  184. struct drm_device *dev = crtc->dev;
  185. struct radeon_device *rdev = dev->dev_private;
  186. int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
  187. ENABLE_CRTC_PS_ALLOCATION args;
  188. memset(&args, 0, sizeof(args));
  189. args.ucCRTC = radeon_crtc->crtc_id;
  190. args.ucEnable = state;
  191. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  192. }
  193. static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
  194. {
  195. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  196. struct drm_device *dev = crtc->dev;
  197. struct radeon_device *rdev = dev->dev_private;
  198. int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
  199. BLANK_CRTC_PS_ALLOCATION args;
  200. memset(&args, 0, sizeof(args));
  201. args.ucCRTC = radeon_crtc->crtc_id;
  202. args.ucBlanking = state;
  203. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  204. }
  205. void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
  206. {
  207. struct drm_device *dev = crtc->dev;
  208. struct radeon_device *rdev = dev->dev_private;
  209. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  210. switch (mode) {
  211. case DRM_MODE_DPMS_ON:
  212. radeon_crtc->enabled = true;
  213. /* adjust pm to dpms changes BEFORE enabling crtcs */
  214. radeon_pm_compute_clocks(rdev);
  215. atombios_enable_crtc(crtc, ATOM_ENABLE);
  216. if (ASIC_IS_DCE3(rdev))
  217. atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
  218. atombios_blank_crtc(crtc, ATOM_DISABLE);
  219. drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
  220. radeon_crtc_load_lut(crtc);
  221. break;
  222. case DRM_MODE_DPMS_STANDBY:
  223. case DRM_MODE_DPMS_SUSPEND:
  224. case DRM_MODE_DPMS_OFF:
  225. drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
  226. if (radeon_crtc->enabled)
  227. atombios_blank_crtc(crtc, ATOM_ENABLE);
  228. if (ASIC_IS_DCE3(rdev))
  229. atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
  230. atombios_enable_crtc(crtc, ATOM_DISABLE);
  231. radeon_crtc->enabled = false;
  232. /* adjust pm to dpms changes AFTER disabling crtcs */
  233. radeon_pm_compute_clocks(rdev);
  234. break;
  235. }
  236. }
  237. static void
  238. atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
  239. struct drm_display_mode *mode)
  240. {
  241. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  242. struct drm_device *dev = crtc->dev;
  243. struct radeon_device *rdev = dev->dev_private;
  244. SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
  245. int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
  246. u16 misc = 0;
  247. memset(&args, 0, sizeof(args));
  248. args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
  249. args.usH_Blanking_Time =
  250. cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
  251. args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
  252. args.usV_Blanking_Time =
  253. cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
  254. args.usH_SyncOffset =
  255. cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
  256. args.usH_SyncWidth =
  257. cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
  258. args.usV_SyncOffset =
  259. cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
  260. args.usV_SyncWidth =
  261. cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
  262. args.ucH_Border = radeon_crtc->h_border;
  263. args.ucV_Border = radeon_crtc->v_border;
  264. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  265. misc |= ATOM_VSYNC_POLARITY;
  266. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  267. misc |= ATOM_HSYNC_POLARITY;
  268. if (mode->flags & DRM_MODE_FLAG_CSYNC)
  269. misc |= ATOM_COMPOSITESYNC;
  270. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  271. misc |= ATOM_INTERLACE;
  272. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  273. misc |= ATOM_DOUBLE_CLOCK_MODE;
  274. args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
  275. args.ucCRTC = radeon_crtc->crtc_id;
  276. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  277. }
  278. static void atombios_crtc_set_timing(struct drm_crtc *crtc,
  279. struct drm_display_mode *mode)
  280. {
  281. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  282. struct drm_device *dev = crtc->dev;
  283. struct radeon_device *rdev = dev->dev_private;
  284. SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
  285. int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
  286. u16 misc = 0;
  287. memset(&args, 0, sizeof(args));
  288. args.usH_Total = cpu_to_le16(mode->crtc_htotal);
  289. args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
  290. args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
  291. args.usH_SyncWidth =
  292. cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
  293. args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
  294. args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
  295. args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
  296. args.usV_SyncWidth =
  297. cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
  298. args.ucOverscanRight = radeon_crtc->h_border;
  299. args.ucOverscanLeft = radeon_crtc->h_border;
  300. args.ucOverscanBottom = radeon_crtc->v_border;
  301. args.ucOverscanTop = radeon_crtc->v_border;
  302. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  303. misc |= ATOM_VSYNC_POLARITY;
  304. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  305. misc |= ATOM_HSYNC_POLARITY;
  306. if (mode->flags & DRM_MODE_FLAG_CSYNC)
  307. misc |= ATOM_COMPOSITESYNC;
  308. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  309. misc |= ATOM_INTERLACE;
  310. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  311. misc |= ATOM_DOUBLE_CLOCK_MODE;
  312. args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
  313. args.ucCRTC = radeon_crtc->crtc_id;
  314. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  315. }
  316. static void atombios_disable_ss(struct drm_crtc *crtc)
  317. {
  318. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  319. struct drm_device *dev = crtc->dev;
  320. struct radeon_device *rdev = dev->dev_private;
  321. u32 ss_cntl;
  322. if (ASIC_IS_DCE4(rdev)) {
  323. switch (radeon_crtc->pll_id) {
  324. case ATOM_PPLL1:
  325. ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
  326. ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
  327. WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
  328. break;
  329. case ATOM_PPLL2:
  330. ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
  331. ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
  332. WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
  333. break;
  334. case ATOM_DCPLL:
  335. case ATOM_PPLL_INVALID:
  336. return;
  337. }
  338. } else if (ASIC_IS_AVIVO(rdev)) {
  339. switch (radeon_crtc->pll_id) {
  340. case ATOM_PPLL1:
  341. ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
  342. ss_cntl &= ~1;
  343. WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
  344. break;
  345. case ATOM_PPLL2:
  346. ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
  347. ss_cntl &= ~1;
  348. WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
  349. break;
  350. case ATOM_DCPLL:
  351. case ATOM_PPLL_INVALID:
  352. return;
  353. }
  354. }
  355. }
  356. union atom_enable_ss {
  357. ENABLE_LVDS_SS_PARAMETERS lvds_ss;
  358. ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
  359. ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
  360. ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
  361. ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
  362. };
  363. static void atombios_crtc_program_ss(struct drm_crtc *crtc,
  364. int enable,
  365. int pll_id,
  366. struct radeon_atom_ss *ss)
  367. {
  368. struct drm_device *dev = crtc->dev;
  369. struct radeon_device *rdev = dev->dev_private;
  370. int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
  371. union atom_enable_ss args;
  372. memset(&args, 0, sizeof(args));
  373. if (ASIC_IS_DCE5(rdev)) {
  374. args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
  375. args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
  376. switch (pll_id) {
  377. case ATOM_PPLL1:
  378. args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
  379. args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
  380. args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
  381. break;
  382. case ATOM_PPLL2:
  383. args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
  384. args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
  385. args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
  386. break;
  387. case ATOM_DCPLL:
  388. args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
  389. args.v3.usSpreadSpectrumAmount = cpu_to_le16(0);
  390. args.v3.usSpreadSpectrumStep = cpu_to_le16(0);
  391. break;
  392. case ATOM_PPLL_INVALID:
  393. return;
  394. }
  395. args.v3.ucEnable = enable;
  396. if ((ss->percentage == 0) || (ss->type & ATOM_EXTERNAL_SS_MASK))
  397. args.v3.ucEnable = ATOM_DISABLE;
  398. } else if (ASIC_IS_DCE4(rdev)) {
  399. args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
  400. args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
  401. switch (pll_id) {
  402. case ATOM_PPLL1:
  403. args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
  404. args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
  405. args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
  406. break;
  407. case ATOM_PPLL2:
  408. args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
  409. args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
  410. args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
  411. break;
  412. case ATOM_DCPLL:
  413. args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
  414. args.v2.usSpreadSpectrumAmount = cpu_to_le16(0);
  415. args.v2.usSpreadSpectrumStep = cpu_to_le16(0);
  416. break;
  417. case ATOM_PPLL_INVALID:
  418. return;
  419. }
  420. args.v2.ucEnable = enable;
  421. if ((ss->percentage == 0) || (ss->type & ATOM_EXTERNAL_SS_MASK))
  422. args.v2.ucEnable = ATOM_DISABLE;
  423. } else if (ASIC_IS_DCE3(rdev)) {
  424. args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
  425. args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
  426. args.v1.ucSpreadSpectrumStep = ss->step;
  427. args.v1.ucSpreadSpectrumDelay = ss->delay;
  428. args.v1.ucSpreadSpectrumRange = ss->range;
  429. args.v1.ucPpll = pll_id;
  430. args.v1.ucEnable = enable;
  431. } else if (ASIC_IS_AVIVO(rdev)) {
  432. if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
  433. (ss->type & ATOM_EXTERNAL_SS_MASK)) {
  434. atombios_disable_ss(crtc);
  435. return;
  436. }
  437. args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
  438. args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
  439. args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
  440. args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
  441. args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
  442. args.lvds_ss_2.ucEnable = enable;
  443. } else {
  444. if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
  445. (ss->type & ATOM_EXTERNAL_SS_MASK)) {
  446. atombios_disable_ss(crtc);
  447. return;
  448. }
  449. args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
  450. args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
  451. args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
  452. args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
  453. args.lvds_ss.ucEnable = enable;
  454. }
  455. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  456. }
  457. union adjust_pixel_clock {
  458. ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
  459. ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
  460. };
  461. static u32 atombios_adjust_pll(struct drm_crtc *crtc,
  462. struct drm_display_mode *mode,
  463. struct radeon_pll *pll,
  464. bool ss_enabled,
  465. struct radeon_atom_ss *ss)
  466. {
  467. struct drm_device *dev = crtc->dev;
  468. struct radeon_device *rdev = dev->dev_private;
  469. struct drm_encoder *encoder = NULL;
  470. struct radeon_encoder *radeon_encoder = NULL;
  471. struct drm_connector *connector = NULL;
  472. u32 adjusted_clock = mode->clock;
  473. int encoder_mode = 0;
  474. u32 dp_clock = mode->clock;
  475. int bpc = 8;
  476. /* reset the pll flags */
  477. pll->flags = 0;
  478. if (ASIC_IS_AVIVO(rdev)) {
  479. if ((rdev->family == CHIP_RS600) ||
  480. (rdev->family == CHIP_RS690) ||
  481. (rdev->family == CHIP_RS740))
  482. pll->flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
  483. RADEON_PLL_PREFER_CLOSEST_LOWER);
  484. if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
  485. pll->flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
  486. else
  487. pll->flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
  488. if (rdev->family < CHIP_RV770)
  489. pll->flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
  490. } else {
  491. pll->flags |= RADEON_PLL_LEGACY;
  492. if (mode->clock > 200000) /* range limits??? */
  493. pll->flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
  494. else
  495. pll->flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
  496. }
  497. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  498. if (encoder->crtc == crtc) {
  499. radeon_encoder = to_radeon_encoder(encoder);
  500. connector = radeon_get_connector_for_encoder(encoder);
  501. if (connector)
  502. bpc = connector->display_info.bpc;
  503. encoder_mode = atombios_get_encoder_mode(encoder);
  504. if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
  505. radeon_encoder_is_dp_bridge(encoder)) {
  506. if (connector) {
  507. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  508. struct radeon_connector_atom_dig *dig_connector =
  509. radeon_connector->con_priv;
  510. dp_clock = dig_connector->dp_clock;
  511. }
  512. }
  513. /* use recommended ref_div for ss */
  514. if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
  515. if (ss_enabled) {
  516. if (ss->refdiv) {
  517. pll->flags |= RADEON_PLL_USE_REF_DIV;
  518. pll->reference_div = ss->refdiv;
  519. if (ASIC_IS_AVIVO(rdev))
  520. pll->flags |= RADEON_PLL_USE_FRAC_FB_DIV;
  521. }
  522. }
  523. }
  524. if (ASIC_IS_AVIVO(rdev)) {
  525. /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
  526. if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
  527. adjusted_clock = mode->clock * 2;
  528. if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
  529. pll->flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
  530. if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  531. pll->flags |= RADEON_PLL_IS_LCD;
  532. } else {
  533. if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
  534. pll->flags |= RADEON_PLL_NO_ODD_POST_DIV;
  535. if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
  536. pll->flags |= RADEON_PLL_USE_REF_DIV;
  537. }
  538. break;
  539. }
  540. }
  541. /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
  542. * accordingly based on the encoder/transmitter to work around
  543. * special hw requirements.
  544. */
  545. if (ASIC_IS_DCE3(rdev)) {
  546. union adjust_pixel_clock args;
  547. u8 frev, crev;
  548. int index;
  549. index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
  550. if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
  551. &crev))
  552. return adjusted_clock;
  553. memset(&args, 0, sizeof(args));
  554. switch (frev) {
  555. case 1:
  556. switch (crev) {
  557. case 1:
  558. case 2:
  559. args.v1.usPixelClock = cpu_to_le16(mode->clock / 10);
  560. args.v1.ucTransmitterID = radeon_encoder->encoder_id;
  561. args.v1.ucEncodeMode = encoder_mode;
  562. if (ss_enabled && ss->percentage)
  563. args.v1.ucConfig |=
  564. ADJUST_DISPLAY_CONFIG_SS_ENABLE;
  565. atom_execute_table(rdev->mode_info.atom_context,
  566. index, (uint32_t *)&args);
  567. adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
  568. break;
  569. case 3:
  570. args.v3.sInput.usPixelClock = cpu_to_le16(mode->clock / 10);
  571. args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
  572. args.v3.sInput.ucEncodeMode = encoder_mode;
  573. args.v3.sInput.ucDispPllConfig = 0;
  574. if (ss_enabled && ss->percentage)
  575. args.v3.sInput.ucDispPllConfig |=
  576. DISPPLL_CONFIG_SS_ENABLE;
  577. if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT) ||
  578. radeon_encoder_is_dp_bridge(encoder)) {
  579. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  580. if (encoder_mode == ATOM_ENCODER_MODE_DP) {
  581. args.v3.sInput.ucDispPllConfig |=
  582. DISPPLL_CONFIG_COHERENT_MODE;
  583. /* 16200 or 27000 */
  584. args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
  585. } else {
  586. if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
  587. /* deep color support */
  588. args.v3.sInput.usPixelClock =
  589. cpu_to_le16((mode->clock * bpc / 8) / 10);
  590. }
  591. if (dig->coherent_mode)
  592. args.v3.sInput.ucDispPllConfig |=
  593. DISPPLL_CONFIG_COHERENT_MODE;
  594. if (mode->clock > 165000)
  595. args.v3.sInput.ucDispPllConfig |=
  596. DISPPLL_CONFIG_DUAL_LINK;
  597. }
  598. } else if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
  599. if (encoder_mode == ATOM_ENCODER_MODE_DP) {
  600. args.v3.sInput.ucDispPllConfig |=
  601. DISPPLL_CONFIG_COHERENT_MODE;
  602. /* 16200 or 27000 */
  603. args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
  604. } else if (encoder_mode != ATOM_ENCODER_MODE_LVDS) {
  605. if (mode->clock > 165000)
  606. args.v3.sInput.ucDispPllConfig |=
  607. DISPPLL_CONFIG_DUAL_LINK;
  608. }
  609. }
  610. atom_execute_table(rdev->mode_info.atom_context,
  611. index, (uint32_t *)&args);
  612. adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
  613. if (args.v3.sOutput.ucRefDiv) {
  614. pll->flags |= RADEON_PLL_USE_FRAC_FB_DIV;
  615. pll->flags |= RADEON_PLL_USE_REF_DIV;
  616. pll->reference_div = args.v3.sOutput.ucRefDiv;
  617. }
  618. if (args.v3.sOutput.ucPostDiv) {
  619. pll->flags |= RADEON_PLL_USE_FRAC_FB_DIV;
  620. pll->flags |= RADEON_PLL_USE_POST_DIV;
  621. pll->post_div = args.v3.sOutput.ucPostDiv;
  622. }
  623. break;
  624. default:
  625. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  626. return adjusted_clock;
  627. }
  628. break;
  629. default:
  630. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  631. return adjusted_clock;
  632. }
  633. }
  634. return adjusted_clock;
  635. }
  636. union set_pixel_clock {
  637. SET_PIXEL_CLOCK_PS_ALLOCATION base;
  638. PIXEL_CLOCK_PARAMETERS v1;
  639. PIXEL_CLOCK_PARAMETERS_V2 v2;
  640. PIXEL_CLOCK_PARAMETERS_V3 v3;
  641. PIXEL_CLOCK_PARAMETERS_V5 v5;
  642. PIXEL_CLOCK_PARAMETERS_V6 v6;
  643. };
  644. /* on DCE5, make sure the voltage is high enough to support the
  645. * required disp clk.
  646. */
  647. static void atombios_crtc_set_dcpll(struct drm_crtc *crtc,
  648. u32 dispclk)
  649. {
  650. struct drm_device *dev = crtc->dev;
  651. struct radeon_device *rdev = dev->dev_private;
  652. u8 frev, crev;
  653. int index;
  654. union set_pixel_clock args;
  655. memset(&args, 0, sizeof(args));
  656. index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
  657. if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
  658. &crev))
  659. return;
  660. switch (frev) {
  661. case 1:
  662. switch (crev) {
  663. case 5:
  664. /* if the default dcpll clock is specified,
  665. * SetPixelClock provides the dividers
  666. */
  667. args.v5.ucCRTC = ATOM_CRTC_INVALID;
  668. args.v5.usPixelClock = cpu_to_le16(dispclk);
  669. args.v5.ucPpll = ATOM_DCPLL;
  670. break;
  671. case 6:
  672. /* if the default dcpll clock is specified,
  673. * SetPixelClock provides the dividers
  674. */
  675. args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
  676. args.v6.ucPpll = ATOM_DCPLL;
  677. break;
  678. default:
  679. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  680. return;
  681. }
  682. break;
  683. default:
  684. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  685. return;
  686. }
  687. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  688. }
  689. static void atombios_crtc_program_pll(struct drm_crtc *crtc,
  690. int crtc_id,
  691. int pll_id,
  692. u32 encoder_mode,
  693. u32 encoder_id,
  694. u32 clock,
  695. u32 ref_div,
  696. u32 fb_div,
  697. u32 frac_fb_div,
  698. u32 post_div,
  699. int bpc,
  700. bool ss_enabled,
  701. struct radeon_atom_ss *ss)
  702. {
  703. struct drm_device *dev = crtc->dev;
  704. struct radeon_device *rdev = dev->dev_private;
  705. u8 frev, crev;
  706. int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
  707. union set_pixel_clock args;
  708. memset(&args, 0, sizeof(args));
  709. if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
  710. &crev))
  711. return;
  712. switch (frev) {
  713. case 1:
  714. switch (crev) {
  715. case 1:
  716. if (clock == ATOM_DISABLE)
  717. return;
  718. args.v1.usPixelClock = cpu_to_le16(clock / 10);
  719. args.v1.usRefDiv = cpu_to_le16(ref_div);
  720. args.v1.usFbDiv = cpu_to_le16(fb_div);
  721. args.v1.ucFracFbDiv = frac_fb_div;
  722. args.v1.ucPostDiv = post_div;
  723. args.v1.ucPpll = pll_id;
  724. args.v1.ucCRTC = crtc_id;
  725. args.v1.ucRefDivSrc = 1;
  726. break;
  727. case 2:
  728. args.v2.usPixelClock = cpu_to_le16(clock / 10);
  729. args.v2.usRefDiv = cpu_to_le16(ref_div);
  730. args.v2.usFbDiv = cpu_to_le16(fb_div);
  731. args.v2.ucFracFbDiv = frac_fb_div;
  732. args.v2.ucPostDiv = post_div;
  733. args.v2.ucPpll = pll_id;
  734. args.v2.ucCRTC = crtc_id;
  735. args.v2.ucRefDivSrc = 1;
  736. break;
  737. case 3:
  738. args.v3.usPixelClock = cpu_to_le16(clock / 10);
  739. args.v3.usRefDiv = cpu_to_le16(ref_div);
  740. args.v3.usFbDiv = cpu_to_le16(fb_div);
  741. args.v3.ucFracFbDiv = frac_fb_div;
  742. args.v3.ucPostDiv = post_div;
  743. args.v3.ucPpll = pll_id;
  744. args.v3.ucMiscInfo = (pll_id << 2);
  745. if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
  746. args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
  747. args.v3.ucTransmitterId = encoder_id;
  748. args.v3.ucEncoderMode = encoder_mode;
  749. break;
  750. case 5:
  751. args.v5.ucCRTC = crtc_id;
  752. args.v5.usPixelClock = cpu_to_le16(clock / 10);
  753. args.v5.ucRefDiv = ref_div;
  754. args.v5.usFbDiv = cpu_to_le16(fb_div);
  755. args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
  756. args.v5.ucPostDiv = post_div;
  757. args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
  758. if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
  759. args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
  760. switch (bpc) {
  761. case 8:
  762. default:
  763. args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
  764. break;
  765. case 10:
  766. args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
  767. break;
  768. }
  769. args.v5.ucTransmitterID = encoder_id;
  770. args.v5.ucEncoderMode = encoder_mode;
  771. args.v5.ucPpll = pll_id;
  772. break;
  773. case 6:
  774. args.v6.ulCrtcPclkFreq.ucCRTC = crtc_id;
  775. args.v6.ulCrtcPclkFreq.ulPixelClock = cpu_to_le32(clock / 10);
  776. args.v6.ucRefDiv = ref_div;
  777. args.v6.usFbDiv = cpu_to_le16(fb_div);
  778. args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
  779. args.v6.ucPostDiv = post_div;
  780. args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
  781. if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
  782. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
  783. switch (bpc) {
  784. case 8:
  785. default:
  786. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
  787. break;
  788. case 10:
  789. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP;
  790. break;
  791. case 12:
  792. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP;
  793. break;
  794. case 16:
  795. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
  796. break;
  797. }
  798. args.v6.ucTransmitterID = encoder_id;
  799. args.v6.ucEncoderMode = encoder_mode;
  800. args.v6.ucPpll = pll_id;
  801. break;
  802. default:
  803. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  804. return;
  805. }
  806. break;
  807. default:
  808. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  809. return;
  810. }
  811. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  812. }
  813. static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
  814. {
  815. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  816. struct drm_device *dev = crtc->dev;
  817. struct radeon_device *rdev = dev->dev_private;
  818. struct drm_encoder *encoder = NULL;
  819. struct radeon_encoder *radeon_encoder = NULL;
  820. u32 pll_clock = mode->clock;
  821. u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
  822. struct radeon_pll *pll;
  823. u32 adjusted_clock;
  824. int encoder_mode = 0;
  825. struct radeon_atom_ss ss;
  826. bool ss_enabled = false;
  827. int bpc = 8;
  828. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  829. if (encoder->crtc == crtc) {
  830. radeon_encoder = to_radeon_encoder(encoder);
  831. encoder_mode = atombios_get_encoder_mode(encoder);
  832. break;
  833. }
  834. }
  835. if (!radeon_encoder)
  836. return;
  837. switch (radeon_crtc->pll_id) {
  838. case ATOM_PPLL1:
  839. pll = &rdev->clock.p1pll;
  840. break;
  841. case ATOM_PPLL2:
  842. pll = &rdev->clock.p2pll;
  843. break;
  844. case ATOM_DCPLL:
  845. case ATOM_PPLL_INVALID:
  846. default:
  847. pll = &rdev->clock.dcpll;
  848. break;
  849. }
  850. if (radeon_encoder->active_device &
  851. (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) {
  852. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  853. struct drm_connector *connector =
  854. radeon_get_connector_for_encoder(encoder);
  855. struct radeon_connector *radeon_connector =
  856. to_radeon_connector(connector);
  857. struct radeon_connector_atom_dig *dig_connector =
  858. radeon_connector->con_priv;
  859. int dp_clock;
  860. bpc = connector->display_info.bpc;
  861. switch (encoder_mode) {
  862. case ATOM_ENCODER_MODE_DP:
  863. /* DP/eDP */
  864. dp_clock = dig_connector->dp_clock / 10;
  865. if (ASIC_IS_DCE4(rdev))
  866. ss_enabled =
  867. radeon_atombios_get_asic_ss_info(rdev, &ss,
  868. ASIC_INTERNAL_SS_ON_DP,
  869. dp_clock);
  870. else {
  871. if (dp_clock == 16200) {
  872. ss_enabled =
  873. radeon_atombios_get_ppll_ss_info(rdev, &ss,
  874. ATOM_DP_SS_ID2);
  875. if (!ss_enabled)
  876. ss_enabled =
  877. radeon_atombios_get_ppll_ss_info(rdev, &ss,
  878. ATOM_DP_SS_ID1);
  879. } else
  880. ss_enabled =
  881. radeon_atombios_get_ppll_ss_info(rdev, &ss,
  882. ATOM_DP_SS_ID1);
  883. }
  884. break;
  885. case ATOM_ENCODER_MODE_LVDS:
  886. if (ASIC_IS_DCE4(rdev))
  887. ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
  888. dig->lcd_ss_id,
  889. mode->clock / 10);
  890. else
  891. ss_enabled = radeon_atombios_get_ppll_ss_info(rdev, &ss,
  892. dig->lcd_ss_id);
  893. break;
  894. case ATOM_ENCODER_MODE_DVI:
  895. if (ASIC_IS_DCE4(rdev))
  896. ss_enabled =
  897. radeon_atombios_get_asic_ss_info(rdev, &ss,
  898. ASIC_INTERNAL_SS_ON_TMDS,
  899. mode->clock / 10);
  900. break;
  901. case ATOM_ENCODER_MODE_HDMI:
  902. if (ASIC_IS_DCE4(rdev))
  903. ss_enabled =
  904. radeon_atombios_get_asic_ss_info(rdev, &ss,
  905. ASIC_INTERNAL_SS_ON_HDMI,
  906. mode->clock / 10);
  907. break;
  908. default:
  909. break;
  910. }
  911. }
  912. /* adjust pixel clock as needed */
  913. adjusted_clock = atombios_adjust_pll(crtc, mode, pll, ss_enabled, &ss);
  914. if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
  915. /* TV seems to prefer the legacy algo on some boards */
  916. radeon_compute_pll_legacy(pll, adjusted_clock, &pll_clock, &fb_div, &frac_fb_div,
  917. &ref_div, &post_div);
  918. else if (ASIC_IS_AVIVO(rdev))
  919. radeon_compute_pll_avivo(pll, adjusted_clock, &pll_clock, &fb_div, &frac_fb_div,
  920. &ref_div, &post_div);
  921. else
  922. radeon_compute_pll_legacy(pll, adjusted_clock, &pll_clock, &fb_div, &frac_fb_div,
  923. &ref_div, &post_div);
  924. atombios_crtc_program_ss(crtc, ATOM_DISABLE, radeon_crtc->pll_id, &ss);
  925. atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
  926. encoder_mode, radeon_encoder->encoder_id, mode->clock,
  927. ref_div, fb_div, frac_fb_div, post_div, bpc, ss_enabled, &ss);
  928. if (ss_enabled) {
  929. /* calculate ss amount and step size */
  930. if (ASIC_IS_DCE4(rdev)) {
  931. u32 step_size;
  932. u32 amount = (((fb_div * 10) + frac_fb_div) * ss.percentage) / 10000;
  933. ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
  934. ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
  935. ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
  936. if (ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
  937. step_size = (4 * amount * ref_div * (ss.rate * 2048)) /
  938. (125 * 25 * pll->reference_freq / 100);
  939. else
  940. step_size = (2 * amount * ref_div * (ss.rate * 2048)) /
  941. (125 * 25 * pll->reference_freq / 100);
  942. ss.step = step_size;
  943. }
  944. atombios_crtc_program_ss(crtc, ATOM_ENABLE, radeon_crtc->pll_id, &ss);
  945. }
  946. }
  947. static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
  948. struct drm_framebuffer *fb,
  949. int x, int y, int atomic)
  950. {
  951. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  952. struct drm_device *dev = crtc->dev;
  953. struct radeon_device *rdev = dev->dev_private;
  954. struct radeon_framebuffer *radeon_fb;
  955. struct drm_framebuffer *target_fb;
  956. struct drm_gem_object *obj;
  957. struct radeon_bo *rbo;
  958. uint64_t fb_location;
  959. uint32_t fb_format, fb_pitch_pixels, tiling_flags;
  960. u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
  961. u32 tmp;
  962. int r;
  963. /* no fb bound */
  964. if (!atomic && !crtc->fb) {
  965. DRM_DEBUG_KMS("No FB bound\n");
  966. return 0;
  967. }
  968. if (atomic) {
  969. radeon_fb = to_radeon_framebuffer(fb);
  970. target_fb = fb;
  971. }
  972. else {
  973. radeon_fb = to_radeon_framebuffer(crtc->fb);
  974. target_fb = crtc->fb;
  975. }
  976. /* If atomic, assume fb object is pinned & idle & fenced and
  977. * just update base pointers
  978. */
  979. obj = radeon_fb->obj;
  980. rbo = gem_to_radeon_bo(obj);
  981. r = radeon_bo_reserve(rbo, false);
  982. if (unlikely(r != 0))
  983. return r;
  984. if (atomic)
  985. fb_location = radeon_bo_gpu_offset(rbo);
  986. else {
  987. r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
  988. if (unlikely(r != 0)) {
  989. radeon_bo_unreserve(rbo);
  990. return -EINVAL;
  991. }
  992. }
  993. radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
  994. radeon_bo_unreserve(rbo);
  995. switch (target_fb->bits_per_pixel) {
  996. case 8:
  997. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
  998. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
  999. break;
  1000. case 15:
  1001. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
  1002. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
  1003. break;
  1004. case 16:
  1005. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
  1006. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
  1007. #ifdef __BIG_ENDIAN
  1008. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
  1009. #endif
  1010. break;
  1011. case 24:
  1012. case 32:
  1013. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
  1014. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
  1015. #ifdef __BIG_ENDIAN
  1016. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
  1017. #endif
  1018. break;
  1019. default:
  1020. DRM_ERROR("Unsupported screen depth %d\n",
  1021. target_fb->bits_per_pixel);
  1022. return -EINVAL;
  1023. }
  1024. if (tiling_flags & RADEON_TILING_MACRO)
  1025. fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
  1026. else if (tiling_flags & RADEON_TILING_MICRO)
  1027. fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
  1028. switch (radeon_crtc->crtc_id) {
  1029. case 0:
  1030. WREG32(AVIVO_D1VGA_CONTROL, 0);
  1031. break;
  1032. case 1:
  1033. WREG32(AVIVO_D2VGA_CONTROL, 0);
  1034. break;
  1035. case 2:
  1036. WREG32(EVERGREEN_D3VGA_CONTROL, 0);
  1037. break;
  1038. case 3:
  1039. WREG32(EVERGREEN_D4VGA_CONTROL, 0);
  1040. break;
  1041. case 4:
  1042. WREG32(EVERGREEN_D5VGA_CONTROL, 0);
  1043. break;
  1044. case 5:
  1045. WREG32(EVERGREEN_D6VGA_CONTROL, 0);
  1046. break;
  1047. default:
  1048. break;
  1049. }
  1050. WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
  1051. upper_32_bits(fb_location));
  1052. WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
  1053. upper_32_bits(fb_location));
  1054. WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
  1055. (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
  1056. WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
  1057. (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
  1058. WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
  1059. WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
  1060. WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
  1061. WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
  1062. WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
  1063. WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
  1064. WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
  1065. WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
  1066. fb_pitch_pixels = target_fb->pitch / (target_fb->bits_per_pixel / 8);
  1067. WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
  1068. WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
  1069. WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
  1070. crtc->mode.vdisplay);
  1071. x &= ~3;
  1072. y &= ~1;
  1073. WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
  1074. (x << 16) | y);
  1075. WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
  1076. (crtc->mode.hdisplay << 16) | crtc->mode.vdisplay);
  1077. /* pageflip setup */
  1078. /* make sure flip is at vb rather than hb */
  1079. tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
  1080. tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
  1081. WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
  1082. /* set pageflip to happen anywhere in vblank interval */
  1083. WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
  1084. if (!atomic && fb && fb != crtc->fb) {
  1085. radeon_fb = to_radeon_framebuffer(fb);
  1086. rbo = gem_to_radeon_bo(radeon_fb->obj);
  1087. r = radeon_bo_reserve(rbo, false);
  1088. if (unlikely(r != 0))
  1089. return r;
  1090. radeon_bo_unpin(rbo);
  1091. radeon_bo_unreserve(rbo);
  1092. }
  1093. /* Bytes per pixel may have changed */
  1094. radeon_bandwidth_update(rdev);
  1095. return 0;
  1096. }
  1097. static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
  1098. struct drm_framebuffer *fb,
  1099. int x, int y, int atomic)
  1100. {
  1101. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1102. struct drm_device *dev = crtc->dev;
  1103. struct radeon_device *rdev = dev->dev_private;
  1104. struct radeon_framebuffer *radeon_fb;
  1105. struct drm_gem_object *obj;
  1106. struct radeon_bo *rbo;
  1107. struct drm_framebuffer *target_fb;
  1108. uint64_t fb_location;
  1109. uint32_t fb_format, fb_pitch_pixels, tiling_flags;
  1110. u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
  1111. u32 tmp;
  1112. int r;
  1113. /* no fb bound */
  1114. if (!atomic && !crtc->fb) {
  1115. DRM_DEBUG_KMS("No FB bound\n");
  1116. return 0;
  1117. }
  1118. if (atomic) {
  1119. radeon_fb = to_radeon_framebuffer(fb);
  1120. target_fb = fb;
  1121. }
  1122. else {
  1123. radeon_fb = to_radeon_framebuffer(crtc->fb);
  1124. target_fb = crtc->fb;
  1125. }
  1126. obj = radeon_fb->obj;
  1127. rbo = gem_to_radeon_bo(obj);
  1128. r = radeon_bo_reserve(rbo, false);
  1129. if (unlikely(r != 0))
  1130. return r;
  1131. /* If atomic, assume fb object is pinned & idle & fenced and
  1132. * just update base pointers
  1133. */
  1134. if (atomic)
  1135. fb_location = radeon_bo_gpu_offset(rbo);
  1136. else {
  1137. r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
  1138. if (unlikely(r != 0)) {
  1139. radeon_bo_unreserve(rbo);
  1140. return -EINVAL;
  1141. }
  1142. }
  1143. radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
  1144. radeon_bo_unreserve(rbo);
  1145. switch (target_fb->bits_per_pixel) {
  1146. case 8:
  1147. fb_format =
  1148. AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
  1149. AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
  1150. break;
  1151. case 15:
  1152. fb_format =
  1153. AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
  1154. AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
  1155. break;
  1156. case 16:
  1157. fb_format =
  1158. AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
  1159. AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
  1160. #ifdef __BIG_ENDIAN
  1161. fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
  1162. #endif
  1163. break;
  1164. case 24:
  1165. case 32:
  1166. fb_format =
  1167. AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
  1168. AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
  1169. #ifdef __BIG_ENDIAN
  1170. fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
  1171. #endif
  1172. break;
  1173. default:
  1174. DRM_ERROR("Unsupported screen depth %d\n",
  1175. target_fb->bits_per_pixel);
  1176. return -EINVAL;
  1177. }
  1178. if (rdev->family >= CHIP_R600) {
  1179. if (tiling_flags & RADEON_TILING_MACRO)
  1180. fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
  1181. else if (tiling_flags & RADEON_TILING_MICRO)
  1182. fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
  1183. } else {
  1184. if (tiling_flags & RADEON_TILING_MACRO)
  1185. fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
  1186. if (tiling_flags & RADEON_TILING_MICRO)
  1187. fb_format |= AVIVO_D1GRPH_TILED;
  1188. }
  1189. if (radeon_crtc->crtc_id == 0)
  1190. WREG32(AVIVO_D1VGA_CONTROL, 0);
  1191. else
  1192. WREG32(AVIVO_D2VGA_CONTROL, 0);
  1193. if (rdev->family >= CHIP_RV770) {
  1194. if (radeon_crtc->crtc_id) {
  1195. WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
  1196. WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
  1197. } else {
  1198. WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
  1199. WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
  1200. }
  1201. }
  1202. WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
  1203. (u32) fb_location);
  1204. WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
  1205. radeon_crtc->crtc_offset, (u32) fb_location);
  1206. WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
  1207. if (rdev->family >= CHIP_R600)
  1208. WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
  1209. WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
  1210. WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
  1211. WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
  1212. WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
  1213. WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
  1214. WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
  1215. fb_pitch_pixels = target_fb->pitch / (target_fb->bits_per_pixel / 8);
  1216. WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
  1217. WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
  1218. WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
  1219. crtc->mode.vdisplay);
  1220. x &= ~3;
  1221. y &= ~1;
  1222. WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
  1223. (x << 16) | y);
  1224. WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
  1225. (crtc->mode.hdisplay << 16) | crtc->mode.vdisplay);
  1226. /* pageflip setup */
  1227. /* make sure flip is at vb rather than hb */
  1228. tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
  1229. tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
  1230. WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
  1231. /* set pageflip to happen anywhere in vblank interval */
  1232. WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 0);
  1233. if (!atomic && fb && fb != crtc->fb) {
  1234. radeon_fb = to_radeon_framebuffer(fb);
  1235. rbo = gem_to_radeon_bo(radeon_fb->obj);
  1236. r = radeon_bo_reserve(rbo, false);
  1237. if (unlikely(r != 0))
  1238. return r;
  1239. radeon_bo_unpin(rbo);
  1240. radeon_bo_unreserve(rbo);
  1241. }
  1242. /* Bytes per pixel may have changed */
  1243. radeon_bandwidth_update(rdev);
  1244. return 0;
  1245. }
  1246. int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  1247. struct drm_framebuffer *old_fb)
  1248. {
  1249. struct drm_device *dev = crtc->dev;
  1250. struct radeon_device *rdev = dev->dev_private;
  1251. if (ASIC_IS_DCE4(rdev))
  1252. return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
  1253. else if (ASIC_IS_AVIVO(rdev))
  1254. return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
  1255. else
  1256. return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
  1257. }
  1258. int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
  1259. struct drm_framebuffer *fb,
  1260. int x, int y, enum mode_set_atomic state)
  1261. {
  1262. struct drm_device *dev = crtc->dev;
  1263. struct radeon_device *rdev = dev->dev_private;
  1264. if (ASIC_IS_DCE4(rdev))
  1265. return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
  1266. else if (ASIC_IS_AVIVO(rdev))
  1267. return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
  1268. else
  1269. return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
  1270. }
  1271. /* properly set additional regs when using atombios */
  1272. static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
  1273. {
  1274. struct drm_device *dev = crtc->dev;
  1275. struct radeon_device *rdev = dev->dev_private;
  1276. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1277. u32 disp_merge_cntl;
  1278. switch (radeon_crtc->crtc_id) {
  1279. case 0:
  1280. disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
  1281. disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
  1282. WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
  1283. break;
  1284. case 1:
  1285. disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
  1286. disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
  1287. WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
  1288. WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
  1289. WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
  1290. break;
  1291. }
  1292. }
  1293. static int radeon_atom_pick_pll(struct drm_crtc *crtc)
  1294. {
  1295. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1296. struct drm_device *dev = crtc->dev;
  1297. struct radeon_device *rdev = dev->dev_private;
  1298. struct drm_encoder *test_encoder;
  1299. struct drm_crtc *test_crtc;
  1300. uint32_t pll_in_use = 0;
  1301. if (ASIC_IS_DCE4(rdev)) {
  1302. list_for_each_entry(test_encoder, &dev->mode_config.encoder_list, head) {
  1303. if (test_encoder->crtc && (test_encoder->crtc == crtc)) {
  1304. /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
  1305. * depending on the asic:
  1306. * DCE4: PPLL or ext clock
  1307. * DCE5: DCPLL or ext clock
  1308. *
  1309. * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
  1310. * PPLL/DCPLL programming and only program the DP DTO for the
  1311. * crtc virtual pixel clock.
  1312. */
  1313. if (atombios_get_encoder_mode(test_encoder) == ATOM_ENCODER_MODE_DP) {
  1314. if (ASIC_IS_DCE5(rdev) || rdev->clock.dp_extclk)
  1315. return ATOM_PPLL_INVALID;
  1316. }
  1317. }
  1318. }
  1319. /* otherwise, pick one of the plls */
  1320. list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
  1321. struct radeon_crtc *radeon_test_crtc;
  1322. if (crtc == test_crtc)
  1323. continue;
  1324. radeon_test_crtc = to_radeon_crtc(test_crtc);
  1325. if ((radeon_test_crtc->pll_id >= ATOM_PPLL1) &&
  1326. (radeon_test_crtc->pll_id <= ATOM_PPLL2))
  1327. pll_in_use |= (1 << radeon_test_crtc->pll_id);
  1328. }
  1329. if (!(pll_in_use & 1))
  1330. return ATOM_PPLL1;
  1331. return ATOM_PPLL2;
  1332. } else
  1333. return radeon_crtc->crtc_id;
  1334. }
  1335. int atombios_crtc_mode_set(struct drm_crtc *crtc,
  1336. struct drm_display_mode *mode,
  1337. struct drm_display_mode *adjusted_mode,
  1338. int x, int y, struct drm_framebuffer *old_fb)
  1339. {
  1340. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1341. struct drm_device *dev = crtc->dev;
  1342. struct radeon_device *rdev = dev->dev_private;
  1343. struct drm_encoder *encoder;
  1344. bool is_tvcv = false;
  1345. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1346. /* find tv std */
  1347. if (encoder->crtc == crtc) {
  1348. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  1349. if (radeon_encoder->active_device &
  1350. (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
  1351. is_tvcv = true;
  1352. }
  1353. }
  1354. /* always set DCPLL */
  1355. if (ASIC_IS_DCE4(rdev)) {
  1356. struct radeon_atom_ss ss;
  1357. bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
  1358. ASIC_INTERNAL_SS_ON_DCPLL,
  1359. rdev->clock.default_dispclk);
  1360. if (ss_enabled)
  1361. atombios_crtc_program_ss(crtc, ATOM_DISABLE, ATOM_DCPLL, &ss);
  1362. /* XXX: DCE5, make sure voltage, dispclk is high enough */
  1363. atombios_crtc_set_dcpll(crtc, rdev->clock.default_dispclk);
  1364. if (ss_enabled)
  1365. atombios_crtc_program_ss(crtc, ATOM_ENABLE, ATOM_DCPLL, &ss);
  1366. }
  1367. atombios_crtc_set_pll(crtc, adjusted_mode);
  1368. if (ASIC_IS_DCE4(rdev))
  1369. atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
  1370. else if (ASIC_IS_AVIVO(rdev)) {
  1371. if (is_tvcv)
  1372. atombios_crtc_set_timing(crtc, adjusted_mode);
  1373. else
  1374. atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
  1375. } else {
  1376. atombios_crtc_set_timing(crtc, adjusted_mode);
  1377. if (radeon_crtc->crtc_id == 0)
  1378. atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
  1379. radeon_legacy_atom_fixup(crtc);
  1380. }
  1381. atombios_crtc_set_base(crtc, x, y, old_fb);
  1382. atombios_overscan_setup(crtc, mode, adjusted_mode);
  1383. atombios_scaler_setup(crtc);
  1384. return 0;
  1385. }
  1386. static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
  1387. struct drm_display_mode *mode,
  1388. struct drm_display_mode *adjusted_mode)
  1389. {
  1390. struct drm_device *dev = crtc->dev;
  1391. struct radeon_device *rdev = dev->dev_private;
  1392. /* adjust pm to upcoming mode change */
  1393. radeon_pm_compute_clocks(rdev);
  1394. if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
  1395. return false;
  1396. return true;
  1397. }
  1398. static void atombios_crtc_prepare(struct drm_crtc *crtc)
  1399. {
  1400. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1401. /* pick pll */
  1402. radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
  1403. atombios_lock_crtc(crtc, ATOM_ENABLE);
  1404. atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  1405. }
  1406. static void atombios_crtc_commit(struct drm_crtc *crtc)
  1407. {
  1408. atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  1409. atombios_lock_crtc(crtc, ATOM_DISABLE);
  1410. }
  1411. static void atombios_crtc_disable(struct drm_crtc *crtc)
  1412. {
  1413. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1414. struct radeon_atom_ss ss;
  1415. atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  1416. switch (radeon_crtc->pll_id) {
  1417. case ATOM_PPLL1:
  1418. case ATOM_PPLL2:
  1419. /* disable the ppll */
  1420. atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
  1421. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  1422. break;
  1423. default:
  1424. break;
  1425. }
  1426. radeon_crtc->pll_id = -1;
  1427. }
  1428. static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
  1429. .dpms = atombios_crtc_dpms,
  1430. .mode_fixup = atombios_crtc_mode_fixup,
  1431. .mode_set = atombios_crtc_mode_set,
  1432. .mode_set_base = atombios_crtc_set_base,
  1433. .mode_set_base_atomic = atombios_crtc_set_base_atomic,
  1434. .prepare = atombios_crtc_prepare,
  1435. .commit = atombios_crtc_commit,
  1436. .load_lut = radeon_crtc_load_lut,
  1437. .disable = atombios_crtc_disable,
  1438. };
  1439. void radeon_atombios_init_crtc(struct drm_device *dev,
  1440. struct radeon_crtc *radeon_crtc)
  1441. {
  1442. struct radeon_device *rdev = dev->dev_private;
  1443. if (ASIC_IS_DCE4(rdev)) {
  1444. switch (radeon_crtc->crtc_id) {
  1445. case 0:
  1446. default:
  1447. radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
  1448. break;
  1449. case 1:
  1450. radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
  1451. break;
  1452. case 2:
  1453. radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
  1454. break;
  1455. case 3:
  1456. radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
  1457. break;
  1458. case 4:
  1459. radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
  1460. break;
  1461. case 5:
  1462. radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
  1463. break;
  1464. }
  1465. } else {
  1466. if (radeon_crtc->crtc_id == 1)
  1467. radeon_crtc->crtc_offset =
  1468. AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
  1469. else
  1470. radeon_crtc->crtc_offset = 0;
  1471. }
  1472. radeon_crtc->pll_id = -1;
  1473. drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
  1474. }