amdgpu_dm.c 131 KB

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