dce_v11_0.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894
  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 "vid.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/dce_11_0_d.h"
  35. #include "dce/dce_11_0_sh_mask.h"
  36. #include "dce/dce_11_0_enum.h"
  37. #include "oss/oss_3_0_d.h"
  38. #include "oss/oss_3_0_sh_mask.h"
  39. #include "gmc/gmc_8_1_d.h"
  40. #include "gmc/gmc_8_1_sh_mask.h"
  41. static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev);
  42. static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev);
  43. static const u32 crtc_offsets[] =
  44. {
  45. CRTC0_REGISTER_OFFSET,
  46. CRTC1_REGISTER_OFFSET,
  47. CRTC2_REGISTER_OFFSET,
  48. CRTC3_REGISTER_OFFSET,
  49. CRTC4_REGISTER_OFFSET,
  50. CRTC5_REGISTER_OFFSET,
  51. CRTC6_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. DIG0_REGISTER_OFFSET,
  64. DIG1_REGISTER_OFFSET,
  65. DIG2_REGISTER_OFFSET,
  66. DIG3_REGISTER_OFFSET,
  67. DIG4_REGISTER_OFFSET,
  68. DIG5_REGISTER_OFFSET,
  69. DIG6_REGISTER_OFFSET,
  70. DIG7_REGISTER_OFFSET,
  71. DIG8_REGISTER_OFFSET
  72. };
  73. static const struct {
  74. uint32_t reg;
  75. uint32_t vblank;
  76. uint32_t vline;
  77. uint32_t hpd;
  78. } interrupt_status_offsets[] = { {
  79. .reg = mmDISP_INTERRUPT_STATUS,
  80. .vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
  81. .vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
  82. .hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
  83. }, {
  84. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE,
  85. .vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
  86. .vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
  87. .hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
  88. }, {
  89. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE2,
  90. .vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
  91. .vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
  92. .hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
  93. }, {
  94. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE3,
  95. .vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
  96. .vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
  97. .hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
  98. }, {
  99. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE4,
  100. .vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
  101. .vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
  102. .hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
  103. }, {
  104. .reg = mmDISP_INTERRUPT_STATUS_CONTINUE5,
  105. .vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
  106. .vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
  107. .hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
  108. } };
  109. static const u32 cz_golden_settings_a11[] =
  110. {
  111. mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
  112. mmFBC_MISC, 0x1f311fff, 0x14300000,
  113. };
  114. static const u32 cz_mgcg_cgcg_init[] =
  115. {
  116. mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
  117. mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
  118. };
  119. static const u32 stoney_golden_settings_a11[] =
  120. {
  121. mmCRTC_DOUBLE_BUFFER_CONTROL, 0x00010101, 0x00010000,
  122. mmFBC_MISC, 0x1f311fff, 0x14302000,
  123. };
  124. static const u32 polaris11_golden_settings_a11[] =
  125. {
  126. mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
  127. mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
  128. mmFBC_DEBUG1, 0xffffffff, 0x00000008,
  129. mmFBC_MISC, 0x9f313fff, 0x14302008,
  130. mmHDMI_CONTROL, 0x313f031f, 0x00000011,
  131. };
  132. static const u32 polaris10_golden_settings_a11[] =
  133. {
  134. mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
  135. mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
  136. mmFBC_MISC, 0x9f313fff, 0x14302008,
  137. mmHDMI_CONTROL, 0x313f031f, 0x00000011,
  138. };
  139. static void dce_v11_0_init_golden_registers(struct amdgpu_device *adev)
  140. {
  141. switch (adev->asic_type) {
  142. case CHIP_CARRIZO:
  143. amdgpu_program_register_sequence(adev,
  144. cz_mgcg_cgcg_init,
  145. (const u32)ARRAY_SIZE(cz_mgcg_cgcg_init));
  146. amdgpu_program_register_sequence(adev,
  147. cz_golden_settings_a11,
  148. (const u32)ARRAY_SIZE(cz_golden_settings_a11));
  149. break;
  150. case CHIP_STONEY:
  151. amdgpu_program_register_sequence(adev,
  152. stoney_golden_settings_a11,
  153. (const u32)ARRAY_SIZE(stoney_golden_settings_a11));
  154. break;
  155. case CHIP_POLARIS11:
  156. amdgpu_program_register_sequence(adev,
  157. polaris11_golden_settings_a11,
  158. (const u32)ARRAY_SIZE(polaris11_golden_settings_a11));
  159. break;
  160. case CHIP_POLARIS10:
  161. amdgpu_program_register_sequence(adev,
  162. polaris10_golden_settings_a11,
  163. (const u32)ARRAY_SIZE(polaris10_golden_settings_a11));
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. static u32 dce_v11_0_audio_endpt_rreg(struct amdgpu_device *adev,
  170. u32 block_offset, u32 reg)
  171. {
  172. unsigned long flags;
  173. u32 r;
  174. spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
  175. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
  176. r = RREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset);
  177. spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
  178. return r;
  179. }
  180. static void dce_v11_0_audio_endpt_wreg(struct amdgpu_device *adev,
  181. u32 block_offset, u32 reg, u32 v)
  182. {
  183. unsigned long flags;
  184. spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
  185. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
  186. WREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset, v);
  187. spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
  188. }
  189. static bool dce_v11_0_is_in_vblank(struct amdgpu_device *adev, int crtc)
  190. {
  191. if (RREG32(mmCRTC_STATUS + crtc_offsets[crtc]) &
  192. CRTC_V_BLANK_START_END__CRTC_V_BLANK_START_MASK)
  193. return true;
  194. else
  195. return false;
  196. }
  197. static bool dce_v11_0_is_counter_moving(struct amdgpu_device *adev, int crtc)
  198. {
  199. u32 pos1, pos2;
  200. pos1 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  201. pos2 = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  202. if (pos1 != pos2)
  203. return true;
  204. else
  205. return false;
  206. }
  207. /**
  208. * dce_v11_0_vblank_wait - vblank wait asic callback.
  209. *
  210. * @adev: amdgpu_device pointer
  211. * @crtc: crtc to wait for vblank on
  212. *
  213. * Wait for vblank on the requested crtc (evergreen+).
  214. */
  215. static void dce_v11_0_vblank_wait(struct amdgpu_device *adev, int crtc)
  216. {
  217. unsigned i = 100;
  218. if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
  219. return;
  220. if (!(RREG32(mmCRTC_CONTROL + crtc_offsets[crtc]) & CRTC_CONTROL__CRTC_MASTER_EN_MASK))
  221. return;
  222. /* depending on when we hit vblank, we may be close to active; if so,
  223. * wait for another frame.
  224. */
  225. while (dce_v11_0_is_in_vblank(adev, crtc)) {
  226. if (i++ == 100) {
  227. i = 0;
  228. if (!dce_v11_0_is_counter_moving(adev, crtc))
  229. break;
  230. }
  231. }
  232. while (!dce_v11_0_is_in_vblank(adev, crtc)) {
  233. if (i++ == 100) {
  234. i = 0;
  235. if (!dce_v11_0_is_counter_moving(adev, crtc))
  236. break;
  237. }
  238. }
  239. }
  240. static u32 dce_v11_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
  241. {
  242. if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
  243. return 0;
  244. else
  245. return RREG32(mmCRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
  246. }
  247. static void dce_v11_0_pageflip_interrupt_init(struct amdgpu_device *adev)
  248. {
  249. unsigned i;
  250. /* Enable pflip interrupts */
  251. for (i = 0; i < adev->mode_info.num_crtc; i++)
  252. amdgpu_irq_get(adev, &adev->pageflip_irq, i);
  253. }
  254. static void dce_v11_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
  255. {
  256. unsigned i;
  257. /* Disable pflip interrupts */
  258. for (i = 0; i < adev->mode_info.num_crtc; i++)
  259. amdgpu_irq_put(adev, &adev->pageflip_irq, i);
  260. }
  261. /**
  262. * dce_v11_0_page_flip - pageflip callback.
  263. *
  264. * @adev: amdgpu_device pointer
  265. * @crtc_id: crtc to cleanup pageflip on
  266. * @crtc_base: new address of the crtc (GPU MC address)
  267. *
  268. * Triggers the actual pageflip by updating the primary
  269. * surface base address.
  270. */
  271. static void dce_v11_0_page_flip(struct amdgpu_device *adev,
  272. int crtc_id, u64 crtc_base, bool async)
  273. {
  274. struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  275. u32 tmp;
  276. /* flip immediate for async, default is vsync */
  277. tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
  278. tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
  279. GRPH_SURFACE_UPDATE_IMMEDIATE_EN, async ? 1 : 0);
  280. WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  281. /* update the scanout addresses */
  282. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  283. upper_32_bits(crtc_base));
  284. /* writing to the low address triggers the update */
  285. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  286. lower_32_bits(crtc_base));
  287. /* post the write */
  288. RREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
  289. }
  290. static int dce_v11_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
  291. u32 *vbl, u32 *position)
  292. {
  293. if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
  294. return -EINVAL;
  295. *vbl = RREG32(mmCRTC_V_BLANK_START_END + crtc_offsets[crtc]);
  296. *position = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
  297. return 0;
  298. }
  299. /**
  300. * dce_v11_0_hpd_sense - hpd sense callback.
  301. *
  302. * @adev: amdgpu_device pointer
  303. * @hpd: hpd (hotplug detect) pin
  304. *
  305. * Checks if a digital monitor is connected (evergreen+).
  306. * Returns true if connected, false if not connected.
  307. */
  308. static bool dce_v11_0_hpd_sense(struct amdgpu_device *adev,
  309. enum amdgpu_hpd_id hpd)
  310. {
  311. int idx;
  312. bool connected = false;
  313. switch (hpd) {
  314. case AMDGPU_HPD_1:
  315. idx = 0;
  316. break;
  317. case AMDGPU_HPD_2:
  318. idx = 1;
  319. break;
  320. case AMDGPU_HPD_3:
  321. idx = 2;
  322. break;
  323. case AMDGPU_HPD_4:
  324. idx = 3;
  325. break;
  326. case AMDGPU_HPD_5:
  327. idx = 4;
  328. break;
  329. case AMDGPU_HPD_6:
  330. idx = 5;
  331. break;
  332. default:
  333. return connected;
  334. }
  335. if (RREG32(mmDC_HPD_INT_STATUS + hpd_offsets[idx]) &
  336. DC_HPD_INT_STATUS__DC_HPD_SENSE_MASK)
  337. connected = true;
  338. return connected;
  339. }
  340. /**
  341. * dce_v11_0_hpd_set_polarity - hpd set polarity callback.
  342. *
  343. * @adev: amdgpu_device pointer
  344. * @hpd: hpd (hotplug detect) pin
  345. *
  346. * Set the polarity of the hpd pin (evergreen+).
  347. */
  348. static void dce_v11_0_hpd_set_polarity(struct amdgpu_device *adev,
  349. enum amdgpu_hpd_id hpd)
  350. {
  351. u32 tmp;
  352. bool connected = dce_v11_0_hpd_sense(adev, hpd);
  353. int idx;
  354. switch (hpd) {
  355. case AMDGPU_HPD_1:
  356. idx = 0;
  357. break;
  358. case AMDGPU_HPD_2:
  359. idx = 1;
  360. break;
  361. case AMDGPU_HPD_3:
  362. idx = 2;
  363. break;
  364. case AMDGPU_HPD_4:
  365. idx = 3;
  366. break;
  367. case AMDGPU_HPD_5:
  368. idx = 4;
  369. break;
  370. case AMDGPU_HPD_6:
  371. idx = 5;
  372. break;
  373. default:
  374. return;
  375. }
  376. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[idx]);
  377. if (connected)
  378. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 0);
  379. else
  380. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 1);
  381. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[idx], tmp);
  382. }
  383. /**
  384. * dce_v11_0_hpd_init - hpd setup callback.
  385. *
  386. * @adev: amdgpu_device pointer
  387. *
  388. * Setup the hpd pins used by the card (evergreen+).
  389. * Enable the pin, set the polarity, and enable the hpd interrupts.
  390. */
  391. static void dce_v11_0_hpd_init(struct amdgpu_device *adev)
  392. {
  393. struct drm_device *dev = adev->ddev;
  394. struct drm_connector *connector;
  395. u32 tmp;
  396. int idx;
  397. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  398. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  399. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
  400. connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
  401. /* don't try to enable hpd on eDP or LVDS avoid breaking the
  402. * aux dp channel on imac and help (but not completely fix)
  403. * https://bugzilla.redhat.com/show_bug.cgi?id=726143
  404. * also avoid interrupt storms during dpms.
  405. */
  406. continue;
  407. }
  408. switch (amdgpu_connector->hpd.hpd) {
  409. case AMDGPU_HPD_1:
  410. idx = 0;
  411. break;
  412. case AMDGPU_HPD_2:
  413. idx = 1;
  414. break;
  415. case AMDGPU_HPD_3:
  416. idx = 2;
  417. break;
  418. case AMDGPU_HPD_4:
  419. idx = 3;
  420. break;
  421. case AMDGPU_HPD_5:
  422. idx = 4;
  423. break;
  424. case AMDGPU_HPD_6:
  425. idx = 5;
  426. break;
  427. default:
  428. continue;
  429. }
  430. tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[idx]);
  431. tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 1);
  432. WREG32(mmDC_HPD_CONTROL + hpd_offsets[idx], tmp);
  433. tmp = RREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[idx]);
  434. tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
  435. DC_HPD_CONNECT_INT_DELAY,
  436. AMDGPU_HPD_CONNECT_INT_DELAY_IN_MS);
  437. tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
  438. DC_HPD_DISCONNECT_INT_DELAY,
  439. AMDGPU_HPD_DISCONNECT_INT_DELAY_IN_MS);
  440. WREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[idx], tmp);
  441. dce_v11_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
  442. amdgpu_irq_get(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
  443. }
  444. }
  445. /**
  446. * dce_v11_0_hpd_fini - hpd tear down callback.
  447. *
  448. * @adev: amdgpu_device pointer
  449. *
  450. * Tear down the hpd pins used by the card (evergreen+).
  451. * Disable the hpd interrupts.
  452. */
  453. static void dce_v11_0_hpd_fini(struct amdgpu_device *adev)
  454. {
  455. struct drm_device *dev = adev->ddev;
  456. struct drm_connector *connector;
  457. u32 tmp;
  458. int idx;
  459. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  460. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  461. switch (amdgpu_connector->hpd.hpd) {
  462. case AMDGPU_HPD_1:
  463. idx = 0;
  464. break;
  465. case AMDGPU_HPD_2:
  466. idx = 1;
  467. break;
  468. case AMDGPU_HPD_3:
  469. idx = 2;
  470. break;
  471. case AMDGPU_HPD_4:
  472. idx = 3;
  473. break;
  474. case AMDGPU_HPD_5:
  475. idx = 4;
  476. break;
  477. case AMDGPU_HPD_6:
  478. idx = 5;
  479. break;
  480. default:
  481. continue;
  482. }
  483. tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[idx]);
  484. tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 0);
  485. WREG32(mmDC_HPD_CONTROL + hpd_offsets[idx], tmp);
  486. amdgpu_irq_put(adev, &adev->hpd_irq, amdgpu_connector->hpd.hpd);
  487. }
  488. }
  489. static u32 dce_v11_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
  490. {
  491. return mmDC_GPIO_HPD_A;
  492. }
  493. static bool dce_v11_0_is_display_hung(struct amdgpu_device *adev)
  494. {
  495. u32 crtc_hung = 0;
  496. u32 crtc_status[6];
  497. u32 i, j, tmp;
  498. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  499. tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
  500. if (REG_GET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN)) {
  501. crtc_status[i] = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
  502. crtc_hung |= (1 << i);
  503. }
  504. }
  505. for (j = 0; j < 10; j++) {
  506. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  507. if (crtc_hung & (1 << i)) {
  508. tmp = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
  509. if (tmp != crtc_status[i])
  510. crtc_hung &= ~(1 << i);
  511. }
  512. }
  513. if (crtc_hung == 0)
  514. return false;
  515. udelay(100);
  516. }
  517. return true;
  518. }
  519. static void dce_v11_0_stop_mc_access(struct amdgpu_device *adev,
  520. struct amdgpu_mode_mc_save *save)
  521. {
  522. u32 crtc_enabled, tmp;
  523. int i;
  524. save->vga_render_control = RREG32(mmVGA_RENDER_CONTROL);
  525. save->vga_hdp_control = RREG32(mmVGA_HDP_CONTROL);
  526. /* disable VGA render */
  527. tmp = RREG32(mmVGA_RENDER_CONTROL);
  528. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
  529. WREG32(mmVGA_RENDER_CONTROL, tmp);
  530. /* blank the display controllers */
  531. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  532. crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
  533. CRTC_CONTROL, CRTC_MASTER_EN);
  534. if (crtc_enabled) {
  535. #if 1
  536. save->crtc_enabled[i] = true;
  537. tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
  538. if (REG_GET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN) == 0) {
  539. /*it is correct only for RGB ; black is 0*/
  540. WREG32(mmCRTC_BLANK_DATA_COLOR + crtc_offsets[i], 0);
  541. tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 1);
  542. WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
  543. }
  544. #else
  545. /* XXX this is a hack to avoid strange behavior with EFI on certain systems */
  546. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
  547. tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
  548. tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
  549. WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
  550. WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
  551. save->crtc_enabled[i] = false;
  552. /* ***** */
  553. #endif
  554. } else {
  555. save->crtc_enabled[i] = false;
  556. }
  557. }
  558. }
  559. static void dce_v11_0_resume_mc_access(struct amdgpu_device *adev,
  560. struct amdgpu_mode_mc_save *save)
  561. {
  562. u32 tmp;
  563. int i;
  564. /* update crtc base addresses */
  565. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  566. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + crtc_offsets[i],
  567. upper_32_bits(adev->mc.vram_start));
  568. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + crtc_offsets[i],
  569. (u32)adev->mc.vram_start);
  570. if (save->crtc_enabled[i]) {
  571. tmp = RREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i]);
  572. tmp = REG_SET_FIELD(tmp, CRTC_BLANK_CONTROL, CRTC_BLANK_DATA_EN, 0);
  573. WREG32(mmCRTC_BLANK_CONTROL + crtc_offsets[i], tmp);
  574. }
  575. }
  576. WREG32(mmVGA_MEMORY_BASE_ADDRESS_HIGH, upper_32_bits(adev->mc.vram_start));
  577. WREG32(mmVGA_MEMORY_BASE_ADDRESS, lower_32_bits(adev->mc.vram_start));
  578. /* Unlock vga access */
  579. WREG32(mmVGA_HDP_CONTROL, save->vga_hdp_control);
  580. mdelay(1);
  581. WREG32(mmVGA_RENDER_CONTROL, save->vga_render_control);
  582. }
  583. static void dce_v11_0_set_vga_render_state(struct amdgpu_device *adev,
  584. bool render)
  585. {
  586. u32 tmp;
  587. /* Lockout access through VGA aperture*/
  588. tmp = RREG32(mmVGA_HDP_CONTROL);
  589. if (render)
  590. tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 0);
  591. else
  592. tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
  593. WREG32(mmVGA_HDP_CONTROL, tmp);
  594. /* disable VGA render */
  595. tmp = RREG32(mmVGA_RENDER_CONTROL);
  596. if (render)
  597. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 1);
  598. else
  599. tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
  600. WREG32(mmVGA_RENDER_CONTROL, tmp);
  601. }
  602. static void dce_v11_0_program_fmt(struct drm_encoder *encoder)
  603. {
  604. struct drm_device *dev = encoder->dev;
  605. struct amdgpu_device *adev = dev->dev_private;
  606. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  607. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  608. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  609. int bpc = 0;
  610. u32 tmp = 0;
  611. enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
  612. if (connector) {
  613. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  614. bpc = amdgpu_connector_get_monitor_bpc(connector);
  615. dither = amdgpu_connector->dither;
  616. }
  617. /* LVDS/eDP FMT is set up by atom */
  618. if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
  619. return;
  620. /* not needed for analog */
  621. if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1) ||
  622. (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2))
  623. return;
  624. if (bpc == 0)
  625. return;
  626. switch (bpc) {
  627. case 6:
  628. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  629. /* XXX sort out optimal dither settings */
  630. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  631. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  632. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  633. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 0);
  634. } else {
  635. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  636. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 0);
  637. }
  638. break;
  639. case 8:
  640. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  641. /* XXX sort out optimal dither settings */
  642. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  643. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  644. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
  645. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  646. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 1);
  647. } else {
  648. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  649. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 1);
  650. }
  651. break;
  652. case 10:
  653. if (dither == AMDGPU_FMT_DITHER_ENABLE) {
  654. /* XXX sort out optimal dither settings */
  655. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
  656. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
  657. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
  658. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
  659. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 2);
  660. } else {
  661. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
  662. tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 2);
  663. }
  664. break;
  665. default:
  666. /* not needed */
  667. break;
  668. }
  669. WREG32(mmFMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  670. }
  671. /* display watermark setup */
  672. /**
  673. * dce_v11_0_line_buffer_adjust - Set up the line buffer
  674. *
  675. * @adev: amdgpu_device pointer
  676. * @amdgpu_crtc: the selected display controller
  677. * @mode: the current display mode on the selected display
  678. * controller
  679. *
  680. * Setup up the line buffer allocation for
  681. * the selected display controller (CIK).
  682. * Returns the line buffer size in pixels.
  683. */
  684. static u32 dce_v11_0_line_buffer_adjust(struct amdgpu_device *adev,
  685. struct amdgpu_crtc *amdgpu_crtc,
  686. struct drm_display_mode *mode)
  687. {
  688. u32 tmp, buffer_alloc, i, mem_cfg;
  689. u32 pipe_offset = amdgpu_crtc->crtc_id;
  690. /*
  691. * Line Buffer Setup
  692. * There are 6 line buffers, one for each display controllers.
  693. * There are 3 partitions per LB. Select the number of partitions
  694. * to enable based on the display width. For display widths larger
  695. * than 4096, you need use to use 2 display controllers and combine
  696. * them using the stereo blender.
  697. */
  698. if (amdgpu_crtc->base.enabled && mode) {
  699. if (mode->crtc_hdisplay < 1920) {
  700. mem_cfg = 1;
  701. buffer_alloc = 2;
  702. } else if (mode->crtc_hdisplay < 2560) {
  703. mem_cfg = 2;
  704. buffer_alloc = 2;
  705. } else if (mode->crtc_hdisplay < 4096) {
  706. mem_cfg = 0;
  707. buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
  708. } else {
  709. DRM_DEBUG_KMS("Mode too big for LB!\n");
  710. mem_cfg = 0;
  711. buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
  712. }
  713. } else {
  714. mem_cfg = 1;
  715. buffer_alloc = 0;
  716. }
  717. tmp = RREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset);
  718. tmp = REG_SET_FIELD(tmp, LB_MEMORY_CTRL, LB_MEMORY_CONFIG, mem_cfg);
  719. WREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset, tmp);
  720. tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
  721. tmp = REG_SET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATED, buffer_alloc);
  722. WREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset, tmp);
  723. for (i = 0; i < adev->usec_timeout; i++) {
  724. tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
  725. if (REG_GET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATION_COMPLETED))
  726. break;
  727. udelay(1);
  728. }
  729. if (amdgpu_crtc->base.enabled && mode) {
  730. switch (mem_cfg) {
  731. case 0:
  732. default:
  733. return 4096 * 2;
  734. case 1:
  735. return 1920 * 2;
  736. case 2:
  737. return 2560 * 2;
  738. }
  739. }
  740. /* controller not enabled, so no lb used */
  741. return 0;
  742. }
  743. /**
  744. * cik_get_number_of_dram_channels - get the number of dram channels
  745. *
  746. * @adev: amdgpu_device pointer
  747. *
  748. * Look up the number of video ram channels (CIK).
  749. * Used for display watermark bandwidth calculations
  750. * Returns the number of dram channels
  751. */
  752. static u32 cik_get_number_of_dram_channels(struct amdgpu_device *adev)
  753. {
  754. u32 tmp = RREG32(mmMC_SHARED_CHMAP);
  755. switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
  756. case 0:
  757. default:
  758. return 1;
  759. case 1:
  760. return 2;
  761. case 2:
  762. return 4;
  763. case 3:
  764. return 8;
  765. case 4:
  766. return 3;
  767. case 5:
  768. return 6;
  769. case 6:
  770. return 10;
  771. case 7:
  772. return 12;
  773. case 8:
  774. return 16;
  775. }
  776. }
  777. struct dce10_wm_params {
  778. u32 dram_channels; /* number of dram channels */
  779. u32 yclk; /* bandwidth per dram data pin in kHz */
  780. u32 sclk; /* engine clock in kHz */
  781. u32 disp_clk; /* display clock in kHz */
  782. u32 src_width; /* viewport width */
  783. u32 active_time; /* active display time in ns */
  784. u32 blank_time; /* blank time in ns */
  785. bool interlaced; /* mode is interlaced */
  786. fixed20_12 vsc; /* vertical scale ratio */
  787. u32 num_heads; /* number of active crtcs */
  788. u32 bytes_per_pixel; /* bytes per pixel display + overlay */
  789. u32 lb_size; /* line buffer allocated to pipe */
  790. u32 vtaps; /* vertical scaler taps */
  791. };
  792. /**
  793. * dce_v11_0_dram_bandwidth - get the dram bandwidth
  794. *
  795. * @wm: watermark calculation data
  796. *
  797. * Calculate the raw dram bandwidth (CIK).
  798. * Used for display watermark bandwidth calculations
  799. * Returns the dram bandwidth in MBytes/s
  800. */
  801. static u32 dce_v11_0_dram_bandwidth(struct dce10_wm_params *wm)
  802. {
  803. /* Calculate raw DRAM Bandwidth */
  804. fixed20_12 dram_efficiency; /* 0.7 */
  805. fixed20_12 yclk, dram_channels, bandwidth;
  806. fixed20_12 a;
  807. a.full = dfixed_const(1000);
  808. yclk.full = dfixed_const(wm->yclk);
  809. yclk.full = dfixed_div(yclk, a);
  810. dram_channels.full = dfixed_const(wm->dram_channels * 4);
  811. a.full = dfixed_const(10);
  812. dram_efficiency.full = dfixed_const(7);
  813. dram_efficiency.full = dfixed_div(dram_efficiency, a);
  814. bandwidth.full = dfixed_mul(dram_channels, yclk);
  815. bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
  816. return dfixed_trunc(bandwidth);
  817. }
  818. /**
  819. * dce_v11_0_dram_bandwidth_for_display - get the dram bandwidth for display
  820. *
  821. * @wm: watermark calculation data
  822. *
  823. * Calculate the dram bandwidth used for display (CIK).
  824. * Used for display watermark bandwidth calculations
  825. * Returns the dram bandwidth for display in MBytes/s
  826. */
  827. static u32 dce_v11_0_dram_bandwidth_for_display(struct dce10_wm_params *wm)
  828. {
  829. /* Calculate DRAM Bandwidth and the part allocated to display. */
  830. fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
  831. fixed20_12 yclk, dram_channels, bandwidth;
  832. fixed20_12 a;
  833. a.full = dfixed_const(1000);
  834. yclk.full = dfixed_const(wm->yclk);
  835. yclk.full = dfixed_div(yclk, a);
  836. dram_channels.full = dfixed_const(wm->dram_channels * 4);
  837. a.full = dfixed_const(10);
  838. disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
  839. disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
  840. bandwidth.full = dfixed_mul(dram_channels, yclk);
  841. bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
  842. return dfixed_trunc(bandwidth);
  843. }
  844. /**
  845. * dce_v11_0_data_return_bandwidth - get the data return bandwidth
  846. *
  847. * @wm: watermark calculation data
  848. *
  849. * Calculate the data return bandwidth used for display (CIK).
  850. * Used for display watermark bandwidth calculations
  851. * Returns the data return bandwidth in MBytes/s
  852. */
  853. static u32 dce_v11_0_data_return_bandwidth(struct dce10_wm_params *wm)
  854. {
  855. /* Calculate the display Data return Bandwidth */
  856. fixed20_12 return_efficiency; /* 0.8 */
  857. fixed20_12 sclk, bandwidth;
  858. fixed20_12 a;
  859. a.full = dfixed_const(1000);
  860. sclk.full = dfixed_const(wm->sclk);
  861. sclk.full = dfixed_div(sclk, a);
  862. a.full = dfixed_const(10);
  863. return_efficiency.full = dfixed_const(8);
  864. return_efficiency.full = dfixed_div(return_efficiency, a);
  865. a.full = dfixed_const(32);
  866. bandwidth.full = dfixed_mul(a, sclk);
  867. bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
  868. return dfixed_trunc(bandwidth);
  869. }
  870. /**
  871. * dce_v11_0_dmif_request_bandwidth - get the dmif bandwidth
  872. *
  873. * @wm: watermark calculation data
  874. *
  875. * Calculate the dmif bandwidth used for display (CIK).
  876. * Used for display watermark bandwidth calculations
  877. * Returns the dmif bandwidth in MBytes/s
  878. */
  879. static u32 dce_v11_0_dmif_request_bandwidth(struct dce10_wm_params *wm)
  880. {
  881. /* Calculate the DMIF Request Bandwidth */
  882. fixed20_12 disp_clk_request_efficiency; /* 0.8 */
  883. fixed20_12 disp_clk, bandwidth;
  884. fixed20_12 a, b;
  885. a.full = dfixed_const(1000);
  886. disp_clk.full = dfixed_const(wm->disp_clk);
  887. disp_clk.full = dfixed_div(disp_clk, a);
  888. a.full = dfixed_const(32);
  889. b.full = dfixed_mul(a, disp_clk);
  890. a.full = dfixed_const(10);
  891. disp_clk_request_efficiency.full = dfixed_const(8);
  892. disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
  893. bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
  894. return dfixed_trunc(bandwidth);
  895. }
  896. /**
  897. * dce_v11_0_available_bandwidth - get the min available bandwidth
  898. *
  899. * @wm: watermark calculation data
  900. *
  901. * Calculate the min available bandwidth used for display (CIK).
  902. * Used for display watermark bandwidth calculations
  903. * Returns the min available bandwidth in MBytes/s
  904. */
  905. static u32 dce_v11_0_available_bandwidth(struct dce10_wm_params *wm)
  906. {
  907. /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
  908. u32 dram_bandwidth = dce_v11_0_dram_bandwidth(wm);
  909. u32 data_return_bandwidth = dce_v11_0_data_return_bandwidth(wm);
  910. u32 dmif_req_bandwidth = dce_v11_0_dmif_request_bandwidth(wm);
  911. return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
  912. }
  913. /**
  914. * dce_v11_0_average_bandwidth - get the average available bandwidth
  915. *
  916. * @wm: watermark calculation data
  917. *
  918. * Calculate the average available bandwidth used for display (CIK).
  919. * Used for display watermark bandwidth calculations
  920. * Returns the average available bandwidth in MBytes/s
  921. */
  922. static u32 dce_v11_0_average_bandwidth(struct dce10_wm_params *wm)
  923. {
  924. /* Calculate the display mode Average Bandwidth
  925. * DisplayMode should contain the source and destination dimensions,
  926. * timing, etc.
  927. */
  928. fixed20_12 bpp;
  929. fixed20_12 line_time;
  930. fixed20_12 src_width;
  931. fixed20_12 bandwidth;
  932. fixed20_12 a;
  933. a.full = dfixed_const(1000);
  934. line_time.full = dfixed_const(wm->active_time + wm->blank_time);
  935. line_time.full = dfixed_div(line_time, a);
  936. bpp.full = dfixed_const(wm->bytes_per_pixel);
  937. src_width.full = dfixed_const(wm->src_width);
  938. bandwidth.full = dfixed_mul(src_width, bpp);
  939. bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
  940. bandwidth.full = dfixed_div(bandwidth, line_time);
  941. return dfixed_trunc(bandwidth);
  942. }
  943. /**
  944. * dce_v11_0_latency_watermark - get the latency watermark
  945. *
  946. * @wm: watermark calculation data
  947. *
  948. * Calculate the latency watermark (CIK).
  949. * Used for display watermark bandwidth calculations
  950. * Returns the latency watermark in ns
  951. */
  952. static u32 dce_v11_0_latency_watermark(struct dce10_wm_params *wm)
  953. {
  954. /* First calculate the latency in ns */
  955. u32 mc_latency = 2000; /* 2000 ns. */
  956. u32 available_bandwidth = dce_v11_0_available_bandwidth(wm);
  957. u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
  958. u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
  959. u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
  960. u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
  961. (wm->num_heads * cursor_line_pair_return_time);
  962. u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
  963. u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
  964. u32 tmp, dmif_size = 12288;
  965. fixed20_12 a, b, c;
  966. if (wm->num_heads == 0)
  967. return 0;
  968. a.full = dfixed_const(2);
  969. b.full = dfixed_const(1);
  970. if ((wm->vsc.full > a.full) ||
  971. ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
  972. (wm->vtaps >= 5) ||
  973. ((wm->vsc.full >= a.full) && wm->interlaced))
  974. max_src_lines_per_dst_line = 4;
  975. else
  976. max_src_lines_per_dst_line = 2;
  977. a.full = dfixed_const(available_bandwidth);
  978. b.full = dfixed_const(wm->num_heads);
  979. a.full = dfixed_div(a, b);
  980. b.full = dfixed_const(mc_latency + 512);
  981. c.full = dfixed_const(wm->disp_clk);
  982. b.full = dfixed_div(b, c);
  983. c.full = dfixed_const(dmif_size);
  984. b.full = dfixed_div(c, b);
  985. tmp = min(dfixed_trunc(a), dfixed_trunc(b));
  986. b.full = dfixed_const(1000);
  987. c.full = dfixed_const(wm->disp_clk);
  988. b.full = dfixed_div(c, b);
  989. c.full = dfixed_const(wm->bytes_per_pixel);
  990. b.full = dfixed_mul(b, c);
  991. lb_fill_bw = min(tmp, dfixed_trunc(b));
  992. a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
  993. b.full = dfixed_const(1000);
  994. c.full = dfixed_const(lb_fill_bw);
  995. b.full = dfixed_div(c, b);
  996. a.full = dfixed_div(a, b);
  997. line_fill_time = dfixed_trunc(a);
  998. if (line_fill_time < wm->active_time)
  999. return latency;
  1000. else
  1001. return latency + (line_fill_time - wm->active_time);
  1002. }
  1003. /**
  1004. * dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display - check
  1005. * average and available dram bandwidth
  1006. *
  1007. * @wm: watermark calculation data
  1008. *
  1009. * Check if the display average bandwidth fits in the display
  1010. * dram bandwidth (CIK).
  1011. * Used for display watermark bandwidth calculations
  1012. * Returns true if the display fits, false if not.
  1013. */
  1014. static bool dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params *wm)
  1015. {
  1016. if (dce_v11_0_average_bandwidth(wm) <=
  1017. (dce_v11_0_dram_bandwidth_for_display(wm) / wm->num_heads))
  1018. return true;
  1019. else
  1020. return false;
  1021. }
  1022. /**
  1023. * dce_v11_0_average_bandwidth_vs_available_bandwidth - check
  1024. * average and available bandwidth
  1025. *
  1026. * @wm: watermark calculation data
  1027. *
  1028. * Check if the display average bandwidth fits in the display
  1029. * available bandwidth (CIK).
  1030. * Used for display watermark bandwidth calculations
  1031. * Returns true if the display fits, false if not.
  1032. */
  1033. static bool dce_v11_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params *wm)
  1034. {
  1035. if (dce_v11_0_average_bandwidth(wm) <=
  1036. (dce_v11_0_available_bandwidth(wm) / wm->num_heads))
  1037. return true;
  1038. else
  1039. return false;
  1040. }
  1041. /**
  1042. * dce_v11_0_check_latency_hiding - check latency hiding
  1043. *
  1044. * @wm: watermark calculation data
  1045. *
  1046. * Check latency hiding (CIK).
  1047. * Used for display watermark bandwidth calculations
  1048. * Returns true if the display fits, false if not.
  1049. */
  1050. static bool dce_v11_0_check_latency_hiding(struct dce10_wm_params *wm)
  1051. {
  1052. u32 lb_partitions = wm->lb_size / wm->src_width;
  1053. u32 line_time = wm->active_time + wm->blank_time;
  1054. u32 latency_tolerant_lines;
  1055. u32 latency_hiding;
  1056. fixed20_12 a;
  1057. a.full = dfixed_const(1);
  1058. if (wm->vsc.full > a.full)
  1059. latency_tolerant_lines = 1;
  1060. else {
  1061. if (lb_partitions <= (wm->vtaps + 1))
  1062. latency_tolerant_lines = 1;
  1063. else
  1064. latency_tolerant_lines = 2;
  1065. }
  1066. latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
  1067. if (dce_v11_0_latency_watermark(wm) <= latency_hiding)
  1068. return true;
  1069. else
  1070. return false;
  1071. }
  1072. /**
  1073. * dce_v11_0_program_watermarks - program display watermarks
  1074. *
  1075. * @adev: amdgpu_device pointer
  1076. * @amdgpu_crtc: the selected display controller
  1077. * @lb_size: line buffer size
  1078. * @num_heads: number of display controllers in use
  1079. *
  1080. * Calculate and program the display watermarks for the
  1081. * selected display controller (CIK).
  1082. */
  1083. static void dce_v11_0_program_watermarks(struct amdgpu_device *adev,
  1084. struct amdgpu_crtc *amdgpu_crtc,
  1085. u32 lb_size, u32 num_heads)
  1086. {
  1087. struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
  1088. struct dce10_wm_params wm_low, wm_high;
  1089. u32 pixel_period;
  1090. u32 line_time = 0;
  1091. u32 latency_watermark_a = 0, latency_watermark_b = 0;
  1092. u32 tmp, wm_mask, lb_vblank_lead_lines = 0;
  1093. if (amdgpu_crtc->base.enabled && num_heads && mode) {
  1094. pixel_period = 1000000 / (u32)mode->clock;
  1095. line_time = min((u32)mode->crtc_htotal * pixel_period, (u32)65535);
  1096. /* watermark for high clocks */
  1097. if (adev->pm.dpm_enabled) {
  1098. wm_high.yclk =
  1099. amdgpu_dpm_get_mclk(adev, false) * 10;
  1100. wm_high.sclk =
  1101. amdgpu_dpm_get_sclk(adev, false) * 10;
  1102. } else {
  1103. wm_high.yclk = adev->pm.current_mclk * 10;
  1104. wm_high.sclk = adev->pm.current_sclk * 10;
  1105. }
  1106. wm_high.disp_clk = mode->clock;
  1107. wm_high.src_width = mode->crtc_hdisplay;
  1108. wm_high.active_time = mode->crtc_hdisplay * pixel_period;
  1109. wm_high.blank_time = line_time - wm_high.active_time;
  1110. wm_high.interlaced = false;
  1111. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1112. wm_high.interlaced = true;
  1113. wm_high.vsc = amdgpu_crtc->vsc;
  1114. wm_high.vtaps = 1;
  1115. if (amdgpu_crtc->rmx_type != RMX_OFF)
  1116. wm_high.vtaps = 2;
  1117. wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
  1118. wm_high.lb_size = lb_size;
  1119. wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
  1120. wm_high.num_heads = num_heads;
  1121. /* set for high clocks */
  1122. latency_watermark_a = min(dce_v11_0_latency_watermark(&wm_high), (u32)65535);
  1123. /* possibly force display priority to high */
  1124. /* should really do this at mode validation time... */
  1125. if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
  1126. !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
  1127. !dce_v11_0_check_latency_hiding(&wm_high) ||
  1128. (adev->mode_info.disp_priority == 2)) {
  1129. DRM_DEBUG_KMS("force priority to high\n");
  1130. }
  1131. /* watermark for low clocks */
  1132. if (adev->pm.dpm_enabled) {
  1133. wm_low.yclk =
  1134. amdgpu_dpm_get_mclk(adev, true) * 10;
  1135. wm_low.sclk =
  1136. amdgpu_dpm_get_sclk(adev, true) * 10;
  1137. } else {
  1138. wm_low.yclk = adev->pm.current_mclk * 10;
  1139. wm_low.sclk = adev->pm.current_sclk * 10;
  1140. }
  1141. wm_low.disp_clk = mode->clock;
  1142. wm_low.src_width = mode->crtc_hdisplay;
  1143. wm_low.active_time = mode->crtc_hdisplay * pixel_period;
  1144. wm_low.blank_time = line_time - wm_low.active_time;
  1145. wm_low.interlaced = false;
  1146. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1147. wm_low.interlaced = true;
  1148. wm_low.vsc = amdgpu_crtc->vsc;
  1149. wm_low.vtaps = 1;
  1150. if (amdgpu_crtc->rmx_type != RMX_OFF)
  1151. wm_low.vtaps = 2;
  1152. wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
  1153. wm_low.lb_size = lb_size;
  1154. wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
  1155. wm_low.num_heads = num_heads;
  1156. /* set for low clocks */
  1157. latency_watermark_b = min(dce_v11_0_latency_watermark(&wm_low), (u32)65535);
  1158. /* possibly force display priority to high */
  1159. /* should really do this at mode validation time... */
  1160. if (!dce_v11_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
  1161. !dce_v11_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
  1162. !dce_v11_0_check_latency_hiding(&wm_low) ||
  1163. (adev->mode_info.disp_priority == 2)) {
  1164. DRM_DEBUG_KMS("force priority to high\n");
  1165. }
  1166. lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
  1167. }
  1168. /* select wm A */
  1169. wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
  1170. tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 1);
  1171. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1172. tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
  1173. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_a);
  1174. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
  1175. WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1176. /* select wm B */
  1177. tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 2);
  1178. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1179. tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
  1180. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_b);
  1181. tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
  1182. WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1183. /* restore original selection */
  1184. WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
  1185. /* save values for DPM */
  1186. amdgpu_crtc->line_time = line_time;
  1187. amdgpu_crtc->wm_high = latency_watermark_a;
  1188. amdgpu_crtc->wm_low = latency_watermark_b;
  1189. /* Save number of lines the linebuffer leads before the scanout */
  1190. amdgpu_crtc->lb_vblank_lead_lines = lb_vblank_lead_lines;
  1191. }
  1192. /**
  1193. * dce_v11_0_bandwidth_update - program display watermarks
  1194. *
  1195. * @adev: amdgpu_device pointer
  1196. *
  1197. * Calculate and program the display watermarks and line
  1198. * buffer allocation (CIK).
  1199. */
  1200. static void dce_v11_0_bandwidth_update(struct amdgpu_device *adev)
  1201. {
  1202. struct drm_display_mode *mode = NULL;
  1203. u32 num_heads = 0, lb_size;
  1204. int i;
  1205. amdgpu_update_display_priority(adev);
  1206. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1207. if (adev->mode_info.crtcs[i]->base.enabled)
  1208. num_heads++;
  1209. }
  1210. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  1211. mode = &adev->mode_info.crtcs[i]->base.mode;
  1212. lb_size = dce_v11_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
  1213. dce_v11_0_program_watermarks(adev, adev->mode_info.crtcs[i],
  1214. lb_size, num_heads);
  1215. }
  1216. }
  1217. static void dce_v11_0_audio_get_connected_pins(struct amdgpu_device *adev)
  1218. {
  1219. int i;
  1220. u32 offset, tmp;
  1221. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1222. offset = adev->mode_info.audio.pin[i].offset;
  1223. tmp = RREG32_AUDIO_ENDPT(offset,
  1224. ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
  1225. if (((tmp &
  1226. AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
  1227. AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
  1228. adev->mode_info.audio.pin[i].connected = false;
  1229. else
  1230. adev->mode_info.audio.pin[i].connected = true;
  1231. }
  1232. }
  1233. static struct amdgpu_audio_pin *dce_v11_0_audio_get_pin(struct amdgpu_device *adev)
  1234. {
  1235. int i;
  1236. dce_v11_0_audio_get_connected_pins(adev);
  1237. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1238. if (adev->mode_info.audio.pin[i].connected)
  1239. return &adev->mode_info.audio.pin[i];
  1240. }
  1241. DRM_ERROR("No connected audio pins found!\n");
  1242. return NULL;
  1243. }
  1244. static void dce_v11_0_afmt_audio_select_pin(struct drm_encoder *encoder)
  1245. {
  1246. struct amdgpu_device *adev = encoder->dev->dev_private;
  1247. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1248. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1249. u32 tmp;
  1250. if (!dig || !dig->afmt || !dig->afmt->pin)
  1251. return;
  1252. tmp = RREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset);
  1253. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_SRC_CONTROL, AFMT_AUDIO_SRC_SELECT, dig->afmt->pin->id);
  1254. WREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, tmp);
  1255. }
  1256. static void dce_v11_0_audio_write_latency_fields(struct drm_encoder *encoder,
  1257. struct drm_display_mode *mode)
  1258. {
  1259. struct amdgpu_device *adev = encoder->dev->dev_private;
  1260. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1261. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1262. struct drm_connector *connector;
  1263. struct amdgpu_connector *amdgpu_connector = NULL;
  1264. u32 tmp;
  1265. int interlace = 0;
  1266. if (!dig || !dig->afmt || !dig->afmt->pin)
  1267. return;
  1268. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1269. if (connector->encoder == encoder) {
  1270. amdgpu_connector = to_amdgpu_connector(connector);
  1271. break;
  1272. }
  1273. }
  1274. if (!amdgpu_connector) {
  1275. DRM_ERROR("Couldn't find encoder's connector\n");
  1276. return;
  1277. }
  1278. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1279. interlace = 1;
  1280. if (connector->latency_present[interlace]) {
  1281. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1282. VIDEO_LIPSYNC, connector->video_latency[interlace]);
  1283. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1284. AUDIO_LIPSYNC, connector->audio_latency[interlace]);
  1285. } else {
  1286. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1287. VIDEO_LIPSYNC, 0);
  1288. tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
  1289. AUDIO_LIPSYNC, 0);
  1290. }
  1291. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1292. ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
  1293. }
  1294. static void dce_v11_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
  1295. {
  1296. struct amdgpu_device *adev = encoder->dev->dev_private;
  1297. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1298. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1299. struct drm_connector *connector;
  1300. struct amdgpu_connector *amdgpu_connector = NULL;
  1301. u32 tmp;
  1302. u8 *sadb = NULL;
  1303. int sad_count;
  1304. if (!dig || !dig->afmt || !dig->afmt->pin)
  1305. return;
  1306. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1307. if (connector->encoder == encoder) {
  1308. amdgpu_connector = to_amdgpu_connector(connector);
  1309. break;
  1310. }
  1311. }
  1312. if (!amdgpu_connector) {
  1313. DRM_ERROR("Couldn't find encoder's connector\n");
  1314. return;
  1315. }
  1316. sad_count = drm_edid_to_speaker_allocation(amdgpu_connector_edid(connector), &sadb);
  1317. if (sad_count < 0) {
  1318. DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
  1319. sad_count = 0;
  1320. }
  1321. /* program the speaker allocation */
  1322. tmp = RREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1323. ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
  1324. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1325. DP_CONNECTION, 0);
  1326. /* set HDMI mode */
  1327. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1328. HDMI_CONNECTION, 1);
  1329. if (sad_count)
  1330. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1331. SPEAKER_ALLOCATION, sadb[0]);
  1332. else
  1333. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
  1334. SPEAKER_ALLOCATION, 5); /* stereo */
  1335. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
  1336. ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
  1337. kfree(sadb);
  1338. }
  1339. static void dce_v11_0_audio_write_sad_regs(struct drm_encoder *encoder)
  1340. {
  1341. struct amdgpu_device *adev = encoder->dev->dev_private;
  1342. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1343. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1344. struct drm_connector *connector;
  1345. struct amdgpu_connector *amdgpu_connector = NULL;
  1346. struct cea_sad *sads;
  1347. int i, sad_count;
  1348. static const u16 eld_reg_to_type[][2] = {
  1349. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
  1350. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
  1351. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
  1352. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
  1353. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
  1354. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
  1355. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
  1356. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
  1357. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
  1358. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
  1359. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
  1360. { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
  1361. };
  1362. if (!dig || !dig->afmt || !dig->afmt->pin)
  1363. return;
  1364. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  1365. if (connector->encoder == encoder) {
  1366. amdgpu_connector = to_amdgpu_connector(connector);
  1367. break;
  1368. }
  1369. }
  1370. if (!amdgpu_connector) {
  1371. DRM_ERROR("Couldn't find encoder's connector\n");
  1372. return;
  1373. }
  1374. sad_count = drm_edid_to_sad(amdgpu_connector_edid(connector), &sads);
  1375. if (sad_count <= 0) {
  1376. DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
  1377. return;
  1378. }
  1379. BUG_ON(!sads);
  1380. for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
  1381. u32 tmp = 0;
  1382. u8 stereo_freqs = 0;
  1383. int max_channels = -1;
  1384. int j;
  1385. for (j = 0; j < sad_count; j++) {
  1386. struct cea_sad *sad = &sads[j];
  1387. if (sad->format == eld_reg_to_type[i][1]) {
  1388. if (sad->channels > max_channels) {
  1389. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1390. MAX_CHANNELS, sad->channels);
  1391. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1392. DESCRIPTOR_BYTE_2, sad->byte2);
  1393. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1394. SUPPORTED_FREQUENCIES, sad->freq);
  1395. max_channels = sad->channels;
  1396. }
  1397. if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
  1398. stereo_freqs |= sad->freq;
  1399. else
  1400. break;
  1401. }
  1402. }
  1403. tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
  1404. SUPPORTED_FREQUENCIES_STEREO, stereo_freqs);
  1405. WREG32_AUDIO_ENDPT(dig->afmt->pin->offset, eld_reg_to_type[i][0], tmp);
  1406. }
  1407. kfree(sads);
  1408. }
  1409. static void dce_v11_0_audio_enable(struct amdgpu_device *adev,
  1410. struct amdgpu_audio_pin *pin,
  1411. bool enable)
  1412. {
  1413. if (!pin)
  1414. return;
  1415. WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
  1416. enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
  1417. }
  1418. static const u32 pin_offsets[] =
  1419. {
  1420. AUD0_REGISTER_OFFSET,
  1421. AUD1_REGISTER_OFFSET,
  1422. AUD2_REGISTER_OFFSET,
  1423. AUD3_REGISTER_OFFSET,
  1424. AUD4_REGISTER_OFFSET,
  1425. AUD5_REGISTER_OFFSET,
  1426. AUD6_REGISTER_OFFSET,
  1427. AUD7_REGISTER_OFFSET,
  1428. };
  1429. static int dce_v11_0_audio_init(struct amdgpu_device *adev)
  1430. {
  1431. int i;
  1432. if (!amdgpu_audio)
  1433. return 0;
  1434. adev->mode_info.audio.enabled = true;
  1435. switch (adev->asic_type) {
  1436. case CHIP_CARRIZO:
  1437. case CHIP_STONEY:
  1438. adev->mode_info.audio.num_pins = 7;
  1439. break;
  1440. case CHIP_POLARIS10:
  1441. adev->mode_info.audio.num_pins = 8;
  1442. break;
  1443. case CHIP_POLARIS11:
  1444. adev->mode_info.audio.num_pins = 6;
  1445. break;
  1446. default:
  1447. return -EINVAL;
  1448. }
  1449. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  1450. adev->mode_info.audio.pin[i].channels = -1;
  1451. adev->mode_info.audio.pin[i].rate = -1;
  1452. adev->mode_info.audio.pin[i].bits_per_sample = -1;
  1453. adev->mode_info.audio.pin[i].status_bits = 0;
  1454. adev->mode_info.audio.pin[i].category_code = 0;
  1455. adev->mode_info.audio.pin[i].connected = false;
  1456. adev->mode_info.audio.pin[i].offset = pin_offsets[i];
  1457. adev->mode_info.audio.pin[i].id = i;
  1458. /* disable audio. it will be set up later */
  1459. /* XXX remove once we switch to ip funcs */
  1460. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  1461. }
  1462. return 0;
  1463. }
  1464. static void dce_v11_0_audio_fini(struct amdgpu_device *adev)
  1465. {
  1466. int i;
  1467. if (!amdgpu_audio)
  1468. return;
  1469. if (!adev->mode_info.audio.enabled)
  1470. return;
  1471. for (i = 0; i < adev->mode_info.audio.num_pins; i++)
  1472. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  1473. adev->mode_info.audio.enabled = false;
  1474. }
  1475. /*
  1476. * update the N and CTS parameters for a given pixel clock rate
  1477. */
  1478. static void dce_v11_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
  1479. {
  1480. struct drm_device *dev = encoder->dev;
  1481. struct amdgpu_device *adev = dev->dev_private;
  1482. struct amdgpu_afmt_acr acr = amdgpu_afmt_acr(clock);
  1483. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1484. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1485. u32 tmp;
  1486. tmp = RREG32(mmHDMI_ACR_32_0 + dig->afmt->offset);
  1487. tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_0, HDMI_ACR_CTS_32, acr.cts_32khz);
  1488. WREG32(mmHDMI_ACR_32_0 + dig->afmt->offset, tmp);
  1489. tmp = RREG32(mmHDMI_ACR_32_1 + dig->afmt->offset);
  1490. tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_1, HDMI_ACR_N_32, acr.n_32khz);
  1491. WREG32(mmHDMI_ACR_32_1 + dig->afmt->offset, tmp);
  1492. tmp = RREG32(mmHDMI_ACR_44_0 + dig->afmt->offset);
  1493. tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_0, HDMI_ACR_CTS_44, acr.cts_44_1khz);
  1494. WREG32(mmHDMI_ACR_44_0 + dig->afmt->offset, tmp);
  1495. tmp = RREG32(mmHDMI_ACR_44_1 + dig->afmt->offset);
  1496. tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_1, HDMI_ACR_N_44, acr.n_44_1khz);
  1497. WREG32(mmHDMI_ACR_44_1 + dig->afmt->offset, tmp);
  1498. tmp = RREG32(mmHDMI_ACR_48_0 + dig->afmt->offset);
  1499. tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_0, HDMI_ACR_CTS_48, acr.cts_48khz);
  1500. WREG32(mmHDMI_ACR_48_0 + dig->afmt->offset, tmp);
  1501. tmp = RREG32(mmHDMI_ACR_48_1 + dig->afmt->offset);
  1502. tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_1, HDMI_ACR_N_48, acr.n_48khz);
  1503. WREG32(mmHDMI_ACR_48_1 + dig->afmt->offset, tmp);
  1504. }
  1505. /*
  1506. * build a HDMI Video Info Frame
  1507. */
  1508. static void dce_v11_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
  1509. void *buffer, size_t size)
  1510. {
  1511. struct drm_device *dev = encoder->dev;
  1512. struct amdgpu_device *adev = dev->dev_private;
  1513. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1514. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1515. uint8_t *frame = buffer + 3;
  1516. uint8_t *header = buffer;
  1517. WREG32(mmAFMT_AVI_INFO0 + dig->afmt->offset,
  1518. frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
  1519. WREG32(mmAFMT_AVI_INFO1 + dig->afmt->offset,
  1520. frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
  1521. WREG32(mmAFMT_AVI_INFO2 + dig->afmt->offset,
  1522. frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
  1523. WREG32(mmAFMT_AVI_INFO3 + dig->afmt->offset,
  1524. frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
  1525. }
  1526. static void dce_v11_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
  1527. {
  1528. struct drm_device *dev = encoder->dev;
  1529. struct amdgpu_device *adev = dev->dev_private;
  1530. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1531. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1532. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  1533. u32 dto_phase = 24 * 1000;
  1534. u32 dto_modulo = clock;
  1535. u32 tmp;
  1536. if (!dig || !dig->afmt)
  1537. return;
  1538. /* XXX two dtos; generally use dto0 for hdmi */
  1539. /* Express [24MHz / target pixel clock] as an exact rational
  1540. * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
  1541. * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
  1542. */
  1543. tmp = RREG32(mmDCCG_AUDIO_DTO_SOURCE);
  1544. tmp = REG_SET_FIELD(tmp, DCCG_AUDIO_DTO_SOURCE, DCCG_AUDIO_DTO0_SOURCE_SEL,
  1545. amdgpu_crtc->crtc_id);
  1546. WREG32(mmDCCG_AUDIO_DTO_SOURCE, tmp);
  1547. WREG32(mmDCCG_AUDIO_DTO0_PHASE, dto_phase);
  1548. WREG32(mmDCCG_AUDIO_DTO0_MODULE, dto_modulo);
  1549. }
  1550. /*
  1551. * update the info frames with the data from the current display mode
  1552. */
  1553. static void dce_v11_0_afmt_setmode(struct drm_encoder *encoder,
  1554. struct drm_display_mode *mode)
  1555. {
  1556. struct drm_device *dev = encoder->dev;
  1557. struct amdgpu_device *adev = dev->dev_private;
  1558. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1559. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1560. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  1561. u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
  1562. struct hdmi_avi_infoframe frame;
  1563. ssize_t err;
  1564. u32 tmp;
  1565. int bpc = 8;
  1566. if (!dig || !dig->afmt)
  1567. return;
  1568. /* Silent, r600_hdmi_enable will raise WARN for us */
  1569. if (!dig->afmt->enabled)
  1570. return;
  1571. /* hdmi deep color mode general control packets setup, if bpc > 8 */
  1572. if (encoder->crtc) {
  1573. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
  1574. bpc = amdgpu_crtc->bpc;
  1575. }
  1576. /* disable audio prior to setting up hw */
  1577. dig->afmt->pin = dce_v11_0_audio_get_pin(adev);
  1578. dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
  1579. dce_v11_0_audio_set_dto(encoder, mode->clock);
  1580. tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
  1581. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1);
  1582. WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp); /* send null packets when required */
  1583. WREG32(mmAFMT_AUDIO_CRC_CONTROL + dig->afmt->offset, 0x1000);
  1584. tmp = RREG32(mmHDMI_CONTROL + dig->afmt->offset);
  1585. switch (bpc) {
  1586. case 0:
  1587. case 6:
  1588. case 8:
  1589. case 16:
  1590. default:
  1591. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 0);
  1592. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 0);
  1593. DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
  1594. connector->name, bpc);
  1595. break;
  1596. case 10:
  1597. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
  1598. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 1);
  1599. DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
  1600. connector->name);
  1601. break;
  1602. case 12:
  1603. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
  1604. tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 2);
  1605. DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
  1606. connector->name);
  1607. break;
  1608. }
  1609. WREG32(mmHDMI_CONTROL + dig->afmt->offset, tmp);
  1610. tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
  1611. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1); /* send null packets when required */
  1612. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_SEND, 1); /* send general control packets */
  1613. tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_CONT, 1); /* send general control packets every frame */
  1614. WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp);
  1615. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1616. /* enable audio info frames (frames won't be set until audio is enabled) */
  1617. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_SEND, 1);
  1618. /* required for audio info values to be updated */
  1619. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_CONT, 1);
  1620. WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1621. tmp = RREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1622. /* required for audio info values to be updated */
  1623. tmp = REG_SET_FIELD(tmp, AFMT_INFOFRAME_CONTROL0, AFMT_AUDIO_INFO_UPDATE, 1);
  1624. WREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1625. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
  1626. /* anything other than 0 */
  1627. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AUDIO_INFO_LINE, 2);
  1628. WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
  1629. WREG32(mmHDMI_GC + dig->afmt->offset, 0); /* unset HDMI_GC_AVMUTE */
  1630. tmp = RREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1631. /* set the default audio delay */
  1632. tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_DELAY_EN, 1);
  1633. /* should be suffient for all audio modes and small enough for all hblanks */
  1634. tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_PACKETS_PER_LINE, 3);
  1635. WREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1636. tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1637. /* allow 60958 channel status fields to be updated */
  1638. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_60958_CS_UPDATE, 1);
  1639. WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1640. tmp = RREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset);
  1641. if (bpc > 8)
  1642. /* clear SW CTS value */
  1643. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 0);
  1644. else
  1645. /* select SW CTS value */
  1646. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 1);
  1647. /* allow hw to sent ACR packets when required */
  1648. tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_AUTO_SEND, 1);
  1649. WREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset, tmp);
  1650. dce_v11_0_afmt_update_ACR(encoder, mode->clock);
  1651. tmp = RREG32(mmAFMT_60958_0 + dig->afmt->offset);
  1652. tmp = REG_SET_FIELD(tmp, AFMT_60958_0, AFMT_60958_CS_CHANNEL_NUMBER_L, 1);
  1653. WREG32(mmAFMT_60958_0 + dig->afmt->offset, tmp);
  1654. tmp = RREG32(mmAFMT_60958_1 + dig->afmt->offset);
  1655. tmp = REG_SET_FIELD(tmp, AFMT_60958_1, AFMT_60958_CS_CHANNEL_NUMBER_R, 2);
  1656. WREG32(mmAFMT_60958_1 + dig->afmt->offset, tmp);
  1657. tmp = RREG32(mmAFMT_60958_2 + dig->afmt->offset);
  1658. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_2, 3);
  1659. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_3, 4);
  1660. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_4, 5);
  1661. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_5, 6);
  1662. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_6, 7);
  1663. tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_7, 8);
  1664. WREG32(mmAFMT_60958_2 + dig->afmt->offset, tmp);
  1665. dce_v11_0_audio_write_speaker_allocation(encoder);
  1666. WREG32(mmAFMT_AUDIO_PACKET_CONTROL2 + dig->afmt->offset,
  1667. (0xff << AFMT_AUDIO_PACKET_CONTROL2__AFMT_AUDIO_CHANNEL_ENABLE__SHIFT));
  1668. dce_v11_0_afmt_audio_select_pin(encoder);
  1669. dce_v11_0_audio_write_sad_regs(encoder);
  1670. dce_v11_0_audio_write_latency_fields(encoder, mode);
  1671. err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
  1672. if (err < 0) {
  1673. DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
  1674. return;
  1675. }
  1676. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  1677. if (err < 0) {
  1678. DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
  1679. return;
  1680. }
  1681. dce_v11_0_afmt_update_avi_infoframe(encoder, buffer, sizeof(buffer));
  1682. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
  1683. /* enable AVI info frames */
  1684. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_SEND, 1);
  1685. /* required for audio info values to be updated */
  1686. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_CONT, 1);
  1687. WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
  1688. tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
  1689. tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AVI_INFO_LINE, 2);
  1690. WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
  1691. tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
  1692. /* send audio packets */
  1693. tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_AUDIO_SAMPLE_SEND, 1);
  1694. WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
  1695. WREG32(mmAFMT_RAMP_CONTROL0 + dig->afmt->offset, 0x00FFFFFF);
  1696. WREG32(mmAFMT_RAMP_CONTROL1 + dig->afmt->offset, 0x007FFFFF);
  1697. WREG32(mmAFMT_RAMP_CONTROL2 + dig->afmt->offset, 0x00000001);
  1698. WREG32(mmAFMT_RAMP_CONTROL3 + dig->afmt->offset, 0x00000001);
  1699. /* enable audio after to setting up hw */
  1700. dce_v11_0_audio_enable(adev, dig->afmt->pin, true);
  1701. }
  1702. static void dce_v11_0_afmt_enable(struct drm_encoder *encoder, bool enable)
  1703. {
  1704. struct drm_device *dev = encoder->dev;
  1705. struct amdgpu_device *adev = dev->dev_private;
  1706. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  1707. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  1708. if (!dig || !dig->afmt)
  1709. return;
  1710. /* Silent, r600_hdmi_enable will raise WARN for us */
  1711. if (enable && dig->afmt->enabled)
  1712. return;
  1713. if (!enable && !dig->afmt->enabled)
  1714. return;
  1715. if (!enable && dig->afmt->pin) {
  1716. dce_v11_0_audio_enable(adev, dig->afmt->pin, false);
  1717. dig->afmt->pin = NULL;
  1718. }
  1719. dig->afmt->enabled = enable;
  1720. DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
  1721. enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
  1722. }
  1723. static int dce_v11_0_afmt_init(struct amdgpu_device *adev)
  1724. {
  1725. int i;
  1726. for (i = 0; i < adev->mode_info.num_dig; i++)
  1727. adev->mode_info.afmt[i] = NULL;
  1728. /* DCE11 has audio blocks tied to DIG encoders */
  1729. for (i = 0; i < adev->mode_info.num_dig; i++) {
  1730. adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
  1731. if (adev->mode_info.afmt[i]) {
  1732. adev->mode_info.afmt[i]->offset = dig_offsets[i];
  1733. adev->mode_info.afmt[i]->id = i;
  1734. } else {
  1735. int j;
  1736. for (j = 0; j < i; j++) {
  1737. kfree(adev->mode_info.afmt[j]);
  1738. adev->mode_info.afmt[j] = NULL;
  1739. }
  1740. return -ENOMEM;
  1741. }
  1742. }
  1743. return 0;
  1744. }
  1745. static void dce_v11_0_afmt_fini(struct amdgpu_device *adev)
  1746. {
  1747. int i;
  1748. for (i = 0; i < adev->mode_info.num_dig; i++) {
  1749. kfree(adev->mode_info.afmt[i]);
  1750. adev->mode_info.afmt[i] = NULL;
  1751. }
  1752. }
  1753. static const u32 vga_control_regs[6] =
  1754. {
  1755. mmD1VGA_CONTROL,
  1756. mmD2VGA_CONTROL,
  1757. mmD3VGA_CONTROL,
  1758. mmD4VGA_CONTROL,
  1759. mmD5VGA_CONTROL,
  1760. mmD6VGA_CONTROL,
  1761. };
  1762. static void dce_v11_0_vga_enable(struct drm_crtc *crtc, bool enable)
  1763. {
  1764. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1765. struct drm_device *dev = crtc->dev;
  1766. struct amdgpu_device *adev = dev->dev_private;
  1767. u32 vga_control;
  1768. vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
  1769. if (enable)
  1770. WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
  1771. else
  1772. WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
  1773. }
  1774. static void dce_v11_0_grph_enable(struct drm_crtc *crtc, bool enable)
  1775. {
  1776. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1777. struct drm_device *dev = crtc->dev;
  1778. struct amdgpu_device *adev = dev->dev_private;
  1779. if (enable)
  1780. WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
  1781. else
  1782. WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
  1783. }
  1784. static int dce_v11_0_crtc_do_set_base(struct drm_crtc *crtc,
  1785. struct drm_framebuffer *fb,
  1786. int x, int y, int atomic)
  1787. {
  1788. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  1789. struct drm_device *dev = crtc->dev;
  1790. struct amdgpu_device *adev = dev->dev_private;
  1791. struct amdgpu_framebuffer *amdgpu_fb;
  1792. struct drm_framebuffer *target_fb;
  1793. struct drm_gem_object *obj;
  1794. struct amdgpu_bo *rbo;
  1795. uint64_t fb_location, tiling_flags;
  1796. uint32_t fb_format, fb_pitch_pixels;
  1797. u32 fb_swap = REG_SET_FIELD(0, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP, ENDIAN_NONE);
  1798. u32 pipe_config;
  1799. u32 tmp, viewport_w, viewport_h;
  1800. int r;
  1801. bool bypass_lut = false;
  1802. char *format_name;
  1803. /* no fb bound */
  1804. if (!atomic && !crtc->primary->fb) {
  1805. DRM_DEBUG_KMS("No FB bound\n");
  1806. return 0;
  1807. }
  1808. if (atomic) {
  1809. amdgpu_fb = to_amdgpu_framebuffer(fb);
  1810. target_fb = fb;
  1811. } else {
  1812. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  1813. target_fb = crtc->primary->fb;
  1814. }
  1815. /* If atomic, assume fb object is pinned & idle & fenced and
  1816. * just update base pointers
  1817. */
  1818. obj = amdgpu_fb->obj;
  1819. rbo = gem_to_amdgpu_bo(obj);
  1820. r = amdgpu_bo_reserve(rbo, false);
  1821. if (unlikely(r != 0))
  1822. return r;
  1823. if (atomic) {
  1824. fb_location = amdgpu_bo_gpu_offset(rbo);
  1825. } else {
  1826. r = amdgpu_bo_pin(rbo, AMDGPU_GEM_DOMAIN_VRAM, &fb_location);
  1827. if (unlikely(r != 0)) {
  1828. amdgpu_bo_unreserve(rbo);
  1829. return -EINVAL;
  1830. }
  1831. }
  1832. amdgpu_bo_get_tiling_flags(rbo, &tiling_flags);
  1833. amdgpu_bo_unreserve(rbo);
  1834. pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
  1835. switch (target_fb->pixel_format) {
  1836. case DRM_FORMAT_C8:
  1837. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 0);
  1838. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1839. break;
  1840. case DRM_FORMAT_XRGB4444:
  1841. case DRM_FORMAT_ARGB4444:
  1842. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1843. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 2);
  1844. #ifdef __BIG_ENDIAN
  1845. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1846. ENDIAN_8IN16);
  1847. #endif
  1848. break;
  1849. case DRM_FORMAT_XRGB1555:
  1850. case DRM_FORMAT_ARGB1555:
  1851. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1852. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1853. #ifdef __BIG_ENDIAN
  1854. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1855. ENDIAN_8IN16);
  1856. #endif
  1857. break;
  1858. case DRM_FORMAT_BGRX5551:
  1859. case DRM_FORMAT_BGRA5551:
  1860. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1861. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 5);
  1862. #ifdef __BIG_ENDIAN
  1863. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1864. ENDIAN_8IN16);
  1865. #endif
  1866. break;
  1867. case DRM_FORMAT_RGB565:
  1868. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
  1869. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
  1870. #ifdef __BIG_ENDIAN
  1871. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1872. ENDIAN_8IN16);
  1873. #endif
  1874. break;
  1875. case DRM_FORMAT_XRGB8888:
  1876. case DRM_FORMAT_ARGB8888:
  1877. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1878. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
  1879. #ifdef __BIG_ENDIAN
  1880. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1881. ENDIAN_8IN32);
  1882. #endif
  1883. break;
  1884. case DRM_FORMAT_XRGB2101010:
  1885. case DRM_FORMAT_ARGB2101010:
  1886. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1887. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
  1888. #ifdef __BIG_ENDIAN
  1889. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1890. ENDIAN_8IN32);
  1891. #endif
  1892. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1893. bypass_lut = true;
  1894. break;
  1895. case DRM_FORMAT_BGRX1010102:
  1896. case DRM_FORMAT_BGRA1010102:
  1897. fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
  1898. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 4);
  1899. #ifdef __BIG_ENDIAN
  1900. fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
  1901. ENDIAN_8IN32);
  1902. #endif
  1903. /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
  1904. bypass_lut = true;
  1905. break;
  1906. default:
  1907. format_name = drm_get_format_name(target_fb->pixel_format);
  1908. DRM_ERROR("Unsupported screen format %s\n", format_name);
  1909. kfree(format_name);
  1910. return -EINVAL;
  1911. }
  1912. if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
  1913. unsigned bankw, bankh, mtaspect, tile_split, num_banks;
  1914. bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
  1915. bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
  1916. mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
  1917. tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
  1918. num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
  1919. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_NUM_BANKS, num_banks);
  1920. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
  1921. ARRAY_2D_TILED_THIN1);
  1922. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_TILE_SPLIT,
  1923. tile_split);
  1924. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_WIDTH, bankw);
  1925. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_HEIGHT, bankh);
  1926. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MACRO_TILE_ASPECT,
  1927. mtaspect);
  1928. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MICRO_TILE_MODE,
  1929. ADDR_SURF_MICRO_TILING_DISPLAY);
  1930. } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1) {
  1931. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
  1932. ARRAY_1D_TILED_THIN1);
  1933. }
  1934. fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_PIPE_CONFIG,
  1935. pipe_config);
  1936. dce_v11_0_vga_enable(crtc, false);
  1937. /* Make sure surface address is updated at vertical blank rather than
  1938. * horizontal blank
  1939. */
  1940. tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
  1941. tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
  1942. GRPH_SURFACE_UPDATE_H_RETRACE_EN, 0);
  1943. WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  1944. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  1945. upper_32_bits(fb_location));
  1946. WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  1947. upper_32_bits(fb_location));
  1948. WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  1949. (u32)fb_location & GRPH_PRIMARY_SURFACE_ADDRESS__GRPH_PRIMARY_SURFACE_ADDRESS_MASK);
  1950. WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  1951. (u32) fb_location & GRPH_SECONDARY_SURFACE_ADDRESS__GRPH_SECONDARY_SURFACE_ADDRESS_MASK);
  1952. WREG32(mmGRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
  1953. WREG32(mmGRPH_SWAP_CNTL + amdgpu_crtc->crtc_offset, fb_swap);
  1954. /*
  1955. * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
  1956. * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
  1957. * retain the full precision throughout the pipeline.
  1958. */
  1959. tmp = RREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset);
  1960. if (bypass_lut)
  1961. tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 1);
  1962. else
  1963. tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 0);
  1964. WREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset, tmp);
  1965. if (bypass_lut)
  1966. DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
  1967. WREG32(mmGRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
  1968. WREG32(mmGRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
  1969. WREG32(mmGRPH_X_START + amdgpu_crtc->crtc_offset, 0);
  1970. WREG32(mmGRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
  1971. WREG32(mmGRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
  1972. WREG32(mmGRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
  1973. fb_pitch_pixels = target_fb->pitches[0] / (target_fb->bits_per_pixel / 8);
  1974. WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
  1975. dce_v11_0_grph_enable(crtc, true);
  1976. WREG32(mmLB_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
  1977. target_fb->height);
  1978. x &= ~3;
  1979. y &= ~1;
  1980. WREG32(mmVIEWPORT_START + amdgpu_crtc->crtc_offset,
  1981. (x << 16) | y);
  1982. viewport_w = crtc->mode.hdisplay;
  1983. viewport_h = (crtc->mode.vdisplay + 1) & ~1;
  1984. WREG32(mmVIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
  1985. (viewport_w << 16) | viewport_h);
  1986. /* set pageflip to happen only at start of vblank interval (front porch) */
  1987. WREG32(mmCRTC_MASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 3);
  1988. if (!atomic && fb && fb != crtc->primary->fb) {
  1989. amdgpu_fb = to_amdgpu_framebuffer(fb);
  1990. rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  1991. r = amdgpu_bo_reserve(rbo, false);
  1992. if (unlikely(r != 0))
  1993. return r;
  1994. amdgpu_bo_unpin(rbo);
  1995. amdgpu_bo_unreserve(rbo);
  1996. }
  1997. /* Bytes per pixel may have changed */
  1998. dce_v11_0_bandwidth_update(adev);
  1999. return 0;
  2000. }
  2001. static void dce_v11_0_set_interleave(struct drm_crtc *crtc,
  2002. struct drm_display_mode *mode)
  2003. {
  2004. struct drm_device *dev = crtc->dev;
  2005. struct amdgpu_device *adev = dev->dev_private;
  2006. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2007. u32 tmp;
  2008. tmp = RREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset);
  2009. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  2010. tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 1);
  2011. else
  2012. tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 0);
  2013. WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset, tmp);
  2014. }
  2015. static void dce_v11_0_crtc_load_lut(struct drm_crtc *crtc)
  2016. {
  2017. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2018. struct drm_device *dev = crtc->dev;
  2019. struct amdgpu_device *adev = dev->dev_private;
  2020. int i;
  2021. u32 tmp;
  2022. DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
  2023. tmp = RREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
  2024. tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_GRPH_MODE, 0);
  2025. WREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2026. tmp = RREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset);
  2027. tmp = REG_SET_FIELD(tmp, PRESCALE_GRPH_CONTROL, GRPH_PRESCALE_BYPASS, 1);
  2028. WREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2029. tmp = RREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  2030. tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, GRPH_INPUT_GAMMA_MODE, 0);
  2031. WREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2032. WREG32(mmDC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
  2033. WREG32(mmDC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
  2034. WREG32(mmDC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
  2035. WREG32(mmDC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
  2036. WREG32(mmDC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
  2037. WREG32(mmDC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
  2038. WREG32(mmDC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
  2039. WREG32(mmDC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
  2040. WREG32(mmDC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
  2041. WREG32(mmDC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
  2042. for (i = 0; i < 256; i++) {
  2043. WREG32(mmDC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
  2044. (amdgpu_crtc->lut_r[i] << 20) |
  2045. (amdgpu_crtc->lut_g[i] << 10) |
  2046. (amdgpu_crtc->lut_b[i] << 0));
  2047. }
  2048. tmp = RREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  2049. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, GRPH_DEGAMMA_MODE, 0);
  2050. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR_DEGAMMA_MODE, 0);
  2051. tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR2_DEGAMMA_MODE, 0);
  2052. WREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2053. tmp = RREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset);
  2054. tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, GRPH_GAMUT_REMAP_MODE, 0);
  2055. WREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2056. tmp = RREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
  2057. tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, GRPH_REGAMMA_MODE, 0);
  2058. WREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2059. tmp = RREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
  2060. tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_GRPH_MODE, 0);
  2061. WREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2062. /* XXX match this to the depth of the crtc fmt block, move to modeset? */
  2063. WREG32(mmDENORM_CONTROL + amdgpu_crtc->crtc_offset, 0);
  2064. /* XXX this only needs to be programmed once per crtc at startup,
  2065. * not sure where the best place for it is
  2066. */
  2067. tmp = RREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset);
  2068. tmp = REG_SET_FIELD(tmp, ALPHA_CONTROL, CURSOR_ALPHA_BLND_ENA, 1);
  2069. WREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2070. }
  2071. static int dce_v11_0_pick_dig_encoder(struct drm_encoder *encoder)
  2072. {
  2073. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2074. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  2075. switch (amdgpu_encoder->encoder_id) {
  2076. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  2077. if (dig->linkb)
  2078. return 1;
  2079. else
  2080. return 0;
  2081. break;
  2082. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  2083. if (dig->linkb)
  2084. return 3;
  2085. else
  2086. return 2;
  2087. break;
  2088. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  2089. if (dig->linkb)
  2090. return 5;
  2091. else
  2092. return 4;
  2093. break;
  2094. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
  2095. return 6;
  2096. break;
  2097. default:
  2098. DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
  2099. return 0;
  2100. }
  2101. }
  2102. /**
  2103. * dce_v11_0_pick_pll - Allocate a PPLL for use by the crtc.
  2104. *
  2105. * @crtc: drm crtc
  2106. *
  2107. * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
  2108. * a single PPLL can be used for all DP crtcs/encoders. For non-DP
  2109. * monitors a dedicated PPLL must be used. If a particular board has
  2110. * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
  2111. * as there is no need to program the PLL itself. If we are not able to
  2112. * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
  2113. * avoid messing up an existing monitor.
  2114. *
  2115. * Asic specific PLL information
  2116. *
  2117. * DCE 10.x
  2118. * Tonga
  2119. * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
  2120. * CI
  2121. * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
  2122. *
  2123. */
  2124. static u32 dce_v11_0_pick_pll(struct drm_crtc *crtc)
  2125. {
  2126. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2127. struct drm_device *dev = crtc->dev;
  2128. struct amdgpu_device *adev = dev->dev_private;
  2129. u32 pll_in_use;
  2130. int pll;
  2131. if ((adev->asic_type == CHIP_POLARIS10) ||
  2132. (adev->asic_type == CHIP_POLARIS11)) {
  2133. struct amdgpu_encoder *amdgpu_encoder =
  2134. to_amdgpu_encoder(amdgpu_crtc->encoder);
  2135. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  2136. if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
  2137. return ATOM_DP_DTO;
  2138. switch (amdgpu_encoder->encoder_id) {
  2139. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  2140. if (dig->linkb)
  2141. return ATOM_COMBOPHY_PLL1;
  2142. else
  2143. return ATOM_COMBOPHY_PLL0;
  2144. break;
  2145. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  2146. if (dig->linkb)
  2147. return ATOM_COMBOPHY_PLL3;
  2148. else
  2149. return ATOM_COMBOPHY_PLL2;
  2150. break;
  2151. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  2152. if (dig->linkb)
  2153. return ATOM_COMBOPHY_PLL5;
  2154. else
  2155. return ATOM_COMBOPHY_PLL4;
  2156. break;
  2157. default:
  2158. DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
  2159. return ATOM_PPLL_INVALID;
  2160. }
  2161. }
  2162. if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
  2163. if (adev->clock.dp_extclk)
  2164. /* skip PPLL programming if using ext clock */
  2165. return ATOM_PPLL_INVALID;
  2166. else {
  2167. /* use the same PPLL for all DP monitors */
  2168. pll = amdgpu_pll_get_shared_dp_ppll(crtc);
  2169. if (pll != ATOM_PPLL_INVALID)
  2170. return pll;
  2171. }
  2172. } else {
  2173. /* use the same PPLL for all monitors with the same clock */
  2174. pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
  2175. if (pll != ATOM_PPLL_INVALID)
  2176. return pll;
  2177. }
  2178. /* XXX need to determine what plls are available on each DCE11 part */
  2179. pll_in_use = amdgpu_pll_get_use_mask(crtc);
  2180. if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY) {
  2181. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  2182. return ATOM_PPLL1;
  2183. if (!(pll_in_use & (1 << ATOM_PPLL0)))
  2184. return ATOM_PPLL0;
  2185. DRM_ERROR("unable to allocate a PPLL\n");
  2186. return ATOM_PPLL_INVALID;
  2187. } else {
  2188. if (!(pll_in_use & (1 << ATOM_PPLL2)))
  2189. return ATOM_PPLL2;
  2190. if (!(pll_in_use & (1 << ATOM_PPLL1)))
  2191. return ATOM_PPLL1;
  2192. if (!(pll_in_use & (1 << ATOM_PPLL0)))
  2193. return ATOM_PPLL0;
  2194. DRM_ERROR("unable to allocate a PPLL\n");
  2195. return ATOM_PPLL_INVALID;
  2196. }
  2197. return ATOM_PPLL_INVALID;
  2198. }
  2199. static void dce_v11_0_lock_cursor(struct drm_crtc *crtc, bool lock)
  2200. {
  2201. struct amdgpu_device *adev = crtc->dev->dev_private;
  2202. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2203. uint32_t cur_lock;
  2204. cur_lock = RREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset);
  2205. if (lock)
  2206. cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 1);
  2207. else
  2208. cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 0);
  2209. WREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
  2210. }
  2211. static void dce_v11_0_hide_cursor(struct drm_crtc *crtc)
  2212. {
  2213. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2214. struct amdgpu_device *adev = crtc->dev->dev_private;
  2215. u32 tmp;
  2216. tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
  2217. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 0);
  2218. WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2219. }
  2220. static void dce_v11_0_show_cursor(struct drm_crtc *crtc)
  2221. {
  2222. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2223. struct amdgpu_device *adev = crtc->dev->dev_private;
  2224. u32 tmp;
  2225. WREG32(mmCUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
  2226. upper_32_bits(amdgpu_crtc->cursor_addr));
  2227. WREG32(mmCUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
  2228. lower_32_bits(amdgpu_crtc->cursor_addr));
  2229. tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
  2230. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 1);
  2231. tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_MODE, 2);
  2232. WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
  2233. }
  2234. static int dce_v11_0_cursor_move_locked(struct drm_crtc *crtc,
  2235. int x, int y)
  2236. {
  2237. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2238. struct amdgpu_device *adev = crtc->dev->dev_private;
  2239. int xorigin = 0, yorigin = 0;
  2240. /* avivo cursor are offset into the total surface */
  2241. x += crtc->x;
  2242. y += crtc->y;
  2243. DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
  2244. if (x < 0) {
  2245. xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
  2246. x = 0;
  2247. }
  2248. if (y < 0) {
  2249. yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
  2250. y = 0;
  2251. }
  2252. WREG32(mmCUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
  2253. WREG32(mmCUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
  2254. WREG32(mmCUR_SIZE + amdgpu_crtc->crtc_offset,
  2255. ((amdgpu_crtc->cursor_width - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
  2256. amdgpu_crtc->cursor_x = x;
  2257. amdgpu_crtc->cursor_y = y;
  2258. return 0;
  2259. }
  2260. static int dce_v11_0_crtc_cursor_move(struct drm_crtc *crtc,
  2261. int x, int y)
  2262. {
  2263. int ret;
  2264. dce_v11_0_lock_cursor(crtc, true);
  2265. ret = dce_v11_0_cursor_move_locked(crtc, x, y);
  2266. dce_v11_0_lock_cursor(crtc, false);
  2267. return ret;
  2268. }
  2269. static int dce_v11_0_crtc_cursor_set2(struct drm_crtc *crtc,
  2270. struct drm_file *file_priv,
  2271. uint32_t handle,
  2272. uint32_t width,
  2273. uint32_t height,
  2274. int32_t hot_x,
  2275. int32_t hot_y)
  2276. {
  2277. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2278. struct drm_gem_object *obj;
  2279. struct amdgpu_bo *aobj;
  2280. int ret;
  2281. if (!handle) {
  2282. /* turn off cursor */
  2283. dce_v11_0_hide_cursor(crtc);
  2284. obj = NULL;
  2285. goto unpin;
  2286. }
  2287. if ((width > amdgpu_crtc->max_cursor_width) ||
  2288. (height > amdgpu_crtc->max_cursor_height)) {
  2289. DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
  2290. return -EINVAL;
  2291. }
  2292. obj = drm_gem_object_lookup(file_priv, handle);
  2293. if (!obj) {
  2294. DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
  2295. return -ENOENT;
  2296. }
  2297. aobj = gem_to_amdgpu_bo(obj);
  2298. ret = amdgpu_bo_reserve(aobj, false);
  2299. if (ret != 0) {
  2300. drm_gem_object_unreference_unlocked(obj);
  2301. return ret;
  2302. }
  2303. ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM, &amdgpu_crtc->cursor_addr);
  2304. amdgpu_bo_unreserve(aobj);
  2305. if (ret) {
  2306. DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
  2307. drm_gem_object_unreference_unlocked(obj);
  2308. return ret;
  2309. }
  2310. amdgpu_crtc->cursor_width = width;
  2311. amdgpu_crtc->cursor_height = height;
  2312. dce_v11_0_lock_cursor(crtc, true);
  2313. if (hot_x != amdgpu_crtc->cursor_hot_x ||
  2314. hot_y != amdgpu_crtc->cursor_hot_y) {
  2315. int x, y;
  2316. x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
  2317. y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
  2318. dce_v11_0_cursor_move_locked(crtc, x, y);
  2319. amdgpu_crtc->cursor_hot_x = hot_x;
  2320. amdgpu_crtc->cursor_hot_y = hot_y;
  2321. }
  2322. dce_v11_0_show_cursor(crtc);
  2323. dce_v11_0_lock_cursor(crtc, false);
  2324. unpin:
  2325. if (amdgpu_crtc->cursor_bo) {
  2326. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2327. ret = amdgpu_bo_reserve(aobj, false);
  2328. if (likely(ret == 0)) {
  2329. amdgpu_bo_unpin(aobj);
  2330. amdgpu_bo_unreserve(aobj);
  2331. }
  2332. drm_gem_object_unreference_unlocked(amdgpu_crtc->cursor_bo);
  2333. }
  2334. amdgpu_crtc->cursor_bo = obj;
  2335. return 0;
  2336. }
  2337. static void dce_v11_0_cursor_reset(struct drm_crtc *crtc)
  2338. {
  2339. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2340. if (amdgpu_crtc->cursor_bo) {
  2341. dce_v11_0_lock_cursor(crtc, true);
  2342. dce_v11_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
  2343. amdgpu_crtc->cursor_y);
  2344. dce_v11_0_show_cursor(crtc);
  2345. dce_v11_0_lock_cursor(crtc, false);
  2346. }
  2347. }
  2348. static int dce_v11_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  2349. u16 *blue, uint32_t size)
  2350. {
  2351. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2352. int i;
  2353. /* userspace palettes are always correct as is */
  2354. for (i = 0; i < size; i++) {
  2355. amdgpu_crtc->lut_r[i] = red[i] >> 6;
  2356. amdgpu_crtc->lut_g[i] = green[i] >> 6;
  2357. amdgpu_crtc->lut_b[i] = blue[i] >> 6;
  2358. }
  2359. dce_v11_0_crtc_load_lut(crtc);
  2360. return 0;
  2361. }
  2362. static void dce_v11_0_crtc_destroy(struct drm_crtc *crtc)
  2363. {
  2364. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2365. drm_crtc_cleanup(crtc);
  2366. kfree(amdgpu_crtc);
  2367. }
  2368. static const struct drm_crtc_funcs dce_v11_0_crtc_funcs = {
  2369. .cursor_set2 = dce_v11_0_crtc_cursor_set2,
  2370. .cursor_move = dce_v11_0_crtc_cursor_move,
  2371. .gamma_set = dce_v11_0_crtc_gamma_set,
  2372. .set_config = amdgpu_crtc_set_config,
  2373. .destroy = dce_v11_0_crtc_destroy,
  2374. .page_flip = amdgpu_crtc_page_flip,
  2375. };
  2376. static void dce_v11_0_crtc_dpms(struct drm_crtc *crtc, int mode)
  2377. {
  2378. struct drm_device *dev = crtc->dev;
  2379. struct amdgpu_device *adev = dev->dev_private;
  2380. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2381. unsigned type;
  2382. switch (mode) {
  2383. case DRM_MODE_DPMS_ON:
  2384. amdgpu_crtc->enabled = true;
  2385. amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
  2386. dce_v11_0_vga_enable(crtc, true);
  2387. amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
  2388. dce_v11_0_vga_enable(crtc, false);
  2389. /* Make sure VBLANK and PFLIP interrupts are still enabled */
  2390. type = amdgpu_crtc_idx_to_irq_type(adev, amdgpu_crtc->crtc_id);
  2391. amdgpu_irq_update(adev, &adev->crtc_irq, type);
  2392. amdgpu_irq_update(adev, &adev->pageflip_irq, type);
  2393. drm_crtc_vblank_on(crtc);
  2394. dce_v11_0_crtc_load_lut(crtc);
  2395. break;
  2396. case DRM_MODE_DPMS_STANDBY:
  2397. case DRM_MODE_DPMS_SUSPEND:
  2398. case DRM_MODE_DPMS_OFF:
  2399. drm_crtc_vblank_off(crtc);
  2400. if (amdgpu_crtc->enabled) {
  2401. dce_v11_0_vga_enable(crtc, true);
  2402. amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
  2403. dce_v11_0_vga_enable(crtc, false);
  2404. }
  2405. amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
  2406. amdgpu_crtc->enabled = false;
  2407. break;
  2408. }
  2409. /* adjust pm to dpms */
  2410. amdgpu_pm_compute_clocks(adev);
  2411. }
  2412. static void dce_v11_0_crtc_prepare(struct drm_crtc *crtc)
  2413. {
  2414. /* disable crtc pair power gating before programming */
  2415. amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
  2416. amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
  2417. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  2418. }
  2419. static void dce_v11_0_crtc_commit(struct drm_crtc *crtc)
  2420. {
  2421. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  2422. amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
  2423. }
  2424. static void dce_v11_0_crtc_disable(struct drm_crtc *crtc)
  2425. {
  2426. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2427. struct drm_device *dev = crtc->dev;
  2428. struct amdgpu_device *adev = dev->dev_private;
  2429. struct amdgpu_atom_ss ss;
  2430. int i;
  2431. dce_v11_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  2432. if (crtc->primary->fb) {
  2433. int r;
  2434. struct amdgpu_framebuffer *amdgpu_fb;
  2435. struct amdgpu_bo *rbo;
  2436. amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
  2437. rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  2438. r = amdgpu_bo_reserve(rbo, false);
  2439. if (unlikely(r))
  2440. DRM_ERROR("failed to reserve rbo before unpin\n");
  2441. else {
  2442. amdgpu_bo_unpin(rbo);
  2443. amdgpu_bo_unreserve(rbo);
  2444. }
  2445. }
  2446. /* disable the GRPH */
  2447. dce_v11_0_grph_enable(crtc, false);
  2448. amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
  2449. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2450. if (adev->mode_info.crtcs[i] &&
  2451. adev->mode_info.crtcs[i]->enabled &&
  2452. i != amdgpu_crtc->crtc_id &&
  2453. amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
  2454. /* one other crtc is using this pll don't turn
  2455. * off the pll
  2456. */
  2457. goto done;
  2458. }
  2459. }
  2460. switch (amdgpu_crtc->pll_id) {
  2461. case ATOM_PPLL0:
  2462. case ATOM_PPLL1:
  2463. case ATOM_PPLL2:
  2464. /* disable the ppll */
  2465. amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
  2466. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  2467. break;
  2468. case ATOM_COMBOPHY_PLL0:
  2469. case ATOM_COMBOPHY_PLL1:
  2470. case ATOM_COMBOPHY_PLL2:
  2471. case ATOM_COMBOPHY_PLL3:
  2472. case ATOM_COMBOPHY_PLL4:
  2473. case ATOM_COMBOPHY_PLL5:
  2474. /* disable the ppll */
  2475. amdgpu_atombios_crtc_program_pll(crtc, ATOM_CRTC_INVALID, amdgpu_crtc->pll_id,
  2476. 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
  2477. break;
  2478. default:
  2479. break;
  2480. }
  2481. done:
  2482. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  2483. amdgpu_crtc->adjusted_clock = 0;
  2484. amdgpu_crtc->encoder = NULL;
  2485. amdgpu_crtc->connector = NULL;
  2486. }
  2487. static int dce_v11_0_crtc_mode_set(struct drm_crtc *crtc,
  2488. struct drm_display_mode *mode,
  2489. struct drm_display_mode *adjusted_mode,
  2490. int x, int y, struct drm_framebuffer *old_fb)
  2491. {
  2492. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2493. struct drm_device *dev = crtc->dev;
  2494. struct amdgpu_device *adev = dev->dev_private;
  2495. if (!amdgpu_crtc->adjusted_clock)
  2496. return -EINVAL;
  2497. if ((adev->asic_type == CHIP_POLARIS10) ||
  2498. (adev->asic_type == CHIP_POLARIS11)) {
  2499. struct amdgpu_encoder *amdgpu_encoder =
  2500. to_amdgpu_encoder(amdgpu_crtc->encoder);
  2501. int encoder_mode =
  2502. amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder);
  2503. /* SetPixelClock calculates the plls and ss values now */
  2504. amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id,
  2505. amdgpu_crtc->pll_id,
  2506. encoder_mode, amdgpu_encoder->encoder_id,
  2507. adjusted_mode->clock, 0, 0, 0, 0,
  2508. amdgpu_crtc->bpc, amdgpu_crtc->ss_enabled, &amdgpu_crtc->ss);
  2509. } else {
  2510. amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
  2511. }
  2512. amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
  2513. dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  2514. amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
  2515. amdgpu_atombios_crtc_scaler_setup(crtc);
  2516. dce_v11_0_cursor_reset(crtc);
  2517. /* update the hw version fpr dpm */
  2518. amdgpu_crtc->hw_mode = *adjusted_mode;
  2519. return 0;
  2520. }
  2521. static bool dce_v11_0_crtc_mode_fixup(struct drm_crtc *crtc,
  2522. const struct drm_display_mode *mode,
  2523. struct drm_display_mode *adjusted_mode)
  2524. {
  2525. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2526. struct drm_device *dev = crtc->dev;
  2527. struct drm_encoder *encoder;
  2528. /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
  2529. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  2530. if (encoder->crtc == crtc) {
  2531. amdgpu_crtc->encoder = encoder;
  2532. amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
  2533. break;
  2534. }
  2535. }
  2536. if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
  2537. amdgpu_crtc->encoder = NULL;
  2538. amdgpu_crtc->connector = NULL;
  2539. return false;
  2540. }
  2541. if (!amdgpu_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
  2542. return false;
  2543. if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
  2544. return false;
  2545. /* pick pll */
  2546. amdgpu_crtc->pll_id = dce_v11_0_pick_pll(crtc);
  2547. /* if we can't get a PPLL for a non-DP encoder, fail */
  2548. if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
  2549. !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
  2550. return false;
  2551. return true;
  2552. }
  2553. static int dce_v11_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
  2554. struct drm_framebuffer *old_fb)
  2555. {
  2556. return dce_v11_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
  2557. }
  2558. static int dce_v11_0_crtc_set_base_atomic(struct drm_crtc *crtc,
  2559. struct drm_framebuffer *fb,
  2560. int x, int y, enum mode_set_atomic state)
  2561. {
  2562. return dce_v11_0_crtc_do_set_base(crtc, fb, x, y, 1);
  2563. }
  2564. static const struct drm_crtc_helper_funcs dce_v11_0_crtc_helper_funcs = {
  2565. .dpms = dce_v11_0_crtc_dpms,
  2566. .mode_fixup = dce_v11_0_crtc_mode_fixup,
  2567. .mode_set = dce_v11_0_crtc_mode_set,
  2568. .mode_set_base = dce_v11_0_crtc_set_base,
  2569. .mode_set_base_atomic = dce_v11_0_crtc_set_base_atomic,
  2570. .prepare = dce_v11_0_crtc_prepare,
  2571. .commit = dce_v11_0_crtc_commit,
  2572. .load_lut = dce_v11_0_crtc_load_lut,
  2573. .disable = dce_v11_0_crtc_disable,
  2574. };
  2575. static int dce_v11_0_crtc_init(struct amdgpu_device *adev, int index)
  2576. {
  2577. struct amdgpu_crtc *amdgpu_crtc;
  2578. int i;
  2579. amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
  2580. (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  2581. if (amdgpu_crtc == NULL)
  2582. return -ENOMEM;
  2583. drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_v11_0_crtc_funcs);
  2584. drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
  2585. amdgpu_crtc->crtc_id = index;
  2586. adev->mode_info.crtcs[index] = amdgpu_crtc;
  2587. amdgpu_crtc->max_cursor_width = 128;
  2588. amdgpu_crtc->max_cursor_height = 128;
  2589. adev->ddev->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
  2590. adev->ddev->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
  2591. for (i = 0; i < 256; i++) {
  2592. amdgpu_crtc->lut_r[i] = i << 2;
  2593. amdgpu_crtc->lut_g[i] = i << 2;
  2594. amdgpu_crtc->lut_b[i] = i << 2;
  2595. }
  2596. switch (amdgpu_crtc->crtc_id) {
  2597. case 0:
  2598. default:
  2599. amdgpu_crtc->crtc_offset = CRTC0_REGISTER_OFFSET;
  2600. break;
  2601. case 1:
  2602. amdgpu_crtc->crtc_offset = CRTC1_REGISTER_OFFSET;
  2603. break;
  2604. case 2:
  2605. amdgpu_crtc->crtc_offset = CRTC2_REGISTER_OFFSET;
  2606. break;
  2607. case 3:
  2608. amdgpu_crtc->crtc_offset = CRTC3_REGISTER_OFFSET;
  2609. break;
  2610. case 4:
  2611. amdgpu_crtc->crtc_offset = CRTC4_REGISTER_OFFSET;
  2612. break;
  2613. case 5:
  2614. amdgpu_crtc->crtc_offset = CRTC5_REGISTER_OFFSET;
  2615. break;
  2616. }
  2617. amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
  2618. amdgpu_crtc->adjusted_clock = 0;
  2619. amdgpu_crtc->encoder = NULL;
  2620. amdgpu_crtc->connector = NULL;
  2621. drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v11_0_crtc_helper_funcs);
  2622. return 0;
  2623. }
  2624. static int dce_v11_0_early_init(void *handle)
  2625. {
  2626. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2627. adev->audio_endpt_rreg = &dce_v11_0_audio_endpt_rreg;
  2628. adev->audio_endpt_wreg = &dce_v11_0_audio_endpt_wreg;
  2629. dce_v11_0_set_display_funcs(adev);
  2630. dce_v11_0_set_irq_funcs(adev);
  2631. switch (adev->asic_type) {
  2632. case CHIP_CARRIZO:
  2633. adev->mode_info.num_crtc = 3;
  2634. adev->mode_info.num_hpd = 6;
  2635. adev->mode_info.num_dig = 9;
  2636. break;
  2637. case CHIP_STONEY:
  2638. adev->mode_info.num_crtc = 2;
  2639. adev->mode_info.num_hpd = 6;
  2640. adev->mode_info.num_dig = 9;
  2641. break;
  2642. case CHIP_POLARIS10:
  2643. adev->mode_info.num_crtc = 6;
  2644. adev->mode_info.num_hpd = 6;
  2645. adev->mode_info.num_dig = 6;
  2646. break;
  2647. case CHIP_POLARIS11:
  2648. adev->mode_info.num_crtc = 5;
  2649. adev->mode_info.num_hpd = 5;
  2650. adev->mode_info.num_dig = 5;
  2651. break;
  2652. default:
  2653. /* FIXME: not supported yet */
  2654. return -EINVAL;
  2655. }
  2656. return 0;
  2657. }
  2658. static int dce_v11_0_sw_init(void *handle)
  2659. {
  2660. int r, i;
  2661. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2662. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2663. r = amdgpu_irq_add_id(adev, i + 1, &adev->crtc_irq);
  2664. if (r)
  2665. return r;
  2666. }
  2667. for (i = 8; i < 20; i += 2) {
  2668. r = amdgpu_irq_add_id(adev, i, &adev->pageflip_irq);
  2669. if (r)
  2670. return r;
  2671. }
  2672. /* HPD hotplug */
  2673. r = amdgpu_irq_add_id(adev, 42, &adev->hpd_irq);
  2674. if (r)
  2675. return r;
  2676. adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
  2677. adev->ddev->mode_config.async_page_flip = true;
  2678. adev->ddev->mode_config.max_width = 16384;
  2679. adev->ddev->mode_config.max_height = 16384;
  2680. adev->ddev->mode_config.preferred_depth = 24;
  2681. adev->ddev->mode_config.prefer_shadow = 1;
  2682. adev->ddev->mode_config.fb_base = adev->mc.aper_base;
  2683. r = amdgpu_modeset_create_props(adev);
  2684. if (r)
  2685. return r;
  2686. adev->ddev->mode_config.max_width = 16384;
  2687. adev->ddev->mode_config.max_height = 16384;
  2688. /* allocate crtcs */
  2689. for (i = 0; i < adev->mode_info.num_crtc; i++) {
  2690. r = dce_v11_0_crtc_init(adev, i);
  2691. if (r)
  2692. return r;
  2693. }
  2694. if (amdgpu_atombios_get_connector_info_from_object_table(adev))
  2695. amdgpu_print_display_setup(adev->ddev);
  2696. else
  2697. return -EINVAL;
  2698. /* setup afmt */
  2699. r = dce_v11_0_afmt_init(adev);
  2700. if (r)
  2701. return r;
  2702. r = dce_v11_0_audio_init(adev);
  2703. if (r)
  2704. return r;
  2705. drm_kms_helper_poll_init(adev->ddev);
  2706. adev->mode_info.mode_config_initialized = true;
  2707. return 0;
  2708. }
  2709. static int dce_v11_0_sw_fini(void *handle)
  2710. {
  2711. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2712. kfree(adev->mode_info.bios_hardcoded_edid);
  2713. drm_kms_helper_poll_fini(adev->ddev);
  2714. dce_v11_0_audio_fini(adev);
  2715. dce_v11_0_afmt_fini(adev);
  2716. adev->mode_info.mode_config_initialized = false;
  2717. return 0;
  2718. }
  2719. static int dce_v11_0_hw_init(void *handle)
  2720. {
  2721. int i;
  2722. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2723. dce_v11_0_init_golden_registers(adev);
  2724. /* init dig PHYs, disp eng pll */
  2725. amdgpu_atombios_crtc_powergate_init(adev);
  2726. amdgpu_atombios_encoder_init_dig(adev);
  2727. if ((adev->asic_type == CHIP_POLARIS10) ||
  2728. (adev->asic_type == CHIP_POLARIS11)) {
  2729. amdgpu_atombios_crtc_set_dce_clock(adev, adev->clock.default_dispclk,
  2730. DCE_CLOCK_TYPE_DISPCLK, ATOM_GCK_DFS);
  2731. amdgpu_atombios_crtc_set_dce_clock(adev, 0,
  2732. DCE_CLOCK_TYPE_DPREFCLK, ATOM_GCK_DFS);
  2733. } else {
  2734. amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
  2735. }
  2736. /* initialize hpd */
  2737. dce_v11_0_hpd_init(adev);
  2738. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2739. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2740. }
  2741. dce_v11_0_pageflip_interrupt_init(adev);
  2742. return 0;
  2743. }
  2744. static int dce_v11_0_hw_fini(void *handle)
  2745. {
  2746. int i;
  2747. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2748. dce_v11_0_hpd_fini(adev);
  2749. for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
  2750. dce_v11_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
  2751. }
  2752. dce_v11_0_pageflip_interrupt_fini(adev);
  2753. return 0;
  2754. }
  2755. static int dce_v11_0_suspend(void *handle)
  2756. {
  2757. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2758. amdgpu_atombios_scratch_regs_save(adev);
  2759. return dce_v11_0_hw_fini(handle);
  2760. }
  2761. static int dce_v11_0_resume(void *handle)
  2762. {
  2763. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2764. int ret;
  2765. ret = dce_v11_0_hw_init(handle);
  2766. amdgpu_atombios_scratch_regs_restore(adev);
  2767. /* turn on the BL */
  2768. if (adev->mode_info.bl_encoder) {
  2769. u8 bl_level = amdgpu_display_backlight_get_level(adev,
  2770. adev->mode_info.bl_encoder);
  2771. amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
  2772. bl_level);
  2773. }
  2774. return ret;
  2775. }
  2776. static bool dce_v11_0_is_idle(void *handle)
  2777. {
  2778. return true;
  2779. }
  2780. static int dce_v11_0_wait_for_idle(void *handle)
  2781. {
  2782. return 0;
  2783. }
  2784. static int dce_v11_0_soft_reset(void *handle)
  2785. {
  2786. u32 srbm_soft_reset = 0, tmp;
  2787. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2788. if (dce_v11_0_is_display_hung(adev))
  2789. srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
  2790. if (srbm_soft_reset) {
  2791. tmp = RREG32(mmSRBM_SOFT_RESET);
  2792. tmp |= srbm_soft_reset;
  2793. dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
  2794. WREG32(mmSRBM_SOFT_RESET, tmp);
  2795. tmp = RREG32(mmSRBM_SOFT_RESET);
  2796. udelay(50);
  2797. tmp &= ~srbm_soft_reset;
  2798. WREG32(mmSRBM_SOFT_RESET, tmp);
  2799. tmp = RREG32(mmSRBM_SOFT_RESET);
  2800. /* Wait a little for things to settle down */
  2801. udelay(50);
  2802. }
  2803. return 0;
  2804. }
  2805. static void dce_v11_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
  2806. int crtc,
  2807. enum amdgpu_interrupt_state state)
  2808. {
  2809. u32 lb_interrupt_mask;
  2810. if (crtc >= adev->mode_info.num_crtc) {
  2811. DRM_DEBUG("invalid crtc %d\n", crtc);
  2812. return;
  2813. }
  2814. switch (state) {
  2815. case AMDGPU_IRQ_STATE_DISABLE:
  2816. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2817. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2818. VBLANK_INTERRUPT_MASK, 0);
  2819. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2820. break;
  2821. case AMDGPU_IRQ_STATE_ENABLE:
  2822. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2823. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2824. VBLANK_INTERRUPT_MASK, 1);
  2825. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2826. break;
  2827. default:
  2828. break;
  2829. }
  2830. }
  2831. static void dce_v11_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
  2832. int crtc,
  2833. enum amdgpu_interrupt_state state)
  2834. {
  2835. u32 lb_interrupt_mask;
  2836. if (crtc >= adev->mode_info.num_crtc) {
  2837. DRM_DEBUG("invalid crtc %d\n", crtc);
  2838. return;
  2839. }
  2840. switch (state) {
  2841. case AMDGPU_IRQ_STATE_DISABLE:
  2842. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2843. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2844. VLINE_INTERRUPT_MASK, 0);
  2845. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2846. break;
  2847. case AMDGPU_IRQ_STATE_ENABLE:
  2848. lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
  2849. lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
  2850. VLINE_INTERRUPT_MASK, 1);
  2851. WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
  2852. break;
  2853. default:
  2854. break;
  2855. }
  2856. }
  2857. static int dce_v11_0_set_hpd_irq_state(struct amdgpu_device *adev,
  2858. struct amdgpu_irq_src *source,
  2859. unsigned hpd,
  2860. enum amdgpu_interrupt_state state)
  2861. {
  2862. u32 tmp;
  2863. if (hpd >= adev->mode_info.num_hpd) {
  2864. DRM_DEBUG("invalid hdp %d\n", hpd);
  2865. return 0;
  2866. }
  2867. switch (state) {
  2868. case AMDGPU_IRQ_STATE_DISABLE:
  2869. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2870. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
  2871. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2872. break;
  2873. case AMDGPU_IRQ_STATE_ENABLE:
  2874. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  2875. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 1);
  2876. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  2877. break;
  2878. default:
  2879. break;
  2880. }
  2881. return 0;
  2882. }
  2883. static int dce_v11_0_set_crtc_irq_state(struct amdgpu_device *adev,
  2884. struct amdgpu_irq_src *source,
  2885. unsigned type,
  2886. enum amdgpu_interrupt_state state)
  2887. {
  2888. switch (type) {
  2889. case AMDGPU_CRTC_IRQ_VBLANK1:
  2890. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 0, state);
  2891. break;
  2892. case AMDGPU_CRTC_IRQ_VBLANK2:
  2893. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 1, state);
  2894. break;
  2895. case AMDGPU_CRTC_IRQ_VBLANK3:
  2896. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 2, state);
  2897. break;
  2898. case AMDGPU_CRTC_IRQ_VBLANK4:
  2899. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 3, state);
  2900. break;
  2901. case AMDGPU_CRTC_IRQ_VBLANK5:
  2902. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 4, state);
  2903. break;
  2904. case AMDGPU_CRTC_IRQ_VBLANK6:
  2905. dce_v11_0_set_crtc_vblank_interrupt_state(adev, 5, state);
  2906. break;
  2907. case AMDGPU_CRTC_IRQ_VLINE1:
  2908. dce_v11_0_set_crtc_vline_interrupt_state(adev, 0, state);
  2909. break;
  2910. case AMDGPU_CRTC_IRQ_VLINE2:
  2911. dce_v11_0_set_crtc_vline_interrupt_state(adev, 1, state);
  2912. break;
  2913. case AMDGPU_CRTC_IRQ_VLINE3:
  2914. dce_v11_0_set_crtc_vline_interrupt_state(adev, 2, state);
  2915. break;
  2916. case AMDGPU_CRTC_IRQ_VLINE4:
  2917. dce_v11_0_set_crtc_vline_interrupt_state(adev, 3, state);
  2918. break;
  2919. case AMDGPU_CRTC_IRQ_VLINE5:
  2920. dce_v11_0_set_crtc_vline_interrupt_state(adev, 4, state);
  2921. break;
  2922. case AMDGPU_CRTC_IRQ_VLINE6:
  2923. dce_v11_0_set_crtc_vline_interrupt_state(adev, 5, state);
  2924. break;
  2925. default:
  2926. break;
  2927. }
  2928. return 0;
  2929. }
  2930. static int dce_v11_0_set_pageflip_irq_state(struct amdgpu_device *adev,
  2931. struct amdgpu_irq_src *src,
  2932. unsigned type,
  2933. enum amdgpu_interrupt_state state)
  2934. {
  2935. u32 reg;
  2936. if (type >= adev->mode_info.num_crtc) {
  2937. DRM_ERROR("invalid pageflip crtc %d\n", type);
  2938. return -EINVAL;
  2939. }
  2940. reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
  2941. if (state == AMDGPU_IRQ_STATE_DISABLE)
  2942. WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
  2943. reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2944. else
  2945. WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
  2946. reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
  2947. return 0;
  2948. }
  2949. static int dce_v11_0_pageflip_irq(struct amdgpu_device *adev,
  2950. struct amdgpu_irq_src *source,
  2951. struct amdgpu_iv_entry *entry)
  2952. {
  2953. unsigned long flags;
  2954. unsigned crtc_id;
  2955. struct amdgpu_crtc *amdgpu_crtc;
  2956. struct amdgpu_flip_work *works;
  2957. crtc_id = (entry->src_id - 8) >> 1;
  2958. amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
  2959. if (crtc_id >= adev->mode_info.num_crtc) {
  2960. DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
  2961. return -EINVAL;
  2962. }
  2963. if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
  2964. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
  2965. WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
  2966. GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
  2967. /* IRQ could occur when in initial stage */
  2968. if(amdgpu_crtc == NULL)
  2969. return 0;
  2970. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  2971. works = amdgpu_crtc->pflip_works;
  2972. if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
  2973. DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
  2974. "AMDGPU_FLIP_SUBMITTED(%d)\n",
  2975. amdgpu_crtc->pflip_status,
  2976. AMDGPU_FLIP_SUBMITTED);
  2977. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2978. return 0;
  2979. }
  2980. /* page flip completed. clean up */
  2981. amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
  2982. amdgpu_crtc->pflip_works = NULL;
  2983. /* wakeup usersapce */
  2984. if(works->event)
  2985. drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
  2986. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  2987. drm_crtc_vblank_put(&amdgpu_crtc->base);
  2988. schedule_work(&works->unpin_work);
  2989. return 0;
  2990. }
  2991. static void dce_v11_0_hpd_int_ack(struct amdgpu_device *adev,
  2992. int hpd)
  2993. {
  2994. u32 tmp;
  2995. if (hpd >= adev->mode_info.num_hpd) {
  2996. DRM_DEBUG("invalid hdp %d\n", hpd);
  2997. return;
  2998. }
  2999. tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
  3000. tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_ACK, 1);
  3001. WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
  3002. }
  3003. static void dce_v11_0_crtc_vblank_int_ack(struct amdgpu_device *adev,
  3004. int crtc)
  3005. {
  3006. u32 tmp;
  3007. if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
  3008. DRM_DEBUG("invalid crtc %d\n", crtc);
  3009. return;
  3010. }
  3011. tmp = RREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc]);
  3012. tmp = REG_SET_FIELD(tmp, LB_VBLANK_STATUS, VBLANK_ACK, 1);
  3013. WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], tmp);
  3014. }
  3015. static void dce_v11_0_crtc_vline_int_ack(struct amdgpu_device *adev,
  3016. int crtc)
  3017. {
  3018. u32 tmp;
  3019. if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
  3020. DRM_DEBUG("invalid crtc %d\n", crtc);
  3021. return;
  3022. }
  3023. tmp = RREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc]);
  3024. tmp = REG_SET_FIELD(tmp, LB_VLINE_STATUS, VLINE_ACK, 1);
  3025. WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], tmp);
  3026. }
  3027. static int dce_v11_0_crtc_irq(struct amdgpu_device *adev,
  3028. struct amdgpu_irq_src *source,
  3029. struct amdgpu_iv_entry *entry)
  3030. {
  3031. unsigned crtc = entry->src_id - 1;
  3032. uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
  3033. unsigned irq_type = amdgpu_crtc_idx_to_irq_type(adev, crtc);
  3034. switch (entry->src_data) {
  3035. case 0: /* vblank */
  3036. if (disp_int & interrupt_status_offsets[crtc].vblank)
  3037. dce_v11_0_crtc_vblank_int_ack(adev, crtc);
  3038. else
  3039. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  3040. if (amdgpu_irq_enabled(adev, source, irq_type)) {
  3041. drm_handle_vblank(adev->ddev, crtc);
  3042. }
  3043. DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
  3044. break;
  3045. case 1: /* vline */
  3046. if (disp_int & interrupt_status_offsets[crtc].vline)
  3047. dce_v11_0_crtc_vline_int_ack(adev, crtc);
  3048. else
  3049. DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
  3050. DRM_DEBUG("IH: D%d vline\n", crtc + 1);
  3051. break;
  3052. default:
  3053. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
  3054. break;
  3055. }
  3056. return 0;
  3057. }
  3058. static int dce_v11_0_hpd_irq(struct amdgpu_device *adev,
  3059. struct amdgpu_irq_src *source,
  3060. struct amdgpu_iv_entry *entry)
  3061. {
  3062. uint32_t disp_int, mask;
  3063. unsigned hpd;
  3064. if (entry->src_data >= adev->mode_info.num_hpd) {
  3065. DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data);
  3066. return 0;
  3067. }
  3068. hpd = entry->src_data;
  3069. disp_int = RREG32(interrupt_status_offsets[hpd].reg);
  3070. mask = interrupt_status_offsets[hpd].hpd;
  3071. if (disp_int & mask) {
  3072. dce_v11_0_hpd_int_ack(adev, hpd);
  3073. schedule_work(&adev->hotplug_work);
  3074. DRM_DEBUG("IH: HPD%d\n", hpd + 1);
  3075. }
  3076. return 0;
  3077. }
  3078. static int dce_v11_0_set_clockgating_state(void *handle,
  3079. enum amd_clockgating_state state)
  3080. {
  3081. return 0;
  3082. }
  3083. static int dce_v11_0_set_powergating_state(void *handle,
  3084. enum amd_powergating_state state)
  3085. {
  3086. return 0;
  3087. }
  3088. const struct amd_ip_funcs dce_v11_0_ip_funcs = {
  3089. .name = "dce_v11_0",
  3090. .early_init = dce_v11_0_early_init,
  3091. .late_init = NULL,
  3092. .sw_init = dce_v11_0_sw_init,
  3093. .sw_fini = dce_v11_0_sw_fini,
  3094. .hw_init = dce_v11_0_hw_init,
  3095. .hw_fini = dce_v11_0_hw_fini,
  3096. .suspend = dce_v11_0_suspend,
  3097. .resume = dce_v11_0_resume,
  3098. .is_idle = dce_v11_0_is_idle,
  3099. .wait_for_idle = dce_v11_0_wait_for_idle,
  3100. .soft_reset = dce_v11_0_soft_reset,
  3101. .set_clockgating_state = dce_v11_0_set_clockgating_state,
  3102. .set_powergating_state = dce_v11_0_set_powergating_state,
  3103. };
  3104. static void
  3105. dce_v11_0_encoder_mode_set(struct drm_encoder *encoder,
  3106. struct drm_display_mode *mode,
  3107. struct drm_display_mode *adjusted_mode)
  3108. {
  3109. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3110. amdgpu_encoder->pixel_clock = adjusted_mode->clock;
  3111. /* need to call this here rather than in prepare() since we need some crtc info */
  3112. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  3113. /* set scaler clears this on some chips */
  3114. dce_v11_0_set_interleave(encoder->crtc, mode);
  3115. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
  3116. dce_v11_0_afmt_enable(encoder, true);
  3117. dce_v11_0_afmt_setmode(encoder, adjusted_mode);
  3118. }
  3119. }
  3120. static void dce_v11_0_encoder_prepare(struct drm_encoder *encoder)
  3121. {
  3122. struct amdgpu_device *adev = encoder->dev->dev_private;
  3123. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3124. struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
  3125. if ((amdgpu_encoder->active_device &
  3126. (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
  3127. (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
  3128. ENCODER_OBJECT_ID_NONE)) {
  3129. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  3130. if (dig) {
  3131. dig->dig_encoder = dce_v11_0_pick_dig_encoder(encoder);
  3132. if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
  3133. dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
  3134. }
  3135. }
  3136. amdgpu_atombios_scratch_regs_lock(adev, true);
  3137. if (connector) {
  3138. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  3139. /* select the clock/data port if it uses a router */
  3140. if (amdgpu_connector->router.cd_valid)
  3141. amdgpu_i2c_router_select_cd_port(amdgpu_connector);
  3142. /* turn eDP panel on for mode set */
  3143. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  3144. amdgpu_atombios_encoder_set_edp_panel_power(connector,
  3145. ATOM_TRANSMITTER_ACTION_POWER_ON);
  3146. }
  3147. /* this is needed for the pll/ss setup to work correctly in some cases */
  3148. amdgpu_atombios_encoder_set_crtc_source(encoder);
  3149. /* set up the FMT blocks */
  3150. dce_v11_0_program_fmt(encoder);
  3151. }
  3152. static void dce_v11_0_encoder_commit(struct drm_encoder *encoder)
  3153. {
  3154. struct drm_device *dev = encoder->dev;
  3155. struct amdgpu_device *adev = dev->dev_private;
  3156. /* need to call this here as we need the crtc set up */
  3157. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  3158. amdgpu_atombios_scratch_regs_lock(adev, false);
  3159. }
  3160. static void dce_v11_0_encoder_disable(struct drm_encoder *encoder)
  3161. {
  3162. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3163. struct amdgpu_encoder_atom_dig *dig;
  3164. amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  3165. if (amdgpu_atombios_encoder_is_digital(encoder)) {
  3166. if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
  3167. dce_v11_0_afmt_enable(encoder, false);
  3168. dig = amdgpu_encoder->enc_priv;
  3169. dig->dig_encoder = -1;
  3170. }
  3171. amdgpu_encoder->active_device = 0;
  3172. }
  3173. /* these are handled by the primary encoders */
  3174. static void dce_v11_0_ext_prepare(struct drm_encoder *encoder)
  3175. {
  3176. }
  3177. static void dce_v11_0_ext_commit(struct drm_encoder *encoder)
  3178. {
  3179. }
  3180. static void
  3181. dce_v11_0_ext_mode_set(struct drm_encoder *encoder,
  3182. struct drm_display_mode *mode,
  3183. struct drm_display_mode *adjusted_mode)
  3184. {
  3185. }
  3186. static void dce_v11_0_ext_disable(struct drm_encoder *encoder)
  3187. {
  3188. }
  3189. static void
  3190. dce_v11_0_ext_dpms(struct drm_encoder *encoder, int mode)
  3191. {
  3192. }
  3193. static const struct drm_encoder_helper_funcs dce_v11_0_ext_helper_funcs = {
  3194. .dpms = dce_v11_0_ext_dpms,
  3195. .prepare = dce_v11_0_ext_prepare,
  3196. .mode_set = dce_v11_0_ext_mode_set,
  3197. .commit = dce_v11_0_ext_commit,
  3198. .disable = dce_v11_0_ext_disable,
  3199. /* no detect for TMDS/LVDS yet */
  3200. };
  3201. static const struct drm_encoder_helper_funcs dce_v11_0_dig_helper_funcs = {
  3202. .dpms = amdgpu_atombios_encoder_dpms,
  3203. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  3204. .prepare = dce_v11_0_encoder_prepare,
  3205. .mode_set = dce_v11_0_encoder_mode_set,
  3206. .commit = dce_v11_0_encoder_commit,
  3207. .disable = dce_v11_0_encoder_disable,
  3208. .detect = amdgpu_atombios_encoder_dig_detect,
  3209. };
  3210. static const struct drm_encoder_helper_funcs dce_v11_0_dac_helper_funcs = {
  3211. .dpms = amdgpu_atombios_encoder_dpms,
  3212. .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
  3213. .prepare = dce_v11_0_encoder_prepare,
  3214. .mode_set = dce_v11_0_encoder_mode_set,
  3215. .commit = dce_v11_0_encoder_commit,
  3216. .detect = amdgpu_atombios_encoder_dac_detect,
  3217. };
  3218. static void dce_v11_0_encoder_destroy(struct drm_encoder *encoder)
  3219. {
  3220. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  3221. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  3222. amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
  3223. kfree(amdgpu_encoder->enc_priv);
  3224. drm_encoder_cleanup(encoder);
  3225. kfree(amdgpu_encoder);
  3226. }
  3227. static const struct drm_encoder_funcs dce_v11_0_encoder_funcs = {
  3228. .destroy = dce_v11_0_encoder_destroy,
  3229. };
  3230. static void dce_v11_0_encoder_add(struct amdgpu_device *adev,
  3231. uint32_t encoder_enum,
  3232. uint32_t supported_device,
  3233. u16 caps)
  3234. {
  3235. struct drm_device *dev = adev->ddev;
  3236. struct drm_encoder *encoder;
  3237. struct amdgpu_encoder *amdgpu_encoder;
  3238. /* see if we already added it */
  3239. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  3240. amdgpu_encoder = to_amdgpu_encoder(encoder);
  3241. if (amdgpu_encoder->encoder_enum == encoder_enum) {
  3242. amdgpu_encoder->devices |= supported_device;
  3243. return;
  3244. }
  3245. }
  3246. /* add a new one */
  3247. amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
  3248. if (!amdgpu_encoder)
  3249. return;
  3250. encoder = &amdgpu_encoder->base;
  3251. switch (adev->mode_info.num_crtc) {
  3252. case 1:
  3253. encoder->possible_crtcs = 0x1;
  3254. break;
  3255. case 2:
  3256. default:
  3257. encoder->possible_crtcs = 0x3;
  3258. break;
  3259. case 4:
  3260. encoder->possible_crtcs = 0xf;
  3261. break;
  3262. case 6:
  3263. encoder->possible_crtcs = 0x3f;
  3264. break;
  3265. }
  3266. amdgpu_encoder->enc_priv = NULL;
  3267. amdgpu_encoder->encoder_enum = encoder_enum;
  3268. amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
  3269. amdgpu_encoder->devices = supported_device;
  3270. amdgpu_encoder->rmx_type = RMX_OFF;
  3271. amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
  3272. amdgpu_encoder->is_ext_encoder = false;
  3273. amdgpu_encoder->caps = caps;
  3274. switch (amdgpu_encoder->encoder_id) {
  3275. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
  3276. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
  3277. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3278. DRM_MODE_ENCODER_DAC, NULL);
  3279. drm_encoder_helper_add(encoder, &dce_v11_0_dac_helper_funcs);
  3280. break;
  3281. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
  3282. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  3283. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  3284. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  3285. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
  3286. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
  3287. amdgpu_encoder->rmx_type = RMX_FULL;
  3288. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3289. DRM_MODE_ENCODER_LVDS, NULL);
  3290. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
  3291. } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
  3292. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3293. DRM_MODE_ENCODER_DAC, NULL);
  3294. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  3295. } else {
  3296. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3297. DRM_MODE_ENCODER_TMDS, NULL);
  3298. amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
  3299. }
  3300. drm_encoder_helper_add(encoder, &dce_v11_0_dig_helper_funcs);
  3301. break;
  3302. case ENCODER_OBJECT_ID_SI170B:
  3303. case ENCODER_OBJECT_ID_CH7303:
  3304. case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
  3305. case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
  3306. case ENCODER_OBJECT_ID_TITFP513:
  3307. case ENCODER_OBJECT_ID_VT1623:
  3308. case ENCODER_OBJECT_ID_HDMI_SI1930:
  3309. case ENCODER_OBJECT_ID_TRAVIS:
  3310. case ENCODER_OBJECT_ID_NUTMEG:
  3311. /* these are handled by the primary encoders */
  3312. amdgpu_encoder->is_ext_encoder = true;
  3313. if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
  3314. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3315. DRM_MODE_ENCODER_LVDS, NULL);
  3316. else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
  3317. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3318. DRM_MODE_ENCODER_DAC, NULL);
  3319. else
  3320. drm_encoder_init(dev, encoder, &dce_v11_0_encoder_funcs,
  3321. DRM_MODE_ENCODER_TMDS, NULL);
  3322. drm_encoder_helper_add(encoder, &dce_v11_0_ext_helper_funcs);
  3323. break;
  3324. }
  3325. }
  3326. static const struct amdgpu_display_funcs dce_v11_0_display_funcs = {
  3327. .set_vga_render_state = &dce_v11_0_set_vga_render_state,
  3328. .bandwidth_update = &dce_v11_0_bandwidth_update,
  3329. .vblank_get_counter = &dce_v11_0_vblank_get_counter,
  3330. .vblank_wait = &dce_v11_0_vblank_wait,
  3331. .is_display_hung = &dce_v11_0_is_display_hung,
  3332. .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
  3333. .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
  3334. .hpd_sense = &dce_v11_0_hpd_sense,
  3335. .hpd_set_polarity = &dce_v11_0_hpd_set_polarity,
  3336. .hpd_get_gpio_reg = &dce_v11_0_hpd_get_gpio_reg,
  3337. .page_flip = &dce_v11_0_page_flip,
  3338. .page_flip_get_scanoutpos = &dce_v11_0_crtc_get_scanoutpos,
  3339. .add_encoder = &dce_v11_0_encoder_add,
  3340. .add_connector = &amdgpu_connector_add,
  3341. .stop_mc_access = &dce_v11_0_stop_mc_access,
  3342. .resume_mc_access = &dce_v11_0_resume_mc_access,
  3343. };
  3344. static void dce_v11_0_set_display_funcs(struct amdgpu_device *adev)
  3345. {
  3346. if (adev->mode_info.funcs == NULL)
  3347. adev->mode_info.funcs = &dce_v11_0_display_funcs;
  3348. }
  3349. static const struct amdgpu_irq_src_funcs dce_v11_0_crtc_irq_funcs = {
  3350. .set = dce_v11_0_set_crtc_irq_state,
  3351. .process = dce_v11_0_crtc_irq,
  3352. };
  3353. static const struct amdgpu_irq_src_funcs dce_v11_0_pageflip_irq_funcs = {
  3354. .set = dce_v11_0_set_pageflip_irq_state,
  3355. .process = dce_v11_0_pageflip_irq,
  3356. };
  3357. static const struct amdgpu_irq_src_funcs dce_v11_0_hpd_irq_funcs = {
  3358. .set = dce_v11_0_set_hpd_irq_state,
  3359. .process = dce_v11_0_hpd_irq,
  3360. };
  3361. static void dce_v11_0_set_irq_funcs(struct amdgpu_device *adev)
  3362. {
  3363. adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_LAST;
  3364. adev->crtc_irq.funcs = &dce_v11_0_crtc_irq_funcs;
  3365. adev->pageflip_irq.num_types = AMDGPU_PAGEFLIP_IRQ_LAST;
  3366. adev->pageflip_irq.funcs = &dce_v11_0_pageflip_irq_funcs;
  3367. adev->hpd_irq.num_types = AMDGPU_HPD_LAST;
  3368. adev->hpd_irq.funcs = &dce_v11_0_hpd_irq_funcs;
  3369. }