amdgpu_dm.c 131 KB

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