dce_v11_0.c 116 KB

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