dce_v8_0.c 106 KB

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