dce_v8_0.c 108 KB

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