dce_v11_0.c 118 KB

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