dce_v8_0.c 106 KB

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