amdgpu_dm.c 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884
  1. /*
  2. * Copyright 2015 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. * Authors: AMD
  23. *
  24. */
  25. #include "dm_services_types.h"
  26. #include "dc.h"
  27. #include "dc/inc/core_types.h"
  28. #include "vid.h"
  29. #include "amdgpu.h"
  30. #include "amdgpu_display.h"
  31. #include "atom.h"
  32. #include "amdgpu_dm.h"
  33. #include "amdgpu_pm.h"
  34. #include "amd_shared.h"
  35. #include "amdgpu_dm_irq.h"
  36. #include "dm_helpers.h"
  37. #include "dm_services_types.h"
  38. #include "amdgpu_dm_mst_types.h"
  39. #include "ivsrcid/ivsrcid_vislands30.h"
  40. #include <linux/module.h>
  41. #include <linux/moduleparam.h>
  42. #include <linux/version.h>
  43. #include <linux/types.h>
  44. #include <drm/drmP.h>
  45. #include <drm/drm_atomic.h>
  46. #include <drm/drm_atomic_helper.h>
  47. #include <drm/drm_dp_mst_helper.h>
  48. #include <drm/drm_fb_helper.h>
  49. #include <drm/drm_edid.h>
  50. #include "modules/inc/mod_freesync.h"
  51. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  52. #include "ivsrcid/irqsrcs_dcn_1_0.h"
  53. #include "raven1/DCN/dcn_1_0_offset.h"
  54. #include "raven1/DCN/dcn_1_0_sh_mask.h"
  55. #include "vega10/soc15ip.h"
  56. #include "soc15_common.h"
  57. #endif
  58. #include "modules/inc/mod_freesync.h"
  59. #include "i2caux_interface.h"
  60. static enum drm_plane_type dm_plane_type_default[AMDGPU_MAX_PLANES] = {
  61. DRM_PLANE_TYPE_PRIMARY,
  62. DRM_PLANE_TYPE_PRIMARY,
  63. DRM_PLANE_TYPE_PRIMARY,
  64. DRM_PLANE_TYPE_PRIMARY,
  65. DRM_PLANE_TYPE_PRIMARY,
  66. DRM_PLANE_TYPE_PRIMARY,
  67. };
  68. static enum drm_plane_type dm_plane_type_carizzo[AMDGPU_MAX_PLANES] = {
  69. DRM_PLANE_TYPE_PRIMARY,
  70. DRM_PLANE_TYPE_PRIMARY,
  71. DRM_PLANE_TYPE_PRIMARY,
  72. DRM_PLANE_TYPE_OVERLAY,/* YUV Capable Underlay */
  73. };
  74. static enum drm_plane_type dm_plane_type_stoney[AMDGPU_MAX_PLANES] = {
  75. DRM_PLANE_TYPE_PRIMARY,
  76. DRM_PLANE_TYPE_PRIMARY,
  77. DRM_PLANE_TYPE_OVERLAY, /* YUV Capable Underlay */
  78. };
  79. /*
  80. * dm_vblank_get_counter
  81. *
  82. * @brief
  83. * Get counter for number of vertical blanks
  84. *
  85. * @param
  86. * struct amdgpu_device *adev - [in] desired amdgpu device
  87. * int disp_idx - [in] which CRTC to get the counter from
  88. *
  89. * @return
  90. * Counter for vertical blanks
  91. */
  92. static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc)
  93. {
  94. if (crtc >= adev->mode_info.num_crtc)
  95. return 0;
  96. else {
  97. struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
  98. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
  99. acrtc->base.state);
  100. if (acrtc_state->stream == NULL) {
  101. DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
  102. crtc);
  103. return 0;
  104. }
  105. return dc_stream_get_vblank_counter(acrtc_state->stream);
  106. }
  107. }
  108. static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
  109. u32 *vbl, u32 *position)
  110. {
  111. uint32_t v_blank_start, v_blank_end, h_position, v_position;
  112. if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
  113. return -EINVAL;
  114. else {
  115. struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc];
  116. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(
  117. acrtc->base.state);
  118. if (acrtc_state->stream == NULL) {
  119. DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n",
  120. crtc);
  121. return 0;
  122. }
  123. /*
  124. * TODO rework base driver to use values directly.
  125. * for now parse it back into reg-format
  126. */
  127. dc_stream_get_scanoutpos(acrtc_state->stream,
  128. &v_blank_start,
  129. &v_blank_end,
  130. &h_position,
  131. &v_position);
  132. *position = v_position | (h_position << 16);
  133. *vbl = v_blank_start | (v_blank_end << 16);
  134. }
  135. return 0;
  136. }
  137. static bool dm_is_idle(void *handle)
  138. {
  139. /* XXX todo */
  140. return true;
  141. }
  142. static int dm_wait_for_idle(void *handle)
  143. {
  144. /* XXX todo */
  145. return 0;
  146. }
  147. static bool dm_check_soft_reset(void *handle)
  148. {
  149. return false;
  150. }
  151. static int dm_soft_reset(void *handle)
  152. {
  153. /* XXX todo */
  154. return 0;
  155. }
  156. static struct amdgpu_crtc *get_crtc_by_otg_inst(
  157. struct amdgpu_device *adev,
  158. int otg_inst)
  159. {
  160. struct drm_device *dev = adev->ddev;
  161. struct drm_crtc *crtc;
  162. struct amdgpu_crtc *amdgpu_crtc;
  163. /*
  164. * following if is check inherited from both functions where this one is
  165. * used now. Need to be checked why it could happen.
  166. */
  167. if (otg_inst == -1) {
  168. WARN_ON(1);
  169. return adev->mode_info.crtcs[0];
  170. }
  171. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  172. amdgpu_crtc = to_amdgpu_crtc(crtc);
  173. if (amdgpu_crtc->otg_inst == otg_inst)
  174. return amdgpu_crtc;
  175. }
  176. return NULL;
  177. }
  178. static void dm_pflip_high_irq(void *interrupt_params)
  179. {
  180. struct amdgpu_crtc *amdgpu_crtc;
  181. struct common_irq_params *irq_params = interrupt_params;
  182. struct amdgpu_device *adev = irq_params->adev;
  183. unsigned long flags;
  184. amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP);
  185. /* IRQ could occur when in initial stage */
  186. /*TODO work and BO cleanup */
  187. if (amdgpu_crtc == NULL) {
  188. DRM_DEBUG_DRIVER("CRTC is null, returning.\n");
  189. return;
  190. }
  191. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  192. if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){
  193. DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n",
  194. amdgpu_crtc->pflip_status,
  195. AMDGPU_FLIP_SUBMITTED,
  196. amdgpu_crtc->crtc_id,
  197. amdgpu_crtc);
  198. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  199. return;
  200. }
  201. /* wakeup usersapce */
  202. if (amdgpu_crtc->event) {
  203. /* Update to correct count/ts if racing with vblank irq */
  204. drm_crtc_accurate_vblank_count(&amdgpu_crtc->base);
  205. drm_crtc_send_vblank_event(&amdgpu_crtc->base, amdgpu_crtc->event);
  206. /* page flip completed. clean up */
  207. amdgpu_crtc->event = NULL;
  208. } else
  209. WARN_ON(1);
  210. amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
  211. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  212. DRM_DEBUG_DRIVER("%s - crtc :%d[%p], pflip_stat:AMDGPU_FLIP_NONE\n",
  213. __func__, amdgpu_crtc->crtc_id, amdgpu_crtc);
  214. drm_crtc_vblank_put(&amdgpu_crtc->base);
  215. }
  216. static void dm_crtc_high_irq(void *interrupt_params)
  217. {
  218. struct common_irq_params *irq_params = interrupt_params;
  219. struct amdgpu_device *adev = irq_params->adev;
  220. uint8_t crtc_index = 0;
  221. struct amdgpu_crtc *acrtc;
  222. acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK);
  223. if (acrtc)
  224. crtc_index = acrtc->crtc_id;
  225. drm_handle_vblank(adev->ddev, crtc_index);
  226. }
  227. static int dm_set_clockgating_state(void *handle,
  228. enum amd_clockgating_state state)
  229. {
  230. return 0;
  231. }
  232. static int dm_set_powergating_state(void *handle,
  233. enum amd_powergating_state state)
  234. {
  235. return 0;
  236. }
  237. /* Prototypes of private functions */
  238. static int dm_early_init(void* handle);
  239. static void hotplug_notify_work_func(struct work_struct *work)
  240. {
  241. struct amdgpu_display_manager *dm = container_of(work, struct amdgpu_display_manager, mst_hotplug_work);
  242. struct drm_device *dev = dm->ddev;
  243. drm_kms_helper_hotplug_event(dev);
  244. }
  245. #ifdef ENABLE_FBC
  246. #include "dal_asic_id.h"
  247. /* Allocate memory for FBC compressed data */
  248. /* TODO: Dynamic allocation */
  249. #define AMDGPU_FBC_SIZE (3840 * 2160 * 4)
  250. void amdgpu_dm_initialize_fbc(struct amdgpu_device *adev)
  251. {
  252. int r;
  253. struct dm_comressor_info *compressor = &adev->dm.compressor;
  254. if (!compressor->bo_ptr) {
  255. r = amdgpu_bo_create_kernel(adev, AMDGPU_FBC_SIZE, PAGE_SIZE,
  256. AMDGPU_GEM_DOMAIN_VRAM, &compressor->bo_ptr,
  257. &compressor->gpu_addr, &compressor->cpu_addr);
  258. if (r)
  259. DRM_ERROR("DM: Failed to initialize fbc\n");
  260. }
  261. }
  262. #endif
  263. /* Init display KMS
  264. *
  265. * Returns 0 on success
  266. */
  267. int amdgpu_dm_init(struct amdgpu_device *adev)
  268. {
  269. struct dc_init_data init_data;
  270. adev->dm.ddev = adev->ddev;
  271. adev->dm.adev = adev;
  272. DRM_INFO("DAL is enabled\n");
  273. /* Zero all the fields */
  274. memset(&init_data, 0, sizeof(init_data));
  275. /* initialize DAL's lock (for SYNC context use) */
  276. spin_lock_init(&adev->dm.dal_lock);
  277. /* initialize DAL's mutex */
  278. mutex_init(&adev->dm.dal_mutex);
  279. if(amdgpu_dm_irq_init(adev)) {
  280. DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n");
  281. goto error;
  282. }
  283. init_data.asic_id.chip_family = adev->family;
  284. init_data.asic_id.pci_revision_id = adev->rev_id;
  285. init_data.asic_id.hw_internal_rev = adev->external_rev_id;
  286. init_data.asic_id.vram_width = adev->mc.vram_width;
  287. /* TODO: initialize init_data.asic_id.vram_type here!!!! */
  288. init_data.asic_id.atombios_base_address =
  289. adev->mode_info.atom_context->bios;
  290. init_data.driver = adev;
  291. adev->dm.cgs_device = amdgpu_cgs_create_device(adev);
  292. if (!adev->dm.cgs_device) {
  293. DRM_ERROR("amdgpu: failed to create cgs device.\n");
  294. goto error;
  295. }
  296. init_data.cgs_device = adev->dm.cgs_device;
  297. adev->dm.dal = NULL;
  298. init_data.dce_environment = DCE_ENV_PRODUCTION_DRV;
  299. init_data.log_mask = DC_DEFAULT_LOG_MASK;
  300. #ifdef ENABLE_FBC
  301. if (adev->family == FAMILY_CZ)
  302. amdgpu_dm_initialize_fbc(adev);
  303. init_data.fbc_gpu_addr = adev->dm.compressor.gpu_addr;
  304. #endif
  305. /* Display Core create. */
  306. adev->dm.dc = dc_create(&init_data);
  307. if (!adev->dm.dc)
  308. DRM_INFO("Display Core failed to initialize!\n");
  309. INIT_WORK(&adev->dm.mst_hotplug_work, hotplug_notify_work_func);
  310. adev->dm.freesync_module = mod_freesync_create(adev->dm.dc);
  311. if (!adev->dm.freesync_module) {
  312. DRM_ERROR(
  313. "amdgpu: failed to initialize freesync_module.\n");
  314. } else
  315. DRM_INFO("amdgpu: freesync_module init done %p.\n",
  316. adev->dm.freesync_module);
  317. if (amdgpu_dm_initialize_drm_device(adev)) {
  318. DRM_ERROR(
  319. "amdgpu: failed to initialize sw for display support.\n");
  320. goto error;
  321. }
  322. /* Update the actual used number of crtc */
  323. adev->mode_info.num_crtc = adev->dm.display_indexes_num;
  324. /* TODO: Add_display_info? */
  325. /* TODO use dynamic cursor width */
  326. adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size;
  327. adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size;
  328. if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) {
  329. DRM_ERROR(
  330. "amdgpu: failed to initialize sw for display support.\n");
  331. goto error;
  332. }
  333. DRM_INFO("KMS initialized.\n");
  334. return 0;
  335. error:
  336. amdgpu_dm_fini(adev);
  337. return -1;
  338. }
  339. void amdgpu_dm_fini(struct amdgpu_device *adev)
  340. {
  341. amdgpu_dm_destroy_drm_device(&adev->dm);
  342. /*
  343. * TODO: pageflip, vlank interrupt
  344. *
  345. * amdgpu_dm_irq_fini(adev);
  346. */
  347. if (adev->dm.cgs_device) {
  348. amdgpu_cgs_destroy_device(adev->dm.cgs_device);
  349. adev->dm.cgs_device = NULL;
  350. }
  351. if (adev->dm.freesync_module) {
  352. mod_freesync_destroy(adev->dm.freesync_module);
  353. adev->dm.freesync_module = NULL;
  354. }
  355. /* DC Destroy TODO: Replace destroy DAL */
  356. if (adev->dm.dc)
  357. dc_destroy(&adev->dm.dc);
  358. return;
  359. }
  360. /* moved from amdgpu_dm_kms.c */
  361. void amdgpu_dm_destroy()
  362. {
  363. }
  364. static int dm_sw_init(void *handle)
  365. {
  366. return 0;
  367. }
  368. static int dm_sw_fini(void *handle)
  369. {
  370. return 0;
  371. }
  372. static int detect_mst_link_for_all_connectors(struct drm_device *dev)
  373. {
  374. struct amdgpu_dm_connector *aconnector;
  375. struct drm_connector *connector;
  376. int ret = 0;
  377. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  378. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  379. aconnector = to_amdgpu_dm_connector(connector);
  380. if (aconnector->dc_link->type == dc_connection_mst_branch) {
  381. DRM_INFO("DM_MST: starting TM on aconnector: %p [id: %d]\n",
  382. aconnector, aconnector->base.base.id);
  383. ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
  384. if (ret < 0) {
  385. DRM_ERROR("DM_MST: Failed to start MST\n");
  386. ((struct dc_link *)aconnector->dc_link)->type = dc_connection_single;
  387. return ret;
  388. }
  389. }
  390. }
  391. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  392. return ret;
  393. }
  394. static int dm_late_init(void *handle)
  395. {
  396. struct drm_device *dev = ((struct amdgpu_device *)handle)->ddev;
  397. int r = detect_mst_link_for_all_connectors(dev);
  398. return r;
  399. }
  400. static void s3_handle_mst(struct drm_device *dev, bool suspend)
  401. {
  402. struct amdgpu_dm_connector *aconnector;
  403. struct drm_connector *connector;
  404. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  405. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  406. aconnector = to_amdgpu_dm_connector(connector);
  407. if (aconnector->dc_link->type == dc_connection_mst_branch &&
  408. !aconnector->mst_port) {
  409. if (suspend)
  410. drm_dp_mst_topology_mgr_suspend(&aconnector->mst_mgr);
  411. else
  412. drm_dp_mst_topology_mgr_resume(&aconnector->mst_mgr);
  413. }
  414. }
  415. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  416. }
  417. static int dm_hw_init(void *handle)
  418. {
  419. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  420. /* Create DAL display manager */
  421. amdgpu_dm_init(adev);
  422. amdgpu_dm_hpd_init(adev);
  423. return 0;
  424. }
  425. static int dm_hw_fini(void *handle)
  426. {
  427. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  428. amdgpu_dm_hpd_fini(adev);
  429. amdgpu_dm_irq_fini(adev);
  430. amdgpu_dm_fini(adev);
  431. return 0;
  432. }
  433. static int dm_suspend(void *handle)
  434. {
  435. struct amdgpu_device *adev = handle;
  436. struct amdgpu_display_manager *dm = &adev->dm;
  437. int ret = 0;
  438. s3_handle_mst(adev->ddev, true);
  439. amdgpu_dm_irq_suspend(adev);
  440. WARN_ON(adev->dm.cached_state);
  441. adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev);
  442. dc_set_power_state(
  443. dm->dc,
  444. DC_ACPI_CM_POWER_STATE_D3
  445. );
  446. return ret;
  447. }
  448. struct amdgpu_dm_connector *amdgpu_dm_find_first_crct_matching_connector(
  449. struct drm_atomic_state *state,
  450. struct drm_crtc *crtc,
  451. bool from_state_var)
  452. {
  453. uint32_t i;
  454. struct drm_connector_state *conn_state;
  455. struct drm_connector *connector;
  456. struct drm_crtc *crtc_from_state;
  457. for_each_connector_in_state(
  458. state,
  459. connector,
  460. conn_state,
  461. i) {
  462. crtc_from_state =
  463. from_state_var ?
  464. conn_state->crtc :
  465. connector->state->crtc;
  466. if (crtc_from_state == crtc)
  467. return to_amdgpu_dm_connector(connector);
  468. }
  469. return NULL;
  470. }
  471. static int dm_resume(void *handle)
  472. {
  473. struct amdgpu_device *adev = handle;
  474. struct amdgpu_display_manager *dm = &adev->dm;
  475. /* power on hardware */
  476. dc_set_power_state(
  477. dm->dc,
  478. DC_ACPI_CM_POWER_STATE_D0
  479. );
  480. return 0;
  481. }
  482. int amdgpu_dm_display_resume(struct amdgpu_device *adev)
  483. {
  484. struct drm_device *ddev = adev->ddev;
  485. struct amdgpu_display_manager *dm = &adev->dm;
  486. struct amdgpu_dm_connector *aconnector;
  487. struct drm_connector *connector;
  488. struct drm_crtc *crtc;
  489. struct drm_crtc_state *crtc_state;
  490. int ret = 0;
  491. int i;
  492. /* program HPD filter */
  493. dc_resume(dm->dc);
  494. /* On resume we need to rewrite the MSTM control bits to enamble MST*/
  495. s3_handle_mst(ddev, false);
  496. /*
  497. * early enable HPD Rx IRQ, should be done before set mode as short
  498. * pulse interrupts are used for MST
  499. */
  500. amdgpu_dm_irq_resume_early(adev);
  501. /* Do detection*/
  502. list_for_each_entry(connector,
  503. &ddev->mode_config.connector_list, head) {
  504. aconnector = to_amdgpu_dm_connector(connector);
  505. /*
  506. * this is the case when traversing through already created
  507. * MST connectors, should be skipped
  508. */
  509. if (aconnector->mst_port)
  510. continue;
  511. mutex_lock(&aconnector->hpd_lock);
  512. dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
  513. aconnector->dc_sink = NULL;
  514. amdgpu_dm_update_connector_after_detect(aconnector);
  515. mutex_unlock(&aconnector->hpd_lock);
  516. }
  517. /* Force mode set in atomic comit */
  518. for_each_crtc_in_state(adev->dm.cached_state, crtc, crtc_state, i)
  519. crtc_state->active_changed = true;
  520. ret = drm_atomic_helper_resume(ddev, adev->dm.cached_state);
  521. drm_atomic_state_put(adev->dm.cached_state);
  522. adev->dm.cached_state = NULL;
  523. amdgpu_dm_irq_resume_late(adev);
  524. return ret;
  525. }
  526. static const struct amd_ip_funcs amdgpu_dm_funcs = {
  527. .name = "dm",
  528. .early_init = dm_early_init,
  529. .late_init = dm_late_init,
  530. .sw_init = dm_sw_init,
  531. .sw_fini = dm_sw_fini,
  532. .hw_init = dm_hw_init,
  533. .hw_fini = dm_hw_fini,
  534. .suspend = dm_suspend,
  535. .resume = dm_resume,
  536. .is_idle = dm_is_idle,
  537. .wait_for_idle = dm_wait_for_idle,
  538. .check_soft_reset = dm_check_soft_reset,
  539. .soft_reset = dm_soft_reset,
  540. .set_clockgating_state = dm_set_clockgating_state,
  541. .set_powergating_state = dm_set_powergating_state,
  542. };
  543. const struct amdgpu_ip_block_version dm_ip_block =
  544. {
  545. .type = AMD_IP_BLOCK_TYPE_DCE,
  546. .major = 1,
  547. .minor = 0,
  548. .rev = 0,
  549. .funcs = &amdgpu_dm_funcs,
  550. };
  551. struct drm_atomic_state *
  552. dm_atomic_state_alloc(struct drm_device *dev)
  553. {
  554. struct dm_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
  555. if (!state)
  556. return NULL;
  557. if (drm_atomic_state_init(dev, &state->base) < 0)
  558. goto fail;
  559. return &state->base;
  560. fail:
  561. kfree(state);
  562. return NULL;
  563. }
  564. static void
  565. dm_atomic_state_clear(struct drm_atomic_state *state)
  566. {
  567. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  568. if (dm_state->context) {
  569. dc_release_state(dm_state->context);
  570. dm_state->context = NULL;
  571. }
  572. drm_atomic_state_default_clear(state);
  573. }
  574. static void
  575. dm_atomic_state_alloc_free(struct drm_atomic_state *state)
  576. {
  577. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  578. drm_atomic_state_default_release(state);
  579. kfree(dm_state);
  580. }
  581. static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = {
  582. .fb_create = amdgpu_user_framebuffer_create,
  583. .output_poll_changed = amdgpu_output_poll_changed,
  584. .atomic_check = amdgpu_dm_atomic_check,
  585. .atomic_commit = amdgpu_dm_atomic_commit,
  586. .atomic_state_alloc = dm_atomic_state_alloc,
  587. .atomic_state_clear = dm_atomic_state_clear,
  588. .atomic_state_free = dm_atomic_state_alloc_free
  589. };
  590. static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = {
  591. .atomic_commit_tail = amdgpu_dm_atomic_commit_tail
  592. };
  593. void amdgpu_dm_update_connector_after_detect(
  594. struct amdgpu_dm_connector *aconnector)
  595. {
  596. struct drm_connector *connector = &aconnector->base;
  597. struct drm_device *dev = connector->dev;
  598. struct dc_sink *sink;
  599. /* MST handled by drm_mst framework */
  600. if (aconnector->mst_mgr.mst_state == true)
  601. return;
  602. sink = aconnector->dc_link->local_sink;
  603. /* Edid mgmt connector gets first update only in mode_valid hook and then
  604. * the connector sink is set to either fake or physical sink depends on link status.
  605. * don't do it here if u are during boot
  606. */
  607. if (aconnector->base.force != DRM_FORCE_UNSPECIFIED
  608. && aconnector->dc_em_sink) {
  609. /* For S3 resume with headless use eml_sink to fake stream
  610. * because on resume connecotr->sink is set ti NULL
  611. */
  612. mutex_lock(&dev->mode_config.mutex);
  613. if (sink) {
  614. if (aconnector->dc_sink) {
  615. amdgpu_dm_remove_sink_from_freesync_module(
  616. connector);
  617. /* retain and release bellow are used for
  618. * bump up refcount for sink because the link don't point
  619. * to it anymore after disconnect so on next crtc to connector
  620. * reshuffle by UMD we will get into unwanted dc_sink release
  621. */
  622. if (aconnector->dc_sink != aconnector->dc_em_sink)
  623. dc_sink_release(aconnector->dc_sink);
  624. }
  625. aconnector->dc_sink = sink;
  626. amdgpu_dm_add_sink_to_freesync_module(
  627. connector, aconnector->edid);
  628. } else {
  629. amdgpu_dm_remove_sink_from_freesync_module(connector);
  630. if (!aconnector->dc_sink)
  631. aconnector->dc_sink = aconnector->dc_em_sink;
  632. else if (aconnector->dc_sink != aconnector->dc_em_sink)
  633. dc_sink_retain(aconnector->dc_sink);
  634. }
  635. mutex_unlock(&dev->mode_config.mutex);
  636. return;
  637. }
  638. /*
  639. * TODO: temporary guard to look for proper fix
  640. * if this sink is MST sink, we should not do anything
  641. */
  642. if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST)
  643. return;
  644. if (aconnector->dc_sink == sink) {
  645. /* We got a DP short pulse (Link Loss, DP CTS, etc...).
  646. * Do nothing!! */
  647. DRM_INFO("DCHPD: connector_id=%d: dc_sink didn't change.\n",
  648. aconnector->connector_id);
  649. return;
  650. }
  651. DRM_INFO("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n",
  652. aconnector->connector_id, aconnector->dc_sink, sink);
  653. mutex_lock(&dev->mode_config.mutex);
  654. /* 1. Update status of the drm connector
  655. * 2. Send an event and let userspace tell us what to do */
  656. if (sink) {
  657. /* TODO: check if we still need the S3 mode update workaround.
  658. * If yes, put it here. */
  659. if (aconnector->dc_sink)
  660. amdgpu_dm_remove_sink_from_freesync_module(
  661. connector);
  662. aconnector->dc_sink = sink;
  663. if (sink->dc_edid.length == 0)
  664. aconnector->edid = NULL;
  665. else {
  666. aconnector->edid =
  667. (struct edid *) sink->dc_edid.raw_edid;
  668. drm_mode_connector_update_edid_property(connector,
  669. aconnector->edid);
  670. }
  671. amdgpu_dm_add_sink_to_freesync_module(connector, aconnector->edid);
  672. } else {
  673. amdgpu_dm_remove_sink_from_freesync_module(connector);
  674. drm_mode_connector_update_edid_property(connector, NULL);
  675. aconnector->num_modes = 0;
  676. aconnector->dc_sink = NULL;
  677. }
  678. mutex_unlock(&dev->mode_config.mutex);
  679. }
  680. static void handle_hpd_irq(void *param)
  681. {
  682. struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
  683. struct drm_connector *connector = &aconnector->base;
  684. struct drm_device *dev = connector->dev;
  685. /* In case of failure or MST no need to update connector status or notify the OS
  686. * since (for MST case) MST does this in it's own context.
  687. */
  688. mutex_lock(&aconnector->hpd_lock);
  689. if (aconnector->fake_enable)
  690. aconnector->fake_enable = false;
  691. if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) {
  692. amdgpu_dm_update_connector_after_detect(aconnector);
  693. drm_modeset_lock_all(dev);
  694. dm_restore_drm_connector_state(dev, connector);
  695. drm_modeset_unlock_all(dev);
  696. if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
  697. drm_kms_helper_hotplug_event(dev);
  698. }
  699. mutex_unlock(&aconnector->hpd_lock);
  700. }
  701. static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector)
  702. {
  703. uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 };
  704. uint8_t dret;
  705. bool new_irq_handled = false;
  706. int dpcd_addr;
  707. int dpcd_bytes_to_read;
  708. const int max_process_count = 30;
  709. int process_count = 0;
  710. const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link);
  711. if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) {
  712. dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT;
  713. /* DPCD 0x200 - 0x201 for downstream IRQ */
  714. dpcd_addr = DP_SINK_COUNT;
  715. } else {
  716. dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI;
  717. /* DPCD 0x2002 - 0x2005 for downstream IRQ */
  718. dpcd_addr = DP_SINK_COUNT_ESI;
  719. }
  720. dret = drm_dp_dpcd_read(
  721. &aconnector->dm_dp_aux.aux,
  722. dpcd_addr,
  723. esi,
  724. dpcd_bytes_to_read);
  725. while (dret == dpcd_bytes_to_read &&
  726. process_count < max_process_count) {
  727. uint8_t retry;
  728. dret = 0;
  729. process_count++;
  730. DRM_DEBUG_KMS("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]);
  731. /* handle HPD short pulse irq */
  732. if (aconnector->mst_mgr.mst_state)
  733. drm_dp_mst_hpd_irq(
  734. &aconnector->mst_mgr,
  735. esi,
  736. &new_irq_handled);
  737. if (new_irq_handled) {
  738. /* ACK at DPCD to notify down stream */
  739. const int ack_dpcd_bytes_to_write =
  740. dpcd_bytes_to_read - 1;
  741. for (retry = 0; retry < 3; retry++) {
  742. uint8_t wret;
  743. wret = drm_dp_dpcd_write(
  744. &aconnector->dm_dp_aux.aux,
  745. dpcd_addr + 1,
  746. &esi[1],
  747. ack_dpcd_bytes_to_write);
  748. if (wret == ack_dpcd_bytes_to_write)
  749. break;
  750. }
  751. /* check if there is new irq to be handle */
  752. dret = drm_dp_dpcd_read(
  753. &aconnector->dm_dp_aux.aux,
  754. dpcd_addr,
  755. esi,
  756. dpcd_bytes_to_read);
  757. new_irq_handled = false;
  758. } else
  759. break;
  760. }
  761. if (process_count == max_process_count)
  762. DRM_DEBUG_KMS("Loop exceeded max iterations\n");
  763. }
  764. static void handle_hpd_rx_irq(void *param)
  765. {
  766. struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param;
  767. struct drm_connector *connector = &aconnector->base;
  768. struct drm_device *dev = connector->dev;
  769. const struct dc_link *dc_link = aconnector->dc_link;
  770. bool is_mst_root_connector = aconnector->mst_mgr.mst_state;
  771. /* TODO:Temporary add mutex to protect hpd interrupt not have a gpio
  772. * conflict, after implement i2c helper, this mutex should be
  773. * retired.
  774. */
  775. if (aconnector->dc_link->type != dc_connection_mst_branch)
  776. mutex_lock(&aconnector->hpd_lock);
  777. if (dc_link_handle_hpd_rx_irq(aconnector->dc_link, NULL) &&
  778. !is_mst_root_connector) {
  779. /* Downstream Port status changed. */
  780. if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPDRX)) {
  781. amdgpu_dm_update_connector_after_detect(aconnector);
  782. drm_modeset_lock_all(dev);
  783. dm_restore_drm_connector_state(dev, connector);
  784. drm_modeset_unlock_all(dev);
  785. drm_kms_helper_hotplug_event(dev);
  786. }
  787. }
  788. if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) ||
  789. (dc_link->type == dc_connection_mst_branch))
  790. dm_handle_hpd_rx_irq(aconnector);
  791. if (aconnector->dc_link->type != dc_connection_mst_branch)
  792. mutex_unlock(&aconnector->hpd_lock);
  793. }
  794. static void register_hpd_handlers(struct amdgpu_device *adev)
  795. {
  796. struct drm_device *dev = adev->ddev;
  797. struct drm_connector *connector;
  798. struct amdgpu_dm_connector *aconnector;
  799. const struct dc_link *dc_link;
  800. struct dc_interrupt_params int_params = {0};
  801. int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
  802. int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
  803. list_for_each_entry(connector,
  804. &dev->mode_config.connector_list, head) {
  805. aconnector = to_amdgpu_dm_connector(connector);
  806. dc_link = aconnector->dc_link;
  807. if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) {
  808. int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
  809. int_params.irq_source = dc_link->irq_source_hpd;
  810. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  811. handle_hpd_irq,
  812. (void *) aconnector);
  813. }
  814. if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) {
  815. /* Also register for DP short pulse (hpd_rx). */
  816. int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT;
  817. int_params.irq_source = dc_link->irq_source_hpd_rx;
  818. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  819. handle_hpd_rx_irq,
  820. (void *) aconnector);
  821. }
  822. }
  823. }
  824. /* Register IRQ sources and initialize IRQ callbacks */
  825. static int dce110_register_irq_handlers(struct amdgpu_device *adev)
  826. {
  827. struct dc *dc = adev->dm.dc;
  828. struct common_irq_params *c_irq_params;
  829. struct dc_interrupt_params int_params = {0};
  830. int r;
  831. int i;
  832. unsigned client_id = AMDGPU_IH_CLIENTID_LEGACY;
  833. if (adev->asic_type == CHIP_VEGA10 ||
  834. adev->asic_type == CHIP_RAVEN)
  835. client_id = AMDGPU_IH_CLIENTID_DCE;
  836. int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
  837. int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
  838. /* Actions of amdgpu_irq_add_id():
  839. * 1. Register a set() function with base driver.
  840. * Base driver will call set() function to enable/disable an
  841. * interrupt in DC hardware.
  842. * 2. Register amdgpu_dm_irq_handler().
  843. * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
  844. * coming from DC hardware.
  845. * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
  846. * for acknowledging and handling. */
  847. /* Use VBLANK interrupt */
  848. for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) {
  849. r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq);
  850. if (r) {
  851. DRM_ERROR("Failed to add crtc irq id!\n");
  852. return r;
  853. }
  854. int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
  855. int_params.irq_source =
  856. dc_interrupt_to_irq_source(dc, i, 0);
  857. c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
  858. c_irq_params->adev = adev;
  859. c_irq_params->irq_src = int_params.irq_source;
  860. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  861. dm_crtc_high_irq, c_irq_params);
  862. }
  863. /* Use GRPH_PFLIP interrupt */
  864. for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP;
  865. i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) {
  866. r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq);
  867. if (r) {
  868. DRM_ERROR("Failed to add page flip irq id!\n");
  869. return r;
  870. }
  871. int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
  872. int_params.irq_source =
  873. dc_interrupt_to_irq_source(dc, i, 0);
  874. c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
  875. c_irq_params->adev = adev;
  876. c_irq_params->irq_src = int_params.irq_source;
  877. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  878. dm_pflip_high_irq, c_irq_params);
  879. }
  880. /* HPD */
  881. r = amdgpu_irq_add_id(adev, client_id,
  882. VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
  883. if (r) {
  884. DRM_ERROR("Failed to add hpd irq id!\n");
  885. return r;
  886. }
  887. register_hpd_handlers(adev);
  888. return 0;
  889. }
  890. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  891. /* Register IRQ sources and initialize IRQ callbacks */
  892. static int dcn10_register_irq_handlers(struct amdgpu_device *adev)
  893. {
  894. struct dc *dc = adev->dm.dc;
  895. struct common_irq_params *c_irq_params;
  896. struct dc_interrupt_params int_params = {0};
  897. int r;
  898. int i;
  899. int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT;
  900. int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT;
  901. /* Actions of amdgpu_irq_add_id():
  902. * 1. Register a set() function with base driver.
  903. * Base driver will call set() function to enable/disable an
  904. * interrupt in DC hardware.
  905. * 2. Register amdgpu_dm_irq_handler().
  906. * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts
  907. * coming from DC hardware.
  908. * amdgpu_dm_irq_handler() will re-direct the interrupt to DC
  909. * for acknowledging and handling.
  910. * */
  911. /* Use VSTARTUP interrupt */
  912. for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP;
  913. i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1;
  914. i++) {
  915. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_DCE, i, &adev->crtc_irq);
  916. if (r) {
  917. DRM_ERROR("Failed to add crtc irq id!\n");
  918. return r;
  919. }
  920. int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
  921. int_params.irq_source =
  922. dc_interrupt_to_irq_source(dc, i, 0);
  923. c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1];
  924. c_irq_params->adev = adev;
  925. c_irq_params->irq_src = int_params.irq_source;
  926. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  927. dm_crtc_high_irq, c_irq_params);
  928. }
  929. /* Use GRPH_PFLIP interrupt */
  930. for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT;
  931. i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1;
  932. i++) {
  933. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_DCE, i, &adev->pageflip_irq);
  934. if (r) {
  935. DRM_ERROR("Failed to add page flip irq id!\n");
  936. return r;
  937. }
  938. int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT;
  939. int_params.irq_source =
  940. dc_interrupt_to_irq_source(dc, i, 0);
  941. c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST];
  942. c_irq_params->adev = adev;
  943. c_irq_params->irq_src = int_params.irq_source;
  944. amdgpu_dm_irq_register_interrupt(adev, &int_params,
  945. dm_pflip_high_irq, c_irq_params);
  946. }
  947. /* HPD */
  948. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT,
  949. &adev->hpd_irq);
  950. if (r) {
  951. DRM_ERROR("Failed to add hpd irq id!\n");
  952. return r;
  953. }
  954. register_hpd_handlers(adev);
  955. return 0;
  956. }
  957. #endif
  958. static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev)
  959. {
  960. int r;
  961. adev->mode_info.mode_config_initialized = true;
  962. adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs;
  963. adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs;
  964. adev->ddev->mode_config.max_width = 16384;
  965. adev->ddev->mode_config.max_height = 16384;
  966. adev->ddev->mode_config.preferred_depth = 24;
  967. adev->ddev->mode_config.prefer_shadow = 1;
  968. /* indicate support of immediate flip */
  969. adev->ddev->mode_config.async_page_flip = true;
  970. adev->ddev->mode_config.fb_base = adev->mc.aper_base;
  971. r = amdgpu_modeset_create_props(adev);
  972. if (r)
  973. return r;
  974. return 0;
  975. }
  976. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
  977. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  978. static int amdgpu_dm_backlight_update_status(struct backlight_device *bd)
  979. {
  980. struct amdgpu_display_manager *dm = bl_get_data(bd);
  981. if (dc_link_set_backlight_level(dm->backlight_link,
  982. bd->props.brightness, 0, 0))
  983. return 0;
  984. else
  985. return 1;
  986. }
  987. static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd)
  988. {
  989. return bd->props.brightness;
  990. }
  991. static const struct backlight_ops amdgpu_dm_backlight_ops = {
  992. .get_brightness = amdgpu_dm_backlight_get_brightness,
  993. .update_status = amdgpu_dm_backlight_update_status,
  994. };
  995. void amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm)
  996. {
  997. char bl_name[16];
  998. struct backlight_properties props = { 0 };
  999. props.max_brightness = AMDGPU_MAX_BL_LEVEL;
  1000. props.type = BACKLIGHT_RAW;
  1001. snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d",
  1002. dm->adev->ddev->primary->index);
  1003. dm->backlight_dev = backlight_device_register(bl_name,
  1004. dm->adev->ddev->dev,
  1005. dm,
  1006. &amdgpu_dm_backlight_ops,
  1007. &props);
  1008. if (NULL == dm->backlight_dev)
  1009. DRM_ERROR("DM: Backlight registration failed!\n");
  1010. else
  1011. DRM_INFO("DM: Registered Backlight device: %s\n", bl_name);
  1012. }
  1013. #endif
  1014. /* In this architecture, the association
  1015. * connector -> encoder -> crtc
  1016. * id not really requried. The crtc and connector will hold the
  1017. * display_index as an abstraction to use with DAL component
  1018. *
  1019. * Returns 0 on success
  1020. */
  1021. int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
  1022. {
  1023. struct amdgpu_display_manager *dm = &adev->dm;
  1024. uint32_t i;
  1025. struct amdgpu_dm_connector *aconnector = NULL;
  1026. struct amdgpu_encoder *aencoder = NULL;
  1027. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  1028. uint32_t link_cnt;
  1029. unsigned long possible_crtcs;
  1030. link_cnt = dm->dc->caps.max_links;
  1031. if (amdgpu_dm_mode_config_init(dm->adev)) {
  1032. DRM_ERROR("DM: Failed to initialize mode config\n");
  1033. return -1;
  1034. }
  1035. for (i = 0; i < dm->dc->caps.max_planes; i++) {
  1036. mode_info->planes[i] = kzalloc(sizeof(struct amdgpu_plane),
  1037. GFP_KERNEL);
  1038. if (!mode_info->planes[i]) {
  1039. DRM_ERROR("KMS: Failed to allocate plane\n");
  1040. goto fail_free_planes;
  1041. }
  1042. mode_info->planes[i]->base.type = mode_info->plane_type[i];
  1043. /*
  1044. * HACK: IGT tests expect that each plane can only have one
  1045. * one possible CRTC. For now, set one CRTC for each
  1046. * plane that is not an underlay, but still allow multiple
  1047. * CRTCs for underlay planes.
  1048. */
  1049. possible_crtcs = 1 << i;
  1050. if (i >= dm->dc->caps.max_streams)
  1051. possible_crtcs = 0xff;
  1052. if (amdgpu_dm_plane_init(dm, mode_info->planes[i], possible_crtcs)) {
  1053. DRM_ERROR("KMS: Failed to initialize plane\n");
  1054. goto fail_free_planes;
  1055. }
  1056. }
  1057. for (i = 0; i < dm->dc->caps.max_streams; i++)
  1058. if (amdgpu_dm_crtc_init(dm, &mode_info->planes[i]->base, i)) {
  1059. DRM_ERROR("KMS: Failed to initialize crtc\n");
  1060. goto fail_free_planes;
  1061. }
  1062. dm->display_indexes_num = dm->dc->caps.max_streams;
  1063. /* loops over all connectors on the board */
  1064. for (i = 0; i < link_cnt; i++) {
  1065. if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
  1066. DRM_ERROR(
  1067. "KMS: Cannot support more than %d display indexes\n",
  1068. AMDGPU_DM_MAX_DISPLAY_INDEX);
  1069. continue;
  1070. }
  1071. aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
  1072. if (!aconnector)
  1073. goto fail_free_planes;
  1074. aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
  1075. if (!aencoder) {
  1076. goto fail_free_connector;
  1077. }
  1078. if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
  1079. DRM_ERROR("KMS: Failed to initialize encoder\n");
  1080. goto fail_free_encoder;
  1081. }
  1082. if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
  1083. DRM_ERROR("KMS: Failed to initialize connector\n");
  1084. goto fail_free_encoder;
  1085. }
  1086. if (dc_link_detect(dc_get_link_at_index(dm->dc, i),
  1087. DETECT_REASON_BOOT))
  1088. amdgpu_dm_update_connector_after_detect(aconnector);
  1089. }
  1090. /* Software is initialized. Now we can register interrupt handlers. */
  1091. switch (adev->asic_type) {
  1092. case CHIP_BONAIRE:
  1093. case CHIP_HAWAII:
  1094. case CHIP_KAVERI:
  1095. case CHIP_KABINI:
  1096. case CHIP_MULLINS:
  1097. case CHIP_TONGA:
  1098. case CHIP_FIJI:
  1099. case CHIP_CARRIZO:
  1100. case CHIP_STONEY:
  1101. case CHIP_POLARIS11:
  1102. case CHIP_POLARIS10:
  1103. case CHIP_POLARIS12:
  1104. case CHIP_VEGA10:
  1105. if (dce110_register_irq_handlers(dm->adev)) {
  1106. DRM_ERROR("DM: Failed to initialize IRQ\n");
  1107. goto fail_free_encoder;
  1108. }
  1109. break;
  1110. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  1111. case CHIP_RAVEN:
  1112. if (dcn10_register_irq_handlers(dm->adev)) {
  1113. DRM_ERROR("DM: Failed to initialize IRQ\n");
  1114. goto fail_free_encoder;
  1115. }
  1116. /*
  1117. * Temporary disable until pplib/smu interaction is implemented
  1118. */
  1119. dm->dc->debug.disable_stutter = true;
  1120. break;
  1121. #endif
  1122. default:
  1123. DRM_ERROR("Usupported ASIC type: 0x%X\n", adev->asic_type);
  1124. goto fail_free_encoder;
  1125. }
  1126. drm_mode_config_reset(dm->ddev);
  1127. return 0;
  1128. fail_free_encoder:
  1129. kfree(aencoder);
  1130. fail_free_connector:
  1131. kfree(aconnector);
  1132. fail_free_planes:
  1133. for (i = 0; i < dm->dc->caps.max_planes; i++)
  1134. kfree(mode_info->planes[i]);
  1135. return -1;
  1136. }
  1137. void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
  1138. {
  1139. drm_mode_config_cleanup(dm->ddev);
  1140. return;
  1141. }
  1142. /******************************************************************************
  1143. * amdgpu_display_funcs functions
  1144. *****************************************************************************/
  1145. /**
  1146. * dm_bandwidth_update - program display watermarks
  1147. *
  1148. * @adev: amdgpu_device pointer
  1149. *
  1150. * Calculate and program the display watermarks and line buffer allocation.
  1151. */
  1152. static void dm_bandwidth_update(struct amdgpu_device *adev)
  1153. {
  1154. /* TODO: implement later */
  1155. }
  1156. static void dm_set_backlight_level(struct amdgpu_encoder *amdgpu_encoder,
  1157. u8 level)
  1158. {
  1159. /* TODO: translate amdgpu_encoder to display_index and call DAL */
  1160. }
  1161. static u8 dm_get_backlight_level(struct amdgpu_encoder *amdgpu_encoder)
  1162. {
  1163. /* TODO: translate amdgpu_encoder to display_index and call DAL */
  1164. return 0;
  1165. }
  1166. static int amdgpu_notify_freesync(struct drm_device *dev, void *data,
  1167. struct drm_file *filp)
  1168. {
  1169. struct mod_freesync_params freesync_params;
  1170. uint8_t num_streams;
  1171. uint8_t i;
  1172. struct amdgpu_device *adev = dev->dev_private;
  1173. int r = 0;
  1174. /* Get freesync enable flag from DRM */
  1175. num_streams = dc_get_current_stream_count(adev->dm.dc);
  1176. for (i = 0; i < num_streams; i++) {
  1177. struct dc_stream_state *stream;
  1178. stream = dc_get_stream_at_index(adev->dm.dc, i);
  1179. mod_freesync_update_state(adev->dm.freesync_module,
  1180. &stream, 1, &freesync_params);
  1181. }
  1182. return r;
  1183. }
  1184. static const struct amdgpu_display_funcs dm_display_funcs = {
  1185. .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
  1186. .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
  1187. .vblank_wait = NULL,
  1188. .backlight_set_level =
  1189. dm_set_backlight_level,/* called unconditionally */
  1190. .backlight_get_level =
  1191. dm_get_backlight_level,/* called unconditionally */
  1192. .hpd_sense = NULL,/* called unconditionally */
  1193. .hpd_set_polarity = NULL, /* called unconditionally */
  1194. .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
  1195. .page_flip_get_scanoutpos =
  1196. dm_crtc_get_scanoutpos,/* called unconditionally */
  1197. .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
  1198. .add_connector = NULL, /* VBIOS parsing. DAL does it. */
  1199. .notify_freesync = amdgpu_notify_freesync,
  1200. };
  1201. #if defined(CONFIG_DEBUG_KERNEL_DC)
  1202. static ssize_t s3_debug_store(
  1203. struct device *device,
  1204. struct device_attribute *attr,
  1205. const char *buf,
  1206. size_t count)
  1207. {
  1208. int ret;
  1209. int s3_state;
  1210. struct pci_dev *pdev = to_pci_dev(device);
  1211. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  1212. struct amdgpu_device *adev = drm_dev->dev_private;
  1213. ret = kstrtoint(buf, 0, &s3_state);
  1214. if (ret == 0) {
  1215. if (s3_state) {
  1216. dm_resume(adev);
  1217. amdgpu_dm_display_resume(adev);
  1218. drm_kms_helper_hotplug_event(adev->ddev);
  1219. } else
  1220. dm_suspend(adev);
  1221. }
  1222. return ret == 0 ? count : 0;
  1223. }
  1224. DEVICE_ATTR_WO(s3_debug);
  1225. #endif
  1226. static int dm_early_init(void *handle)
  1227. {
  1228. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  1229. adev->ddev->driver->driver_features |= DRIVER_ATOMIC;
  1230. amdgpu_dm_set_irq_funcs(adev);
  1231. switch (adev->asic_type) {
  1232. case CHIP_BONAIRE:
  1233. case CHIP_HAWAII:
  1234. adev->mode_info.num_crtc = 6;
  1235. adev->mode_info.num_hpd = 6;
  1236. adev->mode_info.num_dig = 6;
  1237. adev->mode_info.plane_type = dm_plane_type_default;
  1238. break;
  1239. case CHIP_KAVERI:
  1240. adev->mode_info.num_crtc = 4;
  1241. adev->mode_info.num_hpd = 6;
  1242. adev->mode_info.num_dig = 7;
  1243. adev->mode_info.plane_type = dm_plane_type_default;
  1244. break;
  1245. case CHIP_KABINI:
  1246. case CHIP_MULLINS:
  1247. adev->mode_info.num_crtc = 2;
  1248. adev->mode_info.num_hpd = 6;
  1249. adev->mode_info.num_dig = 6;
  1250. adev->mode_info.plane_type = dm_plane_type_default;
  1251. break;
  1252. case CHIP_FIJI:
  1253. case CHIP_TONGA:
  1254. adev->mode_info.num_crtc = 6;
  1255. adev->mode_info.num_hpd = 6;
  1256. adev->mode_info.num_dig = 7;
  1257. adev->mode_info.plane_type = dm_plane_type_default;
  1258. break;
  1259. case CHIP_CARRIZO:
  1260. adev->mode_info.num_crtc = 3;
  1261. adev->mode_info.num_hpd = 6;
  1262. adev->mode_info.num_dig = 9;
  1263. adev->mode_info.plane_type = dm_plane_type_carizzo;
  1264. break;
  1265. case CHIP_STONEY:
  1266. adev->mode_info.num_crtc = 2;
  1267. adev->mode_info.num_hpd = 6;
  1268. adev->mode_info.num_dig = 9;
  1269. adev->mode_info.plane_type = dm_plane_type_stoney;
  1270. break;
  1271. case CHIP_POLARIS11:
  1272. case CHIP_POLARIS12:
  1273. adev->mode_info.num_crtc = 5;
  1274. adev->mode_info.num_hpd = 5;
  1275. adev->mode_info.num_dig = 5;
  1276. adev->mode_info.plane_type = dm_plane_type_default;
  1277. break;
  1278. case CHIP_POLARIS10:
  1279. adev->mode_info.num_crtc = 6;
  1280. adev->mode_info.num_hpd = 6;
  1281. adev->mode_info.num_dig = 6;
  1282. adev->mode_info.plane_type = dm_plane_type_default;
  1283. break;
  1284. case CHIP_VEGA10:
  1285. adev->mode_info.num_crtc = 6;
  1286. adev->mode_info.num_hpd = 6;
  1287. adev->mode_info.num_dig = 6;
  1288. adev->mode_info.plane_type = dm_plane_type_default;
  1289. break;
  1290. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  1291. case CHIP_RAVEN:
  1292. adev->mode_info.num_crtc = 4;
  1293. adev->mode_info.num_hpd = 4;
  1294. adev->mode_info.num_dig = 4;
  1295. adev->mode_info.plane_type = dm_plane_type_default;
  1296. break;
  1297. #endif
  1298. default:
  1299. DRM_ERROR("Usupported ASIC type: 0x%X\n", adev->asic_type);
  1300. return -EINVAL;
  1301. }
  1302. if (adev->mode_info.funcs == NULL)
  1303. adev->mode_info.funcs = &dm_display_funcs;
  1304. /* Note: Do NOT change adev->audio_endpt_rreg and
  1305. * adev->audio_endpt_wreg because they are initialised in
  1306. * amdgpu_device_init() */
  1307. #if defined(CONFIG_DEBUG_KERNEL_DC)
  1308. device_create_file(
  1309. adev->ddev->dev,
  1310. &dev_attr_s3_debug);
  1311. #endif
  1312. return 0;
  1313. }
  1314. bool amdgpu_dm_acquire_dal_lock(struct amdgpu_display_manager *dm)
  1315. {
  1316. /* TODO */
  1317. return true;
  1318. }
  1319. bool amdgpu_dm_release_dal_lock(struct amdgpu_display_manager *dm)
  1320. {
  1321. /* TODO */
  1322. return true;
  1323. }
  1324. struct dm_connector_state {
  1325. struct drm_connector_state base;
  1326. enum amdgpu_rmx_type scaling;
  1327. uint8_t underscan_vborder;
  1328. uint8_t underscan_hborder;
  1329. bool underscan_enable;
  1330. };
  1331. #define to_dm_connector_state(x)\
  1332. container_of((x), struct dm_connector_state, base)
  1333. static bool modeset_required(struct drm_crtc_state *crtc_state,
  1334. struct dc_stream_state *new_stream,
  1335. struct dc_stream_state *old_stream)
  1336. {
  1337. if (!drm_atomic_crtc_needs_modeset(crtc_state))
  1338. return false;
  1339. if (!crtc_state->enable)
  1340. return false;
  1341. return crtc_state->active;
  1342. }
  1343. static bool modereset_required(struct drm_crtc_state *crtc_state)
  1344. {
  1345. if (!drm_atomic_crtc_needs_modeset(crtc_state))
  1346. return false;
  1347. return !crtc_state->enable || !crtc_state->active;
  1348. }
  1349. void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
  1350. {
  1351. drm_encoder_cleanup(encoder);
  1352. kfree(encoder);
  1353. }
  1354. static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
  1355. .destroy = amdgpu_dm_encoder_destroy,
  1356. };
  1357. static bool fill_rects_from_plane_state(
  1358. const struct drm_plane_state *state,
  1359. struct dc_plane_state *plane_state)
  1360. {
  1361. plane_state->src_rect.x = state->src_x >> 16;
  1362. plane_state->src_rect.y = state->src_y >> 16;
  1363. /*we ignore for now mantissa and do not to deal with floating pixels :(*/
  1364. plane_state->src_rect.width = state->src_w >> 16;
  1365. if (plane_state->src_rect.width == 0)
  1366. return false;
  1367. plane_state->src_rect.height = state->src_h >> 16;
  1368. if (plane_state->src_rect.height == 0)
  1369. return false;
  1370. plane_state->dst_rect.x = state->crtc_x;
  1371. plane_state->dst_rect.y = state->crtc_y;
  1372. if (state->crtc_w == 0)
  1373. return false;
  1374. plane_state->dst_rect.width = state->crtc_w;
  1375. if (state->crtc_h == 0)
  1376. return false;
  1377. plane_state->dst_rect.height = state->crtc_h;
  1378. plane_state->clip_rect = plane_state->dst_rect;
  1379. switch (state->rotation & DRM_MODE_ROTATE_MASK) {
  1380. case DRM_MODE_ROTATE_0:
  1381. plane_state->rotation = ROTATION_ANGLE_0;
  1382. break;
  1383. case DRM_MODE_ROTATE_90:
  1384. plane_state->rotation = ROTATION_ANGLE_90;
  1385. break;
  1386. case DRM_MODE_ROTATE_180:
  1387. plane_state->rotation = ROTATION_ANGLE_180;
  1388. break;
  1389. case DRM_MODE_ROTATE_270:
  1390. plane_state->rotation = ROTATION_ANGLE_270;
  1391. break;
  1392. default:
  1393. plane_state->rotation = ROTATION_ANGLE_0;
  1394. break;
  1395. }
  1396. return true;
  1397. }
  1398. static int get_fb_info(
  1399. const struct amdgpu_framebuffer *amdgpu_fb,
  1400. uint64_t *tiling_flags,
  1401. uint64_t *fb_location)
  1402. {
  1403. struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  1404. int r = amdgpu_bo_reserve(rbo, false);
  1405. if (unlikely(r)) {
  1406. DRM_ERROR("Unable to reserve buffer\n");
  1407. return r;
  1408. }
  1409. if (fb_location)
  1410. *fb_location = amdgpu_bo_gpu_offset(rbo);
  1411. if (tiling_flags)
  1412. amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
  1413. amdgpu_bo_unreserve(rbo);
  1414. return r;
  1415. }
  1416. static int fill_plane_attributes_from_fb(
  1417. struct amdgpu_device *adev,
  1418. struct dc_plane_state *plane_state,
  1419. const struct amdgpu_framebuffer *amdgpu_fb, bool addReq)
  1420. {
  1421. uint64_t tiling_flags;
  1422. uint64_t fb_location = 0;
  1423. unsigned int awidth;
  1424. const struct drm_framebuffer *fb = &amdgpu_fb->base;
  1425. int ret = 0;
  1426. struct drm_format_name_buf format_name;
  1427. ret = get_fb_info(
  1428. amdgpu_fb,
  1429. &tiling_flags,
  1430. addReq == true ? &fb_location:NULL);
  1431. if (ret)
  1432. return ret;
  1433. switch (fb->format->format) {
  1434. case DRM_FORMAT_C8:
  1435. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
  1436. break;
  1437. case DRM_FORMAT_RGB565:
  1438. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
  1439. break;
  1440. case DRM_FORMAT_XRGB8888:
  1441. case DRM_FORMAT_ARGB8888:
  1442. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
  1443. break;
  1444. case DRM_FORMAT_XRGB2101010:
  1445. case DRM_FORMAT_ARGB2101010:
  1446. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
  1447. break;
  1448. case DRM_FORMAT_XBGR2101010:
  1449. case DRM_FORMAT_ABGR2101010:
  1450. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
  1451. break;
  1452. case DRM_FORMAT_NV21:
  1453. plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
  1454. break;
  1455. case DRM_FORMAT_NV12:
  1456. plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
  1457. break;
  1458. default:
  1459. DRM_ERROR("Unsupported screen format %s\n",
  1460. drm_get_format_name(fb->format->format, &format_name));
  1461. return -EINVAL;
  1462. }
  1463. if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
  1464. plane_state->address.type = PLN_ADDR_TYPE_GRAPHICS;
  1465. plane_state->address.grph.addr.low_part = lower_32_bits(fb_location);
  1466. plane_state->address.grph.addr.high_part = upper_32_bits(fb_location);
  1467. plane_state->plane_size.grph.surface_size.x = 0;
  1468. plane_state->plane_size.grph.surface_size.y = 0;
  1469. plane_state->plane_size.grph.surface_size.width = fb->width;
  1470. plane_state->plane_size.grph.surface_size.height = fb->height;
  1471. plane_state->plane_size.grph.surface_pitch =
  1472. fb->pitches[0] / fb->format->cpp[0];
  1473. /* TODO: unhardcode */
  1474. plane_state->color_space = COLOR_SPACE_SRGB;
  1475. } else {
  1476. awidth = ALIGN(fb->width, 64);
  1477. plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
  1478. plane_state->address.video_progressive.luma_addr.low_part
  1479. = lower_32_bits(fb_location);
  1480. plane_state->address.video_progressive.chroma_addr.low_part
  1481. = lower_32_bits(fb_location) +
  1482. (awidth * fb->height);
  1483. plane_state->plane_size.video.luma_size.x = 0;
  1484. plane_state->plane_size.video.luma_size.y = 0;
  1485. plane_state->plane_size.video.luma_size.width = awidth;
  1486. plane_state->plane_size.video.luma_size.height = fb->height;
  1487. /* TODO: unhardcode */
  1488. plane_state->plane_size.video.luma_pitch = awidth;
  1489. plane_state->plane_size.video.chroma_size.x = 0;
  1490. plane_state->plane_size.video.chroma_size.y = 0;
  1491. plane_state->plane_size.video.chroma_size.width = awidth;
  1492. plane_state->plane_size.video.chroma_size.height = fb->height;
  1493. plane_state->plane_size.video.chroma_pitch = awidth / 2;
  1494. /* TODO: unhardcode */
  1495. plane_state->color_space = COLOR_SPACE_YCBCR709;
  1496. }
  1497. memset(&plane_state->tiling_info, 0, sizeof(plane_state->tiling_info));
  1498. /* Fill GFX8 params */
  1499. if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
  1500. unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
  1501. bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
  1502. bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
  1503. mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
  1504. tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
  1505. num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
  1506. /* XXX fix me for VI */
  1507. plane_state->tiling_info.gfx8.num_banks = num_banks;
  1508. plane_state->tiling_info.gfx8.array_mode =
  1509. DC_ARRAY_2D_TILED_THIN1;
  1510. plane_state->tiling_info.gfx8.tile_split = tile_split;
  1511. plane_state->tiling_info.gfx8.bank_width = bankw;
  1512. plane_state->tiling_info.gfx8.bank_height = bankh;
  1513. plane_state->tiling_info.gfx8.tile_aspect = mtaspect;
  1514. plane_state->tiling_info.gfx8.tile_mode =
  1515. DC_ADDR_SURF_MICRO_TILING_DISPLAY;
  1516. } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
  1517. == DC_ARRAY_1D_TILED_THIN1) {
  1518. plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
  1519. }
  1520. plane_state->tiling_info.gfx8.pipe_config =
  1521. AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
  1522. if (adev->asic_type == CHIP_VEGA10 ||
  1523. adev->asic_type == CHIP_RAVEN) {
  1524. /* Fill GFX9 params */
  1525. plane_state->tiling_info.gfx9.num_pipes =
  1526. adev->gfx.config.gb_addr_config_fields.num_pipes;
  1527. plane_state->tiling_info.gfx9.num_banks =
  1528. adev->gfx.config.gb_addr_config_fields.num_banks;
  1529. plane_state->tiling_info.gfx9.pipe_interleave =
  1530. adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
  1531. plane_state->tiling_info.gfx9.num_shader_engines =
  1532. adev->gfx.config.gb_addr_config_fields.num_se;
  1533. plane_state->tiling_info.gfx9.max_compressed_frags =
  1534. adev->gfx.config.gb_addr_config_fields.max_compress_frags;
  1535. plane_state->tiling_info.gfx9.num_rb_per_se =
  1536. adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
  1537. plane_state->tiling_info.gfx9.swizzle =
  1538. AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
  1539. plane_state->tiling_info.gfx9.shaderEnable = 1;
  1540. }
  1541. plane_state->visible = true;
  1542. plane_state->scaling_quality.h_taps_c = 0;
  1543. plane_state->scaling_quality.v_taps_c = 0;
  1544. /* is this needed? is plane_state zeroed at allocation? */
  1545. plane_state->scaling_quality.h_taps = 0;
  1546. plane_state->scaling_quality.v_taps = 0;
  1547. plane_state->stereo_format = PLANE_STEREO_FORMAT_NONE;
  1548. return ret;
  1549. }
  1550. static void fill_gamma_from_crtc_state(
  1551. const struct drm_crtc_state *crtc_state,
  1552. struct dc_plane_state *plane_state)
  1553. {
  1554. int i;
  1555. struct dc_gamma *gamma;
  1556. struct drm_color_lut *lut =
  1557. (struct drm_color_lut *) crtc_state->gamma_lut->data;
  1558. gamma = dc_create_gamma();
  1559. if (gamma == NULL) {
  1560. WARN_ON(1);
  1561. return;
  1562. }
  1563. gamma->type = GAMMA_RGB_256;
  1564. gamma->num_entries = GAMMA_RGB_256_ENTRIES;
  1565. for (i = 0; i < GAMMA_RGB_256_ENTRIES; i++) {
  1566. gamma->entries.red[i] = dal_fixed31_32_from_int(lut[i].red);
  1567. gamma->entries.green[i] = dal_fixed31_32_from_int(lut[i].green);
  1568. gamma->entries.blue[i] = dal_fixed31_32_from_int(lut[i].blue);
  1569. }
  1570. plane_state->gamma_correction = gamma;
  1571. }
  1572. static int fill_plane_attributes(
  1573. struct amdgpu_device *adev,
  1574. struct dc_plane_state *dc_plane_state,
  1575. struct drm_plane_state *plane_state,
  1576. struct drm_crtc_state *crtc_state,
  1577. bool addrReq)
  1578. {
  1579. const struct amdgpu_framebuffer *amdgpu_fb =
  1580. to_amdgpu_framebuffer(plane_state->fb);
  1581. const struct drm_crtc *crtc = plane_state->crtc;
  1582. struct dc_transfer_func *input_tf;
  1583. int ret = 0;
  1584. if (!fill_rects_from_plane_state(plane_state, dc_plane_state))
  1585. return -EINVAL;
  1586. ret = fill_plane_attributes_from_fb(
  1587. crtc->dev->dev_private,
  1588. dc_plane_state,
  1589. amdgpu_fb,
  1590. addrReq);
  1591. if (ret)
  1592. return ret;
  1593. input_tf = dc_create_transfer_func();
  1594. if (input_tf == NULL)
  1595. return -ENOMEM;
  1596. input_tf->type = TF_TYPE_PREDEFINED;
  1597. input_tf->tf = TRANSFER_FUNCTION_SRGB;
  1598. dc_plane_state->in_transfer_func = input_tf;
  1599. /* In case of gamma set, update gamma value */
  1600. if (crtc_state->gamma_lut)
  1601. fill_gamma_from_crtc_state(crtc_state, dc_plane_state);
  1602. return ret;
  1603. }
  1604. /*****************************************************************************/
  1605. struct amdgpu_dm_connector *aconnector_from_drm_crtc_id(
  1606. const struct drm_crtc *crtc)
  1607. {
  1608. struct drm_device *dev = crtc->dev;
  1609. struct drm_connector *connector;
  1610. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  1611. struct amdgpu_dm_connector *aconnector;
  1612. list_for_each_entry(connector,
  1613. &dev->mode_config.connector_list, head) {
  1614. aconnector = to_amdgpu_dm_connector(connector);
  1615. if (aconnector->base.state->crtc != &acrtc->base)
  1616. continue;
  1617. /* Found the connector */
  1618. return aconnector;
  1619. }
  1620. /* If we get here, not found. */
  1621. return NULL;
  1622. }
  1623. static void update_stream_scaling_settings(
  1624. const struct drm_display_mode *mode,
  1625. const struct dm_connector_state *dm_state,
  1626. struct dc_stream_state *stream)
  1627. {
  1628. enum amdgpu_rmx_type rmx_type;
  1629. struct rect src = { 0 }; /* viewport in composition space*/
  1630. struct rect dst = { 0 }; /* stream addressable area */
  1631. /* no mode. nothing to be done */
  1632. if (!mode)
  1633. return;
  1634. /* Full screen scaling by default */
  1635. src.width = mode->hdisplay;
  1636. src.height = mode->vdisplay;
  1637. dst.width = stream->timing.h_addressable;
  1638. dst.height = stream->timing.v_addressable;
  1639. rmx_type = dm_state->scaling;
  1640. if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
  1641. if (src.width * dst.height <
  1642. src.height * dst.width) {
  1643. /* height needs less upscaling/more downscaling */
  1644. dst.width = src.width *
  1645. dst.height / src.height;
  1646. } else {
  1647. /* width needs less upscaling/more downscaling */
  1648. dst.height = src.height *
  1649. dst.width / src.width;
  1650. }
  1651. } else if (rmx_type == RMX_CENTER) {
  1652. dst = src;
  1653. }
  1654. dst.x = (stream->timing.h_addressable - dst.width) / 2;
  1655. dst.y = (stream->timing.v_addressable - dst.height) / 2;
  1656. if (dm_state->underscan_enable) {
  1657. dst.x += dm_state->underscan_hborder / 2;
  1658. dst.y += dm_state->underscan_vborder / 2;
  1659. dst.width -= dm_state->underscan_hborder;
  1660. dst.height -= dm_state->underscan_vborder;
  1661. }
  1662. stream->src = src;
  1663. stream->dst = dst;
  1664. DRM_DEBUG_KMS("Destination Rectangle x:%d y:%d width:%d height:%d\n",
  1665. dst.x, dst.y, dst.width, dst.height);
  1666. }
  1667. static enum dc_color_depth convert_color_depth_from_display_info(
  1668. const struct drm_connector *connector)
  1669. {
  1670. uint32_t bpc = connector->display_info.bpc;
  1671. /* Limited color depth to 8bit
  1672. * TODO: Still need to handle deep color
  1673. */
  1674. if (bpc > 8)
  1675. bpc = 8;
  1676. switch (bpc) {
  1677. case 0:
  1678. /* Temporary Work around, DRM don't parse color depth for
  1679. * EDID revision before 1.4
  1680. * TODO: Fix edid parsing
  1681. */
  1682. return COLOR_DEPTH_888;
  1683. case 6:
  1684. return COLOR_DEPTH_666;
  1685. case 8:
  1686. return COLOR_DEPTH_888;
  1687. case 10:
  1688. return COLOR_DEPTH_101010;
  1689. case 12:
  1690. return COLOR_DEPTH_121212;
  1691. case 14:
  1692. return COLOR_DEPTH_141414;
  1693. case 16:
  1694. return COLOR_DEPTH_161616;
  1695. default:
  1696. return COLOR_DEPTH_UNDEFINED;
  1697. }
  1698. }
  1699. static enum dc_aspect_ratio get_aspect_ratio(
  1700. const struct drm_display_mode *mode_in)
  1701. {
  1702. int32_t width = mode_in->crtc_hdisplay * 9;
  1703. int32_t height = mode_in->crtc_vdisplay * 16;
  1704. if ((width - height) < 10 && (width - height) > -10)
  1705. return ASPECT_RATIO_16_9;
  1706. else
  1707. return ASPECT_RATIO_4_3;
  1708. }
  1709. static enum dc_color_space get_output_color_space(
  1710. const struct dc_crtc_timing *dc_crtc_timing)
  1711. {
  1712. enum dc_color_space color_space = COLOR_SPACE_SRGB;
  1713. switch (dc_crtc_timing->pixel_encoding) {
  1714. case PIXEL_ENCODING_YCBCR422:
  1715. case PIXEL_ENCODING_YCBCR444:
  1716. case PIXEL_ENCODING_YCBCR420:
  1717. {
  1718. /*
  1719. * 27030khz is the separation point between HDTV and SDTV
  1720. * according to HDMI spec, we use YCbCr709 and YCbCr601
  1721. * respectively
  1722. */
  1723. if (dc_crtc_timing->pix_clk_khz > 27030) {
  1724. if (dc_crtc_timing->flags.Y_ONLY)
  1725. color_space =
  1726. COLOR_SPACE_YCBCR709_LIMITED;
  1727. else
  1728. color_space = COLOR_SPACE_YCBCR709;
  1729. } else {
  1730. if (dc_crtc_timing->flags.Y_ONLY)
  1731. color_space =
  1732. COLOR_SPACE_YCBCR601_LIMITED;
  1733. else
  1734. color_space = COLOR_SPACE_YCBCR601;
  1735. }
  1736. }
  1737. break;
  1738. case PIXEL_ENCODING_RGB:
  1739. color_space = COLOR_SPACE_SRGB;
  1740. break;
  1741. default:
  1742. WARN_ON(1);
  1743. break;
  1744. }
  1745. return color_space;
  1746. }
  1747. /*****************************************************************************/
  1748. static void fill_stream_properties_from_drm_display_mode(
  1749. struct dc_stream_state *stream,
  1750. const struct drm_display_mode *mode_in,
  1751. const struct drm_connector *connector)
  1752. {
  1753. struct dc_crtc_timing *timing_out = &stream->timing;
  1754. memset(timing_out, 0, sizeof(struct dc_crtc_timing));
  1755. timing_out->h_border_left = 0;
  1756. timing_out->h_border_right = 0;
  1757. timing_out->v_border_top = 0;
  1758. timing_out->v_border_bottom = 0;
  1759. /* TODO: un-hardcode */
  1760. if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
  1761. && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
  1762. timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
  1763. else
  1764. timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
  1765. timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
  1766. timing_out->display_color_depth = convert_color_depth_from_display_info(
  1767. connector);
  1768. timing_out->scan_type = SCANNING_TYPE_NODATA;
  1769. timing_out->hdmi_vic = 0;
  1770. timing_out->vic = drm_match_cea_mode(mode_in);
  1771. timing_out->h_addressable = mode_in->crtc_hdisplay;
  1772. timing_out->h_total = mode_in->crtc_htotal;
  1773. timing_out->h_sync_width =
  1774. mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
  1775. timing_out->h_front_porch =
  1776. mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
  1777. timing_out->v_total = mode_in->crtc_vtotal;
  1778. timing_out->v_addressable = mode_in->crtc_vdisplay;
  1779. timing_out->v_front_porch =
  1780. mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
  1781. timing_out->v_sync_width =
  1782. mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
  1783. timing_out->pix_clk_khz = mode_in->crtc_clock;
  1784. timing_out->aspect_ratio = get_aspect_ratio(mode_in);
  1785. if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
  1786. timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
  1787. if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
  1788. timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
  1789. stream->output_color_space = get_output_color_space(timing_out);
  1790. {
  1791. struct dc_transfer_func *tf = dc_create_transfer_func();
  1792. tf->type = TF_TYPE_PREDEFINED;
  1793. tf->tf = TRANSFER_FUNCTION_SRGB;
  1794. stream->out_transfer_func = tf;
  1795. }
  1796. }
  1797. static void fill_audio_info(
  1798. struct audio_info *audio_info,
  1799. const struct drm_connector *drm_connector,
  1800. const struct dc_sink *dc_sink)
  1801. {
  1802. int i = 0;
  1803. int cea_revision = 0;
  1804. const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
  1805. audio_info->manufacture_id = edid_caps->manufacturer_id;
  1806. audio_info->product_id = edid_caps->product_id;
  1807. cea_revision = drm_connector->display_info.cea_rev;
  1808. while (i < AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS &&
  1809. edid_caps->display_name[i]) {
  1810. audio_info->display_name[i] = edid_caps->display_name[i];
  1811. i++;
  1812. }
  1813. if (cea_revision >= 3) {
  1814. audio_info->mode_count = edid_caps->audio_mode_count;
  1815. for (i = 0; i < audio_info->mode_count; ++i) {
  1816. audio_info->modes[i].format_code =
  1817. (enum audio_format_code)
  1818. (edid_caps->audio_modes[i].format_code);
  1819. audio_info->modes[i].channel_count =
  1820. edid_caps->audio_modes[i].channel_count;
  1821. audio_info->modes[i].sample_rates.all =
  1822. edid_caps->audio_modes[i].sample_rate;
  1823. audio_info->modes[i].sample_size =
  1824. edid_caps->audio_modes[i].sample_size;
  1825. }
  1826. }
  1827. audio_info->flags.all = edid_caps->speaker_flags;
  1828. /* TODO: We only check for the progressive mode, check for interlace mode too */
  1829. if (drm_connector->latency_present[0]) {
  1830. audio_info->video_latency = drm_connector->video_latency[0];
  1831. audio_info->audio_latency = drm_connector->audio_latency[0];
  1832. }
  1833. /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
  1834. }
  1835. static void copy_crtc_timing_for_drm_display_mode(
  1836. const struct drm_display_mode *src_mode,
  1837. struct drm_display_mode *dst_mode)
  1838. {
  1839. dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
  1840. dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
  1841. dst_mode->crtc_clock = src_mode->crtc_clock;
  1842. dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
  1843. dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
  1844. dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
  1845. dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
  1846. dst_mode->crtc_htotal = src_mode->crtc_htotal;
  1847. dst_mode->crtc_hskew = src_mode->crtc_hskew;
  1848. dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
  1849. dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
  1850. dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
  1851. dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
  1852. dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
  1853. }
  1854. static void decide_crtc_timing_for_drm_display_mode(
  1855. struct drm_display_mode *drm_mode,
  1856. const struct drm_display_mode *native_mode,
  1857. bool scale_enabled)
  1858. {
  1859. if (scale_enabled) {
  1860. copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
  1861. } else if (native_mode->clock == drm_mode->clock &&
  1862. native_mode->htotal == drm_mode->htotal &&
  1863. native_mode->vtotal == drm_mode->vtotal) {
  1864. copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
  1865. } else {
  1866. /* no scaling nor amdgpu inserted, no need to patch */
  1867. }
  1868. }
  1869. static void create_fake_sink(struct amdgpu_dm_connector *aconnector)
  1870. {
  1871. struct dc_sink *sink = NULL;
  1872. struct dc_sink_init_data sink_init_data = { 0 };
  1873. sink_init_data.link = aconnector->dc_link;
  1874. sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
  1875. sink = dc_sink_create(&sink_init_data);
  1876. if (!sink)
  1877. DRM_ERROR("Failed to create sink!\n");
  1878. sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
  1879. aconnector->fake_enable = true;
  1880. aconnector->dc_sink = sink;
  1881. aconnector->dc_link->local_sink = sink;
  1882. }
  1883. static struct dc_stream_state *create_stream_for_sink(
  1884. struct amdgpu_dm_connector *aconnector,
  1885. const struct drm_display_mode *drm_mode,
  1886. const struct dm_connector_state *dm_state)
  1887. {
  1888. struct drm_display_mode *preferred_mode = NULL;
  1889. const struct drm_connector *drm_connector;
  1890. struct dc_stream_state *stream = NULL;
  1891. struct drm_display_mode mode = *drm_mode;
  1892. bool native_mode_found = false;
  1893. if (aconnector == NULL) {
  1894. DRM_ERROR("aconnector is NULL!\n");
  1895. goto drm_connector_null;
  1896. }
  1897. if (dm_state == NULL) {
  1898. DRM_ERROR("dm_state is NULL!\n");
  1899. goto dm_state_null;
  1900. }
  1901. drm_connector = &aconnector->base;
  1902. if (!aconnector->dc_sink)
  1903. create_fake_sink(aconnector);
  1904. stream = dc_create_stream_for_sink(aconnector->dc_sink);
  1905. if (stream == NULL) {
  1906. DRM_ERROR("Failed to create stream for sink!\n");
  1907. goto stream_create_fail;
  1908. }
  1909. list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
  1910. /* Search for preferred mode */
  1911. if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
  1912. native_mode_found = true;
  1913. break;
  1914. }
  1915. }
  1916. if (!native_mode_found)
  1917. preferred_mode = list_first_entry_or_null(
  1918. &aconnector->base.modes,
  1919. struct drm_display_mode,
  1920. head);
  1921. if (preferred_mode == NULL) {
  1922. /* This may not be an error, the use case is when we we have no
  1923. * usermode calls to reset and set mode upon hotplug. In this
  1924. * case, we call set mode ourselves to restore the previous mode
  1925. * and the modelist may not be filled in in time.
  1926. */
  1927. DRM_INFO("No preferred mode found\n");
  1928. } else {
  1929. decide_crtc_timing_for_drm_display_mode(
  1930. &mode, preferred_mode,
  1931. dm_state->scaling != RMX_OFF);
  1932. }
  1933. fill_stream_properties_from_drm_display_mode(stream,
  1934. &mode, &aconnector->base);
  1935. update_stream_scaling_settings(&mode, dm_state, stream);
  1936. fill_audio_info(
  1937. &stream->audio_info,
  1938. drm_connector,
  1939. aconnector->dc_sink);
  1940. stream_create_fail:
  1941. dm_state_null:
  1942. drm_connector_null:
  1943. return stream;
  1944. }
  1945. void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
  1946. {
  1947. drm_crtc_cleanup(crtc);
  1948. kfree(crtc);
  1949. }
  1950. static void dm_crtc_destroy_state(struct drm_crtc *crtc,
  1951. struct drm_crtc_state *state)
  1952. {
  1953. struct dm_crtc_state *cur = to_dm_crtc_state(state);
  1954. /* TODO Destroy dc_stream objects are stream object is flattened */
  1955. if (cur->stream)
  1956. dc_stream_release(cur->stream);
  1957. __drm_atomic_helper_crtc_destroy_state(state);
  1958. kfree(state);
  1959. }
  1960. static void dm_crtc_reset_state(struct drm_crtc *crtc)
  1961. {
  1962. struct dm_crtc_state *state;
  1963. if (crtc->state)
  1964. dm_crtc_destroy_state(crtc, crtc->state);
  1965. state = kzalloc(sizeof(*state), GFP_KERNEL);
  1966. if (WARN_ON(!state))
  1967. return;
  1968. crtc->state = &state->base;
  1969. crtc->state->crtc = crtc;
  1970. }
  1971. static struct drm_crtc_state *
  1972. dm_crtc_duplicate_state(struct drm_crtc *crtc)
  1973. {
  1974. struct dm_crtc_state *state, *cur;
  1975. cur = to_dm_crtc_state(crtc->state);
  1976. if (WARN_ON(!crtc->state))
  1977. return NULL;
  1978. state = dm_alloc(sizeof(*state));
  1979. __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
  1980. if (cur->stream) {
  1981. state->stream = cur->stream;
  1982. dc_stream_retain(state->stream);
  1983. }
  1984. /* TODO Duplicate dc_stream after objects are stream object is flattened */
  1985. return &state->base;
  1986. }
  1987. /* Implemented only the options currently availible for the driver */
  1988. static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
  1989. .reset = dm_crtc_reset_state,
  1990. .destroy = amdgpu_dm_crtc_destroy,
  1991. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  1992. .set_config = drm_atomic_helper_set_config,
  1993. .page_flip = drm_atomic_helper_page_flip,
  1994. .atomic_duplicate_state = dm_crtc_duplicate_state,
  1995. .atomic_destroy_state = dm_crtc_destroy_state,
  1996. };
  1997. static enum drm_connector_status
  1998. amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
  1999. {
  2000. bool connected;
  2001. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  2002. /* Notes:
  2003. * 1. This interface is NOT called in context of HPD irq.
  2004. * 2. This interface *is called* in context of user-mode ioctl. Which
  2005. * makes it a bad place for *any* MST-related activit. */
  2006. if (aconnector->base.force == DRM_FORCE_UNSPECIFIED)
  2007. connected = (aconnector->dc_sink != NULL);
  2008. else
  2009. connected = (aconnector->base.force == DRM_FORCE_ON);
  2010. return (connected ? connector_status_connected :
  2011. connector_status_disconnected);
  2012. }
  2013. int amdgpu_dm_connector_atomic_set_property(
  2014. struct drm_connector *connector,
  2015. struct drm_connector_state *connector_state,
  2016. struct drm_property *property,
  2017. uint64_t val)
  2018. {
  2019. struct drm_device *dev = connector->dev;
  2020. struct amdgpu_device *adev = dev->dev_private;
  2021. struct dm_connector_state *dm_old_state =
  2022. to_dm_connector_state(connector->state);
  2023. struct dm_connector_state *dm_new_state =
  2024. to_dm_connector_state(connector_state);
  2025. int ret = -EINVAL;
  2026. if (property == dev->mode_config.scaling_mode_property) {
  2027. enum amdgpu_rmx_type rmx_type;
  2028. switch (val) {
  2029. case DRM_MODE_SCALE_CENTER:
  2030. rmx_type = RMX_CENTER;
  2031. break;
  2032. case DRM_MODE_SCALE_ASPECT:
  2033. rmx_type = RMX_ASPECT;
  2034. break;
  2035. case DRM_MODE_SCALE_FULLSCREEN:
  2036. rmx_type = RMX_FULL;
  2037. break;
  2038. case DRM_MODE_SCALE_NONE:
  2039. default:
  2040. rmx_type = RMX_OFF;
  2041. break;
  2042. }
  2043. if (dm_old_state->scaling == rmx_type)
  2044. return 0;
  2045. dm_new_state->scaling = rmx_type;
  2046. ret = 0;
  2047. } else if (property == adev->mode_info.underscan_hborder_property) {
  2048. dm_new_state->underscan_hborder = val;
  2049. ret = 0;
  2050. } else if (property == adev->mode_info.underscan_vborder_property) {
  2051. dm_new_state->underscan_vborder = val;
  2052. ret = 0;
  2053. } else if (property == adev->mode_info.underscan_property) {
  2054. dm_new_state->underscan_enable = val;
  2055. ret = 0;
  2056. }
  2057. return ret;
  2058. }
  2059. int amdgpu_dm_connector_atomic_get_property(
  2060. struct drm_connector *connector,
  2061. const struct drm_connector_state *state,
  2062. struct drm_property *property,
  2063. uint64_t *val)
  2064. {
  2065. struct drm_device *dev = connector->dev;
  2066. struct amdgpu_device *adev = dev->dev_private;
  2067. struct dm_connector_state *dm_state =
  2068. to_dm_connector_state(state);
  2069. int ret = -EINVAL;
  2070. if (property == dev->mode_config.scaling_mode_property) {
  2071. switch (dm_state->scaling) {
  2072. case RMX_CENTER:
  2073. *val = DRM_MODE_SCALE_CENTER;
  2074. break;
  2075. case RMX_ASPECT:
  2076. *val = DRM_MODE_SCALE_ASPECT;
  2077. break;
  2078. case RMX_FULL:
  2079. *val = DRM_MODE_SCALE_FULLSCREEN;
  2080. break;
  2081. case RMX_OFF:
  2082. default:
  2083. *val = DRM_MODE_SCALE_NONE;
  2084. break;
  2085. }
  2086. ret = 0;
  2087. } else if (property == adev->mode_info.underscan_hborder_property) {
  2088. *val = dm_state->underscan_hborder;
  2089. ret = 0;
  2090. } else if (property == adev->mode_info.underscan_vborder_property) {
  2091. *val = dm_state->underscan_vborder;
  2092. ret = 0;
  2093. } else if (property == adev->mode_info.underscan_property) {
  2094. *val = dm_state->underscan_enable;
  2095. ret = 0;
  2096. }
  2097. return ret;
  2098. }
  2099. void amdgpu_dm_connector_destroy(struct drm_connector *connector)
  2100. {
  2101. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  2102. const struct dc_link *link = aconnector->dc_link;
  2103. struct amdgpu_device *adev = connector->dev->dev_private;
  2104. struct amdgpu_display_manager *dm = &adev->dm;
  2105. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
  2106. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  2107. if (link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) {
  2108. amdgpu_dm_register_backlight_device(dm);
  2109. if (dm->backlight_dev) {
  2110. backlight_device_unregister(dm->backlight_dev);
  2111. dm->backlight_dev = NULL;
  2112. }
  2113. }
  2114. #endif
  2115. drm_connector_unregister(connector);
  2116. drm_connector_cleanup(connector);
  2117. kfree(connector);
  2118. }
  2119. void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
  2120. {
  2121. struct dm_connector_state *state =
  2122. to_dm_connector_state(connector->state);
  2123. kfree(state);
  2124. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2125. if (state) {
  2126. state->scaling = RMX_OFF;
  2127. state->underscan_enable = false;
  2128. state->underscan_hborder = 0;
  2129. state->underscan_vborder = 0;
  2130. connector->state = &state->base;
  2131. connector->state->connector = connector;
  2132. }
  2133. }
  2134. struct drm_connector_state *amdgpu_dm_connector_atomic_duplicate_state(
  2135. struct drm_connector *connector)
  2136. {
  2137. struct dm_connector_state *state =
  2138. to_dm_connector_state(connector->state);
  2139. struct dm_connector_state *new_state =
  2140. kmemdup(state, sizeof(*state), GFP_KERNEL);
  2141. if (new_state) {
  2142. __drm_atomic_helper_connector_duplicate_state(connector,
  2143. &new_state->base);
  2144. return &new_state->base;
  2145. }
  2146. return NULL;
  2147. }
  2148. static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
  2149. .reset = amdgpu_dm_connector_funcs_reset,
  2150. .detect = amdgpu_dm_connector_detect,
  2151. .fill_modes = drm_helper_probe_single_connector_modes,
  2152. .destroy = amdgpu_dm_connector_destroy,
  2153. .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
  2154. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  2155. .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
  2156. .atomic_get_property = amdgpu_dm_connector_atomic_get_property
  2157. };
  2158. static struct drm_encoder *best_encoder(struct drm_connector *connector)
  2159. {
  2160. int enc_id = connector->encoder_ids[0];
  2161. struct drm_mode_object *obj;
  2162. struct drm_encoder *encoder;
  2163. DRM_DEBUG_KMS("Finding the best encoder\n");
  2164. /* pick the encoder ids */
  2165. if (enc_id) {
  2166. obj = drm_mode_object_find(connector->dev, enc_id, DRM_MODE_OBJECT_ENCODER);
  2167. if (!obj) {
  2168. DRM_ERROR("Couldn't find a matching encoder for our connector\n");
  2169. return NULL;
  2170. }
  2171. encoder = obj_to_encoder(obj);
  2172. return encoder;
  2173. }
  2174. DRM_ERROR("No encoder id\n");
  2175. return NULL;
  2176. }
  2177. static int get_modes(struct drm_connector *connector)
  2178. {
  2179. return amdgpu_dm_connector_get_modes(connector);
  2180. }
  2181. static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
  2182. {
  2183. struct dc_sink_init_data init_params = {
  2184. .link = aconnector->dc_link,
  2185. .sink_signal = SIGNAL_TYPE_VIRTUAL
  2186. };
  2187. struct edid *edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
  2188. if (!aconnector->base.edid_blob_ptr ||
  2189. !aconnector->base.edid_blob_ptr->data) {
  2190. DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
  2191. aconnector->base.name);
  2192. aconnector->base.force = DRM_FORCE_OFF;
  2193. aconnector->base.override_edid = false;
  2194. return;
  2195. }
  2196. aconnector->edid = edid;
  2197. aconnector->dc_em_sink = dc_link_add_remote_sink(
  2198. aconnector->dc_link,
  2199. (uint8_t *)edid,
  2200. (edid->extensions + 1) * EDID_LENGTH,
  2201. &init_params);
  2202. if (aconnector->base.force
  2203. == DRM_FORCE_ON)
  2204. aconnector->dc_sink = aconnector->dc_link->local_sink ?
  2205. aconnector->dc_link->local_sink :
  2206. aconnector->dc_em_sink;
  2207. }
  2208. static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
  2209. {
  2210. struct dc_link *link = (struct dc_link *)aconnector->dc_link;
  2211. /* In case of headless boot with force on for DP managed connector
  2212. * Those settings have to be != 0 to get initial modeset
  2213. */
  2214. if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
  2215. link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
  2216. link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
  2217. }
  2218. aconnector->base.override_edid = true;
  2219. create_eml_sink(aconnector);
  2220. }
  2221. int amdgpu_dm_connector_mode_valid(
  2222. struct drm_connector *connector,
  2223. struct drm_display_mode *mode)
  2224. {
  2225. int result = MODE_ERROR;
  2226. struct dc_sink *dc_sink;
  2227. struct amdgpu_device *adev = connector->dev->dev_private;
  2228. /* TODO: Unhardcode stream count */
  2229. struct dc_stream_state *stream;
  2230. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  2231. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  2232. (mode->flags & DRM_MODE_FLAG_DBLSCAN))
  2233. return result;
  2234. /* Only run this the first time mode_valid is called to initilialize
  2235. * EDID mgmt
  2236. */
  2237. if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
  2238. !aconnector->dc_em_sink)
  2239. handle_edid_mgmt(aconnector);
  2240. dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
  2241. if (dc_sink == NULL) {
  2242. DRM_ERROR("dc_sink is NULL!\n");
  2243. goto fail;
  2244. }
  2245. stream = dc_create_stream_for_sink(dc_sink);
  2246. if (stream == NULL) {
  2247. DRM_ERROR("Failed to create stream for sink!\n");
  2248. goto fail;
  2249. }
  2250. drm_mode_set_crtcinfo(mode, 0);
  2251. fill_stream_properties_from_drm_display_mode(stream, mode, connector);
  2252. stream->src.width = mode->hdisplay;
  2253. stream->src.height = mode->vdisplay;
  2254. stream->dst = stream->src;
  2255. if (dc_validate_stream(adev->dm.dc, stream))
  2256. result = MODE_OK;
  2257. dc_stream_release(stream);
  2258. fail:
  2259. /* TODO: error handling*/
  2260. return result;
  2261. }
  2262. static const struct drm_connector_helper_funcs
  2263. amdgpu_dm_connector_helper_funcs = {
  2264. /*
  2265. * If hotplug a second bigger display in FB Con mode, bigger resolution
  2266. * modes will be filtered by drm_mode_validate_size(), and those modes
  2267. * is missing after user start lightdm. So we need to renew modes list.
  2268. * in get_modes call back, not just return the modes count
  2269. */
  2270. .get_modes = get_modes,
  2271. .mode_valid = amdgpu_dm_connector_mode_valid,
  2272. .best_encoder = best_encoder
  2273. };
  2274. static void dm_crtc_helper_disable(struct drm_crtc *crtc)
  2275. {
  2276. }
  2277. static int dm_crtc_helper_atomic_check(
  2278. struct drm_crtc *crtc,
  2279. struct drm_crtc_state *state)
  2280. {
  2281. struct amdgpu_device *adev = crtc->dev->dev_private;
  2282. struct dc *dc = adev->dm.dc;
  2283. struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
  2284. int ret = -EINVAL;
  2285. if (unlikely(!dm_crtc_state->stream &&
  2286. modeset_required(state, NULL, dm_crtc_state->stream))) {
  2287. WARN_ON(1);
  2288. return ret;
  2289. }
  2290. /* In some use cases, like reset, no stream is attached */
  2291. if (!dm_crtc_state->stream)
  2292. return 0;
  2293. if (dc_validate_stream(dc, dm_crtc_state->stream))
  2294. return 0;
  2295. return ret;
  2296. }
  2297. static bool dm_crtc_helper_mode_fixup(
  2298. struct drm_crtc *crtc,
  2299. const struct drm_display_mode *mode,
  2300. struct drm_display_mode *adjusted_mode)
  2301. {
  2302. return true;
  2303. }
  2304. static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
  2305. .disable = dm_crtc_helper_disable,
  2306. .atomic_check = dm_crtc_helper_atomic_check,
  2307. .mode_fixup = dm_crtc_helper_mode_fixup
  2308. };
  2309. static void dm_encoder_helper_disable(struct drm_encoder *encoder)
  2310. {
  2311. }
  2312. static int dm_encoder_helper_atomic_check(
  2313. struct drm_encoder *encoder,
  2314. struct drm_crtc_state *crtc_state,
  2315. struct drm_connector_state *conn_state)
  2316. {
  2317. return 0;
  2318. }
  2319. const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
  2320. .disable = dm_encoder_helper_disable,
  2321. .atomic_check = dm_encoder_helper_atomic_check
  2322. };
  2323. static void dm_drm_plane_reset(struct drm_plane *plane)
  2324. {
  2325. struct dm_plane_state *amdgpu_state = NULL;
  2326. if (plane->state)
  2327. plane->funcs->atomic_destroy_state(plane, plane->state);
  2328. amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
  2329. if (amdgpu_state) {
  2330. plane->state = &amdgpu_state->base;
  2331. plane->state->plane = plane;
  2332. plane->state->rotation = DRM_MODE_ROTATE_0;
  2333. } else
  2334. WARN_ON(1);
  2335. }
  2336. static struct drm_plane_state *
  2337. dm_drm_plane_duplicate_state(struct drm_plane *plane)
  2338. {
  2339. struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
  2340. old_dm_plane_state = to_dm_plane_state(plane->state);
  2341. dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
  2342. if (!dm_plane_state)
  2343. return NULL;
  2344. __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
  2345. if (old_dm_plane_state->dc_state) {
  2346. dm_plane_state->dc_state = old_dm_plane_state->dc_state;
  2347. dc_plane_state_retain(dm_plane_state->dc_state);
  2348. }
  2349. return &dm_plane_state->base;
  2350. }
  2351. void dm_drm_plane_destroy_state(struct drm_plane *plane,
  2352. struct drm_plane_state *state)
  2353. {
  2354. struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
  2355. if (dm_plane_state->dc_state)
  2356. dc_plane_state_release(dm_plane_state->dc_state);
  2357. drm_atomic_helper_plane_destroy_state(plane, state);
  2358. }
  2359. static const struct drm_plane_funcs dm_plane_funcs = {
  2360. .update_plane = drm_atomic_helper_update_plane,
  2361. .disable_plane = drm_atomic_helper_disable_plane,
  2362. .destroy = drm_plane_cleanup,
  2363. .reset = dm_drm_plane_reset,
  2364. .atomic_duplicate_state = dm_drm_plane_duplicate_state,
  2365. .atomic_destroy_state = dm_drm_plane_destroy_state,
  2366. };
  2367. static int dm_plane_helper_prepare_fb(
  2368. struct drm_plane *plane,
  2369. struct drm_plane_state *new_state)
  2370. {
  2371. struct amdgpu_framebuffer *afb;
  2372. struct drm_gem_object *obj;
  2373. struct amdgpu_bo *rbo;
  2374. int r;
  2375. struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
  2376. unsigned int awidth;
  2377. dm_plane_state_old = to_dm_plane_state(plane->state);
  2378. dm_plane_state_new = to_dm_plane_state(new_state);
  2379. if (!new_state->fb) {
  2380. DRM_DEBUG_KMS("No FB bound\n");
  2381. return 0;
  2382. }
  2383. afb = to_amdgpu_framebuffer(new_state->fb);
  2384. obj = afb->obj;
  2385. rbo = gem_to_amdgpu_bo(obj);
  2386. r = amdgpu_bo_reserve(rbo, false);
  2387. if (unlikely(r != 0))
  2388. return r;
  2389. r = amdgpu_bo_pin(rbo, AMDGPU_GEM_DOMAIN_VRAM, &afb->address);
  2390. amdgpu_bo_unreserve(rbo);
  2391. if (unlikely(r != 0)) {
  2392. DRM_ERROR("Failed to pin framebuffer\n");
  2393. return r;
  2394. }
  2395. amdgpu_bo_ref(rbo);
  2396. if (dm_plane_state_new->dc_state &&
  2397. dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
  2398. struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
  2399. if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
  2400. plane_state->address.grph.addr.low_part = lower_32_bits(afb->address);
  2401. plane_state->address.grph.addr.high_part = upper_32_bits(afb->address);
  2402. } else {
  2403. awidth = ALIGN(new_state->fb->width, 64);
  2404. plane_state->address.video_progressive.luma_addr.low_part
  2405. = lower_32_bits(afb->address);
  2406. plane_state->address.video_progressive.chroma_addr.low_part
  2407. = lower_32_bits(afb->address) +
  2408. (awidth * new_state->fb->height);
  2409. }
  2410. }
  2411. /* It's a hack for s3 since in 4.9 kernel filter out cursor buffer
  2412. * prepare and cleanup in drm_atomic_helper_prepare_planes
  2413. * and drm_atomic_helper_cleanup_planes because fb doens't in s3.
  2414. * IN 4.10 kernel this code should be removed and amdgpu_device_suspend
  2415. * code touching fram buffers should be avoided for DC.
  2416. */
  2417. if (plane->type == DRM_PLANE_TYPE_CURSOR) {
  2418. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_state->crtc);
  2419. acrtc->cursor_bo = obj;
  2420. }
  2421. return 0;
  2422. }
  2423. static void dm_plane_helper_cleanup_fb(
  2424. struct drm_plane *plane,
  2425. struct drm_plane_state *old_state)
  2426. {
  2427. struct amdgpu_bo *rbo;
  2428. struct amdgpu_framebuffer *afb;
  2429. int r;
  2430. if (!old_state->fb)
  2431. return;
  2432. afb = to_amdgpu_framebuffer(old_state->fb);
  2433. rbo = gem_to_amdgpu_bo(afb->obj);
  2434. r = amdgpu_bo_reserve(rbo, false);
  2435. if (unlikely(r)) {
  2436. DRM_ERROR("failed to reserve rbo before unpin\n");
  2437. return;
  2438. }
  2439. amdgpu_bo_unpin(rbo);
  2440. amdgpu_bo_unreserve(rbo);
  2441. amdgpu_bo_unref(&rbo);
  2442. }
  2443. int dm_create_validation_set_for_connector(struct drm_connector *connector,
  2444. struct drm_display_mode *mode, struct dc_validation_set *val_set)
  2445. {
  2446. int result = MODE_ERROR;
  2447. struct dc_sink *dc_sink =
  2448. to_amdgpu_dm_connector(connector)->dc_sink;
  2449. /* TODO: Unhardcode stream count */
  2450. struct dc_stream_state *stream;
  2451. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  2452. (mode->flags & DRM_MODE_FLAG_DBLSCAN))
  2453. return result;
  2454. if (dc_sink == NULL) {
  2455. DRM_ERROR("dc_sink is NULL!\n");
  2456. return result;
  2457. }
  2458. stream = dc_create_stream_for_sink(dc_sink);
  2459. if (stream == NULL) {
  2460. DRM_ERROR("Failed to create stream for sink!\n");
  2461. return result;
  2462. }
  2463. drm_mode_set_crtcinfo(mode, 0);
  2464. fill_stream_properties_from_drm_display_mode(stream, mode, connector);
  2465. val_set->stream = stream;
  2466. stream->src.width = mode->hdisplay;
  2467. stream->src.height = mode->vdisplay;
  2468. stream->dst = stream->src;
  2469. return MODE_OK;
  2470. }
  2471. int dm_plane_atomic_check(struct drm_plane *plane,
  2472. struct drm_plane_state *state)
  2473. {
  2474. struct amdgpu_device *adev = plane->dev->dev_private;
  2475. struct dc *dc = adev->dm.dc;
  2476. struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
  2477. if (!dm_plane_state->dc_state)
  2478. return 0;
  2479. if (dc_validate_plane(dc, dm_plane_state->dc_state))
  2480. return 0;
  2481. return -EINVAL;
  2482. }
  2483. static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
  2484. .prepare_fb = dm_plane_helper_prepare_fb,
  2485. .cleanup_fb = dm_plane_helper_cleanup_fb,
  2486. .atomic_check = dm_plane_atomic_check,
  2487. };
  2488. /*
  2489. * TODO: these are currently initialized to rgb formats only.
  2490. * For future use cases we should either initialize them dynamically based on
  2491. * plane capabilities, or initialize this array to all formats, so internal drm
  2492. * check will succeed, and let DC to implement proper check
  2493. */
  2494. static uint32_t rgb_formats[] = {
  2495. DRM_FORMAT_RGB888,
  2496. DRM_FORMAT_XRGB8888,
  2497. DRM_FORMAT_ARGB8888,
  2498. DRM_FORMAT_RGBA8888,
  2499. DRM_FORMAT_XRGB2101010,
  2500. DRM_FORMAT_XBGR2101010,
  2501. DRM_FORMAT_ARGB2101010,
  2502. DRM_FORMAT_ABGR2101010,
  2503. };
  2504. static uint32_t yuv_formats[] = {
  2505. DRM_FORMAT_NV12,
  2506. DRM_FORMAT_NV21,
  2507. };
  2508. static const u32 cursor_formats[] = {
  2509. DRM_FORMAT_ARGB8888
  2510. };
  2511. int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
  2512. struct amdgpu_plane *aplane,
  2513. unsigned long possible_crtcs)
  2514. {
  2515. int res = -EPERM;
  2516. switch (aplane->base.type) {
  2517. case DRM_PLANE_TYPE_PRIMARY:
  2518. aplane->base.format_default = true;
  2519. res = drm_universal_plane_init(
  2520. dm->adev->ddev,
  2521. &aplane->base,
  2522. possible_crtcs,
  2523. &dm_plane_funcs,
  2524. rgb_formats,
  2525. ARRAY_SIZE(rgb_formats),
  2526. NULL, aplane->base.type, NULL);
  2527. break;
  2528. case DRM_PLANE_TYPE_OVERLAY:
  2529. res = drm_universal_plane_init(
  2530. dm->adev->ddev,
  2531. &aplane->base,
  2532. possible_crtcs,
  2533. &dm_plane_funcs,
  2534. yuv_formats,
  2535. ARRAY_SIZE(yuv_formats),
  2536. NULL, aplane->base.type, NULL);
  2537. break;
  2538. case DRM_PLANE_TYPE_CURSOR:
  2539. res = drm_universal_plane_init(
  2540. dm->adev->ddev,
  2541. &aplane->base,
  2542. possible_crtcs,
  2543. &dm_plane_funcs,
  2544. cursor_formats,
  2545. ARRAY_SIZE(cursor_formats),
  2546. NULL, aplane->base.type, NULL);
  2547. break;
  2548. }
  2549. drm_plane_helper_add(&aplane->base, &dm_plane_helper_funcs);
  2550. return res;
  2551. }
  2552. int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
  2553. struct drm_plane *plane,
  2554. uint32_t crtc_index)
  2555. {
  2556. struct amdgpu_crtc *acrtc = NULL;
  2557. struct amdgpu_plane *cursor_plane;
  2558. int res = -ENOMEM;
  2559. cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
  2560. if (!cursor_plane)
  2561. goto fail;
  2562. cursor_plane->base.type = DRM_PLANE_TYPE_CURSOR;
  2563. res = amdgpu_dm_plane_init(dm, cursor_plane, 0);
  2564. acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
  2565. if (!acrtc)
  2566. goto fail;
  2567. res = drm_crtc_init_with_planes(
  2568. dm->ddev,
  2569. &acrtc->base,
  2570. plane,
  2571. &cursor_plane->base,
  2572. &amdgpu_dm_crtc_funcs, NULL);
  2573. if (res)
  2574. goto fail;
  2575. drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
  2576. acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
  2577. acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
  2578. acrtc->crtc_id = crtc_index;
  2579. acrtc->base.enabled = false;
  2580. dm->adev->mode_info.crtcs[crtc_index] = acrtc;
  2581. drm_mode_crtc_set_gamma_size(&acrtc->base, 256);
  2582. return 0;
  2583. fail:
  2584. kfree(acrtc);
  2585. kfree(cursor_plane);
  2586. acrtc->crtc_id = -1;
  2587. return res;
  2588. }
  2589. static int to_drm_connector_type(enum signal_type st)
  2590. {
  2591. switch (st) {
  2592. case SIGNAL_TYPE_HDMI_TYPE_A:
  2593. return DRM_MODE_CONNECTOR_HDMIA;
  2594. case SIGNAL_TYPE_EDP:
  2595. return DRM_MODE_CONNECTOR_eDP;
  2596. case SIGNAL_TYPE_RGB:
  2597. return DRM_MODE_CONNECTOR_VGA;
  2598. case SIGNAL_TYPE_DISPLAY_PORT:
  2599. case SIGNAL_TYPE_DISPLAY_PORT_MST:
  2600. return DRM_MODE_CONNECTOR_DisplayPort;
  2601. case SIGNAL_TYPE_DVI_DUAL_LINK:
  2602. case SIGNAL_TYPE_DVI_SINGLE_LINK:
  2603. return DRM_MODE_CONNECTOR_DVID;
  2604. case SIGNAL_TYPE_VIRTUAL:
  2605. return DRM_MODE_CONNECTOR_VIRTUAL;
  2606. default:
  2607. return DRM_MODE_CONNECTOR_Unknown;
  2608. }
  2609. }
  2610. static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
  2611. {
  2612. const struct drm_connector_helper_funcs *helper =
  2613. connector->helper_private;
  2614. struct drm_encoder *encoder;
  2615. struct amdgpu_encoder *amdgpu_encoder;
  2616. encoder = helper->best_encoder(connector);
  2617. if (encoder == NULL)
  2618. return;
  2619. amdgpu_encoder = to_amdgpu_encoder(encoder);
  2620. amdgpu_encoder->native_mode.clock = 0;
  2621. if (!list_empty(&connector->probed_modes)) {
  2622. struct drm_display_mode *preferred_mode = NULL;
  2623. list_for_each_entry(preferred_mode,
  2624. &connector->probed_modes,
  2625. head) {
  2626. if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
  2627. amdgpu_encoder->native_mode = *preferred_mode;
  2628. break;
  2629. }
  2630. }
  2631. }
  2632. static struct drm_display_mode *amdgpu_dm_create_common_mode(
  2633. struct drm_encoder *encoder, char *name,
  2634. int hdisplay, int vdisplay)
  2635. {
  2636. struct drm_device *dev = encoder->dev;
  2637. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2638. struct drm_display_mode *mode = NULL;
  2639. struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
  2640. mode = drm_mode_duplicate(dev, native_mode);
  2641. if (mode == NULL)
  2642. return NULL;
  2643. mode->hdisplay = hdisplay;
  2644. mode->vdisplay = vdisplay;
  2645. mode->type &= ~DRM_MODE_TYPE_PREFERRED;
  2646. strncpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
  2647. return mode;
  2648. }
  2649. static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
  2650. struct drm_connector *connector)
  2651. {
  2652. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2653. struct drm_display_mode *mode = NULL;
  2654. struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
  2655. struct amdgpu_dm_connector *amdgpu_dm_connector =
  2656. to_amdgpu_dm_connector(connector);
  2657. int i;
  2658. int n;
  2659. struct mode_size {
  2660. char name[DRM_DISPLAY_MODE_LEN];
  2661. int w;
  2662. int h;
  2663. } common_modes[] = {
  2664. { "640x480", 640, 480},
  2665. { "800x600", 800, 600},
  2666. { "1024x768", 1024, 768},
  2667. { "1280x720", 1280, 720},
  2668. { "1280x800", 1280, 800},
  2669. {"1280x1024", 1280, 1024},
  2670. { "1440x900", 1440, 900},
  2671. {"1680x1050", 1680, 1050},
  2672. {"1600x1200", 1600, 1200},
  2673. {"1920x1080", 1920, 1080},
  2674. {"1920x1200", 1920, 1200}
  2675. };
  2676. n = ARRAY_SIZE(common_modes);
  2677. for (i = 0; i < n; i++) {
  2678. struct drm_display_mode *curmode = NULL;
  2679. bool mode_existed = false;
  2680. if (common_modes[i].w > native_mode->hdisplay ||
  2681. common_modes[i].h > native_mode->vdisplay ||
  2682. (common_modes[i].w == native_mode->hdisplay &&
  2683. common_modes[i].h == native_mode->vdisplay))
  2684. continue;
  2685. list_for_each_entry(curmode, &connector->probed_modes, head) {
  2686. if (common_modes[i].w == curmode->hdisplay &&
  2687. common_modes[i].h == curmode->vdisplay) {
  2688. mode_existed = true;
  2689. break;
  2690. }
  2691. }
  2692. if (mode_existed)
  2693. continue;
  2694. mode = amdgpu_dm_create_common_mode(encoder,
  2695. common_modes[i].name, common_modes[i].w,
  2696. common_modes[i].h);
  2697. drm_mode_probed_add(connector, mode);
  2698. amdgpu_dm_connector->num_modes++;
  2699. }
  2700. }
  2701. static void amdgpu_dm_connector_ddc_get_modes(
  2702. struct drm_connector *connector,
  2703. struct edid *edid)
  2704. {
  2705. struct amdgpu_dm_connector *amdgpu_dm_connector =
  2706. to_amdgpu_dm_connector(connector);
  2707. if (edid) {
  2708. /* empty probed_modes */
  2709. INIT_LIST_HEAD(&connector->probed_modes);
  2710. amdgpu_dm_connector->num_modes =
  2711. drm_add_edid_modes(connector, edid);
  2712. drm_edid_to_eld(connector, edid);
  2713. amdgpu_dm_get_native_mode(connector);
  2714. } else
  2715. amdgpu_dm_connector->num_modes = 0;
  2716. }
  2717. int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
  2718. {
  2719. const struct drm_connector_helper_funcs *helper =
  2720. connector->helper_private;
  2721. struct amdgpu_dm_connector *amdgpu_dm_connector =
  2722. to_amdgpu_dm_connector(connector);
  2723. struct drm_encoder *encoder;
  2724. struct edid *edid = amdgpu_dm_connector->edid;
  2725. encoder = helper->best_encoder(connector);
  2726. amdgpu_dm_connector_ddc_get_modes(connector, edid);
  2727. amdgpu_dm_connector_add_common_modes(encoder, connector);
  2728. return amdgpu_dm_connector->num_modes;
  2729. }
  2730. void amdgpu_dm_connector_init_helper(
  2731. struct amdgpu_display_manager *dm,
  2732. struct amdgpu_dm_connector *aconnector,
  2733. int connector_type,
  2734. struct dc_link *link,
  2735. int link_index)
  2736. {
  2737. struct amdgpu_device *adev = dm->ddev->dev_private;
  2738. aconnector->connector_id = link_index;
  2739. aconnector->dc_link = link;
  2740. aconnector->base.interlace_allowed = false;
  2741. aconnector->base.doublescan_allowed = false;
  2742. aconnector->base.stereo_allowed = false;
  2743. aconnector->base.dpms = DRM_MODE_DPMS_OFF;
  2744. aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
  2745. mutex_init(&aconnector->hpd_lock);
  2746. /* configure support HPD hot plug connector_>polled default value is 0
  2747. * which means HPD hot plug not supported
  2748. */
  2749. switch (connector_type) {
  2750. case DRM_MODE_CONNECTOR_HDMIA:
  2751. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  2752. break;
  2753. case DRM_MODE_CONNECTOR_DisplayPort:
  2754. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  2755. break;
  2756. case DRM_MODE_CONNECTOR_DVID:
  2757. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  2758. break;
  2759. default:
  2760. break;
  2761. }
  2762. drm_object_attach_property(&aconnector->base.base,
  2763. dm->ddev->mode_config.scaling_mode_property,
  2764. DRM_MODE_SCALE_NONE);
  2765. drm_object_attach_property(&aconnector->base.base,
  2766. adev->mode_info.underscan_property,
  2767. UNDERSCAN_OFF);
  2768. drm_object_attach_property(&aconnector->base.base,
  2769. adev->mode_info.underscan_hborder_property,
  2770. 0);
  2771. drm_object_attach_property(&aconnector->base.base,
  2772. adev->mode_info.underscan_vborder_property,
  2773. 0);
  2774. }
  2775. int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
  2776. struct i2c_msg *msgs, int num)
  2777. {
  2778. struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
  2779. struct ddc_service *ddc_service = i2c->ddc_service;
  2780. struct i2c_command cmd;
  2781. int i;
  2782. int result = -EIO;
  2783. cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
  2784. if (!cmd.payloads)
  2785. return result;
  2786. cmd.number_of_payloads = num;
  2787. cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
  2788. cmd.speed = 100;
  2789. for (i = 0; i < num; i++) {
  2790. cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
  2791. cmd.payloads[i].address = msgs[i].addr;
  2792. cmd.payloads[i].length = msgs[i].len;
  2793. cmd.payloads[i].data = msgs[i].buf;
  2794. }
  2795. if (dal_i2caux_submit_i2c_command(
  2796. ddc_service->ctx->i2caux,
  2797. ddc_service->ddc_pin,
  2798. &cmd))
  2799. result = num;
  2800. kfree(cmd.payloads);
  2801. return result;
  2802. }
  2803. u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
  2804. {
  2805. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  2806. }
  2807. static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
  2808. .master_xfer = amdgpu_dm_i2c_xfer,
  2809. .functionality = amdgpu_dm_i2c_func,
  2810. };
  2811. static struct amdgpu_i2c_adapter *create_i2c(
  2812. struct ddc_service *ddc_service,
  2813. int link_index,
  2814. int *res)
  2815. {
  2816. struct amdgpu_device *adev = ddc_service->ctx->driver_context;
  2817. struct amdgpu_i2c_adapter *i2c;
  2818. i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
  2819. i2c->base.owner = THIS_MODULE;
  2820. i2c->base.class = I2C_CLASS_DDC;
  2821. i2c->base.dev.parent = &adev->pdev->dev;
  2822. i2c->base.algo = &amdgpu_dm_i2c_algo;
  2823. snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
  2824. i2c_set_adapdata(&i2c->base, i2c);
  2825. i2c->ddc_service = ddc_service;
  2826. return i2c;
  2827. }
  2828. /* Note: this function assumes that dc_link_detect() was called for the
  2829. * dc_link which will be represented by this aconnector.
  2830. */
  2831. int amdgpu_dm_connector_init(
  2832. struct amdgpu_display_manager *dm,
  2833. struct amdgpu_dm_connector *aconnector,
  2834. uint32_t link_index,
  2835. struct amdgpu_encoder *aencoder)
  2836. {
  2837. int res = 0;
  2838. int connector_type;
  2839. struct dc *dc = dm->dc;
  2840. struct dc_link *link = dc_get_link_at_index(dc, link_index);
  2841. struct amdgpu_i2c_adapter *i2c;
  2842. ((struct dc_link *)link)->priv = aconnector;
  2843. DRM_DEBUG_KMS("%s()\n", __func__);
  2844. i2c = create_i2c(link->ddc, link->link_index, &res);
  2845. aconnector->i2c = i2c;
  2846. res = i2c_add_adapter(&i2c->base);
  2847. if (res) {
  2848. DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
  2849. goto out_free;
  2850. }
  2851. connector_type = to_drm_connector_type(link->connector_signal);
  2852. res = drm_connector_init(
  2853. dm->ddev,
  2854. &aconnector->base,
  2855. &amdgpu_dm_connector_funcs,
  2856. connector_type);
  2857. if (res) {
  2858. DRM_ERROR("connector_init failed\n");
  2859. aconnector->connector_id = -1;
  2860. goto out_free;
  2861. }
  2862. drm_connector_helper_add(
  2863. &aconnector->base,
  2864. &amdgpu_dm_connector_helper_funcs);
  2865. amdgpu_dm_connector_init_helper(
  2866. dm,
  2867. aconnector,
  2868. connector_type,
  2869. link,
  2870. link_index);
  2871. drm_mode_connector_attach_encoder(
  2872. &aconnector->base, &aencoder->base);
  2873. drm_connector_register(&aconnector->base);
  2874. if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
  2875. || connector_type == DRM_MODE_CONNECTOR_eDP)
  2876. amdgpu_dm_initialize_dp_connector(dm, aconnector);
  2877. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
  2878. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  2879. /* NOTE: this currently will create backlight device even if a panel
  2880. * is not connected to the eDP/LVDS connector.
  2881. *
  2882. * This is less than ideal but we don't have sink information at this
  2883. * stage since detection happens after. We can't do detection earlier
  2884. * since MST detection needs connectors to be created first.
  2885. */
  2886. if (link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) {
  2887. /* Event if registration failed, we should continue with
  2888. * DM initialization because not having a backlight control
  2889. * is better then a black screen.
  2890. */
  2891. amdgpu_dm_register_backlight_device(dm);
  2892. if (dm->backlight_dev)
  2893. dm->backlight_link = link;
  2894. }
  2895. #endif
  2896. out_free:
  2897. if (res) {
  2898. kfree(i2c);
  2899. aconnector->i2c = NULL;
  2900. }
  2901. return res;
  2902. }
  2903. int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
  2904. {
  2905. switch (adev->mode_info.num_crtc) {
  2906. case 1:
  2907. return 0x1;
  2908. case 2:
  2909. return 0x3;
  2910. case 3:
  2911. return 0x7;
  2912. case 4:
  2913. return 0xf;
  2914. case 5:
  2915. return 0x1f;
  2916. case 6:
  2917. default:
  2918. return 0x3f;
  2919. }
  2920. }
  2921. int amdgpu_dm_encoder_init(
  2922. struct drm_device *dev,
  2923. struct amdgpu_encoder *aencoder,
  2924. uint32_t link_index)
  2925. {
  2926. struct amdgpu_device *adev = dev->dev_private;
  2927. int res = drm_encoder_init(dev,
  2928. &aencoder->base,
  2929. &amdgpu_dm_encoder_funcs,
  2930. DRM_MODE_ENCODER_TMDS,
  2931. NULL);
  2932. aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
  2933. if (!res)
  2934. aencoder->encoder_id = link_index;
  2935. else
  2936. aencoder->encoder_id = -1;
  2937. drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
  2938. return res;
  2939. }
  2940. static void manage_dm_interrupts(
  2941. struct amdgpu_device *adev,
  2942. struct amdgpu_crtc *acrtc,
  2943. bool enable)
  2944. {
  2945. /*
  2946. * this is not correct translation but will work as soon as VBLANK
  2947. * constant is the same as PFLIP
  2948. */
  2949. int irq_type =
  2950. amdgpu_crtc_idx_to_irq_type(
  2951. adev,
  2952. acrtc->crtc_id);
  2953. if (enable) {
  2954. drm_crtc_vblank_on(&acrtc->base);
  2955. amdgpu_irq_get(
  2956. adev,
  2957. &adev->pageflip_irq,
  2958. irq_type);
  2959. } else {
  2960. amdgpu_irq_put(
  2961. adev,
  2962. &adev->pageflip_irq,
  2963. irq_type);
  2964. drm_crtc_vblank_off(&acrtc->base);
  2965. }
  2966. }
  2967. static bool is_scaling_state_different(
  2968. const struct dm_connector_state *dm_state,
  2969. const struct dm_connector_state *old_dm_state)
  2970. {
  2971. if (dm_state->scaling != old_dm_state->scaling)
  2972. return true;
  2973. if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
  2974. if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
  2975. return true;
  2976. } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
  2977. if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
  2978. return true;
  2979. } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
  2980. dm_state->underscan_vborder != old_dm_state->underscan_vborder)
  2981. return true;
  2982. return false;
  2983. }
  2984. static void remove_stream(
  2985. struct amdgpu_device *adev,
  2986. struct amdgpu_crtc *acrtc,
  2987. struct dc_stream_state *stream)
  2988. {
  2989. /* this is the update mode case */
  2990. if (adev->dm.freesync_module)
  2991. mod_freesync_remove_stream(adev->dm.freesync_module, stream);
  2992. acrtc->otg_inst = -1;
  2993. acrtc->enabled = false;
  2994. }
  2995. int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
  2996. struct dc_cursor_position *position)
  2997. {
  2998. struct amdgpu_crtc *amdgpu_crtc = amdgpu_crtc = to_amdgpu_crtc(crtc);
  2999. int x, y;
  3000. int xorigin = 0, yorigin = 0;
  3001. if (!crtc || !plane->state->fb) {
  3002. position->enable = false;
  3003. position->x = 0;
  3004. position->y = 0;
  3005. return 0;
  3006. }
  3007. if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
  3008. (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
  3009. DRM_ERROR("%s: bad cursor width or height %d x %d\n",
  3010. __func__,
  3011. plane->state->crtc_w,
  3012. plane->state->crtc_h);
  3013. return -EINVAL;
  3014. }
  3015. x = plane->state->crtc_x;
  3016. y = plane->state->crtc_y;
  3017. /* avivo cursor are offset into the total surface */
  3018. x += crtc->primary->state->src_x >> 16;
  3019. y += crtc->primary->state->src_y >> 16;
  3020. if (x < 0) {
  3021. xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
  3022. x = 0;
  3023. }
  3024. if (y < 0) {
  3025. yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
  3026. y = 0;
  3027. }
  3028. position->enable = true;
  3029. position->x = x;
  3030. position->y = y;
  3031. position->x_hotspot = xorigin;
  3032. position->y_hotspot = yorigin;
  3033. return 0;
  3034. }
  3035. static void handle_cursor_update(
  3036. struct drm_plane *plane,
  3037. struct drm_plane_state *old_plane_state)
  3038. {
  3039. struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
  3040. struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
  3041. struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
  3042. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  3043. uint64_t address = afb ? afb->address : 0;
  3044. struct dc_cursor_position position;
  3045. struct dc_cursor_attributes attributes;
  3046. int ret;
  3047. if (!plane->state->fb && !old_plane_state->fb)
  3048. return;
  3049. DRM_DEBUG_KMS("%s: crtc_id=%d with size %d to %d\n",
  3050. __func__,
  3051. amdgpu_crtc->crtc_id,
  3052. plane->state->crtc_w,
  3053. plane->state->crtc_h);
  3054. ret = get_cursor_position(plane, crtc, &position);
  3055. if (ret)
  3056. return;
  3057. if (!position.enable) {
  3058. /* turn off cursor */
  3059. if (crtc_state && crtc_state->stream)
  3060. dc_stream_set_cursor_position(crtc_state->stream,
  3061. &position);
  3062. return;
  3063. }
  3064. amdgpu_crtc->cursor_width = plane->state->crtc_w;
  3065. amdgpu_crtc->cursor_height = plane->state->crtc_h;
  3066. attributes.address.high_part = upper_32_bits(address);
  3067. attributes.address.low_part = lower_32_bits(address);
  3068. attributes.width = plane->state->crtc_w;
  3069. attributes.height = plane->state->crtc_h;
  3070. attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
  3071. attributes.rotation_angle = 0;
  3072. attributes.attribute_flags.value = 0;
  3073. attributes.pitch = attributes.width;
  3074. if (!dc_stream_set_cursor_attributes(crtc_state->stream,
  3075. &attributes))
  3076. DRM_ERROR("DC failed to set cursor attributes\n");
  3077. if (crtc_state->stream)
  3078. if (!dc_stream_set_cursor_position(crtc_state->stream,
  3079. &position))
  3080. DRM_ERROR("DC failed to set cursor position\n");
  3081. }
  3082. static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
  3083. {
  3084. assert_spin_locked(&acrtc->base.dev->event_lock);
  3085. WARN_ON(acrtc->event);
  3086. acrtc->event = acrtc->base.state->event;
  3087. /* Set the flip status */
  3088. acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
  3089. /* Mark this event as consumed */
  3090. acrtc->base.state->event = NULL;
  3091. DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
  3092. acrtc->crtc_id);
  3093. }
  3094. /*
  3095. * Executes flip
  3096. *
  3097. * Waits on all BO's fences and for proper vblank count
  3098. */
  3099. static void amdgpu_dm_do_flip(
  3100. struct drm_crtc *crtc,
  3101. struct drm_framebuffer *fb,
  3102. uint32_t target)
  3103. {
  3104. unsigned long flags;
  3105. uint32_t target_vblank;
  3106. int r, vpos, hpos;
  3107. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3108. struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
  3109. struct amdgpu_bo *abo = gem_to_amdgpu_bo(afb->obj);
  3110. struct amdgpu_device *adev = crtc->dev->dev_private;
  3111. bool async_flip = (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
  3112. struct dc_flip_addrs addr = { {0} };
  3113. /* TODO eliminate or rename surface_update */
  3114. struct dc_surface_update surface_updates[1] = { {0} };
  3115. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
  3116. /* Prepare wait for target vblank early - before the fence-waits */
  3117. target_vblank = target - drm_crtc_vblank_count(crtc) +
  3118. amdgpu_get_vblank_counter_kms(crtc->dev, acrtc->crtc_id);
  3119. /* TODO This might fail and hence better not used, wait
  3120. * explicitly on fences instead
  3121. * and in general should be called for
  3122. * blocking commit to as per framework helpers
  3123. */
  3124. r = amdgpu_bo_reserve(abo, true);
  3125. if (unlikely(r != 0)) {
  3126. DRM_ERROR("failed to reserve buffer before flip\n");
  3127. WARN_ON(1);
  3128. }
  3129. /* Wait for all fences on this FB */
  3130. WARN_ON(reservation_object_wait_timeout_rcu(abo->tbo.resv, true, false,
  3131. MAX_SCHEDULE_TIMEOUT) < 0);
  3132. amdgpu_bo_unreserve(abo);
  3133. /* Wait until we're out of the vertical blank period before the one
  3134. * targeted by the flip
  3135. */
  3136. while ((acrtc->enabled &&
  3137. (amdgpu_get_crtc_scanoutpos(adev->ddev, acrtc->crtc_id, 0,
  3138. &vpos, &hpos, NULL, NULL,
  3139. &crtc->hwmode)
  3140. & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
  3141. (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
  3142. (int)(target_vblank -
  3143. amdgpu_get_vblank_counter_kms(adev->ddev, acrtc->crtc_id)) > 0)) {
  3144. usleep_range(1000, 1100);
  3145. }
  3146. /* Flip */
  3147. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  3148. /* update crtc fb */
  3149. crtc->primary->fb = fb;
  3150. WARN_ON(acrtc->pflip_status != AMDGPU_FLIP_NONE);
  3151. WARN_ON(!acrtc_state->stream);
  3152. addr.address.grph.addr.low_part = lower_32_bits(afb->address);
  3153. addr.address.grph.addr.high_part = upper_32_bits(afb->address);
  3154. addr.flip_immediate = async_flip;
  3155. if (acrtc->base.state->event)
  3156. prepare_flip_isr(acrtc);
  3157. surface_updates->surface = dc_stream_get_status(acrtc_state->stream)->plane_states[0];
  3158. surface_updates->flip_addr = &addr;
  3159. dc_update_planes_and_stream(adev->dm.dc, surface_updates, 1, acrtc_state->stream, NULL);
  3160. DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x \n",
  3161. __func__,
  3162. addr.address.grph.addr.high_part,
  3163. addr.address.grph.addr.low_part);
  3164. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  3165. }
  3166. static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
  3167. struct drm_device *dev,
  3168. struct amdgpu_display_manager *dm,
  3169. struct drm_crtc *pcrtc,
  3170. bool *wait_for_vblank)
  3171. {
  3172. uint32_t i;
  3173. struct drm_plane *plane;
  3174. struct drm_plane_state *old_plane_state;
  3175. struct dc_stream_state *dc_stream_attach;
  3176. struct dc_plane_state *plane_states_constructed[MAX_SURFACES];
  3177. struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
  3178. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(pcrtc->state);
  3179. int planes_count = 0;
  3180. unsigned long flags;
  3181. /* update planes when needed */
  3182. for_each_plane_in_state(state, plane, old_plane_state, i) {
  3183. struct drm_plane_state *plane_state = plane->state;
  3184. struct drm_crtc *crtc = plane_state->crtc;
  3185. struct drm_framebuffer *fb = plane_state->fb;
  3186. bool pflip_needed;
  3187. struct dm_plane_state *dm_plane_state = to_dm_plane_state(plane_state);
  3188. if (plane->type == DRM_PLANE_TYPE_CURSOR) {
  3189. handle_cursor_update(plane, old_plane_state);
  3190. continue;
  3191. }
  3192. if (!fb || !crtc || pcrtc != crtc || !crtc->state->active)
  3193. continue;
  3194. pflip_needed = !state->allow_modeset;
  3195. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  3196. if (acrtc_attach->pflip_status != AMDGPU_FLIP_NONE) {
  3197. DRM_ERROR("%s: acrtc %d, already busy\n",
  3198. __func__,
  3199. acrtc_attach->crtc_id);
  3200. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  3201. /* In commit tail framework this cannot happen */
  3202. WARN_ON(1);
  3203. }
  3204. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  3205. if (!pflip_needed) {
  3206. WARN_ON(!dm_plane_state->dc_state);
  3207. plane_states_constructed[planes_count] = dm_plane_state->dc_state;
  3208. dc_stream_attach = acrtc_state->stream;
  3209. planes_count++;
  3210. } else if (crtc->state->planes_changed) {
  3211. /* Assume even ONE crtc with immediate flip means
  3212. * entire can't wait for VBLANK
  3213. * TODO Check if it's correct
  3214. */
  3215. *wait_for_vblank =
  3216. pcrtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC ?
  3217. false : true;
  3218. /* TODO: Needs rework for multiplane flip */
  3219. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  3220. drm_crtc_vblank_get(crtc);
  3221. amdgpu_dm_do_flip(
  3222. crtc,
  3223. fb,
  3224. drm_crtc_vblank_count(crtc) + *wait_for_vblank);
  3225. }
  3226. }
  3227. if (planes_count) {
  3228. unsigned long flags;
  3229. if (pcrtc->state->event) {
  3230. drm_crtc_vblank_get(pcrtc);
  3231. spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
  3232. prepare_flip_isr(acrtc_attach);
  3233. spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
  3234. }
  3235. if (false == dc_commit_planes_to_stream(dm->dc,
  3236. plane_states_constructed,
  3237. planes_count,
  3238. dc_stream_attach))
  3239. dm_error("%s: Failed to attach plane!\n", __func__);
  3240. } else {
  3241. /*TODO BUG Here should go disable planes on CRTC. */
  3242. }
  3243. }
  3244. int amdgpu_dm_atomic_commit(
  3245. struct drm_device *dev,
  3246. struct drm_atomic_state *state,
  3247. bool nonblock)
  3248. {
  3249. struct drm_crtc *crtc;
  3250. struct drm_crtc_state *new_state;
  3251. struct amdgpu_device *adev = dev->dev_private;
  3252. int i;
  3253. /*
  3254. * We evade vblanks and pflips on crtc that
  3255. * should be changed. We do it here to flush & disable
  3256. * interrupts before drm_swap_state is called in drm_atomic_helper_commit
  3257. * it will update crtc->dm_crtc_state->stream pointer which is used in
  3258. * the ISRs.
  3259. */
  3260. for_each_crtc_in_state(state, crtc, new_state, i) {
  3261. struct dm_crtc_state *old_acrtc_state = to_dm_crtc_state(crtc->state);
  3262. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3263. if (drm_atomic_crtc_needs_modeset(new_state) && old_acrtc_state->stream)
  3264. manage_dm_interrupts(adev, acrtc, false);
  3265. }
  3266. return drm_atomic_helper_commit(dev, state, nonblock);
  3267. /*TODO Handle EINTR, reenable IRQ*/
  3268. }
  3269. void amdgpu_dm_atomic_commit_tail(
  3270. struct drm_atomic_state *state)
  3271. {
  3272. struct drm_device *dev = state->dev;
  3273. struct amdgpu_device *adev = dev->dev_private;
  3274. struct amdgpu_display_manager *dm = &adev->dm;
  3275. struct dm_atomic_state *dm_state;
  3276. uint32_t i, j;
  3277. uint32_t new_crtcs_count = 0;
  3278. struct drm_crtc *crtc, *pcrtc;
  3279. struct drm_crtc_state *old_crtc_state;
  3280. struct amdgpu_crtc *new_crtcs[MAX_STREAMS];
  3281. struct dc_stream_state *new_stream = NULL;
  3282. unsigned long flags;
  3283. bool wait_for_vblank = true;
  3284. struct drm_connector *connector;
  3285. struct drm_connector_state *old_conn_state;
  3286. struct dm_crtc_state *old_acrtc_state, *new_acrtc_state;
  3287. drm_atomic_helper_update_legacy_modeset_state(dev, state);
  3288. dm_state = to_dm_atomic_state(state);
  3289. /* update changed items */
  3290. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  3291. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3292. struct drm_crtc_state *new_state = crtc->state;
  3293. new_acrtc_state = to_dm_crtc_state(new_state);
  3294. old_acrtc_state = to_dm_crtc_state(old_crtc_state);
  3295. DRM_DEBUG_KMS(
  3296. "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
  3297. "planes_changed:%d, mode_changed:%d,active_changed:%d,"
  3298. "connectors_changed:%d\n",
  3299. acrtc->crtc_id,
  3300. new_state->enable,
  3301. new_state->active,
  3302. new_state->planes_changed,
  3303. new_state->mode_changed,
  3304. new_state->active_changed,
  3305. new_state->connectors_changed);
  3306. /* handles headless hotplug case, updating new_state and
  3307. * aconnector as needed
  3308. */
  3309. if (modeset_required(new_state, new_acrtc_state->stream, old_acrtc_state->stream)) {
  3310. DRM_INFO("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
  3311. if (!new_acrtc_state->stream) {
  3312. /*
  3313. * this could happen because of issues with
  3314. * userspace notifications delivery.
  3315. * In this case userspace tries to set mode on
  3316. * display which is disconnect in fact.
  3317. * dc_sink in NULL in this case on aconnector.
  3318. * We expect reset mode will come soon.
  3319. *
  3320. * This can also happen when unplug is done
  3321. * during resume sequence ended
  3322. *
  3323. * In this case, we want to pretend we still
  3324. * have a sink to keep the pipe running so that
  3325. * hw state is consistent with the sw state
  3326. */
  3327. DRM_DEBUG_KMS("%s: Failed to create new stream for crtc %d\n",
  3328. __func__, acrtc->base.base.id);
  3329. continue;
  3330. }
  3331. if (old_acrtc_state->stream)
  3332. remove_stream(adev, acrtc, old_acrtc_state->stream);
  3333. /*
  3334. * this loop saves set mode crtcs
  3335. * we needed to enable vblanks once all
  3336. * resources acquired in dc after dc_commit_streams
  3337. */
  3338. /*TODO move all this into dm_crtc_state, get rid of
  3339. * new_crtcs array and use old and new atomic states
  3340. * instead
  3341. */
  3342. new_crtcs[new_crtcs_count] = acrtc;
  3343. new_crtcs_count++;
  3344. acrtc->enabled = true;
  3345. acrtc->hw_mode = crtc->state->mode;
  3346. crtc->hwmode = crtc->state->mode;
  3347. } else if (modereset_required(new_state)) {
  3348. DRM_INFO("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
  3349. /* i.e. reset mode */
  3350. if (old_acrtc_state->stream)
  3351. remove_stream(adev, acrtc, old_acrtc_state->stream);
  3352. }
  3353. } /* for_each_crtc_in_state() */
  3354. /*
  3355. * Add streams after required streams from new and replaced streams
  3356. * are removed from freesync module
  3357. */
  3358. if (adev->dm.freesync_module) {
  3359. for (i = 0; i < new_crtcs_count; i++) {
  3360. struct amdgpu_dm_connector *aconnector = NULL;
  3361. new_acrtc_state = to_dm_crtc_state(new_crtcs[i]->base.state);
  3362. new_stream = new_acrtc_state->stream;
  3363. aconnector =
  3364. amdgpu_dm_find_first_crct_matching_connector(
  3365. state,
  3366. &new_crtcs[i]->base,
  3367. false);
  3368. if (!aconnector) {
  3369. DRM_INFO("Atomic commit: Failed to find connector for acrtc id:%d "
  3370. "skipping freesync init\n",
  3371. new_crtcs[i]->crtc_id);
  3372. continue;
  3373. }
  3374. mod_freesync_add_stream(adev->dm.freesync_module,
  3375. new_stream, &aconnector->caps);
  3376. }
  3377. }
  3378. if (dm_state->context)
  3379. WARN_ON(!dc_commit_state(dm->dc, dm_state->context));
  3380. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3381. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3382. new_acrtc_state = to_dm_crtc_state(crtc->state);
  3383. if (new_acrtc_state->stream != NULL) {
  3384. const struct dc_stream_status *status =
  3385. dc_stream_get_status(new_acrtc_state->stream);
  3386. if (!status)
  3387. DC_ERR("got no status for stream %p on acrtc%p\n", new_acrtc_state->stream, acrtc);
  3388. else
  3389. acrtc->otg_inst = status->primary_otg_inst;
  3390. }
  3391. }
  3392. /* Handle scaling and undersacn changes*/
  3393. for_each_connector_in_state(state, connector, old_conn_state, i) {
  3394. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  3395. struct dm_connector_state *con_new_state =
  3396. to_dm_connector_state(aconnector->base.state);
  3397. struct dm_connector_state *con_old_state =
  3398. to_dm_connector_state(old_conn_state);
  3399. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(con_new_state->base.crtc);
  3400. struct dc_stream_status *status = NULL;
  3401. /* Skip any modesets/resets */
  3402. if (!acrtc || drm_atomic_crtc_needs_modeset(acrtc->base.state))
  3403. continue;
  3404. /* Skip any thing not scale or underscan changes */
  3405. if (!is_scaling_state_different(con_new_state, con_old_state))
  3406. continue;
  3407. new_acrtc_state = to_dm_crtc_state(acrtc->base.state);
  3408. update_stream_scaling_settings(&con_new_state->base.crtc->mode,
  3409. con_new_state, (struct dc_stream_state *)new_acrtc_state->stream);
  3410. status = dc_stream_get_status(new_acrtc_state->stream);
  3411. WARN_ON(!status);
  3412. WARN_ON(!status->plane_count);
  3413. if (!new_acrtc_state->stream)
  3414. continue;
  3415. /*TODO How it works with MPO ?*/
  3416. if (!dc_commit_planes_to_stream(
  3417. dm->dc,
  3418. status->plane_states,
  3419. status->plane_count,
  3420. new_acrtc_state->stream))
  3421. dm_error("%s: Failed to update stream scaling!\n", __func__);
  3422. }
  3423. for (i = 0; i < new_crtcs_count; i++) {
  3424. /*
  3425. * loop to enable interrupts on newly arrived crtc
  3426. */
  3427. struct amdgpu_crtc *acrtc = new_crtcs[i];
  3428. new_acrtc_state = to_dm_crtc_state(acrtc->base.state);
  3429. if (adev->dm.freesync_module)
  3430. mod_freesync_notify_mode_change(
  3431. adev->dm.freesync_module, &new_acrtc_state->stream, 1);
  3432. manage_dm_interrupts(adev, acrtc, true);
  3433. }
  3434. /* update planes when needed per crtc*/
  3435. for_each_crtc_in_state(state, pcrtc, old_crtc_state, j) {
  3436. new_acrtc_state = to_dm_crtc_state(pcrtc->state);
  3437. if (new_acrtc_state->stream)
  3438. amdgpu_dm_commit_planes(state, dev, dm, pcrtc, &wait_for_vblank);
  3439. }
  3440. /*
  3441. * send vblank event on all events not handled in flip and
  3442. * mark consumed event for drm_atomic_helper_commit_hw_done
  3443. */
  3444. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  3445. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  3446. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3447. if (acrtc->base.state->event)
  3448. drm_send_event_locked(dev, &crtc->state->event->base);
  3449. acrtc->base.state->event = NULL;
  3450. }
  3451. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  3452. /* Signal HW programming completion */
  3453. drm_atomic_helper_commit_hw_done(state);
  3454. if (wait_for_vblank)
  3455. drm_atomic_helper_wait_for_vblanks(dev, state);
  3456. drm_atomic_helper_cleanup_planes(dev, state);
  3457. }
  3458. static int dm_force_atomic_commit(struct drm_connector *connector)
  3459. {
  3460. int ret = 0;
  3461. struct drm_device *ddev = connector->dev;
  3462. struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
  3463. struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
  3464. struct drm_plane *plane = disconnected_acrtc->base.primary;
  3465. struct drm_connector_state *conn_state;
  3466. struct drm_crtc_state *crtc_state;
  3467. struct drm_plane_state *plane_state;
  3468. if (!state)
  3469. return -ENOMEM;
  3470. state->acquire_ctx = ddev->mode_config.acquire_ctx;
  3471. /* Construct an atomic state to restore previous display setting */
  3472. /*
  3473. * Attach connectors to drm_atomic_state
  3474. */
  3475. conn_state = drm_atomic_get_connector_state(state, connector);
  3476. ret = PTR_ERR_OR_ZERO(conn_state);
  3477. if (ret)
  3478. goto err;
  3479. /* Attach crtc to drm_atomic_state*/
  3480. crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
  3481. ret = PTR_ERR_OR_ZERO(crtc_state);
  3482. if (ret)
  3483. goto err;
  3484. /* force a restore */
  3485. crtc_state->mode_changed = true;
  3486. /* Attach plane to drm_atomic_state */
  3487. plane_state = drm_atomic_get_plane_state(state, plane);
  3488. ret = PTR_ERR_OR_ZERO(plane_state);
  3489. if (ret)
  3490. goto err;
  3491. /* Call commit internally with the state we just constructed */
  3492. ret = drm_atomic_commit(state);
  3493. if (!ret)
  3494. return 0;
  3495. err:
  3496. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3497. drm_atomic_state_put(state);
  3498. return ret;
  3499. }
  3500. /*
  3501. * This functions handle all cases when set mode does not come upon hotplug.
  3502. * This include when the same display is unplugged then plugged back into the
  3503. * same port and when we are running without usermode desktop manager supprot
  3504. */
  3505. void dm_restore_drm_connector_state(struct drm_device *dev, struct drm_connector *connector)
  3506. {
  3507. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  3508. struct amdgpu_crtc *disconnected_acrtc;
  3509. struct dm_crtc_state *acrtc_state;
  3510. if (!aconnector->dc_sink || !connector->state || !connector->encoder)
  3511. return;
  3512. disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
  3513. acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
  3514. if (!disconnected_acrtc || !acrtc_state->stream)
  3515. return;
  3516. /*
  3517. * If the previous sink is not released and different from the current,
  3518. * we deduce we are in a state where we can not rely on usermode call
  3519. * to turn on the display, so we do it here
  3520. */
  3521. if (acrtc_state->stream->sink != aconnector->dc_sink)
  3522. dm_force_atomic_commit(&aconnector->base);
  3523. }
  3524. /*`
  3525. * Grabs all modesetting locks to serialize against any blocking commits,
  3526. * Waits for completion of all non blocking commits.
  3527. */
  3528. static int do_aquire_global_lock(
  3529. struct drm_device *dev,
  3530. struct drm_atomic_state *state)
  3531. {
  3532. struct drm_crtc *crtc;
  3533. struct drm_crtc_commit *commit;
  3534. long ret;
  3535. /* Adding all modeset locks to aquire_ctx will
  3536. * ensure that when the framework release it the
  3537. * extra locks we are locking here will get released to
  3538. */
  3539. ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
  3540. if (ret)
  3541. return ret;
  3542. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3543. spin_lock(&crtc->commit_lock);
  3544. commit = list_first_entry_or_null(&crtc->commit_list,
  3545. struct drm_crtc_commit, commit_entry);
  3546. if (commit)
  3547. drm_crtc_commit_get(commit);
  3548. spin_unlock(&crtc->commit_lock);
  3549. if (!commit)
  3550. continue;
  3551. /* Make sure all pending HW programming completed and
  3552. * page flips done
  3553. */
  3554. ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
  3555. if (ret > 0)
  3556. ret = wait_for_completion_interruptible_timeout(
  3557. &commit->flip_done, 10*HZ);
  3558. if (ret == 0)
  3559. DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
  3560. "timed out\n", crtc->base.id, crtc->name);
  3561. drm_crtc_commit_put(commit);
  3562. }
  3563. return ret < 0 ? ret : 0;
  3564. }
  3565. static int dm_update_crtcs_state(
  3566. struct dc *dc,
  3567. struct drm_atomic_state *state,
  3568. bool enable,
  3569. bool *lock_and_validation_needed)
  3570. {
  3571. struct drm_crtc *crtc;
  3572. struct drm_crtc_state *crtc_state;
  3573. int i;
  3574. struct dm_crtc_state *old_acrtc_state, *new_acrtc_state;
  3575. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  3576. struct dc_stream_state *new_stream;
  3577. int ret = 0;
  3578. /*TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set */
  3579. /* update changed items */
  3580. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3581. struct amdgpu_crtc *acrtc = NULL;
  3582. struct amdgpu_dm_connector *aconnector = NULL;
  3583. struct drm_connector_state *conn_state = NULL;
  3584. struct dm_connector_state *dm_conn_state = NULL;
  3585. new_stream = NULL;
  3586. old_acrtc_state = to_dm_crtc_state(crtc->state);
  3587. new_acrtc_state = to_dm_crtc_state(crtc_state);
  3588. acrtc = to_amdgpu_crtc(crtc);
  3589. aconnector = amdgpu_dm_find_first_crct_matching_connector(state, crtc, true);
  3590. /* TODO This hack should go away */
  3591. if (aconnector) {
  3592. conn_state = drm_atomic_get_connector_state(state,
  3593. &aconnector->base);
  3594. if (IS_ERR(conn_state)) {
  3595. ret = PTR_ERR_OR_ZERO(conn_state);
  3596. break;
  3597. }
  3598. dm_conn_state = to_dm_connector_state(conn_state);
  3599. new_stream = create_stream_for_sink(aconnector,
  3600. &crtc_state->mode,
  3601. dm_conn_state);
  3602. /*
  3603. * we can have no stream on ACTION_SET if a display
  3604. * was disconnected during S3, in this case it not and
  3605. * error, the OS will be updated after detection, and
  3606. * do the right thing on next atomic commit
  3607. */
  3608. if (!new_stream) {
  3609. DRM_DEBUG_KMS("%s: Failed to create new stream for crtc %d\n",
  3610. __func__, acrtc->base.base.id);
  3611. break;
  3612. }
  3613. }
  3614. if (dc_is_stream_unchanged(new_stream,
  3615. old_acrtc_state->stream)) {
  3616. crtc_state->mode_changed = false;
  3617. DRM_DEBUG_KMS("Mode change not required, setting mode_changed to %d",
  3618. crtc_state->mode_changed);
  3619. }
  3620. if (!drm_atomic_crtc_needs_modeset(crtc_state))
  3621. goto next_crtc;
  3622. DRM_DEBUG_KMS(
  3623. "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
  3624. "planes_changed:%d, mode_changed:%d,active_changed:%d,"
  3625. "connectors_changed:%d\n",
  3626. acrtc->crtc_id,
  3627. crtc_state->enable,
  3628. crtc_state->active,
  3629. crtc_state->planes_changed,
  3630. crtc_state->mode_changed,
  3631. crtc_state->active_changed,
  3632. crtc_state->connectors_changed);
  3633. /* Remove stream for any changed/disabled CRTC */
  3634. if (!enable) {
  3635. if (!old_acrtc_state->stream)
  3636. goto next_crtc;
  3637. DRM_DEBUG_KMS("Disabling DRM crtc: %d\n",
  3638. crtc->base.id);
  3639. /* i.e. reset mode */
  3640. if (!dc_remove_stream_from_ctx(
  3641. dc,
  3642. dm_state->context,
  3643. old_acrtc_state->stream)) {
  3644. ret = -EINVAL;
  3645. goto fail;
  3646. }
  3647. dc_stream_release(old_acrtc_state->stream);
  3648. new_acrtc_state->stream = NULL;
  3649. *lock_and_validation_needed = true;
  3650. } else {/* Add stream for any updated/enabled CRTC */
  3651. if (modereset_required(crtc_state))
  3652. goto next_crtc;
  3653. if (modeset_required(crtc_state, new_stream,
  3654. old_acrtc_state->stream)) {
  3655. WARN_ON(new_acrtc_state->stream);
  3656. new_acrtc_state->stream = new_stream;
  3657. dc_stream_retain(new_stream);
  3658. DRM_DEBUG_KMS("Enabling DRM crtc: %d\n",
  3659. crtc->base.id);
  3660. if (!dc_add_stream_to_ctx(
  3661. dc,
  3662. dm_state->context,
  3663. new_acrtc_state->stream)) {
  3664. ret = -EINVAL;
  3665. goto fail;
  3666. }
  3667. *lock_and_validation_needed = true;
  3668. }
  3669. }
  3670. next_crtc:
  3671. /* Release extra reference */
  3672. if (new_stream)
  3673. dc_stream_release(new_stream);
  3674. }
  3675. return ret;
  3676. fail:
  3677. if (new_stream)
  3678. dc_stream_release(new_stream);
  3679. return ret;
  3680. }
  3681. static int dm_update_planes_state(
  3682. struct dc *dc,
  3683. struct drm_atomic_state *state,
  3684. bool enable,
  3685. bool *lock_and_validation_needed)
  3686. {
  3687. struct drm_crtc *new_plane_crtc, *old_plane_crtc;
  3688. struct drm_crtc_state *new_crtc_state;
  3689. struct drm_plane *plane;
  3690. struct drm_plane_state *old_plane_state, *new_plane_state;
  3691. struct dm_crtc_state *new_acrtc_state, *old_acrtc_state;
  3692. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  3693. struct dm_plane_state *new_dm_plane_state, *old_dm_plane_state;
  3694. int i ;
  3695. /* TODO return page_flip_needed() function */
  3696. bool pflip_needed = !state->allow_modeset;
  3697. int ret = 0;
  3698. if (pflip_needed)
  3699. return ret;
  3700. /* Add new planes */
  3701. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  3702. new_plane_crtc = new_plane_state->crtc;
  3703. old_plane_crtc = old_plane_state->crtc;
  3704. new_dm_plane_state = to_dm_plane_state(new_plane_state);
  3705. old_dm_plane_state = to_dm_plane_state(old_plane_state);
  3706. /*TODO Implement atomic check for cursor plane */
  3707. if (plane->type == DRM_PLANE_TYPE_CURSOR)
  3708. continue;
  3709. /* Remove any changed/removed planes */
  3710. if (!enable) {
  3711. if (!old_plane_crtc)
  3712. continue;
  3713. old_acrtc_state = to_dm_crtc_state(
  3714. drm_atomic_get_old_crtc_state(
  3715. state,
  3716. old_plane_crtc));
  3717. if (!old_acrtc_state->stream)
  3718. continue;
  3719. DRM_DEBUG_KMS("Disabling DRM plane: %d on DRM crtc %d\n",
  3720. plane->base.id, old_plane_crtc->base.id);
  3721. if (!dc_remove_plane_from_context(
  3722. dc,
  3723. old_acrtc_state->stream,
  3724. old_dm_plane_state->dc_state,
  3725. dm_state->context)) {
  3726. ret = EINVAL;
  3727. return ret;
  3728. }
  3729. dc_plane_state_release(old_dm_plane_state->dc_state);
  3730. new_dm_plane_state->dc_state = NULL;
  3731. *lock_and_validation_needed = true;
  3732. } else { /* Add new planes */
  3733. if (drm_atomic_plane_disabling(plane->state, new_plane_state))
  3734. continue;
  3735. if (!new_plane_crtc)
  3736. continue;
  3737. new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
  3738. new_acrtc_state = to_dm_crtc_state(new_crtc_state);
  3739. if (!new_acrtc_state->stream)
  3740. continue;
  3741. WARN_ON(new_dm_plane_state->dc_state);
  3742. new_dm_plane_state->dc_state = dc_create_plane_state(dc);
  3743. DRM_DEBUG_KMS("Enabling DRM plane: %d on DRM crtc %d\n",
  3744. plane->base.id, new_plane_crtc->base.id);
  3745. if (!new_dm_plane_state->dc_state) {
  3746. ret = -EINVAL;
  3747. return ret;
  3748. }
  3749. ret = fill_plane_attributes(
  3750. new_plane_crtc->dev->dev_private,
  3751. new_dm_plane_state->dc_state,
  3752. new_plane_state,
  3753. new_crtc_state,
  3754. false);
  3755. if (ret)
  3756. return ret;
  3757. if (!dc_add_plane_to_context(
  3758. dc,
  3759. new_acrtc_state->stream,
  3760. new_dm_plane_state->dc_state,
  3761. dm_state->context)) {
  3762. ret = -EINVAL;
  3763. return ret;
  3764. }
  3765. *lock_and_validation_needed = true;
  3766. }
  3767. }
  3768. return ret;
  3769. }
  3770. int amdgpu_dm_atomic_check(struct drm_device *dev,
  3771. struct drm_atomic_state *state)
  3772. {
  3773. int i;
  3774. int ret;
  3775. struct amdgpu_device *adev = dev->dev_private;
  3776. struct dc *dc = adev->dm.dc;
  3777. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  3778. struct drm_connector *connector;
  3779. struct drm_connector_state *conn_state;
  3780. struct drm_crtc *crtc;
  3781. struct drm_crtc_state *crtc_state;
  3782. /*
  3783. * This bool will be set for true for any modeset/reset
  3784. * or plane update which implies non fast surface update.
  3785. */
  3786. bool lock_and_validation_needed = false;
  3787. ret = drm_atomic_helper_check_modeset(dev, state);
  3788. if (ret) {
  3789. DRM_ERROR("Atomic state validation failed with error :%d !\n", ret);
  3790. return ret;
  3791. }
  3792. /*
  3793. * Hack: Commit needs planes right now, specifically for gamma
  3794. * TODO rework commit to check CRTC for gamma change
  3795. */
  3796. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3797. if (crtc_state->color_mgmt_changed) {
  3798. ret = drm_atomic_add_affected_planes(state, crtc);
  3799. if (ret)
  3800. goto fail;
  3801. }
  3802. }
  3803. dm_state->context = dc_create_state();
  3804. ASSERT(dm_state->context);
  3805. dc_resource_state_copy_construct_current(dc, dm_state->context);
  3806. /* Remove exiting planes if they are modified */
  3807. ret = dm_update_planes_state(dc, state, false, &lock_and_validation_needed);
  3808. if (ret) {
  3809. goto fail;
  3810. }
  3811. /* Disable all crtcs which require disable */
  3812. ret = dm_update_crtcs_state(dc, state, false, &lock_and_validation_needed);
  3813. if (ret) {
  3814. goto fail;
  3815. }
  3816. /* Enable all crtcs which require enable */
  3817. ret = dm_update_crtcs_state(dc, state, true, &lock_and_validation_needed);
  3818. if (ret) {
  3819. goto fail;
  3820. }
  3821. /* Add new/modified planes */
  3822. ret = dm_update_planes_state(dc, state, true, &lock_and_validation_needed);
  3823. if (ret) {
  3824. goto fail;
  3825. }
  3826. /* Run this here since we want to validate the streams we created */
  3827. ret = drm_atomic_helper_check_planes(dev, state);
  3828. if (ret)
  3829. goto fail;
  3830. /* Check scaling and undersacn changes*/
  3831. /*TODO Removed scaling changes validation due to inability to commit
  3832. * new stream into context w\o causing full reset. Need to
  3833. * decide how to handle.
  3834. */
  3835. for_each_connector_in_state(state, connector, conn_state, i) {
  3836. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  3837. struct dm_connector_state *con_old_state =
  3838. to_dm_connector_state(aconnector->base.state);
  3839. struct dm_connector_state *con_new_state =
  3840. to_dm_connector_state(conn_state);
  3841. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(con_new_state->base.crtc);
  3842. /* Skip any modesets/resets */
  3843. if (!acrtc || drm_atomic_crtc_needs_modeset(acrtc->base.state))
  3844. continue;
  3845. /* Skip any thing not scale or underscan changes */
  3846. if (!is_scaling_state_different(con_new_state, con_old_state))
  3847. continue;
  3848. lock_and_validation_needed = true;
  3849. }
  3850. /*
  3851. * For full updates case when
  3852. * removing/adding/updating streams on once CRTC while flipping
  3853. * on another CRTC,
  3854. * acquiring global lock will guarantee that any such full
  3855. * update commit
  3856. * will wait for completion of any outstanding flip using DRMs
  3857. * synchronization events.
  3858. */
  3859. if (lock_and_validation_needed) {
  3860. ret = do_aquire_global_lock(dev, state);
  3861. if (ret)
  3862. goto fail;
  3863. if (!dc_validate_global_state(dc, dm_state->context)) {
  3864. ret = -EINVAL;
  3865. goto fail;
  3866. }
  3867. }
  3868. /* Must be success */
  3869. WARN_ON(ret);
  3870. return ret;
  3871. fail:
  3872. if (ret == -EDEADLK)
  3873. DRM_DEBUG_KMS("Atomic check stopped due to to deadlock.\n");
  3874. else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
  3875. DRM_DEBUG_KMS("Atomic check stopped due to to signal.\n");
  3876. else
  3877. DRM_ERROR("Atomic check failed with err: %d \n", ret);
  3878. return ret;
  3879. }
  3880. static bool is_dp_capable_without_timing_msa(
  3881. struct dc *dc,
  3882. struct amdgpu_dm_connector *amdgpu_dm_connector)
  3883. {
  3884. uint8_t dpcd_data;
  3885. bool capable = false;
  3886. if (amdgpu_dm_connector->dc_link &&
  3887. dm_helpers_dp_read_dpcd(
  3888. NULL,
  3889. amdgpu_dm_connector->dc_link,
  3890. DP_DOWN_STREAM_PORT_COUNT,
  3891. &dpcd_data,
  3892. sizeof(dpcd_data))) {
  3893. capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
  3894. }
  3895. return capable;
  3896. }
  3897. void amdgpu_dm_add_sink_to_freesync_module(
  3898. struct drm_connector *connector,
  3899. struct edid *edid)
  3900. {
  3901. int i;
  3902. uint64_t val_capable;
  3903. bool edid_check_required;
  3904. struct detailed_timing *timing;
  3905. struct detailed_non_pixel *data;
  3906. struct detailed_data_monitor_range *range;
  3907. struct amdgpu_dm_connector *amdgpu_dm_connector =
  3908. to_amdgpu_dm_connector(connector);
  3909. struct drm_device *dev = connector->dev;
  3910. struct amdgpu_device *adev = dev->dev_private;
  3911. edid_check_required = false;
  3912. if (!amdgpu_dm_connector->dc_sink) {
  3913. DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
  3914. return;
  3915. }
  3916. if (!adev->dm.freesync_module)
  3917. return;
  3918. /*
  3919. * if edid non zero restrict freesync only for dp and edp
  3920. */
  3921. if (edid) {
  3922. if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
  3923. || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
  3924. edid_check_required = is_dp_capable_without_timing_msa(
  3925. adev->dm.dc,
  3926. amdgpu_dm_connector);
  3927. }
  3928. }
  3929. val_capable = 0;
  3930. if (edid_check_required == true && (edid->version > 1 ||
  3931. (edid->version == 1 && edid->revision > 1))) {
  3932. for (i = 0; i < 4; i++) {
  3933. timing = &edid->detailed_timings[i];
  3934. data = &timing->data.other_data;
  3935. range = &data->data.range;
  3936. /*
  3937. * Check if monitor has continuous frequency mode
  3938. */
  3939. if (data->type != EDID_DETAIL_MONITOR_RANGE)
  3940. continue;
  3941. /*
  3942. * Check for flag range limits only. If flag == 1 then
  3943. * no additional timing information provided.
  3944. * Default GTF, GTF Secondary curve and CVT are not
  3945. * supported
  3946. */
  3947. if (range->flags != 1)
  3948. continue;
  3949. amdgpu_dm_connector->min_vfreq = range->min_vfreq;
  3950. amdgpu_dm_connector->max_vfreq = range->max_vfreq;
  3951. amdgpu_dm_connector->pixel_clock_mhz =
  3952. range->pixel_clock_mhz * 10;
  3953. break;
  3954. }
  3955. if (amdgpu_dm_connector->max_vfreq -
  3956. amdgpu_dm_connector->min_vfreq > 10) {
  3957. amdgpu_dm_connector->caps.supported = true;
  3958. amdgpu_dm_connector->caps.min_refresh_in_micro_hz =
  3959. amdgpu_dm_connector->min_vfreq * 1000000;
  3960. amdgpu_dm_connector->caps.max_refresh_in_micro_hz =
  3961. amdgpu_dm_connector->max_vfreq * 1000000;
  3962. val_capable = 1;
  3963. }
  3964. }
  3965. /*
  3966. * TODO figure out how to notify user-mode or DRM of freesync caps
  3967. * once we figure out how to deal with freesync in an upstreamable
  3968. * fashion
  3969. */
  3970. }
  3971. void amdgpu_dm_remove_sink_from_freesync_module(
  3972. struct drm_connector *connector)
  3973. {
  3974. /*
  3975. * TODO fill in once we figure out how to deal with freesync in
  3976. * an upstreamable fashion
  3977. */
  3978. }