amdgpu_dm.c 140 KB

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