dce_v8_0.c 111 KB

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