dce_v8_0.c 107 KB

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