dce_v10_0.c 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include "drmP.h"
  24. #include "amdgpu.h"
  25. #include "amdgpu_pm.h"
  26. #include "amdgpu_i2c.h"
  27. #include "vid.h"
  28. #include "atom.h"
  29. #include "amdgpu_atombios.h"
  30. #include "atombios_crtc.h"
  31. #include "atombios_encoders.h"
  32. #include "amdgpu_pll.h"
  33. #include "amdgpu_connectors.h"
  34. #include "dce/dce_10_0_d.h"
  35. #include "dce/dce_10_0_sh_mask.h"
  36. #include "dce/dce_10_0_enum.h"
  37. #include "oss/oss_3_0_d.h"
  38. #include "oss/oss_3_0_sh_mask.h"
  39. #include "gmc/gmc_8_1_d.h"
  40. #include "gmc/gmc_8_1_sh_mask.h"
  41. static void dce_v10_0_set_display_funcs(struct amdgpu_device *adev);
  42. static void dce_v10_0_set_irq_funcs(struct amdgpu_device *adev);
  43. static const u32 crtc_offsets[] =
  44. {
  45. CRTC0_REGISTER_OFFSET,
  46. CRTC1_REGISTER_OFFSET,
  47. CRTC2_REGISTER_OFFSET,
  48. CRTC3_REGISTER_OFFSET,
  49. CRTC4_REGISTER_OFFSET,
  50. CRTC5_REGISTER_OFFSET,
  51. CRTC6_REGISTER_OFFSET
  52. };
  53. static const u32 hpd_offsets[] =
  54. {
  55. HPD0_REGISTER_OFFSET,
  56. HPD1_REGISTER_OFFSET,
  57. HPD2_REGISTER_OFFSET,
  58. HPD3_REGISTER_OFFSET,
  59. HPD4_REGISTER_OFFSET,
  60. HPD5_REGISTER_OFFSET
  61. };
  62. static const uint32_t dig_offsets[] = {
  63. DIG0_REGISTER_OFFSET,
  64. DIG1_REGISTER_OFFSET,
  65. DIG2_REGISTER_OFFSET,
  66. DIG3_REGISTER_OFFSET,
  67. DIG4_REGISTER_OFFSET,
  68. DIG5_REGISTER_OFFSET,
  69. DIG6_REGISTER_OFFSET
  70. };
  71. static const struct {
  72. uint32_t reg;
  73. uint32_t vblank;
  74. uint32_t vline;
  75. uint32_t hpd;
  76. } interrupt_status_offsets[] = { {
  77. .reg = mmDISP_INTERRUPT_STATUS,
  78. .vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
  79. .vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
  80. .hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
  81. }, {
  82. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE,
  83. .vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
  84. .vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
  85. .hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
  86. }, {
  87. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE2,
  88. .vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
  89. .vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
  90. .hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
  91. }, {
  92. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE3,
  93. .vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
  94. .vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
  95. .hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
  96. }, {
  97. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE4,
  98. .vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
  99. .vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
  100. .hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
  101. }, {
  102. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE5,
  103. .vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
  104. .vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
  105. .hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
  106. } };
  107. static const u32 golden_settings_tonga_a11[] =
  108. {
  109. mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
  110. mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
  111. mmFBC_MISC, 0x1f311fff, 0x12300000,
  112. mmHDMI_CONTROL, 0x31000111, 0x00000011,
  113. };
  114. static const u32 tonga_mgcg_cgcg_init[] =
  115. {
  116. mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
  117. mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
  118. };
  119. static const u32 golden_settings_fiji_a10[] =
  120. {
  121. mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
  122. mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
  123. mmFBC_MISC, 0x1f311fff, 0x12300000,
  124. mmHDMI_CONTROL, 0x31000111, 0x00000011,
  125. };
  126. static const u32 fiji_mgcg_cgcg_init[] =
  127. {
  128. mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
  129. mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
  130. };
  131. static void dce_v10_0_init_golden_registers(struct amdgpu_device *adev)
  132. {
  133. switch (adev->asic_type) {
  134. case CHIP_FIJI:
  135. amdgpu_program_register_sequence(adev,
  136. fiji_mgcg_cgcg_init,
  137. (const u32)ARRAY_SIZE(fiji_mgcg_cgcg_init));
  138. amdgpu_program_register_sequence(adev,
  139. golden_settings_fiji_a10,
  140. (const u32)ARRAY_SIZE(golden_settings_fiji_a10));
  141. break;
  142. case CHIP_TONGA:
  143. amdgpu_program_register_sequence(adev,
  144. tonga_mgcg_cgcg_init,
  145. (const u32)ARRAY_SIZE(tonga_mgcg_cgcg_init));
  146. amdgpu_program_register_sequence(adev,
  147. golden_settings_tonga_a11,
  148. (const u32)ARRAY_SIZE(golden_settings_tonga_a11));
  149. break;
  150. default:
  151. break;
  152. }
  153. }
  154. static u32 dce_v10_0_audio_endpt_rreg(struct amdgpu_device *adev,
  155. u32 block_offset, u32 reg)
  156. {
  157. unsigned long flags;
  158. u32 r;
  159. spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
  160. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
  161. r = RREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset);
  162. spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
  163. return r;
  164. }
  165. static void dce_v10_0_audio_endpt_wreg(struct amdgpu_device *adev,
  166. u32 block_offset, u32 reg, u32 v)
  167. {
  168. unsigned long flags;
  169. spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
  170. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
  171. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset, v);
  172. spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
  173. }
  174. static bool dce_v10_0_is_in_vblank(struct amdgpu_device *adev, int crtc)
  175. {
  176. if (RREG32(mmCRTC_STATUS + crtc_offsets[crtc]) &
  177. CRTC_V_BLANK_START_END__CRTC_V_BLANK_START_MASK)
  178. return true;
  179. else
  180. return false;
  181. }
  182. static bool dce_v10_0_is_counter_moving(struct amdgpu_device *adev, int crtc)
  183. {
  184. u32 pos1, pos2;
  185. pos1 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  186. pos2 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  187. if (pos1 != pos2)
  188. return true;
  189. else
  190. return false;
  191. }
  192. /**
  193. * dce_v10_0_vblank_wait - vblank wait asic callback.
  194. *
  195. * @adev: amdgpu_device pointer
  196. * @crtc: crtc to wait for vblank on
  197. *
  198. * Wait for vblank on the requested crtc (evergreen+).
  199. */
  200. static void dce_v10_0_vblank_wait(struct amdgpu_device *adev, int crtc)
  201. {
  202. unsigned i = 0;
  203. if (crtc >= adev->mode_info.num_crtc)
  204. return;
  205. if (!(RREG32(mmCRTC_CONTROL + crtc_offsets[crtc]) & CRTC_CONTROL__CRTC_MASTER_EN_MASK))
  206. return;
  207. /* depending on when we hit vblank, we may be close to active; if so,
  208. * wait for another frame.
  209. */
  210. while (dce_v10_0_is_in_vblank(adev, crtc)) {
  211. if (i++ % 100 == 0) {
  212. if (!dce_v10_0_is_counter_moving(adev, crtc))
  213. break;
  214. }
  215. }
  216. while (!dce_v10_0_is_in_vblank(adev, crtc)) {
  217. if (i++ % 100 == 0) {
  218. if (!dce_v10_0_is_counter_moving(adev, crtc))
  219. break;
  220. }
  221. }
  222. }
  223. static u32 dce_v10_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
  224. {
  225. if (crtc >= adev->mode_info.num_crtc)
  226. return 0;
  227. else
  228. return RREG32(mmCRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
  229. }
  230. static void dce_v10_0_pageflip_interrupt_init(struct amdgpu_device *adev)
  231. {
  232. unsigned i;
  233. /* Enable pflip interrupts */
  234. for (i = 0; i < adev->mode_info.num_crtc; i++)
  235. amdgpu_irq_get(adev, &adev->pageflip_irq, i);
  236. }
  237. static void dce_v10_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
  238. {
  239. unsigned i;
  240. /* Disable pflip interrupts */
  241. for (i = 0; i < adev->mode_info.num_crtc; i++)
  242. amdgpu_irq_put(adev, &adev->pageflip_irq, i);
  243. }
  244. /**
  245. * dce_v10_0_page_flip - pageflip callback.
  246. *
  247. * @adev: amdgpu_device pointer
  248. * @crtc_id: crtc to cleanup pageflip on
  249. * @crtc_base: new address of the crtc (GPU MC address)
  250. *
  251. * Triggers the actual pageflip by updating the primary
  252. * surface base address.
  253. */
  254. static void dce_v10_0_page_flip(struct amdgpu_device *adev,
  255. int crtc_id, u64 crtc_base)
  256. {
  257. struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  258. /* update the primary scanout address */
  259. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  260. upper_32_bits(crtc_base));
  261. /* writing to the low address triggers the update */
  262. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  263. lower_32_bits(crtc_base));
  264. /* post the write */
  265. RREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
  266. }
  267. static int dce_v10_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
  268. u32 *vbl, u32 *position)
  269. {
  270. if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
  271. return -EINVAL;
  272. *vbl = RREG32(mmCRTC_V_BLANK_START_END + crtc_offsets[crtc]);
  273. *position = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  274. return 0;
  275. }
  276. /**
  277. * dce_v10_0_hpd_sense - hpd sense callback.
  278. *
  279. * @adev: amdgpu_device pointer
  280. * @hpd: hpd (hotplug detect) pin
  281. *
  282. * Checks if a digital monitor is connected (evergreen+).
  283. * Returns true if connected, false if not connected.
  284. */
  285. static bool dce_v10_0_hpd_sense(struct amdgpu_device *adev,
  286. enum amdgpu_hpd_id hpd)
  287. {
  288. int idx;
  289. bool connected = false;
  290. switch (hpd) {
  291. case AMDGPU_HPD_1:
  292. idx = 0;
  293. break;
  294. case AMDGPU_HPD_2:
  295. idx = 1;
  296. break;
  297. case AMDGPU_HPD_3:
  298. idx = 2;
  299. break;
  300. case AMDGPU_HPD_4:
  301. idx = 3;
  302. break;
  303. case AMDGPU_HPD_5:
  304. idx = 4;
  305. break;
  306. case AMDGPU_HPD_6:
  307. idx = 5;
  308. break;
  309. default:
  310. return connected;
  311. }
  312. if (RREG32(mmDC_HPD_INT_STATUS + hpd_offsets[idx]) &
  313. DC_HPD_INT_STATUS__DC_HPD_SENSE_MASK)
  314. connected = true;
  315. return connected;
  316. }
  317. /**
  318. * dce_v10_0_hpd_set_polarity - hpd set polarity callback.
  319. *
  320. * @adev: amdgpu_device pointer
  321. * @hpd: hpd (hotplug detect) pin
  322. *
  323. * Set the polarity of the hpd pin (evergreen+).
  324. */
  325. static void dce_v10_0_hpd_set_polarity(struct amdgpu_device *adev,
  326. enum amdgpu_hpd_id hpd)
  327. {
  328. u32 tmp;
  329. bool connected = dce_v10_0_hpd_sense(adev, hpd);
  330. int idx;
  331. switch (hpd) {
  332. case AMDGPU_HPD_1:
  333. idx = 0;
  334. break;
  335. case AMDGPU_HPD_2:
  336. idx = 1;
  337. break;
  338. case AMDGPU_HPD_3:
  339. idx = 2;
  340. break;
  341. case AMDGPU_HPD_4:
  342. idx = 3;
  343. break;
  344. case AMDGPU_HPD_5:
  345. idx = 4;
  346. break;
  347. case AMDGPU_HPD_6:
  348. idx = 5;
  349. break;
  350. default:
  351. return;
  352. }
  353. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[idx]);
  354. if (connected)
  355. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 0);
  356. else
  357. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 1);
  358. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[idx], tmp);
  359. }
  360. /**
  361. * dce_v10_0_hpd_init - hpd setup callback.
  362. *
  363. * @adev: amdgpu_device pointer
  364. *
  365. * Setup the hpd pins used by the card (evergreen+).
  366. * Enable the pin, set the polarity, and enable the hpd interrupts.
  367. */
  368. static void dce_v10_0_hpd_init(struct amdgpu_device *adev)
  369. {
  370. struct drm_device *dev = adev->ddev;
  371. struct drm_connector *connector;
  372. u32 tmp;
  373. int idx;
  374. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  375. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  376. switch (amdgpu_connector->hpd.hpd) {
  377. case AMDGPU_HPD_1:
  378. idx = 0;
  379. break;
  380. case AMDGPU_HPD_2:
  381. idx = 1;
  382. break;
  383. case AMDGPU_HPD_3:
  384. idx = 2;
  385. break;
  386. case AMDGPU_HPD_4:
  387. idx = 3;
  388. break;
  389. case AMDGPU_HPD_5:
  390. idx = 4;
  391. break;
  392. case AMDGPU_HPD_6:
  393. idx = 5;
  394. break;
  395. default:
  396. continue;
  397. }
  398. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
  399. connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
  400. /* don't try to enable hpd on eDP or LVDS avoid breaking the
  401. * aux dp channel on imac and help (but not completely fix)
  402. * https://bugzilla.redhat.com/show_bug.cgi?id=726143
  403. * also avoid interrupt storms during dpms.
  404. */
  405. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[idx]);
  406. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
  407. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[idx], tmp);
  408. continue;
  409. }
  410. tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[idx]);
  411. tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 1);
  412. WREG32(mmDC_HPD_CONTROL + hpd_offsets[idx], tmp);
  413. tmp = RREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[idx]);
  414. tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
  415. DC_HPD_CONNECT_INT_DELAY,
  416. AMDGPU_HPD_CONNECT_INT_DELAY_IN_MS);
  417. tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
  418. DC_HPD_DISCONNECT_INT_DELAY,
  419. AMDGPU_HPD_DISCONNECT_INT_DELAY_IN_MS);
  420. WREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[idx], tmp);
  421. dce_v10_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
  422. amdgpu_irq_get(adev, &adev->hpd_irq,
  423. amdgpu_connector->hpd.hpd);
  424. }
  425. }
  426. /**
  427. * dce_v10_0_hpd_fini - hpd tear down callback.
  428. *
  429. * @adev: amdgpu_device pointer
  430. *
  431. * Tear down the hpd pins used by the card (evergreen+).
  432. * Disable the hpd interrupts.
  433. */
  434. static void dce_v10_0_hpd_fini(struct amdgpu_device *adev)
  435. {
  436. struct drm_device *dev = adev->ddev;
  437. struct drm_connector *connector;
  438. u32 tmp;
  439. int idx;
  440. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  441. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  442. switch (amdgpu_connector->hpd.hpd) {
  443. case AMDGPU_HPD_1:
  444. idx = 0;
  445. break;
  446. case AMDGPU_HPD_2:
  447. idx = 1;
  448. break;
  449. case AMDGPU_HPD_3:
  450. idx = 2;
  451. break;
  452. case AMDGPU_HPD_4:
  453. idx = 3;
  454. break;
  455. case AMDGPU_HPD_5:
  456. idx = 4;
  457. break;
  458. case AMDGPU_HPD_6:
  459. idx = 5;
  460. break;
  461. default:
  462. continue;
  463. }
  464. tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[idx]);
  465. tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 0);
  466. WREG32(mmDC_HPD_CONTROL + hpd_offsets[idx], tmp);
  467. amdgpu_irq_put(adev, &adev->hpd_irq,
  468. amdgpu_connector->hpd.hpd);
  469. }
  470. }
  471. static u32 dce_v10_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
  472. {
  473. return mmDC_GPIO_HPD_A;
  474. }
  475. static bool dce_v10_0_is_display_hung(struct amdgpu_device *adev)
  476. {
  477. u32 crtc_hung = 0;
  478. u32 crtc_status[6];
  479. u32 i, j, tmp;
  480. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  481. tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
  482. if (REG_GET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN)) {
  483. crtc_status[i] = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
  484. crtc_hung |= (1 << i);
  485. }
  486. }
  487. for (j = 0; j < 10; j++) {
  488. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  489. if (crtc_hung & (1 << i)) {
  490. tmp = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
  491. if (tmp != crtc_status[i])
  492. crtc_hung &= ~(1 << i);
  493. }
  494. }
  495. if (crtc_hung == 0)
  496. return false;
  497. udelay(100);
  498. }
  499. return true;
  500. }
  501. static void dce_v10_0_stop_mc_access(struct amdgpu_device *adev,
  502. struct amdgpu_mode_mc_save *save)
  503. {
  504. u32 crtc_enabled, tmp;
  505. int i;
  506. save->vga_render_control = RREG32(mmVGA_RENDER_CONTROL);
  507. save->vga_hdp_control = RREG32(mmVGA_HDP_CONTROL);
  508. /* disable VGA render */
  509. tmp = RREG32(mmVGA_RENDER_CONTROL);
  510. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
  511. WREG32(mmVGA_RENDER_CONTROL, tmp);
  512. /* blank the display controllers */
  513. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  514. crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
  515. CRTC_CONTROL, CRTC_MASTER_EN);
  516. if (crtc_enabled) {
  517. #if 0
  518. u32 frame_count;
  519. int j;
  520. save->crtc_enabled[i] = true;
  521. tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
  522. if (REG_GET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN) == 0) {
  523. amdgpu_display_vblank_wait(adev, i);
  524. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
  525. tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 1);
  526. WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
  527. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
  528. }
  529. /* wait for the next frame */
  530. frame_count = amdgpu_display_vblank_get_counter(adev, i);
  531. for (j = 0; j < adev->usec_timeout; j++) {
  532. if (amdgpu_display_vblank_get_counter(adev, i) != frame_count)
  533. break;
  534. udelay(1);
  535. }
  536. tmp = RREG32(mmGRPH_UPDATE + crtc_offsets[i]);
  537. if (REG_GET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK) == 0) {
  538. tmp = REG_SET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK, 1);
  539. WREG32(mmGRPH_UPDATE + crtc_offsets[i], tmp);
  540. }
  541. tmp = RREG32(mmMASTER_UPDATE_LOCK + crtc_offsets[i]);
  542. if (REG_GET_FIELD(tmp, MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK) == 0) {
  543. tmp = REG_SET_FIELD(tmp, MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK, 1);
  544. WREG32(mmMASTER_UPDATE_LOCK + crtc_offsets[i], tmp);
  545. }
  546. #else
  547. /* XXX this is a hack to avoid strange behavior with EFI on certain systems */
  548. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
  549. tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
  550. tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
  551. WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
  552. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
  553. save->crtc_enabled[i] = false;
  554. /* ***** */
  555. #endif
  556. } else {
  557. save->crtc_enabled[i] = false;
  558. }
  559. }
  560. }
  561. static void dce_v10_0_resume_mc_access(struct amdgpu_device *adev,
  562. struct amdgpu_mode_mc_save *save)
  563. {
  564. u32 tmp, frame_count;
  565. int i, j;
  566. /* update crtc base addresses */
  567. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  568. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + crtc_offsets[i],
  569. upper_32_bits(adev->mc.vram_start));
  570. WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + crtc_offsets[i],
  571. upper_32_bits(adev->mc.vram_start));
  572. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + crtc_offsets[i],
  573. (u32)adev->mc.vram_start);
  574. WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + crtc_offsets[i],
  575. (u32)adev->mc.vram_start);
  576. if (save->crtc_enabled[i]) {
  577. tmp = RREG32(mmMASTER_UPDATE_MODE + crtc_offsets[i]);
  578. if (REG_GET_FIELD(tmp, MASTER_UPDATE_MODE, MASTER_UPDATE_MODE) != 3) {
  579. tmp = REG_SET_FIELD(tmp, MASTER_UPDATE_MODE, MASTER_UPDATE_MODE, 3);
  580. WREG32(mmMASTER_UPDATE_MODE + crtc_offsets[i], tmp);
  581. }
  582. tmp = RREG32(mmGRPH_UPDATE + crtc_offsets[i]);
  583. if (REG_GET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK)) {
  584. tmp = REG_SET_FIELD(tmp, GRPH_UPDATE, GRPH_UPDATE_LOCK, 0);
  585. WREG32(mmGRPH_UPDATE + crtc_offsets[i], tmp);
  586. }
  587. tmp = RREG32(mmMASTER_UPDATE_LOCK + crtc_offsets[i]);
  588. if (REG_GET_FIELD(tmp, MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK)) {
  589. tmp = REG_SET_FIELD(tmp, MASTER_UPDATE_LOCK, MASTER_UPDATE_LOCK, 0);
  590. WREG32(mmMASTER_UPDATE_LOCK + crtc_offsets[i], tmp);
  591. }
  592. for (j = 0; j < adev->usec_timeout; j++) {
  593. tmp = RREG32(mmGRPH_UPDATE + crtc_offsets[i]);
  594. if (REG_GET_FIELD(tmp, GRPH_UPDATE, GRPH_SURFACE_UPDATE_PENDING) == 0)
  595. break;
  596. udelay(1);
  597. }
  598. tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
  599. tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 0);
  600. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
  601. WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
  602. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
  603. /* wait for the next frame */
  604. frame_count = amdgpu_display_vblank_get_counter(adev, i);
  605. for (j = 0; j < adev->usec_timeout; j++) {
  606. if (amdgpu_display_vblank_get_counter(adev, i) != frame_count)
  607. break;
  608. udelay(1);
  609. }
  610. }
  611. }
  612. WREG32(mmVGA_MEMORY_BASE_ADDRESS_HIGH, upper_32_bits(adev->mc.vram_start));
  613. WREG32(mmVGA_MEMORY_BASE_ADDRESS, lower_32_bits(adev->mc.vram_start));
  614. /* Unlock vga access */
  615. WREG32(mmVGA_HDP_CONTROL, save->vga_hdp_control);
  616. mdelay(1);
  617. WREG32(mmVGA_RENDER_CONTROL, save->vga_render_control);
  618. }
  619. static void dce_v10_0_set_vga_render_state(struct amdgpu_device *adev,
  620. bool render)
  621. {
  622. u32 tmp;
  623. /* Lockout access through VGA aperture*/
  624. tmp = RREG32(mmVGA_HDP_CONTROL);
  625. if (render)
  626. tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 0);
  627. else
  628. tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
  629. WREG32(mmVGA_HDP_CONTROL, tmp);
  630. /* disable VGA render */
  631. tmp = RREG32(mmVGA_RENDER_CONTROL);
  632. if (render)
  633. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 1);
  634. else
  635. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
  636. WREG32(mmVGA_RENDER_CONTROL, tmp);
  637. }
  638. static void dce_v10_0_program_fmt(struct drm_encoder *encoder)
  639. {
  640. struct drm_device *dev = encoder->dev;
  641. struct amdgpu_device *adev = dev->dev_private;
  642. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  643. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  644. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  645. int bpc = 0;
  646. u32 tmp = 0;
  647. enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
  648. if (connector) {
  649. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  650. bpc = amdgpu_connector_get_monitor_bpc(connector);
  651. dither = amdgpu_connector->dither;
  652. }
  653. /* LVDS/eDP FMT is set up by atom */
  654. if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
  655. return;
  656. /* not needed for analog */
  657. if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1) ||
  658. (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2))
  659. return;
  660. if (bpc == 0)
  661. return;
  662. switch (bpc) {
  663. case 6:
  664. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  665. /* XXX sort out optimal dither settings */
  666. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  667. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  668. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  669. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 0);
  670. } else {
  671. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  672. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 0);
  673. }
  674. break;
  675. case 8:
  676. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  677. /* XXX sort out optimal dither settings */
  678. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  679. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  680. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
  681. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  682. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 1);
  683. } else {
  684. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  685. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 1);
  686. }
  687. break;
  688. case 10:
  689. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  690. /* XXX sort out optimal dither settings */
  691. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  692. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  693. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
  694. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  695. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 2);
  696. } else {
  697. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  698. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 2);
  699. }
  700. break;
  701. default:
  702. /* not needed */
  703. break;
  704. }
  705. WREG32(mmFMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  706. }
  707. /* display watermark setup */
  708. /**
  709. * dce_v10_0_line_buffer_adjust - Set up the line buffer
  710. *
  711. * @adev: amdgpu_device pointer
  712. * @amdgpu_crtc: the selected display controller
  713. * @mode: the current display mode on the selected display
  714. * controller
  715. *
  716. * Setup up the line buffer allocation for
  717. * the selected display controller (CIK).
  718. * Returns the line buffer size in pixels.
  719. */
  720. static u32 dce_v10_0_line_buffer_adjust(struct amdgpu_device *adev,
  721. struct amdgpu_crtc *amdgpu_crtc,
  722. struct drm_display_mode *mode)
  723. {
  724. u32 tmp, buffer_alloc, i, mem_cfg;
  725. u32 pipe_offset = amdgpu_crtc->crtc_id;
  726. /*
  727. * Line Buffer Setup
  728. * There are 6 line buffers, one for each display controllers.
  729. * There are 3 partitions per LB. Select the number of partitions
  730. * to enable based on the display width. For display widths larger
  731. * than 4096, you need use to use 2 display controllers and combine
  732. * them using the stereo blender.
  733. */
  734. if (amdgpu_crtc->base.enabled && mode) {
  735. if (mode->crtc_hdisplay < 1920) {
  736. mem_cfg = 1;
  737. buffer_alloc = 2;
  738. } else if (mode->crtc_hdisplay < 2560) {
  739. mem_cfg = 2;
  740. buffer_alloc = 2;
  741. } else if (mode->crtc_hdisplay < 4096) {
  742. mem_cfg = 0;
  743. buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
  744. } else {
  745. DRM_DEBUG_KMS("Mode too big for LB!\n");
  746. mem_cfg = 0;
  747. buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
  748. }
  749. } else {
  750. mem_cfg = 1;
  751. buffer_alloc = 0;
  752. }
  753. tmp = RREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset);
  754. tmp = REG_SET_FIELD(tmp, LB_MEMORY_CTRL, LB_MEMORY_CONFIG, mem_cfg);
  755. WREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset, tmp);
  756. tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
  757. tmp = REG_SET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATED, buffer_alloc);
  758. WREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset, tmp);
  759. for (i = 0; i < adev->usec_timeout; i++) {
  760. tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
  761. if (REG_GET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATION_COMPLETED))
  762. break;
  763. udelay(1);
  764. }
  765. if (amdgpu_crtc->base.enabled && mode) {
  766. switch (mem_cfg) {
  767. case 0:
  768. default:
  769. return 4096 * 2;
  770. case 1:
  771. return 1920 * 2;
  772. case 2:
  773. return 2560 * 2;
  774. }
  775. }
  776. /* controller not enabled, so no lb used */
  777. return 0;
  778. }
  779. /**
  780. * cik_get_number_of_dram_channels - get the number of dram channels
  781. *
  782. * @adev: amdgpu_device pointer
  783. *
  784. * Look up the number of video ram channels (CIK).
  785. * Used for display watermark bandwidth calculations
  786. * Returns the number of dram channels
  787. */
  788. static u32 cik_get_number_of_dram_channels(struct amdgpu_device *adev)
  789. {
  790. u32 tmp = RREG32(mmMC_SHARED_CHMAP);
  791. switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
  792. case 0:
  793. default:
  794. return 1;
  795. case 1:
  796. return 2;
  797. case 2:
  798. return 4;
  799. case 3:
  800. return 8;
  801. case 4:
  802. return 3;
  803. case 5:
  804. return 6;
  805. case 6:
  806. return 10;
  807. case 7:
  808. return 12;
  809. case 8:
  810. return 16;
  811. }
  812. }
  813. struct dce10_wm_params {
  814. u32 dram_channels; /* number of dram channels */
  815. u32 yclk; /* bandwidth per dram data pin in kHz */
  816. u32 sclk; /* engine clock in kHz */
  817. u32 disp_clk; /* display clock in kHz */
  818. u32 src_width; /* viewport width */
  819. u32 active_time; /* active display time in ns */
  820. u32 blank_time; /* blank time in ns */
  821. bool interlaced; /* mode is interlaced */
  822. fixed20_12 vsc; /* vertical scale ratio */
  823. u32 num_heads; /* number of active crtcs */
  824. u32 bytes_per_pixel; /* bytes per pixel display + overlay */
  825. u32 lb_size; /* line buffer allocated to pipe */
  826. u32 vtaps; /* vertical scaler taps */
  827. };
  828. /**
  829. * dce_v10_0_dram_bandwidth - get the dram bandwidth
  830. *
  831. * @wm: watermark calculation data
  832. *
  833. * Calculate the raw dram bandwidth (CIK).
  834. * Used for display watermark bandwidth calculations
  835. * Returns the dram bandwidth in MBytes/s
  836. */
  837. static u32 dce_v10_0_dram_bandwidth(struct dce10_wm_params *wm)
  838. {
  839. /* Calculate raw DRAM Bandwidth */
  840. fixed20_12 dram_efficiency; /* 0.7 */
  841. fixed20_12 yclk, dram_channels, bandwidth;
  842. fixed20_12 a;
  843. a.full = dfixed_const(1000);
  844. yclk.full = dfixed_const(wm->yclk);
  845. yclk.full = dfixed_div(yclk, a);
  846. dram_channels.full = dfixed_const(wm->dram_channels * 4);
  847. a.full = dfixed_const(10);
  848. dram_efficiency.full = dfixed_const(7);
  849. dram_efficiency.full = dfixed_div(dram_efficiency, a);
  850. bandwidth.full = dfixed_mul(dram_channels, yclk);
  851. bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
  852. return dfixed_trunc(bandwidth);
  853. }
  854. /**
  855. * dce_v10_0_dram_bandwidth_for_display - get the dram bandwidth for display
  856. *
  857. * @wm: watermark calculation data
  858. *
  859. * Calculate the dram bandwidth used for display (CIK).
  860. * Used for display watermark bandwidth calculations
  861. * Returns the dram bandwidth for display in MBytes/s
  862. */
  863. static u32 dce_v10_0_dram_bandwidth_for_display(struct dce10_wm_params *wm)
  864. {
  865. /* Calculate DRAM Bandwidth and the part allocated to display. */
  866. fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
  867. fixed20_12 yclk, dram_channels, bandwidth;
  868. fixed20_12 a;
  869. a.full = dfixed_const(1000);
  870. yclk.full = dfixed_const(wm->yclk);
  871. yclk.full = dfixed_div(yclk, a);
  872. dram_channels.full = dfixed_const(wm->dram_channels * 4);
  873. a.full = dfixed_const(10);
  874. disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
  875. disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
  876. bandwidth.full = dfixed_mul(dram_channels, yclk);
  877. bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
  878. return dfixed_trunc(bandwidth);
  879. }
  880. /**
  881. * dce_v10_0_data_return_bandwidth - get the data return bandwidth
  882. *
  883. * @wm: watermark calculation data
  884. *
  885. * Calculate the data return bandwidth used for display (CIK).
  886. * Used for display watermark bandwidth calculations
  887. * Returns the data return bandwidth in MBytes/s
  888. */
  889. static u32 dce_v10_0_data_return_bandwidth(struct dce10_wm_params *wm)
  890. {
  891. /* Calculate the display Data return Bandwidth */
  892. fixed20_12 return_efficiency; /* 0.8 */
  893. fixed20_12 sclk, bandwidth;
  894. fixed20_12 a;
  895. a.full = dfixed_const(1000);
  896. sclk.full = dfixed_const(wm->sclk);
  897. sclk.full = dfixed_div(sclk, a);
  898. a.full = dfixed_const(10);
  899. return_efficiency.full = dfixed_const(8);
  900. return_efficiency.full = dfixed_div(return_efficiency, a);
  901. a.full = dfixed_const(32);
  902. bandwidth.full = dfixed_mul(a, sclk);
  903. bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
  904. return dfixed_trunc(bandwidth);
  905. }
  906. /**
  907. * dce_v10_0_dmif_request_bandwidth - get the dmif bandwidth
  908. *
  909. * @wm: watermark calculation data
  910. *
  911. * Calculate the dmif bandwidth used for display (CIK).
  912. * Used for display watermark bandwidth calculations
  913. * Returns the dmif bandwidth in MBytes/s
  914. */
  915. static u32 dce_v10_0_dmif_request_bandwidth(struct dce10_wm_params *wm)
  916. {
  917. /* Calculate the DMIF Request Bandwidth */
  918. fixed20_12 disp_clk_request_efficiency; /* 0.8 */
  919. fixed20_12 disp_clk, bandwidth;
  920. fixed20_12 a, b;
  921. a.full = dfixed_const(1000);
  922. disp_clk.full = dfixed_const(wm->disp_clk);
  923. disp_clk.full = dfixed_div(disp_clk, a);
  924. a.full = dfixed_const(32);
  925. b.full = dfixed_mul(a, disp_clk);
  926. a.full = dfixed_const(10);
  927. disp_clk_request_efficiency.full = dfixed_const(8);
  928. disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
  929. bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
  930. return dfixed_trunc(bandwidth);
  931. }
  932. /**
  933. * dce_v10_0_available_bandwidth - get the min available bandwidth
  934. *
  935. * @wm: watermark calculation data
  936. *
  937. * Calculate the min available bandwidth used for display (CIK).
  938. * Used for display watermark bandwidth calculations
  939. * Returns the min available bandwidth in MBytes/s
  940. */
  941. static u32 dce_v10_0_available_bandwidth(struct dce10_wm_params *wm)
  942. {
  943. /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
  944. u32 dram_bandwidth = dce_v10_0_dram_bandwidth(wm);
  945. u32 data_return_bandwidth = dce_v10_0_data_return_bandwidth(wm);
  946. u32 dmif_req_bandwidth = dce_v10_0_dmif_request_bandwidth(wm);
  947. return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
  948. }
  949. /**
  950. * dce_v10_0_average_bandwidth - get the average available bandwidth
  951. *
  952. * @wm: watermark calculation data
  953. *
  954. * Calculate the average available bandwidth used for display (CIK).
  955. * Used for display watermark bandwidth calculations
  956. * Returns the average available bandwidth in MBytes/s
  957. */
  958. static u32 dce_v10_0_average_bandwidth(struct dce10_wm_params *wm)
  959. {
  960. /* Calculate the display mode Average Bandwidth
  961. * DisplayMode should contain the source and destination dimensions,
  962. * timing, etc.
  963. */
  964. fixed20_12 bpp;
  965. fixed20_12 line_time;
  966. fixed20_12 src_width;
  967. fixed20_12 bandwidth;
  968. fixed20_12 a;
  969. a.full = dfixed_const(1000);
  970. line_time.full = dfixed_const(wm->active_time + wm->blank_time);
  971. line_time.full = dfixed_div(line_time, a);
  972. bpp.full = dfixed_const(wm->bytes_per_pixel);
  973. src_width.full = dfixed_const(wm->src_width);
  974. bandwidth.full = dfixed_mul(src_width, bpp);
  975. bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
  976. bandwidth.full = dfixed_div(bandwidth, line_time);
  977. return dfixed_trunc(bandwidth);
  978. }
  979. /**
  980. * dce_v10_0_latency_watermark - get the latency watermark
  981. *
  982. * @wm: watermark calculation data
  983. *
  984. * Calculate the latency watermark (CIK).
  985. * Used for display watermark bandwidth calculations
  986. * Returns the latency watermark in ns
  987. */
  988. static u32 dce_v10_0_latency_watermark(struct dce10_wm_params *wm)
  989. {
  990. /* First calculate the latency in ns */
  991. u32 mc_latency = 2000; /* 2000 ns. */
  992. u32 available_bandwidth = dce_v10_0_available_bandwidth(wm);
  993. u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
  994. u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
  995. u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
  996. u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
  997. (wm->num_heads * cursor_line_pair_return_time);
  998. u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
  999. u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
  1000. u32 tmp, dmif_size = 12288;
  1001. fixed20_12 a, b, c;
  1002. if (wm->num_heads == 0)
  1003. return 0;
  1004. a.full = dfixed_const(2);
  1005. b.full = dfixed_const(1);
  1006. if ((wm->vsc.full > a.full) ||
  1007. ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
  1008. (wm->vtaps >= 5) ||
  1009. ((wm->vsc.full >= a.full) && wm->interlaced))
  1010. max_src_lines_per_dst_line = 4;
  1011. else
  1012. max_src_lines_per_dst_line = 2;
  1013. a.full = dfixed_const(available_bandwidth);
  1014. b.full = dfixed_const(wm->num_heads);
  1015. a.full = dfixed_div(a, b);
  1016. tmp = div_u64((u64) dmif_size * (u64) wm->disp_clk, mc_latency + 512);
  1017. tmp = min(dfixed_trunc(a), tmp);
  1018. lb_fill_bw = min(tmp, wm->disp_clk * wm->bytes_per_pixel / 1000);
  1019. a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
  1020. b.full = dfixed_const(1000);
  1021. c.full = dfixed_const(lb_fill_bw);
  1022. b.full = dfixed_div(c, b);
  1023. a.full = dfixed_div(a, b);
  1024. line_fill_time = dfixed_trunc(a);
  1025. if (line_fill_time < wm->active_time)
  1026. return latency;
  1027. else
  1028. return latency + (line_fill_time - wm->active_time);
  1029. }
  1030. /**
  1031. * dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display - check
  1032. * average and available dram bandwidth
  1033. *
  1034. * @wm: watermark calculation data
  1035. *
  1036. * Check if the display average bandwidth fits in the display
  1037. * dram bandwidth (CIK).
  1038. * Used for display watermark bandwidth calculations
  1039. * Returns true if the display fits, false if not.
  1040. */
  1041. static bool dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params *wm)
  1042. {
  1043. if (dce_v10_0_average_bandwidth(wm) <=
  1044. (dce_v10_0_dram_bandwidth_for_display(wm) / wm->num_heads))
  1045. return true;
  1046. else
  1047. return false;
  1048. }
  1049. /**
  1050. * dce_v10_0_average_bandwidth_vs_available_bandwidth - check
  1051. * average and available bandwidth
  1052. *
  1053. * @wm: watermark calculation data
  1054. *
  1055. * Check if the display average bandwidth fits in the display
  1056. * available bandwidth (CIK).
  1057. * Used for display watermark bandwidth calculations
  1058. * Returns true if the display fits, false if not.
  1059. */
  1060. static bool dce_v10_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params *wm)
  1061. {
  1062. if (dce_v10_0_average_bandwidth(wm) <=
  1063. (dce_v10_0_available_bandwidth(wm) / wm->num_heads))
  1064. return true;
  1065. else
  1066. return false;
  1067. }
  1068. /**
  1069. * dce_v10_0_check_latency_hiding - check latency hiding
  1070. *
  1071. * @wm: watermark calculation data
  1072. *
  1073. * Check latency hiding (CIK).
  1074. * Used for display watermark bandwidth calculations
  1075. * Returns true if the display fits, false if not.
  1076. */
  1077. static bool dce_v10_0_check_latency_hiding(struct dce10_wm_params *wm)
  1078. {
  1079. u32 lb_partitions = wm->lb_size / wm->src_width;
  1080. u32 line_time = wm->active_time + wm->blank_time;
  1081. u32 latency_tolerant_lines;
  1082. u32 latency_hiding;
  1083. fixed20_12 a;
  1084. a.full = dfixed_const(1);
  1085. if (wm->vsc.full > a.full)
  1086. latency_tolerant_lines = 1;
  1087. else {
  1088. if (lb_partitions <= (wm->vtaps + 1))
  1089. latency_tolerant_lines = 1;
  1090. else
  1091. latency_tolerant_lines = 2;
  1092. }
  1093. latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
  1094. if (dce_v10_0_latency_watermark(wm) <= latency_hiding)
  1095. return true;
  1096. else
  1097. return false;
  1098. }
  1099. /**
  1100. * dce_v10_0_program_watermarks - program display watermarks
  1101. *
  1102. * @adev: amdgpu_device pointer
  1103. * @amdgpu_crtc: the selected display controller
  1104. * @lb_size: line buffer size
  1105. * @num_heads: number of display controllers in use
  1106. *
  1107. * Calculate and program the display watermarks for the
  1108. * selected display controller (CIK).
  1109. */
  1110. static void dce_v10_0_program_watermarks(struct amdgpu_device *adev,
  1111. struct amdgpu_crtc *amdgpu_crtc,
  1112. u32 lb_size, u32 num_heads)
  1113. {
  1114. struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
  1115. struct dce10_wm_params wm_low, wm_high;
  1116. u32 active_time;
  1117. u32 line_time = 0;
  1118. u32 latency_watermark_a = 0, latency_watermark_b = 0;
  1119. u32 tmp, wm_mask, lb_vblank_lead_lines = 0;
  1120. if (amdgpu_crtc->base.enabled && num_heads && mode) {
  1121. active_time = 1000000UL * (u32)mode->crtc_hdisplay / (u32)mode->clock;
  1122. line_time = min((u32) (1000000UL * (u32)mode->crtc_htotal / (u32)mode->clock), (u32)65535);
  1123. /* watermark for high clocks */
  1124. if (adev->pm.dpm_enabled) {
  1125. wm_high.yclk =
  1126. amdgpu_dpm_get_mclk(adev, false) * 10;
  1127. wm_high.sclk =
  1128. amdgpu_dpm_get_sclk(adev, false) * 10;
  1129. } else {
  1130. wm_high.yclk = adev->pm.current_mclk * 10;
  1131. wm_high.sclk = adev->pm.current_sclk * 10;
  1132. }
  1133. wm_high.disp_clk = mode->clock;
  1134. wm_high.src_width = mode->crtc_hdisplay;
  1135. wm_high.active_time = active_time;
  1136. wm_high.blank_time = line_time - wm_high.active_time;
  1137. wm_high.interlaced = false;
  1138. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1139. wm_high.interlaced = true;
  1140. wm_high.vsc = amdgpu_crtc->vsc;
  1141. wm_high.vtaps = 1;
  1142. if (amdgpu_crtc->rmx_type != RMX_OFF)
  1143. wm_high.vtaps = 2;
  1144. wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
  1145. wm_high.lb_size = lb_size;
  1146. wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
  1147. wm_high.num_heads = num_heads;
  1148. /* set for high clocks */
  1149. latency_watermark_a = min(dce_v10_0_latency_watermark(&wm_high), (u32)65535);
  1150. /* possibly force display priority to high */
  1151. /* should really do this at mode validation time... */
  1152. if (!dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
  1153. !dce_v10_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
  1154. !dce_v10_0_check_latency_hiding(&wm_high) ||
  1155. (adev->mode_info.disp_priority == 2)) {
  1156. DRM_DEBUG_KMS("force priority to high\n");
  1157. }
  1158. /* watermark for low clocks */
  1159. if (adev->pm.dpm_enabled) {
  1160. wm_low.yclk =
  1161. amdgpu_dpm_get_mclk(adev, true) * 10;
  1162. wm_low.sclk =
  1163. amdgpu_dpm_get_sclk(adev, true) * 10;
  1164. } else {
  1165. wm_low.yclk = adev->pm.current_mclk * 10;
  1166. wm_low.sclk = adev->pm.current_sclk * 10;
  1167. }
  1168. wm_low.disp_clk = mode->clock;
  1169. wm_low.src_width = mode->crtc_hdisplay;
  1170. wm_low.active_time = active_time;
  1171. wm_low.blank_time = line_time - wm_low.active_time;
  1172. wm_low.interlaced = false;
  1173. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1174. wm_low.interlaced = true;
  1175. wm_low.vsc = amdgpu_crtc->vsc;
  1176. wm_low.vtaps = 1;
  1177. if (amdgpu_crtc->rmx_type != RMX_OFF)
  1178. wm_low.vtaps = 2;
  1179. wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
  1180. wm_low.lb_size = lb_size;
  1181. wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
  1182. wm_low.num_heads = num_heads;
  1183. /* set for low clocks */
  1184. latency_watermark_b = min(dce_v10_0_latency_watermark(&wm_low), (u32)65535);
  1185. /* possibly force display priority to high */
  1186. /* should really do this at mode validation time... */
  1187. if (!dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
  1188. !dce_v10_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
  1189. !dce_v10_0_check_latency_hiding(&wm_low) ||
  1190. (adev->mode_info.disp_priority == 2)) {
  1191. DRM_DEBUG_KMS("force priority to high\n");
  1192. }
  1193. lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
  1194. }
  1195. /* select wm A */
  1196. wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
  1197. tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 1);
  1198. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1199. tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
  1200. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_a);
  1201. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
  1202. WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1203. /* select wm B */
  1204. tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 2);
  1205. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1206. tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
  1207. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_b);
  1208. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
  1209. WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1210. /* restore original selection */
  1211. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
  1212. /* save values for DPM */
  1213. amdgpu_crtc->line_time = line_time;
  1214. amdgpu_crtc->wm_high = latency_watermark_a;
  1215. amdgpu_crtc->wm_low = latency_watermark_b;
  1216. /* Save number of lines the linebuffer leads before the scanout */
  1217. amdgpu_crtc->lb_vblank_lead_lines = lb_vblank_lead_lines;
  1218. }
  1219. /**
  1220. * dce_v10_0_bandwidth_update - program display watermarks
  1221. *
  1222. * @adev: amdgpu_device pointer
  1223. *
  1224. * Calculate and program the display watermarks and line
  1225. * buffer allocation (CIK).
  1226. */
  1227. static void dce_v10_0_bandwidth_update(struct amdgpu_device *adev)
  1228. {
  1229. struct drm_display_mode *mode = NULL;
  1230. u32 num_heads = 0, lb_size;
  1231. int i;
  1232. amdgpu_update_display_priority(adev);
  1233. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1234. if (adev->mode_info.crtcs[i]->base.enabled)
  1235. num_heads++;
  1236. }
  1237. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1238. mode = &adev->mode_info.crtcs[i]->base.mode;
  1239. lb_size = dce_v10_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
  1240. dce_v10_0_program_watermarks(adev, adev->mode_info.crtcs[i],
  1241. lb_size, num_heads);
  1242. }
  1243. }
  1244. static void dce_v10_0_audio_get_connected_pins(struct amdgpu_device *adev)
  1245. {
  1246. int i;
  1247. u32 offset, tmp;
  1248. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1249. offset = adev->mode_info.audio.pin[i].offset;
  1250. tmp = RREG32_AUDIO_ENDPT(offset,
  1251. ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
  1252. if (((tmp &
  1253. AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
  1254. AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
  1255. adev->mode_info.audio.pin[i].connected = false;
  1256. else
  1257. adev->mode_info.audio.pin[i].connected = true;
  1258. }
  1259. }
  1260. static struct amdgpu_audio_pin *dce_v10_0_audio_get_pin(struct amdgpu_device *adev)
  1261. {
  1262. int i;
  1263. dce_v10_0_audio_get_connected_pins(adev);
  1264. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1265. if (adev->mode_info.audio.pin[i].connected)
  1266. return &adev->mode_info.audio.pin[i];
  1267. }
  1268. DRM_ERROR("No connected audio pins found!\n");
  1269. return NULL;
  1270. }
  1271. static void dce_v10_0_afmt_audio_select_pin(struct drm_encoder *encoder)
  1272. {
  1273. struct amdgpu_device *adev = encoder->dev->dev_private;
  1274. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1275. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1276. u32 tmp;
  1277. if (!dig || !dig->afmt || !dig->afmt->pin)
  1278. return;
  1279. tmp = RREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset);
  1280. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_SRC_CONTROL, AFMT_AUDIO_SRC_SELECT, dig->afmt->pin->id);
  1281. WREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, tmp);
  1282. }
  1283. static void dce_v10_0_audio_write_latency_fields(struct drm_encoder *encoder,
  1284. struct drm_display_mode *mode)
  1285. {
  1286. struct amdgpu_device *adev = encoder->dev->dev_private;
  1287. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1288. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1289. struct drm_connector *connector;
  1290. struct amdgpu_connector *amdgpu_connector = NULL;
  1291. u32 tmp;
  1292. int interlace = 0;
  1293. if (!dig || !dig->afmt || !dig->afmt->pin)
  1294. return;
  1295. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1296. if (connector->encoder == encoder) {
  1297. amdgpu_connector = to_amdgpu_connector(connector);
  1298. break;
  1299. }
  1300. }
  1301. if (!amdgpu_connector) {
  1302. DRM_ERROR("Couldn't find encoder's connector\n");
  1303. return;
  1304. }
  1305. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1306. interlace = 1;
  1307. if (connector->latency_present[interlace]) {
  1308. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1309. VIDEO_LIPSYNC, connector->video_latency[interlace]);
  1310. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1311. AUDIO_LIPSYNC, connector->audio_latency[interlace]);
  1312. } else {
  1313. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1314. VIDEO_LIPSYNC, 0);
  1315. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1316. AUDIO_LIPSYNC, 0);
  1317. }
  1318. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1319. ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
  1320. }
  1321. static void dce_v10_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
  1322. {
  1323. struct amdgpu_device *adev = encoder->dev->dev_private;
  1324. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1325. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1326. struct drm_connector *connector;
  1327. struct amdgpu_connector *amdgpu_connector = NULL;
  1328. u32 tmp;
  1329. u8 *sadb = NULL;
  1330. int sad_count;
  1331. if (!dig || !dig->afmt || !dig->afmt->pin)
  1332. return;
  1333. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1334. if (connector->encoder == encoder) {
  1335. amdgpu_connector = to_amdgpu_connector(connector);
  1336. break;
  1337. }
  1338. }
  1339. if (!amdgpu_connector) {
  1340. DRM_ERROR("Couldn't find encoder's connector\n");
  1341. return;
  1342. }
  1343. sad_count = drm_edid_to_speaker_allocation(amdgpu_connector_edid(connector), &sadb);
  1344. if (sad_count < 0) {
  1345. DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
  1346. sad_count = 0;
  1347. }
  1348. /* program the speaker allocation */
  1349. tmp = RREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1350. ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
  1351. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1352. DP_CONNECTION, 0);
  1353. /* set HDMI mode */
  1354. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1355. HDMI_CONNECTION, 1);
  1356. if (sad_count)
  1357. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1358. SPEAKER_ALLOCATION, sadb[0]);
  1359. else
  1360. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1361. SPEAKER_ALLOCATION, 5); /* stereo */
  1362. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1363. ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
  1364. kfree(sadb);
  1365. }
  1366. static void dce_v10_0_audio_write_sad_regs(struct drm_encoder *encoder)
  1367. {
  1368. struct amdgpu_device *adev = encoder->dev->dev_private;
  1369. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1370. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1371. struct drm_connector *connector;
  1372. struct amdgpu_connector *amdgpu_connector = NULL;
  1373. struct cea_sad *sads;
  1374. int i, sad_count;
  1375. static const u16 eld_reg_to_type[][2] = {
  1376. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
  1377. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
  1378. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
  1379. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
  1380. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
  1381. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
  1382. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
  1383. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
  1384. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
  1385. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
  1386. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
  1387. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
  1388. };
  1389. if (!dig || !dig->afmt || !dig->afmt->pin)
  1390. return;
  1391. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1392. if (connector->encoder == encoder) {
  1393. amdgpu_connector = to_amdgpu_connector(connector);
  1394. break;
  1395. }
  1396. }
  1397. if (!amdgpu_connector) {
  1398. DRM_ERROR("Couldn't find encoder's connector\n");
  1399. return;
  1400. }
  1401. sad_count = drm_edid_to_sad(amdgpu_connector_edid(connector), &sads);
  1402. if (sad_count <= 0) {
  1403. DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
  1404. return;
  1405. }
  1406. BUG_ON(!sads);
  1407. for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
  1408. u32 tmp = 0;
  1409. u8 stereo_freqs = 0;
  1410. int max_channels = -1;
  1411. int j;
  1412. for (j = 0; j < sad_count; j++) {
  1413. struct cea_sad *sad = &sads[j];
  1414. if (sad->format == eld_reg_to_type[i][1]) {
  1415. if (sad->channels > max_channels) {
  1416. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1417. MAX_CHANNELS, sad->channels);
  1418. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1419. DESCRIPTOR_BYTE_2, sad->byte2);
  1420. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1421. SUPPORTED_FREQUENCIES, sad->freq);
  1422. max_channels = sad->channels;
  1423. }
  1424. if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
  1425. stereo_freqs |= sad->freq;
  1426. else
  1427. break;
  1428. }
  1429. }
  1430. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1431. SUPPORTED_FREQUENCIES_STEREO, stereo_freqs);
  1432. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset, eld_reg_to_type[i][0], tmp);
  1433. }
  1434. kfree(sads);
  1435. }
  1436. static void dce_v10_0_audio_enable(struct amdgpu_device *adev,
  1437. struct amdgpu_audio_pin *pin,
  1438. bool enable)
  1439. {
  1440. if (!pin)
  1441. return;
  1442. WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
  1443. enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
  1444. }
  1445. static const u32 pin_offsets[] =
  1446. {
  1447. AUD0_REGISTER_OFFSET,
  1448. AUD1_REGISTER_OFFSET,
  1449. AUD2_REGISTER_OFFSET,
  1450. AUD3_REGISTER_OFFSET,
  1451. AUD4_REGISTER_OFFSET,
  1452. AUD5_REGISTER_OFFSET,
  1453. AUD6_REGISTER_OFFSET,
  1454. };
  1455. static int dce_v10_0_audio_init(struct amdgpu_device *adev)
  1456. {
  1457. int i;
  1458. if (!amdgpu_audio)
  1459. return 0;
  1460. adev->mode_info.audio.enabled = true;
  1461. adev->mode_info.audio.num_pins = 7;
  1462. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1463. adev->mode_info.audio.pin[i].channels = -1;
  1464. adev->mode_info.audio.pin[i].rate = -1;
  1465. adev->mode_info.audio.pin[i].bits_per_sample = -1;
  1466. adev->mode_info.audio.pin[i].status_bits = 0;
  1467. adev->mode_info.audio.pin[i].category_code = 0;
  1468. adev->mode_info.audio.pin[i].connected = false;
  1469. adev->mode_info.audio.pin[i].offset = pin_offsets[i];
  1470. adev->mode_info.audio.pin[i].id = i;
  1471. /* disable audio. it will be set up later */
  1472. /* XXX remove once we switch to ip funcs */
  1473. dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  1474. }
  1475. return 0;
  1476. }
  1477. static void dce_v10_0_audio_fini(struct amdgpu_device *adev)
  1478. {
  1479. int i;
  1480. if (!adev->mode_info.audio.enabled)
  1481. return;
  1482. for (i = 0; i < adev->mode_info.audio.num_pins; i++)
  1483. dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  1484. adev->mode_info.audio.enabled = false;
  1485. }
  1486. /*
  1487. * update the N and CTS parameters for a given pixel clock rate
  1488. */
  1489. static void dce_v10_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
  1490. {
  1491. struct drm_device *dev = encoder->dev;
  1492. struct amdgpu_device *adev = dev->dev_private;
  1493. struct amdgpu_afmt_acr acr = amdgpu_afmt_acr(clock);
  1494. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1495. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1496. u32 tmp;
  1497. tmp = RREG32(mmHDMI_ACR_32_0 + dig->afmt->offset);
  1498. tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_0, HDMI_ACR_CTS_32, acr.cts_32khz);
  1499. WREG32(mmHDMI_ACR_32_0 + dig->afmt->offset, tmp);
  1500. tmp = RREG32(mmHDMI_ACR_32_1 + dig->afmt->offset);
  1501. tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_1, HDMI_ACR_N_32, acr.n_32khz);
  1502. WREG32(mmHDMI_ACR_32_1 + dig->afmt->offset, tmp);
  1503. tmp = RREG32(mmHDMI_ACR_44_0 + dig->afmt->offset);
  1504. tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_0, HDMI_ACR_CTS_44, acr.cts_44_1khz);
  1505. WREG32(mmHDMI_ACR_44_0 + dig->afmt->offset, tmp);
  1506. tmp = RREG32(mmHDMI_ACR_44_1 + dig->afmt->offset);
  1507. tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_1, HDMI_ACR_N_44, acr.n_44_1khz);
  1508. WREG32(mmHDMI_ACR_44_1 + dig->afmt->offset, tmp);
  1509. tmp = RREG32(mmHDMI_ACR_48_0 + dig->afmt->offset);
  1510. tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_0, HDMI_ACR_CTS_48, acr.cts_48khz);
  1511. WREG32(mmHDMI_ACR_48_0 + dig->afmt->offset, tmp);
  1512. tmp = RREG32(mmHDMI_ACR_48_1 + dig->afmt->offset);
  1513. tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_1, HDMI_ACR_N_48, acr.n_48khz);
  1514. WREG32(mmHDMI_ACR_48_1 + dig->afmt->offset, tmp);
  1515. }
  1516. /*
  1517. * build a HDMI Video Info Frame
  1518. */
  1519. static void dce_v10_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
  1520. void *buffer, size_t size)
  1521. {
  1522. struct drm_device *dev = encoder->dev;
  1523. struct amdgpu_device *adev = dev->dev_private;
  1524. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1525. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1526. uint8_t *frame = buffer + 3;
  1527. uint8_t *header = buffer;
  1528. WREG32(mmAFMT_AVI_INFO0 + dig->afmt->offset,
  1529. frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
  1530. WREG32(mmAFMT_AVI_INFO1 + dig->afmt->offset,
  1531. frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
  1532. WREG32(mmAFMT_AVI_INFO2 + dig->afmt->offset,
  1533. frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
  1534. WREG32(mmAFMT_AVI_INFO3 + dig->afmt->offset,
  1535. frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
  1536. }
  1537. static void dce_v10_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
  1538. {
  1539. struct drm_device *dev = encoder->dev;
  1540. struct amdgpu_device *adev = dev->dev_private;
  1541. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1542. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1543. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  1544. u32 dto_phase = 24 * 1000;
  1545. u32 dto_modulo = clock;
  1546. u32 tmp;
  1547. if (!dig || !dig->afmt)
  1548. return;
  1549. /* XXX two dtos; generally use dto0 for hdmi */
  1550. /* Express [24MHz / target pixel clock] as an exact rational
  1551. * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
  1552. * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
  1553. */
  1554. tmp = RREG32(mmDCCG_AUDIO_DTO_SOURCE);
  1555. tmp = REG_SET_FIELD(tmp, DCCG_AUDIO_DTO_SOURCE, DCCG_AUDIO_DTO0_SOURCE_SEL,
  1556. amdgpu_crtc->crtc_id);
  1557. WREG32(mmDCCG_AUDIO_DTO_SOURCE, tmp);
  1558. WREG32(mmDCCG_AUDIO_DTO0_PHASE, dto_phase);
  1559. WREG32(mmDCCG_AUDIO_DTO0_MODULE, dto_modulo);
  1560. }
  1561. /*
  1562. * update the info frames with the data from the current display mode
  1563. */
  1564. static void dce_v10_0_afmt_setmode(struct drm_encoder *encoder,
  1565. struct drm_display_mode *mode)
  1566. {
  1567. struct drm_device *dev = encoder->dev;
  1568. struct amdgpu_device *adev = dev->dev_private;
  1569. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1570. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1571. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  1572. u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
  1573. struct hdmi_avi_infoframe frame;
  1574. ssize_t err;
  1575. u32 tmp;
  1576. int bpc = 8;
  1577. if (!dig || !dig->afmt)
  1578. return;
  1579. /* Silent, r600_hdmi_enable will raise WARN for us */
  1580. if (!dig->afmt->enabled)
  1581. return;
  1582. /* hdmi deep color mode general control packets setup, if bpc > 8 */
  1583. if (encoder->crtc) {
  1584. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  1585. bpc = amdgpu_crtc->bpc;
  1586. }
  1587. /* disable audio prior to setting up hw */
  1588. dig->afmt->pin = dce_v10_0_audio_get_pin(adev);
  1589. dce_v10_0_audio_enable(adev, dig->afmt->pin, false);
  1590. dce_v10_0_audio_set_dto(encoder, mode->clock);
  1591. tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
  1592. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1);
  1593. WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp); /* send null packets when required */
  1594. WREG32(mmAFMT_AUDIO_CRC_CONTROL + dig->afmt->offset, 0x1000);
  1595. tmp = RREG32(mmHDMI_CONTROL + dig->afmt->offset);
  1596. switch (bpc) {
  1597. case 0:
  1598. case 6:
  1599. case 8:
  1600. case 16:
  1601. default:
  1602. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 0);
  1603. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 0);
  1604. DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
  1605. connector->name, bpc);
  1606. break;
  1607. case 10:
  1608. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
  1609. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 1);
  1610. DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
  1611. connector->name);
  1612. break;
  1613. case 12:
  1614. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
  1615. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 2);
  1616. DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
  1617. connector->name);
  1618. break;
  1619. }
  1620. WREG32(mmHDMI_CONTROL + dig->afmt->offset, tmp);
  1621. tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
  1622. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1); /* send null packets when required */
  1623. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_SEND, 1); /* send general control packets */
  1624. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_CONT, 1); /* send general control packets every frame */
  1625. WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp);
  1626. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1627. /* enable audio info frames (frames won't be set until audio is enabled) */
  1628. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_SEND, 1);
  1629. /* required for audio info values to be updated */
  1630. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_CONT, 1);
  1631. WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1632. tmp = RREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1633. /* required for audio info values to be updated */
  1634. tmp = REG_SET_FIELD(tmp, AFMT_INFOFRAME_CONTROL0, AFMT_AUDIO_INFO_UPDATE, 1);
  1635. WREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1636. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
  1637. /* anything other than 0 */
  1638. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AUDIO_INFO_LINE, 2);
  1639. WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
  1640. WREG32(mmHDMI_GC + dig->afmt->offset, 0); /* unset HDMI_GC_AVMUTE */
  1641. tmp = RREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1642. /* set the default audio delay */
  1643. tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_DELAY_EN, 1);
  1644. /* should be suffient for all audio modes and small enough for all hblanks */
  1645. tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_PACKETS_PER_LINE, 3);
  1646. WREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1647. tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1648. /* allow 60958 channel status fields to be updated */
  1649. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_60958_CS_UPDATE, 1);
  1650. WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1651. tmp = RREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset);
  1652. if (bpc > 8)
  1653. /* clear SW CTS value */
  1654. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 0);
  1655. else
  1656. /* select SW CTS value */
  1657. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 1);
  1658. /* allow hw to sent ACR packets when required */
  1659. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_AUTO_SEND, 1);
  1660. WREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset, tmp);
  1661. dce_v10_0_afmt_update_ACR(encoder, mode->clock);
  1662. tmp = RREG32(mmAFMT_60958_0 + dig->afmt->offset);
  1663. tmp = REG_SET_FIELD(tmp, AFMT_60958_0, AFMT_60958_CS_CHANNEL_NUMBER_L, 1);
  1664. WREG32(mmAFMT_60958_0 + dig->afmt->offset, tmp);
  1665. tmp = RREG32(mmAFMT_60958_1 + dig->afmt->offset);
  1666. tmp = REG_SET_FIELD(tmp, AFMT_60958_1, AFMT_60958_CS_CHANNEL_NUMBER_R, 2);
  1667. WREG32(mmAFMT_60958_1 + dig->afmt->offset, tmp);
  1668. tmp = RREG32(mmAFMT_60958_2 + dig->afmt->offset);
  1669. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_2, 3);
  1670. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_3, 4);
  1671. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_4, 5);
  1672. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_5, 6);
  1673. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_6, 7);
  1674. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_7, 8);
  1675. WREG32(mmAFMT_60958_2 + dig->afmt->offset, tmp);
  1676. dce_v10_0_audio_write_speaker_allocation(encoder);
  1677. WREG32(mmAFMT_AUDIO_PACKET_CONTROL2 + dig->afmt->offset,
  1678. (0xff << AFMT_AUDIO_PACKET_CONTROL2__AFMT_AUDIO_CHANNEL_ENABLE__SHIFT));
  1679. dce_v10_0_afmt_audio_select_pin(encoder);
  1680. dce_v10_0_audio_write_sad_regs(encoder);
  1681. dce_v10_0_audio_write_latency_fields(encoder, mode);
  1682. err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
  1683. if (err < 0) {
  1684. DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
  1685. return;
  1686. }
  1687. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  1688. if (err < 0) {
  1689. DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
  1690. return;
  1691. }
  1692. dce_v10_0_afmt_update_avi_infoframe(encoder, buffer, sizeof(buffer));
  1693. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1694. /* enable AVI info frames */
  1695. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_SEND, 1);
  1696. /* required for audio info values to be updated */
  1697. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_CONT, 1);
  1698. WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1699. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
  1700. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AVI_INFO_LINE, 2);
  1701. WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
  1702. tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1703. /* send audio packets */
  1704. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_AUDIO_SAMPLE_SEND, 1);
  1705. WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1706. WREG32(mmAFMT_RAMP_CONTROL0 + dig->afmt->offset, 0x00FFFFFF);
  1707. WREG32(mmAFMT_RAMP_CONTROL1 + dig->afmt->offset, 0x007FFFFF);
  1708. WREG32(mmAFMT_RAMP_CONTROL2 + dig->afmt->offset, 0x00000001);
  1709. WREG32(mmAFMT_RAMP_CONTROL3 + dig->afmt->offset, 0x00000001);
  1710. /* enable audio after to setting up hw */
  1711. dce_v10_0_audio_enable(adev, dig->afmt->pin, true);
  1712. }
  1713. static void dce_v10_0_afmt_enable(struct drm_encoder *encoder, bool enable)
  1714. {
  1715. struct drm_device *dev = encoder->dev;
  1716. struct amdgpu_device *adev = dev->dev_private;
  1717. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1718. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1719. if (!dig || !dig->afmt)
  1720. return;
  1721. /* Silent, r600_hdmi_enable will raise WARN for us */
  1722. if (enable && dig->afmt->enabled)
  1723. return;
  1724. if (!enable && !dig->afmt->enabled)
  1725. return;
  1726. if (!enable && dig->afmt->pin) {
  1727. dce_v10_0_audio_enable(adev, dig->afmt->pin, false);
  1728. dig->afmt->pin = NULL;
  1729. }
  1730. dig->afmt->enabled = enable;
  1731. DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
  1732. enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
  1733. }
  1734. static void dce_v10_0_afmt_init(struct amdgpu_device *adev)
  1735. {
  1736. int i;
  1737. for (i = 0; i < adev->mode_info.num_dig; i++)
  1738. adev->mode_info.afmt[i] = NULL;
  1739. /* DCE10 has audio blocks tied to DIG encoders */
  1740. for (i = 0; i < adev->mode_info.num_dig; i++) {
  1741. adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
  1742. if (adev->mode_info.afmt[i]) {
  1743. adev->mode_info.afmt[i]->offset = dig_offsets[i];
  1744. adev->mode_info.afmt[i]->id = i;
  1745. }
  1746. }
  1747. }
  1748. static void dce_v10_0_afmt_fini(struct amdgpu_device *adev)
  1749. {
  1750. int i;
  1751. for (i = 0; i < adev->mode_info.num_dig; i++) {
  1752. kfree(adev->mode_info.afmt[i]);
  1753. adev->mode_info.afmt[i] = NULL;
  1754. }
  1755. }
  1756. static const u32 vga_control_regs[6] =
  1757. {
  1758. mmD1VGA_CONTROL,
  1759. mmD2VGA_CONTROL,
  1760. mmD3VGA_CONTROL,
  1761. mmD4VGA_CONTROL,
  1762. mmD5VGA_CONTROL,
  1763. mmD6VGA_CONTROL,
  1764. };
  1765. static void dce_v10_0_vga_enable(struct drm_crtc *crtc, bool enable)
  1766. {
  1767. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1768. struct drm_device *dev = crtc->dev;
  1769. struct amdgpu_device *adev = dev->dev_private;
  1770. u32 vga_control;
  1771. vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
  1772. if (enable)
  1773. WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
  1774. else
  1775. WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
  1776. }
  1777. static void dce_v10_0_grph_enable(struct drm_crtc *crtc, bool enable)
  1778. {
  1779. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1780. struct drm_device *dev = crtc->dev;
  1781. struct amdgpu_device *adev = dev->dev_private;
  1782. if (enable)
  1783. WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
  1784. else
  1785. WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
  1786. }
  1787. static int dce_v10_0_crtc_do_set_base(struct drm_crtc *crtc,
  1788. struct drm_framebuffer *fb,
  1789. int x, int y, int atomic)
  1790. {
  1791. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1792. struct drm_device *dev = crtc->dev;
  1793. struct amdgpu_device *adev = dev->dev_private;
  1794. struct amdgpu_framebuffer *amdgpu_fb;
  1795. struct drm_framebuffer *target_fb;
  1796. struct drm_gem_object *obj;
  1797. struct amdgpu_bo *rbo;
  1798. uint64_t fb_location, tiling_flags;
  1799. uint32_t fb_format, fb_pitch_pixels;
  1800. u32 fb_swap = REG_SET_FIELD(0, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP, ENDIAN_NONE);
  1801. u32 pipe_config;
  1802. u32 tmp, viewport_w, viewport_h;
  1803. int r;
  1804. bool bypass_lut = false;
  1805. /* no fb bound */
  1806. if (!atomic && !crtc->primary->fb) {
  1807. DRM_DEBUG_KMS("No FB bound\n");
  1808. return 0;
  1809. }
  1810. if (atomic) {
  1811. amdgpu_fb = to_amdgpu_framebuffer(fb);
  1812. target_fb = fb;
  1813. }
  1814. else {
  1815. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  1816. target_fb = crtc->primary->fb;
  1817. }
  1818. /* If atomic, assume fb object is pinned & idle & fenced and
  1819. * just update base pointers
  1820. */
  1821. obj = amdgpu_fb->obj;
  1822. rbo = gem_to_amdgpu_bo(obj);
  1823. r = amdgpu_bo_reserve(rbo, false);
  1824. if (unlikely(r != 0))
  1825. return r;
  1826. if (atomic)
  1827. fb_location = amdgpu_bo_gpu_offset(rbo);
  1828. else {
  1829. r = amdgpu_bo_pin(rbo, AMDGPU_GEM_DOMAIN_VRAM, &fb_location);
  1830. if (unlikely(r != 0)) {
  1831. amdgpu_bo_unreserve(rbo);
  1832. return -EINVAL;
  1833. }
  1834. }
  1835. amdgpu_bo_get_tiling_flags(rbo, &tiling_flags);
  1836. amdgpu_bo_unreserve(rbo);
  1837. pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
  1838. switch (target_fb->pixel_format) {
  1839. case DRM_FORMAT_C8:
  1840. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 0);
  1841. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1842. break;
  1843. case DRM_FORMAT_XRGB4444:
  1844. case DRM_FORMAT_ARGB4444:
  1845. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1846. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 2);
  1847. #ifdef __BIG_ENDIAN
  1848. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1849. ENDIAN_8IN16);
  1850. #endif
  1851. break;
  1852. case DRM_FORMAT_XRGB1555:
  1853. case DRM_FORMAT_ARGB1555:
  1854. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1855. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1856. #ifdef __BIG_ENDIAN
  1857. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1858. ENDIAN_8IN16);
  1859. #endif
  1860. break;
  1861. case DRM_FORMAT_BGRX5551:
  1862. case DRM_FORMAT_BGRA5551:
  1863. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1864. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 5);
  1865. #ifdef __BIG_ENDIAN
  1866. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1867. ENDIAN_8IN16);
  1868. #endif
  1869. break;
  1870. case DRM_FORMAT_RGB565:
  1871. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1872. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
  1873. #ifdef __BIG_ENDIAN
  1874. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1875. ENDIAN_8IN16);
  1876. #endif
  1877. break;
  1878. case DRM_FORMAT_XRGB8888:
  1879. case DRM_FORMAT_ARGB8888:
  1880. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1881. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1882. #ifdef __BIG_ENDIAN
  1883. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1884. ENDIAN_8IN32);
  1885. #endif
  1886. break;
  1887. case DRM_FORMAT_XRGB2101010:
  1888. case DRM_FORMAT_ARGB2101010:
  1889. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1890. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
  1891. #ifdef __BIG_ENDIAN
  1892. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1893. ENDIAN_8IN32);
  1894. #endif
  1895. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1896. bypass_lut = true;
  1897. break;
  1898. case DRM_FORMAT_BGRX1010102:
  1899. case DRM_FORMAT_BGRA1010102:
  1900. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1901. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 4);
  1902. #ifdef __BIG_ENDIAN
  1903. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1904. ENDIAN_8IN32);
  1905. #endif
  1906. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1907. bypass_lut = true;
  1908. break;
  1909. default:
  1910. DRM_ERROR("Unsupported screen format %s\n",
  1911. drm_get_format_name(target_fb->pixel_format));
  1912. return -EINVAL;
  1913. }
  1914. if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
  1915. unsigned bankw, bankh, mtaspect, tile_split, num_banks;
  1916. bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
  1917. bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
  1918. mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
  1919. tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
  1920. num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
  1921. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_NUM_BANKS, num_banks);
  1922. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
  1923. ARRAY_2D_TILED_THIN1);
  1924. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_TILE_SPLIT,
  1925. tile_split);
  1926. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_WIDTH, bankw);
  1927. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_HEIGHT, bankh);
  1928. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MACRO_TILE_ASPECT,
  1929. mtaspect);
  1930. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MICRO_TILE_MODE,
  1931. ADDR_SURF_MICRO_TILING_DISPLAY);
  1932. } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1) {
  1933. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
  1934. ARRAY_1D_TILED_THIN1);
  1935. }
  1936. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_PIPE_CONFIG,
  1937. pipe_config);
  1938. dce_v10_0_vga_enable(crtc, false);
  1939. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  1940. upper_32_bits(fb_location));
  1941. WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  1942. upper_32_bits(fb_location));
  1943. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  1944. (u32)fb_location & GRPH_PRIMARY_SURFACE_ADDRESS__GRPH_PRIMARY_SURFACE_ADDRESS_MASK);
  1945. WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  1946. (u32) fb_location & GRPH_SECONDARY_SURFACE_ADDRESS__GRPH_SECONDARY_SURFACE_ADDRESS_MASK);
  1947. WREG32(mmGRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
  1948. WREG32(mmGRPH_SWAP_CNTL + amdgpu_crtc->crtc_offset, fb_swap);
  1949. /*
  1950. * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
  1951. * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
  1952. * retain the full precision throughout the pipeline.
  1953. */
  1954. tmp = RREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset);
  1955. if (bypass_lut)
  1956. tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 1);
  1957. else
  1958. tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 0);
  1959. WREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset, tmp);
  1960. if (bypass_lut)
  1961. DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
  1962. WREG32(mmGRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
  1963. WREG32(mmGRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
  1964. WREG32(mmGRPH_X_START + amdgpu_crtc->crtc_offset, 0);
  1965. WREG32(mmGRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
  1966. WREG32(mmGRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
  1967. WREG32(mmGRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
  1968. fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
  1969. WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
  1970. dce_v10_0_grph_enable(crtc, true);
  1971. WREG32(mmLB_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
  1972. target_fb->height);
  1973. x &= ~3;
  1974. y &= ~1;
  1975. WREG32(mmVIEWPORT_START + amdgpu_crtc->crtc_offset,
  1976. (x << 16) | y);
  1977. viewport_w = crtc->mode.hdisplay;
  1978. viewport_h = (crtc->mode.vdisplay + 1) & ~1;
  1979. WREG32(mmVIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
  1980. (viewport_w << 16) | viewport_h);
  1981. /* pageflip setup */
  1982. /* make sure flip is at vb rather than hb */
  1983. tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
  1984. tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
  1985. GRPH_SURFACE_UPDATE_H_RETRACE_EN, 0);
  1986. WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1987. /* set pageflip to happen only at start of vblank interval (front porch) */
  1988. WREG32(mmMASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 3);
  1989. if (!atomic && fb && fb != crtc->primary->fb) {
  1990. amdgpu_fb = to_amdgpu_framebuffer(fb);
  1991. rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  1992. r = amdgpu_bo_reserve(rbo, false);
  1993. if (unlikely(r != 0))
  1994. return r;
  1995. amdgpu_bo_unpin(rbo);
  1996. amdgpu_bo_unreserve(rbo);
  1997. }
  1998. /* Bytes per pixel may have changed */
  1999. dce_v10_0_bandwidth_update(adev);
  2000. return 0;
  2001. }
  2002. static void dce_v10_0_set_interleave(struct drm_crtc *crtc,
  2003. struct drm_display_mode *mode)
  2004. {
  2005. struct drm_device *dev = crtc->dev;
  2006. struct amdgpu_device *adev = dev->dev_private;
  2007. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2008. u32 tmp;
  2009. tmp = RREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset);
  2010. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  2011. tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 1);
  2012. else
  2013. tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 0);
  2014. WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset, tmp);
  2015. }
  2016. static void dce_v10_0_crtc_load_lut(struct drm_crtc *crtc)
  2017. {
  2018. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2019. struct drm_device *dev = crtc->dev;
  2020. struct amdgpu_device *adev = dev->dev_private;
  2021. int i;
  2022. u32 tmp;
  2023. DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
  2024. tmp = RREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
  2025. tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_GRPH_MODE, 0);
  2026. tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_OVL_MODE, 0);
  2027. WREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2028. tmp = RREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset);
  2029. tmp = REG_SET_FIELD(tmp, PRESCALE_GRPH_CONTROL, GRPH_PRESCALE_BYPASS, 1);
  2030. WREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2031. tmp = RREG32(mmPRESCALE_OVL_CONTROL + amdgpu_crtc->crtc_offset);
  2032. tmp = REG_SET_FIELD(tmp, PRESCALE_OVL_CONTROL, OVL_PRESCALE_BYPASS, 1);
  2033. WREG32(mmPRESCALE_OVL_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2034. tmp = RREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  2035. tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, GRPH_INPUT_GAMMA_MODE, 0);
  2036. tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, OVL_INPUT_GAMMA_MODE, 0);
  2037. WREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2038. WREG32(mmDC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
  2039. WREG32(mmDC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
  2040. WREG32(mmDC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
  2041. WREG32(mmDC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
  2042. WREG32(mmDC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
  2043. WREG32(mmDC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
  2044. WREG32(mmDC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
  2045. WREG32(mmDC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
  2046. WREG32(mmDC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
  2047. WREG32(mmDC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
  2048. for (i = 0; i < 256; i++) {
  2049. WREG32(mmDC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
  2050. (amdgpu_crtc->lut_r[i] << 20) |
  2051. (amdgpu_crtc->lut_g[i] << 10) |
  2052. (amdgpu_crtc->lut_b[i] << 0));
  2053. }
  2054. tmp = RREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  2055. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, GRPH_DEGAMMA_MODE, 0);
  2056. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, OVL_DEGAMMA_MODE, 0);
  2057. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR_DEGAMMA_MODE, 0);
  2058. WREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2059. tmp = RREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset);
  2060. tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, GRPH_GAMUT_REMAP_MODE, 0);
  2061. tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, OVL_GAMUT_REMAP_MODE, 0);
  2062. WREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2063. tmp = RREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  2064. tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, GRPH_REGAMMA_MODE, 0);
  2065. tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, OVL_REGAMMA_MODE, 0);
  2066. WREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2067. tmp = RREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
  2068. tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_GRPH_MODE, 0);
  2069. tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_OVL_MODE, 0);
  2070. WREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2071. /* XXX match this to the depth of the crtc fmt block, move to modeset? */
  2072. WREG32(mmDENORM_CONTROL + amdgpu_crtc->crtc_offset, 0);
  2073. /* XXX this only needs to be programmed once per crtc at startup,
  2074. * not sure where the best place for it is
  2075. */
  2076. tmp = RREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset);
  2077. tmp = REG_SET_FIELD(tmp, ALPHA_CONTROL, CURSOR_ALPHA_BLND_ENA, 1);
  2078. WREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2079. }
  2080. static int dce_v10_0_pick_dig_encoder(struct drm_encoder *encoder)
  2081. {
  2082. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2083. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  2084. switch (amdgpu_encoder->encoder_id) {
  2085. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  2086. if (dig->linkb)
  2087. return 1;
  2088. else
  2089. return 0;
  2090. break;
  2091. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  2092. if (dig->linkb)
  2093. return 3;
  2094. else
  2095. return 2;
  2096. break;
  2097. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  2098. if (dig->linkb)
  2099. return 5;
  2100. else
  2101. return 4;
  2102. break;
  2103. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
  2104. return 6;
  2105. break;
  2106. default:
  2107. DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
  2108. return 0;
  2109. }
  2110. }
  2111. /**
  2112. * dce_v10_0_pick_pll - Allocate a PPLL for use by the crtc.
  2113. *
  2114. * @crtc: drm crtc
  2115. *
  2116. * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
  2117. * a single PPLL can be used for all DP crtcs/encoders. For non-DP
  2118. * monitors a dedicated PPLL must be used. If a particular board has
  2119. * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
  2120. * as there is no need to program the PLL itself. If we are not able to
  2121. * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
  2122. * avoid messing up an existing monitor.
  2123. *
  2124. * Asic specific PLL information
  2125. *
  2126. * DCE 10.x
  2127. * Tonga
  2128. * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
  2129. * CI
  2130. * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
  2131. *
  2132. */
  2133. static u32 dce_v10_0_pick_pll(struct drm_crtc *crtc)
  2134. {
  2135. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2136. struct drm_device *dev = crtc->dev;
  2137. struct amdgpu_device *adev = dev->dev_private;
  2138. u32 pll_in_use;
  2139. int pll;
  2140. if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
  2141. if (adev->clock.dp_extclk)
  2142. /* skip PPLL programming if using ext clock */
  2143. return ATOM_PPLL_INVALID;
  2144. else {
  2145. /* use the same PPLL for all DP monitors */
  2146. pll = amdgpu_pll_get_shared_dp_ppll(crtc);
  2147. if (pll != ATOM_PPLL_INVALID)
  2148. return pll;
  2149. }
  2150. } else {
  2151. /* use the same PPLL for all monitors with the same clock */
  2152. pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
  2153. if (pll != ATOM_PPLL_INVALID)
  2154. return pll;
  2155. }
  2156. /* DCE10 has PPLL0, PPLL1, and PPLL2 */
  2157. pll_in_use = amdgpu_pll_get_use_mask(crtc);
  2158. if (!(pll_in_use & (1 << ATOM_PPLL2)))
  2159. return ATOM_PPLL2;
  2160. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  2161. return ATOM_PPLL1;
  2162. if (!(pll_in_use & (1 << ATOM_PPLL0)))
  2163. return ATOM_PPLL0;
  2164. DRM_ERROR("unable to allocate a PPLL\n");
  2165. return ATOM_PPLL_INVALID;
  2166. }
  2167. static void dce_v10_0_lock_cursor(struct drm_crtc *crtc, bool lock)
  2168. {
  2169. struct amdgpu_device *adev = crtc->dev->dev_private;
  2170. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2171. uint32_t cur_lock;
  2172. cur_lock = RREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset);
  2173. if (lock)
  2174. cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 1);
  2175. else
  2176. cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 0);
  2177. WREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
  2178. }
  2179. static void dce_v10_0_hide_cursor(struct drm_crtc *crtc)
  2180. {
  2181. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2182. struct amdgpu_device *adev = crtc->dev->dev_private;
  2183. u32 tmp;
  2184. tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
  2185. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 0);
  2186. WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2187. }
  2188. static void dce_v10_0_show_cursor(struct drm_crtc *crtc)
  2189. {
  2190. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2191. struct amdgpu_device *adev = crtc->dev->dev_private;
  2192. u32 tmp;
  2193. WREG32(mmCUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  2194. upper_32_bits(amdgpu_crtc->cursor_addr));
  2195. WREG32(mmCUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  2196. lower_32_bits(amdgpu_crtc->cursor_addr));
  2197. tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
  2198. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 1);
  2199. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_MODE, 2);
  2200. WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2201. }
  2202. static int dce_v10_0_cursor_move_locked(struct drm_crtc *crtc,
  2203. int x, int y)
  2204. {
  2205. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2206. struct amdgpu_device *adev = crtc->dev->dev_private;
  2207. int xorigin = 0, yorigin = 0;
  2208. /* avivo cursor are offset into the total surface */
  2209. x += crtc->x;
  2210. y += crtc->y;
  2211. DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
  2212. if (x < 0) {
  2213. xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
  2214. x = 0;
  2215. }
  2216. if (y < 0) {
  2217. yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
  2218. y = 0;
  2219. }
  2220. WREG32(mmCUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
  2221. WREG32(mmCUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
  2222. WREG32(mmCUR_SIZE + amdgpu_crtc->crtc_offset,
  2223. ((amdgpu_crtc->cursor_width - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
  2224. amdgpu_crtc->cursor_x = x;
  2225. amdgpu_crtc->cursor_y = y;
  2226. return 0;
  2227. }
  2228. static int dce_v10_0_crtc_cursor_move(struct drm_crtc *crtc,
  2229. int x, int y)
  2230. {
  2231. int ret;
  2232. dce_v10_0_lock_cursor(crtc, true);
  2233. ret = dce_v10_0_cursor_move_locked(crtc, x, y);
  2234. dce_v10_0_lock_cursor(crtc, false);
  2235. return ret;
  2236. }
  2237. static int dce_v10_0_crtc_cursor_set2(struct drm_crtc *crtc,
  2238. struct drm_file *file_priv,
  2239. uint32_t handle,
  2240. uint32_t width,
  2241. uint32_t height,
  2242. int32_t hot_x,
  2243. int32_t hot_y)
  2244. {
  2245. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2246. struct drm_gem_object *obj;
  2247. struct amdgpu_bo *aobj;
  2248. int ret;
  2249. if (!handle) {
  2250. /* turn off cursor */
  2251. dce_v10_0_hide_cursor(crtc);
  2252. obj = NULL;
  2253. goto unpin;
  2254. }
  2255. if ((width > amdgpu_crtc->max_cursor_width) ||
  2256. (height > amdgpu_crtc->max_cursor_height)) {
  2257. DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
  2258. return -EINVAL;
  2259. }
  2260. obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
  2261. if (!obj) {
  2262. DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
  2263. return -ENOENT;
  2264. }
  2265. aobj = gem_to_amdgpu_bo(obj);
  2266. ret = amdgpu_bo_reserve(aobj, false);
  2267. if (ret != 0) {
  2268. drm_gem_object_unreference_unlocked(obj);
  2269. return ret;
  2270. }
  2271. ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM, &amdgpu_crtc->cursor_addr);
  2272. amdgpu_bo_unreserve(aobj);
  2273. if (ret) {
  2274. DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
  2275. drm_gem_object_unreference_unlocked(obj);
  2276. return ret;
  2277. }
  2278. amdgpu_crtc->cursor_width = width;
  2279. amdgpu_crtc->cursor_height = height;
  2280. dce_v10_0_lock_cursor(crtc, true);
  2281. if (hot_x != amdgpu_crtc->cursor_hot_x ||
  2282. hot_y != amdgpu_crtc->cursor_hot_y) {
  2283. int x, y;
  2284. x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
  2285. y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
  2286. dce_v10_0_cursor_move_locked(crtc, x, y);
  2287. amdgpu_crtc->cursor_hot_x = hot_x;
  2288. amdgpu_crtc->cursor_hot_y = hot_y;
  2289. }
  2290. dce_v10_0_show_cursor(crtc);
  2291. dce_v10_0_lock_cursor(crtc, false);
  2292. unpin:
  2293. if (amdgpu_crtc->cursor_bo) {
  2294. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2295. ret = amdgpu_bo_reserve(aobj, false);
  2296. if (likely(ret == 0)) {
  2297. amdgpu_bo_unpin(aobj);
  2298. amdgpu_bo_unreserve(aobj);
  2299. }
  2300. drm_gem_object_unreference_unlocked(amdgpu_crtc->cursor_bo);
  2301. }
  2302. amdgpu_crtc->cursor_bo = obj;
  2303. return 0;
  2304. }
  2305. static void dce_v10_0_cursor_reset(struct drm_crtc *crtc)
  2306. {
  2307. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2308. if (amdgpu_crtc->cursor_bo) {
  2309. dce_v10_0_lock_cursor(crtc, true);
  2310. dce_v10_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
  2311. amdgpu_crtc->cursor_y);
  2312. dce_v10_0_show_cursor(crtc);
  2313. dce_v10_0_lock_cursor(crtc, false);
  2314. }
  2315. }
  2316. static void dce_v10_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  2317. u16 *blue, uint32_t start, uint32_t size)
  2318. {
  2319. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2320. int end = (start + size > 256) ? 256 : start + size, i;
  2321. /* userspace palettes are always correct as is */
  2322. for (i = start; i < end; i++) {
  2323. amdgpu_crtc->lut_r[i] = red[i] >> 6;
  2324. amdgpu_crtc->lut_g[i] = green[i] >> 6;
  2325. amdgpu_crtc->lut_b[i] = blue[i] >> 6;
  2326. }
  2327. dce_v10_0_crtc_load_lut(crtc);
  2328. }
  2329. static void dce_v10_0_crtc_destroy(struct drm_crtc *crtc)
  2330. {
  2331. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2332. drm_crtc_cleanup(crtc);
  2333. destroy_workqueue(amdgpu_crtc->pflip_queue);
  2334. kfree(amdgpu_crtc);
  2335. }
  2336. static const struct drm_crtc_funcs dce_v10_0_crtc_funcs = {
  2337. .cursor_set2 = dce_v10_0_crtc_cursor_set2,
  2338. .cursor_move = dce_v10_0_crtc_cursor_move,
  2339. .gamma_set = dce_v10_0_crtc_gamma_set,
  2340. .set_config = amdgpu_crtc_set_config,
  2341. .destroy = dce_v10_0_crtc_destroy,
  2342. .page_flip = amdgpu_crtc_page_flip,
  2343. };
  2344. static void dce_v10_0_crtc_dpms(struct drm_crtc *crtc, int mode)
  2345. {
  2346. struct drm_device *dev = crtc->dev;
  2347. struct amdgpu_device *adev = dev->dev_private;
  2348. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2349. unsigned type;
  2350. switch (mode) {
  2351. case DRM_MODE_DPMS_ON:
  2352. amdgpu_crtc->enabled = true;
  2353. amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
  2354. dce_v10_0_vga_enable(crtc, true);
  2355. amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
  2356. dce_v10_0_vga_enable(crtc, false);
  2357. /* Make sure VBLANK and PFLIP interrupts are still enabled */
  2358. type = amdgpu_crtc_idx_to_irq_type(adev, amdgpu_crtc->crtc_id);
  2359. amdgpu_irq_update(adev, &adev->crtc_irq, type);
  2360. amdgpu_irq_update(adev, &adev->pageflip_irq, type);
  2361. drm_vblank_post_modeset(dev, amdgpu_crtc->crtc_id);
  2362. dce_v10_0_crtc_load_lut(crtc);
  2363. break;
  2364. case DRM_MODE_DPMS_STANDBY:
  2365. case DRM_MODE_DPMS_SUSPEND:
  2366. case DRM_MODE_DPMS_OFF:
  2367. drm_vblank_pre_modeset(dev, amdgpu_crtc->crtc_id);
  2368. if (amdgpu_crtc->enabled) {
  2369. dce_v10_0_vga_enable(crtc, true);
  2370. amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
  2371. dce_v10_0_vga_enable(crtc, false);
  2372. }
  2373. amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
  2374. amdgpu_crtc->enabled = false;
  2375. break;
  2376. }
  2377. /* adjust pm to dpms */
  2378. amdgpu_pm_compute_clocks(adev);
  2379. }
  2380. static void dce_v10_0_crtc_prepare(struct drm_crtc *crtc)
  2381. {
  2382. /* disable crtc pair power gating before programming */
  2383. amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
  2384. amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
  2385. dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  2386. }
  2387. static void dce_v10_0_crtc_commit(struct drm_crtc *crtc)
  2388. {
  2389. dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  2390. amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
  2391. }
  2392. static void dce_v10_0_crtc_disable(struct drm_crtc *crtc)
  2393. {
  2394. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2395. struct drm_device *dev = crtc->dev;
  2396. struct amdgpu_device *adev = dev->dev_private;
  2397. struct amdgpu_atom_ss ss;
  2398. int i;
  2399. dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  2400. if (crtc->primary->fb) {
  2401. int r;
  2402. struct amdgpu_framebuffer *amdgpu_fb;
  2403. struct amdgpu_bo *rbo;
  2404. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  2405. rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  2406. r = amdgpu_bo_reserve(rbo, false);
  2407. if (unlikely(r))
  2408. DRM_ERROR("failed to reserve rbo before unpin\n");
  2409. else {
  2410. amdgpu_bo_unpin(rbo);
  2411. amdgpu_bo_unreserve(rbo);
  2412. }
  2413. }
  2414. /* disable the GRPH */
  2415. dce_v10_0_grph_enable(crtc, false);
  2416. amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
  2417. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2418. if (adev->mode_info.crtcs[i] &&
  2419. adev->mode_info.crtcs[i]->enabled &&
  2420. i != amdgpu_crtc->crtc_id &&
  2421. amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
  2422. /* one other crtc is using this pll don't turn
  2423. * off the pll
  2424. */
  2425. goto done;
  2426. }
  2427. }
  2428. switch (amdgpu_crtc->pll_id) {
  2429. case ATOM_PPLL0:
  2430. case ATOM_PPLL1:
  2431. case ATOM_PPLL2:
  2432. /* disable the ppll */
  2433. amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
  2434. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  2435. break;
  2436. default:
  2437. break;
  2438. }
  2439. done:
  2440. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  2441. amdgpu_crtc->adjusted_clock = 0;
  2442. amdgpu_crtc->encoder = NULL;
  2443. amdgpu_crtc->connector = NULL;
  2444. }
  2445. static int dce_v10_0_crtc_mode_set(struct drm_crtc *crtc,
  2446. struct drm_display_mode *mode,
  2447. struct drm_display_mode *adjusted_mode,
  2448. int x, int y, struct drm_framebuffer *old_fb)
  2449. {
  2450. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2451. if (!amdgpu_crtc->adjusted_clock)
  2452. return -EINVAL;
  2453. amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
  2454. amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
  2455. dce_v10_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  2456. amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
  2457. amdgpu_atombios_crtc_scaler_setup(crtc);
  2458. dce_v10_0_cursor_reset(crtc);
  2459. /* update the hw version fpr dpm */
  2460. amdgpu_crtc->hw_mode = *adjusted_mode;
  2461. return 0;
  2462. }
  2463. static bool dce_v10_0_crtc_mode_fixup(struct drm_crtc *crtc,
  2464. const struct drm_display_mode *mode,
  2465. struct drm_display_mode *adjusted_mode)
  2466. {
  2467. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2468. struct drm_device *dev = crtc->dev;
  2469. struct drm_encoder *encoder;
  2470. /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
  2471. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  2472. if (encoder->crtc == crtc) {
  2473. amdgpu_crtc->encoder = encoder;
  2474. amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
  2475. break;
  2476. }
  2477. }
  2478. if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
  2479. amdgpu_crtc->encoder = NULL;
  2480. amdgpu_crtc->connector = NULL;
  2481. return false;
  2482. }
  2483. if (!amdgpu_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
  2484. return false;
  2485. if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
  2486. return false;
  2487. /* pick pll */
  2488. amdgpu_crtc->pll_id = dce_v10_0_pick_pll(crtc);
  2489. /* if we can't get a PPLL for a non-DP encoder, fail */
  2490. if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
  2491. !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
  2492. return false;
  2493. return true;
  2494. }
  2495. static int dce_v10_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  2496. struct drm_framebuffer *old_fb)
  2497. {
  2498. return dce_v10_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  2499. }
  2500. static int dce_v10_0_crtc_set_base_atomic(struct drm_crtc *crtc,
  2501. struct drm_framebuffer *fb,
  2502. int x, int y, enum mode_set_atomic state)
  2503. {
  2504. return dce_v10_0_crtc_do_set_base(crtc, fb, x, y, 1);
  2505. }
  2506. static const struct drm_crtc_helper_funcs dce_v10_0_crtc_helper_funcs = {
  2507. .dpms = dce_v10_0_crtc_dpms,
  2508. .mode_fixup = dce_v10_0_crtc_mode_fixup,
  2509. .mode_set = dce_v10_0_crtc_mode_set,
  2510. .mode_set_base = dce_v10_0_crtc_set_base,
  2511. .mode_set_base_atomic = dce_v10_0_crtc_set_base_atomic,
  2512. .prepare = dce_v10_0_crtc_prepare,
  2513. .commit = dce_v10_0_crtc_commit,
  2514. .load_lut = dce_v10_0_crtc_load_lut,
  2515. .disable = dce_v10_0_crtc_disable,
  2516. };
  2517. static int dce_v10_0_crtc_init(struct amdgpu_device *adev, int index)
  2518. {
  2519. struct amdgpu_crtc *amdgpu_crtc;
  2520. int i;
  2521. amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
  2522. (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  2523. if (amdgpu_crtc == NULL)
  2524. return -ENOMEM;
  2525. drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_v10_0_crtc_funcs);
  2526. drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
  2527. amdgpu_crtc->crtc_id = index;
  2528. amdgpu_crtc->pflip_queue = create_singlethread_workqueue("amdgpu-pageflip-queue");
  2529. adev->mode_info.crtcs[index] = amdgpu_crtc;
  2530. amdgpu_crtc->max_cursor_width = 128;
  2531. amdgpu_crtc->max_cursor_height = 128;
  2532. adev->ddev->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
  2533. adev->ddev->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
  2534. for (i = 0; i < 256; i++) {
  2535. amdgpu_crtc->lut_r[i] = i << 2;
  2536. amdgpu_crtc->lut_g[i] = i << 2;
  2537. amdgpu_crtc->lut_b[i] = i << 2;
  2538. }
  2539. switch (amdgpu_crtc->crtc_id) {
  2540. case 0:
  2541. default:
  2542. amdgpu_crtc->crtc_offset = CRTC0_REGISTER_OFFSET;
  2543. break;
  2544. case 1:
  2545. amdgpu_crtc->crtc_offset = CRTC1_REGISTER_OFFSET;
  2546. break;
  2547. case 2:
  2548. amdgpu_crtc->crtc_offset = CRTC2_REGISTER_OFFSET;
  2549. break;
  2550. case 3:
  2551. amdgpu_crtc->crtc_offset = CRTC3_REGISTER_OFFSET;
  2552. break;
  2553. case 4:
  2554. amdgpu_crtc->crtc_offset = CRTC4_REGISTER_OFFSET;
  2555. break;
  2556. case 5:
  2557. amdgpu_crtc->crtc_offset = CRTC5_REGISTER_OFFSET;
  2558. break;
  2559. }
  2560. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  2561. amdgpu_crtc->adjusted_clock = 0;
  2562. amdgpu_crtc->encoder = NULL;
  2563. amdgpu_crtc->connector = NULL;
  2564. drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v10_0_crtc_helper_funcs);
  2565. return 0;
  2566. }
  2567. static int dce_v10_0_early_init(void *handle)
  2568. {
  2569. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2570. adev->audio_endpt_rreg = &dce_v10_0_audio_endpt_rreg;
  2571. adev->audio_endpt_wreg = &dce_v10_0_audio_endpt_wreg;
  2572. dce_v10_0_set_display_funcs(adev);
  2573. dce_v10_0_set_irq_funcs(adev);
  2574. switch (adev->asic_type) {
  2575. case CHIP_FIJI:
  2576. case CHIP_TONGA:
  2577. adev->mode_info.num_crtc = 6; /* XXX 7??? */
  2578. adev->mode_info.num_hpd = 6;
  2579. adev->mode_info.num_dig = 7;
  2580. break;
  2581. default:
  2582. /* FIXME: not supported yet */
  2583. return -EINVAL;
  2584. }
  2585. return 0;
  2586. }
  2587. static int dce_v10_0_sw_init(void *handle)
  2588. {
  2589. int r, i;
  2590. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2591. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2592. r = amdgpu_irq_add_id(adev, i + 1, &adev->crtc_irq);
  2593. if (r)
  2594. return r;
  2595. }
  2596. for (i = 8; i < 20; i += 2) {
  2597. r = amdgpu_irq_add_id(adev, i, &adev->pageflip_irq);
  2598. if (r)
  2599. return r;
  2600. }
  2601. /* HPD hotplug */
  2602. r = amdgpu_irq_add_id(adev, 42, &adev->hpd_irq);
  2603. if (r)
  2604. return r;
  2605. adev->mode_info.mode_config_initialized = true;
  2606. adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
  2607. adev->ddev->mode_config.max_width = 16384;
  2608. adev->ddev->mode_config.max_height = 16384;
  2609. adev->ddev->mode_config.preferred_depth = 24;
  2610. adev->ddev->mode_config.prefer_shadow = 1;
  2611. adev->ddev->mode_config.fb_base = adev->mc.aper_base;
  2612. r = amdgpu_modeset_create_props(adev);
  2613. if (r)
  2614. return r;
  2615. adev->ddev->mode_config.max_width = 16384;
  2616. adev->ddev->mode_config.max_height = 16384;
  2617. /* allocate crtcs */
  2618. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2619. r = dce_v10_0_crtc_init(adev, i);
  2620. if (r)
  2621. return r;
  2622. }
  2623. if (amdgpu_atombios_get_connector_info_from_object_table(adev))
  2624. amdgpu_print_display_setup(adev->ddev);
  2625. else
  2626. return -EINVAL;
  2627. /* setup afmt */
  2628. dce_v10_0_afmt_init(adev);
  2629. r = dce_v10_0_audio_init(adev);
  2630. if (r)
  2631. return r;
  2632. drm_kms_helper_poll_init(adev->ddev);
  2633. return r;
  2634. }
  2635. static int dce_v10_0_sw_fini(void *handle)
  2636. {
  2637. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2638. kfree(adev->mode_info.bios_hardcoded_edid);
  2639. drm_kms_helper_poll_fini(adev->ddev);
  2640. dce_v10_0_audio_fini(adev);
  2641. dce_v10_0_afmt_fini(adev);
  2642. drm_mode_config_cleanup(adev->ddev);
  2643. adev->mode_info.mode_config_initialized = false;
  2644. return 0;
  2645. }
  2646. static int dce_v10_0_hw_init(void *handle)
  2647. {
  2648. int i;
  2649. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2650. dce_v10_0_init_golden_registers(adev);
  2651. /* init dig PHYs, disp eng pll */
  2652. amdgpu_atombios_encoder_init_dig(adev);
  2653. amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
  2654. /* initialize hpd */
  2655. dce_v10_0_hpd_init(adev);
  2656. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2657. dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2658. }
  2659. dce_v10_0_pageflip_interrupt_init(adev);
  2660. return 0;
  2661. }
  2662. static int dce_v10_0_hw_fini(void *handle)
  2663. {
  2664. int i;
  2665. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2666. dce_v10_0_hpd_fini(adev);
  2667. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2668. dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2669. }
  2670. dce_v10_0_pageflip_interrupt_fini(adev);
  2671. return 0;
  2672. }
  2673. static int dce_v10_0_suspend(void *handle)
  2674. {
  2675. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2676. amdgpu_atombios_scratch_regs_save(adev);
  2677. return dce_v10_0_hw_fini(handle);
  2678. }
  2679. static int dce_v10_0_resume(void *handle)
  2680. {
  2681. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2682. int ret;
  2683. ret = dce_v10_0_hw_init(handle);
  2684. amdgpu_atombios_scratch_regs_restore(adev);
  2685. /* turn on the BL */
  2686. if (adev->mode_info.bl_encoder) {
  2687. u8 bl_level = amdgpu_display_backlight_get_level(adev,
  2688. adev->mode_info.bl_encoder);
  2689. amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
  2690. bl_level);
  2691. }
  2692. return ret;
  2693. }
  2694. static bool dce_v10_0_is_idle(void *handle)
  2695. {
  2696. return true;
  2697. }
  2698. static int dce_v10_0_wait_for_idle(void *handle)
  2699. {
  2700. return 0;
  2701. }
  2702. static void dce_v10_0_print_status(void *handle)
  2703. {
  2704. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2705. dev_info(adev->dev, "DCE 10.x registers\n");
  2706. /* XXX todo */
  2707. }
  2708. static int dce_v10_0_soft_reset(void *handle)
  2709. {
  2710. u32 srbm_soft_reset = 0, tmp;
  2711. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2712. if (dce_v10_0_is_display_hung(adev))
  2713. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
  2714. if (srbm_soft_reset) {
  2715. dce_v10_0_print_status((void *)adev);
  2716. tmp = RREG32(mmSRBM_SOFT_RESET);
  2717. tmp |= srbm_soft_reset;
  2718. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  2719. WREG32(mmSRBM_SOFT_RESET, tmp);
  2720. tmp = RREG32(mmSRBM_SOFT_RESET);
  2721. udelay(50);
  2722. tmp &= ~srbm_soft_reset;
  2723. WREG32(mmSRBM_SOFT_RESET, tmp);
  2724. tmp = RREG32(mmSRBM_SOFT_RESET);
  2725. /* Wait a little for things to settle down */
  2726. udelay(50);
  2727. dce_v10_0_print_status((void *)adev);
  2728. }
  2729. return 0;
  2730. }
  2731. static void dce_v10_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
  2732. int crtc,
  2733. enum amdgpu_interrupt_state state)
  2734. {
  2735. u32 lb_interrupt_mask;
  2736. if (crtc >= adev->mode_info.num_crtc) {
  2737. DRM_DEBUG("invalid crtc %d\n", crtc);
  2738. return;
  2739. }
  2740. switch (state) {
  2741. case AMDGPU_IRQ_STATE_DISABLE:
  2742. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2743. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2744. VBLANK_INTERRUPT_MASK, 0);
  2745. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2746. break;
  2747. case AMDGPU_IRQ_STATE_ENABLE:
  2748. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2749. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2750. VBLANK_INTERRUPT_MASK, 1);
  2751. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2752. break;
  2753. default:
  2754. break;
  2755. }
  2756. }
  2757. static void dce_v10_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
  2758. int crtc,
  2759. enum amdgpu_interrupt_state state)
  2760. {
  2761. u32 lb_interrupt_mask;
  2762. if (crtc >= adev->mode_info.num_crtc) {
  2763. DRM_DEBUG("invalid crtc %d\n", crtc);
  2764. return;
  2765. }
  2766. switch (state) {
  2767. case AMDGPU_IRQ_STATE_DISABLE:
  2768. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2769. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2770. VLINE_INTERRUPT_MASK, 0);
  2771. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2772. break;
  2773. case AMDGPU_IRQ_STATE_ENABLE:
  2774. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2775. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2776. VLINE_INTERRUPT_MASK, 1);
  2777. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2778. break;
  2779. default:
  2780. break;
  2781. }
  2782. }
  2783. static int dce_v10_0_set_hpd_irq_state(struct amdgpu_device *adev,
  2784. struct amdgpu_irq_src *source,
  2785. unsigned hpd,
  2786. enum amdgpu_interrupt_state state)
  2787. {
  2788. u32 tmp;
  2789. if (hpd >= adev->mode_info.num_hpd) {
  2790. DRM_DEBUG("invalid hdp %d\n", hpd);
  2791. return 0;
  2792. }
  2793. switch (state) {
  2794. case AMDGPU_IRQ_STATE_DISABLE:
  2795. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2796. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
  2797. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2798. break;
  2799. case AMDGPU_IRQ_STATE_ENABLE:
  2800. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2801. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 1);
  2802. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2803. break;
  2804. default:
  2805. break;
  2806. }
  2807. return 0;
  2808. }
  2809. static int dce_v10_0_set_crtc_irq_state(struct amdgpu_device *adev,
  2810. struct amdgpu_irq_src *source,
  2811. unsigned type,
  2812. enum amdgpu_interrupt_state state)
  2813. {
  2814. switch (type) {
  2815. case AMDGPU_CRTC_IRQ_VBLANK1:
  2816. dce_v10_0_set_crtc_vblank_interrupt_state(adev, 0, state);
  2817. break;
  2818. case AMDGPU_CRTC_IRQ_VBLANK2:
  2819. dce_v10_0_set_crtc_vblank_interrupt_state(adev, 1, state);
  2820. break;
  2821. case AMDGPU_CRTC_IRQ_VBLANK3:
  2822. dce_v10_0_set_crtc_vblank_interrupt_state(adev, 2, state);
  2823. break;
  2824. case AMDGPU_CRTC_IRQ_VBLANK4:
  2825. dce_v10_0_set_crtc_vblank_interrupt_state(adev, 3, state);
  2826. break;
  2827. case AMDGPU_CRTC_IRQ_VBLANK5:
  2828. dce_v10_0_set_crtc_vblank_interrupt_state(adev, 4, state);
  2829. break;
  2830. case AMDGPU_CRTC_IRQ_VBLANK6:
  2831. dce_v10_0_set_crtc_vblank_interrupt_state(adev, 5, state);
  2832. break;
  2833. case AMDGPU_CRTC_IRQ_VLINE1:
  2834. dce_v10_0_set_crtc_vline_interrupt_state(adev, 0, state);
  2835. break;
  2836. case AMDGPU_CRTC_IRQ_VLINE2:
  2837. dce_v10_0_set_crtc_vline_interrupt_state(adev, 1, state);
  2838. break;
  2839. case AMDGPU_CRTC_IRQ_VLINE3:
  2840. dce_v10_0_set_crtc_vline_interrupt_state(adev, 2, state);
  2841. break;
  2842. case AMDGPU_CRTC_IRQ_VLINE4:
  2843. dce_v10_0_set_crtc_vline_interrupt_state(adev, 3, state);
  2844. break;
  2845. case AMDGPU_CRTC_IRQ_VLINE5:
  2846. dce_v10_0_set_crtc_vline_interrupt_state(adev, 4, state);
  2847. break;
  2848. case AMDGPU_CRTC_IRQ_VLINE6:
  2849. dce_v10_0_set_crtc_vline_interrupt_state(adev, 5, state);
  2850. break;
  2851. default:
  2852. break;
  2853. }
  2854. return 0;
  2855. }
  2856. static int dce_v10_0_set_pageflip_irq_state(struct amdgpu_device *adev,
  2857. struct amdgpu_irq_src *src,
  2858. unsigned type,
  2859. enum amdgpu_interrupt_state state)
  2860. {
  2861. u32 reg;
  2862. if (type >= adev->mode_info.num_crtc) {
  2863. DRM_ERROR("invalid pageflip crtc %d\n", type);
  2864. return -EINVAL;
  2865. }
  2866. reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
  2867. if (state == AMDGPU_IRQ_STATE_DISABLE)
  2868. WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
  2869. reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2870. else
  2871. WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
  2872. reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2873. return 0;
  2874. }
  2875. static int dce_v10_0_pageflip_irq(struct amdgpu_device *adev,
  2876. struct amdgpu_irq_src *source,
  2877. struct amdgpu_iv_entry *entry)
  2878. {
  2879. unsigned long flags;
  2880. unsigned crtc_id;
  2881. struct amdgpu_crtc *amdgpu_crtc;
  2882. struct amdgpu_flip_work *works;
  2883. crtc_id = (entry->src_id - 8) >> 1;
  2884. amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  2885. if (crtc_id >= adev->mode_info.num_crtc) {
  2886. DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
  2887. return -EINVAL;
  2888. }
  2889. if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
  2890. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
  2891. WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
  2892. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
  2893. /* IRQ could occur when in initial stage */
  2894. if (amdgpu_crtc == NULL)
  2895. return 0;
  2896. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  2897. works = amdgpu_crtc->pflip_works;
  2898. if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
  2899. DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
  2900. "AMDGPU_FLIP_SUBMITTED(%d)\n",
  2901. amdgpu_crtc->pflip_status,
  2902. AMDGPU_FLIP_SUBMITTED);
  2903. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2904. return 0;
  2905. }
  2906. /* page flip completed. clean up */
  2907. amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
  2908. amdgpu_crtc->pflip_works = NULL;
  2909. /* wakeup usersapce */
  2910. if (works->event)
  2911. drm_send_vblank_event(adev->ddev, crtc_id, works->event);
  2912. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2913. drm_vblank_put(adev->ddev, amdgpu_crtc->crtc_id);
  2914. queue_work(amdgpu_crtc->pflip_queue, &works->unpin_work);
  2915. return 0;
  2916. }
  2917. static void dce_v10_0_hpd_int_ack(struct amdgpu_device *adev,
  2918. int hpd)
  2919. {
  2920. u32 tmp;
  2921. if (hpd >= adev->mode_info.num_hpd) {
  2922. DRM_DEBUG("invalid hdp %d\n", hpd);
  2923. return;
  2924. }
  2925. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2926. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_ACK, 1);
  2927. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2928. }
  2929. static void dce_v10_0_crtc_vblank_int_ack(struct amdgpu_device *adev,
  2930. int crtc)
  2931. {
  2932. u32 tmp;
  2933. if (crtc >= adev->mode_info.num_crtc) {
  2934. DRM_DEBUG("invalid crtc %d\n", crtc);
  2935. return;
  2936. }
  2937. tmp = RREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc]);
  2938. tmp = REG_SET_FIELD(tmp, LB_VBLANK_STATUS, VBLANK_ACK, 1);
  2939. WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], tmp);
  2940. }
  2941. static void dce_v10_0_crtc_vline_int_ack(struct amdgpu_device *adev,
  2942. int crtc)
  2943. {
  2944. u32 tmp;
  2945. if (crtc >= adev->mode_info.num_crtc) {
  2946. DRM_DEBUG("invalid crtc %d\n", crtc);
  2947. return;
  2948. }
  2949. tmp = RREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc]);
  2950. tmp = REG_SET_FIELD(tmp, LB_VLINE_STATUS, VLINE_ACK, 1);
  2951. WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], tmp);
  2952. }
  2953. static int dce_v10_0_crtc_irq(struct amdgpu_device *adev,
  2954. struct amdgpu_irq_src *source,
  2955. struct amdgpu_iv_entry *entry)
  2956. {
  2957. unsigned crtc = entry->src_id - 1;
  2958. uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
  2959. unsigned irq_type = amdgpu_crtc_idx_to_irq_type(adev, crtc);
  2960. switch (entry->src_data) {
  2961. case 0: /* vblank */
  2962. if (disp_int & interrupt_status_offsets[crtc].vblank)
  2963. dce_v10_0_crtc_vblank_int_ack(adev, crtc);
  2964. else
  2965. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  2966. if (amdgpu_irq_enabled(adev, source, irq_type)) {
  2967. drm_handle_vblank(adev->ddev, crtc);
  2968. }
  2969. DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
  2970. break;
  2971. case 1: /* vline */
  2972. if (disp_int & interrupt_status_offsets[crtc].vline)
  2973. dce_v10_0_crtc_vline_int_ack(adev, crtc);
  2974. else
  2975. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  2976. DRM_DEBUG("IH: D%d vline\n", crtc + 1);
  2977. break;
  2978. default:
  2979. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
  2980. break;
  2981. }
  2982. return 0;
  2983. }
  2984. static int dce_v10_0_hpd_irq(struct amdgpu_device *adev,
  2985. struct amdgpu_irq_src *source,
  2986. struct amdgpu_iv_entry *entry)
  2987. {
  2988. uint32_t disp_int, mask;
  2989. unsigned hpd;
  2990. if (entry->src_data >= adev->mode_info.num_hpd) {
  2991. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
  2992. return 0;
  2993. }
  2994. hpd = entry->src_data;
  2995. disp_int = RREG32(interrupt_status_offsets[hpd].reg);
  2996. mask = interrupt_status_offsets[hpd].hpd;
  2997. if (disp_int & mask) {
  2998. dce_v10_0_hpd_int_ack(adev, hpd);
  2999. schedule_work(&adev->hotplug_work);
  3000. DRM_DEBUG("IH: HPD%d\n", hpd + 1);
  3001. }
  3002. return 0;
  3003. }
  3004. static int dce_v10_0_set_clockgating_state(void *handle,
  3005. enum amd_clockgating_state state)
  3006. {
  3007. return 0;
  3008. }
  3009. static int dce_v10_0_set_powergating_state(void *handle,
  3010. enum amd_powergating_state state)
  3011. {
  3012. return 0;
  3013. }
  3014. const struct amd_ip_funcs dce_v10_0_ip_funcs = {
  3015. .early_init = dce_v10_0_early_init,
  3016. .late_init = NULL,
  3017. .sw_init = dce_v10_0_sw_init,
  3018. .sw_fini = dce_v10_0_sw_fini,
  3019. .hw_init = dce_v10_0_hw_init,
  3020. .hw_fini = dce_v10_0_hw_fini,
  3021. .suspend = dce_v10_0_suspend,
  3022. .resume = dce_v10_0_resume,
  3023. .is_idle = dce_v10_0_is_idle,
  3024. .wait_for_idle = dce_v10_0_wait_for_idle,
  3025. .soft_reset = dce_v10_0_soft_reset,
  3026. .print_status = dce_v10_0_print_status,
  3027. .set_clockgating_state = dce_v10_0_set_clockgating_state,
  3028. .set_powergating_state = dce_v10_0_set_powergating_state,
  3029. };
  3030. static void
  3031. dce_v10_0_encoder_mode_set(struct drm_encoder *encoder,
  3032. struct drm_display_mode *mode,
  3033. struct drm_display_mode *adjusted_mode)
  3034. {
  3035. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3036. amdgpu_encoder->pixel_clock = adjusted_mode->clock;
  3037. /* need to call this here rather than in prepare() since we need some crtc info */
  3038. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  3039. /* set scaler clears this on some chips */
  3040. dce_v10_0_set_interleave(encoder->crtc, mode);
  3041. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
  3042. dce_v10_0_afmt_enable(encoder, true);
  3043. dce_v10_0_afmt_setmode(encoder, adjusted_mode);
  3044. }
  3045. }
  3046. static void dce_v10_0_encoder_prepare(struct drm_encoder *encoder)
  3047. {
  3048. struct amdgpu_device *adev = encoder->dev->dev_private;
  3049. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3050. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  3051. if ((amdgpu_encoder->active_device &
  3052. (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
  3053. (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
  3054. ENCODER_OBJECT_ID_NONE)) {
  3055. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  3056. if (dig) {
  3057. dig->dig_encoder = dce_v10_0_pick_dig_encoder(encoder);
  3058. if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
  3059. dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
  3060. }
  3061. }
  3062. amdgpu_atombios_scratch_regs_lock(adev, true);
  3063. if (connector) {
  3064. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  3065. /* select the clock/data port if it uses a router */
  3066. if (amdgpu_connector->router.cd_valid)
  3067. amdgpu_i2c_router_select_cd_port(amdgpu_connector);
  3068. /* turn eDP panel on for mode set */
  3069. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  3070. amdgpu_atombios_encoder_set_edp_panel_power(connector,
  3071. ATOM_TRANSMITTER_ACTION_POWER_ON);
  3072. }
  3073. /* this is needed for the pll/ss setup to work correctly in some cases */
  3074. amdgpu_atombios_encoder_set_crtc_source(encoder);
  3075. /* set up the FMT blocks */
  3076. dce_v10_0_program_fmt(encoder);
  3077. }
  3078. static void dce_v10_0_encoder_commit(struct drm_encoder *encoder)
  3079. {
  3080. struct drm_device *dev = encoder->dev;
  3081. struct amdgpu_device *adev = dev->dev_private;
  3082. /* need to call this here as we need the crtc set up */
  3083. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  3084. amdgpu_atombios_scratch_regs_lock(adev, false);
  3085. }
  3086. static void dce_v10_0_encoder_disable(struct drm_encoder *encoder)
  3087. {
  3088. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3089. struct amdgpu_encoder_atom_dig *dig;
  3090. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  3091. if (amdgpu_atombios_encoder_is_digital(encoder)) {
  3092. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
  3093. dce_v10_0_afmt_enable(encoder, false);
  3094. dig = amdgpu_encoder->enc_priv;
  3095. dig->dig_encoder = -1;
  3096. }
  3097. amdgpu_encoder->active_device = 0;
  3098. }
  3099. /* these are handled by the primary encoders */
  3100. static void dce_v10_0_ext_prepare(struct drm_encoder *encoder)
  3101. {
  3102. }
  3103. static void dce_v10_0_ext_commit(struct drm_encoder *encoder)
  3104. {
  3105. }
  3106. static void
  3107. dce_v10_0_ext_mode_set(struct drm_encoder *encoder,
  3108. struct drm_display_mode *mode,
  3109. struct drm_display_mode *adjusted_mode)
  3110. {
  3111. }
  3112. static void dce_v10_0_ext_disable(struct drm_encoder *encoder)
  3113. {
  3114. }
  3115. static void
  3116. dce_v10_0_ext_dpms(struct drm_encoder *encoder, int mode)
  3117. {
  3118. }
  3119. static bool dce_v10_0_ext_mode_fixup(struct drm_encoder *encoder,
  3120. const struct drm_display_mode *mode,
  3121. struct drm_display_mode *adjusted_mode)
  3122. {
  3123. return true;
  3124. }
  3125. static const struct drm_encoder_helper_funcs dce_v10_0_ext_helper_funcs = {
  3126. .dpms = dce_v10_0_ext_dpms,
  3127. .mode_fixup = dce_v10_0_ext_mode_fixup,
  3128. .prepare = dce_v10_0_ext_prepare,
  3129. .mode_set = dce_v10_0_ext_mode_set,
  3130. .commit = dce_v10_0_ext_commit,
  3131. .disable = dce_v10_0_ext_disable,
  3132. /* no detect for TMDS/LVDS yet */
  3133. };
  3134. static const struct drm_encoder_helper_funcs dce_v10_0_dig_helper_funcs = {
  3135. .dpms = amdgpu_atombios_encoder_dpms,
  3136. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  3137. .prepare = dce_v10_0_encoder_prepare,
  3138. .mode_set = dce_v10_0_encoder_mode_set,
  3139. .commit = dce_v10_0_encoder_commit,
  3140. .disable = dce_v10_0_encoder_disable,
  3141. .detect = amdgpu_atombios_encoder_dig_detect,
  3142. };
  3143. static const struct drm_encoder_helper_funcs dce_v10_0_dac_helper_funcs = {
  3144. .dpms = amdgpu_atombios_encoder_dpms,
  3145. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  3146. .prepare = dce_v10_0_encoder_prepare,
  3147. .mode_set = dce_v10_0_encoder_mode_set,
  3148. .commit = dce_v10_0_encoder_commit,
  3149. .detect = amdgpu_atombios_encoder_dac_detect,
  3150. };
  3151. static void dce_v10_0_encoder_destroy(struct drm_encoder *encoder)
  3152. {
  3153. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3154. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  3155. amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
  3156. kfree(amdgpu_encoder->enc_priv);
  3157. drm_encoder_cleanup(encoder);
  3158. kfree(amdgpu_encoder);
  3159. }
  3160. static const struct drm_encoder_funcs dce_v10_0_encoder_funcs = {
  3161. .destroy = dce_v10_0_encoder_destroy,
  3162. };
  3163. static void dce_v10_0_encoder_add(struct amdgpu_device *adev,
  3164. uint32_t encoder_enum,
  3165. uint32_t supported_device,
  3166. u16 caps)
  3167. {
  3168. struct drm_device *dev = adev->ddev;
  3169. struct drm_encoder *encoder;
  3170. struct amdgpu_encoder *amdgpu_encoder;
  3171. /* see if we already added it */
  3172. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  3173. amdgpu_encoder = to_amdgpu_encoder(encoder);
  3174. if (amdgpu_encoder->encoder_enum == encoder_enum) {
  3175. amdgpu_encoder->devices |= supported_device;
  3176. return;
  3177. }
  3178. }
  3179. /* add a new one */
  3180. amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
  3181. if (!amdgpu_encoder)
  3182. return;
  3183. encoder = &amdgpu_encoder->base;
  3184. switch (adev->mode_info.num_crtc) {
  3185. case 1:
  3186. encoder->possible_crtcs = 0x1;
  3187. break;
  3188. case 2:
  3189. default:
  3190. encoder->possible_crtcs = 0x3;
  3191. break;
  3192. case 4:
  3193. encoder->possible_crtcs = 0xf;
  3194. break;
  3195. case 6:
  3196. encoder->possible_crtcs = 0x3f;
  3197. break;
  3198. }
  3199. amdgpu_encoder->enc_priv = NULL;
  3200. amdgpu_encoder->encoder_enum = encoder_enum;
  3201. amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
  3202. amdgpu_encoder->devices = supported_device;
  3203. amdgpu_encoder->rmx_type = RMX_OFF;
  3204. amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
  3205. amdgpu_encoder->is_ext_encoder = false;
  3206. amdgpu_encoder->caps = caps;
  3207. switch (amdgpu_encoder->encoder_id) {
  3208. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
  3209. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
  3210. drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
  3211. DRM_MODE_ENCODER_DAC);
  3212. drm_encoder_helper_add(encoder, &dce_v10_0_dac_helper_funcs);
  3213. break;
  3214. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
  3215. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  3216. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  3217. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  3218. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
  3219. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
  3220. amdgpu_encoder->rmx_type = RMX_FULL;
  3221. drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
  3222. DRM_MODE_ENCODER_LVDS);
  3223. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
  3224. } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
  3225. drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
  3226. DRM_MODE_ENCODER_DAC);
  3227. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  3228. } else {
  3229. drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
  3230. DRM_MODE_ENCODER_TMDS);
  3231. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  3232. }
  3233. drm_encoder_helper_add(encoder, &dce_v10_0_dig_helper_funcs);
  3234. break;
  3235. case ENCODER_OBJECT_ID_SI170B:
  3236. case ENCODER_OBJECT_ID_CH7303:
  3237. case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
  3238. case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
  3239. case ENCODER_OBJECT_ID_TITFP513:
  3240. case ENCODER_OBJECT_ID_VT1623:
  3241. case ENCODER_OBJECT_ID_HDMI_SI1930:
  3242. case ENCODER_OBJECT_ID_TRAVIS:
  3243. case ENCODER_OBJECT_ID_NUTMEG:
  3244. /* these are handled by the primary encoders */
  3245. amdgpu_encoder->is_ext_encoder = true;
  3246. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  3247. drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
  3248. DRM_MODE_ENCODER_LVDS);
  3249. else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
  3250. drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
  3251. DRM_MODE_ENCODER_DAC);
  3252. else
  3253. drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
  3254. DRM_MODE_ENCODER_TMDS);
  3255. drm_encoder_helper_add(encoder, &dce_v10_0_ext_helper_funcs);
  3256. break;
  3257. }
  3258. }
  3259. static const struct amdgpu_display_funcs dce_v10_0_display_funcs = {
  3260. .set_vga_render_state = &dce_v10_0_set_vga_render_state,
  3261. .bandwidth_update = &dce_v10_0_bandwidth_update,
  3262. .vblank_get_counter = &dce_v10_0_vblank_get_counter,
  3263. .vblank_wait = &dce_v10_0_vblank_wait,
  3264. .is_display_hung = &dce_v10_0_is_display_hung,
  3265. .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
  3266. .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
  3267. .hpd_sense = &dce_v10_0_hpd_sense,
  3268. .hpd_set_polarity = &dce_v10_0_hpd_set_polarity,
  3269. .hpd_get_gpio_reg = &dce_v10_0_hpd_get_gpio_reg,
  3270. .page_flip = &dce_v10_0_page_flip,
  3271. .page_flip_get_scanoutpos = &dce_v10_0_crtc_get_scanoutpos,
  3272. .add_encoder = &dce_v10_0_encoder_add,
  3273. .add_connector = &amdgpu_connector_add,
  3274. .stop_mc_access = &dce_v10_0_stop_mc_access,
  3275. .resume_mc_access = &dce_v10_0_resume_mc_access,
  3276. };
  3277. static void dce_v10_0_set_display_funcs(struct amdgpu_device *adev)
  3278. {
  3279. if (adev->mode_info.funcs == NULL)
  3280. adev->mode_info.funcs = &dce_v10_0_display_funcs;
  3281. }
  3282. static const struct amdgpu_irq_src_funcs dce_v10_0_crtc_irq_funcs = {
  3283. .set = dce_v10_0_set_crtc_irq_state,
  3284. .process = dce_v10_0_crtc_irq,
  3285. };
  3286. static const struct amdgpu_irq_src_funcs dce_v10_0_pageflip_irq_funcs = {
  3287. .set = dce_v10_0_set_pageflip_irq_state,
  3288. .process = dce_v10_0_pageflip_irq,
  3289. };
  3290. static const struct amdgpu_irq_src_funcs dce_v10_0_hpd_irq_funcs = {
  3291. .set = dce_v10_0_set_hpd_irq_state,
  3292. .process = dce_v10_0_hpd_irq,
  3293. };
  3294. static void dce_v10_0_set_irq_funcs(struct amdgpu_device *adev)
  3295. {
  3296. adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_LAST;
  3297. adev->crtc_irq.funcs = &dce_v10_0_crtc_irq_funcs;
  3298. adev->pageflip_irq.num_types = AMDGPU_PAGEFLIP_IRQ_LAST;
  3299. adev->pageflip_irq.funcs = &dce_v10_0_pageflip_irq_funcs;
  3300. adev->hpd_irq.num_types = AMDGPU_HPD_LAST;
  3301. adev->hpd_irq.funcs = &dce_v10_0_hpd_irq_funcs;
  3302. }