amdgpu_dm.c 143 KB

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