amdgpu_dm.c 128 KB

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