amdgpu_dm.c 135 KB

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