dce_v8_0.c 110 KB

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