amdgpu_dm.c 134 KB

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