dce_v11_0.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875
  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 <drm/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. tmp = div_u64((u64) dmif_size * (u64) wm->disp_clk, mc_latency + 512);
  945. tmp = min(dfixed_trunc(a), tmp);
  946. lb_fill_bw = min(tmp, wm->disp_clk * wm->bytes_per_pixel / 1000);
  947. a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
  948. b.full = dfixed_const(1000);
  949. c.full = dfixed_const(lb_fill_bw);
  950. b.full = dfixed_div(c, b);
  951. a.full = dfixed_div(a, b);
  952. line_fill_time = dfixed_trunc(a);
  953. if (line_fill_time < wm->active_time)
  954. return latency;
  955. else
  956. return latency + (line_fill_time - wm->active_time);
  957. }
  958. /**
  959. * dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display - check
  960. * average and available dram bandwidth
  961. *
  962. * @wm: watermark calculation data
  963. *
  964. * Check if the display average bandwidth fits in the display
  965. * dram bandwidth (CIK).
  966. * Used for display watermark bandwidth calculations
  967. * Returns true if the display fits, false if not.
  968. */
  969. static bool dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params *wm)
  970. {
  971. if (dce_v11_0_average_bandwidth(wm) <=
  972. (dce_v11_0_dram_bandwidth_for_display(wm) / wm->num_heads))
  973. return true;
  974. else
  975. return false;
  976. }
  977. /**
  978. * dce_v11_0_average_bandwidth_vs_available_bandwidth - check
  979. * average and available bandwidth
  980. *
  981. * @wm: watermark calculation data
  982. *
  983. * Check if the display average bandwidth fits in the display
  984. * available bandwidth (CIK).
  985. * Used for display watermark bandwidth calculations
  986. * Returns true if the display fits, false if not.
  987. */
  988. static bool dce_v11_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params *wm)
  989. {
  990. if (dce_v11_0_average_bandwidth(wm) <=
  991. (dce_v11_0_available_bandwidth(wm) / wm->num_heads))
  992. return true;
  993. else
  994. return false;
  995. }
  996. /**
  997. * dce_v11_0_check_latency_hiding - check latency hiding
  998. *
  999. * @wm: watermark calculation data
  1000. *
  1001. * Check latency hiding (CIK).
  1002. * Used for display watermark bandwidth calculations
  1003. * Returns true if the display fits, false if not.
  1004. */
  1005. static bool dce_v11_0_check_latency_hiding(struct dce10_wm_params *wm)
  1006. {
  1007. u32 lb_partitions = wm->lb_size / wm->src_width;
  1008. u32 line_time = wm->active_time + wm->blank_time;
  1009. u32 latency_tolerant_lines;
  1010. u32 latency_hiding;
  1011. fixed20_12 a;
  1012. a.full = dfixed_const(1);
  1013. if (wm->vsc.full > a.full)
  1014. latency_tolerant_lines = 1;
  1015. else {
  1016. if (lb_partitions <= (wm->vtaps + 1))
  1017. latency_tolerant_lines = 1;
  1018. else
  1019. latency_tolerant_lines = 2;
  1020. }
  1021. latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
  1022. if (dce_v11_0_latency_watermark(wm) <= latency_hiding)
  1023. return true;
  1024. else
  1025. return false;
  1026. }
  1027. /**
  1028. * dce_v11_0_program_watermarks - program display watermarks
  1029. *
  1030. * @adev: amdgpu_device pointer
  1031. * @amdgpu_crtc: the selected display controller
  1032. * @lb_size: line buffer size
  1033. * @num_heads: number of display controllers in use
  1034. *
  1035. * Calculate and program the display watermarks for the
  1036. * selected display controller (CIK).
  1037. */
  1038. static void dce_v11_0_program_watermarks(struct amdgpu_device *adev,
  1039. struct amdgpu_crtc *amdgpu_crtc,
  1040. u32 lb_size, u32 num_heads)
  1041. {
  1042. struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
  1043. struct dce10_wm_params wm_low, wm_high;
  1044. u32 active_time;
  1045. u32 line_time = 0;
  1046. u32 latency_watermark_a = 0, latency_watermark_b = 0;
  1047. u32 tmp, wm_mask, lb_vblank_lead_lines = 0;
  1048. if (amdgpu_crtc->base.enabled && num_heads && mode) {
  1049. active_time = (u32) div_u64((u64)mode->crtc_hdisplay * 1000000,
  1050. (u32)mode->clock);
  1051. line_time = (u32) div_u64((u64)mode->crtc_htotal * 1000000,
  1052. (u32)mode->clock);
  1053. line_time = min(line_time, (u32)65535);
  1054. /* watermark for high clocks */
  1055. if (adev->pm.dpm_enabled) {
  1056. wm_high.yclk =
  1057. amdgpu_dpm_get_mclk(adev, false) * 10;
  1058. wm_high.sclk =
  1059. amdgpu_dpm_get_sclk(adev, false) * 10;
  1060. } else {
  1061. wm_high.yclk = adev->pm.current_mclk * 10;
  1062. wm_high.sclk = adev->pm.current_sclk * 10;
  1063. }
  1064. wm_high.disp_clk = mode->clock;
  1065. wm_high.src_width = mode->crtc_hdisplay;
  1066. wm_high.active_time = active_time;
  1067. wm_high.blank_time = line_time - wm_high.active_time;
  1068. wm_high.interlaced = false;
  1069. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1070. wm_high.interlaced = true;
  1071. wm_high.vsc = amdgpu_crtc->vsc;
  1072. wm_high.vtaps = 1;
  1073. if (amdgpu_crtc->rmx_type != RMX_OFF)
  1074. wm_high.vtaps = 2;
  1075. wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
  1076. wm_high.lb_size = lb_size;
  1077. wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
  1078. wm_high.num_heads = num_heads;
  1079. /* set for high clocks */
  1080. latency_watermark_a = min(dce_v11_0_latency_watermark(&wm_high), (u32)65535);
  1081. /* possibly force display priority to high */
  1082. /* should really do this at mode validation time... */
  1083. if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
  1084. !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
  1085. !dce_v11_0_check_latency_hiding(&wm_high) ||
  1086. (adev->mode_info.disp_priority == 2)) {
  1087. DRM_DEBUG_KMS("force priority to high\n");
  1088. }
  1089. /* watermark for low clocks */
  1090. if (adev->pm.dpm_enabled) {
  1091. wm_low.yclk =
  1092. amdgpu_dpm_get_mclk(adev, true) * 10;
  1093. wm_low.sclk =
  1094. amdgpu_dpm_get_sclk(adev, true) * 10;
  1095. } else {
  1096. wm_low.yclk = adev->pm.current_mclk * 10;
  1097. wm_low.sclk = adev->pm.current_sclk * 10;
  1098. }
  1099. wm_low.disp_clk = mode->clock;
  1100. wm_low.src_width = mode->crtc_hdisplay;
  1101. wm_low.active_time = active_time;
  1102. wm_low.blank_time = line_time - wm_low.active_time;
  1103. wm_low.interlaced = false;
  1104. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1105. wm_low.interlaced = true;
  1106. wm_low.vsc = amdgpu_crtc->vsc;
  1107. wm_low.vtaps = 1;
  1108. if (amdgpu_crtc->rmx_type != RMX_OFF)
  1109. wm_low.vtaps = 2;
  1110. wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
  1111. wm_low.lb_size = lb_size;
  1112. wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
  1113. wm_low.num_heads = num_heads;
  1114. /* set for low clocks */
  1115. latency_watermark_b = min(dce_v11_0_latency_watermark(&wm_low), (u32)65535);
  1116. /* possibly force display priority to high */
  1117. /* should really do this at mode validation time... */
  1118. if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
  1119. !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
  1120. !dce_v11_0_check_latency_hiding(&wm_low) ||
  1121. (adev->mode_info.disp_priority == 2)) {
  1122. DRM_DEBUG_KMS("force priority to high\n");
  1123. }
  1124. lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
  1125. }
  1126. /* select wm A */
  1127. wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
  1128. tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 1);
  1129. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1130. tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
  1131. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_a);
  1132. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
  1133. WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1134. /* select wm B */
  1135. tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 2);
  1136. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1137. tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
  1138. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_b);
  1139. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
  1140. WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1141. /* restore original selection */
  1142. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
  1143. /* save values for DPM */
  1144. amdgpu_crtc->line_time = line_time;
  1145. amdgpu_crtc->wm_high = latency_watermark_a;
  1146. amdgpu_crtc->wm_low = latency_watermark_b;
  1147. /* Save number of lines the linebuffer leads before the scanout */
  1148. amdgpu_crtc->lb_vblank_lead_lines = lb_vblank_lead_lines;
  1149. }
  1150. /**
  1151. * dce_v11_0_bandwidth_update - program display watermarks
  1152. *
  1153. * @adev: amdgpu_device pointer
  1154. *
  1155. * Calculate and program the display watermarks and line
  1156. * buffer allocation (CIK).
  1157. */
  1158. static void dce_v11_0_bandwidth_update(struct amdgpu_device *adev)
  1159. {
  1160. struct drm_display_mode *mode = NULL;
  1161. u32 num_heads = 0, lb_size;
  1162. int i;
  1163. amdgpu_update_display_priority(adev);
  1164. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1165. if (adev->mode_info.crtcs[i]->base.enabled)
  1166. num_heads++;
  1167. }
  1168. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1169. mode = &adev->mode_info.crtcs[i]->base.mode;
  1170. lb_size = dce_v11_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
  1171. dce_v11_0_program_watermarks(adev, adev->mode_info.crtcs[i],
  1172. lb_size, num_heads);
  1173. }
  1174. }
  1175. static void dce_v11_0_audio_get_connected_pins(struct amdgpu_device *adev)
  1176. {
  1177. int i;
  1178. u32 offset, tmp;
  1179. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1180. offset = adev->mode_info.audio.pin[i].offset;
  1181. tmp = RREG32_AUDIO_ENDPT(offset,
  1182. ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
  1183. if (((tmp &
  1184. AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
  1185. AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
  1186. adev->mode_info.audio.pin[i].connected = false;
  1187. else
  1188. adev->mode_info.audio.pin[i].connected = true;
  1189. }
  1190. }
  1191. static struct amdgpu_audio_pin *dce_v11_0_audio_get_pin(struct amdgpu_device *adev)
  1192. {
  1193. int i;
  1194. dce_v11_0_audio_get_connected_pins(adev);
  1195. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1196. if (adev->mode_info.audio.pin[i].connected)
  1197. return &adev->mode_info.audio.pin[i];
  1198. }
  1199. DRM_ERROR("No connected audio pins found!\n");
  1200. return NULL;
  1201. }
  1202. static void dce_v11_0_afmt_audio_select_pin(struct drm_encoder *encoder)
  1203. {
  1204. struct amdgpu_device *adev = encoder->dev->dev_private;
  1205. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1206. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1207. u32 tmp;
  1208. if (!dig || !dig->afmt || !dig->afmt->pin)
  1209. return;
  1210. tmp = RREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset);
  1211. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_SRC_CONTROL, AFMT_AUDIO_SRC_SELECT, dig->afmt->pin->id);
  1212. WREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, tmp);
  1213. }
  1214. static void dce_v11_0_audio_write_latency_fields(struct drm_encoder *encoder,
  1215. struct drm_display_mode *mode)
  1216. {
  1217. struct amdgpu_device *adev = encoder->dev->dev_private;
  1218. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1219. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1220. struct drm_connector *connector;
  1221. struct amdgpu_connector *amdgpu_connector = NULL;
  1222. u32 tmp;
  1223. int interlace = 0;
  1224. if (!dig || !dig->afmt || !dig->afmt->pin)
  1225. return;
  1226. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1227. if (connector->encoder == encoder) {
  1228. amdgpu_connector = to_amdgpu_connector(connector);
  1229. break;
  1230. }
  1231. }
  1232. if (!amdgpu_connector) {
  1233. DRM_ERROR("Couldn't find encoder's connector\n");
  1234. return;
  1235. }
  1236. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1237. interlace = 1;
  1238. if (connector->latency_present[interlace]) {
  1239. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1240. VIDEO_LIPSYNC, connector->video_latency[interlace]);
  1241. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1242. AUDIO_LIPSYNC, connector->audio_latency[interlace]);
  1243. } else {
  1244. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1245. VIDEO_LIPSYNC, 0);
  1246. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1247. AUDIO_LIPSYNC, 0);
  1248. }
  1249. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1250. ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
  1251. }
  1252. static void dce_v11_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
  1253. {
  1254. struct amdgpu_device *adev = encoder->dev->dev_private;
  1255. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1256. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1257. struct drm_connector *connector;
  1258. struct amdgpu_connector *amdgpu_connector = NULL;
  1259. u32 tmp;
  1260. u8 *sadb = NULL;
  1261. int sad_count;
  1262. if (!dig || !dig->afmt || !dig->afmt->pin)
  1263. return;
  1264. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1265. if (connector->encoder == encoder) {
  1266. amdgpu_connector = to_amdgpu_connector(connector);
  1267. break;
  1268. }
  1269. }
  1270. if (!amdgpu_connector) {
  1271. DRM_ERROR("Couldn't find encoder's connector\n");
  1272. return;
  1273. }
  1274. sad_count = drm_edid_to_speaker_allocation(amdgpu_connector_edid(connector), &sadb);
  1275. if (sad_count < 0) {
  1276. DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
  1277. sad_count = 0;
  1278. }
  1279. /* program the speaker allocation */
  1280. tmp = RREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1281. ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
  1282. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1283. DP_CONNECTION, 0);
  1284. /* set HDMI mode */
  1285. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1286. HDMI_CONNECTION, 1);
  1287. if (sad_count)
  1288. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1289. SPEAKER_ALLOCATION, sadb[0]);
  1290. else
  1291. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1292. SPEAKER_ALLOCATION, 5); /* stereo */
  1293. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1294. ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
  1295. kfree(sadb);
  1296. }
  1297. static void dce_v11_0_audio_write_sad_regs(struct drm_encoder *encoder)
  1298. {
  1299. struct amdgpu_device *adev = encoder->dev->dev_private;
  1300. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1301. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1302. struct drm_connector *connector;
  1303. struct amdgpu_connector *amdgpu_connector = NULL;
  1304. struct cea_sad *sads;
  1305. int i, sad_count;
  1306. static const u16 eld_reg_to_type[][2] = {
  1307. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
  1308. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
  1309. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
  1310. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
  1311. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
  1312. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
  1313. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
  1314. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
  1315. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
  1316. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
  1317. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
  1318. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
  1319. };
  1320. if (!dig || !dig->afmt || !dig->afmt->pin)
  1321. return;
  1322. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1323. if (connector->encoder == encoder) {
  1324. amdgpu_connector = to_amdgpu_connector(connector);
  1325. break;
  1326. }
  1327. }
  1328. if (!amdgpu_connector) {
  1329. DRM_ERROR("Couldn't find encoder's connector\n");
  1330. return;
  1331. }
  1332. sad_count = drm_edid_to_sad(amdgpu_connector_edid(connector), &sads);
  1333. if (sad_count <= 0) {
  1334. DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
  1335. return;
  1336. }
  1337. BUG_ON(!sads);
  1338. for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
  1339. u32 tmp = 0;
  1340. u8 stereo_freqs = 0;
  1341. int max_channels = -1;
  1342. int j;
  1343. for (j = 0; j < sad_count; j++) {
  1344. struct cea_sad *sad = &sads[j];
  1345. if (sad->format == eld_reg_to_type[i][1]) {
  1346. if (sad->channels > max_channels) {
  1347. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1348. MAX_CHANNELS, sad->channels);
  1349. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1350. DESCRIPTOR_BYTE_2, sad->byte2);
  1351. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1352. SUPPORTED_FREQUENCIES, sad->freq);
  1353. max_channels = sad->channels;
  1354. }
  1355. if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
  1356. stereo_freqs |= sad->freq;
  1357. else
  1358. break;
  1359. }
  1360. }
  1361. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1362. SUPPORTED_FREQUENCIES_STEREO, stereo_freqs);
  1363. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset, eld_reg_to_type[i][0], tmp);
  1364. }
  1365. kfree(sads);
  1366. }
  1367. static void dce_v11_0_audio_enable(struct amdgpu_device *adev,
  1368. struct amdgpu_audio_pin *pin,
  1369. bool enable)
  1370. {
  1371. if (!pin)
  1372. return;
  1373. WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
  1374. enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
  1375. }
  1376. static const u32 pin_offsets[] =
  1377. {
  1378. AUD0_REGISTER_OFFSET,
  1379. AUD1_REGISTER_OFFSET,
  1380. AUD2_REGISTER_OFFSET,
  1381. AUD3_REGISTER_OFFSET,
  1382. AUD4_REGISTER_OFFSET,
  1383. AUD5_REGISTER_OFFSET,
  1384. AUD6_REGISTER_OFFSET,
  1385. AUD7_REGISTER_OFFSET,
  1386. };
  1387. static int dce_v11_0_audio_init(struct amdgpu_device *adev)
  1388. {
  1389. int i;
  1390. if (!amdgpu_audio)
  1391. return 0;
  1392. adev->mode_info.audio.enabled = true;
  1393. switch (adev->asic_type) {
  1394. case CHIP_CARRIZO:
  1395. case CHIP_STONEY:
  1396. adev->mode_info.audio.num_pins = 7;
  1397. break;
  1398. case CHIP_POLARIS10:
  1399. adev->mode_info.audio.num_pins = 8;
  1400. break;
  1401. case CHIP_POLARIS11:
  1402. case CHIP_POLARIS12:
  1403. adev->mode_info.audio.num_pins = 6;
  1404. break;
  1405. default:
  1406. return -EINVAL;
  1407. }
  1408. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1409. adev->mode_info.audio.pin[i].channels = -1;
  1410. adev->mode_info.audio.pin[i].rate = -1;
  1411. adev->mode_info.audio.pin[i].bits_per_sample = -1;
  1412. adev->mode_info.audio.pin[i].status_bits = 0;
  1413. adev->mode_info.audio.pin[i].category_code = 0;
  1414. adev->mode_info.audio.pin[i].connected = false;
  1415. adev->mode_info.audio.pin[i].offset = pin_offsets[i];
  1416. adev->mode_info.audio.pin[i].id = i;
  1417. /* disable audio. it will be set up later */
  1418. /* XXX remove once we switch to ip funcs */
  1419. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  1420. }
  1421. return 0;
  1422. }
  1423. static void dce_v11_0_audio_fini(struct amdgpu_device *adev)
  1424. {
  1425. int i;
  1426. if (!amdgpu_audio)
  1427. return;
  1428. if (!adev->mode_info.audio.enabled)
  1429. return;
  1430. for (i = 0; i < adev->mode_info.audio.num_pins; i++)
  1431. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  1432. adev->mode_info.audio.enabled = false;
  1433. }
  1434. /*
  1435. * update the N and CTS parameters for a given pixel clock rate
  1436. */
  1437. static void dce_v11_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
  1438. {
  1439. struct drm_device *dev = encoder->dev;
  1440. struct amdgpu_device *adev = dev->dev_private;
  1441. struct amdgpu_afmt_acr acr = amdgpu_afmt_acr(clock);
  1442. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1443. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1444. u32 tmp;
  1445. tmp = RREG32(mmHDMI_ACR_32_0 + dig->afmt->offset);
  1446. tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_0, HDMI_ACR_CTS_32, acr.cts_32khz);
  1447. WREG32(mmHDMI_ACR_32_0 + dig->afmt->offset, tmp);
  1448. tmp = RREG32(mmHDMI_ACR_32_1 + dig->afmt->offset);
  1449. tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_1, HDMI_ACR_N_32, acr.n_32khz);
  1450. WREG32(mmHDMI_ACR_32_1 + dig->afmt->offset, tmp);
  1451. tmp = RREG32(mmHDMI_ACR_44_0 + dig->afmt->offset);
  1452. tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_0, HDMI_ACR_CTS_44, acr.cts_44_1khz);
  1453. WREG32(mmHDMI_ACR_44_0 + dig->afmt->offset, tmp);
  1454. tmp = RREG32(mmHDMI_ACR_44_1 + dig->afmt->offset);
  1455. tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_1, HDMI_ACR_N_44, acr.n_44_1khz);
  1456. WREG32(mmHDMI_ACR_44_1 + dig->afmt->offset, tmp);
  1457. tmp = RREG32(mmHDMI_ACR_48_0 + dig->afmt->offset);
  1458. tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_0, HDMI_ACR_CTS_48, acr.cts_48khz);
  1459. WREG32(mmHDMI_ACR_48_0 + dig->afmt->offset, tmp);
  1460. tmp = RREG32(mmHDMI_ACR_48_1 + dig->afmt->offset);
  1461. tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_1, HDMI_ACR_N_48, acr.n_48khz);
  1462. WREG32(mmHDMI_ACR_48_1 + dig->afmt->offset, tmp);
  1463. }
  1464. /*
  1465. * build a HDMI Video Info Frame
  1466. */
  1467. static void dce_v11_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
  1468. void *buffer, size_t size)
  1469. {
  1470. struct drm_device *dev = encoder->dev;
  1471. struct amdgpu_device *adev = dev->dev_private;
  1472. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1473. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1474. uint8_t *frame = buffer + 3;
  1475. uint8_t *header = buffer;
  1476. WREG32(mmAFMT_AVI_INFO0 + dig->afmt->offset,
  1477. frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
  1478. WREG32(mmAFMT_AVI_INFO1 + dig->afmt->offset,
  1479. frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
  1480. WREG32(mmAFMT_AVI_INFO2 + dig->afmt->offset,
  1481. frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
  1482. WREG32(mmAFMT_AVI_INFO3 + dig->afmt->offset,
  1483. frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
  1484. }
  1485. static void dce_v11_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
  1486. {
  1487. struct drm_device *dev = encoder->dev;
  1488. struct amdgpu_device *adev = dev->dev_private;
  1489. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1490. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1491. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  1492. u32 dto_phase = 24 * 1000;
  1493. u32 dto_modulo = clock;
  1494. u32 tmp;
  1495. if (!dig || !dig->afmt)
  1496. return;
  1497. /* XXX two dtos; generally use dto0 for hdmi */
  1498. /* Express [24MHz / target pixel clock] as an exact rational
  1499. * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
  1500. * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
  1501. */
  1502. tmp = RREG32(mmDCCG_AUDIO_DTO_SOURCE);
  1503. tmp = REG_SET_FIELD(tmp, DCCG_AUDIO_DTO_SOURCE, DCCG_AUDIO_DTO0_SOURCE_SEL,
  1504. amdgpu_crtc->crtc_id);
  1505. WREG32(mmDCCG_AUDIO_DTO_SOURCE, tmp);
  1506. WREG32(mmDCCG_AUDIO_DTO0_PHASE, dto_phase);
  1507. WREG32(mmDCCG_AUDIO_DTO0_MODULE, dto_modulo);
  1508. }
  1509. /*
  1510. * update the info frames with the data from the current display mode
  1511. */
  1512. static void dce_v11_0_afmt_setmode(struct drm_encoder *encoder,
  1513. struct drm_display_mode *mode)
  1514. {
  1515. struct drm_device *dev = encoder->dev;
  1516. struct amdgpu_device *adev = dev->dev_private;
  1517. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1518. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1519. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  1520. u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
  1521. struct hdmi_avi_infoframe frame;
  1522. ssize_t err;
  1523. u32 tmp;
  1524. int bpc = 8;
  1525. if (!dig || !dig->afmt)
  1526. return;
  1527. /* Silent, r600_hdmi_enable will raise WARN for us */
  1528. if (!dig->afmt->enabled)
  1529. return;
  1530. /* hdmi deep color mode general control packets setup, if bpc > 8 */
  1531. if (encoder->crtc) {
  1532. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  1533. bpc = amdgpu_crtc->bpc;
  1534. }
  1535. /* disable audio prior to setting up hw */
  1536. dig->afmt->pin = dce_v11_0_audio_get_pin(adev);
  1537. dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
  1538. dce_v11_0_audio_set_dto(encoder, mode->clock);
  1539. tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
  1540. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1);
  1541. WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp); /* send null packets when required */
  1542. WREG32(mmAFMT_AUDIO_CRC_CONTROL + dig->afmt->offset, 0x1000);
  1543. tmp = RREG32(mmHDMI_CONTROL + dig->afmt->offset);
  1544. switch (bpc) {
  1545. case 0:
  1546. case 6:
  1547. case 8:
  1548. case 16:
  1549. default:
  1550. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 0);
  1551. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 0);
  1552. DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
  1553. connector->name, bpc);
  1554. break;
  1555. case 10:
  1556. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
  1557. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 1);
  1558. DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
  1559. connector->name);
  1560. break;
  1561. case 12:
  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, 2);
  1564. DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
  1565. connector->name);
  1566. break;
  1567. }
  1568. WREG32(mmHDMI_CONTROL + dig->afmt->offset, tmp);
  1569. tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
  1570. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1); /* send null packets when required */
  1571. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_SEND, 1); /* send general control packets */
  1572. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_CONT, 1); /* send general control packets every frame */
  1573. WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp);
  1574. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1575. /* enable audio info frames (frames won't be set until audio is enabled) */
  1576. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_SEND, 1);
  1577. /* required for audio info values to be updated */
  1578. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_CONT, 1);
  1579. WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1580. tmp = RREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1581. /* required for audio info values to be updated */
  1582. tmp = REG_SET_FIELD(tmp, AFMT_INFOFRAME_CONTROL0, AFMT_AUDIO_INFO_UPDATE, 1);
  1583. WREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1584. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
  1585. /* anything other than 0 */
  1586. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AUDIO_INFO_LINE, 2);
  1587. WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
  1588. WREG32(mmHDMI_GC + dig->afmt->offset, 0); /* unset HDMI_GC_AVMUTE */
  1589. tmp = RREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1590. /* set the default audio delay */
  1591. tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_DELAY_EN, 1);
  1592. /* should be suffient for all audio modes and small enough for all hblanks */
  1593. tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_PACKETS_PER_LINE, 3);
  1594. WREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1595. tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1596. /* allow 60958 channel status fields to be updated */
  1597. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_60958_CS_UPDATE, 1);
  1598. WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1599. tmp = RREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset);
  1600. if (bpc > 8)
  1601. /* clear SW CTS value */
  1602. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 0);
  1603. else
  1604. /* select SW CTS value */
  1605. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 1);
  1606. /* allow hw to sent ACR packets when required */
  1607. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_AUTO_SEND, 1);
  1608. WREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset, tmp);
  1609. dce_v11_0_afmt_update_ACR(encoder, mode->clock);
  1610. tmp = RREG32(mmAFMT_60958_0 + dig->afmt->offset);
  1611. tmp = REG_SET_FIELD(tmp, AFMT_60958_0, AFMT_60958_CS_CHANNEL_NUMBER_L, 1);
  1612. WREG32(mmAFMT_60958_0 + dig->afmt->offset, tmp);
  1613. tmp = RREG32(mmAFMT_60958_1 + dig->afmt->offset);
  1614. tmp = REG_SET_FIELD(tmp, AFMT_60958_1, AFMT_60958_CS_CHANNEL_NUMBER_R, 2);
  1615. WREG32(mmAFMT_60958_1 + dig->afmt->offset, tmp);
  1616. tmp = RREG32(mmAFMT_60958_2 + dig->afmt->offset);
  1617. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_2, 3);
  1618. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_3, 4);
  1619. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_4, 5);
  1620. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_5, 6);
  1621. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_6, 7);
  1622. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_7, 8);
  1623. WREG32(mmAFMT_60958_2 + dig->afmt->offset, tmp);
  1624. dce_v11_0_audio_write_speaker_allocation(encoder);
  1625. WREG32(mmAFMT_AUDIO_PACKET_CONTROL2 + dig->afmt->offset,
  1626. (0xff << AFMT_AUDIO_PACKET_CONTROL2__AFMT_AUDIO_CHANNEL_ENABLE__SHIFT));
  1627. dce_v11_0_afmt_audio_select_pin(encoder);
  1628. dce_v11_0_audio_write_sad_regs(encoder);
  1629. dce_v11_0_audio_write_latency_fields(encoder, mode);
  1630. err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
  1631. if (err < 0) {
  1632. DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
  1633. return;
  1634. }
  1635. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  1636. if (err < 0) {
  1637. DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
  1638. return;
  1639. }
  1640. dce_v11_0_afmt_update_avi_infoframe(encoder, buffer, sizeof(buffer));
  1641. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1642. /* enable AVI info frames */
  1643. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_SEND, 1);
  1644. /* required for audio info values to be updated */
  1645. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_CONT, 1);
  1646. WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1647. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
  1648. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AVI_INFO_LINE, 2);
  1649. WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
  1650. tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1651. /* send audio packets */
  1652. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_AUDIO_SAMPLE_SEND, 1);
  1653. WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1654. WREG32(mmAFMT_RAMP_CONTROL0 + dig->afmt->offset, 0x00FFFFFF);
  1655. WREG32(mmAFMT_RAMP_CONTROL1 + dig->afmt->offset, 0x007FFFFF);
  1656. WREG32(mmAFMT_RAMP_CONTROL2 + dig->afmt->offset, 0x00000001);
  1657. WREG32(mmAFMT_RAMP_CONTROL3 + dig->afmt->offset, 0x00000001);
  1658. /* enable audio after to setting up hw */
  1659. dce_v11_0_audio_enable(adev, dig->afmt->pin, true);
  1660. }
  1661. static void dce_v11_0_afmt_enable(struct drm_encoder *encoder, bool enable)
  1662. {
  1663. struct drm_device *dev = encoder->dev;
  1664. struct amdgpu_device *adev = dev->dev_private;
  1665. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1666. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1667. if (!dig || !dig->afmt)
  1668. return;
  1669. /* Silent, r600_hdmi_enable will raise WARN for us */
  1670. if (enable && dig->afmt->enabled)
  1671. return;
  1672. if (!enable && !dig->afmt->enabled)
  1673. return;
  1674. if (!enable && dig->afmt->pin) {
  1675. dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
  1676. dig->afmt->pin = NULL;
  1677. }
  1678. dig->afmt->enabled = enable;
  1679. DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
  1680. enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
  1681. }
  1682. static int dce_v11_0_afmt_init(struct amdgpu_device *adev)
  1683. {
  1684. int i;
  1685. for (i = 0; i < adev->mode_info.num_dig; i++)
  1686. adev->mode_info.afmt[i] = NULL;
  1687. /* DCE11 has audio blocks tied to DIG encoders */
  1688. for (i = 0; i < adev->mode_info.num_dig; i++) {
  1689. adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
  1690. if (adev->mode_info.afmt[i]) {
  1691. adev->mode_info.afmt[i]->offset = dig_offsets[i];
  1692. adev->mode_info.afmt[i]->id = i;
  1693. } else {
  1694. int j;
  1695. for (j = 0; j < i; j++) {
  1696. kfree(adev->mode_info.afmt[j]);
  1697. adev->mode_info.afmt[j] = NULL;
  1698. }
  1699. return -ENOMEM;
  1700. }
  1701. }
  1702. return 0;
  1703. }
  1704. static void dce_v11_0_afmt_fini(struct amdgpu_device *adev)
  1705. {
  1706. int i;
  1707. for (i = 0; i < adev->mode_info.num_dig; i++) {
  1708. kfree(adev->mode_info.afmt[i]);
  1709. adev->mode_info.afmt[i] = NULL;
  1710. }
  1711. }
  1712. static const u32 vga_control_regs[6] =
  1713. {
  1714. mmD1VGA_CONTROL,
  1715. mmD2VGA_CONTROL,
  1716. mmD3VGA_CONTROL,
  1717. mmD4VGA_CONTROL,
  1718. mmD5VGA_CONTROL,
  1719. mmD6VGA_CONTROL,
  1720. };
  1721. static void dce_v11_0_vga_enable(struct drm_crtc *crtc, bool enable)
  1722. {
  1723. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1724. struct drm_device *dev = crtc->dev;
  1725. struct amdgpu_device *adev = dev->dev_private;
  1726. u32 vga_control;
  1727. vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
  1728. if (enable)
  1729. WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
  1730. else
  1731. WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
  1732. }
  1733. static void dce_v11_0_grph_enable(struct drm_crtc *crtc, bool enable)
  1734. {
  1735. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1736. struct drm_device *dev = crtc->dev;
  1737. struct amdgpu_device *adev = dev->dev_private;
  1738. if (enable)
  1739. WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
  1740. else
  1741. WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
  1742. }
  1743. static int dce_v11_0_crtc_do_set_base(struct drm_crtc *crtc,
  1744. struct drm_framebuffer *fb,
  1745. int x, int y, int atomic)
  1746. {
  1747. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1748. struct drm_device *dev = crtc->dev;
  1749. struct amdgpu_device *adev = dev->dev_private;
  1750. struct amdgpu_framebuffer *amdgpu_fb;
  1751. struct drm_framebuffer *target_fb;
  1752. struct drm_gem_object *obj;
  1753. struct amdgpu_bo *abo;
  1754. uint64_t fb_location, tiling_flags;
  1755. uint32_t fb_format, fb_pitch_pixels;
  1756. u32 fb_swap = REG_SET_FIELD(0, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP, ENDIAN_NONE);
  1757. u32 pipe_config;
  1758. u32 tmp, viewport_w, viewport_h;
  1759. int r;
  1760. bool bypass_lut = false;
  1761. struct drm_format_name_buf format_name;
  1762. /* no fb bound */
  1763. if (!atomic && !crtc->primary->fb) {
  1764. DRM_DEBUG_KMS("No FB bound\n");
  1765. return 0;
  1766. }
  1767. if (atomic) {
  1768. amdgpu_fb = to_amdgpu_framebuffer(fb);
  1769. target_fb = fb;
  1770. } else {
  1771. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  1772. target_fb = crtc->primary->fb;
  1773. }
  1774. /* If atomic, assume fb object is pinned & idle & fenced and
  1775. * just update base pointers
  1776. */
  1777. obj = amdgpu_fb->obj;
  1778. abo = gem_to_amdgpu_bo(obj);
  1779. r = amdgpu_bo_reserve(abo, false);
  1780. if (unlikely(r != 0))
  1781. return r;
  1782. if (atomic) {
  1783. fb_location = amdgpu_bo_gpu_offset(abo);
  1784. } else {
  1785. r = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM, &fb_location);
  1786. if (unlikely(r != 0)) {
  1787. amdgpu_bo_unreserve(abo);
  1788. return -EINVAL;
  1789. }
  1790. }
  1791. amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
  1792. amdgpu_bo_unreserve(abo);
  1793. pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
  1794. switch (target_fb->format->format) {
  1795. case DRM_FORMAT_C8:
  1796. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 0);
  1797. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1798. break;
  1799. case DRM_FORMAT_XRGB4444:
  1800. case DRM_FORMAT_ARGB4444:
  1801. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1802. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 2);
  1803. #ifdef __BIG_ENDIAN
  1804. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1805. ENDIAN_8IN16);
  1806. #endif
  1807. break;
  1808. case DRM_FORMAT_XRGB1555:
  1809. case DRM_FORMAT_ARGB1555:
  1810. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1811. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1812. #ifdef __BIG_ENDIAN
  1813. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1814. ENDIAN_8IN16);
  1815. #endif
  1816. break;
  1817. case DRM_FORMAT_BGRX5551:
  1818. case DRM_FORMAT_BGRA5551:
  1819. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1820. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 5);
  1821. #ifdef __BIG_ENDIAN
  1822. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1823. ENDIAN_8IN16);
  1824. #endif
  1825. break;
  1826. case DRM_FORMAT_RGB565:
  1827. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1828. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
  1829. #ifdef __BIG_ENDIAN
  1830. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1831. ENDIAN_8IN16);
  1832. #endif
  1833. break;
  1834. case DRM_FORMAT_XRGB8888:
  1835. case DRM_FORMAT_ARGB8888:
  1836. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1837. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1838. #ifdef __BIG_ENDIAN
  1839. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1840. ENDIAN_8IN32);
  1841. #endif
  1842. break;
  1843. case DRM_FORMAT_XRGB2101010:
  1844. case DRM_FORMAT_ARGB2101010:
  1845. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1846. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
  1847. #ifdef __BIG_ENDIAN
  1848. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1849. ENDIAN_8IN32);
  1850. #endif
  1851. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1852. bypass_lut = true;
  1853. break;
  1854. case DRM_FORMAT_BGRX1010102:
  1855. case DRM_FORMAT_BGRA1010102:
  1856. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1857. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 4);
  1858. #ifdef __BIG_ENDIAN
  1859. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1860. ENDIAN_8IN32);
  1861. #endif
  1862. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1863. bypass_lut = true;
  1864. break;
  1865. default:
  1866. DRM_ERROR("Unsupported screen format %s\n",
  1867. drm_get_format_name(target_fb->format->format, &format_name));
  1868. return -EINVAL;
  1869. }
  1870. if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
  1871. unsigned bankw, bankh, mtaspect, tile_split, num_banks;
  1872. bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
  1873. bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
  1874. mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
  1875. tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
  1876. num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
  1877. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_NUM_BANKS, num_banks);
  1878. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
  1879. ARRAY_2D_TILED_THIN1);
  1880. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_TILE_SPLIT,
  1881. tile_split);
  1882. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_WIDTH, bankw);
  1883. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_HEIGHT, bankh);
  1884. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MACRO_TILE_ASPECT,
  1885. mtaspect);
  1886. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MICRO_TILE_MODE,
  1887. ADDR_SURF_MICRO_TILING_DISPLAY);
  1888. } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1) {
  1889. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
  1890. ARRAY_1D_TILED_THIN1);
  1891. }
  1892. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_PIPE_CONFIG,
  1893. pipe_config);
  1894. dce_v11_0_vga_enable(crtc, false);
  1895. /* Make sure surface address is updated at vertical blank rather than
  1896. * horizontal blank
  1897. */
  1898. tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
  1899. tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
  1900. GRPH_SURFACE_UPDATE_H_RETRACE_EN, 0);
  1901. WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1902. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  1903. upper_32_bits(fb_location));
  1904. WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  1905. upper_32_bits(fb_location));
  1906. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  1907. (u32)fb_location & GRPH_PRIMARY_SURFACE_ADDRESS__GRPH_PRIMARY_SURFACE_ADDRESS_MASK);
  1908. WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  1909. (u32) fb_location & GRPH_SECONDARY_SURFACE_ADDRESS__GRPH_SECONDARY_SURFACE_ADDRESS_MASK);
  1910. WREG32(mmGRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
  1911. WREG32(mmGRPH_SWAP_CNTL + amdgpu_crtc->crtc_offset, fb_swap);
  1912. /*
  1913. * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
  1914. * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
  1915. * retain the full precision throughout the pipeline.
  1916. */
  1917. tmp = RREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset);
  1918. if (bypass_lut)
  1919. tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 1);
  1920. else
  1921. tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 0);
  1922. WREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset, tmp);
  1923. if (bypass_lut)
  1924. DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
  1925. WREG32(mmGRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
  1926. WREG32(mmGRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
  1927. WREG32(mmGRPH_X_START + amdgpu_crtc->crtc_offset, 0);
  1928. WREG32(mmGRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
  1929. WREG32(mmGRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
  1930. WREG32(mmGRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
  1931. fb_pitch_pixels = target_fb->pitches[0] / target_fb->format->cpp[0];
  1932. WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
  1933. dce_v11_0_grph_enable(crtc, true);
  1934. WREG32(mmLB_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
  1935. target_fb->height);
  1936. x &= ~3;
  1937. y &= ~1;
  1938. WREG32(mmVIEWPORT_START + amdgpu_crtc->crtc_offset,
  1939. (x << 16) | y);
  1940. viewport_w = crtc->mode.hdisplay;
  1941. viewport_h = (crtc->mode.vdisplay + 1) & ~1;
  1942. WREG32(mmVIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
  1943. (viewport_w << 16) | viewport_h);
  1944. /* set pageflip to happen anywhere in vblank interval */
  1945. WREG32(mmCRTC_MASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 0);
  1946. if (!atomic && fb && fb != crtc->primary->fb) {
  1947. amdgpu_fb = to_amdgpu_framebuffer(fb);
  1948. abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  1949. r = amdgpu_bo_reserve(abo, true);
  1950. if (unlikely(r != 0))
  1951. return r;
  1952. amdgpu_bo_unpin(abo);
  1953. amdgpu_bo_unreserve(abo);
  1954. }
  1955. /* Bytes per pixel may have changed */
  1956. dce_v11_0_bandwidth_update(adev);
  1957. return 0;
  1958. }
  1959. static void dce_v11_0_set_interleave(struct drm_crtc *crtc,
  1960. struct drm_display_mode *mode)
  1961. {
  1962. struct drm_device *dev = crtc->dev;
  1963. struct amdgpu_device *adev = dev->dev_private;
  1964. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1965. u32 tmp;
  1966. tmp = RREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset);
  1967. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1968. tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 1);
  1969. else
  1970. tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 0);
  1971. WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset, tmp);
  1972. }
  1973. static void dce_v11_0_crtc_load_lut(struct drm_crtc *crtc)
  1974. {
  1975. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1976. struct drm_device *dev = crtc->dev;
  1977. struct amdgpu_device *adev = dev->dev_private;
  1978. int i;
  1979. u32 tmp;
  1980. DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
  1981. tmp = RREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
  1982. tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_GRPH_MODE, 0);
  1983. WREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1984. tmp = RREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset);
  1985. tmp = REG_SET_FIELD(tmp, PRESCALE_GRPH_CONTROL, GRPH_PRESCALE_BYPASS, 1);
  1986. WREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1987. tmp = RREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  1988. tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, GRPH_INPUT_GAMMA_MODE, 0);
  1989. WREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1990. WREG32(mmDC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
  1991. WREG32(mmDC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
  1992. WREG32(mmDC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
  1993. WREG32(mmDC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
  1994. WREG32(mmDC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
  1995. WREG32(mmDC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
  1996. WREG32(mmDC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
  1997. WREG32(mmDC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
  1998. WREG32(mmDC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
  1999. WREG32(mmDC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
  2000. for (i = 0; i < 256; i++) {
  2001. WREG32(mmDC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
  2002. (amdgpu_crtc->lut_r[i] << 20) |
  2003. (amdgpu_crtc->lut_g[i] << 10) |
  2004. (amdgpu_crtc->lut_b[i] << 0));
  2005. }
  2006. tmp = RREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  2007. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, GRPH_DEGAMMA_MODE, 0);
  2008. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR_DEGAMMA_MODE, 0);
  2009. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR2_DEGAMMA_MODE, 0);
  2010. WREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2011. tmp = RREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset);
  2012. tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, GRPH_GAMUT_REMAP_MODE, 0);
  2013. WREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2014. tmp = RREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  2015. tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, GRPH_REGAMMA_MODE, 0);
  2016. WREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2017. tmp = RREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
  2018. tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_GRPH_MODE, 0);
  2019. WREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2020. /* XXX match this to the depth of the crtc fmt block, move to modeset? */
  2021. WREG32(mmDENORM_CONTROL + amdgpu_crtc->crtc_offset, 0);
  2022. /* XXX this only needs to be programmed once per crtc at startup,
  2023. * not sure where the best place for it is
  2024. */
  2025. tmp = RREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset);
  2026. tmp = REG_SET_FIELD(tmp, ALPHA_CONTROL, CURSOR_ALPHA_BLND_ENA, 1);
  2027. WREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2028. }
  2029. static int dce_v11_0_pick_dig_encoder(struct drm_encoder *encoder)
  2030. {
  2031. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2032. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  2033. switch (amdgpu_encoder->encoder_id) {
  2034. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  2035. if (dig->linkb)
  2036. return 1;
  2037. else
  2038. return 0;
  2039. break;
  2040. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  2041. if (dig->linkb)
  2042. return 3;
  2043. else
  2044. return 2;
  2045. break;
  2046. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  2047. if (dig->linkb)
  2048. return 5;
  2049. else
  2050. return 4;
  2051. break;
  2052. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
  2053. return 6;
  2054. break;
  2055. default:
  2056. DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
  2057. return 0;
  2058. }
  2059. }
  2060. /**
  2061. * dce_v11_0_pick_pll - Allocate a PPLL for use by the crtc.
  2062. *
  2063. * @crtc: drm crtc
  2064. *
  2065. * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
  2066. * a single PPLL can be used for all DP crtcs/encoders. For non-DP
  2067. * monitors a dedicated PPLL must be used. If a particular board has
  2068. * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
  2069. * as there is no need to program the PLL itself. If we are not able to
  2070. * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
  2071. * avoid messing up an existing monitor.
  2072. *
  2073. * Asic specific PLL information
  2074. *
  2075. * DCE 10.x
  2076. * Tonga
  2077. * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
  2078. * CI
  2079. * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
  2080. *
  2081. */
  2082. static u32 dce_v11_0_pick_pll(struct drm_crtc *crtc)
  2083. {
  2084. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2085. struct drm_device *dev = crtc->dev;
  2086. struct amdgpu_device *adev = dev->dev_private;
  2087. u32 pll_in_use;
  2088. int pll;
  2089. if ((adev->asic_type == CHIP_POLARIS10) ||
  2090. (adev->asic_type == CHIP_POLARIS11) ||
  2091. (adev->asic_type == CHIP_POLARIS12)) {
  2092. struct amdgpu_encoder *amdgpu_encoder =
  2093. to_amdgpu_encoder(amdgpu_crtc->encoder);
  2094. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  2095. if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
  2096. return ATOM_DP_DTO;
  2097. switch (amdgpu_encoder->encoder_id) {
  2098. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  2099. if (dig->linkb)
  2100. return ATOM_COMBOPHY_PLL1;
  2101. else
  2102. return ATOM_COMBOPHY_PLL0;
  2103. break;
  2104. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  2105. if (dig->linkb)
  2106. return ATOM_COMBOPHY_PLL3;
  2107. else
  2108. return ATOM_COMBOPHY_PLL2;
  2109. break;
  2110. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  2111. if (dig->linkb)
  2112. return ATOM_COMBOPHY_PLL5;
  2113. else
  2114. return ATOM_COMBOPHY_PLL4;
  2115. break;
  2116. default:
  2117. DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
  2118. return ATOM_PPLL_INVALID;
  2119. }
  2120. }
  2121. if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
  2122. if (adev->clock.dp_extclk)
  2123. /* skip PPLL programming if using ext clock */
  2124. return ATOM_PPLL_INVALID;
  2125. else {
  2126. /* use the same PPLL for all DP monitors */
  2127. pll = amdgpu_pll_get_shared_dp_ppll(crtc);
  2128. if (pll != ATOM_PPLL_INVALID)
  2129. return pll;
  2130. }
  2131. } else {
  2132. /* use the same PPLL for all monitors with the same clock */
  2133. pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
  2134. if (pll != ATOM_PPLL_INVALID)
  2135. return pll;
  2136. }
  2137. /* XXX need to determine what plls are available on each DCE11 part */
  2138. pll_in_use = amdgpu_pll_get_use_mask(crtc);
  2139. if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY) {
  2140. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  2141. return ATOM_PPLL1;
  2142. if (!(pll_in_use & (1 << ATOM_PPLL0)))
  2143. return ATOM_PPLL0;
  2144. DRM_ERROR("unable to allocate a PPLL\n");
  2145. return ATOM_PPLL_INVALID;
  2146. } else {
  2147. if (!(pll_in_use & (1 << ATOM_PPLL2)))
  2148. return ATOM_PPLL2;
  2149. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  2150. return ATOM_PPLL1;
  2151. if (!(pll_in_use & (1 << ATOM_PPLL0)))
  2152. return ATOM_PPLL0;
  2153. DRM_ERROR("unable to allocate a PPLL\n");
  2154. return ATOM_PPLL_INVALID;
  2155. }
  2156. return ATOM_PPLL_INVALID;
  2157. }
  2158. static void dce_v11_0_lock_cursor(struct drm_crtc *crtc, bool lock)
  2159. {
  2160. struct amdgpu_device *adev = crtc->dev->dev_private;
  2161. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2162. uint32_t cur_lock;
  2163. cur_lock = RREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset);
  2164. if (lock)
  2165. cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 1);
  2166. else
  2167. cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 0);
  2168. WREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
  2169. }
  2170. static void dce_v11_0_hide_cursor(struct drm_crtc *crtc)
  2171. {
  2172. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2173. struct amdgpu_device *adev = crtc->dev->dev_private;
  2174. u32 tmp;
  2175. tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
  2176. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 0);
  2177. WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2178. }
  2179. static void dce_v11_0_show_cursor(struct drm_crtc *crtc)
  2180. {
  2181. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2182. struct amdgpu_device *adev = crtc->dev->dev_private;
  2183. u32 tmp;
  2184. WREG32(mmCUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  2185. upper_32_bits(amdgpu_crtc->cursor_addr));
  2186. WREG32(mmCUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  2187. lower_32_bits(amdgpu_crtc->cursor_addr));
  2188. tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
  2189. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 1);
  2190. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_MODE, 2);
  2191. WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2192. }
  2193. static int dce_v11_0_cursor_move_locked(struct drm_crtc *crtc,
  2194. int x, int y)
  2195. {
  2196. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2197. struct amdgpu_device *adev = crtc->dev->dev_private;
  2198. int xorigin = 0, yorigin = 0;
  2199. amdgpu_crtc->cursor_x = x;
  2200. amdgpu_crtc->cursor_y = y;
  2201. /* avivo cursor are offset into the total surface */
  2202. x += crtc->x;
  2203. y += crtc->y;
  2204. DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
  2205. if (x < 0) {
  2206. xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
  2207. x = 0;
  2208. }
  2209. if (y < 0) {
  2210. yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
  2211. y = 0;
  2212. }
  2213. WREG32(mmCUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
  2214. WREG32(mmCUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
  2215. WREG32(mmCUR_SIZE + amdgpu_crtc->crtc_offset,
  2216. ((amdgpu_crtc->cursor_width - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
  2217. return 0;
  2218. }
  2219. static int dce_v11_0_crtc_cursor_move(struct drm_crtc *crtc,
  2220. int x, int y)
  2221. {
  2222. int ret;
  2223. dce_v11_0_lock_cursor(crtc, true);
  2224. ret = dce_v11_0_cursor_move_locked(crtc, x, y);
  2225. dce_v11_0_lock_cursor(crtc, false);
  2226. return ret;
  2227. }
  2228. static int dce_v11_0_crtc_cursor_set2(struct drm_crtc *crtc,
  2229. struct drm_file *file_priv,
  2230. uint32_t handle,
  2231. uint32_t width,
  2232. uint32_t height,
  2233. int32_t hot_x,
  2234. int32_t hot_y)
  2235. {
  2236. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2237. struct drm_gem_object *obj;
  2238. struct amdgpu_bo *aobj;
  2239. int ret;
  2240. if (!handle) {
  2241. /* turn off cursor */
  2242. dce_v11_0_hide_cursor(crtc);
  2243. obj = NULL;
  2244. goto unpin;
  2245. }
  2246. if ((width > amdgpu_crtc->max_cursor_width) ||
  2247. (height > amdgpu_crtc->max_cursor_height)) {
  2248. DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
  2249. return -EINVAL;
  2250. }
  2251. obj = drm_gem_object_lookup(file_priv, handle);
  2252. if (!obj) {
  2253. DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
  2254. return -ENOENT;
  2255. }
  2256. aobj = gem_to_amdgpu_bo(obj);
  2257. ret = amdgpu_bo_reserve(aobj, false);
  2258. if (ret != 0) {
  2259. drm_gem_object_unreference_unlocked(obj);
  2260. return ret;
  2261. }
  2262. ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM, &amdgpu_crtc->cursor_addr);
  2263. amdgpu_bo_unreserve(aobj);
  2264. if (ret) {
  2265. DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
  2266. drm_gem_object_unreference_unlocked(obj);
  2267. return ret;
  2268. }
  2269. dce_v11_0_lock_cursor(crtc, true);
  2270. if (width != amdgpu_crtc->cursor_width ||
  2271. height != amdgpu_crtc->cursor_height ||
  2272. hot_x != amdgpu_crtc->cursor_hot_x ||
  2273. hot_y != amdgpu_crtc->cursor_hot_y) {
  2274. int x, y;
  2275. x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
  2276. y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
  2277. dce_v11_0_cursor_move_locked(crtc, x, y);
  2278. amdgpu_crtc->cursor_width = width;
  2279. amdgpu_crtc->cursor_height = height;
  2280. amdgpu_crtc->cursor_hot_x = hot_x;
  2281. amdgpu_crtc->cursor_hot_y = hot_y;
  2282. }
  2283. dce_v11_0_show_cursor(crtc);
  2284. dce_v11_0_lock_cursor(crtc, false);
  2285. unpin:
  2286. if (amdgpu_crtc->cursor_bo) {
  2287. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2288. ret = amdgpu_bo_reserve(aobj, true);
  2289. if (likely(ret == 0)) {
  2290. amdgpu_bo_unpin(aobj);
  2291. amdgpu_bo_unreserve(aobj);
  2292. }
  2293. drm_gem_object_unreference_unlocked(amdgpu_crtc->cursor_bo);
  2294. }
  2295. amdgpu_crtc->cursor_bo = obj;
  2296. return 0;
  2297. }
  2298. static void dce_v11_0_cursor_reset(struct drm_crtc *crtc)
  2299. {
  2300. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2301. if (amdgpu_crtc->cursor_bo) {
  2302. dce_v11_0_lock_cursor(crtc, true);
  2303. dce_v11_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
  2304. amdgpu_crtc->cursor_y);
  2305. dce_v11_0_show_cursor(crtc);
  2306. dce_v11_0_lock_cursor(crtc, false);
  2307. }
  2308. }
  2309. static int dce_v11_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  2310. u16 *blue, uint32_t size,
  2311. struct drm_modeset_acquire_ctx *ctx)
  2312. {
  2313. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2314. int i;
  2315. /* userspace palettes are always correct as is */
  2316. for (i = 0; i < size; i++) {
  2317. amdgpu_crtc->lut_r[i] = red[i] >> 6;
  2318. amdgpu_crtc->lut_g[i] = green[i] >> 6;
  2319. amdgpu_crtc->lut_b[i] = blue[i] >> 6;
  2320. }
  2321. dce_v11_0_crtc_load_lut(crtc);
  2322. return 0;
  2323. }
  2324. static void dce_v11_0_crtc_destroy(struct drm_crtc *crtc)
  2325. {
  2326. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2327. drm_crtc_cleanup(crtc);
  2328. kfree(amdgpu_crtc);
  2329. }
  2330. static const struct drm_crtc_funcs dce_v11_0_crtc_funcs = {
  2331. .cursor_set2 = dce_v11_0_crtc_cursor_set2,
  2332. .cursor_move = dce_v11_0_crtc_cursor_move,
  2333. .gamma_set = dce_v11_0_crtc_gamma_set,
  2334. .set_config = amdgpu_crtc_set_config,
  2335. .destroy = dce_v11_0_crtc_destroy,
  2336. .page_flip_target = amdgpu_crtc_page_flip_target,
  2337. };
  2338. static void dce_v11_0_crtc_dpms(struct drm_crtc *crtc, int mode)
  2339. {
  2340. struct drm_device *dev = crtc->dev;
  2341. struct amdgpu_device *adev = dev->dev_private;
  2342. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2343. unsigned type;
  2344. switch (mode) {
  2345. case DRM_MODE_DPMS_ON:
  2346. amdgpu_crtc->enabled = true;
  2347. amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
  2348. dce_v11_0_vga_enable(crtc, true);
  2349. amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
  2350. dce_v11_0_vga_enable(crtc, false);
  2351. /* Make sure VBLANK and PFLIP interrupts are still enabled */
  2352. type = amdgpu_crtc_idx_to_irq_type(adev, amdgpu_crtc->crtc_id);
  2353. amdgpu_irq_update(adev, &adev->crtc_irq, type);
  2354. amdgpu_irq_update(adev, &adev->pageflip_irq, type);
  2355. drm_crtc_vblank_on(crtc);
  2356. dce_v11_0_crtc_load_lut(crtc);
  2357. break;
  2358. case DRM_MODE_DPMS_STANDBY:
  2359. case DRM_MODE_DPMS_SUSPEND:
  2360. case DRM_MODE_DPMS_OFF:
  2361. drm_crtc_vblank_off(crtc);
  2362. if (amdgpu_crtc->enabled) {
  2363. dce_v11_0_vga_enable(crtc, true);
  2364. amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
  2365. dce_v11_0_vga_enable(crtc, false);
  2366. }
  2367. amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
  2368. amdgpu_crtc->enabled = false;
  2369. break;
  2370. }
  2371. /* adjust pm to dpms */
  2372. amdgpu_pm_compute_clocks(adev);
  2373. }
  2374. static void dce_v11_0_crtc_prepare(struct drm_crtc *crtc)
  2375. {
  2376. /* disable crtc pair power gating before programming */
  2377. amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
  2378. amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
  2379. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  2380. }
  2381. static void dce_v11_0_crtc_commit(struct drm_crtc *crtc)
  2382. {
  2383. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  2384. amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
  2385. }
  2386. static void dce_v11_0_crtc_disable(struct drm_crtc *crtc)
  2387. {
  2388. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2389. struct drm_device *dev = crtc->dev;
  2390. struct amdgpu_device *adev = dev->dev_private;
  2391. struct amdgpu_atom_ss ss;
  2392. int i;
  2393. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  2394. if (crtc->primary->fb) {
  2395. int r;
  2396. struct amdgpu_framebuffer *amdgpu_fb;
  2397. struct amdgpu_bo *abo;
  2398. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  2399. abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  2400. r = amdgpu_bo_reserve(abo, true);
  2401. if (unlikely(r))
  2402. DRM_ERROR("failed to reserve abo before unpin\n");
  2403. else {
  2404. amdgpu_bo_unpin(abo);
  2405. amdgpu_bo_unreserve(abo);
  2406. }
  2407. }
  2408. /* disable the GRPH */
  2409. dce_v11_0_grph_enable(crtc, false);
  2410. amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
  2411. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2412. if (adev->mode_info.crtcs[i] &&
  2413. adev->mode_info.crtcs[i]->enabled &&
  2414. i != amdgpu_crtc->crtc_id &&
  2415. amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
  2416. /* one other crtc is using this pll don't turn
  2417. * off the pll
  2418. */
  2419. goto done;
  2420. }
  2421. }
  2422. switch (amdgpu_crtc->pll_id) {
  2423. case ATOM_PPLL0:
  2424. case ATOM_PPLL1:
  2425. case ATOM_PPLL2:
  2426. /* disable the ppll */
  2427. amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
  2428. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  2429. break;
  2430. case ATOM_COMBOPHY_PLL0:
  2431. case ATOM_COMBOPHY_PLL1:
  2432. case ATOM_COMBOPHY_PLL2:
  2433. case ATOM_COMBOPHY_PLL3:
  2434. case ATOM_COMBOPHY_PLL4:
  2435. case ATOM_COMBOPHY_PLL5:
  2436. /* disable the ppll */
  2437. amdgpu_atombios_crtc_program_pll(crtc, ATOM_CRTC_INVALID, amdgpu_crtc->pll_id,
  2438. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  2439. break;
  2440. default:
  2441. break;
  2442. }
  2443. done:
  2444. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  2445. amdgpu_crtc->adjusted_clock = 0;
  2446. amdgpu_crtc->encoder = NULL;
  2447. amdgpu_crtc->connector = NULL;
  2448. }
  2449. static int dce_v11_0_crtc_mode_set(struct drm_crtc *crtc,
  2450. struct drm_display_mode *mode,
  2451. struct drm_display_mode *adjusted_mode,
  2452. int x, int y, struct drm_framebuffer *old_fb)
  2453. {
  2454. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2455. struct drm_device *dev = crtc->dev;
  2456. struct amdgpu_device *adev = dev->dev_private;
  2457. if (!amdgpu_crtc->adjusted_clock)
  2458. return -EINVAL;
  2459. if ((adev->asic_type == CHIP_POLARIS10) ||
  2460. (adev->asic_type == CHIP_POLARIS11) ||
  2461. (adev->asic_type == CHIP_POLARIS12)) {
  2462. struct amdgpu_encoder *amdgpu_encoder =
  2463. to_amdgpu_encoder(amdgpu_crtc->encoder);
  2464. int encoder_mode =
  2465. amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder);
  2466. /* SetPixelClock calculates the plls and ss values now */
  2467. amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id,
  2468. amdgpu_crtc->pll_id,
  2469. encoder_mode, amdgpu_encoder->encoder_id,
  2470. adjusted_mode->clock, 0, 0, 0, 0,
  2471. amdgpu_crtc->bpc, amdgpu_crtc->ss_enabled, &amdgpu_crtc->ss);
  2472. } else {
  2473. amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
  2474. }
  2475. amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
  2476. dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  2477. amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
  2478. amdgpu_atombios_crtc_scaler_setup(crtc);
  2479. dce_v11_0_cursor_reset(crtc);
  2480. /* update the hw version fpr dpm */
  2481. amdgpu_crtc->hw_mode = *adjusted_mode;
  2482. return 0;
  2483. }
  2484. static bool dce_v11_0_crtc_mode_fixup(struct drm_crtc *crtc,
  2485. const struct drm_display_mode *mode,
  2486. struct drm_display_mode *adjusted_mode)
  2487. {
  2488. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2489. struct drm_device *dev = crtc->dev;
  2490. struct drm_encoder *encoder;
  2491. /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
  2492. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  2493. if (encoder->crtc == crtc) {
  2494. amdgpu_crtc->encoder = encoder;
  2495. amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
  2496. break;
  2497. }
  2498. }
  2499. if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
  2500. amdgpu_crtc->encoder = NULL;
  2501. amdgpu_crtc->connector = NULL;
  2502. return false;
  2503. }
  2504. if (!amdgpu_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
  2505. return false;
  2506. if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
  2507. return false;
  2508. /* pick pll */
  2509. amdgpu_crtc->pll_id = dce_v11_0_pick_pll(crtc);
  2510. /* if we can't get a PPLL for a non-DP encoder, fail */
  2511. if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
  2512. !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
  2513. return false;
  2514. return true;
  2515. }
  2516. static int dce_v11_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  2517. struct drm_framebuffer *old_fb)
  2518. {
  2519. return dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  2520. }
  2521. static int dce_v11_0_crtc_set_base_atomic(struct drm_crtc *crtc,
  2522. struct drm_framebuffer *fb,
  2523. int x, int y, enum mode_set_atomic state)
  2524. {
  2525. return dce_v11_0_crtc_do_set_base(crtc, fb, x, y, 1);
  2526. }
  2527. static const struct drm_crtc_helper_funcs dce_v11_0_crtc_helper_funcs = {
  2528. .dpms = dce_v11_0_crtc_dpms,
  2529. .mode_fixup = dce_v11_0_crtc_mode_fixup,
  2530. .mode_set = dce_v11_0_crtc_mode_set,
  2531. .mode_set_base = dce_v11_0_crtc_set_base,
  2532. .mode_set_base_atomic = dce_v11_0_crtc_set_base_atomic,
  2533. .prepare = dce_v11_0_crtc_prepare,
  2534. .commit = dce_v11_0_crtc_commit,
  2535. .load_lut = dce_v11_0_crtc_load_lut,
  2536. .disable = dce_v11_0_crtc_disable,
  2537. };
  2538. static int dce_v11_0_crtc_init(struct amdgpu_device *adev, int index)
  2539. {
  2540. struct amdgpu_crtc *amdgpu_crtc;
  2541. int i;
  2542. amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
  2543. (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  2544. if (amdgpu_crtc == NULL)
  2545. return -ENOMEM;
  2546. drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_v11_0_crtc_funcs);
  2547. drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
  2548. amdgpu_crtc->crtc_id = index;
  2549. adev->mode_info.crtcs[index] = amdgpu_crtc;
  2550. amdgpu_crtc->max_cursor_width = 128;
  2551. amdgpu_crtc->max_cursor_height = 128;
  2552. adev->ddev->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
  2553. adev->ddev->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
  2554. for (i = 0; i < 256; i++) {
  2555. amdgpu_crtc->lut_r[i] = i << 2;
  2556. amdgpu_crtc->lut_g[i] = i << 2;
  2557. amdgpu_crtc->lut_b[i] = i << 2;
  2558. }
  2559. switch (amdgpu_crtc->crtc_id) {
  2560. case 0:
  2561. default:
  2562. amdgpu_crtc->crtc_offset = CRTC0_REGISTER_OFFSET;
  2563. break;
  2564. case 1:
  2565. amdgpu_crtc->crtc_offset = CRTC1_REGISTER_OFFSET;
  2566. break;
  2567. case 2:
  2568. amdgpu_crtc->crtc_offset = CRTC2_REGISTER_OFFSET;
  2569. break;
  2570. case 3:
  2571. amdgpu_crtc->crtc_offset = CRTC3_REGISTER_OFFSET;
  2572. break;
  2573. case 4:
  2574. amdgpu_crtc->crtc_offset = CRTC4_REGISTER_OFFSET;
  2575. break;
  2576. case 5:
  2577. amdgpu_crtc->crtc_offset = CRTC5_REGISTER_OFFSET;
  2578. break;
  2579. }
  2580. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  2581. amdgpu_crtc->adjusted_clock = 0;
  2582. amdgpu_crtc->encoder = NULL;
  2583. amdgpu_crtc->connector = NULL;
  2584. drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v11_0_crtc_helper_funcs);
  2585. return 0;
  2586. }
  2587. static int dce_v11_0_early_init(void *handle)
  2588. {
  2589. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2590. adev->audio_endpt_rreg = &dce_v11_0_audio_endpt_rreg;
  2591. adev->audio_endpt_wreg = &dce_v11_0_audio_endpt_wreg;
  2592. dce_v11_0_set_display_funcs(adev);
  2593. dce_v11_0_set_irq_funcs(adev);
  2594. adev->mode_info.num_crtc = dce_v11_0_get_num_crtc(adev);
  2595. switch (adev->asic_type) {
  2596. case CHIP_CARRIZO:
  2597. adev->mode_info.num_hpd = 6;
  2598. adev->mode_info.num_dig = 9;
  2599. break;
  2600. case CHIP_STONEY:
  2601. adev->mode_info.num_hpd = 6;
  2602. adev->mode_info.num_dig = 9;
  2603. break;
  2604. case CHIP_POLARIS10:
  2605. adev->mode_info.num_hpd = 6;
  2606. adev->mode_info.num_dig = 6;
  2607. break;
  2608. case CHIP_POLARIS11:
  2609. case CHIP_POLARIS12:
  2610. adev->mode_info.num_hpd = 5;
  2611. adev->mode_info.num_dig = 5;
  2612. break;
  2613. default:
  2614. /* FIXME: not supported yet */
  2615. return -EINVAL;
  2616. }
  2617. return 0;
  2618. }
  2619. static int dce_v11_0_sw_init(void *handle)
  2620. {
  2621. int r, i;
  2622. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2623. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2624. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, i + 1, &adev->crtc_irq);
  2625. if (r)
  2626. return r;
  2627. }
  2628. for (i = 8; i < 20; i += 2) {
  2629. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, i, &adev->pageflip_irq);
  2630. if (r)
  2631. return r;
  2632. }
  2633. /* HPD hotplug */
  2634. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 42, &adev->hpd_irq);
  2635. if (r)
  2636. return r;
  2637. adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
  2638. adev->ddev->mode_config.async_page_flip = true;
  2639. adev->ddev->mode_config.max_width = 16384;
  2640. adev->ddev->mode_config.max_height = 16384;
  2641. adev->ddev->mode_config.preferred_depth = 24;
  2642. adev->ddev->mode_config.prefer_shadow = 1;
  2643. adev->ddev->mode_config.fb_base = adev->mc.aper_base;
  2644. r = amdgpu_modeset_create_props(adev);
  2645. if (r)
  2646. return r;
  2647. adev->ddev->mode_config.max_width = 16384;
  2648. adev->ddev->mode_config.max_height = 16384;
  2649. /* allocate crtcs */
  2650. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2651. r = dce_v11_0_crtc_init(adev, i);
  2652. if (r)
  2653. return r;
  2654. }
  2655. if (amdgpu_atombios_get_connector_info_from_object_table(adev))
  2656. amdgpu_print_display_setup(adev->ddev);
  2657. else
  2658. return -EINVAL;
  2659. /* setup afmt */
  2660. r = dce_v11_0_afmt_init(adev);
  2661. if (r)
  2662. return r;
  2663. r = dce_v11_0_audio_init(adev);
  2664. if (r)
  2665. return r;
  2666. drm_kms_helper_poll_init(adev->ddev);
  2667. adev->mode_info.mode_config_initialized = true;
  2668. return 0;
  2669. }
  2670. static int dce_v11_0_sw_fini(void *handle)
  2671. {
  2672. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2673. kfree(adev->mode_info.bios_hardcoded_edid);
  2674. drm_kms_helper_poll_fini(adev->ddev);
  2675. dce_v11_0_audio_fini(adev);
  2676. dce_v11_0_afmt_fini(adev);
  2677. drm_mode_config_cleanup(adev->ddev);
  2678. adev->mode_info.mode_config_initialized = false;
  2679. return 0;
  2680. }
  2681. static int dce_v11_0_hw_init(void *handle)
  2682. {
  2683. int i;
  2684. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2685. dce_v11_0_init_golden_registers(adev);
  2686. /* init dig PHYs, disp eng pll */
  2687. amdgpu_atombios_crtc_powergate_init(adev);
  2688. amdgpu_atombios_encoder_init_dig(adev);
  2689. if ((adev->asic_type == CHIP_POLARIS10) ||
  2690. (adev->asic_type == CHIP_POLARIS11) ||
  2691. (adev->asic_type == CHIP_POLARIS12)) {
  2692. amdgpu_atombios_crtc_set_dce_clock(adev, adev->clock.default_dispclk,
  2693. DCE_CLOCK_TYPE_DISPCLK, ATOM_GCK_DFS);
  2694. amdgpu_atombios_crtc_set_dce_clock(adev, 0,
  2695. DCE_CLOCK_TYPE_DPREFCLK, ATOM_GCK_DFS);
  2696. } else {
  2697. amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
  2698. }
  2699. /* initialize hpd */
  2700. dce_v11_0_hpd_init(adev);
  2701. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2702. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2703. }
  2704. dce_v11_0_pageflip_interrupt_init(adev);
  2705. return 0;
  2706. }
  2707. static int dce_v11_0_hw_fini(void *handle)
  2708. {
  2709. int i;
  2710. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2711. dce_v11_0_hpd_fini(adev);
  2712. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2713. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2714. }
  2715. dce_v11_0_pageflip_interrupt_fini(adev);
  2716. return 0;
  2717. }
  2718. static int dce_v11_0_suspend(void *handle)
  2719. {
  2720. return dce_v11_0_hw_fini(handle);
  2721. }
  2722. static int dce_v11_0_resume(void *handle)
  2723. {
  2724. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2725. int ret;
  2726. ret = dce_v11_0_hw_init(handle);
  2727. /* turn on the BL */
  2728. if (adev->mode_info.bl_encoder) {
  2729. u8 bl_level = amdgpu_display_backlight_get_level(adev,
  2730. adev->mode_info.bl_encoder);
  2731. amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
  2732. bl_level);
  2733. }
  2734. return ret;
  2735. }
  2736. static bool dce_v11_0_is_idle(void *handle)
  2737. {
  2738. return true;
  2739. }
  2740. static int dce_v11_0_wait_for_idle(void *handle)
  2741. {
  2742. return 0;
  2743. }
  2744. static int dce_v11_0_soft_reset(void *handle)
  2745. {
  2746. u32 srbm_soft_reset = 0, tmp;
  2747. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2748. if (dce_v11_0_is_display_hung(adev))
  2749. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
  2750. if (srbm_soft_reset) {
  2751. tmp = RREG32(mmSRBM_SOFT_RESET);
  2752. tmp |= srbm_soft_reset;
  2753. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  2754. WREG32(mmSRBM_SOFT_RESET, tmp);
  2755. tmp = RREG32(mmSRBM_SOFT_RESET);
  2756. udelay(50);
  2757. tmp &= ~srbm_soft_reset;
  2758. WREG32(mmSRBM_SOFT_RESET, tmp);
  2759. tmp = RREG32(mmSRBM_SOFT_RESET);
  2760. /* Wait a little for things to settle down */
  2761. udelay(50);
  2762. }
  2763. return 0;
  2764. }
  2765. static void dce_v11_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
  2766. int crtc,
  2767. enum amdgpu_interrupt_state state)
  2768. {
  2769. u32 lb_interrupt_mask;
  2770. if (crtc >= adev->mode_info.num_crtc) {
  2771. DRM_DEBUG("invalid crtc %d\n", crtc);
  2772. return;
  2773. }
  2774. switch (state) {
  2775. case AMDGPU_IRQ_STATE_DISABLE:
  2776. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2777. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2778. VBLANK_INTERRUPT_MASK, 0);
  2779. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2780. break;
  2781. case AMDGPU_IRQ_STATE_ENABLE:
  2782. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2783. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2784. VBLANK_INTERRUPT_MASK, 1);
  2785. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2786. break;
  2787. default:
  2788. break;
  2789. }
  2790. }
  2791. static void dce_v11_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
  2792. int crtc,
  2793. enum amdgpu_interrupt_state state)
  2794. {
  2795. u32 lb_interrupt_mask;
  2796. if (crtc >= adev->mode_info.num_crtc) {
  2797. DRM_DEBUG("invalid crtc %d\n", crtc);
  2798. return;
  2799. }
  2800. switch (state) {
  2801. case AMDGPU_IRQ_STATE_DISABLE:
  2802. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2803. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2804. VLINE_INTERRUPT_MASK, 0);
  2805. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2806. break;
  2807. case AMDGPU_IRQ_STATE_ENABLE:
  2808. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2809. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2810. VLINE_INTERRUPT_MASK, 1);
  2811. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2812. break;
  2813. default:
  2814. break;
  2815. }
  2816. }
  2817. static int dce_v11_0_set_hpd_irq_state(struct amdgpu_device *adev,
  2818. struct amdgpu_irq_src *source,
  2819. unsigned hpd,
  2820. enum amdgpu_interrupt_state state)
  2821. {
  2822. u32 tmp;
  2823. if (hpd >= adev->mode_info.num_hpd) {
  2824. DRM_DEBUG("invalid hdp %d\n", hpd);
  2825. return 0;
  2826. }
  2827. switch (state) {
  2828. case AMDGPU_IRQ_STATE_DISABLE:
  2829. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2830. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
  2831. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2832. break;
  2833. case AMDGPU_IRQ_STATE_ENABLE:
  2834. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2835. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 1);
  2836. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2837. break;
  2838. default:
  2839. break;
  2840. }
  2841. return 0;
  2842. }
  2843. static int dce_v11_0_set_crtc_irq_state(struct amdgpu_device *adev,
  2844. struct amdgpu_irq_src *source,
  2845. unsigned type,
  2846. enum amdgpu_interrupt_state state)
  2847. {
  2848. switch (type) {
  2849. case AMDGPU_CRTC_IRQ_VBLANK1:
  2850. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 0, state);
  2851. break;
  2852. case AMDGPU_CRTC_IRQ_VBLANK2:
  2853. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 1, state);
  2854. break;
  2855. case AMDGPU_CRTC_IRQ_VBLANK3:
  2856. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 2, state);
  2857. break;
  2858. case AMDGPU_CRTC_IRQ_VBLANK4:
  2859. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 3, state);
  2860. break;
  2861. case AMDGPU_CRTC_IRQ_VBLANK5:
  2862. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 4, state);
  2863. break;
  2864. case AMDGPU_CRTC_IRQ_VBLANK6:
  2865. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 5, state);
  2866. break;
  2867. case AMDGPU_CRTC_IRQ_VLINE1:
  2868. dce_v11_0_set_crtc_vline_interrupt_state(adev, 0, state);
  2869. break;
  2870. case AMDGPU_CRTC_IRQ_VLINE2:
  2871. dce_v11_0_set_crtc_vline_interrupt_state(adev, 1, state);
  2872. break;
  2873. case AMDGPU_CRTC_IRQ_VLINE3:
  2874. dce_v11_0_set_crtc_vline_interrupt_state(adev, 2, state);
  2875. break;
  2876. case AMDGPU_CRTC_IRQ_VLINE4:
  2877. dce_v11_0_set_crtc_vline_interrupt_state(adev, 3, state);
  2878. break;
  2879. case AMDGPU_CRTC_IRQ_VLINE5:
  2880. dce_v11_0_set_crtc_vline_interrupt_state(adev, 4, state);
  2881. break;
  2882. case AMDGPU_CRTC_IRQ_VLINE6:
  2883. dce_v11_0_set_crtc_vline_interrupt_state(adev, 5, state);
  2884. break;
  2885. default:
  2886. break;
  2887. }
  2888. return 0;
  2889. }
  2890. static int dce_v11_0_set_pageflip_irq_state(struct amdgpu_device *adev,
  2891. struct amdgpu_irq_src *src,
  2892. unsigned type,
  2893. enum amdgpu_interrupt_state state)
  2894. {
  2895. u32 reg;
  2896. if (type >= adev->mode_info.num_crtc) {
  2897. DRM_ERROR("invalid pageflip crtc %d\n", type);
  2898. return -EINVAL;
  2899. }
  2900. reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
  2901. if (state == AMDGPU_IRQ_STATE_DISABLE)
  2902. WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
  2903. reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2904. else
  2905. WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
  2906. reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2907. return 0;
  2908. }
  2909. static int dce_v11_0_pageflip_irq(struct amdgpu_device *adev,
  2910. struct amdgpu_irq_src *source,
  2911. struct amdgpu_iv_entry *entry)
  2912. {
  2913. unsigned long flags;
  2914. unsigned crtc_id;
  2915. struct amdgpu_crtc *amdgpu_crtc;
  2916. struct amdgpu_flip_work *works;
  2917. crtc_id = (entry->src_id - 8) >> 1;
  2918. amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  2919. if (crtc_id >= adev->mode_info.num_crtc) {
  2920. DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
  2921. return -EINVAL;
  2922. }
  2923. if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
  2924. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
  2925. WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
  2926. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
  2927. /* IRQ could occur when in initial stage */
  2928. if(amdgpu_crtc == NULL)
  2929. return 0;
  2930. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  2931. works = amdgpu_crtc->pflip_works;
  2932. if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
  2933. DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
  2934. "AMDGPU_FLIP_SUBMITTED(%d)\n",
  2935. amdgpu_crtc->pflip_status,
  2936. AMDGPU_FLIP_SUBMITTED);
  2937. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2938. return 0;
  2939. }
  2940. /* page flip completed. clean up */
  2941. amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
  2942. amdgpu_crtc->pflip_works = NULL;
  2943. /* wakeup usersapce */
  2944. if(works->event)
  2945. drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
  2946. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2947. drm_crtc_vblank_put(&amdgpu_crtc->base);
  2948. schedule_work(&works->unpin_work);
  2949. return 0;
  2950. }
  2951. static void dce_v11_0_hpd_int_ack(struct amdgpu_device *adev,
  2952. int hpd)
  2953. {
  2954. u32 tmp;
  2955. if (hpd >= adev->mode_info.num_hpd) {
  2956. DRM_DEBUG("invalid hdp %d\n", hpd);
  2957. return;
  2958. }
  2959. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2960. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_ACK, 1);
  2961. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2962. }
  2963. static void dce_v11_0_crtc_vblank_int_ack(struct amdgpu_device *adev,
  2964. int crtc)
  2965. {
  2966. u32 tmp;
  2967. if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
  2968. DRM_DEBUG("invalid crtc %d\n", crtc);
  2969. return;
  2970. }
  2971. tmp = RREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc]);
  2972. tmp = REG_SET_FIELD(tmp, LB_VBLANK_STATUS, VBLANK_ACK, 1);
  2973. WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], tmp);
  2974. }
  2975. static void dce_v11_0_crtc_vline_int_ack(struct amdgpu_device *adev,
  2976. int crtc)
  2977. {
  2978. u32 tmp;
  2979. if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
  2980. DRM_DEBUG("invalid crtc %d\n", crtc);
  2981. return;
  2982. }
  2983. tmp = RREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc]);
  2984. tmp = REG_SET_FIELD(tmp, LB_VLINE_STATUS, VLINE_ACK, 1);
  2985. WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], tmp);
  2986. }
  2987. static int dce_v11_0_crtc_irq(struct amdgpu_device *adev,
  2988. struct amdgpu_irq_src *source,
  2989. struct amdgpu_iv_entry *entry)
  2990. {
  2991. unsigned crtc = entry->src_id - 1;
  2992. uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
  2993. unsigned irq_type = amdgpu_crtc_idx_to_irq_type(adev, crtc);
  2994. switch (entry->src_data[0]) {
  2995. case 0: /* vblank */
  2996. if (disp_int & interrupt_status_offsets[crtc].vblank)
  2997. dce_v11_0_crtc_vblank_int_ack(adev, crtc);
  2998. else
  2999. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  3000. if (amdgpu_irq_enabled(adev, source, irq_type)) {
  3001. drm_handle_vblank(adev->ddev, crtc);
  3002. }
  3003. DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
  3004. break;
  3005. case 1: /* vline */
  3006. if (disp_int & interrupt_status_offsets[crtc].vline)
  3007. dce_v11_0_crtc_vline_int_ack(adev, crtc);
  3008. else
  3009. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  3010. DRM_DEBUG("IH: D%d vline\n", crtc + 1);
  3011. break;
  3012. default:
  3013. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
  3014. break;
  3015. }
  3016. return 0;
  3017. }
  3018. static int dce_v11_0_hpd_irq(struct amdgpu_device *adev,
  3019. struct amdgpu_irq_src *source,
  3020. struct amdgpu_iv_entry *entry)
  3021. {
  3022. uint32_t disp_int, mask;
  3023. unsigned hpd;
  3024. if (entry->src_data[0] >= adev->mode_info.num_hpd) {
  3025. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
  3026. return 0;
  3027. }
  3028. hpd = entry->src_data[0];
  3029. disp_int = RREG32(interrupt_status_offsets[hpd].reg);
  3030. mask = interrupt_status_offsets[hpd].hpd;
  3031. if (disp_int & mask) {
  3032. dce_v11_0_hpd_int_ack(adev, hpd);
  3033. schedule_work(&adev->hotplug_work);
  3034. DRM_DEBUG("IH: HPD%d\n", hpd + 1);
  3035. }
  3036. return 0;
  3037. }
  3038. static int dce_v11_0_set_clockgating_state(void *handle,
  3039. enum amd_clockgating_state state)
  3040. {
  3041. return 0;
  3042. }
  3043. static int dce_v11_0_set_powergating_state(void *handle,
  3044. enum amd_powergating_state state)
  3045. {
  3046. return 0;
  3047. }
  3048. static const struct amd_ip_funcs dce_v11_0_ip_funcs = {
  3049. .name = "dce_v11_0",
  3050. .early_init = dce_v11_0_early_init,
  3051. .late_init = NULL,
  3052. .sw_init = dce_v11_0_sw_init,
  3053. .sw_fini = dce_v11_0_sw_fini,
  3054. .hw_init = dce_v11_0_hw_init,
  3055. .hw_fini = dce_v11_0_hw_fini,
  3056. .suspend = dce_v11_0_suspend,
  3057. .resume = dce_v11_0_resume,
  3058. .is_idle = dce_v11_0_is_idle,
  3059. .wait_for_idle = dce_v11_0_wait_for_idle,
  3060. .soft_reset = dce_v11_0_soft_reset,
  3061. .set_clockgating_state = dce_v11_0_set_clockgating_state,
  3062. .set_powergating_state = dce_v11_0_set_powergating_state,
  3063. };
  3064. static void
  3065. dce_v11_0_encoder_mode_set(struct drm_encoder *encoder,
  3066. struct drm_display_mode *mode,
  3067. struct drm_display_mode *adjusted_mode)
  3068. {
  3069. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3070. amdgpu_encoder->pixel_clock = adjusted_mode->clock;
  3071. /* need to call this here rather than in prepare() since we need some crtc info */
  3072. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  3073. /* set scaler clears this on some chips */
  3074. dce_v11_0_set_interleave(encoder->crtc, mode);
  3075. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
  3076. dce_v11_0_afmt_enable(encoder, true);
  3077. dce_v11_0_afmt_setmode(encoder, adjusted_mode);
  3078. }
  3079. }
  3080. static void dce_v11_0_encoder_prepare(struct drm_encoder *encoder)
  3081. {
  3082. struct amdgpu_device *adev = encoder->dev->dev_private;
  3083. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3084. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  3085. if ((amdgpu_encoder->active_device &
  3086. (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
  3087. (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
  3088. ENCODER_OBJECT_ID_NONE)) {
  3089. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  3090. if (dig) {
  3091. dig->dig_encoder = dce_v11_0_pick_dig_encoder(encoder);
  3092. if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
  3093. dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
  3094. }
  3095. }
  3096. amdgpu_atombios_scratch_regs_lock(adev, true);
  3097. if (connector) {
  3098. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  3099. /* select the clock/data port if it uses a router */
  3100. if (amdgpu_connector->router.cd_valid)
  3101. amdgpu_i2c_router_select_cd_port(amdgpu_connector);
  3102. /* turn eDP panel on for mode set */
  3103. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  3104. amdgpu_atombios_encoder_set_edp_panel_power(connector,
  3105. ATOM_TRANSMITTER_ACTION_POWER_ON);
  3106. }
  3107. /* this is needed for the pll/ss setup to work correctly in some cases */
  3108. amdgpu_atombios_encoder_set_crtc_source(encoder);
  3109. /* set up the FMT blocks */
  3110. dce_v11_0_program_fmt(encoder);
  3111. }
  3112. static void dce_v11_0_encoder_commit(struct drm_encoder *encoder)
  3113. {
  3114. struct drm_device *dev = encoder->dev;
  3115. struct amdgpu_device *adev = dev->dev_private;
  3116. /* need to call this here as we need the crtc set up */
  3117. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  3118. amdgpu_atombios_scratch_regs_lock(adev, false);
  3119. }
  3120. static void dce_v11_0_encoder_disable(struct drm_encoder *encoder)
  3121. {
  3122. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3123. struct amdgpu_encoder_atom_dig *dig;
  3124. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  3125. if (amdgpu_atombios_encoder_is_digital(encoder)) {
  3126. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
  3127. dce_v11_0_afmt_enable(encoder, false);
  3128. dig = amdgpu_encoder->enc_priv;
  3129. dig->dig_encoder = -1;
  3130. }
  3131. amdgpu_encoder->active_device = 0;
  3132. }
  3133. /* these are handled by the primary encoders */
  3134. static void dce_v11_0_ext_prepare(struct drm_encoder *encoder)
  3135. {
  3136. }
  3137. static void dce_v11_0_ext_commit(struct drm_encoder *encoder)
  3138. {
  3139. }
  3140. static void
  3141. dce_v11_0_ext_mode_set(struct drm_encoder *encoder,
  3142. struct drm_display_mode *mode,
  3143. struct drm_display_mode *adjusted_mode)
  3144. {
  3145. }
  3146. static void dce_v11_0_ext_disable(struct drm_encoder *encoder)
  3147. {
  3148. }
  3149. static void
  3150. dce_v11_0_ext_dpms(struct drm_encoder *encoder, int mode)
  3151. {
  3152. }
  3153. static const struct drm_encoder_helper_funcs dce_v11_0_ext_helper_funcs = {
  3154. .dpms = dce_v11_0_ext_dpms,
  3155. .prepare = dce_v11_0_ext_prepare,
  3156. .mode_set = dce_v11_0_ext_mode_set,
  3157. .commit = dce_v11_0_ext_commit,
  3158. .disable = dce_v11_0_ext_disable,
  3159. /* no detect for TMDS/LVDS yet */
  3160. };
  3161. static const struct drm_encoder_helper_funcs dce_v11_0_dig_helper_funcs = {
  3162. .dpms = amdgpu_atombios_encoder_dpms,
  3163. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  3164. .prepare = dce_v11_0_encoder_prepare,
  3165. .mode_set = dce_v11_0_encoder_mode_set,
  3166. .commit = dce_v11_0_encoder_commit,
  3167. .disable = dce_v11_0_encoder_disable,
  3168. .detect = amdgpu_atombios_encoder_dig_detect,
  3169. };
  3170. static const struct drm_encoder_helper_funcs dce_v11_0_dac_helper_funcs = {
  3171. .dpms = amdgpu_atombios_encoder_dpms,
  3172. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  3173. .prepare = dce_v11_0_encoder_prepare,
  3174. .mode_set = dce_v11_0_encoder_mode_set,
  3175. .commit = dce_v11_0_encoder_commit,
  3176. .detect = amdgpu_atombios_encoder_dac_detect,
  3177. };
  3178. static void dce_v11_0_encoder_destroy(struct drm_encoder *encoder)
  3179. {
  3180. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3181. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  3182. amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
  3183. kfree(amdgpu_encoder->enc_priv);
  3184. drm_encoder_cleanup(encoder);
  3185. kfree(amdgpu_encoder);
  3186. }
  3187. static const struct drm_encoder_funcs dce_v11_0_encoder_funcs = {
  3188. .destroy = dce_v11_0_encoder_destroy,
  3189. };
  3190. static void dce_v11_0_encoder_add(struct amdgpu_device *adev,
  3191. uint32_t encoder_enum,
  3192. uint32_t supported_device,
  3193. u16 caps)
  3194. {
  3195. struct drm_device *dev = adev->ddev;
  3196. struct drm_encoder *encoder;
  3197. struct amdgpu_encoder *amdgpu_encoder;
  3198. /* see if we already added it */
  3199. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  3200. amdgpu_encoder = to_amdgpu_encoder(encoder);
  3201. if (amdgpu_encoder->encoder_enum == encoder_enum) {
  3202. amdgpu_encoder->devices |= supported_device;
  3203. return;
  3204. }
  3205. }
  3206. /* add a new one */
  3207. amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
  3208. if (!amdgpu_encoder)
  3209. return;
  3210. encoder = &amdgpu_encoder->base;
  3211. switch (adev->mode_info.num_crtc) {
  3212. case 1:
  3213. encoder->possible_crtcs = 0x1;
  3214. break;
  3215. case 2:
  3216. default:
  3217. encoder->possible_crtcs = 0x3;
  3218. break;
  3219. case 3:
  3220. encoder->possible_crtcs = 0x7;
  3221. break;
  3222. case 4:
  3223. encoder->possible_crtcs = 0xf;
  3224. break;
  3225. case 5:
  3226. encoder->possible_crtcs = 0x1f;
  3227. break;
  3228. case 6:
  3229. encoder->possible_crtcs = 0x3f;
  3230. break;
  3231. }
  3232. amdgpu_encoder->enc_priv = NULL;
  3233. amdgpu_encoder->encoder_enum = encoder_enum;
  3234. amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
  3235. amdgpu_encoder->devices = supported_device;
  3236. amdgpu_encoder->rmx_type = RMX_OFF;
  3237. amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
  3238. amdgpu_encoder->is_ext_encoder = false;
  3239. amdgpu_encoder->caps = caps;
  3240. switch (amdgpu_encoder->encoder_id) {
  3241. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
  3242. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
  3243. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3244. DRM_MODE_ENCODER_DAC, NULL);
  3245. drm_encoder_helper_add(encoder, &dce_v11_0_dac_helper_funcs);
  3246. break;
  3247. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
  3248. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  3249. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  3250. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  3251. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
  3252. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
  3253. amdgpu_encoder->rmx_type = RMX_FULL;
  3254. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3255. DRM_MODE_ENCODER_LVDS, NULL);
  3256. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
  3257. } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
  3258. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3259. DRM_MODE_ENCODER_DAC, NULL);
  3260. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  3261. } else {
  3262. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3263. DRM_MODE_ENCODER_TMDS, NULL);
  3264. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  3265. }
  3266. drm_encoder_helper_add(encoder, &dce_v11_0_dig_helper_funcs);
  3267. break;
  3268. case ENCODER_OBJECT_ID_SI170B:
  3269. case ENCODER_OBJECT_ID_CH7303:
  3270. case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
  3271. case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
  3272. case ENCODER_OBJECT_ID_TITFP513:
  3273. case ENCODER_OBJECT_ID_VT1623:
  3274. case ENCODER_OBJECT_ID_HDMI_SI1930:
  3275. case ENCODER_OBJECT_ID_TRAVIS:
  3276. case ENCODER_OBJECT_ID_NUTMEG:
  3277. /* these are handled by the primary encoders */
  3278. amdgpu_encoder->is_ext_encoder = true;
  3279. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  3280. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3281. DRM_MODE_ENCODER_LVDS, NULL);
  3282. else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
  3283. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3284. DRM_MODE_ENCODER_DAC, NULL);
  3285. else
  3286. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3287. DRM_MODE_ENCODER_TMDS, NULL);
  3288. drm_encoder_helper_add(encoder, &dce_v11_0_ext_helper_funcs);
  3289. break;
  3290. }
  3291. }
  3292. static const struct amdgpu_display_funcs dce_v11_0_display_funcs = {
  3293. .set_vga_render_state = &dce_v11_0_set_vga_render_state,
  3294. .bandwidth_update = &dce_v11_0_bandwidth_update,
  3295. .vblank_get_counter = &dce_v11_0_vblank_get_counter,
  3296. .vblank_wait = &dce_v11_0_vblank_wait,
  3297. .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
  3298. .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
  3299. .hpd_sense = &dce_v11_0_hpd_sense,
  3300. .hpd_set_polarity = &dce_v11_0_hpd_set_polarity,
  3301. .hpd_get_gpio_reg = &dce_v11_0_hpd_get_gpio_reg,
  3302. .page_flip = &dce_v11_0_page_flip,
  3303. .page_flip_get_scanoutpos = &dce_v11_0_crtc_get_scanoutpos,
  3304. .add_encoder = &dce_v11_0_encoder_add,
  3305. .add_connector = &amdgpu_connector_add,
  3306. .stop_mc_access = &dce_v11_0_stop_mc_access,
  3307. .resume_mc_access = &dce_v11_0_resume_mc_access,
  3308. };
  3309. static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev)
  3310. {
  3311. if (adev->mode_info.funcs == NULL)
  3312. adev->mode_info.funcs = &dce_v11_0_display_funcs;
  3313. }
  3314. static const struct amdgpu_irq_src_funcs dce_v11_0_crtc_irq_funcs = {
  3315. .set = dce_v11_0_set_crtc_irq_state,
  3316. .process = dce_v11_0_crtc_irq,
  3317. };
  3318. static const struct amdgpu_irq_src_funcs dce_v11_0_pageflip_irq_funcs = {
  3319. .set = dce_v11_0_set_pageflip_irq_state,
  3320. .process = dce_v11_0_pageflip_irq,
  3321. };
  3322. static const struct amdgpu_irq_src_funcs dce_v11_0_hpd_irq_funcs = {
  3323. .set = dce_v11_0_set_hpd_irq_state,
  3324. .process = dce_v11_0_hpd_irq,
  3325. };
  3326. static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev)
  3327. {
  3328. adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_LAST;
  3329. adev->crtc_irq.funcs = &dce_v11_0_crtc_irq_funcs;
  3330. adev->pageflip_irq.num_types = AMDGPU_PAGEFLIP_IRQ_LAST;
  3331. adev->pageflip_irq.funcs = &dce_v11_0_pageflip_irq_funcs;
  3332. adev->hpd_irq.num_types = AMDGPU_HPD_LAST;
  3333. adev->hpd_irq.funcs = &dce_v11_0_hpd_irq_funcs;
  3334. }
  3335. const struct amdgpu_ip_block_version dce_v11_0_ip_block =
  3336. {
  3337. .type = AMD_IP_BLOCK_TYPE_DCE,
  3338. .major = 11,
  3339. .minor = 0,
  3340. .rev = 0,
  3341. .funcs = &dce_v11_0_ip_funcs,
  3342. };
  3343. const struct amdgpu_ip_block_version dce_v11_2_ip_block =
  3344. {
  3345. .type = AMD_IP_BLOCK_TYPE_DCE,
  3346. .major = 11,
  3347. .minor = 2,
  3348. .rev = 0,
  3349. .funcs = &dce_v11_0_ip_funcs,
  3350. };