dce_v8_0.c 107 KB

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