amdgpu_dm.c 154 KB

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