amdgpu_dm.c 138 KB

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