atombios_crtc.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258
  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. struct radeon_encoder *radeon_encoder =
  81. to_radeon_encoder(radeon_crtc->encoder);
  82. /* fixme - fill in enc_priv for atom dac */
  83. enum radeon_tv_std tv_std = TV_STD_NTSC;
  84. bool is_tv = false, is_cv = false;
  85. if (!ASIC_IS_AVIVO(rdev) && radeon_crtc->crtc_id)
  86. return;
  87. if (radeon_encoder->active_device & ATOM_DEVICE_TV_SUPPORT) {
  88. struct radeon_encoder_atom_dac *tv_dac = radeon_encoder->enc_priv;
  89. tv_std = tv_dac->tv_std;
  90. is_tv = true;
  91. }
  92. memset(&args, 0, sizeof(args));
  93. args.ucScaler = radeon_crtc->crtc_id;
  94. if (is_tv) {
  95. switch (tv_std) {
  96. case TV_STD_NTSC:
  97. default:
  98. args.ucTVStandard = ATOM_TV_NTSC;
  99. break;
  100. case TV_STD_PAL:
  101. args.ucTVStandard = ATOM_TV_PAL;
  102. break;
  103. case TV_STD_PAL_M:
  104. args.ucTVStandard = ATOM_TV_PALM;
  105. break;
  106. case TV_STD_PAL_60:
  107. args.ucTVStandard = ATOM_TV_PAL60;
  108. break;
  109. case TV_STD_NTSC_J:
  110. args.ucTVStandard = ATOM_TV_NTSCJ;
  111. break;
  112. case TV_STD_SCART_PAL:
  113. args.ucTVStandard = ATOM_TV_PAL; /* ??? */
  114. break;
  115. case TV_STD_SECAM:
  116. args.ucTVStandard = ATOM_TV_SECAM;
  117. break;
  118. case TV_STD_PAL_CN:
  119. args.ucTVStandard = ATOM_TV_PALCN;
  120. break;
  121. }
  122. args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
  123. } else if (is_cv) {
  124. args.ucTVStandard = ATOM_TV_CV;
  125. args.ucEnable = SCALER_ENABLE_MULTITAP_MODE;
  126. } else {
  127. switch (radeon_crtc->rmx_type) {
  128. case RMX_FULL:
  129. args.ucEnable = ATOM_SCALER_EXPANSION;
  130. break;
  131. case RMX_CENTER:
  132. args.ucEnable = ATOM_SCALER_CENTER;
  133. break;
  134. case RMX_ASPECT:
  135. args.ucEnable = ATOM_SCALER_EXPANSION;
  136. break;
  137. default:
  138. if (ASIC_IS_AVIVO(rdev))
  139. args.ucEnable = ATOM_SCALER_DISABLE;
  140. else
  141. args.ucEnable = ATOM_SCALER_CENTER;
  142. break;
  143. }
  144. }
  145. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  146. if ((is_tv || is_cv)
  147. && rdev->family >= CHIP_RV515 && rdev->family <= CHIP_R580) {
  148. atom_rv515_force_tv_scaler(rdev, radeon_crtc);
  149. }
  150. }
  151. static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
  152. {
  153. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  154. struct drm_device *dev = crtc->dev;
  155. struct radeon_device *rdev = dev->dev_private;
  156. int index =
  157. GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
  158. ENABLE_CRTC_PS_ALLOCATION args;
  159. memset(&args, 0, sizeof(args));
  160. args.ucCRTC = radeon_crtc->crtc_id;
  161. args.ucEnable = lock;
  162. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  163. }
  164. static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
  165. {
  166. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  167. struct drm_device *dev = crtc->dev;
  168. struct radeon_device *rdev = dev->dev_private;
  169. int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
  170. ENABLE_CRTC_PS_ALLOCATION args;
  171. memset(&args, 0, sizeof(args));
  172. args.ucCRTC = radeon_crtc->crtc_id;
  173. args.ucEnable = state;
  174. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  175. }
  176. static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
  177. {
  178. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  179. struct drm_device *dev = crtc->dev;
  180. struct radeon_device *rdev = dev->dev_private;
  181. int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
  182. ENABLE_CRTC_PS_ALLOCATION args;
  183. memset(&args, 0, sizeof(args));
  184. args.ucCRTC = radeon_crtc->crtc_id;
  185. args.ucEnable = state;
  186. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  187. }
  188. static const u32 vga_control_regs[6] =
  189. {
  190. AVIVO_D1VGA_CONTROL,
  191. AVIVO_D2VGA_CONTROL,
  192. EVERGREEN_D3VGA_CONTROL,
  193. EVERGREEN_D4VGA_CONTROL,
  194. EVERGREEN_D5VGA_CONTROL,
  195. EVERGREEN_D6VGA_CONTROL,
  196. };
  197. static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
  198. {
  199. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  200. struct drm_device *dev = crtc->dev;
  201. struct radeon_device *rdev = dev->dev_private;
  202. int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
  203. BLANK_CRTC_PS_ALLOCATION args;
  204. u32 vga_control = 0;
  205. memset(&args, 0, sizeof(args));
  206. if (ASIC_IS_DCE8(rdev)) {
  207. vga_control = RREG32(vga_control_regs[radeon_crtc->crtc_id]);
  208. WREG32(vga_control_regs[radeon_crtc->crtc_id], vga_control | 1);
  209. }
  210. args.ucCRTC = radeon_crtc->crtc_id;
  211. args.ucBlanking = state;
  212. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  213. if (ASIC_IS_DCE8(rdev)) {
  214. WREG32(vga_control_regs[radeon_crtc->crtc_id], vga_control);
  215. }
  216. }
  217. static void atombios_powergate_crtc(struct drm_crtc *crtc, int state)
  218. {
  219. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  220. struct drm_device *dev = crtc->dev;
  221. struct radeon_device *rdev = dev->dev_private;
  222. int index = GetIndexIntoMasterTable(COMMAND, EnableDispPowerGating);
  223. ENABLE_DISP_POWER_GATING_PARAMETERS_V2_1 args;
  224. memset(&args, 0, sizeof(args));
  225. args.ucDispPipeId = radeon_crtc->crtc_id;
  226. args.ucEnable = state;
  227. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  228. }
  229. void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
  230. {
  231. struct drm_device *dev = crtc->dev;
  232. struct radeon_device *rdev = dev->dev_private;
  233. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  234. switch (mode) {
  235. case DRM_MODE_DPMS_ON:
  236. radeon_crtc->enabled = true;
  237. atombios_enable_crtc(crtc, ATOM_ENABLE);
  238. if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
  239. atombios_enable_crtc_memreq(crtc, ATOM_ENABLE);
  240. atombios_blank_crtc(crtc, ATOM_DISABLE);
  241. drm_vblank_post_modeset(dev, radeon_crtc->crtc_id);
  242. /* Make sure vblank interrupt is still enabled if needed */
  243. radeon_irq_set(rdev);
  244. radeon_crtc_load_lut(crtc);
  245. break;
  246. case DRM_MODE_DPMS_STANDBY:
  247. case DRM_MODE_DPMS_SUSPEND:
  248. case DRM_MODE_DPMS_OFF:
  249. drm_vblank_pre_modeset(dev, radeon_crtc->crtc_id);
  250. if (radeon_crtc->enabled)
  251. atombios_blank_crtc(crtc, ATOM_ENABLE);
  252. if (ASIC_IS_DCE3(rdev) && !ASIC_IS_DCE6(rdev))
  253. atombios_enable_crtc_memreq(crtc, ATOM_DISABLE);
  254. atombios_enable_crtc(crtc, ATOM_DISABLE);
  255. radeon_crtc->enabled = false;
  256. break;
  257. }
  258. /* adjust pm to dpms */
  259. radeon_pm_compute_clocks(rdev);
  260. }
  261. static void
  262. atombios_set_crtc_dtd_timing(struct drm_crtc *crtc,
  263. struct drm_display_mode *mode)
  264. {
  265. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  266. struct drm_device *dev = crtc->dev;
  267. struct radeon_device *rdev = dev->dev_private;
  268. SET_CRTC_USING_DTD_TIMING_PARAMETERS args;
  269. int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_UsingDTDTiming);
  270. u16 misc = 0;
  271. memset(&args, 0, sizeof(args));
  272. args.usH_Size = cpu_to_le16(mode->crtc_hdisplay - (radeon_crtc->h_border * 2));
  273. args.usH_Blanking_Time =
  274. cpu_to_le16(mode->crtc_hblank_end - mode->crtc_hdisplay + (radeon_crtc->h_border * 2));
  275. args.usV_Size = cpu_to_le16(mode->crtc_vdisplay - (radeon_crtc->v_border * 2));
  276. args.usV_Blanking_Time =
  277. cpu_to_le16(mode->crtc_vblank_end - mode->crtc_vdisplay + (radeon_crtc->v_border * 2));
  278. args.usH_SyncOffset =
  279. cpu_to_le16(mode->crtc_hsync_start - mode->crtc_hdisplay + radeon_crtc->h_border);
  280. args.usH_SyncWidth =
  281. cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
  282. args.usV_SyncOffset =
  283. cpu_to_le16(mode->crtc_vsync_start - mode->crtc_vdisplay + radeon_crtc->v_border);
  284. args.usV_SyncWidth =
  285. cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
  286. args.ucH_Border = radeon_crtc->h_border;
  287. args.ucV_Border = radeon_crtc->v_border;
  288. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  289. misc |= ATOM_VSYNC_POLARITY;
  290. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  291. misc |= ATOM_HSYNC_POLARITY;
  292. if (mode->flags & DRM_MODE_FLAG_CSYNC)
  293. misc |= ATOM_COMPOSITESYNC;
  294. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  295. misc |= ATOM_INTERLACE;
  296. if (mode->flags & DRM_MODE_FLAG_DBLCLK)
  297. misc |= ATOM_DOUBLE_CLOCK_MODE;
  298. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  299. misc |= ATOM_H_REPLICATIONBY2 | ATOM_V_REPLICATIONBY2;
  300. args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
  301. args.ucCRTC = radeon_crtc->crtc_id;
  302. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  303. }
  304. static void atombios_crtc_set_timing(struct drm_crtc *crtc,
  305. struct drm_display_mode *mode)
  306. {
  307. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  308. struct drm_device *dev = crtc->dev;
  309. struct radeon_device *rdev = dev->dev_private;
  310. SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION args;
  311. int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
  312. u16 misc = 0;
  313. memset(&args, 0, sizeof(args));
  314. args.usH_Total = cpu_to_le16(mode->crtc_htotal);
  315. args.usH_Disp = cpu_to_le16(mode->crtc_hdisplay);
  316. args.usH_SyncStart = cpu_to_le16(mode->crtc_hsync_start);
  317. args.usH_SyncWidth =
  318. cpu_to_le16(mode->crtc_hsync_end - mode->crtc_hsync_start);
  319. args.usV_Total = cpu_to_le16(mode->crtc_vtotal);
  320. args.usV_Disp = cpu_to_le16(mode->crtc_vdisplay);
  321. args.usV_SyncStart = cpu_to_le16(mode->crtc_vsync_start);
  322. args.usV_SyncWidth =
  323. cpu_to_le16(mode->crtc_vsync_end - mode->crtc_vsync_start);
  324. args.ucOverscanRight = radeon_crtc->h_border;
  325. args.ucOverscanLeft = radeon_crtc->h_border;
  326. args.ucOverscanBottom = radeon_crtc->v_border;
  327. args.ucOverscanTop = radeon_crtc->v_border;
  328. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  329. misc |= ATOM_VSYNC_POLARITY;
  330. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  331. misc |= ATOM_HSYNC_POLARITY;
  332. if (mode->flags & DRM_MODE_FLAG_CSYNC)
  333. misc |= ATOM_COMPOSITESYNC;
  334. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  335. misc |= ATOM_INTERLACE;
  336. if (mode->flags & DRM_MODE_FLAG_DBLCLK)
  337. misc |= ATOM_DOUBLE_CLOCK_MODE;
  338. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  339. misc |= ATOM_H_REPLICATIONBY2 | ATOM_V_REPLICATIONBY2;
  340. args.susModeMiscInfo.usAccess = cpu_to_le16(misc);
  341. args.ucCRTC = radeon_crtc->crtc_id;
  342. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  343. }
  344. static void atombios_disable_ss(struct radeon_device *rdev, int pll_id)
  345. {
  346. u32 ss_cntl;
  347. if (ASIC_IS_DCE4(rdev)) {
  348. switch (pll_id) {
  349. case ATOM_PPLL1:
  350. ss_cntl = RREG32(EVERGREEN_P1PLL_SS_CNTL);
  351. ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
  352. WREG32(EVERGREEN_P1PLL_SS_CNTL, ss_cntl);
  353. break;
  354. case ATOM_PPLL2:
  355. ss_cntl = RREG32(EVERGREEN_P2PLL_SS_CNTL);
  356. ss_cntl &= ~EVERGREEN_PxPLL_SS_EN;
  357. WREG32(EVERGREEN_P2PLL_SS_CNTL, ss_cntl);
  358. break;
  359. case ATOM_DCPLL:
  360. case ATOM_PPLL_INVALID:
  361. return;
  362. }
  363. } else if (ASIC_IS_AVIVO(rdev)) {
  364. switch (pll_id) {
  365. case ATOM_PPLL1:
  366. ss_cntl = RREG32(AVIVO_P1PLL_INT_SS_CNTL);
  367. ss_cntl &= ~1;
  368. WREG32(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl);
  369. break;
  370. case ATOM_PPLL2:
  371. ss_cntl = RREG32(AVIVO_P2PLL_INT_SS_CNTL);
  372. ss_cntl &= ~1;
  373. WREG32(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl);
  374. break;
  375. case ATOM_DCPLL:
  376. case ATOM_PPLL_INVALID:
  377. return;
  378. }
  379. }
  380. }
  381. union atom_enable_ss {
  382. ENABLE_LVDS_SS_PARAMETERS lvds_ss;
  383. ENABLE_LVDS_SS_PARAMETERS_V2 lvds_ss_2;
  384. ENABLE_SPREAD_SPECTRUM_ON_PPLL_PS_ALLOCATION v1;
  385. ENABLE_SPREAD_SPECTRUM_ON_PPLL_V2 v2;
  386. ENABLE_SPREAD_SPECTRUM_ON_PPLL_V3 v3;
  387. };
  388. static void atombios_crtc_program_ss(struct radeon_device *rdev,
  389. int enable,
  390. int pll_id,
  391. int crtc_id,
  392. struct radeon_atom_ss *ss)
  393. {
  394. unsigned i;
  395. int index = GetIndexIntoMasterTable(COMMAND, EnableSpreadSpectrumOnPPLL);
  396. union atom_enable_ss args;
  397. if (enable) {
  398. /* Don't mess with SS if percentage is 0 or external ss.
  399. * SS is already disabled previously, and disabling it
  400. * again can cause display problems if the pll is already
  401. * programmed.
  402. */
  403. if (ss->percentage == 0)
  404. return;
  405. if (ss->type & ATOM_EXTERNAL_SS_MASK)
  406. return;
  407. } else {
  408. for (i = 0; i < rdev->num_crtc; i++) {
  409. if (rdev->mode_info.crtcs[i] &&
  410. rdev->mode_info.crtcs[i]->enabled &&
  411. i != crtc_id &&
  412. pll_id == rdev->mode_info.crtcs[i]->pll_id) {
  413. /* one other crtc is using this pll don't turn
  414. * off spread spectrum as it might turn off
  415. * display on active crtc
  416. */
  417. return;
  418. }
  419. }
  420. }
  421. memset(&args, 0, sizeof(args));
  422. if (ASIC_IS_DCE5(rdev)) {
  423. args.v3.usSpreadSpectrumAmountFrac = cpu_to_le16(0);
  424. args.v3.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
  425. switch (pll_id) {
  426. case ATOM_PPLL1:
  427. args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P1PLL;
  428. break;
  429. case ATOM_PPLL2:
  430. args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_P2PLL;
  431. break;
  432. case ATOM_DCPLL:
  433. args.v3.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V3_DCPLL;
  434. break;
  435. case ATOM_PPLL_INVALID:
  436. return;
  437. }
  438. args.v3.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
  439. args.v3.usSpreadSpectrumStep = cpu_to_le16(ss->step);
  440. args.v3.ucEnable = enable;
  441. } else if (ASIC_IS_DCE4(rdev)) {
  442. args.v2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
  443. args.v2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
  444. switch (pll_id) {
  445. case ATOM_PPLL1:
  446. args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P1PLL;
  447. break;
  448. case ATOM_PPLL2:
  449. args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_P2PLL;
  450. break;
  451. case ATOM_DCPLL:
  452. args.v2.ucSpreadSpectrumType |= ATOM_PPLL_SS_TYPE_V2_DCPLL;
  453. break;
  454. case ATOM_PPLL_INVALID:
  455. return;
  456. }
  457. args.v2.usSpreadSpectrumAmount = cpu_to_le16(ss->amount);
  458. args.v2.usSpreadSpectrumStep = cpu_to_le16(ss->step);
  459. args.v2.ucEnable = enable;
  460. } else if (ASIC_IS_DCE3(rdev)) {
  461. args.v1.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
  462. args.v1.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
  463. args.v1.ucSpreadSpectrumStep = ss->step;
  464. args.v1.ucSpreadSpectrumDelay = ss->delay;
  465. args.v1.ucSpreadSpectrumRange = ss->range;
  466. args.v1.ucPpll = pll_id;
  467. args.v1.ucEnable = enable;
  468. } else if (ASIC_IS_AVIVO(rdev)) {
  469. if ((enable == ATOM_DISABLE) || (ss->percentage == 0) ||
  470. (ss->type & ATOM_EXTERNAL_SS_MASK)) {
  471. atombios_disable_ss(rdev, pll_id);
  472. return;
  473. }
  474. args.lvds_ss_2.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
  475. args.lvds_ss_2.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
  476. args.lvds_ss_2.ucSpreadSpectrumStep = ss->step;
  477. args.lvds_ss_2.ucSpreadSpectrumDelay = ss->delay;
  478. args.lvds_ss_2.ucSpreadSpectrumRange = ss->range;
  479. args.lvds_ss_2.ucEnable = enable;
  480. } else {
  481. if (enable == ATOM_DISABLE) {
  482. atombios_disable_ss(rdev, pll_id);
  483. return;
  484. }
  485. args.lvds_ss.usSpreadSpectrumPercentage = cpu_to_le16(ss->percentage);
  486. args.lvds_ss.ucSpreadSpectrumType = ss->type & ATOM_SS_CENTRE_SPREAD_MODE_MASK;
  487. args.lvds_ss.ucSpreadSpectrumStepSize_Delay = (ss->step & 3) << 2;
  488. args.lvds_ss.ucSpreadSpectrumStepSize_Delay |= (ss->delay & 7) << 4;
  489. args.lvds_ss.ucEnable = enable;
  490. }
  491. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  492. }
  493. union adjust_pixel_clock {
  494. ADJUST_DISPLAY_PLL_PS_ALLOCATION v1;
  495. ADJUST_DISPLAY_PLL_PS_ALLOCATION_V3 v3;
  496. };
  497. static u32 atombios_adjust_pll(struct drm_crtc *crtc,
  498. struct drm_display_mode *mode)
  499. {
  500. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  501. struct drm_device *dev = crtc->dev;
  502. struct radeon_device *rdev = dev->dev_private;
  503. struct drm_encoder *encoder = radeon_crtc->encoder;
  504. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  505. struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
  506. u32 adjusted_clock = mode->clock;
  507. int encoder_mode = atombios_get_encoder_mode(encoder);
  508. u32 dp_clock = mode->clock;
  509. u32 clock = mode->clock;
  510. int bpc = radeon_crtc->bpc;
  511. bool is_duallink = radeon_dig_monitor_is_duallink(encoder, mode->clock);
  512. /* reset the pll flags */
  513. radeon_crtc->pll_flags = 0;
  514. if (ASIC_IS_AVIVO(rdev)) {
  515. if ((rdev->family == CHIP_RS600) ||
  516. (rdev->family == CHIP_RS690) ||
  517. (rdev->family == CHIP_RS740))
  518. radeon_crtc->pll_flags |= (/*RADEON_PLL_USE_FRAC_FB_DIV |*/
  519. RADEON_PLL_PREFER_CLOSEST_LOWER);
  520. if (ASIC_IS_DCE32(rdev) && mode->clock > 200000) /* range limits??? */
  521. radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
  522. else
  523. radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
  524. if (rdev->family < CHIP_RV770)
  525. radeon_crtc->pll_flags |= RADEON_PLL_PREFER_MINM_OVER_MAXP;
  526. /* use frac fb div on APUs */
  527. if (ASIC_IS_DCE41(rdev) || ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
  528. radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
  529. /* use frac fb div on RS780/RS880 */
  530. if ((rdev->family == CHIP_RS780) || (rdev->family == CHIP_RS880))
  531. radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
  532. if (ASIC_IS_DCE32(rdev) && mode->clock > 165000)
  533. radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
  534. } else {
  535. radeon_crtc->pll_flags |= RADEON_PLL_LEGACY;
  536. if (mode->clock > 200000) /* range limits??? */
  537. radeon_crtc->pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
  538. else
  539. radeon_crtc->pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
  540. }
  541. if ((radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
  542. (radeon_encoder_get_dp_bridge_encoder_id(encoder) != ENCODER_OBJECT_ID_NONE)) {
  543. if (connector) {
  544. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  545. struct radeon_connector_atom_dig *dig_connector =
  546. radeon_connector->con_priv;
  547. dp_clock = dig_connector->dp_clock;
  548. }
  549. }
  550. if (radeon_encoder->is_mst_encoder) {
  551. struct radeon_encoder_mst *mst_enc = radeon_encoder->enc_priv;
  552. struct radeon_connector_atom_dig *dig_connector = mst_enc->connector->con_priv;
  553. dp_clock = dig_connector->dp_clock;
  554. }
  555. /* use recommended ref_div for ss */
  556. if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
  557. if (radeon_crtc->ss_enabled) {
  558. if (radeon_crtc->ss.refdiv) {
  559. radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
  560. radeon_crtc->pll_reference_div = radeon_crtc->ss.refdiv;
  561. if (ASIC_IS_AVIVO(rdev))
  562. radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
  563. }
  564. }
  565. }
  566. if (ASIC_IS_AVIVO(rdev)) {
  567. /* DVO wants 2x pixel clock if the DVO chip is in 12 bit mode */
  568. if (radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1)
  569. adjusted_clock = mode->clock * 2;
  570. if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
  571. radeon_crtc->pll_flags |= RADEON_PLL_PREFER_CLOSEST_LOWER;
  572. if (radeon_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  573. radeon_crtc->pll_flags |= RADEON_PLL_IS_LCD;
  574. } else {
  575. if (encoder->encoder_type != DRM_MODE_ENCODER_DAC)
  576. radeon_crtc->pll_flags |= RADEON_PLL_NO_ODD_POST_DIV;
  577. if (encoder->encoder_type == DRM_MODE_ENCODER_LVDS)
  578. radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
  579. }
  580. /* adjust pll for deep color modes */
  581. if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
  582. switch (bpc) {
  583. case 8:
  584. default:
  585. break;
  586. case 10:
  587. clock = (clock * 5) / 4;
  588. break;
  589. case 12:
  590. clock = (clock * 3) / 2;
  591. break;
  592. case 16:
  593. clock = clock * 2;
  594. break;
  595. }
  596. }
  597. /* DCE3+ has an AdjustDisplayPll that will adjust the pixel clock
  598. * accordingly based on the encoder/transmitter to work around
  599. * special hw requirements.
  600. */
  601. if (ASIC_IS_DCE3(rdev)) {
  602. union adjust_pixel_clock args;
  603. u8 frev, crev;
  604. int index;
  605. index = GetIndexIntoMasterTable(COMMAND, AdjustDisplayPll);
  606. if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
  607. &crev))
  608. return adjusted_clock;
  609. memset(&args, 0, sizeof(args));
  610. switch (frev) {
  611. case 1:
  612. switch (crev) {
  613. case 1:
  614. case 2:
  615. args.v1.usPixelClock = cpu_to_le16(clock / 10);
  616. args.v1.ucTransmitterID = radeon_encoder->encoder_id;
  617. args.v1.ucEncodeMode = encoder_mode;
  618. if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
  619. args.v1.ucConfig |=
  620. ADJUST_DISPLAY_CONFIG_SS_ENABLE;
  621. atom_execute_table(rdev->mode_info.atom_context,
  622. index, (uint32_t *)&args);
  623. adjusted_clock = le16_to_cpu(args.v1.usPixelClock) * 10;
  624. break;
  625. case 3:
  626. args.v3.sInput.usPixelClock = cpu_to_le16(clock / 10);
  627. args.v3.sInput.ucTransmitterID = radeon_encoder->encoder_id;
  628. args.v3.sInput.ucEncodeMode = encoder_mode;
  629. args.v3.sInput.ucDispPllConfig = 0;
  630. if (radeon_crtc->ss_enabled && radeon_crtc->ss.percentage)
  631. args.v3.sInput.ucDispPllConfig |=
  632. DISPPLL_CONFIG_SS_ENABLE;
  633. if (ENCODER_MODE_IS_DP(encoder_mode)) {
  634. args.v3.sInput.ucDispPllConfig |=
  635. DISPPLL_CONFIG_COHERENT_MODE;
  636. /* 16200 or 27000 */
  637. args.v3.sInput.usPixelClock = cpu_to_le16(dp_clock / 10);
  638. } else if (radeon_encoder->devices & (ATOM_DEVICE_DFP_SUPPORT)) {
  639. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  640. if (dig->coherent_mode)
  641. args.v3.sInput.ucDispPllConfig |=
  642. DISPPLL_CONFIG_COHERENT_MODE;
  643. if (is_duallink)
  644. args.v3.sInput.ucDispPllConfig |=
  645. DISPPLL_CONFIG_DUAL_LINK;
  646. }
  647. if (radeon_encoder_get_dp_bridge_encoder_id(encoder) !=
  648. ENCODER_OBJECT_ID_NONE)
  649. args.v3.sInput.ucExtTransmitterID =
  650. radeon_encoder_get_dp_bridge_encoder_id(encoder);
  651. else
  652. args.v3.sInput.ucExtTransmitterID = 0;
  653. atom_execute_table(rdev->mode_info.atom_context,
  654. index, (uint32_t *)&args);
  655. adjusted_clock = le32_to_cpu(args.v3.sOutput.ulDispPllFreq) * 10;
  656. if (args.v3.sOutput.ucRefDiv) {
  657. radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
  658. radeon_crtc->pll_flags |= RADEON_PLL_USE_REF_DIV;
  659. radeon_crtc->pll_reference_div = args.v3.sOutput.ucRefDiv;
  660. }
  661. if (args.v3.sOutput.ucPostDiv) {
  662. radeon_crtc->pll_flags |= RADEON_PLL_USE_FRAC_FB_DIV;
  663. radeon_crtc->pll_flags |= RADEON_PLL_USE_POST_DIV;
  664. radeon_crtc->pll_post_div = args.v3.sOutput.ucPostDiv;
  665. }
  666. break;
  667. default:
  668. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  669. return adjusted_clock;
  670. }
  671. break;
  672. default:
  673. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  674. return adjusted_clock;
  675. }
  676. }
  677. return adjusted_clock;
  678. }
  679. union set_pixel_clock {
  680. SET_PIXEL_CLOCK_PS_ALLOCATION base;
  681. PIXEL_CLOCK_PARAMETERS v1;
  682. PIXEL_CLOCK_PARAMETERS_V2 v2;
  683. PIXEL_CLOCK_PARAMETERS_V3 v3;
  684. PIXEL_CLOCK_PARAMETERS_V5 v5;
  685. PIXEL_CLOCK_PARAMETERS_V6 v6;
  686. };
  687. /* on DCE5, make sure the voltage is high enough to support the
  688. * required disp clk.
  689. */
  690. static void atombios_crtc_set_disp_eng_pll(struct radeon_device *rdev,
  691. u32 dispclk)
  692. {
  693. u8 frev, crev;
  694. int index;
  695. union set_pixel_clock args;
  696. memset(&args, 0, sizeof(args));
  697. index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
  698. if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
  699. &crev))
  700. return;
  701. switch (frev) {
  702. case 1:
  703. switch (crev) {
  704. case 5:
  705. /* if the default dcpll clock is specified,
  706. * SetPixelClock provides the dividers
  707. */
  708. args.v5.ucCRTC = ATOM_CRTC_INVALID;
  709. args.v5.usPixelClock = cpu_to_le16(dispclk);
  710. args.v5.ucPpll = ATOM_DCPLL;
  711. break;
  712. case 6:
  713. /* if the default dcpll clock is specified,
  714. * SetPixelClock provides the dividers
  715. */
  716. args.v6.ulDispEngClkFreq = cpu_to_le32(dispclk);
  717. if (ASIC_IS_DCE61(rdev) || ASIC_IS_DCE8(rdev))
  718. args.v6.ucPpll = ATOM_EXT_PLL1;
  719. else if (ASIC_IS_DCE6(rdev))
  720. args.v6.ucPpll = ATOM_PPLL0;
  721. else
  722. args.v6.ucPpll = ATOM_DCPLL;
  723. break;
  724. default:
  725. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  726. return;
  727. }
  728. break;
  729. default:
  730. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  731. return;
  732. }
  733. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  734. }
  735. static void atombios_crtc_program_pll(struct drm_crtc *crtc,
  736. u32 crtc_id,
  737. int pll_id,
  738. u32 encoder_mode,
  739. u32 encoder_id,
  740. u32 clock,
  741. u32 ref_div,
  742. u32 fb_div,
  743. u32 frac_fb_div,
  744. u32 post_div,
  745. int bpc,
  746. bool ss_enabled,
  747. struct radeon_atom_ss *ss)
  748. {
  749. struct drm_device *dev = crtc->dev;
  750. struct radeon_device *rdev = dev->dev_private;
  751. u8 frev, crev;
  752. int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
  753. union set_pixel_clock args;
  754. memset(&args, 0, sizeof(args));
  755. if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev,
  756. &crev))
  757. return;
  758. switch (frev) {
  759. case 1:
  760. switch (crev) {
  761. case 1:
  762. if (clock == ATOM_DISABLE)
  763. return;
  764. args.v1.usPixelClock = cpu_to_le16(clock / 10);
  765. args.v1.usRefDiv = cpu_to_le16(ref_div);
  766. args.v1.usFbDiv = cpu_to_le16(fb_div);
  767. args.v1.ucFracFbDiv = frac_fb_div;
  768. args.v1.ucPostDiv = post_div;
  769. args.v1.ucPpll = pll_id;
  770. args.v1.ucCRTC = crtc_id;
  771. args.v1.ucRefDivSrc = 1;
  772. break;
  773. case 2:
  774. args.v2.usPixelClock = cpu_to_le16(clock / 10);
  775. args.v2.usRefDiv = cpu_to_le16(ref_div);
  776. args.v2.usFbDiv = cpu_to_le16(fb_div);
  777. args.v2.ucFracFbDiv = frac_fb_div;
  778. args.v2.ucPostDiv = post_div;
  779. args.v2.ucPpll = pll_id;
  780. args.v2.ucCRTC = crtc_id;
  781. args.v2.ucRefDivSrc = 1;
  782. break;
  783. case 3:
  784. args.v3.usPixelClock = cpu_to_le16(clock / 10);
  785. args.v3.usRefDiv = cpu_to_le16(ref_div);
  786. args.v3.usFbDiv = cpu_to_le16(fb_div);
  787. args.v3.ucFracFbDiv = frac_fb_div;
  788. args.v3.ucPostDiv = post_div;
  789. args.v3.ucPpll = pll_id;
  790. if (crtc_id == ATOM_CRTC2)
  791. args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC2;
  792. else
  793. args.v3.ucMiscInfo = PIXEL_CLOCK_MISC_CRTC_SEL_CRTC1;
  794. if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
  795. args.v3.ucMiscInfo |= PIXEL_CLOCK_MISC_REF_DIV_SRC;
  796. args.v3.ucTransmitterId = encoder_id;
  797. args.v3.ucEncoderMode = encoder_mode;
  798. break;
  799. case 5:
  800. args.v5.ucCRTC = crtc_id;
  801. args.v5.usPixelClock = cpu_to_le16(clock / 10);
  802. args.v5.ucRefDiv = ref_div;
  803. args.v5.usFbDiv = cpu_to_le16(fb_div);
  804. args.v5.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
  805. args.v5.ucPostDiv = post_div;
  806. args.v5.ucMiscInfo = 0; /* HDMI depth, etc. */
  807. if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
  808. args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_REF_DIV_SRC;
  809. if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
  810. switch (bpc) {
  811. case 8:
  812. default:
  813. args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_24BPP;
  814. break;
  815. case 10:
  816. /* yes this is correct, the atom define is wrong */
  817. args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_32BPP;
  818. break;
  819. case 12:
  820. /* yes this is correct, the atom define is wrong */
  821. args.v5.ucMiscInfo |= PIXEL_CLOCK_V5_MISC_HDMI_30BPP;
  822. break;
  823. }
  824. }
  825. args.v5.ucTransmitterID = encoder_id;
  826. args.v5.ucEncoderMode = encoder_mode;
  827. args.v5.ucPpll = pll_id;
  828. break;
  829. case 6:
  830. args.v6.ulDispEngClkFreq = cpu_to_le32(crtc_id << 24 | clock / 10);
  831. args.v6.ucRefDiv = ref_div;
  832. args.v6.usFbDiv = cpu_to_le16(fb_div);
  833. args.v6.ulFbDivDecFrac = cpu_to_le32(frac_fb_div * 100000);
  834. args.v6.ucPostDiv = post_div;
  835. args.v6.ucMiscInfo = 0; /* HDMI depth, etc. */
  836. if (ss_enabled && (ss->type & ATOM_EXTERNAL_SS_MASK))
  837. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_REF_DIV_SRC;
  838. if (encoder_mode == ATOM_ENCODER_MODE_HDMI) {
  839. switch (bpc) {
  840. case 8:
  841. default:
  842. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_24BPP;
  843. break;
  844. case 10:
  845. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_30BPP_V6;
  846. break;
  847. case 12:
  848. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_36BPP_V6;
  849. break;
  850. case 16:
  851. args.v6.ucMiscInfo |= PIXEL_CLOCK_V6_MISC_HDMI_48BPP;
  852. break;
  853. }
  854. }
  855. args.v6.ucTransmitterID = encoder_id;
  856. args.v6.ucEncoderMode = encoder_mode;
  857. args.v6.ucPpll = pll_id;
  858. break;
  859. default:
  860. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  861. return;
  862. }
  863. break;
  864. default:
  865. DRM_ERROR("Unknown table version %d %d\n", frev, crev);
  866. return;
  867. }
  868. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  869. }
  870. static bool atombios_crtc_prepare_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
  871. {
  872. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  873. struct drm_device *dev = crtc->dev;
  874. struct radeon_device *rdev = dev->dev_private;
  875. struct radeon_encoder *radeon_encoder =
  876. to_radeon_encoder(radeon_crtc->encoder);
  877. int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
  878. radeon_crtc->bpc = 8;
  879. radeon_crtc->ss_enabled = false;
  880. if (radeon_encoder->is_mst_encoder) {
  881. radeon_dp_mst_prepare_pll(crtc, mode);
  882. } else if ((radeon_encoder->active_device & (ATOM_DEVICE_LCD_SUPPORT | ATOM_DEVICE_DFP_SUPPORT)) ||
  883. (radeon_encoder_get_dp_bridge_encoder_id(radeon_crtc->encoder) != ENCODER_OBJECT_ID_NONE)) {
  884. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  885. struct drm_connector *connector =
  886. radeon_get_connector_for_encoder(radeon_crtc->encoder);
  887. struct radeon_connector *radeon_connector =
  888. to_radeon_connector(connector);
  889. struct radeon_connector_atom_dig *dig_connector =
  890. radeon_connector->con_priv;
  891. int dp_clock;
  892. /* Assign mode clock for hdmi deep color max clock limit check */
  893. radeon_connector->pixelclock_for_modeset = mode->clock;
  894. radeon_crtc->bpc = radeon_get_monitor_bpc(connector);
  895. switch (encoder_mode) {
  896. case ATOM_ENCODER_MODE_DP_MST:
  897. case ATOM_ENCODER_MODE_DP:
  898. /* DP/eDP */
  899. dp_clock = dig_connector->dp_clock / 10;
  900. if (ASIC_IS_DCE4(rdev))
  901. radeon_crtc->ss_enabled =
  902. radeon_atombios_get_asic_ss_info(rdev, &radeon_crtc->ss,
  903. ASIC_INTERNAL_SS_ON_DP,
  904. dp_clock);
  905. else {
  906. if (dp_clock == 16200) {
  907. radeon_crtc->ss_enabled =
  908. radeon_atombios_get_ppll_ss_info(rdev,
  909. &radeon_crtc->ss,
  910. ATOM_DP_SS_ID2);
  911. if (!radeon_crtc->ss_enabled)
  912. radeon_crtc->ss_enabled =
  913. radeon_atombios_get_ppll_ss_info(rdev,
  914. &radeon_crtc->ss,
  915. ATOM_DP_SS_ID1);
  916. } else {
  917. radeon_crtc->ss_enabled =
  918. radeon_atombios_get_ppll_ss_info(rdev,
  919. &radeon_crtc->ss,
  920. ATOM_DP_SS_ID1);
  921. }
  922. /* disable spread spectrum on DCE3 DP */
  923. radeon_crtc->ss_enabled = false;
  924. }
  925. break;
  926. case ATOM_ENCODER_MODE_LVDS:
  927. if (ASIC_IS_DCE4(rdev))
  928. radeon_crtc->ss_enabled =
  929. radeon_atombios_get_asic_ss_info(rdev,
  930. &radeon_crtc->ss,
  931. dig->lcd_ss_id,
  932. mode->clock / 10);
  933. else
  934. radeon_crtc->ss_enabled =
  935. radeon_atombios_get_ppll_ss_info(rdev,
  936. &radeon_crtc->ss,
  937. dig->lcd_ss_id);
  938. break;
  939. case ATOM_ENCODER_MODE_DVI:
  940. if (ASIC_IS_DCE4(rdev))
  941. radeon_crtc->ss_enabled =
  942. radeon_atombios_get_asic_ss_info(rdev,
  943. &radeon_crtc->ss,
  944. ASIC_INTERNAL_SS_ON_TMDS,
  945. mode->clock / 10);
  946. break;
  947. case ATOM_ENCODER_MODE_HDMI:
  948. if (ASIC_IS_DCE4(rdev))
  949. radeon_crtc->ss_enabled =
  950. radeon_atombios_get_asic_ss_info(rdev,
  951. &radeon_crtc->ss,
  952. ASIC_INTERNAL_SS_ON_HDMI,
  953. mode->clock / 10);
  954. break;
  955. default:
  956. break;
  957. }
  958. }
  959. /* adjust pixel clock as needed */
  960. radeon_crtc->adjusted_clock = atombios_adjust_pll(crtc, mode);
  961. return true;
  962. }
  963. static void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
  964. {
  965. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  966. struct drm_device *dev = crtc->dev;
  967. struct radeon_device *rdev = dev->dev_private;
  968. struct radeon_encoder *radeon_encoder =
  969. to_radeon_encoder(radeon_crtc->encoder);
  970. u32 pll_clock = mode->clock;
  971. u32 clock = mode->clock;
  972. u32 ref_div = 0, fb_div = 0, frac_fb_div = 0, post_div = 0;
  973. struct radeon_pll *pll;
  974. int encoder_mode = atombios_get_encoder_mode(radeon_crtc->encoder);
  975. /* pass the actual clock to atombios_crtc_program_pll for DCE5,6 for HDMI */
  976. if (ASIC_IS_DCE5(rdev) &&
  977. (encoder_mode == ATOM_ENCODER_MODE_HDMI) &&
  978. (radeon_crtc->bpc > 8))
  979. clock = radeon_crtc->adjusted_clock;
  980. switch (radeon_crtc->pll_id) {
  981. case ATOM_PPLL1:
  982. pll = &rdev->clock.p1pll;
  983. break;
  984. case ATOM_PPLL2:
  985. pll = &rdev->clock.p2pll;
  986. break;
  987. case ATOM_DCPLL:
  988. case ATOM_PPLL_INVALID:
  989. default:
  990. pll = &rdev->clock.dcpll;
  991. break;
  992. }
  993. /* update pll params */
  994. pll->flags = radeon_crtc->pll_flags;
  995. pll->reference_div = radeon_crtc->pll_reference_div;
  996. pll->post_div = radeon_crtc->pll_post_div;
  997. if (radeon_encoder->active_device & (ATOM_DEVICE_TV_SUPPORT))
  998. /* TV seems to prefer the legacy algo on some boards */
  999. radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
  1000. &fb_div, &frac_fb_div, &ref_div, &post_div);
  1001. else if (ASIC_IS_AVIVO(rdev))
  1002. radeon_compute_pll_avivo(pll, radeon_crtc->adjusted_clock, &pll_clock,
  1003. &fb_div, &frac_fb_div, &ref_div, &post_div);
  1004. else
  1005. radeon_compute_pll_legacy(pll, radeon_crtc->adjusted_clock, &pll_clock,
  1006. &fb_div, &frac_fb_div, &ref_div, &post_div);
  1007. atombios_crtc_program_ss(rdev, ATOM_DISABLE, radeon_crtc->pll_id,
  1008. radeon_crtc->crtc_id, &radeon_crtc->ss);
  1009. atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
  1010. encoder_mode, radeon_encoder->encoder_id, clock,
  1011. ref_div, fb_div, frac_fb_div, post_div,
  1012. radeon_crtc->bpc, radeon_crtc->ss_enabled, &radeon_crtc->ss);
  1013. if (radeon_crtc->ss_enabled) {
  1014. /* calculate ss amount and step size */
  1015. if (ASIC_IS_DCE4(rdev)) {
  1016. u32 step_size;
  1017. u32 amount = (((fb_div * 10) + frac_fb_div) *
  1018. (u32)radeon_crtc->ss.percentage) /
  1019. (100 * (u32)radeon_crtc->ss.percentage_divider);
  1020. radeon_crtc->ss.amount = (amount / 10) & ATOM_PPLL_SS_AMOUNT_V2_FBDIV_MASK;
  1021. radeon_crtc->ss.amount |= ((amount - (amount / 10)) << ATOM_PPLL_SS_AMOUNT_V2_NFRAC_SHIFT) &
  1022. ATOM_PPLL_SS_AMOUNT_V2_NFRAC_MASK;
  1023. if (radeon_crtc->ss.type & ATOM_PPLL_SS_TYPE_V2_CENTRE_SPREAD)
  1024. step_size = (4 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
  1025. (125 * 25 * pll->reference_freq / 100);
  1026. else
  1027. step_size = (2 * amount * ref_div * ((u32)radeon_crtc->ss.rate * 2048)) /
  1028. (125 * 25 * pll->reference_freq / 100);
  1029. radeon_crtc->ss.step = step_size;
  1030. }
  1031. atombios_crtc_program_ss(rdev, ATOM_ENABLE, radeon_crtc->pll_id,
  1032. radeon_crtc->crtc_id, &radeon_crtc->ss);
  1033. }
  1034. }
  1035. static int dce4_crtc_do_set_base(struct drm_crtc *crtc,
  1036. struct drm_framebuffer *fb,
  1037. int x, int y, int atomic)
  1038. {
  1039. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1040. struct drm_device *dev = crtc->dev;
  1041. struct radeon_device *rdev = dev->dev_private;
  1042. struct radeon_framebuffer *radeon_fb;
  1043. struct drm_framebuffer *target_fb;
  1044. struct drm_gem_object *obj;
  1045. struct radeon_bo *rbo;
  1046. uint64_t fb_location;
  1047. uint32_t fb_format, fb_pitch_pixels, tiling_flags;
  1048. unsigned bankw, bankh, mtaspect, tile_split;
  1049. u32 fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_NONE);
  1050. u32 tmp, viewport_w, viewport_h;
  1051. int r;
  1052. bool bypass_lut = false;
  1053. /* no fb bound */
  1054. if (!atomic && !crtc->primary->fb) {
  1055. DRM_DEBUG_KMS("No FB bound\n");
  1056. return 0;
  1057. }
  1058. if (atomic) {
  1059. radeon_fb = to_radeon_framebuffer(fb);
  1060. target_fb = fb;
  1061. }
  1062. else {
  1063. radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
  1064. target_fb = crtc->primary->fb;
  1065. }
  1066. /* If atomic, assume fb object is pinned & idle & fenced and
  1067. * just update base pointers
  1068. */
  1069. obj = radeon_fb->obj;
  1070. rbo = gem_to_radeon_bo(obj);
  1071. r = radeon_bo_reserve(rbo, false);
  1072. if (unlikely(r != 0))
  1073. return r;
  1074. if (atomic)
  1075. fb_location = radeon_bo_gpu_offset(rbo);
  1076. else {
  1077. r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
  1078. if (unlikely(r != 0)) {
  1079. radeon_bo_unreserve(rbo);
  1080. return -EINVAL;
  1081. }
  1082. }
  1083. radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
  1084. radeon_bo_unreserve(rbo);
  1085. switch (target_fb->pixel_format) {
  1086. case DRM_FORMAT_C8:
  1087. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_8BPP) |
  1088. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_INDEXED));
  1089. break;
  1090. case DRM_FORMAT_XRGB4444:
  1091. case DRM_FORMAT_ARGB4444:
  1092. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
  1093. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB4444));
  1094. #ifdef __BIG_ENDIAN
  1095. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
  1096. #endif
  1097. break;
  1098. case DRM_FORMAT_XRGB1555:
  1099. case DRM_FORMAT_ARGB1555:
  1100. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
  1101. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB1555));
  1102. #ifdef __BIG_ENDIAN
  1103. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
  1104. #endif
  1105. break;
  1106. case DRM_FORMAT_BGRX5551:
  1107. case DRM_FORMAT_BGRA5551:
  1108. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
  1109. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_BGRA5551));
  1110. #ifdef __BIG_ENDIAN
  1111. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
  1112. #endif
  1113. break;
  1114. case DRM_FORMAT_RGB565:
  1115. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_16BPP) |
  1116. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB565));
  1117. #ifdef __BIG_ENDIAN
  1118. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN16);
  1119. #endif
  1120. break;
  1121. case DRM_FORMAT_XRGB8888:
  1122. case DRM_FORMAT_ARGB8888:
  1123. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
  1124. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB8888));
  1125. #ifdef __BIG_ENDIAN
  1126. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
  1127. #endif
  1128. break;
  1129. case DRM_FORMAT_XRGB2101010:
  1130. case DRM_FORMAT_ARGB2101010:
  1131. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
  1132. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_ARGB2101010));
  1133. #ifdef __BIG_ENDIAN
  1134. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
  1135. #endif
  1136. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1137. bypass_lut = true;
  1138. break;
  1139. case DRM_FORMAT_BGRX1010102:
  1140. case DRM_FORMAT_BGRA1010102:
  1141. fb_format = (EVERGREEN_GRPH_DEPTH(EVERGREEN_GRPH_DEPTH_32BPP) |
  1142. EVERGREEN_GRPH_FORMAT(EVERGREEN_GRPH_FORMAT_BGRA1010102));
  1143. #ifdef __BIG_ENDIAN
  1144. fb_swap = EVERGREEN_GRPH_ENDIAN_SWAP(EVERGREEN_GRPH_ENDIAN_8IN32);
  1145. #endif
  1146. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1147. bypass_lut = true;
  1148. break;
  1149. default:
  1150. DRM_ERROR("Unsupported screen format %s\n",
  1151. drm_get_format_name(target_fb->pixel_format));
  1152. return -EINVAL;
  1153. }
  1154. if (tiling_flags & RADEON_TILING_MACRO) {
  1155. evergreen_tiling_fields(tiling_flags, &bankw, &bankh, &mtaspect, &tile_split);
  1156. /* Set NUM_BANKS. */
  1157. if (rdev->family >= CHIP_TAHITI) {
  1158. unsigned index, num_banks;
  1159. if (rdev->family >= CHIP_BONAIRE) {
  1160. unsigned tileb, tile_split_bytes;
  1161. /* Calculate the macrotile mode index. */
  1162. tile_split_bytes = 64 << tile_split;
  1163. tileb = 8 * 8 * target_fb->bits_per_pixel / 8;
  1164. tileb = min(tile_split_bytes, tileb);
  1165. for (index = 0; tileb > 64; index++)
  1166. tileb >>= 1;
  1167. if (index >= 16) {
  1168. DRM_ERROR("Wrong screen bpp (%u) or tile split (%u)\n",
  1169. target_fb->bits_per_pixel, tile_split);
  1170. return -EINVAL;
  1171. }
  1172. num_banks = (rdev->config.cik.macrotile_mode_array[index] >> 6) & 0x3;
  1173. } else {
  1174. switch (target_fb->bits_per_pixel) {
  1175. case 8:
  1176. index = 10;
  1177. break;
  1178. case 16:
  1179. index = SI_TILE_MODE_COLOR_2D_SCANOUT_16BPP;
  1180. break;
  1181. default:
  1182. case 32:
  1183. index = SI_TILE_MODE_COLOR_2D_SCANOUT_32BPP;
  1184. break;
  1185. }
  1186. num_banks = (rdev->config.si.tile_mode_array[index] >> 20) & 0x3;
  1187. }
  1188. fb_format |= EVERGREEN_GRPH_NUM_BANKS(num_banks);
  1189. } else {
  1190. /* NI and older. */
  1191. if (rdev->family >= CHIP_CAYMAN)
  1192. tmp = rdev->config.cayman.tile_config;
  1193. else
  1194. tmp = rdev->config.evergreen.tile_config;
  1195. switch ((tmp & 0xf0) >> 4) {
  1196. case 0: /* 4 banks */
  1197. fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_4_BANK);
  1198. break;
  1199. case 1: /* 8 banks */
  1200. default:
  1201. fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_8_BANK);
  1202. break;
  1203. case 2: /* 16 banks */
  1204. fb_format |= EVERGREEN_GRPH_NUM_BANKS(EVERGREEN_ADDR_SURF_16_BANK);
  1205. break;
  1206. }
  1207. }
  1208. fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_2D_TILED_THIN1);
  1209. fb_format |= EVERGREEN_GRPH_TILE_SPLIT(tile_split);
  1210. fb_format |= EVERGREEN_GRPH_BANK_WIDTH(bankw);
  1211. fb_format |= EVERGREEN_GRPH_BANK_HEIGHT(bankh);
  1212. fb_format |= EVERGREEN_GRPH_MACRO_TILE_ASPECT(mtaspect);
  1213. if (rdev->family >= CHIP_BONAIRE) {
  1214. /* XXX need to know more about the surface tiling mode */
  1215. fb_format |= CIK_GRPH_MICRO_TILE_MODE(CIK_DISPLAY_MICRO_TILING);
  1216. }
  1217. } else if (tiling_flags & RADEON_TILING_MICRO)
  1218. fb_format |= EVERGREEN_GRPH_ARRAY_MODE(EVERGREEN_GRPH_ARRAY_1D_TILED_THIN1);
  1219. if (rdev->family >= CHIP_BONAIRE) {
  1220. /* Read the pipe config from the 2D TILED SCANOUT mode.
  1221. * It should be the same for the other modes too, but not all
  1222. * modes set the pipe config field. */
  1223. u32 pipe_config = (rdev->config.cik.tile_mode_array[10] >> 6) & 0x1f;
  1224. fb_format |= CIK_GRPH_PIPE_CONFIG(pipe_config);
  1225. } else if ((rdev->family == CHIP_TAHITI) ||
  1226. (rdev->family == CHIP_PITCAIRN))
  1227. fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P8_32x32_8x16);
  1228. else if ((rdev->family == CHIP_VERDE) ||
  1229. (rdev->family == CHIP_OLAND) ||
  1230. (rdev->family == CHIP_HAINAN)) /* for completeness. HAINAN has no display hw */
  1231. fb_format |= SI_GRPH_PIPE_CONFIG(SI_ADDR_SURF_P4_8x16);
  1232. switch (radeon_crtc->crtc_id) {
  1233. case 0:
  1234. WREG32(AVIVO_D1VGA_CONTROL, 0);
  1235. break;
  1236. case 1:
  1237. WREG32(AVIVO_D2VGA_CONTROL, 0);
  1238. break;
  1239. case 2:
  1240. WREG32(EVERGREEN_D3VGA_CONTROL, 0);
  1241. break;
  1242. case 3:
  1243. WREG32(EVERGREEN_D4VGA_CONTROL, 0);
  1244. break;
  1245. case 4:
  1246. WREG32(EVERGREEN_D5VGA_CONTROL, 0);
  1247. break;
  1248. case 5:
  1249. WREG32(EVERGREEN_D6VGA_CONTROL, 0);
  1250. break;
  1251. default:
  1252. break;
  1253. }
  1254. WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
  1255. upper_32_bits(fb_location));
  1256. WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
  1257. upper_32_bits(fb_location));
  1258. WREG32(EVERGREEN_GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
  1259. (u32)fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
  1260. WREG32(EVERGREEN_GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
  1261. (u32) fb_location & EVERGREEN_GRPH_SURFACE_ADDRESS_MASK);
  1262. WREG32(EVERGREEN_GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
  1263. WREG32(EVERGREEN_GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
  1264. /*
  1265. * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
  1266. * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
  1267. * retain the full precision throughout the pipeline.
  1268. */
  1269. WREG32_P(EVERGREEN_GRPH_LUT_10BIT_BYPASS_CONTROL + radeon_crtc->crtc_offset,
  1270. (bypass_lut ? EVERGREEN_LUT_10BIT_BYPASS_EN : 0),
  1271. ~EVERGREEN_LUT_10BIT_BYPASS_EN);
  1272. if (bypass_lut)
  1273. DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
  1274. WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
  1275. WREG32(EVERGREEN_GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
  1276. WREG32(EVERGREEN_GRPH_X_START + radeon_crtc->crtc_offset, 0);
  1277. WREG32(EVERGREEN_GRPH_Y_START + radeon_crtc->crtc_offset, 0);
  1278. WREG32(EVERGREEN_GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
  1279. WREG32(EVERGREEN_GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
  1280. fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
  1281. WREG32(EVERGREEN_GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
  1282. WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
  1283. if (rdev->family >= CHIP_BONAIRE)
  1284. WREG32(CIK_LB_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
  1285. target_fb->height);
  1286. else
  1287. WREG32(EVERGREEN_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
  1288. target_fb->height);
  1289. x &= ~3;
  1290. y &= ~1;
  1291. WREG32(EVERGREEN_VIEWPORT_START + radeon_crtc->crtc_offset,
  1292. (x << 16) | y);
  1293. viewport_w = crtc->mode.hdisplay;
  1294. viewport_h = (crtc->mode.vdisplay + 1) & ~1;
  1295. if ((rdev->family >= CHIP_BONAIRE) &&
  1296. (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE))
  1297. viewport_h *= 2;
  1298. WREG32(EVERGREEN_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
  1299. (viewport_w << 16) | viewport_h);
  1300. /* pageflip setup */
  1301. /* make sure flip is at vb rather than hb */
  1302. tmp = RREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
  1303. tmp &= ~EVERGREEN_GRPH_SURFACE_UPDATE_H_RETRACE_EN;
  1304. WREG32(EVERGREEN_GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
  1305. /* set pageflip to happen only at start of vblank interval (front porch) */
  1306. WREG32(EVERGREEN_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 3);
  1307. if (!atomic && fb && fb != crtc->primary->fb) {
  1308. radeon_fb = to_radeon_framebuffer(fb);
  1309. rbo = gem_to_radeon_bo(radeon_fb->obj);
  1310. r = radeon_bo_reserve(rbo, false);
  1311. if (unlikely(r != 0))
  1312. return r;
  1313. radeon_bo_unpin(rbo);
  1314. radeon_bo_unreserve(rbo);
  1315. }
  1316. /* Bytes per pixel may have changed */
  1317. radeon_bandwidth_update(rdev);
  1318. return 0;
  1319. }
  1320. static int avivo_crtc_do_set_base(struct drm_crtc *crtc,
  1321. struct drm_framebuffer *fb,
  1322. int x, int y, int atomic)
  1323. {
  1324. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1325. struct drm_device *dev = crtc->dev;
  1326. struct radeon_device *rdev = dev->dev_private;
  1327. struct radeon_framebuffer *radeon_fb;
  1328. struct drm_gem_object *obj;
  1329. struct radeon_bo *rbo;
  1330. struct drm_framebuffer *target_fb;
  1331. uint64_t fb_location;
  1332. uint32_t fb_format, fb_pitch_pixels, tiling_flags;
  1333. u32 fb_swap = R600_D1GRPH_SWAP_ENDIAN_NONE;
  1334. u32 tmp, viewport_w, viewport_h;
  1335. int r;
  1336. bool bypass_lut = false;
  1337. /* no fb bound */
  1338. if (!atomic && !crtc->primary->fb) {
  1339. DRM_DEBUG_KMS("No FB bound\n");
  1340. return 0;
  1341. }
  1342. if (atomic) {
  1343. radeon_fb = to_radeon_framebuffer(fb);
  1344. target_fb = fb;
  1345. }
  1346. else {
  1347. radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
  1348. target_fb = crtc->primary->fb;
  1349. }
  1350. obj = radeon_fb->obj;
  1351. rbo = gem_to_radeon_bo(obj);
  1352. r = radeon_bo_reserve(rbo, false);
  1353. if (unlikely(r != 0))
  1354. return r;
  1355. /* If atomic, assume fb object is pinned & idle & fenced and
  1356. * just update base pointers
  1357. */
  1358. if (atomic)
  1359. fb_location = radeon_bo_gpu_offset(rbo);
  1360. else {
  1361. r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_location);
  1362. if (unlikely(r != 0)) {
  1363. radeon_bo_unreserve(rbo);
  1364. return -EINVAL;
  1365. }
  1366. }
  1367. radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
  1368. radeon_bo_unreserve(rbo);
  1369. switch (target_fb->pixel_format) {
  1370. case DRM_FORMAT_C8:
  1371. fb_format =
  1372. AVIVO_D1GRPH_CONTROL_DEPTH_8BPP |
  1373. AVIVO_D1GRPH_CONTROL_8BPP_INDEXED;
  1374. break;
  1375. case DRM_FORMAT_XRGB4444:
  1376. case DRM_FORMAT_ARGB4444:
  1377. fb_format =
  1378. AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
  1379. AVIVO_D1GRPH_CONTROL_16BPP_ARGB4444;
  1380. #ifdef __BIG_ENDIAN
  1381. fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
  1382. #endif
  1383. break;
  1384. case DRM_FORMAT_XRGB1555:
  1385. fb_format =
  1386. AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
  1387. AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
  1388. #ifdef __BIG_ENDIAN
  1389. fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
  1390. #endif
  1391. break;
  1392. case DRM_FORMAT_RGB565:
  1393. fb_format =
  1394. AVIVO_D1GRPH_CONTROL_DEPTH_16BPP |
  1395. AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
  1396. #ifdef __BIG_ENDIAN
  1397. fb_swap = R600_D1GRPH_SWAP_ENDIAN_16BIT;
  1398. #endif
  1399. break;
  1400. case DRM_FORMAT_XRGB8888:
  1401. case DRM_FORMAT_ARGB8888:
  1402. fb_format =
  1403. AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
  1404. AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
  1405. #ifdef __BIG_ENDIAN
  1406. fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
  1407. #endif
  1408. break;
  1409. case DRM_FORMAT_XRGB2101010:
  1410. case DRM_FORMAT_ARGB2101010:
  1411. fb_format =
  1412. AVIVO_D1GRPH_CONTROL_DEPTH_32BPP |
  1413. AVIVO_D1GRPH_CONTROL_32BPP_ARGB2101010;
  1414. #ifdef __BIG_ENDIAN
  1415. fb_swap = R600_D1GRPH_SWAP_ENDIAN_32BIT;
  1416. #endif
  1417. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1418. bypass_lut = true;
  1419. break;
  1420. default:
  1421. DRM_ERROR("Unsupported screen format %s\n",
  1422. drm_get_format_name(target_fb->pixel_format));
  1423. return -EINVAL;
  1424. }
  1425. if (rdev->family >= CHIP_R600) {
  1426. if (tiling_flags & RADEON_TILING_MACRO)
  1427. fb_format |= R600_D1GRPH_ARRAY_MODE_2D_TILED_THIN1;
  1428. else if (tiling_flags & RADEON_TILING_MICRO)
  1429. fb_format |= R600_D1GRPH_ARRAY_MODE_1D_TILED_THIN1;
  1430. } else {
  1431. if (tiling_flags & RADEON_TILING_MACRO)
  1432. fb_format |= AVIVO_D1GRPH_MACRO_ADDRESS_MODE;
  1433. if (tiling_flags & RADEON_TILING_MICRO)
  1434. fb_format |= AVIVO_D1GRPH_TILED;
  1435. }
  1436. if (radeon_crtc->crtc_id == 0)
  1437. WREG32(AVIVO_D1VGA_CONTROL, 0);
  1438. else
  1439. WREG32(AVIVO_D2VGA_CONTROL, 0);
  1440. if (rdev->family >= CHIP_RV770) {
  1441. if (radeon_crtc->crtc_id) {
  1442. WREG32(R700_D2GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
  1443. WREG32(R700_D2GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
  1444. } else {
  1445. WREG32(R700_D1GRPH_PRIMARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
  1446. WREG32(R700_D1GRPH_SECONDARY_SURFACE_ADDRESS_HIGH, upper_32_bits(fb_location));
  1447. }
  1448. }
  1449. WREG32(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
  1450. (u32) fb_location);
  1451. WREG32(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS +
  1452. radeon_crtc->crtc_offset, (u32) fb_location);
  1453. WREG32(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
  1454. if (rdev->family >= CHIP_R600)
  1455. WREG32(R600_D1GRPH_SWAP_CONTROL + radeon_crtc->crtc_offset, fb_swap);
  1456. /* LUT only has 256 slots for 8 bpc fb. Bypass for > 8 bpc scanout for precision */
  1457. WREG32_P(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset,
  1458. (bypass_lut ? AVIVO_LUT_10BIT_BYPASS_EN : 0), ~AVIVO_LUT_10BIT_BYPASS_EN);
  1459. if (bypass_lut)
  1460. DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
  1461. WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
  1462. WREG32(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
  1463. WREG32(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, 0);
  1464. WREG32(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, 0);
  1465. WREG32(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, target_fb->width);
  1466. WREG32(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, target_fb->height);
  1467. fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
  1468. WREG32(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
  1469. WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
  1470. WREG32(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
  1471. target_fb->height);
  1472. x &= ~3;
  1473. y &= ~1;
  1474. WREG32(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset,
  1475. (x << 16) | y);
  1476. viewport_w = crtc->mode.hdisplay;
  1477. viewport_h = (crtc->mode.vdisplay + 1) & ~1;
  1478. WREG32(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
  1479. (viewport_w << 16) | viewport_h);
  1480. /* pageflip setup */
  1481. /* make sure flip is at vb rather than hb */
  1482. tmp = RREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset);
  1483. tmp &= ~AVIVO_D1GRPH_SURFACE_UPDATE_H_RETRACE_EN;
  1484. WREG32(AVIVO_D1GRPH_FLIP_CONTROL + radeon_crtc->crtc_offset, tmp);
  1485. /* set pageflip to happen only at start of vblank interval (front porch) */
  1486. WREG32(AVIVO_D1MODE_MASTER_UPDATE_MODE + radeon_crtc->crtc_offset, 3);
  1487. if (!atomic && fb && fb != crtc->primary->fb) {
  1488. radeon_fb = to_radeon_framebuffer(fb);
  1489. rbo = gem_to_radeon_bo(radeon_fb->obj);
  1490. r = radeon_bo_reserve(rbo, false);
  1491. if (unlikely(r != 0))
  1492. return r;
  1493. radeon_bo_unpin(rbo);
  1494. radeon_bo_unreserve(rbo);
  1495. }
  1496. /* Bytes per pixel may have changed */
  1497. radeon_bandwidth_update(rdev);
  1498. return 0;
  1499. }
  1500. int atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  1501. struct drm_framebuffer *old_fb)
  1502. {
  1503. struct drm_device *dev = crtc->dev;
  1504. struct radeon_device *rdev = dev->dev_private;
  1505. if (ASIC_IS_DCE4(rdev))
  1506. return dce4_crtc_do_set_base(crtc, old_fb, x, y, 0);
  1507. else if (ASIC_IS_AVIVO(rdev))
  1508. return avivo_crtc_do_set_base(crtc, old_fb, x, y, 0);
  1509. else
  1510. return radeon_crtc_do_set_base(crtc, old_fb, x, y, 0);
  1511. }
  1512. int atombios_crtc_set_base_atomic(struct drm_crtc *crtc,
  1513. struct drm_framebuffer *fb,
  1514. int x, int y, enum mode_set_atomic state)
  1515. {
  1516. struct drm_device *dev = crtc->dev;
  1517. struct radeon_device *rdev = dev->dev_private;
  1518. if (ASIC_IS_DCE4(rdev))
  1519. return dce4_crtc_do_set_base(crtc, fb, x, y, 1);
  1520. else if (ASIC_IS_AVIVO(rdev))
  1521. return avivo_crtc_do_set_base(crtc, fb, x, y, 1);
  1522. else
  1523. return radeon_crtc_do_set_base(crtc, fb, x, y, 1);
  1524. }
  1525. /* properly set additional regs when using atombios */
  1526. static void radeon_legacy_atom_fixup(struct drm_crtc *crtc)
  1527. {
  1528. struct drm_device *dev = crtc->dev;
  1529. struct radeon_device *rdev = dev->dev_private;
  1530. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1531. u32 disp_merge_cntl;
  1532. switch (radeon_crtc->crtc_id) {
  1533. case 0:
  1534. disp_merge_cntl = RREG32(RADEON_DISP_MERGE_CNTL);
  1535. disp_merge_cntl &= ~RADEON_DISP_RGB_OFFSET_EN;
  1536. WREG32(RADEON_DISP_MERGE_CNTL, disp_merge_cntl);
  1537. break;
  1538. case 1:
  1539. disp_merge_cntl = RREG32(RADEON_DISP2_MERGE_CNTL);
  1540. disp_merge_cntl &= ~RADEON_DISP2_RGB_OFFSET_EN;
  1541. WREG32(RADEON_DISP2_MERGE_CNTL, disp_merge_cntl);
  1542. WREG32(RADEON_FP_H2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_H_SYNC_STRT_WID));
  1543. WREG32(RADEON_FP_V2_SYNC_STRT_WID, RREG32(RADEON_CRTC2_V_SYNC_STRT_WID));
  1544. break;
  1545. }
  1546. }
  1547. /**
  1548. * radeon_get_pll_use_mask - look up a mask of which pplls are in use
  1549. *
  1550. * @crtc: drm crtc
  1551. *
  1552. * Returns the mask of which PPLLs (Pixel PLLs) are in use.
  1553. */
  1554. static u32 radeon_get_pll_use_mask(struct drm_crtc *crtc)
  1555. {
  1556. struct drm_device *dev = crtc->dev;
  1557. struct drm_crtc *test_crtc;
  1558. struct radeon_crtc *test_radeon_crtc;
  1559. u32 pll_in_use = 0;
  1560. list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
  1561. if (crtc == test_crtc)
  1562. continue;
  1563. test_radeon_crtc = to_radeon_crtc(test_crtc);
  1564. if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
  1565. pll_in_use |= (1 << test_radeon_crtc->pll_id);
  1566. }
  1567. return pll_in_use;
  1568. }
  1569. /**
  1570. * radeon_get_shared_dp_ppll - return the PPLL used by another crtc for DP
  1571. *
  1572. * @crtc: drm crtc
  1573. *
  1574. * Returns the PPLL (Pixel PLL) used by another crtc/encoder which is
  1575. * also in DP mode. For DP, a single PPLL can be used for all DP
  1576. * crtcs/encoders.
  1577. */
  1578. static int radeon_get_shared_dp_ppll(struct drm_crtc *crtc)
  1579. {
  1580. struct drm_device *dev = crtc->dev;
  1581. struct radeon_device *rdev = dev->dev_private;
  1582. struct drm_crtc *test_crtc;
  1583. struct radeon_crtc *test_radeon_crtc;
  1584. list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
  1585. if (crtc == test_crtc)
  1586. continue;
  1587. test_radeon_crtc = to_radeon_crtc(test_crtc);
  1588. if (test_radeon_crtc->encoder &&
  1589. ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
  1590. /* PPLL2 is exclusive to UNIPHYA on DCE61 */
  1591. if (ASIC_IS_DCE61(rdev) && !ASIC_IS_DCE8(rdev) &&
  1592. test_radeon_crtc->pll_id == ATOM_PPLL2)
  1593. continue;
  1594. /* for DP use the same PLL for all */
  1595. if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
  1596. return test_radeon_crtc->pll_id;
  1597. }
  1598. }
  1599. return ATOM_PPLL_INVALID;
  1600. }
  1601. /**
  1602. * radeon_get_shared_nondp_ppll - return the PPLL used by another non-DP crtc
  1603. *
  1604. * @crtc: drm crtc
  1605. * @encoder: drm encoder
  1606. *
  1607. * Returns the PPLL (Pixel PLL) used by another non-DP crtc/encoder which can
  1608. * be shared (i.e., same clock).
  1609. */
  1610. static int radeon_get_shared_nondp_ppll(struct drm_crtc *crtc)
  1611. {
  1612. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1613. struct drm_device *dev = crtc->dev;
  1614. struct radeon_device *rdev = dev->dev_private;
  1615. struct drm_crtc *test_crtc;
  1616. struct radeon_crtc *test_radeon_crtc;
  1617. u32 adjusted_clock, test_adjusted_clock;
  1618. adjusted_clock = radeon_crtc->adjusted_clock;
  1619. if (adjusted_clock == 0)
  1620. return ATOM_PPLL_INVALID;
  1621. list_for_each_entry(test_crtc, &dev->mode_config.crtc_list, head) {
  1622. if (crtc == test_crtc)
  1623. continue;
  1624. test_radeon_crtc = to_radeon_crtc(test_crtc);
  1625. if (test_radeon_crtc->encoder &&
  1626. !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(test_radeon_crtc->encoder))) {
  1627. /* PPLL2 is exclusive to UNIPHYA on DCE61 */
  1628. if (ASIC_IS_DCE61(rdev) && !ASIC_IS_DCE8(rdev) &&
  1629. test_radeon_crtc->pll_id == ATOM_PPLL2)
  1630. continue;
  1631. /* check if we are already driving this connector with another crtc */
  1632. if (test_radeon_crtc->connector == radeon_crtc->connector) {
  1633. /* if we are, return that pll */
  1634. if (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID)
  1635. return test_radeon_crtc->pll_id;
  1636. }
  1637. /* for non-DP check the clock */
  1638. test_adjusted_clock = test_radeon_crtc->adjusted_clock;
  1639. if ((crtc->mode.clock == test_crtc->mode.clock) &&
  1640. (adjusted_clock == test_adjusted_clock) &&
  1641. (radeon_crtc->ss_enabled == test_radeon_crtc->ss_enabled) &&
  1642. (test_radeon_crtc->pll_id != ATOM_PPLL_INVALID))
  1643. return test_radeon_crtc->pll_id;
  1644. }
  1645. }
  1646. return ATOM_PPLL_INVALID;
  1647. }
  1648. /**
  1649. * radeon_atom_pick_pll - Allocate a PPLL for use by the crtc.
  1650. *
  1651. * @crtc: drm crtc
  1652. *
  1653. * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
  1654. * a single PPLL can be used for all DP crtcs/encoders. For non-DP
  1655. * monitors a dedicated PPLL must be used. If a particular board has
  1656. * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
  1657. * as there is no need to program the PLL itself. If we are not able to
  1658. * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
  1659. * avoid messing up an existing monitor.
  1660. *
  1661. * Asic specific PLL information
  1662. *
  1663. * DCE 8.x
  1664. * KB/KV
  1665. * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
  1666. * CI
  1667. * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
  1668. *
  1669. * DCE 6.1
  1670. * - PPLL2 is only available to UNIPHYA (both DP and non-DP)
  1671. * - PPLL0, PPLL1 are available for UNIPHYB/C/D/E/F (both DP and non-DP)
  1672. *
  1673. * DCE 6.0
  1674. * - PPLL0 is available to all UNIPHY (DP only)
  1675. * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
  1676. *
  1677. * DCE 5.0
  1678. * - DCPLL is available to all UNIPHY (DP only)
  1679. * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
  1680. *
  1681. * DCE 3.0/4.0/4.1
  1682. * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
  1683. *
  1684. */
  1685. static int radeon_atom_pick_pll(struct drm_crtc *crtc)
  1686. {
  1687. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1688. struct drm_device *dev = crtc->dev;
  1689. struct radeon_device *rdev = dev->dev_private;
  1690. struct radeon_encoder *radeon_encoder =
  1691. to_radeon_encoder(radeon_crtc->encoder);
  1692. u32 pll_in_use;
  1693. int pll;
  1694. if (ASIC_IS_DCE8(rdev)) {
  1695. if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
  1696. if (rdev->clock.dp_extclk)
  1697. /* skip PPLL programming if using ext clock */
  1698. return ATOM_PPLL_INVALID;
  1699. else {
  1700. /* use the same PPLL for all DP monitors */
  1701. pll = radeon_get_shared_dp_ppll(crtc);
  1702. if (pll != ATOM_PPLL_INVALID)
  1703. return pll;
  1704. }
  1705. } else {
  1706. /* use the same PPLL for all monitors with the same clock */
  1707. pll = radeon_get_shared_nondp_ppll(crtc);
  1708. if (pll != ATOM_PPLL_INVALID)
  1709. return pll;
  1710. }
  1711. /* otherwise, pick one of the plls */
  1712. if ((rdev->family == CHIP_KABINI) ||
  1713. (rdev->family == CHIP_MULLINS)) {
  1714. /* KB/ML has PPLL1 and PPLL2 */
  1715. pll_in_use = radeon_get_pll_use_mask(crtc);
  1716. if (!(pll_in_use & (1 << ATOM_PPLL2)))
  1717. return ATOM_PPLL2;
  1718. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  1719. return ATOM_PPLL1;
  1720. DRM_ERROR("unable to allocate a PPLL\n");
  1721. return ATOM_PPLL_INVALID;
  1722. } else {
  1723. /* CI/KV has PPLL0, PPLL1, and PPLL2 */
  1724. pll_in_use = radeon_get_pll_use_mask(crtc);
  1725. if (!(pll_in_use & (1 << ATOM_PPLL2)))
  1726. return ATOM_PPLL2;
  1727. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  1728. return ATOM_PPLL1;
  1729. if (!(pll_in_use & (1 << ATOM_PPLL0)))
  1730. return ATOM_PPLL0;
  1731. DRM_ERROR("unable to allocate a PPLL\n");
  1732. return ATOM_PPLL_INVALID;
  1733. }
  1734. } else if (ASIC_IS_DCE61(rdev)) {
  1735. struct radeon_encoder_atom_dig *dig =
  1736. radeon_encoder->enc_priv;
  1737. if ((radeon_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_UNIPHY) &&
  1738. (dig->linkb == false))
  1739. /* UNIPHY A uses PPLL2 */
  1740. return ATOM_PPLL2;
  1741. else if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
  1742. /* UNIPHY B/C/D/E/F */
  1743. if (rdev->clock.dp_extclk)
  1744. /* skip PPLL programming if using ext clock */
  1745. return ATOM_PPLL_INVALID;
  1746. else {
  1747. /* use the same PPLL for all DP monitors */
  1748. pll = radeon_get_shared_dp_ppll(crtc);
  1749. if (pll != ATOM_PPLL_INVALID)
  1750. return pll;
  1751. }
  1752. } else {
  1753. /* use the same PPLL for all monitors with the same clock */
  1754. pll = radeon_get_shared_nondp_ppll(crtc);
  1755. if (pll != ATOM_PPLL_INVALID)
  1756. return pll;
  1757. }
  1758. /* UNIPHY B/C/D/E/F */
  1759. pll_in_use = radeon_get_pll_use_mask(crtc);
  1760. if (!(pll_in_use & (1 << ATOM_PPLL0)))
  1761. return ATOM_PPLL0;
  1762. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  1763. return ATOM_PPLL1;
  1764. DRM_ERROR("unable to allocate a PPLL\n");
  1765. return ATOM_PPLL_INVALID;
  1766. } else if (ASIC_IS_DCE41(rdev)) {
  1767. /* Don't share PLLs on DCE4.1 chips */
  1768. if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
  1769. if (rdev->clock.dp_extclk)
  1770. /* skip PPLL programming if using ext clock */
  1771. return ATOM_PPLL_INVALID;
  1772. }
  1773. pll_in_use = radeon_get_pll_use_mask(crtc);
  1774. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  1775. return ATOM_PPLL1;
  1776. if (!(pll_in_use & (1 << ATOM_PPLL2)))
  1777. return ATOM_PPLL2;
  1778. DRM_ERROR("unable to allocate a PPLL\n");
  1779. return ATOM_PPLL_INVALID;
  1780. } else if (ASIC_IS_DCE4(rdev)) {
  1781. /* in DP mode, the DP ref clock can come from PPLL, DCPLL, or ext clock,
  1782. * depending on the asic:
  1783. * DCE4: PPLL or ext clock
  1784. * DCE5: PPLL, DCPLL, or ext clock
  1785. * DCE6: PPLL, PPLL0, or ext clock
  1786. *
  1787. * Setting ATOM_PPLL_INVALID will cause SetPixelClock to skip
  1788. * PPLL/DCPLL programming and only program the DP DTO for the
  1789. * crtc virtual pixel clock.
  1790. */
  1791. if (ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder))) {
  1792. if (rdev->clock.dp_extclk)
  1793. /* skip PPLL programming if using ext clock */
  1794. return ATOM_PPLL_INVALID;
  1795. else if (ASIC_IS_DCE6(rdev))
  1796. /* use PPLL0 for all DP */
  1797. return ATOM_PPLL0;
  1798. else if (ASIC_IS_DCE5(rdev))
  1799. /* use DCPLL for all DP */
  1800. return ATOM_DCPLL;
  1801. else {
  1802. /* use the same PPLL for all DP monitors */
  1803. pll = radeon_get_shared_dp_ppll(crtc);
  1804. if (pll != ATOM_PPLL_INVALID)
  1805. return pll;
  1806. }
  1807. } else {
  1808. /* use the same PPLL for all monitors with the same clock */
  1809. pll = radeon_get_shared_nondp_ppll(crtc);
  1810. if (pll != ATOM_PPLL_INVALID)
  1811. return pll;
  1812. }
  1813. /* all other cases */
  1814. pll_in_use = radeon_get_pll_use_mask(crtc);
  1815. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  1816. return ATOM_PPLL1;
  1817. if (!(pll_in_use & (1 << ATOM_PPLL2)))
  1818. return ATOM_PPLL2;
  1819. DRM_ERROR("unable to allocate a PPLL\n");
  1820. return ATOM_PPLL_INVALID;
  1821. } else {
  1822. /* on pre-R5xx asics, the crtc to pll mapping is hardcoded */
  1823. /* some atombios (observed in some DCE2/DCE3) code have a bug,
  1824. * the matching btw pll and crtc is done through
  1825. * PCLK_CRTC[1|2]_CNTL (0x480/0x484) but atombios code use the
  1826. * pll (1 or 2) to select which register to write. ie if using
  1827. * pll1 it will use PCLK_CRTC1_CNTL (0x480) and if using pll2
  1828. * it will use PCLK_CRTC2_CNTL (0x484), it then use crtc id to
  1829. * choose which value to write. Which is reverse order from
  1830. * register logic. So only case that works is when pllid is
  1831. * same as crtcid or when both pll and crtc are enabled and
  1832. * both use same clock.
  1833. *
  1834. * So just return crtc id as if crtc and pll were hard linked
  1835. * together even if they aren't
  1836. */
  1837. return radeon_crtc->crtc_id;
  1838. }
  1839. }
  1840. void radeon_atom_disp_eng_pll_init(struct radeon_device *rdev)
  1841. {
  1842. /* always set DCPLL */
  1843. if (ASIC_IS_DCE6(rdev))
  1844. atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
  1845. else if (ASIC_IS_DCE4(rdev)) {
  1846. struct radeon_atom_ss ss;
  1847. bool ss_enabled = radeon_atombios_get_asic_ss_info(rdev, &ss,
  1848. ASIC_INTERNAL_SS_ON_DCPLL,
  1849. rdev->clock.default_dispclk);
  1850. if (ss_enabled)
  1851. atombios_crtc_program_ss(rdev, ATOM_DISABLE, ATOM_DCPLL, -1, &ss);
  1852. /* XXX: DCE5, make sure voltage, dispclk is high enough */
  1853. atombios_crtc_set_disp_eng_pll(rdev, rdev->clock.default_dispclk);
  1854. if (ss_enabled)
  1855. atombios_crtc_program_ss(rdev, ATOM_ENABLE, ATOM_DCPLL, -1, &ss);
  1856. }
  1857. }
  1858. int atombios_crtc_mode_set(struct drm_crtc *crtc,
  1859. struct drm_display_mode *mode,
  1860. struct drm_display_mode *adjusted_mode,
  1861. int x, int y, struct drm_framebuffer *old_fb)
  1862. {
  1863. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1864. struct drm_device *dev = crtc->dev;
  1865. struct radeon_device *rdev = dev->dev_private;
  1866. struct radeon_encoder *radeon_encoder =
  1867. to_radeon_encoder(radeon_crtc->encoder);
  1868. bool is_tvcv = false;
  1869. if (radeon_encoder->active_device &
  1870. (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
  1871. is_tvcv = true;
  1872. if (!radeon_crtc->adjusted_clock)
  1873. return -EINVAL;
  1874. atombios_crtc_set_pll(crtc, adjusted_mode);
  1875. if (ASIC_IS_DCE4(rdev))
  1876. atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
  1877. else if (ASIC_IS_AVIVO(rdev)) {
  1878. if (is_tvcv)
  1879. atombios_crtc_set_timing(crtc, adjusted_mode);
  1880. else
  1881. atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
  1882. } else {
  1883. atombios_crtc_set_timing(crtc, adjusted_mode);
  1884. if (radeon_crtc->crtc_id == 0)
  1885. atombios_set_crtc_dtd_timing(crtc, adjusted_mode);
  1886. radeon_legacy_atom_fixup(crtc);
  1887. }
  1888. atombios_crtc_set_base(crtc, x, y, old_fb);
  1889. atombios_overscan_setup(crtc, mode, adjusted_mode);
  1890. atombios_scaler_setup(crtc);
  1891. radeon_cursor_reset(crtc);
  1892. /* update the hw version fpr dpm */
  1893. radeon_crtc->hw_mode = *adjusted_mode;
  1894. return 0;
  1895. }
  1896. static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
  1897. const struct drm_display_mode *mode,
  1898. struct drm_display_mode *adjusted_mode)
  1899. {
  1900. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1901. struct drm_device *dev = crtc->dev;
  1902. struct drm_encoder *encoder;
  1903. /* assign the encoder to the radeon crtc to avoid repeated lookups later */
  1904. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1905. if (encoder->crtc == crtc) {
  1906. radeon_crtc->encoder = encoder;
  1907. radeon_crtc->connector = radeon_get_connector_for_encoder(encoder);
  1908. break;
  1909. }
  1910. }
  1911. if ((radeon_crtc->encoder == NULL) || (radeon_crtc->connector == NULL)) {
  1912. radeon_crtc->encoder = NULL;
  1913. radeon_crtc->connector = NULL;
  1914. return false;
  1915. }
  1916. if (radeon_crtc->encoder) {
  1917. struct radeon_encoder *radeon_encoder =
  1918. to_radeon_encoder(radeon_crtc->encoder);
  1919. radeon_crtc->output_csc = radeon_encoder->output_csc;
  1920. }
  1921. if (!radeon_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
  1922. return false;
  1923. if (!atombios_crtc_prepare_pll(crtc, adjusted_mode))
  1924. return false;
  1925. /* pick pll */
  1926. radeon_crtc->pll_id = radeon_atom_pick_pll(crtc);
  1927. /* if we can't get a PPLL for a non-DP encoder, fail */
  1928. if ((radeon_crtc->pll_id == ATOM_PPLL_INVALID) &&
  1929. !ENCODER_MODE_IS_DP(atombios_get_encoder_mode(radeon_crtc->encoder)))
  1930. return false;
  1931. return true;
  1932. }
  1933. static void atombios_crtc_prepare(struct drm_crtc *crtc)
  1934. {
  1935. struct drm_device *dev = crtc->dev;
  1936. struct radeon_device *rdev = dev->dev_private;
  1937. /* disable crtc pair power gating before programming */
  1938. if (ASIC_IS_DCE6(rdev))
  1939. atombios_powergate_crtc(crtc, ATOM_DISABLE);
  1940. atombios_lock_crtc(crtc, ATOM_ENABLE);
  1941. atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  1942. }
  1943. static void atombios_crtc_commit(struct drm_crtc *crtc)
  1944. {
  1945. atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  1946. atombios_lock_crtc(crtc, ATOM_DISABLE);
  1947. }
  1948. static void atombios_crtc_disable(struct drm_crtc *crtc)
  1949. {
  1950. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1951. struct drm_device *dev = crtc->dev;
  1952. struct radeon_device *rdev = dev->dev_private;
  1953. struct radeon_atom_ss ss;
  1954. int i;
  1955. atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  1956. if (crtc->primary->fb) {
  1957. int r;
  1958. struct radeon_framebuffer *radeon_fb;
  1959. struct radeon_bo *rbo;
  1960. radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
  1961. rbo = gem_to_radeon_bo(radeon_fb->obj);
  1962. r = radeon_bo_reserve(rbo, false);
  1963. if (unlikely(r))
  1964. DRM_ERROR("failed to reserve rbo before unpin\n");
  1965. else {
  1966. radeon_bo_unpin(rbo);
  1967. radeon_bo_unreserve(rbo);
  1968. }
  1969. }
  1970. /* disable the GRPH */
  1971. if (ASIC_IS_DCE4(rdev))
  1972. WREG32(EVERGREEN_GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
  1973. else if (ASIC_IS_AVIVO(rdev))
  1974. WREG32(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 0);
  1975. if (ASIC_IS_DCE6(rdev))
  1976. atombios_powergate_crtc(crtc, ATOM_ENABLE);
  1977. for (i = 0; i < rdev->num_crtc; i++) {
  1978. if (rdev->mode_info.crtcs[i] &&
  1979. rdev->mode_info.crtcs[i]->enabled &&
  1980. i != radeon_crtc->crtc_id &&
  1981. radeon_crtc->pll_id == rdev->mode_info.crtcs[i]->pll_id) {
  1982. /* one other crtc is using this pll don't turn
  1983. * off the pll
  1984. */
  1985. goto done;
  1986. }
  1987. }
  1988. switch (radeon_crtc->pll_id) {
  1989. case ATOM_PPLL1:
  1990. case ATOM_PPLL2:
  1991. /* disable the ppll */
  1992. atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
  1993. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  1994. break;
  1995. case ATOM_PPLL0:
  1996. /* disable the ppll */
  1997. if ((rdev->family == CHIP_ARUBA) ||
  1998. (rdev->family == CHIP_KAVERI) ||
  1999. (rdev->family == CHIP_BONAIRE) ||
  2000. (rdev->family == CHIP_HAWAII))
  2001. atombios_crtc_program_pll(crtc, radeon_crtc->crtc_id, radeon_crtc->pll_id,
  2002. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  2003. break;
  2004. default:
  2005. break;
  2006. }
  2007. done:
  2008. radeon_crtc->pll_id = ATOM_PPLL_INVALID;
  2009. radeon_crtc->adjusted_clock = 0;
  2010. radeon_crtc->encoder = NULL;
  2011. radeon_crtc->connector = NULL;
  2012. }
  2013. static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
  2014. .dpms = atombios_crtc_dpms,
  2015. .mode_fixup = atombios_crtc_mode_fixup,
  2016. .mode_set = atombios_crtc_mode_set,
  2017. .mode_set_base = atombios_crtc_set_base,
  2018. .mode_set_base_atomic = atombios_crtc_set_base_atomic,
  2019. .prepare = atombios_crtc_prepare,
  2020. .commit = atombios_crtc_commit,
  2021. .load_lut = radeon_crtc_load_lut,
  2022. .disable = atombios_crtc_disable,
  2023. };
  2024. void radeon_atombios_init_crtc(struct drm_device *dev,
  2025. struct radeon_crtc *radeon_crtc)
  2026. {
  2027. struct radeon_device *rdev = dev->dev_private;
  2028. if (ASIC_IS_DCE4(rdev)) {
  2029. switch (radeon_crtc->crtc_id) {
  2030. case 0:
  2031. default:
  2032. radeon_crtc->crtc_offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
  2033. break;
  2034. case 1:
  2035. radeon_crtc->crtc_offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
  2036. break;
  2037. case 2:
  2038. radeon_crtc->crtc_offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
  2039. break;
  2040. case 3:
  2041. radeon_crtc->crtc_offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
  2042. break;
  2043. case 4:
  2044. radeon_crtc->crtc_offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
  2045. break;
  2046. case 5:
  2047. radeon_crtc->crtc_offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
  2048. break;
  2049. }
  2050. } else {
  2051. if (radeon_crtc->crtc_id == 1)
  2052. radeon_crtc->crtc_offset =
  2053. AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
  2054. else
  2055. radeon_crtc->crtc_offset = 0;
  2056. }
  2057. radeon_crtc->pll_id = ATOM_PPLL_INVALID;
  2058. radeon_crtc->adjusted_clock = 0;
  2059. radeon_crtc->encoder = NULL;
  2060. radeon_crtc->connector = NULL;
  2061. drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);
  2062. }