amdgpu_dm.c 135 KB

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