amdgpu_dm.c 131 KB

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