dce_v6_0.c 105 KB

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