amdgpu_dm.c 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109
  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. static void register_backlight_device(struct amdgpu_display_manager *dm,
  1116. struct dc_link *link)
  1117. {
  1118. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
  1119. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  1120. if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
  1121. link->type != dc_connection_none) {
  1122. /* Event if registration failed, we should continue with
  1123. * DM initialization because not having a backlight control
  1124. * is better then a black screen.
  1125. */
  1126. amdgpu_dm_register_backlight_device(dm);
  1127. if (dm->backlight_dev)
  1128. dm->backlight_link = link;
  1129. }
  1130. #endif
  1131. }
  1132. /* In this architecture, the association
  1133. * connector -> encoder -> crtc
  1134. * id not really requried. The crtc and connector will hold the
  1135. * display_index as an abstraction to use with DAL component
  1136. *
  1137. * Returns 0 on success
  1138. */
  1139. static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev)
  1140. {
  1141. struct amdgpu_display_manager *dm = &adev->dm;
  1142. int32_t i;
  1143. struct amdgpu_dm_connector *aconnector = NULL;
  1144. struct amdgpu_encoder *aencoder = NULL;
  1145. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  1146. uint32_t link_cnt;
  1147. int32_t total_overlay_planes, total_primary_planes;
  1148. link_cnt = dm->dc->caps.max_links;
  1149. if (amdgpu_dm_mode_config_init(dm->adev)) {
  1150. DRM_ERROR("DM: Failed to initialize mode config\n");
  1151. return -1;
  1152. }
  1153. /* Identify the number of planes to be initialized */
  1154. total_overlay_planes = dm->dc->caps.max_slave_planes;
  1155. total_primary_planes = dm->dc->caps.max_planes - dm->dc->caps.max_slave_planes;
  1156. /* First initialize overlay planes, index starting after primary planes */
  1157. for (i = (total_overlay_planes - 1); i >= 0; i--) {
  1158. if (initialize_plane(dm, mode_info, (total_primary_planes + i))) {
  1159. DRM_ERROR("KMS: Failed to initialize overlay plane\n");
  1160. goto fail;
  1161. }
  1162. }
  1163. /* Initialize primary planes */
  1164. for (i = (total_primary_planes - 1); i >= 0; i--) {
  1165. if (initialize_plane(dm, mode_info, i)) {
  1166. DRM_ERROR("KMS: Failed to initialize primary plane\n");
  1167. goto fail;
  1168. }
  1169. }
  1170. for (i = 0; i < dm->dc->caps.max_streams; i++)
  1171. if (amdgpu_dm_crtc_init(dm, &mode_info->planes[i]->base, i)) {
  1172. DRM_ERROR("KMS: Failed to initialize crtc\n");
  1173. goto fail;
  1174. }
  1175. dm->display_indexes_num = dm->dc->caps.max_streams;
  1176. /* loops over all connectors on the board */
  1177. for (i = 0; i < link_cnt; i++) {
  1178. struct dc_link *link = NULL;
  1179. if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) {
  1180. DRM_ERROR(
  1181. "KMS: Cannot support more than %d display indexes\n",
  1182. AMDGPU_DM_MAX_DISPLAY_INDEX);
  1183. continue;
  1184. }
  1185. aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL);
  1186. if (!aconnector)
  1187. goto fail;
  1188. aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL);
  1189. if (!aencoder)
  1190. goto fail;
  1191. if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) {
  1192. DRM_ERROR("KMS: Failed to initialize encoder\n");
  1193. goto fail;
  1194. }
  1195. if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) {
  1196. DRM_ERROR("KMS: Failed to initialize connector\n");
  1197. goto fail;
  1198. }
  1199. link = dc_get_link_at_index(dm->dc, i);
  1200. if (dc_link_detect(link, DETECT_REASON_BOOT)) {
  1201. amdgpu_dm_update_connector_after_detect(aconnector);
  1202. register_backlight_device(dm, link);
  1203. }
  1204. }
  1205. /* Software is initialized. Now we can register interrupt handlers. */
  1206. switch (adev->asic_type) {
  1207. case CHIP_BONAIRE:
  1208. case CHIP_HAWAII:
  1209. case CHIP_KAVERI:
  1210. case CHIP_KABINI:
  1211. case CHIP_MULLINS:
  1212. case CHIP_TONGA:
  1213. case CHIP_FIJI:
  1214. case CHIP_CARRIZO:
  1215. case CHIP_STONEY:
  1216. case CHIP_POLARIS11:
  1217. case CHIP_POLARIS10:
  1218. case CHIP_POLARIS12:
  1219. case CHIP_VEGA10:
  1220. case CHIP_VEGA12:
  1221. if (dce110_register_irq_handlers(dm->adev)) {
  1222. DRM_ERROR("DM: Failed to initialize IRQ\n");
  1223. goto fail;
  1224. }
  1225. break;
  1226. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  1227. case CHIP_RAVEN:
  1228. if (dcn10_register_irq_handlers(dm->adev)) {
  1229. DRM_ERROR("DM: Failed to initialize IRQ\n");
  1230. goto fail;
  1231. }
  1232. /*
  1233. * Temporary disable until pplib/smu interaction is implemented
  1234. */
  1235. dm->dc->debug.disable_stutter = true;
  1236. break;
  1237. #endif
  1238. default:
  1239. DRM_ERROR("Usupported ASIC type: 0x%X\n", adev->asic_type);
  1240. goto fail;
  1241. }
  1242. return 0;
  1243. fail:
  1244. kfree(aencoder);
  1245. kfree(aconnector);
  1246. for (i = 0; i < dm->dc->caps.max_planes; i++)
  1247. kfree(mode_info->planes[i]);
  1248. return -1;
  1249. }
  1250. static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm)
  1251. {
  1252. drm_mode_config_cleanup(dm->ddev);
  1253. return;
  1254. }
  1255. /******************************************************************************
  1256. * amdgpu_display_funcs functions
  1257. *****************************************************************************/
  1258. /**
  1259. * dm_bandwidth_update - program display watermarks
  1260. *
  1261. * @adev: amdgpu_device pointer
  1262. *
  1263. * Calculate and program the display watermarks and line buffer allocation.
  1264. */
  1265. static void dm_bandwidth_update(struct amdgpu_device *adev)
  1266. {
  1267. /* TODO: implement later */
  1268. }
  1269. static void dm_set_backlight_level(struct amdgpu_encoder *amdgpu_encoder,
  1270. u8 level)
  1271. {
  1272. /* TODO: translate amdgpu_encoder to display_index and call DAL */
  1273. }
  1274. static u8 dm_get_backlight_level(struct amdgpu_encoder *amdgpu_encoder)
  1275. {
  1276. /* TODO: translate amdgpu_encoder to display_index and call DAL */
  1277. return 0;
  1278. }
  1279. static int amdgpu_notify_freesync(struct drm_device *dev, void *data,
  1280. struct drm_file *filp)
  1281. {
  1282. struct mod_freesync_params freesync_params;
  1283. uint8_t num_streams;
  1284. uint8_t i;
  1285. struct amdgpu_device *adev = dev->dev_private;
  1286. int r = 0;
  1287. /* Get freesync enable flag from DRM */
  1288. num_streams = dc_get_current_stream_count(adev->dm.dc);
  1289. for (i = 0; i < num_streams; i++) {
  1290. struct dc_stream_state *stream;
  1291. stream = dc_get_stream_at_index(adev->dm.dc, i);
  1292. mod_freesync_update_state(adev->dm.freesync_module,
  1293. &stream, 1, &freesync_params);
  1294. }
  1295. return r;
  1296. }
  1297. static const struct amdgpu_display_funcs dm_display_funcs = {
  1298. .bandwidth_update = dm_bandwidth_update, /* called unconditionally */
  1299. .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */
  1300. .backlight_set_level =
  1301. dm_set_backlight_level,/* called unconditionally */
  1302. .backlight_get_level =
  1303. dm_get_backlight_level,/* called unconditionally */
  1304. .hpd_sense = NULL,/* called unconditionally */
  1305. .hpd_set_polarity = NULL, /* called unconditionally */
  1306. .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */
  1307. .page_flip_get_scanoutpos =
  1308. dm_crtc_get_scanoutpos,/* called unconditionally */
  1309. .add_encoder = NULL, /* VBIOS parsing. DAL does it. */
  1310. .add_connector = NULL, /* VBIOS parsing. DAL does it. */
  1311. .notify_freesync = amdgpu_notify_freesync,
  1312. };
  1313. #if defined(CONFIG_DEBUG_KERNEL_DC)
  1314. static ssize_t s3_debug_store(struct device *device,
  1315. struct device_attribute *attr,
  1316. const char *buf,
  1317. size_t count)
  1318. {
  1319. int ret;
  1320. int s3_state;
  1321. struct pci_dev *pdev = to_pci_dev(device);
  1322. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  1323. struct amdgpu_device *adev = drm_dev->dev_private;
  1324. ret = kstrtoint(buf, 0, &s3_state);
  1325. if (ret == 0) {
  1326. if (s3_state) {
  1327. dm_resume(adev);
  1328. amdgpu_dm_display_resume(adev);
  1329. drm_kms_helper_hotplug_event(adev->ddev);
  1330. } else
  1331. dm_suspend(adev);
  1332. }
  1333. return ret == 0 ? count : 0;
  1334. }
  1335. DEVICE_ATTR_WO(s3_debug);
  1336. #endif
  1337. static int dm_early_init(void *handle)
  1338. {
  1339. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  1340. switch (adev->asic_type) {
  1341. case CHIP_BONAIRE:
  1342. case CHIP_HAWAII:
  1343. adev->mode_info.num_crtc = 6;
  1344. adev->mode_info.num_hpd = 6;
  1345. adev->mode_info.num_dig = 6;
  1346. adev->mode_info.plane_type = dm_plane_type_default;
  1347. break;
  1348. case CHIP_KAVERI:
  1349. adev->mode_info.num_crtc = 4;
  1350. adev->mode_info.num_hpd = 6;
  1351. adev->mode_info.num_dig = 7;
  1352. adev->mode_info.plane_type = dm_plane_type_default;
  1353. break;
  1354. case CHIP_KABINI:
  1355. case CHIP_MULLINS:
  1356. adev->mode_info.num_crtc = 2;
  1357. adev->mode_info.num_hpd = 6;
  1358. adev->mode_info.num_dig = 6;
  1359. adev->mode_info.plane_type = dm_plane_type_default;
  1360. break;
  1361. case CHIP_FIJI:
  1362. case CHIP_TONGA:
  1363. adev->mode_info.num_crtc = 6;
  1364. adev->mode_info.num_hpd = 6;
  1365. adev->mode_info.num_dig = 7;
  1366. adev->mode_info.plane_type = dm_plane_type_default;
  1367. break;
  1368. case CHIP_CARRIZO:
  1369. adev->mode_info.num_crtc = 3;
  1370. adev->mode_info.num_hpd = 6;
  1371. adev->mode_info.num_dig = 9;
  1372. adev->mode_info.plane_type = dm_plane_type_carizzo;
  1373. break;
  1374. case CHIP_STONEY:
  1375. adev->mode_info.num_crtc = 2;
  1376. adev->mode_info.num_hpd = 6;
  1377. adev->mode_info.num_dig = 9;
  1378. adev->mode_info.plane_type = dm_plane_type_stoney;
  1379. break;
  1380. case CHIP_POLARIS11:
  1381. case CHIP_POLARIS12:
  1382. adev->mode_info.num_crtc = 5;
  1383. adev->mode_info.num_hpd = 5;
  1384. adev->mode_info.num_dig = 5;
  1385. adev->mode_info.plane_type = dm_plane_type_default;
  1386. break;
  1387. case CHIP_POLARIS10:
  1388. adev->mode_info.num_crtc = 6;
  1389. adev->mode_info.num_hpd = 6;
  1390. adev->mode_info.num_dig = 6;
  1391. adev->mode_info.plane_type = dm_plane_type_default;
  1392. break;
  1393. case CHIP_VEGA10:
  1394. case CHIP_VEGA12:
  1395. adev->mode_info.num_crtc = 6;
  1396. adev->mode_info.num_hpd = 6;
  1397. adev->mode_info.num_dig = 6;
  1398. adev->mode_info.plane_type = dm_plane_type_default;
  1399. break;
  1400. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  1401. case CHIP_RAVEN:
  1402. adev->mode_info.num_crtc = 4;
  1403. adev->mode_info.num_hpd = 4;
  1404. adev->mode_info.num_dig = 4;
  1405. adev->mode_info.plane_type = dm_plane_type_default;
  1406. break;
  1407. #endif
  1408. default:
  1409. DRM_ERROR("Usupported ASIC type: 0x%X\n", adev->asic_type);
  1410. return -EINVAL;
  1411. }
  1412. amdgpu_dm_set_irq_funcs(adev);
  1413. if (adev->mode_info.funcs == NULL)
  1414. adev->mode_info.funcs = &dm_display_funcs;
  1415. /* Note: Do NOT change adev->audio_endpt_rreg and
  1416. * adev->audio_endpt_wreg because they are initialised in
  1417. * amdgpu_device_init() */
  1418. #if defined(CONFIG_DEBUG_KERNEL_DC)
  1419. device_create_file(
  1420. adev->ddev->dev,
  1421. &dev_attr_s3_debug);
  1422. #endif
  1423. return 0;
  1424. }
  1425. static bool modeset_required(struct drm_crtc_state *crtc_state,
  1426. struct dc_stream_state *new_stream,
  1427. struct dc_stream_state *old_stream)
  1428. {
  1429. if (!drm_atomic_crtc_needs_modeset(crtc_state))
  1430. return false;
  1431. if (!crtc_state->enable)
  1432. return false;
  1433. return crtc_state->active;
  1434. }
  1435. static bool modereset_required(struct drm_crtc_state *crtc_state)
  1436. {
  1437. if (!drm_atomic_crtc_needs_modeset(crtc_state))
  1438. return false;
  1439. return !crtc_state->enable || !crtc_state->active;
  1440. }
  1441. static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder)
  1442. {
  1443. drm_encoder_cleanup(encoder);
  1444. kfree(encoder);
  1445. }
  1446. static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = {
  1447. .destroy = amdgpu_dm_encoder_destroy,
  1448. };
  1449. static bool fill_rects_from_plane_state(const struct drm_plane_state *state,
  1450. struct dc_plane_state *plane_state)
  1451. {
  1452. plane_state->src_rect.x = state->src_x >> 16;
  1453. plane_state->src_rect.y = state->src_y >> 16;
  1454. /*we ignore for now mantissa and do not to deal with floating pixels :(*/
  1455. plane_state->src_rect.width = state->src_w >> 16;
  1456. if (plane_state->src_rect.width == 0)
  1457. return false;
  1458. plane_state->src_rect.height = state->src_h >> 16;
  1459. if (plane_state->src_rect.height == 0)
  1460. return false;
  1461. plane_state->dst_rect.x = state->crtc_x;
  1462. plane_state->dst_rect.y = state->crtc_y;
  1463. if (state->crtc_w == 0)
  1464. return false;
  1465. plane_state->dst_rect.width = state->crtc_w;
  1466. if (state->crtc_h == 0)
  1467. return false;
  1468. plane_state->dst_rect.height = state->crtc_h;
  1469. plane_state->clip_rect = plane_state->dst_rect;
  1470. switch (state->rotation & DRM_MODE_ROTATE_MASK) {
  1471. case DRM_MODE_ROTATE_0:
  1472. plane_state->rotation = ROTATION_ANGLE_0;
  1473. break;
  1474. case DRM_MODE_ROTATE_90:
  1475. plane_state->rotation = ROTATION_ANGLE_90;
  1476. break;
  1477. case DRM_MODE_ROTATE_180:
  1478. plane_state->rotation = ROTATION_ANGLE_180;
  1479. break;
  1480. case DRM_MODE_ROTATE_270:
  1481. plane_state->rotation = ROTATION_ANGLE_270;
  1482. break;
  1483. default:
  1484. plane_state->rotation = ROTATION_ANGLE_0;
  1485. break;
  1486. }
  1487. return true;
  1488. }
  1489. static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
  1490. uint64_t *tiling_flags)
  1491. {
  1492. struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->obj);
  1493. int r = amdgpu_bo_reserve(rbo, false);
  1494. if (unlikely(r)) {
  1495. // Don't show error msg. when return -ERESTARTSYS
  1496. if (r != -ERESTARTSYS)
  1497. DRM_ERROR("Unable to reserve buffer: %d\n", r);
  1498. return r;
  1499. }
  1500. if (tiling_flags)
  1501. amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
  1502. amdgpu_bo_unreserve(rbo);
  1503. return r;
  1504. }
  1505. static int fill_plane_attributes_from_fb(struct amdgpu_device *adev,
  1506. struct dc_plane_state *plane_state,
  1507. const struct amdgpu_framebuffer *amdgpu_fb)
  1508. {
  1509. uint64_t tiling_flags;
  1510. unsigned int awidth;
  1511. const struct drm_framebuffer *fb = &amdgpu_fb->base;
  1512. int ret = 0;
  1513. struct drm_format_name_buf format_name;
  1514. ret = get_fb_info(
  1515. amdgpu_fb,
  1516. &tiling_flags);
  1517. if (ret)
  1518. return ret;
  1519. switch (fb->format->format) {
  1520. case DRM_FORMAT_C8:
  1521. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS;
  1522. break;
  1523. case DRM_FORMAT_RGB565:
  1524. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565;
  1525. break;
  1526. case DRM_FORMAT_XRGB8888:
  1527. case DRM_FORMAT_ARGB8888:
  1528. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888;
  1529. break;
  1530. case DRM_FORMAT_XRGB2101010:
  1531. case DRM_FORMAT_ARGB2101010:
  1532. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010;
  1533. break;
  1534. case DRM_FORMAT_XBGR2101010:
  1535. case DRM_FORMAT_ABGR2101010:
  1536. plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010;
  1537. break;
  1538. case DRM_FORMAT_NV21:
  1539. plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr;
  1540. break;
  1541. case DRM_FORMAT_NV12:
  1542. plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb;
  1543. break;
  1544. default:
  1545. DRM_ERROR("Unsupported screen format %s\n",
  1546. drm_get_format_name(fb->format->format, &format_name));
  1547. return -EINVAL;
  1548. }
  1549. if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
  1550. plane_state->address.type = PLN_ADDR_TYPE_GRAPHICS;
  1551. plane_state->plane_size.grph.surface_size.x = 0;
  1552. plane_state->plane_size.grph.surface_size.y = 0;
  1553. plane_state->plane_size.grph.surface_size.width = fb->width;
  1554. plane_state->plane_size.grph.surface_size.height = fb->height;
  1555. plane_state->plane_size.grph.surface_pitch =
  1556. fb->pitches[0] / fb->format->cpp[0];
  1557. /* TODO: unhardcode */
  1558. plane_state->color_space = COLOR_SPACE_SRGB;
  1559. } else {
  1560. awidth = ALIGN(fb->width, 64);
  1561. plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
  1562. plane_state->plane_size.video.luma_size.x = 0;
  1563. plane_state->plane_size.video.luma_size.y = 0;
  1564. plane_state->plane_size.video.luma_size.width = awidth;
  1565. plane_state->plane_size.video.luma_size.height = fb->height;
  1566. /* TODO: unhardcode */
  1567. plane_state->plane_size.video.luma_pitch = awidth;
  1568. plane_state->plane_size.video.chroma_size.x = 0;
  1569. plane_state->plane_size.video.chroma_size.y = 0;
  1570. plane_state->plane_size.video.chroma_size.width = awidth;
  1571. plane_state->plane_size.video.chroma_size.height = fb->height;
  1572. plane_state->plane_size.video.chroma_pitch = awidth / 2;
  1573. /* TODO: unhardcode */
  1574. plane_state->color_space = COLOR_SPACE_YCBCR709;
  1575. }
  1576. memset(&plane_state->tiling_info, 0, sizeof(plane_state->tiling_info));
  1577. /* Fill GFX8 params */
  1578. if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) {
  1579. unsigned int bankw, bankh, mtaspect, tile_split, num_banks;
  1580. bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
  1581. bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
  1582. mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
  1583. tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
  1584. num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
  1585. /* XXX fix me for VI */
  1586. plane_state->tiling_info.gfx8.num_banks = num_banks;
  1587. plane_state->tiling_info.gfx8.array_mode =
  1588. DC_ARRAY_2D_TILED_THIN1;
  1589. plane_state->tiling_info.gfx8.tile_split = tile_split;
  1590. plane_state->tiling_info.gfx8.bank_width = bankw;
  1591. plane_state->tiling_info.gfx8.bank_height = bankh;
  1592. plane_state->tiling_info.gfx8.tile_aspect = mtaspect;
  1593. plane_state->tiling_info.gfx8.tile_mode =
  1594. DC_ADDR_SURF_MICRO_TILING_DISPLAY;
  1595. } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE)
  1596. == DC_ARRAY_1D_TILED_THIN1) {
  1597. plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1;
  1598. }
  1599. plane_state->tiling_info.gfx8.pipe_config =
  1600. AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
  1601. if (adev->asic_type == CHIP_VEGA10 ||
  1602. adev->asic_type == CHIP_VEGA12 ||
  1603. adev->asic_type == CHIP_RAVEN) {
  1604. /* Fill GFX9 params */
  1605. plane_state->tiling_info.gfx9.num_pipes =
  1606. adev->gfx.config.gb_addr_config_fields.num_pipes;
  1607. plane_state->tiling_info.gfx9.num_banks =
  1608. adev->gfx.config.gb_addr_config_fields.num_banks;
  1609. plane_state->tiling_info.gfx9.pipe_interleave =
  1610. adev->gfx.config.gb_addr_config_fields.pipe_interleave_size;
  1611. plane_state->tiling_info.gfx9.num_shader_engines =
  1612. adev->gfx.config.gb_addr_config_fields.num_se;
  1613. plane_state->tiling_info.gfx9.max_compressed_frags =
  1614. adev->gfx.config.gb_addr_config_fields.max_compress_frags;
  1615. plane_state->tiling_info.gfx9.num_rb_per_se =
  1616. adev->gfx.config.gb_addr_config_fields.num_rb_per_se;
  1617. plane_state->tiling_info.gfx9.swizzle =
  1618. AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
  1619. plane_state->tiling_info.gfx9.shaderEnable = 1;
  1620. }
  1621. plane_state->visible = true;
  1622. plane_state->scaling_quality.h_taps_c = 0;
  1623. plane_state->scaling_quality.v_taps_c = 0;
  1624. /* is this needed? is plane_state zeroed at allocation? */
  1625. plane_state->scaling_quality.h_taps = 0;
  1626. plane_state->scaling_quality.v_taps = 0;
  1627. plane_state->stereo_format = PLANE_STEREO_FORMAT_NONE;
  1628. return ret;
  1629. }
  1630. static int fill_plane_attributes(struct amdgpu_device *adev,
  1631. struct dc_plane_state *dc_plane_state,
  1632. struct drm_plane_state *plane_state,
  1633. struct drm_crtc_state *crtc_state)
  1634. {
  1635. const struct amdgpu_framebuffer *amdgpu_fb =
  1636. to_amdgpu_framebuffer(plane_state->fb);
  1637. const struct drm_crtc *crtc = plane_state->crtc;
  1638. struct dc_transfer_func *input_tf;
  1639. int ret = 0;
  1640. if (!fill_rects_from_plane_state(plane_state, dc_plane_state))
  1641. return -EINVAL;
  1642. ret = fill_plane_attributes_from_fb(
  1643. crtc->dev->dev_private,
  1644. dc_plane_state,
  1645. amdgpu_fb);
  1646. if (ret)
  1647. return ret;
  1648. input_tf = dc_create_transfer_func();
  1649. if (input_tf == NULL)
  1650. return -ENOMEM;
  1651. dc_plane_state->in_transfer_func = input_tf;
  1652. /*
  1653. * Always set input transfer function, since plane state is refreshed
  1654. * every time.
  1655. */
  1656. ret = amdgpu_dm_set_degamma_lut(crtc_state, dc_plane_state);
  1657. if (ret) {
  1658. dc_transfer_func_release(dc_plane_state->in_transfer_func);
  1659. dc_plane_state->in_transfer_func = NULL;
  1660. }
  1661. return ret;
  1662. }
  1663. /*****************************************************************************/
  1664. static void update_stream_scaling_settings(const struct drm_display_mode *mode,
  1665. const struct dm_connector_state *dm_state,
  1666. struct dc_stream_state *stream)
  1667. {
  1668. enum amdgpu_rmx_type rmx_type;
  1669. struct rect src = { 0 }; /* viewport in composition space*/
  1670. struct rect dst = { 0 }; /* stream addressable area */
  1671. /* no mode. nothing to be done */
  1672. if (!mode)
  1673. return;
  1674. /* Full screen scaling by default */
  1675. src.width = mode->hdisplay;
  1676. src.height = mode->vdisplay;
  1677. dst.width = stream->timing.h_addressable;
  1678. dst.height = stream->timing.v_addressable;
  1679. if (dm_state) {
  1680. rmx_type = dm_state->scaling;
  1681. if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) {
  1682. if (src.width * dst.height <
  1683. src.height * dst.width) {
  1684. /* height needs less upscaling/more downscaling */
  1685. dst.width = src.width *
  1686. dst.height / src.height;
  1687. } else {
  1688. /* width needs less upscaling/more downscaling */
  1689. dst.height = src.height *
  1690. dst.width / src.width;
  1691. }
  1692. } else if (rmx_type == RMX_CENTER) {
  1693. dst = src;
  1694. }
  1695. dst.x = (stream->timing.h_addressable - dst.width) / 2;
  1696. dst.y = (stream->timing.v_addressable - dst.height) / 2;
  1697. if (dm_state->underscan_enable) {
  1698. dst.x += dm_state->underscan_hborder / 2;
  1699. dst.y += dm_state->underscan_vborder / 2;
  1700. dst.width -= dm_state->underscan_hborder;
  1701. dst.height -= dm_state->underscan_vborder;
  1702. }
  1703. }
  1704. stream->src = src;
  1705. stream->dst = dst;
  1706. DRM_DEBUG_DRIVER("Destination Rectangle x:%d y:%d width:%d height:%d\n",
  1707. dst.x, dst.y, dst.width, dst.height);
  1708. }
  1709. static enum dc_color_depth
  1710. convert_color_depth_from_display_info(const struct drm_connector *connector)
  1711. {
  1712. uint32_t bpc = connector->display_info.bpc;
  1713. /* Limited color depth to 8bit
  1714. * TODO: Still need to handle deep color
  1715. */
  1716. if (bpc > 8)
  1717. bpc = 8;
  1718. switch (bpc) {
  1719. case 0:
  1720. /* Temporary Work around, DRM don't parse color depth for
  1721. * EDID revision before 1.4
  1722. * TODO: Fix edid parsing
  1723. */
  1724. return COLOR_DEPTH_888;
  1725. case 6:
  1726. return COLOR_DEPTH_666;
  1727. case 8:
  1728. return COLOR_DEPTH_888;
  1729. case 10:
  1730. return COLOR_DEPTH_101010;
  1731. case 12:
  1732. return COLOR_DEPTH_121212;
  1733. case 14:
  1734. return COLOR_DEPTH_141414;
  1735. case 16:
  1736. return COLOR_DEPTH_161616;
  1737. default:
  1738. return COLOR_DEPTH_UNDEFINED;
  1739. }
  1740. }
  1741. static enum dc_aspect_ratio
  1742. get_aspect_ratio(const struct drm_display_mode *mode_in)
  1743. {
  1744. int32_t width = mode_in->crtc_hdisplay * 9;
  1745. int32_t height = mode_in->crtc_vdisplay * 16;
  1746. if ((width - height) < 10 && (width - height) > -10)
  1747. return ASPECT_RATIO_16_9;
  1748. else
  1749. return ASPECT_RATIO_4_3;
  1750. }
  1751. static enum dc_color_space
  1752. get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing)
  1753. {
  1754. enum dc_color_space color_space = COLOR_SPACE_SRGB;
  1755. switch (dc_crtc_timing->pixel_encoding) {
  1756. case PIXEL_ENCODING_YCBCR422:
  1757. case PIXEL_ENCODING_YCBCR444:
  1758. case PIXEL_ENCODING_YCBCR420:
  1759. {
  1760. /*
  1761. * 27030khz is the separation point between HDTV and SDTV
  1762. * according to HDMI spec, we use YCbCr709 and YCbCr601
  1763. * respectively
  1764. */
  1765. if (dc_crtc_timing->pix_clk_khz > 27030) {
  1766. if (dc_crtc_timing->flags.Y_ONLY)
  1767. color_space =
  1768. COLOR_SPACE_YCBCR709_LIMITED;
  1769. else
  1770. color_space = COLOR_SPACE_YCBCR709;
  1771. } else {
  1772. if (dc_crtc_timing->flags.Y_ONLY)
  1773. color_space =
  1774. COLOR_SPACE_YCBCR601_LIMITED;
  1775. else
  1776. color_space = COLOR_SPACE_YCBCR601;
  1777. }
  1778. }
  1779. break;
  1780. case PIXEL_ENCODING_RGB:
  1781. color_space = COLOR_SPACE_SRGB;
  1782. break;
  1783. default:
  1784. WARN_ON(1);
  1785. break;
  1786. }
  1787. return color_space;
  1788. }
  1789. /*****************************************************************************/
  1790. static void
  1791. fill_stream_properties_from_drm_display_mode(struct dc_stream_state *stream,
  1792. const struct drm_display_mode *mode_in,
  1793. const struct drm_connector *connector)
  1794. {
  1795. struct dc_crtc_timing *timing_out = &stream->timing;
  1796. struct dc_transfer_func *tf = dc_create_transfer_func();
  1797. memset(timing_out, 0, sizeof(struct dc_crtc_timing));
  1798. timing_out->h_border_left = 0;
  1799. timing_out->h_border_right = 0;
  1800. timing_out->v_border_top = 0;
  1801. timing_out->v_border_bottom = 0;
  1802. /* TODO: un-hardcode */
  1803. if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444)
  1804. && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A)
  1805. timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444;
  1806. else
  1807. timing_out->pixel_encoding = PIXEL_ENCODING_RGB;
  1808. timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE;
  1809. timing_out->display_color_depth = convert_color_depth_from_display_info(
  1810. connector);
  1811. timing_out->scan_type = SCANNING_TYPE_NODATA;
  1812. timing_out->hdmi_vic = 0;
  1813. timing_out->vic = drm_match_cea_mode(mode_in);
  1814. timing_out->h_addressable = mode_in->crtc_hdisplay;
  1815. timing_out->h_total = mode_in->crtc_htotal;
  1816. timing_out->h_sync_width =
  1817. mode_in->crtc_hsync_end - mode_in->crtc_hsync_start;
  1818. timing_out->h_front_porch =
  1819. mode_in->crtc_hsync_start - mode_in->crtc_hdisplay;
  1820. timing_out->v_total = mode_in->crtc_vtotal;
  1821. timing_out->v_addressable = mode_in->crtc_vdisplay;
  1822. timing_out->v_front_porch =
  1823. mode_in->crtc_vsync_start - mode_in->crtc_vdisplay;
  1824. timing_out->v_sync_width =
  1825. mode_in->crtc_vsync_end - mode_in->crtc_vsync_start;
  1826. timing_out->pix_clk_khz = mode_in->crtc_clock;
  1827. timing_out->aspect_ratio = get_aspect_ratio(mode_in);
  1828. if (mode_in->flags & DRM_MODE_FLAG_PHSYNC)
  1829. timing_out->flags.HSYNC_POSITIVE_POLARITY = 1;
  1830. if (mode_in->flags & DRM_MODE_FLAG_PVSYNC)
  1831. timing_out->flags.VSYNC_POSITIVE_POLARITY = 1;
  1832. stream->output_color_space = get_output_color_space(timing_out);
  1833. tf->type = TF_TYPE_PREDEFINED;
  1834. tf->tf = TRANSFER_FUNCTION_SRGB;
  1835. stream->out_transfer_func = tf;
  1836. }
  1837. static void fill_audio_info(struct audio_info *audio_info,
  1838. const struct drm_connector *drm_connector,
  1839. const struct dc_sink *dc_sink)
  1840. {
  1841. int i = 0;
  1842. int cea_revision = 0;
  1843. const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps;
  1844. audio_info->manufacture_id = edid_caps->manufacturer_id;
  1845. audio_info->product_id = edid_caps->product_id;
  1846. cea_revision = drm_connector->display_info.cea_rev;
  1847. strncpy(audio_info->display_name,
  1848. edid_caps->display_name,
  1849. AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS - 1);
  1850. if (cea_revision >= 3) {
  1851. audio_info->mode_count = edid_caps->audio_mode_count;
  1852. for (i = 0; i < audio_info->mode_count; ++i) {
  1853. audio_info->modes[i].format_code =
  1854. (enum audio_format_code)
  1855. (edid_caps->audio_modes[i].format_code);
  1856. audio_info->modes[i].channel_count =
  1857. edid_caps->audio_modes[i].channel_count;
  1858. audio_info->modes[i].sample_rates.all =
  1859. edid_caps->audio_modes[i].sample_rate;
  1860. audio_info->modes[i].sample_size =
  1861. edid_caps->audio_modes[i].sample_size;
  1862. }
  1863. }
  1864. audio_info->flags.all = edid_caps->speaker_flags;
  1865. /* TODO: We only check for the progressive mode, check for interlace mode too */
  1866. if (drm_connector->latency_present[0]) {
  1867. audio_info->video_latency = drm_connector->video_latency[0];
  1868. audio_info->audio_latency = drm_connector->audio_latency[0];
  1869. }
  1870. /* TODO: For DP, video and audio latency should be calculated from DPCD caps */
  1871. }
  1872. static void
  1873. copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode,
  1874. struct drm_display_mode *dst_mode)
  1875. {
  1876. dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay;
  1877. dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay;
  1878. dst_mode->crtc_clock = src_mode->crtc_clock;
  1879. dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start;
  1880. dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end;
  1881. dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start;
  1882. dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end;
  1883. dst_mode->crtc_htotal = src_mode->crtc_htotal;
  1884. dst_mode->crtc_hskew = src_mode->crtc_hskew;
  1885. dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start;
  1886. dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end;
  1887. dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start;
  1888. dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end;
  1889. dst_mode->crtc_vtotal = src_mode->crtc_vtotal;
  1890. }
  1891. static void
  1892. decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode,
  1893. const struct drm_display_mode *native_mode,
  1894. bool scale_enabled)
  1895. {
  1896. if (scale_enabled) {
  1897. copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
  1898. } else if (native_mode->clock == drm_mode->clock &&
  1899. native_mode->htotal == drm_mode->htotal &&
  1900. native_mode->vtotal == drm_mode->vtotal) {
  1901. copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode);
  1902. } else {
  1903. /* no scaling nor amdgpu inserted, no need to patch */
  1904. }
  1905. }
  1906. static int create_fake_sink(struct amdgpu_dm_connector *aconnector)
  1907. {
  1908. struct dc_sink *sink = NULL;
  1909. struct dc_sink_init_data sink_init_data = { 0 };
  1910. sink_init_data.link = aconnector->dc_link;
  1911. sink_init_data.sink_signal = aconnector->dc_link->connector_signal;
  1912. sink = dc_sink_create(&sink_init_data);
  1913. if (!sink) {
  1914. DRM_ERROR("Failed to create sink!\n");
  1915. return -ENOMEM;
  1916. }
  1917. sink->sink_signal = SIGNAL_TYPE_VIRTUAL;
  1918. aconnector->fake_enable = true;
  1919. aconnector->dc_sink = sink;
  1920. aconnector->dc_link->local_sink = sink;
  1921. return 0;
  1922. }
  1923. static void set_multisync_trigger_params(
  1924. struct dc_stream_state *stream)
  1925. {
  1926. if (stream->triggered_crtc_reset.enabled) {
  1927. stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING;
  1928. stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE;
  1929. }
  1930. }
  1931. static void set_master_stream(struct dc_stream_state *stream_set[],
  1932. int stream_count)
  1933. {
  1934. int j, highest_rfr = 0, master_stream = 0;
  1935. for (j = 0; j < stream_count; j++) {
  1936. if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) {
  1937. int refresh_rate = 0;
  1938. refresh_rate = (stream_set[j]->timing.pix_clk_khz*1000)/
  1939. (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total);
  1940. if (refresh_rate > highest_rfr) {
  1941. highest_rfr = refresh_rate;
  1942. master_stream = j;
  1943. }
  1944. }
  1945. }
  1946. for (j = 0; j < stream_count; j++) {
  1947. if (stream_set[j])
  1948. stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream];
  1949. }
  1950. }
  1951. static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context)
  1952. {
  1953. int i = 0;
  1954. if (context->stream_count < 2)
  1955. return;
  1956. for (i = 0; i < context->stream_count ; i++) {
  1957. if (!context->streams[i])
  1958. continue;
  1959. /* TODO: add a function to read AMD VSDB bits and will set
  1960. * crtc_sync_master.multi_sync_enabled flag
  1961. * For now its set to false
  1962. */
  1963. set_multisync_trigger_params(context->streams[i]);
  1964. }
  1965. set_master_stream(context->streams, context->stream_count);
  1966. }
  1967. static struct dc_stream_state *
  1968. create_stream_for_sink(struct amdgpu_dm_connector *aconnector,
  1969. const struct drm_display_mode *drm_mode,
  1970. const struct dm_connector_state *dm_state)
  1971. {
  1972. struct drm_display_mode *preferred_mode = NULL;
  1973. struct drm_connector *drm_connector;
  1974. struct dc_stream_state *stream = NULL;
  1975. struct drm_display_mode mode = *drm_mode;
  1976. bool native_mode_found = false;
  1977. if (aconnector == NULL) {
  1978. DRM_ERROR("aconnector is NULL!\n");
  1979. return stream;
  1980. }
  1981. drm_connector = &aconnector->base;
  1982. if (!aconnector->dc_sink) {
  1983. /*
  1984. * Create dc_sink when necessary to MST
  1985. * Don't apply fake_sink to MST
  1986. */
  1987. if (aconnector->mst_port) {
  1988. dm_dp_mst_dc_sink_create(drm_connector);
  1989. return stream;
  1990. }
  1991. if (create_fake_sink(aconnector))
  1992. return stream;
  1993. }
  1994. stream = dc_create_stream_for_sink(aconnector->dc_sink);
  1995. if (stream == NULL) {
  1996. DRM_ERROR("Failed to create stream for sink!\n");
  1997. return stream;
  1998. }
  1999. list_for_each_entry(preferred_mode, &aconnector->base.modes, head) {
  2000. /* Search for preferred mode */
  2001. if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) {
  2002. native_mode_found = true;
  2003. break;
  2004. }
  2005. }
  2006. if (!native_mode_found)
  2007. preferred_mode = list_first_entry_or_null(
  2008. &aconnector->base.modes,
  2009. struct drm_display_mode,
  2010. head);
  2011. if (preferred_mode == NULL) {
  2012. /* This may not be an error, the use case is when we we have no
  2013. * usermode calls to reset and set mode upon hotplug. In this
  2014. * case, we call set mode ourselves to restore the previous mode
  2015. * and the modelist may not be filled in in time.
  2016. */
  2017. DRM_DEBUG_DRIVER("No preferred mode found\n");
  2018. } else {
  2019. decide_crtc_timing_for_drm_display_mode(
  2020. &mode, preferred_mode,
  2021. dm_state ? (dm_state->scaling != RMX_OFF) : false);
  2022. }
  2023. if (!dm_state)
  2024. drm_mode_set_crtcinfo(&mode, 0);
  2025. fill_stream_properties_from_drm_display_mode(stream,
  2026. &mode, &aconnector->base);
  2027. update_stream_scaling_settings(&mode, dm_state, stream);
  2028. fill_audio_info(
  2029. &stream->audio_info,
  2030. drm_connector,
  2031. aconnector->dc_sink);
  2032. update_stream_signal(stream);
  2033. return stream;
  2034. }
  2035. static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc)
  2036. {
  2037. drm_crtc_cleanup(crtc);
  2038. kfree(crtc);
  2039. }
  2040. static void dm_crtc_destroy_state(struct drm_crtc *crtc,
  2041. struct drm_crtc_state *state)
  2042. {
  2043. struct dm_crtc_state *cur = to_dm_crtc_state(state);
  2044. /* TODO Destroy dc_stream objects are stream object is flattened */
  2045. if (cur->stream)
  2046. dc_stream_release(cur->stream);
  2047. __drm_atomic_helper_crtc_destroy_state(state);
  2048. kfree(state);
  2049. }
  2050. static void dm_crtc_reset_state(struct drm_crtc *crtc)
  2051. {
  2052. struct dm_crtc_state *state;
  2053. if (crtc->state)
  2054. dm_crtc_destroy_state(crtc, crtc->state);
  2055. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2056. if (WARN_ON(!state))
  2057. return;
  2058. crtc->state = &state->base;
  2059. crtc->state->crtc = crtc;
  2060. }
  2061. static struct drm_crtc_state *
  2062. dm_crtc_duplicate_state(struct drm_crtc *crtc)
  2063. {
  2064. struct dm_crtc_state *state, *cur;
  2065. cur = to_dm_crtc_state(crtc->state);
  2066. if (WARN_ON(!crtc->state))
  2067. return NULL;
  2068. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2069. if (!state)
  2070. return NULL;
  2071. __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
  2072. if (cur->stream) {
  2073. state->stream = cur->stream;
  2074. dc_stream_retain(state->stream);
  2075. }
  2076. /* TODO Duplicate dc_stream after objects are stream object is flattened */
  2077. return &state->base;
  2078. }
  2079. static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable)
  2080. {
  2081. enum dc_irq_source irq_source;
  2082. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  2083. struct amdgpu_device *adev = crtc->dev->dev_private;
  2084. irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst;
  2085. return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY;
  2086. }
  2087. static int dm_enable_vblank(struct drm_crtc *crtc)
  2088. {
  2089. return dm_set_vblank(crtc, true);
  2090. }
  2091. static void dm_disable_vblank(struct drm_crtc *crtc)
  2092. {
  2093. dm_set_vblank(crtc, false);
  2094. }
  2095. /* Implemented only the options currently availible for the driver */
  2096. static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = {
  2097. .reset = dm_crtc_reset_state,
  2098. .destroy = amdgpu_dm_crtc_destroy,
  2099. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  2100. .set_config = drm_atomic_helper_set_config,
  2101. .page_flip = drm_atomic_helper_page_flip,
  2102. .atomic_duplicate_state = dm_crtc_duplicate_state,
  2103. .atomic_destroy_state = dm_crtc_destroy_state,
  2104. .set_crc_source = amdgpu_dm_crtc_set_crc_source,
  2105. .enable_vblank = dm_enable_vblank,
  2106. .disable_vblank = dm_disable_vblank,
  2107. };
  2108. static enum drm_connector_status
  2109. amdgpu_dm_connector_detect(struct drm_connector *connector, bool force)
  2110. {
  2111. bool connected;
  2112. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  2113. /* Notes:
  2114. * 1. This interface is NOT called in context of HPD irq.
  2115. * 2. This interface *is called* in context of user-mode ioctl. Which
  2116. * makes it a bad place for *any* MST-related activit. */
  2117. if (aconnector->base.force == DRM_FORCE_UNSPECIFIED &&
  2118. !aconnector->fake_enable)
  2119. connected = (aconnector->dc_sink != NULL);
  2120. else
  2121. connected = (aconnector->base.force == DRM_FORCE_ON);
  2122. return (connected ? connector_status_connected :
  2123. connector_status_disconnected);
  2124. }
  2125. int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector,
  2126. struct drm_connector_state *connector_state,
  2127. struct drm_property *property,
  2128. uint64_t val)
  2129. {
  2130. struct drm_device *dev = connector->dev;
  2131. struct amdgpu_device *adev = dev->dev_private;
  2132. struct dm_connector_state *dm_old_state =
  2133. to_dm_connector_state(connector->state);
  2134. struct dm_connector_state *dm_new_state =
  2135. to_dm_connector_state(connector_state);
  2136. int ret = -EINVAL;
  2137. if (property == dev->mode_config.scaling_mode_property) {
  2138. enum amdgpu_rmx_type rmx_type;
  2139. switch (val) {
  2140. case DRM_MODE_SCALE_CENTER:
  2141. rmx_type = RMX_CENTER;
  2142. break;
  2143. case DRM_MODE_SCALE_ASPECT:
  2144. rmx_type = RMX_ASPECT;
  2145. break;
  2146. case DRM_MODE_SCALE_FULLSCREEN:
  2147. rmx_type = RMX_FULL;
  2148. break;
  2149. case DRM_MODE_SCALE_NONE:
  2150. default:
  2151. rmx_type = RMX_OFF;
  2152. break;
  2153. }
  2154. if (dm_old_state->scaling == rmx_type)
  2155. return 0;
  2156. dm_new_state->scaling = rmx_type;
  2157. ret = 0;
  2158. } else if (property == adev->mode_info.underscan_hborder_property) {
  2159. dm_new_state->underscan_hborder = val;
  2160. ret = 0;
  2161. } else if (property == adev->mode_info.underscan_vborder_property) {
  2162. dm_new_state->underscan_vborder = val;
  2163. ret = 0;
  2164. } else if (property == adev->mode_info.underscan_property) {
  2165. dm_new_state->underscan_enable = val;
  2166. ret = 0;
  2167. }
  2168. return ret;
  2169. }
  2170. int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector,
  2171. const struct drm_connector_state *state,
  2172. struct drm_property *property,
  2173. uint64_t *val)
  2174. {
  2175. struct drm_device *dev = connector->dev;
  2176. struct amdgpu_device *adev = dev->dev_private;
  2177. struct dm_connector_state *dm_state =
  2178. to_dm_connector_state(state);
  2179. int ret = -EINVAL;
  2180. if (property == dev->mode_config.scaling_mode_property) {
  2181. switch (dm_state->scaling) {
  2182. case RMX_CENTER:
  2183. *val = DRM_MODE_SCALE_CENTER;
  2184. break;
  2185. case RMX_ASPECT:
  2186. *val = DRM_MODE_SCALE_ASPECT;
  2187. break;
  2188. case RMX_FULL:
  2189. *val = DRM_MODE_SCALE_FULLSCREEN;
  2190. break;
  2191. case RMX_OFF:
  2192. default:
  2193. *val = DRM_MODE_SCALE_NONE;
  2194. break;
  2195. }
  2196. ret = 0;
  2197. } else if (property == adev->mode_info.underscan_hborder_property) {
  2198. *val = dm_state->underscan_hborder;
  2199. ret = 0;
  2200. } else if (property == adev->mode_info.underscan_vborder_property) {
  2201. *val = dm_state->underscan_vborder;
  2202. ret = 0;
  2203. } else if (property == adev->mode_info.underscan_property) {
  2204. *val = dm_state->underscan_enable;
  2205. ret = 0;
  2206. }
  2207. return ret;
  2208. }
  2209. static void amdgpu_dm_connector_destroy(struct drm_connector *connector)
  2210. {
  2211. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  2212. const struct dc_link *link = aconnector->dc_link;
  2213. struct amdgpu_device *adev = connector->dev->dev_private;
  2214. struct amdgpu_display_manager *dm = &adev->dm;
  2215. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\
  2216. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  2217. if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) &&
  2218. link->type != dc_connection_none) {
  2219. amdgpu_dm_register_backlight_device(dm);
  2220. if (dm->backlight_dev) {
  2221. backlight_device_unregister(dm->backlight_dev);
  2222. dm->backlight_dev = NULL;
  2223. }
  2224. }
  2225. #endif
  2226. drm_connector_unregister(connector);
  2227. drm_connector_cleanup(connector);
  2228. kfree(connector);
  2229. }
  2230. void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector)
  2231. {
  2232. struct dm_connector_state *state =
  2233. to_dm_connector_state(connector->state);
  2234. kfree(state);
  2235. state = kzalloc(sizeof(*state), GFP_KERNEL);
  2236. if (state) {
  2237. state->scaling = RMX_OFF;
  2238. state->underscan_enable = false;
  2239. state->underscan_hborder = 0;
  2240. state->underscan_vborder = 0;
  2241. connector->state = &state->base;
  2242. connector->state->connector = connector;
  2243. }
  2244. }
  2245. struct drm_connector_state *
  2246. amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector)
  2247. {
  2248. struct dm_connector_state *state =
  2249. to_dm_connector_state(connector->state);
  2250. struct dm_connector_state *new_state =
  2251. kmemdup(state, sizeof(*state), GFP_KERNEL);
  2252. if (new_state) {
  2253. __drm_atomic_helper_connector_duplicate_state(connector,
  2254. &new_state->base);
  2255. return &new_state->base;
  2256. }
  2257. return NULL;
  2258. }
  2259. static const struct drm_connector_funcs amdgpu_dm_connector_funcs = {
  2260. .reset = amdgpu_dm_connector_funcs_reset,
  2261. .detect = amdgpu_dm_connector_detect,
  2262. .fill_modes = drm_helper_probe_single_connector_modes,
  2263. .destroy = amdgpu_dm_connector_destroy,
  2264. .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
  2265. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  2266. .atomic_set_property = amdgpu_dm_connector_atomic_set_property,
  2267. .atomic_get_property = amdgpu_dm_connector_atomic_get_property
  2268. };
  2269. static struct drm_encoder *best_encoder(struct drm_connector *connector)
  2270. {
  2271. int enc_id = connector->encoder_ids[0];
  2272. struct drm_mode_object *obj;
  2273. struct drm_encoder *encoder;
  2274. DRM_DEBUG_DRIVER("Finding the best encoder\n");
  2275. /* pick the encoder ids */
  2276. if (enc_id) {
  2277. obj = drm_mode_object_find(connector->dev, NULL, enc_id, DRM_MODE_OBJECT_ENCODER);
  2278. if (!obj) {
  2279. DRM_ERROR("Couldn't find a matching encoder for our connector\n");
  2280. return NULL;
  2281. }
  2282. encoder = obj_to_encoder(obj);
  2283. return encoder;
  2284. }
  2285. DRM_ERROR("No encoder id\n");
  2286. return NULL;
  2287. }
  2288. static int get_modes(struct drm_connector *connector)
  2289. {
  2290. return amdgpu_dm_connector_get_modes(connector);
  2291. }
  2292. static void create_eml_sink(struct amdgpu_dm_connector *aconnector)
  2293. {
  2294. struct dc_sink_init_data init_params = {
  2295. .link = aconnector->dc_link,
  2296. .sink_signal = SIGNAL_TYPE_VIRTUAL
  2297. };
  2298. struct edid *edid;
  2299. if (!aconnector->base.edid_blob_ptr) {
  2300. DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n",
  2301. aconnector->base.name);
  2302. aconnector->base.force = DRM_FORCE_OFF;
  2303. aconnector->base.override_edid = false;
  2304. return;
  2305. }
  2306. edid = (struct edid *) aconnector->base.edid_blob_ptr->data;
  2307. aconnector->edid = edid;
  2308. aconnector->dc_em_sink = dc_link_add_remote_sink(
  2309. aconnector->dc_link,
  2310. (uint8_t *)edid,
  2311. (edid->extensions + 1) * EDID_LENGTH,
  2312. &init_params);
  2313. if (aconnector->base.force == DRM_FORCE_ON)
  2314. aconnector->dc_sink = aconnector->dc_link->local_sink ?
  2315. aconnector->dc_link->local_sink :
  2316. aconnector->dc_em_sink;
  2317. }
  2318. static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector)
  2319. {
  2320. struct dc_link *link = (struct dc_link *)aconnector->dc_link;
  2321. /* In case of headless boot with force on for DP managed connector
  2322. * Those settings have to be != 0 to get initial modeset
  2323. */
  2324. if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) {
  2325. link->verified_link_cap.lane_count = LANE_COUNT_FOUR;
  2326. link->verified_link_cap.link_rate = LINK_RATE_HIGH2;
  2327. }
  2328. aconnector->base.override_edid = true;
  2329. create_eml_sink(aconnector);
  2330. }
  2331. int amdgpu_dm_connector_mode_valid(struct drm_connector *connector,
  2332. struct drm_display_mode *mode)
  2333. {
  2334. int result = MODE_ERROR;
  2335. struct dc_sink *dc_sink;
  2336. struct amdgpu_device *adev = connector->dev->dev_private;
  2337. /* TODO: Unhardcode stream count */
  2338. struct dc_stream_state *stream;
  2339. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  2340. enum dc_status dc_result = DC_OK;
  2341. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  2342. (mode->flags & DRM_MODE_FLAG_DBLSCAN))
  2343. return result;
  2344. /* Only run this the first time mode_valid is called to initilialize
  2345. * EDID mgmt
  2346. */
  2347. if (aconnector->base.force != DRM_FORCE_UNSPECIFIED &&
  2348. !aconnector->dc_em_sink)
  2349. handle_edid_mgmt(aconnector);
  2350. dc_sink = to_amdgpu_dm_connector(connector)->dc_sink;
  2351. if (dc_sink == NULL) {
  2352. DRM_ERROR("dc_sink is NULL!\n");
  2353. goto fail;
  2354. }
  2355. stream = create_stream_for_sink(aconnector, mode, NULL);
  2356. if (stream == NULL) {
  2357. DRM_ERROR("Failed to create stream for sink!\n");
  2358. goto fail;
  2359. }
  2360. dc_result = dc_validate_stream(adev->dm.dc, stream);
  2361. if (dc_result == DC_OK)
  2362. result = MODE_OK;
  2363. else
  2364. DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n",
  2365. mode->vdisplay,
  2366. mode->hdisplay,
  2367. mode->clock,
  2368. dc_result);
  2369. dc_stream_release(stream);
  2370. fail:
  2371. /* TODO: error handling*/
  2372. return result;
  2373. }
  2374. static const struct drm_connector_helper_funcs
  2375. amdgpu_dm_connector_helper_funcs = {
  2376. /*
  2377. * If hotplug a second bigger display in FB Con mode, bigger resolution
  2378. * modes will be filtered by drm_mode_validate_size(), and those modes
  2379. * is missing after user start lightdm. So we need to renew modes list.
  2380. * in get_modes call back, not just return the modes count
  2381. */
  2382. .get_modes = get_modes,
  2383. .mode_valid = amdgpu_dm_connector_mode_valid,
  2384. .best_encoder = best_encoder
  2385. };
  2386. static void dm_crtc_helper_disable(struct drm_crtc *crtc)
  2387. {
  2388. }
  2389. static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc,
  2390. struct drm_crtc_state *state)
  2391. {
  2392. struct amdgpu_device *adev = crtc->dev->dev_private;
  2393. struct dc *dc = adev->dm.dc;
  2394. struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state);
  2395. int ret = -EINVAL;
  2396. if (unlikely(!dm_crtc_state->stream &&
  2397. modeset_required(state, NULL, dm_crtc_state->stream))) {
  2398. WARN_ON(1);
  2399. return ret;
  2400. }
  2401. /* In some use cases, like reset, no stream is attached */
  2402. if (!dm_crtc_state->stream)
  2403. return 0;
  2404. if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK)
  2405. return 0;
  2406. return ret;
  2407. }
  2408. static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc,
  2409. const struct drm_display_mode *mode,
  2410. struct drm_display_mode *adjusted_mode)
  2411. {
  2412. return true;
  2413. }
  2414. static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = {
  2415. .disable = dm_crtc_helper_disable,
  2416. .atomic_check = dm_crtc_helper_atomic_check,
  2417. .mode_fixup = dm_crtc_helper_mode_fixup
  2418. };
  2419. static void dm_encoder_helper_disable(struct drm_encoder *encoder)
  2420. {
  2421. }
  2422. static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder,
  2423. struct drm_crtc_state *crtc_state,
  2424. struct drm_connector_state *conn_state)
  2425. {
  2426. return 0;
  2427. }
  2428. const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = {
  2429. .disable = dm_encoder_helper_disable,
  2430. .atomic_check = dm_encoder_helper_atomic_check
  2431. };
  2432. static void dm_drm_plane_reset(struct drm_plane *plane)
  2433. {
  2434. struct dm_plane_state *amdgpu_state = NULL;
  2435. if (plane->state)
  2436. plane->funcs->atomic_destroy_state(plane, plane->state);
  2437. amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL);
  2438. WARN_ON(amdgpu_state == NULL);
  2439. if (amdgpu_state) {
  2440. plane->state = &amdgpu_state->base;
  2441. plane->state->plane = plane;
  2442. plane->state->rotation = DRM_MODE_ROTATE_0;
  2443. }
  2444. }
  2445. static struct drm_plane_state *
  2446. dm_drm_plane_duplicate_state(struct drm_plane *plane)
  2447. {
  2448. struct dm_plane_state *dm_plane_state, *old_dm_plane_state;
  2449. old_dm_plane_state = to_dm_plane_state(plane->state);
  2450. dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL);
  2451. if (!dm_plane_state)
  2452. return NULL;
  2453. __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base);
  2454. if (old_dm_plane_state->dc_state) {
  2455. dm_plane_state->dc_state = old_dm_plane_state->dc_state;
  2456. dc_plane_state_retain(dm_plane_state->dc_state);
  2457. }
  2458. return &dm_plane_state->base;
  2459. }
  2460. void dm_drm_plane_destroy_state(struct drm_plane *plane,
  2461. struct drm_plane_state *state)
  2462. {
  2463. struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
  2464. if (dm_plane_state->dc_state)
  2465. dc_plane_state_release(dm_plane_state->dc_state);
  2466. drm_atomic_helper_plane_destroy_state(plane, state);
  2467. }
  2468. static const struct drm_plane_funcs dm_plane_funcs = {
  2469. .update_plane = drm_atomic_helper_update_plane,
  2470. .disable_plane = drm_atomic_helper_disable_plane,
  2471. .destroy = drm_plane_cleanup,
  2472. .reset = dm_drm_plane_reset,
  2473. .atomic_duplicate_state = dm_drm_plane_duplicate_state,
  2474. .atomic_destroy_state = dm_drm_plane_destroy_state,
  2475. };
  2476. static int dm_plane_helper_prepare_fb(struct drm_plane *plane,
  2477. struct drm_plane_state *new_state)
  2478. {
  2479. struct amdgpu_framebuffer *afb;
  2480. struct drm_gem_object *obj;
  2481. struct amdgpu_device *adev;
  2482. struct amdgpu_bo *rbo;
  2483. uint64_t chroma_addr = 0;
  2484. struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old;
  2485. unsigned int awidth;
  2486. uint32_t domain;
  2487. int r;
  2488. dm_plane_state_old = to_dm_plane_state(plane->state);
  2489. dm_plane_state_new = to_dm_plane_state(new_state);
  2490. if (!new_state->fb) {
  2491. DRM_DEBUG_DRIVER("No FB bound\n");
  2492. return 0;
  2493. }
  2494. afb = to_amdgpu_framebuffer(new_state->fb);
  2495. obj = afb->obj;
  2496. rbo = gem_to_amdgpu_bo(obj);
  2497. adev = amdgpu_ttm_adev(rbo->tbo.bdev);
  2498. r = amdgpu_bo_reserve(rbo, false);
  2499. if (unlikely(r != 0))
  2500. return r;
  2501. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  2502. domain = amdgpu_display_framebuffer_domains(adev);
  2503. else
  2504. domain = AMDGPU_GEM_DOMAIN_VRAM;
  2505. r = amdgpu_bo_pin(rbo, domain, &afb->address);
  2506. amdgpu_bo_unreserve(rbo);
  2507. if (unlikely(r != 0)) {
  2508. if (r != -ERESTARTSYS)
  2509. DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
  2510. return r;
  2511. }
  2512. amdgpu_bo_ref(rbo);
  2513. if (dm_plane_state_new->dc_state &&
  2514. dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) {
  2515. struct dc_plane_state *plane_state = dm_plane_state_new->dc_state;
  2516. if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) {
  2517. plane_state->address.grph.addr.low_part = lower_32_bits(afb->address);
  2518. plane_state->address.grph.addr.high_part = upper_32_bits(afb->address);
  2519. } else {
  2520. awidth = ALIGN(new_state->fb->width, 64);
  2521. plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE;
  2522. plane_state->address.video_progressive.luma_addr.low_part
  2523. = lower_32_bits(afb->address);
  2524. plane_state->address.video_progressive.luma_addr.high_part
  2525. = upper_32_bits(afb->address);
  2526. chroma_addr = afb->address + (u64)awidth * new_state->fb->height;
  2527. plane_state->address.video_progressive.chroma_addr.low_part
  2528. = lower_32_bits(chroma_addr);
  2529. plane_state->address.video_progressive.chroma_addr.high_part
  2530. = upper_32_bits(chroma_addr);
  2531. }
  2532. }
  2533. /* It's a hack for s3 since in 4.9 kernel filter out cursor buffer
  2534. * prepare and cleanup in drm_atomic_helper_prepare_planes
  2535. * and drm_atomic_helper_cleanup_planes because fb doens't in s3.
  2536. * IN 4.10 kernel this code should be removed and amdgpu_device_suspend
  2537. * code touching fram buffers should be avoided for DC.
  2538. */
  2539. if (plane->type == DRM_PLANE_TYPE_CURSOR) {
  2540. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(new_state->crtc);
  2541. acrtc->cursor_bo = obj;
  2542. }
  2543. return 0;
  2544. }
  2545. static void dm_plane_helper_cleanup_fb(struct drm_plane *plane,
  2546. struct drm_plane_state *old_state)
  2547. {
  2548. struct amdgpu_bo *rbo;
  2549. struct amdgpu_framebuffer *afb;
  2550. int r;
  2551. if (!old_state->fb)
  2552. return;
  2553. afb = to_amdgpu_framebuffer(old_state->fb);
  2554. rbo = gem_to_amdgpu_bo(afb->obj);
  2555. r = amdgpu_bo_reserve(rbo, false);
  2556. if (unlikely(r)) {
  2557. DRM_ERROR("failed to reserve rbo before unpin\n");
  2558. return;
  2559. }
  2560. amdgpu_bo_unpin(rbo);
  2561. amdgpu_bo_unreserve(rbo);
  2562. amdgpu_bo_unref(&rbo);
  2563. }
  2564. static int dm_plane_atomic_check(struct drm_plane *plane,
  2565. struct drm_plane_state *state)
  2566. {
  2567. struct amdgpu_device *adev = plane->dev->dev_private;
  2568. struct dc *dc = adev->dm.dc;
  2569. struct dm_plane_state *dm_plane_state = to_dm_plane_state(state);
  2570. if (!dm_plane_state->dc_state)
  2571. return 0;
  2572. if (!fill_rects_from_plane_state(state, dm_plane_state->dc_state))
  2573. return -EINVAL;
  2574. if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK)
  2575. return 0;
  2576. return -EINVAL;
  2577. }
  2578. static const struct drm_plane_helper_funcs dm_plane_helper_funcs = {
  2579. .prepare_fb = dm_plane_helper_prepare_fb,
  2580. .cleanup_fb = dm_plane_helper_cleanup_fb,
  2581. .atomic_check = dm_plane_atomic_check,
  2582. };
  2583. /*
  2584. * TODO: these are currently initialized to rgb formats only.
  2585. * For future use cases we should either initialize them dynamically based on
  2586. * plane capabilities, or initialize this array to all formats, so internal drm
  2587. * check will succeed, and let DC to implement proper check
  2588. */
  2589. static const uint32_t rgb_formats[] = {
  2590. DRM_FORMAT_RGB888,
  2591. DRM_FORMAT_XRGB8888,
  2592. DRM_FORMAT_ARGB8888,
  2593. DRM_FORMAT_RGBA8888,
  2594. DRM_FORMAT_XRGB2101010,
  2595. DRM_FORMAT_XBGR2101010,
  2596. DRM_FORMAT_ARGB2101010,
  2597. DRM_FORMAT_ABGR2101010,
  2598. };
  2599. static const uint32_t yuv_formats[] = {
  2600. DRM_FORMAT_NV12,
  2601. DRM_FORMAT_NV21,
  2602. };
  2603. static const u32 cursor_formats[] = {
  2604. DRM_FORMAT_ARGB8888
  2605. };
  2606. static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm,
  2607. struct amdgpu_plane *aplane,
  2608. unsigned long possible_crtcs)
  2609. {
  2610. int res = -EPERM;
  2611. switch (aplane->base.type) {
  2612. case DRM_PLANE_TYPE_PRIMARY:
  2613. res = drm_universal_plane_init(
  2614. dm->adev->ddev,
  2615. &aplane->base,
  2616. possible_crtcs,
  2617. &dm_plane_funcs,
  2618. rgb_formats,
  2619. ARRAY_SIZE(rgb_formats),
  2620. NULL, aplane->base.type, NULL);
  2621. break;
  2622. case DRM_PLANE_TYPE_OVERLAY:
  2623. res = drm_universal_plane_init(
  2624. dm->adev->ddev,
  2625. &aplane->base,
  2626. possible_crtcs,
  2627. &dm_plane_funcs,
  2628. yuv_formats,
  2629. ARRAY_SIZE(yuv_formats),
  2630. NULL, aplane->base.type, NULL);
  2631. break;
  2632. case DRM_PLANE_TYPE_CURSOR:
  2633. res = drm_universal_plane_init(
  2634. dm->adev->ddev,
  2635. &aplane->base,
  2636. possible_crtcs,
  2637. &dm_plane_funcs,
  2638. cursor_formats,
  2639. ARRAY_SIZE(cursor_formats),
  2640. NULL, aplane->base.type, NULL);
  2641. break;
  2642. }
  2643. drm_plane_helper_add(&aplane->base, &dm_plane_helper_funcs);
  2644. /* Create (reset) the plane state */
  2645. if (aplane->base.funcs->reset)
  2646. aplane->base.funcs->reset(&aplane->base);
  2647. return res;
  2648. }
  2649. static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm,
  2650. struct drm_plane *plane,
  2651. uint32_t crtc_index)
  2652. {
  2653. struct amdgpu_crtc *acrtc = NULL;
  2654. struct amdgpu_plane *cursor_plane;
  2655. int res = -ENOMEM;
  2656. cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL);
  2657. if (!cursor_plane)
  2658. goto fail;
  2659. cursor_plane->base.type = DRM_PLANE_TYPE_CURSOR;
  2660. res = amdgpu_dm_plane_init(dm, cursor_plane, 0);
  2661. acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL);
  2662. if (!acrtc)
  2663. goto fail;
  2664. res = drm_crtc_init_with_planes(
  2665. dm->ddev,
  2666. &acrtc->base,
  2667. plane,
  2668. &cursor_plane->base,
  2669. &amdgpu_dm_crtc_funcs, NULL);
  2670. if (res)
  2671. goto fail;
  2672. drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs);
  2673. /* Create (reset) the plane state */
  2674. if (acrtc->base.funcs->reset)
  2675. acrtc->base.funcs->reset(&acrtc->base);
  2676. acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size;
  2677. acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size;
  2678. acrtc->crtc_id = crtc_index;
  2679. acrtc->base.enabled = false;
  2680. dm->adev->mode_info.crtcs[crtc_index] = acrtc;
  2681. drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES,
  2682. true, MAX_COLOR_LUT_ENTRIES);
  2683. drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES);
  2684. return 0;
  2685. fail:
  2686. kfree(acrtc);
  2687. kfree(cursor_plane);
  2688. return res;
  2689. }
  2690. static int to_drm_connector_type(enum signal_type st)
  2691. {
  2692. switch (st) {
  2693. case SIGNAL_TYPE_HDMI_TYPE_A:
  2694. return DRM_MODE_CONNECTOR_HDMIA;
  2695. case SIGNAL_TYPE_EDP:
  2696. return DRM_MODE_CONNECTOR_eDP;
  2697. case SIGNAL_TYPE_RGB:
  2698. return DRM_MODE_CONNECTOR_VGA;
  2699. case SIGNAL_TYPE_DISPLAY_PORT:
  2700. case SIGNAL_TYPE_DISPLAY_PORT_MST:
  2701. return DRM_MODE_CONNECTOR_DisplayPort;
  2702. case SIGNAL_TYPE_DVI_DUAL_LINK:
  2703. case SIGNAL_TYPE_DVI_SINGLE_LINK:
  2704. return DRM_MODE_CONNECTOR_DVID;
  2705. case SIGNAL_TYPE_VIRTUAL:
  2706. return DRM_MODE_CONNECTOR_VIRTUAL;
  2707. default:
  2708. return DRM_MODE_CONNECTOR_Unknown;
  2709. }
  2710. }
  2711. static void amdgpu_dm_get_native_mode(struct drm_connector *connector)
  2712. {
  2713. const struct drm_connector_helper_funcs *helper =
  2714. connector->helper_private;
  2715. struct drm_encoder *encoder;
  2716. struct amdgpu_encoder *amdgpu_encoder;
  2717. encoder = helper->best_encoder(connector);
  2718. if (encoder == NULL)
  2719. return;
  2720. amdgpu_encoder = to_amdgpu_encoder(encoder);
  2721. amdgpu_encoder->native_mode.clock = 0;
  2722. if (!list_empty(&connector->probed_modes)) {
  2723. struct drm_display_mode *preferred_mode = NULL;
  2724. list_for_each_entry(preferred_mode,
  2725. &connector->probed_modes,
  2726. head) {
  2727. if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED)
  2728. amdgpu_encoder->native_mode = *preferred_mode;
  2729. break;
  2730. }
  2731. }
  2732. }
  2733. static struct drm_display_mode *
  2734. amdgpu_dm_create_common_mode(struct drm_encoder *encoder,
  2735. char *name,
  2736. int hdisplay, int vdisplay)
  2737. {
  2738. struct drm_device *dev = encoder->dev;
  2739. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2740. struct drm_display_mode *mode = NULL;
  2741. struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
  2742. mode = drm_mode_duplicate(dev, native_mode);
  2743. if (mode == NULL)
  2744. return NULL;
  2745. mode->hdisplay = hdisplay;
  2746. mode->vdisplay = vdisplay;
  2747. mode->type &= ~DRM_MODE_TYPE_PREFERRED;
  2748. strncpy(mode->name, name, DRM_DISPLAY_MODE_LEN);
  2749. return mode;
  2750. }
  2751. static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder,
  2752. struct drm_connector *connector)
  2753. {
  2754. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  2755. struct drm_display_mode *mode = NULL;
  2756. struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode;
  2757. struct amdgpu_dm_connector *amdgpu_dm_connector =
  2758. to_amdgpu_dm_connector(connector);
  2759. int i;
  2760. int n;
  2761. struct mode_size {
  2762. char name[DRM_DISPLAY_MODE_LEN];
  2763. int w;
  2764. int h;
  2765. } common_modes[] = {
  2766. { "640x480", 640, 480},
  2767. { "800x600", 800, 600},
  2768. { "1024x768", 1024, 768},
  2769. { "1280x720", 1280, 720},
  2770. { "1280x800", 1280, 800},
  2771. {"1280x1024", 1280, 1024},
  2772. { "1440x900", 1440, 900},
  2773. {"1680x1050", 1680, 1050},
  2774. {"1600x1200", 1600, 1200},
  2775. {"1920x1080", 1920, 1080},
  2776. {"1920x1200", 1920, 1200}
  2777. };
  2778. n = ARRAY_SIZE(common_modes);
  2779. for (i = 0; i < n; i++) {
  2780. struct drm_display_mode *curmode = NULL;
  2781. bool mode_existed = false;
  2782. if (common_modes[i].w > native_mode->hdisplay ||
  2783. common_modes[i].h > native_mode->vdisplay ||
  2784. (common_modes[i].w == native_mode->hdisplay &&
  2785. common_modes[i].h == native_mode->vdisplay))
  2786. continue;
  2787. list_for_each_entry(curmode, &connector->probed_modes, head) {
  2788. if (common_modes[i].w == curmode->hdisplay &&
  2789. common_modes[i].h == curmode->vdisplay) {
  2790. mode_existed = true;
  2791. break;
  2792. }
  2793. }
  2794. if (mode_existed)
  2795. continue;
  2796. mode = amdgpu_dm_create_common_mode(encoder,
  2797. common_modes[i].name, common_modes[i].w,
  2798. common_modes[i].h);
  2799. drm_mode_probed_add(connector, mode);
  2800. amdgpu_dm_connector->num_modes++;
  2801. }
  2802. }
  2803. static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector,
  2804. struct edid *edid)
  2805. {
  2806. struct amdgpu_dm_connector *amdgpu_dm_connector =
  2807. to_amdgpu_dm_connector(connector);
  2808. if (edid) {
  2809. /* empty probed_modes */
  2810. INIT_LIST_HEAD(&connector->probed_modes);
  2811. amdgpu_dm_connector->num_modes =
  2812. drm_add_edid_modes(connector, edid);
  2813. amdgpu_dm_get_native_mode(connector);
  2814. } else {
  2815. amdgpu_dm_connector->num_modes = 0;
  2816. }
  2817. }
  2818. static int amdgpu_dm_connector_get_modes(struct drm_connector *connector)
  2819. {
  2820. const struct drm_connector_helper_funcs *helper =
  2821. connector->helper_private;
  2822. struct amdgpu_dm_connector *amdgpu_dm_connector =
  2823. to_amdgpu_dm_connector(connector);
  2824. struct drm_encoder *encoder;
  2825. struct edid *edid = amdgpu_dm_connector->edid;
  2826. encoder = helper->best_encoder(connector);
  2827. amdgpu_dm_connector_ddc_get_modes(connector, edid);
  2828. amdgpu_dm_connector_add_common_modes(encoder, connector);
  2829. #if defined(CONFIG_DRM_AMD_DC_FBC)
  2830. amdgpu_dm_fbc_init(connector);
  2831. #endif
  2832. return amdgpu_dm_connector->num_modes;
  2833. }
  2834. void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm,
  2835. struct amdgpu_dm_connector *aconnector,
  2836. int connector_type,
  2837. struct dc_link *link,
  2838. int link_index)
  2839. {
  2840. struct amdgpu_device *adev = dm->ddev->dev_private;
  2841. aconnector->connector_id = link_index;
  2842. aconnector->dc_link = link;
  2843. aconnector->base.interlace_allowed = false;
  2844. aconnector->base.doublescan_allowed = false;
  2845. aconnector->base.stereo_allowed = false;
  2846. aconnector->base.dpms = DRM_MODE_DPMS_OFF;
  2847. aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */
  2848. mutex_init(&aconnector->hpd_lock);
  2849. /* configure support HPD hot plug connector_>polled default value is 0
  2850. * which means HPD hot plug not supported
  2851. */
  2852. switch (connector_type) {
  2853. case DRM_MODE_CONNECTOR_HDMIA:
  2854. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  2855. break;
  2856. case DRM_MODE_CONNECTOR_DisplayPort:
  2857. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  2858. break;
  2859. case DRM_MODE_CONNECTOR_DVID:
  2860. aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
  2861. break;
  2862. default:
  2863. break;
  2864. }
  2865. drm_object_attach_property(&aconnector->base.base,
  2866. dm->ddev->mode_config.scaling_mode_property,
  2867. DRM_MODE_SCALE_NONE);
  2868. drm_object_attach_property(&aconnector->base.base,
  2869. adev->mode_info.underscan_property,
  2870. UNDERSCAN_OFF);
  2871. drm_object_attach_property(&aconnector->base.base,
  2872. adev->mode_info.underscan_hborder_property,
  2873. 0);
  2874. drm_object_attach_property(&aconnector->base.base,
  2875. adev->mode_info.underscan_vborder_property,
  2876. 0);
  2877. }
  2878. static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap,
  2879. struct i2c_msg *msgs, int num)
  2880. {
  2881. struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap);
  2882. struct ddc_service *ddc_service = i2c->ddc_service;
  2883. struct i2c_command cmd;
  2884. int i;
  2885. int result = -EIO;
  2886. cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL);
  2887. if (!cmd.payloads)
  2888. return result;
  2889. cmd.number_of_payloads = num;
  2890. cmd.engine = I2C_COMMAND_ENGINE_DEFAULT;
  2891. cmd.speed = 100;
  2892. for (i = 0; i < num; i++) {
  2893. cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD);
  2894. cmd.payloads[i].address = msgs[i].addr;
  2895. cmd.payloads[i].length = msgs[i].len;
  2896. cmd.payloads[i].data = msgs[i].buf;
  2897. }
  2898. if (dal_i2caux_submit_i2c_command(
  2899. ddc_service->ctx->i2caux,
  2900. ddc_service->ddc_pin,
  2901. &cmd))
  2902. result = num;
  2903. kfree(cmd.payloads);
  2904. return result;
  2905. }
  2906. static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap)
  2907. {
  2908. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  2909. }
  2910. static const struct i2c_algorithm amdgpu_dm_i2c_algo = {
  2911. .master_xfer = amdgpu_dm_i2c_xfer,
  2912. .functionality = amdgpu_dm_i2c_func,
  2913. };
  2914. static struct amdgpu_i2c_adapter *
  2915. create_i2c(struct ddc_service *ddc_service,
  2916. int link_index,
  2917. int *res)
  2918. {
  2919. struct amdgpu_device *adev = ddc_service->ctx->driver_context;
  2920. struct amdgpu_i2c_adapter *i2c;
  2921. i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL);
  2922. if (!i2c)
  2923. return NULL;
  2924. i2c->base.owner = THIS_MODULE;
  2925. i2c->base.class = I2C_CLASS_DDC;
  2926. i2c->base.dev.parent = &adev->pdev->dev;
  2927. i2c->base.algo = &amdgpu_dm_i2c_algo;
  2928. snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
  2929. i2c_set_adapdata(&i2c->base, i2c);
  2930. i2c->ddc_service = ddc_service;
  2931. return i2c;
  2932. }
  2933. /* Note: this function assumes that dc_link_detect() was called for the
  2934. * dc_link which will be represented by this aconnector.
  2935. */
  2936. static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm,
  2937. struct amdgpu_dm_connector *aconnector,
  2938. uint32_t link_index,
  2939. struct amdgpu_encoder *aencoder)
  2940. {
  2941. int res = 0;
  2942. int connector_type;
  2943. struct dc *dc = dm->dc;
  2944. struct dc_link *link = dc_get_link_at_index(dc, link_index);
  2945. struct amdgpu_i2c_adapter *i2c;
  2946. link->priv = aconnector;
  2947. DRM_DEBUG_DRIVER("%s()\n", __func__);
  2948. i2c = create_i2c(link->ddc, link->link_index, &res);
  2949. if (!i2c) {
  2950. DRM_ERROR("Failed to create i2c adapter data\n");
  2951. return -ENOMEM;
  2952. }
  2953. aconnector->i2c = i2c;
  2954. res = i2c_add_adapter(&i2c->base);
  2955. if (res) {
  2956. DRM_ERROR("Failed to register hw i2c %d\n", link->link_index);
  2957. goto out_free;
  2958. }
  2959. connector_type = to_drm_connector_type(link->connector_signal);
  2960. res = drm_connector_init(
  2961. dm->ddev,
  2962. &aconnector->base,
  2963. &amdgpu_dm_connector_funcs,
  2964. connector_type);
  2965. if (res) {
  2966. DRM_ERROR("connector_init failed\n");
  2967. aconnector->connector_id = -1;
  2968. goto out_free;
  2969. }
  2970. drm_connector_helper_add(
  2971. &aconnector->base,
  2972. &amdgpu_dm_connector_helper_funcs);
  2973. if (aconnector->base.funcs->reset)
  2974. aconnector->base.funcs->reset(&aconnector->base);
  2975. amdgpu_dm_connector_init_helper(
  2976. dm,
  2977. aconnector,
  2978. connector_type,
  2979. link,
  2980. link_index);
  2981. drm_mode_connector_attach_encoder(
  2982. &aconnector->base, &aencoder->base);
  2983. drm_connector_register(&aconnector->base);
  2984. if (connector_type == DRM_MODE_CONNECTOR_DisplayPort
  2985. || connector_type == DRM_MODE_CONNECTOR_eDP)
  2986. amdgpu_dm_initialize_dp_connector(dm, aconnector);
  2987. out_free:
  2988. if (res) {
  2989. kfree(i2c);
  2990. aconnector->i2c = NULL;
  2991. }
  2992. return res;
  2993. }
  2994. int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev)
  2995. {
  2996. switch (adev->mode_info.num_crtc) {
  2997. case 1:
  2998. return 0x1;
  2999. case 2:
  3000. return 0x3;
  3001. case 3:
  3002. return 0x7;
  3003. case 4:
  3004. return 0xf;
  3005. case 5:
  3006. return 0x1f;
  3007. case 6:
  3008. default:
  3009. return 0x3f;
  3010. }
  3011. }
  3012. static int amdgpu_dm_encoder_init(struct drm_device *dev,
  3013. struct amdgpu_encoder *aencoder,
  3014. uint32_t link_index)
  3015. {
  3016. struct amdgpu_device *adev = dev->dev_private;
  3017. int res = drm_encoder_init(dev,
  3018. &aencoder->base,
  3019. &amdgpu_dm_encoder_funcs,
  3020. DRM_MODE_ENCODER_TMDS,
  3021. NULL);
  3022. aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev);
  3023. if (!res)
  3024. aencoder->encoder_id = link_index;
  3025. else
  3026. aencoder->encoder_id = -1;
  3027. drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs);
  3028. return res;
  3029. }
  3030. static void manage_dm_interrupts(struct amdgpu_device *adev,
  3031. struct amdgpu_crtc *acrtc,
  3032. bool enable)
  3033. {
  3034. /*
  3035. * this is not correct translation but will work as soon as VBLANK
  3036. * constant is the same as PFLIP
  3037. */
  3038. int irq_type =
  3039. amdgpu_display_crtc_idx_to_irq_type(
  3040. adev,
  3041. acrtc->crtc_id);
  3042. if (enable) {
  3043. drm_crtc_vblank_on(&acrtc->base);
  3044. amdgpu_irq_get(
  3045. adev,
  3046. &adev->pageflip_irq,
  3047. irq_type);
  3048. } else {
  3049. amdgpu_irq_put(
  3050. adev,
  3051. &adev->pageflip_irq,
  3052. irq_type);
  3053. drm_crtc_vblank_off(&acrtc->base);
  3054. }
  3055. }
  3056. static bool
  3057. is_scaling_state_different(const struct dm_connector_state *dm_state,
  3058. const struct dm_connector_state *old_dm_state)
  3059. {
  3060. if (dm_state->scaling != old_dm_state->scaling)
  3061. return true;
  3062. if (!dm_state->underscan_enable && old_dm_state->underscan_enable) {
  3063. if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0)
  3064. return true;
  3065. } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) {
  3066. if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0)
  3067. return true;
  3068. } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder ||
  3069. dm_state->underscan_vborder != old_dm_state->underscan_vborder)
  3070. return true;
  3071. return false;
  3072. }
  3073. static void remove_stream(struct amdgpu_device *adev,
  3074. struct amdgpu_crtc *acrtc,
  3075. struct dc_stream_state *stream)
  3076. {
  3077. /* this is the update mode case */
  3078. if (adev->dm.freesync_module)
  3079. mod_freesync_remove_stream(adev->dm.freesync_module, stream);
  3080. acrtc->otg_inst = -1;
  3081. acrtc->enabled = false;
  3082. }
  3083. static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc,
  3084. struct dc_cursor_position *position)
  3085. {
  3086. struct amdgpu_crtc *amdgpu_crtc = amdgpu_crtc = to_amdgpu_crtc(crtc);
  3087. int x, y;
  3088. int xorigin = 0, yorigin = 0;
  3089. if (!crtc || !plane->state->fb) {
  3090. position->enable = false;
  3091. position->x = 0;
  3092. position->y = 0;
  3093. return 0;
  3094. }
  3095. if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) ||
  3096. (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) {
  3097. DRM_ERROR("%s: bad cursor width or height %d x %d\n",
  3098. __func__,
  3099. plane->state->crtc_w,
  3100. plane->state->crtc_h);
  3101. return -EINVAL;
  3102. }
  3103. x = plane->state->crtc_x;
  3104. y = plane->state->crtc_y;
  3105. /* avivo cursor are offset into the total surface */
  3106. x += crtc->primary->state->src_x >> 16;
  3107. y += crtc->primary->state->src_y >> 16;
  3108. if (x < 0) {
  3109. xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
  3110. x = 0;
  3111. }
  3112. if (y < 0) {
  3113. yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
  3114. y = 0;
  3115. }
  3116. position->enable = true;
  3117. position->x = x;
  3118. position->y = y;
  3119. position->x_hotspot = xorigin;
  3120. position->y_hotspot = yorigin;
  3121. return 0;
  3122. }
  3123. static void handle_cursor_update(struct drm_plane *plane,
  3124. struct drm_plane_state *old_plane_state)
  3125. {
  3126. struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb);
  3127. struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc;
  3128. struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL;
  3129. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  3130. uint64_t address = afb ? afb->address : 0;
  3131. struct dc_cursor_position position;
  3132. struct dc_cursor_attributes attributes;
  3133. int ret;
  3134. if (!plane->state->fb && !old_plane_state->fb)
  3135. return;
  3136. DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n",
  3137. __func__,
  3138. amdgpu_crtc->crtc_id,
  3139. plane->state->crtc_w,
  3140. plane->state->crtc_h);
  3141. ret = get_cursor_position(plane, crtc, &position);
  3142. if (ret)
  3143. return;
  3144. if (!position.enable) {
  3145. /* turn off cursor */
  3146. if (crtc_state && crtc_state->stream)
  3147. dc_stream_set_cursor_position(crtc_state->stream,
  3148. &position);
  3149. return;
  3150. }
  3151. amdgpu_crtc->cursor_width = plane->state->crtc_w;
  3152. amdgpu_crtc->cursor_height = plane->state->crtc_h;
  3153. attributes.address.high_part = upper_32_bits(address);
  3154. attributes.address.low_part = lower_32_bits(address);
  3155. attributes.width = plane->state->crtc_w;
  3156. attributes.height = plane->state->crtc_h;
  3157. attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA;
  3158. attributes.rotation_angle = 0;
  3159. attributes.attribute_flags.value = 0;
  3160. attributes.pitch = attributes.width;
  3161. if (crtc_state->stream) {
  3162. if (!dc_stream_set_cursor_attributes(crtc_state->stream,
  3163. &attributes))
  3164. DRM_ERROR("DC failed to set cursor attributes\n");
  3165. if (!dc_stream_set_cursor_position(crtc_state->stream,
  3166. &position))
  3167. DRM_ERROR("DC failed to set cursor position\n");
  3168. }
  3169. }
  3170. static void prepare_flip_isr(struct amdgpu_crtc *acrtc)
  3171. {
  3172. assert_spin_locked(&acrtc->base.dev->event_lock);
  3173. WARN_ON(acrtc->event);
  3174. acrtc->event = acrtc->base.state->event;
  3175. /* Set the flip status */
  3176. acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
  3177. /* Mark this event as consumed */
  3178. acrtc->base.state->event = NULL;
  3179. DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n",
  3180. acrtc->crtc_id);
  3181. }
  3182. /*
  3183. * Executes flip
  3184. *
  3185. * Waits on all BO's fences and for proper vblank count
  3186. */
  3187. static void amdgpu_dm_do_flip(struct drm_crtc *crtc,
  3188. struct drm_framebuffer *fb,
  3189. uint32_t target,
  3190. struct dc_state *state)
  3191. {
  3192. unsigned long flags;
  3193. uint32_t target_vblank;
  3194. int r, vpos, hpos;
  3195. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3196. struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb);
  3197. struct amdgpu_bo *abo = gem_to_amdgpu_bo(afb->obj);
  3198. struct amdgpu_device *adev = crtc->dev->dev_private;
  3199. bool async_flip = (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
  3200. struct dc_flip_addrs addr = { {0} };
  3201. /* TODO eliminate or rename surface_update */
  3202. struct dc_surface_update surface_updates[1] = { {0} };
  3203. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state);
  3204. /* Prepare wait for target vblank early - before the fence-waits */
  3205. target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) +
  3206. amdgpu_get_vblank_counter_kms(crtc->dev, acrtc->crtc_id);
  3207. /* TODO This might fail and hence better not used, wait
  3208. * explicitly on fences instead
  3209. * and in general should be called for
  3210. * blocking commit to as per framework helpers
  3211. */
  3212. r = amdgpu_bo_reserve(abo, true);
  3213. if (unlikely(r != 0)) {
  3214. DRM_ERROR("failed to reserve buffer before flip\n");
  3215. WARN_ON(1);
  3216. }
  3217. /* Wait for all fences on this FB */
  3218. WARN_ON(reservation_object_wait_timeout_rcu(abo->tbo.resv, true, false,
  3219. MAX_SCHEDULE_TIMEOUT) < 0);
  3220. amdgpu_bo_unreserve(abo);
  3221. /* Wait until we're out of the vertical blank period before the one
  3222. * targeted by the flip
  3223. */
  3224. while ((acrtc->enabled &&
  3225. (amdgpu_display_get_crtc_scanoutpos(adev->ddev, acrtc->crtc_id,
  3226. 0, &vpos, &hpos, NULL,
  3227. NULL, &crtc->hwmode)
  3228. & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
  3229. (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
  3230. (int)(target_vblank -
  3231. amdgpu_get_vblank_counter_kms(adev->ddev, acrtc->crtc_id)) > 0)) {
  3232. usleep_range(1000, 1100);
  3233. }
  3234. /* Flip */
  3235. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  3236. /* update crtc fb */
  3237. crtc->primary->fb = fb;
  3238. WARN_ON(acrtc->pflip_status != AMDGPU_FLIP_NONE);
  3239. WARN_ON(!acrtc_state->stream);
  3240. addr.address.grph.addr.low_part = lower_32_bits(afb->address);
  3241. addr.address.grph.addr.high_part = upper_32_bits(afb->address);
  3242. addr.flip_immediate = async_flip;
  3243. if (acrtc->base.state->event)
  3244. prepare_flip_isr(acrtc);
  3245. surface_updates->surface = dc_stream_get_status(acrtc_state->stream)->plane_states[0];
  3246. surface_updates->flip_addr = &addr;
  3247. dc_commit_updates_for_stream(adev->dm.dc,
  3248. surface_updates,
  3249. 1,
  3250. acrtc_state->stream,
  3251. NULL,
  3252. &surface_updates->surface,
  3253. state);
  3254. DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x \n",
  3255. __func__,
  3256. addr.address.grph.addr.high_part,
  3257. addr.address.grph.addr.low_part);
  3258. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  3259. }
  3260. static void amdgpu_dm_commit_planes(struct drm_atomic_state *state,
  3261. struct drm_device *dev,
  3262. struct amdgpu_display_manager *dm,
  3263. struct drm_crtc *pcrtc,
  3264. bool *wait_for_vblank)
  3265. {
  3266. uint32_t i;
  3267. struct drm_plane *plane;
  3268. struct drm_plane_state *old_plane_state, *new_plane_state;
  3269. struct dc_stream_state *dc_stream_attach;
  3270. struct dc_plane_state *plane_states_constructed[MAX_SURFACES];
  3271. struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc);
  3272. struct drm_crtc_state *new_pcrtc_state =
  3273. drm_atomic_get_new_crtc_state(state, pcrtc);
  3274. struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state);
  3275. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  3276. int planes_count = 0;
  3277. unsigned long flags;
  3278. /* update planes when needed */
  3279. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  3280. struct drm_crtc *crtc = new_plane_state->crtc;
  3281. struct drm_crtc_state *new_crtc_state;
  3282. struct drm_framebuffer *fb = new_plane_state->fb;
  3283. bool pflip_needed;
  3284. struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state);
  3285. if (plane->type == DRM_PLANE_TYPE_CURSOR) {
  3286. handle_cursor_update(plane, old_plane_state);
  3287. continue;
  3288. }
  3289. if (!fb || !crtc || pcrtc != crtc)
  3290. continue;
  3291. new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  3292. if (!new_crtc_state->active)
  3293. continue;
  3294. pflip_needed = !state->allow_modeset;
  3295. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  3296. if (acrtc_attach->pflip_status != AMDGPU_FLIP_NONE) {
  3297. DRM_ERROR("%s: acrtc %d, already busy\n",
  3298. __func__,
  3299. acrtc_attach->crtc_id);
  3300. /* In commit tail framework this cannot happen */
  3301. WARN_ON(1);
  3302. }
  3303. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  3304. if (!pflip_needed) {
  3305. WARN_ON(!dm_new_plane_state->dc_state);
  3306. plane_states_constructed[planes_count] = dm_new_plane_state->dc_state;
  3307. dc_stream_attach = acrtc_state->stream;
  3308. planes_count++;
  3309. } else if (new_crtc_state->planes_changed) {
  3310. /* Assume even ONE crtc with immediate flip means
  3311. * entire can't wait for VBLANK
  3312. * TODO Check if it's correct
  3313. */
  3314. *wait_for_vblank =
  3315. new_pcrtc_state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC ?
  3316. false : true;
  3317. /* TODO: Needs rework for multiplane flip */
  3318. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  3319. drm_crtc_vblank_get(crtc);
  3320. amdgpu_dm_do_flip(
  3321. crtc,
  3322. fb,
  3323. (uint32_t)drm_crtc_vblank_count(crtc) + *wait_for_vblank,
  3324. dm_state->context);
  3325. }
  3326. }
  3327. if (planes_count) {
  3328. unsigned long flags;
  3329. if (new_pcrtc_state->event) {
  3330. drm_crtc_vblank_get(pcrtc);
  3331. spin_lock_irqsave(&pcrtc->dev->event_lock, flags);
  3332. prepare_flip_isr(acrtc_attach);
  3333. spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags);
  3334. }
  3335. if (false == dc_commit_planes_to_stream(dm->dc,
  3336. plane_states_constructed,
  3337. planes_count,
  3338. dc_stream_attach,
  3339. dm_state->context))
  3340. dm_error("%s: Failed to attach plane!\n", __func__);
  3341. } else {
  3342. /*TODO BUG Here should go disable planes on CRTC. */
  3343. }
  3344. }
  3345. /**
  3346. * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC
  3347. * @crtc_state: the DRM CRTC state
  3348. * @stream_state: the DC stream state.
  3349. *
  3350. * Copy the mirrored transient state flags from DRM, to DC. It is used to bring
  3351. * a dc_stream_state's flags in sync with a drm_crtc_state's flags.
  3352. */
  3353. static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state,
  3354. struct dc_stream_state *stream_state)
  3355. {
  3356. stream_state->mode_changed = crtc_state->mode_changed;
  3357. }
  3358. static int amdgpu_dm_atomic_commit(struct drm_device *dev,
  3359. struct drm_atomic_state *state,
  3360. bool nonblock)
  3361. {
  3362. struct drm_crtc *crtc;
  3363. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  3364. struct amdgpu_device *adev = dev->dev_private;
  3365. int i;
  3366. /*
  3367. * We evade vblanks and pflips on crtc that
  3368. * should be changed. We do it here to flush & disable
  3369. * interrupts before drm_swap_state is called in drm_atomic_helper_commit
  3370. * it will update crtc->dm_crtc_state->stream pointer which is used in
  3371. * the ISRs.
  3372. */
  3373. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  3374. struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  3375. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3376. if (drm_atomic_crtc_needs_modeset(new_crtc_state) && dm_old_crtc_state->stream)
  3377. manage_dm_interrupts(adev, acrtc, false);
  3378. }
  3379. /* Add check here for SoC's that support hardware cursor plane, to
  3380. * unset legacy_cursor_update */
  3381. return drm_atomic_helper_commit(dev, state, nonblock);
  3382. /*TODO Handle EINTR, reenable IRQ*/
  3383. }
  3384. static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state)
  3385. {
  3386. struct drm_device *dev = state->dev;
  3387. struct amdgpu_device *adev = dev->dev_private;
  3388. struct amdgpu_display_manager *dm = &adev->dm;
  3389. struct dm_atomic_state *dm_state;
  3390. uint32_t i, j;
  3391. struct drm_crtc *crtc;
  3392. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  3393. unsigned long flags;
  3394. bool wait_for_vblank = true;
  3395. struct drm_connector *connector;
  3396. struct drm_connector_state *old_con_state, *new_con_state;
  3397. struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
  3398. drm_atomic_helper_update_legacy_modeset_state(dev, state);
  3399. dm_state = to_dm_atomic_state(state);
  3400. /* update changed items */
  3401. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  3402. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3403. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3404. dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  3405. DRM_DEBUG_DRIVER(
  3406. "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
  3407. "planes_changed:%d, mode_changed:%d,active_changed:%d,"
  3408. "connectors_changed:%d\n",
  3409. acrtc->crtc_id,
  3410. new_crtc_state->enable,
  3411. new_crtc_state->active,
  3412. new_crtc_state->planes_changed,
  3413. new_crtc_state->mode_changed,
  3414. new_crtc_state->active_changed,
  3415. new_crtc_state->connectors_changed);
  3416. /* Copy all transient state flags into dc state */
  3417. if (dm_new_crtc_state->stream) {
  3418. amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base,
  3419. dm_new_crtc_state->stream);
  3420. }
  3421. /* handles headless hotplug case, updating new_state and
  3422. * aconnector as needed
  3423. */
  3424. if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) {
  3425. DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc);
  3426. if (!dm_new_crtc_state->stream) {
  3427. /*
  3428. * this could happen because of issues with
  3429. * userspace notifications delivery.
  3430. * In this case userspace tries to set mode on
  3431. * display which is disconnect in fact.
  3432. * dc_sink in NULL in this case on aconnector.
  3433. * We expect reset mode will come soon.
  3434. *
  3435. * This can also happen when unplug is done
  3436. * during resume sequence ended
  3437. *
  3438. * In this case, we want to pretend we still
  3439. * have a sink to keep the pipe running so that
  3440. * hw state is consistent with the sw state
  3441. */
  3442. DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
  3443. __func__, acrtc->base.base.id);
  3444. continue;
  3445. }
  3446. if (dm_old_crtc_state->stream)
  3447. remove_stream(adev, acrtc, dm_old_crtc_state->stream);
  3448. acrtc->enabled = true;
  3449. acrtc->hw_mode = new_crtc_state->mode;
  3450. crtc->hwmode = new_crtc_state->mode;
  3451. } else if (modereset_required(new_crtc_state)) {
  3452. DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc);
  3453. /* i.e. reset mode */
  3454. if (dm_old_crtc_state->stream)
  3455. remove_stream(adev, acrtc, dm_old_crtc_state->stream);
  3456. }
  3457. } /* for_each_crtc_in_state() */
  3458. /*
  3459. * Add streams after required streams from new and replaced streams
  3460. * are removed from freesync module
  3461. */
  3462. if (adev->dm.freesync_module) {
  3463. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  3464. new_crtc_state, i) {
  3465. struct amdgpu_dm_connector *aconnector = NULL;
  3466. struct dm_connector_state *dm_new_con_state = NULL;
  3467. struct amdgpu_crtc *acrtc = NULL;
  3468. bool modeset_needed;
  3469. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3470. dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  3471. modeset_needed = modeset_required(
  3472. new_crtc_state,
  3473. dm_new_crtc_state->stream,
  3474. dm_old_crtc_state->stream);
  3475. /* We add stream to freesync if:
  3476. * 1. Said stream is not null, and
  3477. * 2. A modeset is requested. This means that the
  3478. * stream was removed previously, and needs to be
  3479. * replaced.
  3480. */
  3481. if (dm_new_crtc_state->stream == NULL ||
  3482. !modeset_needed)
  3483. continue;
  3484. acrtc = to_amdgpu_crtc(crtc);
  3485. aconnector =
  3486. amdgpu_dm_find_first_crtc_matching_connector(
  3487. state, crtc);
  3488. if (!aconnector) {
  3489. DRM_DEBUG_DRIVER("Atomic commit: Failed to "
  3490. "find connector for acrtc "
  3491. "id:%d skipping freesync "
  3492. "init\n",
  3493. acrtc->crtc_id);
  3494. continue;
  3495. }
  3496. mod_freesync_add_stream(adev->dm.freesync_module,
  3497. dm_new_crtc_state->stream,
  3498. &aconnector->caps);
  3499. new_con_state = drm_atomic_get_new_connector_state(
  3500. state, &aconnector->base);
  3501. dm_new_con_state = to_dm_connector_state(new_con_state);
  3502. mod_freesync_set_user_enable(adev->dm.freesync_module,
  3503. &dm_new_crtc_state->stream,
  3504. 1,
  3505. &dm_new_con_state->user_enable);
  3506. }
  3507. }
  3508. if (dm_state->context) {
  3509. dm_enable_per_frame_crtc_master_sync(dm_state->context);
  3510. WARN_ON(!dc_commit_state(dm->dc, dm_state->context));
  3511. }
  3512. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  3513. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3514. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3515. if (dm_new_crtc_state->stream != NULL) {
  3516. const struct dc_stream_status *status =
  3517. dc_stream_get_status(dm_new_crtc_state->stream);
  3518. if (!status)
  3519. DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc);
  3520. else
  3521. acrtc->otg_inst = status->primary_otg_inst;
  3522. }
  3523. }
  3524. /* Handle scaling and underscan changes*/
  3525. for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
  3526. struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
  3527. struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
  3528. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
  3529. struct dc_stream_status *status = NULL;
  3530. if (acrtc)
  3531. new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base);
  3532. /* Skip any modesets/resets */
  3533. if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state))
  3534. continue;
  3535. /* Skip any thing not scale or underscan changes */
  3536. if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
  3537. continue;
  3538. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3539. update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode,
  3540. dm_new_con_state, (struct dc_stream_state *)dm_new_crtc_state->stream);
  3541. if (!dm_new_crtc_state->stream)
  3542. continue;
  3543. status = dc_stream_get_status(dm_new_crtc_state->stream);
  3544. WARN_ON(!status);
  3545. WARN_ON(!status->plane_count);
  3546. /*TODO How it works with MPO ?*/
  3547. if (!dc_commit_planes_to_stream(
  3548. dm->dc,
  3549. status->plane_states,
  3550. status->plane_count,
  3551. dm_new_crtc_state->stream,
  3552. dm_state->context))
  3553. dm_error("%s: Failed to update stream scaling!\n", __func__);
  3554. }
  3555. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  3556. new_crtc_state, i) {
  3557. /*
  3558. * loop to enable interrupts on newly arrived crtc
  3559. */
  3560. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
  3561. bool modeset_needed;
  3562. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3563. dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  3564. modeset_needed = modeset_required(
  3565. new_crtc_state,
  3566. dm_new_crtc_state->stream,
  3567. dm_old_crtc_state->stream);
  3568. if (dm_new_crtc_state->stream == NULL || !modeset_needed)
  3569. continue;
  3570. if (adev->dm.freesync_module)
  3571. mod_freesync_notify_mode_change(
  3572. adev->dm.freesync_module,
  3573. &dm_new_crtc_state->stream, 1);
  3574. manage_dm_interrupts(adev, acrtc, true);
  3575. }
  3576. /* update planes when needed per crtc*/
  3577. for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) {
  3578. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3579. if (dm_new_crtc_state->stream)
  3580. amdgpu_dm_commit_planes(state, dev, dm, crtc, &wait_for_vblank);
  3581. }
  3582. /*
  3583. * send vblank event on all events not handled in flip and
  3584. * mark consumed event for drm_atomic_helper_commit_hw_done
  3585. */
  3586. spin_lock_irqsave(&adev->ddev->event_lock, flags);
  3587. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  3588. if (new_crtc_state->event)
  3589. drm_send_event_locked(dev, &new_crtc_state->event->base);
  3590. new_crtc_state->event = NULL;
  3591. }
  3592. spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
  3593. /* Signal HW programming completion */
  3594. drm_atomic_helper_commit_hw_done(state);
  3595. if (wait_for_vblank)
  3596. drm_atomic_helper_wait_for_flip_done(dev, state);
  3597. drm_atomic_helper_cleanup_planes(dev, state);
  3598. }
  3599. static int dm_force_atomic_commit(struct drm_connector *connector)
  3600. {
  3601. int ret = 0;
  3602. struct drm_device *ddev = connector->dev;
  3603. struct drm_atomic_state *state = drm_atomic_state_alloc(ddev);
  3604. struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
  3605. struct drm_plane *plane = disconnected_acrtc->base.primary;
  3606. struct drm_connector_state *conn_state;
  3607. struct drm_crtc_state *crtc_state;
  3608. struct drm_plane_state *plane_state;
  3609. if (!state)
  3610. return -ENOMEM;
  3611. state->acquire_ctx = ddev->mode_config.acquire_ctx;
  3612. /* Construct an atomic state to restore previous display setting */
  3613. /*
  3614. * Attach connectors to drm_atomic_state
  3615. */
  3616. conn_state = drm_atomic_get_connector_state(state, connector);
  3617. ret = PTR_ERR_OR_ZERO(conn_state);
  3618. if (ret)
  3619. goto err;
  3620. /* Attach crtc to drm_atomic_state*/
  3621. crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base);
  3622. ret = PTR_ERR_OR_ZERO(crtc_state);
  3623. if (ret)
  3624. goto err;
  3625. /* force a restore */
  3626. crtc_state->mode_changed = true;
  3627. /* Attach plane to drm_atomic_state */
  3628. plane_state = drm_atomic_get_plane_state(state, plane);
  3629. ret = PTR_ERR_OR_ZERO(plane_state);
  3630. if (ret)
  3631. goto err;
  3632. /* Call commit internally with the state we just constructed */
  3633. ret = drm_atomic_commit(state);
  3634. if (!ret)
  3635. return 0;
  3636. err:
  3637. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3638. drm_atomic_state_put(state);
  3639. return ret;
  3640. }
  3641. /*
  3642. * This functions handle all cases when set mode does not come upon hotplug.
  3643. * This include when the same display is unplugged then plugged back into the
  3644. * same port and when we are running without usermode desktop manager supprot
  3645. */
  3646. void dm_restore_drm_connector_state(struct drm_device *dev,
  3647. struct drm_connector *connector)
  3648. {
  3649. struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
  3650. struct amdgpu_crtc *disconnected_acrtc;
  3651. struct dm_crtc_state *acrtc_state;
  3652. if (!aconnector->dc_sink || !connector->state || !connector->encoder)
  3653. return;
  3654. disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc);
  3655. if (!disconnected_acrtc)
  3656. return;
  3657. acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state);
  3658. if (!acrtc_state->stream)
  3659. return;
  3660. /*
  3661. * If the previous sink is not released and different from the current,
  3662. * we deduce we are in a state where we can not rely on usermode call
  3663. * to turn on the display, so we do it here
  3664. */
  3665. if (acrtc_state->stream->sink != aconnector->dc_sink)
  3666. dm_force_atomic_commit(&aconnector->base);
  3667. }
  3668. /*`
  3669. * Grabs all modesetting locks to serialize against any blocking commits,
  3670. * Waits for completion of all non blocking commits.
  3671. */
  3672. static int do_aquire_global_lock(struct drm_device *dev,
  3673. struct drm_atomic_state *state)
  3674. {
  3675. struct drm_crtc *crtc;
  3676. struct drm_crtc_commit *commit;
  3677. long ret;
  3678. /* Adding all modeset locks to aquire_ctx will
  3679. * ensure that when the framework release it the
  3680. * extra locks we are locking here will get released to
  3681. */
  3682. ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
  3683. if (ret)
  3684. return ret;
  3685. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3686. spin_lock(&crtc->commit_lock);
  3687. commit = list_first_entry_or_null(&crtc->commit_list,
  3688. struct drm_crtc_commit, commit_entry);
  3689. if (commit)
  3690. drm_crtc_commit_get(commit);
  3691. spin_unlock(&crtc->commit_lock);
  3692. if (!commit)
  3693. continue;
  3694. /* Make sure all pending HW programming completed and
  3695. * page flips done
  3696. */
  3697. ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ);
  3698. if (ret > 0)
  3699. ret = wait_for_completion_interruptible_timeout(
  3700. &commit->flip_done, 10*HZ);
  3701. if (ret == 0)
  3702. DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done "
  3703. "timed out\n", crtc->base.id, crtc->name);
  3704. drm_crtc_commit_put(commit);
  3705. }
  3706. return ret < 0 ? ret : 0;
  3707. }
  3708. static int dm_update_crtcs_state(struct dc *dc,
  3709. struct drm_atomic_state *state,
  3710. bool enable,
  3711. bool *lock_and_validation_needed)
  3712. {
  3713. struct drm_crtc *crtc;
  3714. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  3715. int i;
  3716. struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state;
  3717. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  3718. struct dc_stream_state *new_stream;
  3719. int ret = 0;
  3720. /*TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set */
  3721. /* update changed items */
  3722. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  3723. struct amdgpu_crtc *acrtc = NULL;
  3724. struct amdgpu_dm_connector *aconnector = NULL;
  3725. struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL;
  3726. struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL;
  3727. struct drm_plane_state *new_plane_state = NULL;
  3728. new_stream = NULL;
  3729. dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  3730. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3731. acrtc = to_amdgpu_crtc(crtc);
  3732. new_plane_state = drm_atomic_get_new_plane_state(state, new_crtc_state->crtc->primary);
  3733. if (new_crtc_state->enable && new_plane_state && !new_plane_state->fb) {
  3734. ret = -EINVAL;
  3735. goto fail;
  3736. }
  3737. aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc);
  3738. /* TODO This hack should go away */
  3739. if (aconnector && enable) {
  3740. // Make sure fake sink is created in plug-in scenario
  3741. drm_new_conn_state = drm_atomic_get_new_connector_state(state,
  3742. &aconnector->base);
  3743. drm_old_conn_state = drm_atomic_get_old_connector_state(state,
  3744. &aconnector->base);
  3745. if (IS_ERR(drm_new_conn_state)) {
  3746. ret = PTR_ERR_OR_ZERO(drm_new_conn_state);
  3747. break;
  3748. }
  3749. dm_new_conn_state = to_dm_connector_state(drm_new_conn_state);
  3750. dm_old_conn_state = to_dm_connector_state(drm_old_conn_state);
  3751. new_stream = create_stream_for_sink(aconnector,
  3752. &new_crtc_state->mode,
  3753. dm_new_conn_state);
  3754. /*
  3755. * we can have no stream on ACTION_SET if a display
  3756. * was disconnected during S3, in this case it not and
  3757. * error, the OS will be updated after detection, and
  3758. * do the right thing on next atomic commit
  3759. */
  3760. if (!new_stream) {
  3761. DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n",
  3762. __func__, acrtc->base.base.id);
  3763. break;
  3764. }
  3765. if (dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) &&
  3766. dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) {
  3767. new_crtc_state->mode_changed = false;
  3768. DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d",
  3769. new_crtc_state->mode_changed);
  3770. }
  3771. }
  3772. if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
  3773. goto next_crtc;
  3774. DRM_DEBUG_DRIVER(
  3775. "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
  3776. "planes_changed:%d, mode_changed:%d,active_changed:%d,"
  3777. "connectors_changed:%d\n",
  3778. acrtc->crtc_id,
  3779. new_crtc_state->enable,
  3780. new_crtc_state->active,
  3781. new_crtc_state->planes_changed,
  3782. new_crtc_state->mode_changed,
  3783. new_crtc_state->active_changed,
  3784. new_crtc_state->connectors_changed);
  3785. /* Remove stream for any changed/disabled CRTC */
  3786. if (!enable) {
  3787. if (!dm_old_crtc_state->stream)
  3788. goto next_crtc;
  3789. DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n",
  3790. crtc->base.id);
  3791. /* i.e. reset mode */
  3792. if (dc_remove_stream_from_ctx(
  3793. dc,
  3794. dm_state->context,
  3795. dm_old_crtc_state->stream) != DC_OK) {
  3796. ret = -EINVAL;
  3797. goto fail;
  3798. }
  3799. dc_stream_release(dm_old_crtc_state->stream);
  3800. dm_new_crtc_state->stream = NULL;
  3801. *lock_and_validation_needed = true;
  3802. } else {/* Add stream for any updated/enabled CRTC */
  3803. /*
  3804. * Quick fix to prevent NULL pointer on new_stream when
  3805. * added MST connectors not found in existing crtc_state in the chained mode
  3806. * TODO: need to dig out the root cause of that
  3807. */
  3808. if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port))
  3809. goto next_crtc;
  3810. if (modereset_required(new_crtc_state))
  3811. goto next_crtc;
  3812. if (modeset_required(new_crtc_state, new_stream,
  3813. dm_old_crtc_state->stream)) {
  3814. WARN_ON(dm_new_crtc_state->stream);
  3815. dm_new_crtc_state->stream = new_stream;
  3816. dc_stream_retain(new_stream);
  3817. DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n",
  3818. crtc->base.id);
  3819. if (dc_add_stream_to_ctx(
  3820. dc,
  3821. dm_state->context,
  3822. dm_new_crtc_state->stream) != DC_OK) {
  3823. ret = -EINVAL;
  3824. goto fail;
  3825. }
  3826. *lock_and_validation_needed = true;
  3827. }
  3828. }
  3829. next_crtc:
  3830. /* Release extra reference */
  3831. if (new_stream)
  3832. dc_stream_release(new_stream);
  3833. /*
  3834. * We want to do dc stream updates that do not require a
  3835. * full modeset below.
  3836. */
  3837. if (!(enable && aconnector && new_crtc_state->enable &&
  3838. new_crtc_state->active))
  3839. continue;
  3840. /*
  3841. * Given above conditions, the dc state cannot be NULL because:
  3842. * 1. We're in the process of enabling CRTCs (just been added
  3843. * to the dc context, or already is on the context)
  3844. * 2. Has a valid connector attached, and
  3845. * 3. Is currently active and enabled.
  3846. * => The dc stream state currently exists.
  3847. */
  3848. BUG_ON(dm_new_crtc_state->stream == NULL);
  3849. /* Scaling or underscan settings */
  3850. if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state))
  3851. update_stream_scaling_settings(
  3852. &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream);
  3853. /*
  3854. * Color management settings. We also update color properties
  3855. * when a modeset is needed, to ensure it gets reprogrammed.
  3856. */
  3857. if (dm_new_crtc_state->base.color_mgmt_changed ||
  3858. drm_atomic_crtc_needs_modeset(new_crtc_state)) {
  3859. ret = amdgpu_dm_set_regamma_lut(dm_new_crtc_state);
  3860. if (ret)
  3861. goto fail;
  3862. amdgpu_dm_set_ctm(dm_new_crtc_state);
  3863. }
  3864. }
  3865. return ret;
  3866. fail:
  3867. if (new_stream)
  3868. dc_stream_release(new_stream);
  3869. return ret;
  3870. }
  3871. static int dm_update_planes_state(struct dc *dc,
  3872. struct drm_atomic_state *state,
  3873. bool enable,
  3874. bool *lock_and_validation_needed)
  3875. {
  3876. struct drm_crtc *new_plane_crtc, *old_plane_crtc;
  3877. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  3878. struct drm_plane *plane;
  3879. struct drm_plane_state *old_plane_state, *new_plane_state;
  3880. struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state;
  3881. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  3882. struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state;
  3883. int i ;
  3884. /* TODO return page_flip_needed() function */
  3885. bool pflip_needed = !state->allow_modeset;
  3886. int ret = 0;
  3887. /* Add new planes, in reverse order as DC expectation */
  3888. for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) {
  3889. new_plane_crtc = new_plane_state->crtc;
  3890. old_plane_crtc = old_plane_state->crtc;
  3891. dm_new_plane_state = to_dm_plane_state(new_plane_state);
  3892. dm_old_plane_state = to_dm_plane_state(old_plane_state);
  3893. /*TODO Implement atomic check for cursor plane */
  3894. if (plane->type == DRM_PLANE_TYPE_CURSOR)
  3895. continue;
  3896. /* Remove any changed/removed planes */
  3897. if (!enable) {
  3898. if (pflip_needed)
  3899. continue;
  3900. if (!old_plane_crtc)
  3901. continue;
  3902. old_crtc_state = drm_atomic_get_old_crtc_state(
  3903. state, old_plane_crtc);
  3904. dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  3905. if (!dm_old_crtc_state->stream)
  3906. continue;
  3907. DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n",
  3908. plane->base.id, old_plane_crtc->base.id);
  3909. if (!dc_remove_plane_from_context(
  3910. dc,
  3911. dm_old_crtc_state->stream,
  3912. dm_old_plane_state->dc_state,
  3913. dm_state->context)) {
  3914. ret = EINVAL;
  3915. return ret;
  3916. }
  3917. dc_plane_state_release(dm_old_plane_state->dc_state);
  3918. dm_new_plane_state->dc_state = NULL;
  3919. *lock_and_validation_needed = true;
  3920. } else { /* Add new planes */
  3921. struct dc_plane_state *dc_new_plane_state;
  3922. if (drm_atomic_plane_disabling(plane->state, new_plane_state))
  3923. continue;
  3924. if (!new_plane_crtc)
  3925. continue;
  3926. new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc);
  3927. dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
  3928. if (!dm_new_crtc_state->stream)
  3929. continue;
  3930. if (pflip_needed)
  3931. continue;
  3932. WARN_ON(dm_new_plane_state->dc_state);
  3933. dc_new_plane_state = dc_create_plane_state(dc);
  3934. if (!dc_new_plane_state)
  3935. return -ENOMEM;
  3936. DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n",
  3937. plane->base.id, new_plane_crtc->base.id);
  3938. ret = fill_plane_attributes(
  3939. new_plane_crtc->dev->dev_private,
  3940. dc_new_plane_state,
  3941. new_plane_state,
  3942. new_crtc_state);
  3943. if (ret) {
  3944. dc_plane_state_release(dc_new_plane_state);
  3945. return ret;
  3946. }
  3947. /*
  3948. * Any atomic check errors that occur after this will
  3949. * not need a release. The plane state will be attached
  3950. * to the stream, and therefore part of the atomic
  3951. * state. It'll be released when the atomic state is
  3952. * cleaned.
  3953. */
  3954. if (!dc_add_plane_to_context(
  3955. dc,
  3956. dm_new_crtc_state->stream,
  3957. dc_new_plane_state,
  3958. dm_state->context)) {
  3959. dc_plane_state_release(dc_new_plane_state);
  3960. return -EINVAL;
  3961. }
  3962. dm_new_plane_state->dc_state = dc_new_plane_state;
  3963. /* Tell DC to do a full surface update every time there
  3964. * is a plane change. Inefficient, but works for now.
  3965. */
  3966. dm_new_plane_state->dc_state->update_flags.bits.full_update = 1;
  3967. *lock_and_validation_needed = true;
  3968. }
  3969. }
  3970. return ret;
  3971. }
  3972. static int amdgpu_dm_atomic_check(struct drm_device *dev,
  3973. struct drm_atomic_state *state)
  3974. {
  3975. struct amdgpu_device *adev = dev->dev_private;
  3976. struct dc *dc = adev->dm.dc;
  3977. struct dm_atomic_state *dm_state = to_dm_atomic_state(state);
  3978. struct drm_connector *connector;
  3979. struct drm_connector_state *old_con_state, *new_con_state;
  3980. struct drm_crtc *crtc;
  3981. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  3982. int ret, i;
  3983. /*
  3984. * This bool will be set for true for any modeset/reset
  3985. * or plane update which implies non fast surface update.
  3986. */
  3987. bool lock_and_validation_needed = false;
  3988. ret = drm_atomic_helper_check_modeset(dev, state);
  3989. if (ret)
  3990. goto fail;
  3991. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  3992. if (!drm_atomic_crtc_needs_modeset(new_crtc_state) &&
  3993. !new_crtc_state->color_mgmt_changed)
  3994. continue;
  3995. if (!new_crtc_state->enable)
  3996. continue;
  3997. ret = drm_atomic_add_affected_connectors(state, crtc);
  3998. if (ret)
  3999. return ret;
  4000. ret = drm_atomic_add_affected_planes(state, crtc);
  4001. if (ret)
  4002. goto fail;
  4003. }
  4004. dm_state->context = dc_create_state();
  4005. ASSERT(dm_state->context);
  4006. dc_resource_state_copy_construct_current(dc, dm_state->context);
  4007. /* Remove exiting planes if they are modified */
  4008. ret = dm_update_planes_state(dc, state, false, &lock_and_validation_needed);
  4009. if (ret) {
  4010. goto fail;
  4011. }
  4012. /* Disable all crtcs which require disable */
  4013. ret = dm_update_crtcs_state(dc, state, false, &lock_and_validation_needed);
  4014. if (ret) {
  4015. goto fail;
  4016. }
  4017. /* Enable all crtcs which require enable */
  4018. ret = dm_update_crtcs_state(dc, state, true, &lock_and_validation_needed);
  4019. if (ret) {
  4020. goto fail;
  4021. }
  4022. /* Add new/modified planes */
  4023. ret = dm_update_planes_state(dc, state, true, &lock_and_validation_needed);
  4024. if (ret) {
  4025. goto fail;
  4026. }
  4027. /* Run this here since we want to validate the streams we created */
  4028. ret = drm_atomic_helper_check_planes(dev, state);
  4029. if (ret)
  4030. goto fail;
  4031. /* Check scaling and underscan changes*/
  4032. /*TODO Removed scaling changes validation due to inability to commit
  4033. * new stream into context w\o causing full reset. Need to
  4034. * decide how to handle.
  4035. */
  4036. for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) {
  4037. struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state);
  4038. struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state);
  4039. struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc);
  4040. /* Skip any modesets/resets */
  4041. if (!acrtc || drm_atomic_crtc_needs_modeset(
  4042. drm_atomic_get_new_crtc_state(state, &acrtc->base)))
  4043. continue;
  4044. /* Skip any thing not scale or underscan changes */
  4045. if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state))
  4046. continue;
  4047. lock_and_validation_needed = true;
  4048. }
  4049. /*
  4050. * For full updates case when
  4051. * removing/adding/updating streams on once CRTC while flipping
  4052. * on another CRTC,
  4053. * acquiring global lock will guarantee that any such full
  4054. * update commit
  4055. * will wait for completion of any outstanding flip using DRMs
  4056. * synchronization events.
  4057. */
  4058. if (lock_and_validation_needed) {
  4059. ret = do_aquire_global_lock(dev, state);
  4060. if (ret)
  4061. goto fail;
  4062. if (dc_validate_global_state(dc, dm_state->context) != DC_OK) {
  4063. ret = -EINVAL;
  4064. goto fail;
  4065. }
  4066. }
  4067. /* Must be success */
  4068. WARN_ON(ret);
  4069. return ret;
  4070. fail:
  4071. if (ret == -EDEADLK)
  4072. DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n");
  4073. else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS)
  4074. DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n");
  4075. else
  4076. DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret);
  4077. return ret;
  4078. }
  4079. static bool is_dp_capable_without_timing_msa(struct dc *dc,
  4080. struct amdgpu_dm_connector *amdgpu_dm_connector)
  4081. {
  4082. uint8_t dpcd_data;
  4083. bool capable = false;
  4084. if (amdgpu_dm_connector->dc_link &&
  4085. dm_helpers_dp_read_dpcd(
  4086. NULL,
  4087. amdgpu_dm_connector->dc_link,
  4088. DP_DOWN_STREAM_PORT_COUNT,
  4089. &dpcd_data,
  4090. sizeof(dpcd_data))) {
  4091. capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false;
  4092. }
  4093. return capable;
  4094. }
  4095. void amdgpu_dm_add_sink_to_freesync_module(struct drm_connector *connector,
  4096. struct edid *edid)
  4097. {
  4098. int i;
  4099. uint64_t val_capable;
  4100. bool edid_check_required;
  4101. struct detailed_timing *timing;
  4102. struct detailed_non_pixel *data;
  4103. struct detailed_data_monitor_range *range;
  4104. struct amdgpu_dm_connector *amdgpu_dm_connector =
  4105. to_amdgpu_dm_connector(connector);
  4106. struct drm_device *dev = connector->dev;
  4107. struct amdgpu_device *adev = dev->dev_private;
  4108. edid_check_required = false;
  4109. if (!amdgpu_dm_connector->dc_sink) {
  4110. DRM_ERROR("dc_sink NULL, could not add free_sync module.\n");
  4111. return;
  4112. }
  4113. if (!adev->dm.freesync_module)
  4114. return;
  4115. /*
  4116. * if edid non zero restrict freesync only for dp and edp
  4117. */
  4118. if (edid) {
  4119. if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT
  4120. || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) {
  4121. edid_check_required = is_dp_capable_without_timing_msa(
  4122. adev->dm.dc,
  4123. amdgpu_dm_connector);
  4124. }
  4125. }
  4126. val_capable = 0;
  4127. if (edid_check_required == true && (edid->version > 1 ||
  4128. (edid->version == 1 && edid->revision > 1))) {
  4129. for (i = 0; i < 4; i++) {
  4130. timing = &edid->detailed_timings[i];
  4131. data = &timing->data.other_data;
  4132. range = &data->data.range;
  4133. /*
  4134. * Check if monitor has continuous frequency mode
  4135. */
  4136. if (data->type != EDID_DETAIL_MONITOR_RANGE)
  4137. continue;
  4138. /*
  4139. * Check for flag range limits only. If flag == 1 then
  4140. * no additional timing information provided.
  4141. * Default GTF, GTF Secondary curve and CVT are not
  4142. * supported
  4143. */
  4144. if (range->flags != 1)
  4145. continue;
  4146. amdgpu_dm_connector->min_vfreq = range->min_vfreq;
  4147. amdgpu_dm_connector->max_vfreq = range->max_vfreq;
  4148. amdgpu_dm_connector->pixel_clock_mhz =
  4149. range->pixel_clock_mhz * 10;
  4150. break;
  4151. }
  4152. if (amdgpu_dm_connector->max_vfreq -
  4153. amdgpu_dm_connector->min_vfreq > 10) {
  4154. amdgpu_dm_connector->caps.supported = true;
  4155. amdgpu_dm_connector->caps.min_refresh_in_micro_hz =
  4156. amdgpu_dm_connector->min_vfreq * 1000000;
  4157. amdgpu_dm_connector->caps.max_refresh_in_micro_hz =
  4158. amdgpu_dm_connector->max_vfreq * 1000000;
  4159. val_capable = 1;
  4160. }
  4161. }
  4162. /*
  4163. * TODO figure out how to notify user-mode or DRM of freesync caps
  4164. * once we figure out how to deal with freesync in an upstreamable
  4165. * fashion
  4166. */
  4167. }
  4168. void amdgpu_dm_remove_sink_from_freesync_module(struct drm_connector *connector)
  4169. {
  4170. /*
  4171. * TODO fill in once we figure out how to deal with freesync in
  4172. * an upstreamable fashion
  4173. */
  4174. }