dce_v6_0.c 109 KB

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