dce_v8_0.c 110 KB

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