dce_v6_0.c 108 KB

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