drm_dp_mst_topology.c 177 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264
  1. /*
  2. * Copyright © 2014 Red Hat
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <linux/bitfield.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/i2c.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/random.h>
  29. #include <linux/sched.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/iopoll.h>
  32. #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
  33. #include <linux/stacktrace.h>
  34. #include <linux/sort.h>
  35. #include <linux/timekeeping.h>
  36. #include <linux/math64.h>
  37. #endif
  38. #include <drm/display/drm_dp_mst_helper.h>
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_drv.h>
  42. #include <drm/drm_edid.h>
  43. #include <drm/drm_fixed.h>
  44. #include <drm/drm_print.h>
  45. #include <drm/drm_probe_helper.h>
  46. #include "drm_dp_helper_internal.h"
  47. #include "drm_dp_mst_topology_internal.h"
  48. /**
  49. * DOC: dp mst helper
  50. *
  51. * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
  52. * protocol. The helpers contain a topology manager and bandwidth manager.
  53. * The helpers encapsulate the sending and received of sideband msgs.
  54. */
  55. struct drm_dp_pending_up_req {
  56. struct drm_dp_sideband_msg_hdr hdr;
  57. struct drm_dp_sideband_msg_req_body msg;
  58. struct list_head next;
  59. };
  60. static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
  61. char *buf);
  62. static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port);
  63. static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
  64. int id, u8 start_slot, u8 num_slots);
  65. static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
  66. struct drm_dp_mst_port *port,
  67. int offset, int size, u8 *bytes);
  68. static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
  69. struct drm_dp_mst_port *port,
  70. int offset, int size, u8 *bytes);
  71. static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
  72. struct drm_dp_mst_branch *mstb);
  73. static void
  74. drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
  75. struct drm_dp_mst_branch *mstb);
  76. static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
  77. struct drm_dp_mst_branch *mstb,
  78. struct drm_dp_mst_port *port);
  79. static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
  80. guid_t *guid);
  81. static int drm_dp_mst_register_i2c_bus(struct drm_dp_mst_port *port);
  82. static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_mst_port *port);
  83. static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
  84. static bool drm_dp_mst_port_downstream_of_branch(struct drm_dp_mst_port *port,
  85. struct drm_dp_mst_branch *branch);
  86. #define DBG_PREFIX "[dp_mst]"
  87. #define DP_STR(x) [DP_ ## x] = #x
  88. static const char *drm_dp_mst_req_type_str(u8 req_type)
  89. {
  90. static const char * const req_type_str[] = {
  91. DP_STR(GET_MSG_TRANSACTION_VERSION),
  92. DP_STR(LINK_ADDRESS),
  93. DP_STR(CONNECTION_STATUS_NOTIFY),
  94. DP_STR(ENUM_PATH_RESOURCES),
  95. DP_STR(ALLOCATE_PAYLOAD),
  96. DP_STR(QUERY_PAYLOAD),
  97. DP_STR(RESOURCE_STATUS_NOTIFY),
  98. DP_STR(CLEAR_PAYLOAD_ID_TABLE),
  99. DP_STR(REMOTE_DPCD_READ),
  100. DP_STR(REMOTE_DPCD_WRITE),
  101. DP_STR(REMOTE_I2C_READ),
  102. DP_STR(REMOTE_I2C_WRITE),
  103. DP_STR(POWER_UP_PHY),
  104. DP_STR(POWER_DOWN_PHY),
  105. DP_STR(SINK_EVENT_NOTIFY),
  106. DP_STR(QUERY_STREAM_ENC_STATUS),
  107. };
  108. if (req_type >= ARRAY_SIZE(req_type_str) ||
  109. !req_type_str[req_type])
  110. return "unknown";
  111. return req_type_str[req_type];
  112. }
  113. #undef DP_STR
  114. #define DP_STR(x) [DP_NAK_ ## x] = #x
  115. static const char *drm_dp_mst_nak_reason_str(u8 nak_reason)
  116. {
  117. static const char * const nak_reason_str[] = {
  118. DP_STR(WRITE_FAILURE),
  119. DP_STR(INVALID_READ),
  120. DP_STR(CRC_FAILURE),
  121. DP_STR(BAD_PARAM),
  122. DP_STR(DEFER),
  123. DP_STR(LINK_FAILURE),
  124. DP_STR(NO_RESOURCES),
  125. DP_STR(DPCD_FAIL),
  126. DP_STR(I2C_NAK),
  127. DP_STR(ALLOCATE_FAIL),
  128. };
  129. if (nak_reason >= ARRAY_SIZE(nak_reason_str) ||
  130. !nak_reason_str[nak_reason])
  131. return "unknown";
  132. return nak_reason_str[nak_reason];
  133. }
  134. #undef DP_STR
  135. #define DP_STR(x) [DRM_DP_SIDEBAND_TX_ ## x] = #x
  136. static const char *drm_dp_mst_sideband_tx_state_str(int state)
  137. {
  138. static const char * const sideband_reason_str[] = {
  139. DP_STR(QUEUED),
  140. DP_STR(START_SEND),
  141. DP_STR(SENT),
  142. DP_STR(RX),
  143. DP_STR(TIMEOUT),
  144. };
  145. if (state >= ARRAY_SIZE(sideband_reason_str) ||
  146. !sideband_reason_str[state])
  147. return "unknown";
  148. return sideband_reason_str[state];
  149. }
  150. static int
  151. drm_dp_mst_rad_to_str(const u8 rad[8], u8 lct, char *out, size_t len)
  152. {
  153. int i;
  154. u8 unpacked_rad[16] = {};
  155. for (i = 1; i < lct; i++) {
  156. if (i % 2)
  157. unpacked_rad[i] = rad[(i - 1) / 2] >> 4;
  158. else
  159. unpacked_rad[i] = rad[(i - 1) / 2] & 0xF;
  160. }
  161. /* TODO: Eventually add something to printk so we can format the rad
  162. * like this: 1.2.3
  163. */
  164. return snprintf(out, len, "%*phC", lct, unpacked_rad);
  165. }
  166. /* sideband msg handling */
  167. static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
  168. {
  169. u8 bitmask = 0x80;
  170. u8 bitshift = 7;
  171. u8 array_index = 0;
  172. int number_of_bits = num_nibbles * 4;
  173. u8 remainder = 0;
  174. while (number_of_bits != 0) {
  175. number_of_bits--;
  176. remainder <<= 1;
  177. remainder |= (data[array_index] & bitmask) >> bitshift;
  178. bitmask >>= 1;
  179. bitshift--;
  180. if (bitmask == 0) {
  181. bitmask = 0x80;
  182. bitshift = 7;
  183. array_index++;
  184. }
  185. if ((remainder & 0x10) == 0x10)
  186. remainder ^= 0x13;
  187. }
  188. number_of_bits = 4;
  189. while (number_of_bits != 0) {
  190. number_of_bits--;
  191. remainder <<= 1;
  192. if ((remainder & 0x10) != 0)
  193. remainder ^= 0x13;
  194. }
  195. return remainder;
  196. }
  197. static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
  198. {
  199. u8 bitmask = 0x80;
  200. u8 bitshift = 7;
  201. u8 array_index = 0;
  202. int number_of_bits = number_of_bytes * 8;
  203. u16 remainder = 0;
  204. while (number_of_bits != 0) {
  205. number_of_bits--;
  206. remainder <<= 1;
  207. remainder |= (data[array_index] & bitmask) >> bitshift;
  208. bitmask >>= 1;
  209. bitshift--;
  210. if (bitmask == 0) {
  211. bitmask = 0x80;
  212. bitshift = 7;
  213. array_index++;
  214. }
  215. if ((remainder & 0x100) == 0x100)
  216. remainder ^= 0xd5;
  217. }
  218. number_of_bits = 8;
  219. while (number_of_bits != 0) {
  220. number_of_bits--;
  221. remainder <<= 1;
  222. if ((remainder & 0x100) != 0)
  223. remainder ^= 0xd5;
  224. }
  225. return remainder & 0xff;
  226. }
  227. static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
  228. {
  229. u8 size = 3;
  230. size += (hdr->lct / 2);
  231. return size;
  232. }
  233. static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
  234. u8 *buf, int *len)
  235. {
  236. int idx = 0;
  237. int i;
  238. u8 crc4;
  239. buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
  240. for (i = 0; i < (hdr->lct / 2); i++)
  241. buf[idx++] = hdr->rad[i];
  242. buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
  243. (hdr->msg_len & 0x3f);
  244. buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
  245. crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
  246. buf[idx - 1] |= (crc4 & 0xf);
  247. *len = idx;
  248. }
  249. static bool drm_dp_decode_sideband_msg_hdr(const struct drm_dp_mst_topology_mgr *mgr,
  250. struct drm_dp_sideband_msg_hdr *hdr,
  251. u8 *buf, int buflen, u8 *hdrlen)
  252. {
  253. u8 crc4;
  254. u8 len;
  255. int i;
  256. u8 idx;
  257. if (buf[0] == 0)
  258. return false;
  259. len = 3;
  260. len += ((buf[0] & 0xf0) >> 4) / 2;
  261. if (len > buflen)
  262. return false;
  263. crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
  264. if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
  265. drm_dbg_kms(mgr->dev, "crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
  266. return false;
  267. }
  268. hdr->lct = (buf[0] & 0xf0) >> 4;
  269. hdr->lcr = (buf[0] & 0xf);
  270. idx = 1;
  271. for (i = 0; i < (hdr->lct / 2); i++)
  272. hdr->rad[i] = buf[idx++];
  273. hdr->broadcast = (buf[idx] >> 7) & 0x1;
  274. hdr->path_msg = (buf[idx] >> 6) & 0x1;
  275. hdr->msg_len = buf[idx] & 0x3f;
  276. if (hdr->msg_len < 1) /* min space for body CRC */
  277. return false;
  278. idx++;
  279. hdr->somt = (buf[idx] >> 7) & 0x1;
  280. hdr->eomt = (buf[idx] >> 6) & 0x1;
  281. hdr->seqno = (buf[idx] >> 4) & 0x1;
  282. idx++;
  283. *hdrlen = idx;
  284. return true;
  285. }
  286. void
  287. drm_dp_encode_sideband_req(const struct drm_dp_sideband_msg_req_body *req,
  288. struct drm_dp_sideband_msg_tx *raw)
  289. {
  290. int idx = 0;
  291. int i;
  292. u8 *buf = raw->msg;
  293. buf[idx++] = req->req_type & 0x7f;
  294. switch (req->req_type) {
  295. case DP_ENUM_PATH_RESOURCES:
  296. case DP_POWER_DOWN_PHY:
  297. case DP_POWER_UP_PHY:
  298. buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
  299. idx++;
  300. break;
  301. case DP_ALLOCATE_PAYLOAD:
  302. buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
  303. (req->u.allocate_payload.number_sdp_streams & 0xf);
  304. idx++;
  305. buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
  306. idx++;
  307. buf[idx] = (req->u.allocate_payload.pbn >> 8);
  308. idx++;
  309. buf[idx] = (req->u.allocate_payload.pbn & 0xff);
  310. idx++;
  311. for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
  312. buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
  313. (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
  314. idx++;
  315. }
  316. if (req->u.allocate_payload.number_sdp_streams & 1) {
  317. i = req->u.allocate_payload.number_sdp_streams - 1;
  318. buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
  319. idx++;
  320. }
  321. break;
  322. case DP_QUERY_PAYLOAD:
  323. buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
  324. idx++;
  325. buf[idx] = (req->u.query_payload.vcpi & 0x7f);
  326. idx++;
  327. break;
  328. case DP_REMOTE_DPCD_READ:
  329. buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
  330. buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
  331. idx++;
  332. buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
  333. idx++;
  334. buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
  335. idx++;
  336. buf[idx] = (req->u.dpcd_read.num_bytes);
  337. idx++;
  338. break;
  339. case DP_REMOTE_DPCD_WRITE:
  340. buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
  341. buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
  342. idx++;
  343. buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
  344. idx++;
  345. buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
  346. idx++;
  347. buf[idx] = (req->u.dpcd_write.num_bytes);
  348. idx++;
  349. memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
  350. idx += req->u.dpcd_write.num_bytes;
  351. break;
  352. case DP_REMOTE_I2C_READ:
  353. buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
  354. buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
  355. idx++;
  356. for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
  357. buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
  358. idx++;
  359. buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
  360. idx++;
  361. memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
  362. idx += req->u.i2c_read.transactions[i].num_bytes;
  363. buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 4;
  364. buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
  365. idx++;
  366. }
  367. buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
  368. idx++;
  369. buf[idx] = (req->u.i2c_read.num_bytes_read);
  370. idx++;
  371. break;
  372. case DP_REMOTE_I2C_WRITE:
  373. buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
  374. idx++;
  375. buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
  376. idx++;
  377. buf[idx] = (req->u.i2c_write.num_bytes);
  378. idx++;
  379. memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
  380. idx += req->u.i2c_write.num_bytes;
  381. break;
  382. case DP_QUERY_STREAM_ENC_STATUS: {
  383. const struct drm_dp_query_stream_enc_status *msg;
  384. msg = &req->u.enc_status;
  385. buf[idx] = msg->stream_id;
  386. idx++;
  387. memcpy(&buf[idx], msg->client_id, sizeof(msg->client_id));
  388. idx += sizeof(msg->client_id);
  389. buf[idx] = 0;
  390. buf[idx] |= FIELD_PREP(GENMASK(1, 0), msg->stream_event);
  391. buf[idx] |= msg->valid_stream_event ? BIT(2) : 0;
  392. buf[idx] |= FIELD_PREP(GENMASK(4, 3), msg->stream_behavior);
  393. buf[idx] |= msg->valid_stream_behavior ? BIT(5) : 0;
  394. idx++;
  395. }
  396. break;
  397. }
  398. raw->cur_len = idx;
  399. }
  400. EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_encode_sideband_req);
  401. /* Decode a sideband request we've encoded, mainly used for debugging */
  402. int
  403. drm_dp_decode_sideband_req(const struct drm_dp_sideband_msg_tx *raw,
  404. struct drm_dp_sideband_msg_req_body *req)
  405. {
  406. const u8 *buf = raw->msg;
  407. int i, idx = 0;
  408. req->req_type = buf[idx++] & 0x7f;
  409. switch (req->req_type) {
  410. case DP_ENUM_PATH_RESOURCES:
  411. case DP_POWER_DOWN_PHY:
  412. case DP_POWER_UP_PHY:
  413. req->u.port_num.port_number = (buf[idx] >> 4) & 0xf;
  414. break;
  415. case DP_ALLOCATE_PAYLOAD:
  416. {
  417. struct drm_dp_allocate_payload *a =
  418. &req->u.allocate_payload;
  419. a->number_sdp_streams = buf[idx] & 0xf;
  420. a->port_number = (buf[idx] >> 4) & 0xf;
  421. WARN_ON(buf[++idx] & 0x80);
  422. a->vcpi = buf[idx] & 0x7f;
  423. a->pbn = buf[++idx] << 8;
  424. a->pbn |= buf[++idx];
  425. idx++;
  426. for (i = 0; i < a->number_sdp_streams; i++) {
  427. a->sdp_stream_sink[i] =
  428. (buf[idx + (i / 2)] >> ((i % 2) ? 0 : 4)) & 0xf;
  429. }
  430. }
  431. break;
  432. case DP_QUERY_PAYLOAD:
  433. req->u.query_payload.port_number = (buf[idx] >> 4) & 0xf;
  434. WARN_ON(buf[++idx] & 0x80);
  435. req->u.query_payload.vcpi = buf[idx] & 0x7f;
  436. break;
  437. case DP_REMOTE_DPCD_READ:
  438. {
  439. struct drm_dp_remote_dpcd_read *r = &req->u.dpcd_read;
  440. r->port_number = (buf[idx] >> 4) & 0xf;
  441. r->dpcd_address = (buf[idx] << 16) & 0xf0000;
  442. r->dpcd_address |= (buf[++idx] << 8) & 0xff00;
  443. r->dpcd_address |= buf[++idx] & 0xff;
  444. r->num_bytes = buf[++idx];
  445. }
  446. break;
  447. case DP_REMOTE_DPCD_WRITE:
  448. {
  449. struct drm_dp_remote_dpcd_write *w =
  450. &req->u.dpcd_write;
  451. w->port_number = (buf[idx] >> 4) & 0xf;
  452. w->dpcd_address = (buf[idx] << 16) & 0xf0000;
  453. w->dpcd_address |= (buf[++idx] << 8) & 0xff00;
  454. w->dpcd_address |= buf[++idx] & 0xff;
  455. w->num_bytes = buf[++idx];
  456. w->bytes = kmemdup(&buf[++idx], w->num_bytes,
  457. GFP_KERNEL);
  458. if (!w->bytes)
  459. return -ENOMEM;
  460. }
  461. break;
  462. case DP_REMOTE_I2C_READ:
  463. {
  464. struct drm_dp_remote_i2c_read *r = &req->u.i2c_read;
  465. struct drm_dp_remote_i2c_read_tx *tx;
  466. bool failed = false;
  467. r->num_transactions = buf[idx] & 0x3;
  468. r->port_number = (buf[idx] >> 4) & 0xf;
  469. for (i = 0; i < r->num_transactions; i++) {
  470. tx = &r->transactions[i];
  471. tx->i2c_dev_id = buf[++idx] & 0x7f;
  472. tx->num_bytes = buf[++idx];
  473. tx->bytes = kmemdup(&buf[++idx],
  474. tx->num_bytes,
  475. GFP_KERNEL);
  476. if (!tx->bytes) {
  477. failed = true;
  478. break;
  479. }
  480. idx += tx->num_bytes;
  481. tx->no_stop_bit = (buf[idx] >> 5) & 0x1;
  482. tx->i2c_transaction_delay = buf[idx] & 0xf;
  483. }
  484. if (failed) {
  485. for (i = 0; i < r->num_transactions; i++) {
  486. tx = &r->transactions[i];
  487. kfree(tx->bytes);
  488. }
  489. return -ENOMEM;
  490. }
  491. r->read_i2c_device_id = buf[++idx] & 0x7f;
  492. r->num_bytes_read = buf[++idx];
  493. }
  494. break;
  495. case DP_REMOTE_I2C_WRITE:
  496. {
  497. struct drm_dp_remote_i2c_write *w = &req->u.i2c_write;
  498. w->port_number = (buf[idx] >> 4) & 0xf;
  499. w->write_i2c_device_id = buf[++idx] & 0x7f;
  500. w->num_bytes = buf[++idx];
  501. w->bytes = kmemdup(&buf[++idx], w->num_bytes,
  502. GFP_KERNEL);
  503. if (!w->bytes)
  504. return -ENOMEM;
  505. }
  506. break;
  507. case DP_QUERY_STREAM_ENC_STATUS:
  508. req->u.enc_status.stream_id = buf[idx++];
  509. for (i = 0; i < sizeof(req->u.enc_status.client_id); i++)
  510. req->u.enc_status.client_id[i] = buf[idx++];
  511. req->u.enc_status.stream_event = FIELD_GET(GENMASK(1, 0),
  512. buf[idx]);
  513. req->u.enc_status.valid_stream_event = FIELD_GET(BIT(2),
  514. buf[idx]);
  515. req->u.enc_status.stream_behavior = FIELD_GET(GENMASK(4, 3),
  516. buf[idx]);
  517. req->u.enc_status.valid_stream_behavior = FIELD_GET(BIT(5),
  518. buf[idx]);
  519. break;
  520. }
  521. return 0;
  522. }
  523. EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_decode_sideband_req);
  524. void
  525. drm_dp_dump_sideband_msg_req_body(const struct drm_dp_sideband_msg_req_body *req,
  526. int indent, struct drm_printer *printer)
  527. {
  528. int i;
  529. #define P(f, ...) drm_printf_indent(printer, indent, f, ##__VA_ARGS__)
  530. if (req->req_type == DP_LINK_ADDRESS) {
  531. /* No contents to print */
  532. P("type=%s\n", drm_dp_mst_req_type_str(req->req_type));
  533. return;
  534. }
  535. P("type=%s contents:\n", drm_dp_mst_req_type_str(req->req_type));
  536. indent++;
  537. switch (req->req_type) {
  538. case DP_ENUM_PATH_RESOURCES:
  539. case DP_POWER_DOWN_PHY:
  540. case DP_POWER_UP_PHY:
  541. P("port=%d\n", req->u.port_num.port_number);
  542. break;
  543. case DP_ALLOCATE_PAYLOAD:
  544. P("port=%d vcpi=%d pbn=%d sdp_streams=%d %*ph\n",
  545. req->u.allocate_payload.port_number,
  546. req->u.allocate_payload.vcpi, req->u.allocate_payload.pbn,
  547. req->u.allocate_payload.number_sdp_streams,
  548. req->u.allocate_payload.number_sdp_streams,
  549. req->u.allocate_payload.sdp_stream_sink);
  550. break;
  551. case DP_QUERY_PAYLOAD:
  552. P("port=%d vcpi=%d\n",
  553. req->u.query_payload.port_number,
  554. req->u.query_payload.vcpi);
  555. break;
  556. case DP_REMOTE_DPCD_READ:
  557. P("port=%d dpcd_addr=%05x len=%d\n",
  558. req->u.dpcd_read.port_number, req->u.dpcd_read.dpcd_address,
  559. req->u.dpcd_read.num_bytes);
  560. break;
  561. case DP_REMOTE_DPCD_WRITE:
  562. P("port=%d addr=%05x len=%d: %*ph\n",
  563. req->u.dpcd_write.port_number,
  564. req->u.dpcd_write.dpcd_address,
  565. req->u.dpcd_write.num_bytes, req->u.dpcd_write.num_bytes,
  566. req->u.dpcd_write.bytes);
  567. break;
  568. case DP_REMOTE_I2C_READ:
  569. P("port=%d num_tx=%d id=%d size=%d:\n",
  570. req->u.i2c_read.port_number,
  571. req->u.i2c_read.num_transactions,
  572. req->u.i2c_read.read_i2c_device_id,
  573. req->u.i2c_read.num_bytes_read);
  574. indent++;
  575. for (i = 0; i < req->u.i2c_read.num_transactions; i++) {
  576. const struct drm_dp_remote_i2c_read_tx *rtx =
  577. &req->u.i2c_read.transactions[i];
  578. P("%d: id=%03d size=%03d no_stop_bit=%d tx_delay=%03d: %*ph\n",
  579. i, rtx->i2c_dev_id, rtx->num_bytes,
  580. rtx->no_stop_bit, rtx->i2c_transaction_delay,
  581. rtx->num_bytes, rtx->bytes);
  582. }
  583. break;
  584. case DP_REMOTE_I2C_WRITE:
  585. P("port=%d id=%d size=%d: %*ph\n",
  586. req->u.i2c_write.port_number,
  587. req->u.i2c_write.write_i2c_device_id,
  588. req->u.i2c_write.num_bytes, req->u.i2c_write.num_bytes,
  589. req->u.i2c_write.bytes);
  590. break;
  591. case DP_QUERY_STREAM_ENC_STATUS:
  592. P("stream_id=%u client_id=%*ph stream_event=%x "
  593. "valid_event=%d stream_behavior=%x valid_behavior=%d",
  594. req->u.enc_status.stream_id,
  595. (int)ARRAY_SIZE(req->u.enc_status.client_id),
  596. req->u.enc_status.client_id, req->u.enc_status.stream_event,
  597. req->u.enc_status.valid_stream_event,
  598. req->u.enc_status.stream_behavior,
  599. req->u.enc_status.valid_stream_behavior);
  600. break;
  601. default:
  602. P("???\n");
  603. break;
  604. }
  605. #undef P
  606. }
  607. EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_dump_sideband_msg_req_body);
  608. static inline void
  609. drm_dp_mst_dump_sideband_msg_tx(struct drm_printer *p,
  610. const struct drm_dp_sideband_msg_tx *txmsg)
  611. {
  612. struct drm_dp_sideband_msg_req_body req;
  613. char buf[64];
  614. int ret;
  615. int i;
  616. drm_dp_mst_rad_to_str(txmsg->dst->rad, txmsg->dst->lct, buf,
  617. sizeof(buf));
  618. drm_printf(p, "txmsg cur_offset=%x cur_len=%x seqno=%x state=%s path_msg=%d dst=%s\n",
  619. txmsg->cur_offset, txmsg->cur_len, txmsg->seqno,
  620. drm_dp_mst_sideband_tx_state_str(txmsg->state),
  621. txmsg->path_msg, buf);
  622. ret = drm_dp_decode_sideband_req(txmsg, &req);
  623. if (ret) {
  624. drm_printf(p, "<failed to decode sideband req: %d>\n", ret);
  625. return;
  626. }
  627. drm_dp_dump_sideband_msg_req_body(&req, 1, p);
  628. switch (req.req_type) {
  629. case DP_REMOTE_DPCD_WRITE:
  630. kfree(req.u.dpcd_write.bytes);
  631. break;
  632. case DP_REMOTE_I2C_READ:
  633. for (i = 0; i < req.u.i2c_read.num_transactions; i++)
  634. kfree(req.u.i2c_read.transactions[i].bytes);
  635. break;
  636. case DP_REMOTE_I2C_WRITE:
  637. kfree(req.u.i2c_write.bytes);
  638. break;
  639. }
  640. }
  641. static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
  642. {
  643. u8 crc4;
  644. crc4 = drm_dp_msg_data_crc4(msg, len);
  645. msg[len] = crc4;
  646. }
  647. static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
  648. struct drm_dp_sideband_msg_tx *raw)
  649. {
  650. int idx = 0;
  651. u8 *buf = raw->msg;
  652. buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
  653. raw->cur_len = idx;
  654. }
  655. static int drm_dp_sideband_msg_set_header(struct drm_dp_sideband_msg_rx *msg,
  656. struct drm_dp_sideband_msg_hdr *hdr,
  657. u8 hdrlen)
  658. {
  659. /*
  660. * ignore out-of-order messages or messages that are part of a
  661. * failed transaction
  662. */
  663. if (!hdr->somt && !msg->have_somt)
  664. return false;
  665. /* get length contained in this portion */
  666. msg->curchunk_idx = 0;
  667. msg->curchunk_len = hdr->msg_len;
  668. msg->curchunk_hdrlen = hdrlen;
  669. /* we have already gotten an somt - don't bother parsing */
  670. if (hdr->somt && msg->have_somt)
  671. return false;
  672. if (hdr->somt) {
  673. memcpy(&msg->initial_hdr, hdr,
  674. sizeof(struct drm_dp_sideband_msg_hdr));
  675. msg->have_somt = true;
  676. }
  677. if (hdr->eomt)
  678. msg->have_eomt = true;
  679. return true;
  680. }
  681. /* this adds a chunk of msg to the builder to get the final msg */
  682. static bool drm_dp_sideband_append_payload(struct drm_dp_sideband_msg_rx *msg,
  683. u8 *replybuf, u8 replybuflen)
  684. {
  685. u8 crc4;
  686. memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
  687. msg->curchunk_idx += replybuflen;
  688. if (msg->curchunk_idx >= msg->curchunk_len) {
  689. /* do CRC */
  690. crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
  691. if (crc4 != msg->chunk[msg->curchunk_len - 1])
  692. print_hex_dump(KERN_DEBUG, "wrong crc",
  693. DUMP_PREFIX_NONE, 16, 1,
  694. msg->chunk, msg->curchunk_len, false);
  695. /* copy chunk into bigger msg */
  696. memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
  697. msg->curlen += msg->curchunk_len - 1;
  698. }
  699. return true;
  700. }
  701. static bool drm_dp_sideband_parse_link_address(const struct drm_dp_mst_topology_mgr *mgr,
  702. struct drm_dp_sideband_msg_rx *raw,
  703. struct drm_dp_sideband_msg_reply_body *repmsg)
  704. {
  705. int idx = 1;
  706. int i;
  707. import_guid(&repmsg->u.link_addr.guid, &raw->msg[idx]);
  708. idx += 16;
  709. repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
  710. idx++;
  711. if (idx > raw->curlen)
  712. goto fail_len;
  713. for (i = 0; i < repmsg->u.link_addr.nports; i++) {
  714. if (raw->msg[idx] & 0x80)
  715. repmsg->u.link_addr.ports[i].input_port = 1;
  716. repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
  717. repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
  718. idx++;
  719. if (idx > raw->curlen)
  720. goto fail_len;
  721. repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
  722. repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
  723. if (repmsg->u.link_addr.ports[i].input_port == 0)
  724. repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
  725. idx++;
  726. if (idx > raw->curlen)
  727. goto fail_len;
  728. if (repmsg->u.link_addr.ports[i].input_port == 0) {
  729. repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
  730. idx++;
  731. if (idx > raw->curlen)
  732. goto fail_len;
  733. import_guid(&repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx]);
  734. idx += 16;
  735. if (idx > raw->curlen)
  736. goto fail_len;
  737. repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
  738. repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
  739. idx++;
  740. }
  741. if (idx > raw->curlen)
  742. goto fail_len;
  743. }
  744. return true;
  745. fail_len:
  746. DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
  747. return false;
  748. }
  749. static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
  750. struct drm_dp_sideband_msg_reply_body *repmsg)
  751. {
  752. int idx = 1;
  753. repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
  754. idx++;
  755. if (idx > raw->curlen)
  756. goto fail_len;
  757. repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
  758. idx++;
  759. if (idx > raw->curlen)
  760. goto fail_len;
  761. memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
  762. return true;
  763. fail_len:
  764. DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
  765. return false;
  766. }
  767. static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
  768. struct drm_dp_sideband_msg_reply_body *repmsg)
  769. {
  770. int idx = 1;
  771. repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
  772. idx++;
  773. if (idx > raw->curlen)
  774. goto fail_len;
  775. return true;
  776. fail_len:
  777. DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
  778. return false;
  779. }
  780. static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
  781. struct drm_dp_sideband_msg_reply_body *repmsg)
  782. {
  783. int idx = 1;
  784. repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
  785. idx++;
  786. if (idx > raw->curlen)
  787. goto fail_len;
  788. repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
  789. idx++;
  790. /* TODO check */
  791. memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
  792. return true;
  793. fail_len:
  794. DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
  795. return false;
  796. }
  797. static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
  798. struct drm_dp_sideband_msg_reply_body *repmsg)
  799. {
  800. int idx = 1;
  801. repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
  802. repmsg->u.path_resources.fec_capable = raw->msg[idx] & 0x1;
  803. idx++;
  804. if (idx > raw->curlen)
  805. goto fail_len;
  806. repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
  807. idx += 2;
  808. if (idx > raw->curlen)
  809. goto fail_len;
  810. repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
  811. idx += 2;
  812. if (idx > raw->curlen)
  813. goto fail_len;
  814. return true;
  815. fail_len:
  816. DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
  817. return false;
  818. }
  819. static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
  820. struct drm_dp_sideband_msg_reply_body *repmsg)
  821. {
  822. int idx = 1;
  823. repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
  824. idx++;
  825. if (idx > raw->curlen)
  826. goto fail_len;
  827. repmsg->u.allocate_payload.vcpi = raw->msg[idx];
  828. idx++;
  829. if (idx > raw->curlen)
  830. goto fail_len;
  831. repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
  832. idx += 2;
  833. if (idx > raw->curlen)
  834. goto fail_len;
  835. return true;
  836. fail_len:
  837. DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
  838. return false;
  839. }
  840. static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
  841. struct drm_dp_sideband_msg_reply_body *repmsg)
  842. {
  843. int idx = 1;
  844. repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
  845. idx++;
  846. if (idx > raw->curlen)
  847. goto fail_len;
  848. repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
  849. idx += 2;
  850. if (idx > raw->curlen)
  851. goto fail_len;
  852. return true;
  853. fail_len:
  854. DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
  855. return false;
  856. }
  857. static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
  858. struct drm_dp_sideband_msg_reply_body *repmsg)
  859. {
  860. int idx = 1;
  861. repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
  862. idx++;
  863. if (idx > raw->curlen) {
  864. DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
  865. idx, raw->curlen);
  866. return false;
  867. }
  868. return true;
  869. }
  870. static bool
  871. drm_dp_sideband_parse_query_stream_enc_status(
  872. struct drm_dp_sideband_msg_rx *raw,
  873. struct drm_dp_sideband_msg_reply_body *repmsg)
  874. {
  875. struct drm_dp_query_stream_enc_status_ack_reply *reply;
  876. reply = &repmsg->u.enc_status;
  877. reply->stream_id = raw->msg[3];
  878. reply->reply_signed = raw->msg[2] & BIT(0);
  879. /*
  880. * NOTE: It's my impression from reading the spec that the below parsing
  881. * is correct. However I noticed while testing with an HDCP 1.4 display
  882. * through an HDCP 2.2 hub that only bit 3 was set. In that case, I
  883. * would expect both bits to be set. So keep the parsing following the
  884. * spec, but beware reality might not match the spec (at least for some
  885. * configurations).
  886. */
  887. reply->hdcp_1x_device_present = raw->msg[2] & BIT(4);
  888. reply->hdcp_2x_device_present = raw->msg[2] & BIT(3);
  889. reply->query_capable_device_present = raw->msg[2] & BIT(5);
  890. reply->legacy_device_present = raw->msg[2] & BIT(6);
  891. reply->unauthorizable_device_present = raw->msg[2] & BIT(7);
  892. reply->auth_completed = !!(raw->msg[1] & BIT(3));
  893. reply->encryption_enabled = !!(raw->msg[1] & BIT(4));
  894. reply->repeater_present = !!(raw->msg[1] & BIT(5));
  895. reply->state = (raw->msg[1] & GENMASK(7, 6)) >> 6;
  896. return true;
  897. }
  898. static bool drm_dp_sideband_parse_reply(const struct drm_dp_mst_topology_mgr *mgr,
  899. struct drm_dp_sideband_msg_rx *raw,
  900. struct drm_dp_sideband_msg_reply_body *msg)
  901. {
  902. memset(msg, 0, sizeof(*msg));
  903. msg->reply_type = (raw->msg[0] & 0x80) >> 7;
  904. msg->req_type = (raw->msg[0] & 0x7f);
  905. if (msg->reply_type == DP_SIDEBAND_REPLY_NAK) {
  906. import_guid(&msg->u.nak.guid, &raw->msg[1]);
  907. msg->u.nak.reason = raw->msg[17];
  908. msg->u.nak.nak_data = raw->msg[18];
  909. return false;
  910. }
  911. switch (msg->req_type) {
  912. case DP_LINK_ADDRESS:
  913. return drm_dp_sideband_parse_link_address(mgr, raw, msg);
  914. case DP_QUERY_PAYLOAD:
  915. return drm_dp_sideband_parse_query_payload_ack(raw, msg);
  916. case DP_REMOTE_DPCD_READ:
  917. return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
  918. case DP_REMOTE_DPCD_WRITE:
  919. return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
  920. case DP_REMOTE_I2C_READ:
  921. return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
  922. case DP_REMOTE_I2C_WRITE:
  923. return true; /* since there's nothing to parse */
  924. case DP_ENUM_PATH_RESOURCES:
  925. return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
  926. case DP_ALLOCATE_PAYLOAD:
  927. return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
  928. case DP_POWER_DOWN_PHY:
  929. case DP_POWER_UP_PHY:
  930. return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
  931. case DP_CLEAR_PAYLOAD_ID_TABLE:
  932. return true; /* since there's nothing to parse */
  933. case DP_QUERY_STREAM_ENC_STATUS:
  934. return drm_dp_sideband_parse_query_stream_enc_status(raw, msg);
  935. default:
  936. drm_err(mgr->dev, "Got unknown reply 0x%02x (%s)\n",
  937. msg->req_type, drm_dp_mst_req_type_str(msg->req_type));
  938. return false;
  939. }
  940. }
  941. static bool
  942. drm_dp_sideband_parse_connection_status_notify(const struct drm_dp_mst_topology_mgr *mgr,
  943. struct drm_dp_sideband_msg_rx *raw,
  944. struct drm_dp_sideband_msg_req_body *msg)
  945. {
  946. int idx = 1;
  947. msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
  948. idx++;
  949. if (idx > raw->curlen)
  950. goto fail_len;
  951. import_guid(&msg->u.conn_stat.guid, &raw->msg[idx]);
  952. idx += 16;
  953. if (idx > raw->curlen)
  954. goto fail_len;
  955. msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
  956. msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
  957. msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
  958. msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
  959. msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
  960. idx++;
  961. return true;
  962. fail_len:
  963. drm_dbg_kms(mgr->dev, "connection status reply parse length fail %d %d\n",
  964. idx, raw->curlen);
  965. return false;
  966. }
  967. static bool drm_dp_sideband_parse_resource_status_notify(const struct drm_dp_mst_topology_mgr *mgr,
  968. struct drm_dp_sideband_msg_rx *raw,
  969. struct drm_dp_sideband_msg_req_body *msg)
  970. {
  971. int idx = 1;
  972. msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
  973. idx++;
  974. if (idx > raw->curlen)
  975. goto fail_len;
  976. import_guid(&msg->u.resource_stat.guid, &raw->msg[idx]);
  977. idx += 16;
  978. if (idx > raw->curlen)
  979. goto fail_len;
  980. msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
  981. idx++;
  982. return true;
  983. fail_len:
  984. drm_dbg_kms(mgr->dev, "resource status reply parse length fail %d %d\n", idx, raw->curlen);
  985. return false;
  986. }
  987. static bool drm_dp_sideband_parse_req(const struct drm_dp_mst_topology_mgr *mgr,
  988. struct drm_dp_sideband_msg_rx *raw,
  989. struct drm_dp_sideband_msg_req_body *msg)
  990. {
  991. memset(msg, 0, sizeof(*msg));
  992. msg->req_type = (raw->msg[0] & 0x7f);
  993. switch (msg->req_type) {
  994. case DP_CONNECTION_STATUS_NOTIFY:
  995. return drm_dp_sideband_parse_connection_status_notify(mgr, raw, msg);
  996. case DP_RESOURCE_STATUS_NOTIFY:
  997. return drm_dp_sideband_parse_resource_status_notify(mgr, raw, msg);
  998. default:
  999. drm_err(mgr->dev, "Got unknown request 0x%02x (%s)\n",
  1000. msg->req_type, drm_dp_mst_req_type_str(msg->req_type));
  1001. return false;
  1002. }
  1003. }
  1004. static void build_dpcd_write(struct drm_dp_sideband_msg_tx *msg,
  1005. u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
  1006. {
  1007. struct drm_dp_sideband_msg_req_body req;
  1008. req.req_type = DP_REMOTE_DPCD_WRITE;
  1009. req.u.dpcd_write.port_number = port_num;
  1010. req.u.dpcd_write.dpcd_address = offset;
  1011. req.u.dpcd_write.num_bytes = num_bytes;
  1012. req.u.dpcd_write.bytes = bytes;
  1013. drm_dp_encode_sideband_req(&req, msg);
  1014. }
  1015. static void build_link_address(struct drm_dp_sideband_msg_tx *msg)
  1016. {
  1017. struct drm_dp_sideband_msg_req_body req;
  1018. req.req_type = DP_LINK_ADDRESS;
  1019. drm_dp_encode_sideband_req(&req, msg);
  1020. }
  1021. static void build_clear_payload_id_table(struct drm_dp_sideband_msg_tx *msg)
  1022. {
  1023. struct drm_dp_sideband_msg_req_body req;
  1024. req.req_type = DP_CLEAR_PAYLOAD_ID_TABLE;
  1025. drm_dp_encode_sideband_req(&req, msg);
  1026. msg->path_msg = true;
  1027. }
  1028. static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg,
  1029. int port_num)
  1030. {
  1031. struct drm_dp_sideband_msg_req_body req;
  1032. req.req_type = DP_ENUM_PATH_RESOURCES;
  1033. req.u.port_num.port_number = port_num;
  1034. drm_dp_encode_sideband_req(&req, msg);
  1035. msg->path_msg = true;
  1036. return 0;
  1037. }
  1038. static void build_allocate_payload(struct drm_dp_sideband_msg_tx *msg,
  1039. int port_num,
  1040. u8 vcpi, uint16_t pbn,
  1041. u8 number_sdp_streams,
  1042. u8 *sdp_stream_sink)
  1043. {
  1044. struct drm_dp_sideband_msg_req_body req;
  1045. memset(&req, 0, sizeof(req));
  1046. req.req_type = DP_ALLOCATE_PAYLOAD;
  1047. req.u.allocate_payload.port_number = port_num;
  1048. req.u.allocate_payload.vcpi = vcpi;
  1049. req.u.allocate_payload.pbn = pbn;
  1050. req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
  1051. memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
  1052. number_sdp_streams);
  1053. drm_dp_encode_sideband_req(&req, msg);
  1054. msg->path_msg = true;
  1055. }
  1056. static void build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
  1057. int port_num, bool power_up)
  1058. {
  1059. struct drm_dp_sideband_msg_req_body req;
  1060. if (power_up)
  1061. req.req_type = DP_POWER_UP_PHY;
  1062. else
  1063. req.req_type = DP_POWER_DOWN_PHY;
  1064. req.u.port_num.port_number = port_num;
  1065. drm_dp_encode_sideband_req(&req, msg);
  1066. msg->path_msg = true;
  1067. }
  1068. static int
  1069. build_query_stream_enc_status(struct drm_dp_sideband_msg_tx *msg, u8 stream_id,
  1070. u8 *q_id)
  1071. {
  1072. struct drm_dp_sideband_msg_req_body req;
  1073. req.req_type = DP_QUERY_STREAM_ENC_STATUS;
  1074. req.u.enc_status.stream_id = stream_id;
  1075. memcpy(req.u.enc_status.client_id, q_id,
  1076. sizeof(req.u.enc_status.client_id));
  1077. req.u.enc_status.stream_event = 0;
  1078. req.u.enc_status.valid_stream_event = false;
  1079. req.u.enc_status.stream_behavior = 0;
  1080. req.u.enc_status.valid_stream_behavior = false;
  1081. drm_dp_encode_sideband_req(&req, msg);
  1082. return 0;
  1083. }
  1084. static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
  1085. struct drm_dp_sideband_msg_tx *txmsg)
  1086. {
  1087. unsigned int state;
  1088. /*
  1089. * All updates to txmsg->state are protected by mgr->qlock, and the two
  1090. * cases we check here are terminal states. For those the barriers
  1091. * provided by the wake_up/wait_event pair are enough.
  1092. */
  1093. state = READ_ONCE(txmsg->state);
  1094. return (state == DRM_DP_SIDEBAND_TX_RX ||
  1095. state == DRM_DP_SIDEBAND_TX_TIMEOUT);
  1096. }
  1097. static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
  1098. struct drm_dp_sideband_msg_tx *txmsg)
  1099. {
  1100. struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
  1101. unsigned long wait_timeout = msecs_to_jiffies(4000);
  1102. unsigned long wait_expires = jiffies + wait_timeout;
  1103. int ret;
  1104. for (;;) {
  1105. /*
  1106. * If the driver provides a way for this, change to
  1107. * poll-waiting for the MST reply interrupt if we didn't receive
  1108. * it for 50 msec. This would cater for cases where the HPD
  1109. * pulse signal got lost somewhere, even though the sink raised
  1110. * the corresponding MST interrupt correctly. One example is the
  1111. * Club 3D CAC-1557 TypeC -> DP adapter which for some reason
  1112. * filters out short pulses with a duration less than ~540 usec.
  1113. *
  1114. * The poll period is 50 msec to avoid missing an interrupt
  1115. * after the sink has cleared it (after a 110msec timeout
  1116. * since it raised the interrupt).
  1117. */
  1118. ret = wait_event_timeout(mgr->tx_waitq,
  1119. check_txmsg_state(mgr, txmsg),
  1120. mgr->cbs->poll_hpd_irq ?
  1121. msecs_to_jiffies(50) :
  1122. wait_timeout);
  1123. if (ret || !mgr->cbs->poll_hpd_irq ||
  1124. time_after(jiffies, wait_expires))
  1125. break;
  1126. mgr->cbs->poll_hpd_irq(mgr);
  1127. }
  1128. mutex_lock(&mgr->qlock);
  1129. if (ret > 0) {
  1130. if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
  1131. ret = -EIO;
  1132. goto out;
  1133. }
  1134. } else {
  1135. drm_dbg_kms(mgr->dev, "timedout msg send %p %d %d\n",
  1136. txmsg, txmsg->state, txmsg->seqno);
  1137. /* dump some state */
  1138. ret = -EIO;
  1139. /* remove from q */
  1140. if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
  1141. txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
  1142. txmsg->state == DRM_DP_SIDEBAND_TX_SENT)
  1143. list_del(&txmsg->next);
  1144. }
  1145. out:
  1146. if (unlikely(ret == -EIO) && drm_debug_enabled(DRM_UT_DP)) {
  1147. struct drm_printer p = drm_dbg_printer(mgr->dev, DRM_UT_DP,
  1148. DBG_PREFIX);
  1149. drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
  1150. }
  1151. mutex_unlock(&mgr->qlock);
  1152. drm_dp_mst_kick_tx(mgr);
  1153. return ret;
  1154. }
  1155. static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
  1156. {
  1157. struct drm_dp_mst_branch *mstb;
  1158. mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
  1159. if (!mstb)
  1160. return NULL;
  1161. mstb->lct = lct;
  1162. if (lct > 1)
  1163. memcpy(mstb->rad, rad, lct / 2);
  1164. INIT_LIST_HEAD(&mstb->ports);
  1165. kref_init(&mstb->topology_kref);
  1166. kref_init(&mstb->malloc_kref);
  1167. return mstb;
  1168. }
  1169. static void drm_dp_free_mst_branch_device(struct kref *kref)
  1170. {
  1171. struct drm_dp_mst_branch *mstb =
  1172. container_of(kref, struct drm_dp_mst_branch, malloc_kref);
  1173. if (mstb->port_parent)
  1174. drm_dp_mst_put_port_malloc(mstb->port_parent);
  1175. kfree(mstb);
  1176. }
  1177. /**
  1178. * DOC: Branch device and port refcounting
  1179. *
  1180. * Topology refcount overview
  1181. * ~~~~~~~~~~~~~~~~~~~~~~~~~~
  1182. *
  1183. * The refcounting schemes for &struct drm_dp_mst_branch and &struct
  1184. * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
  1185. * two different kinds of refcounts: topology refcounts, and malloc refcounts.
  1186. *
  1187. * Topology refcounts are not exposed to drivers, and are handled internally
  1188. * by the DP MST helpers. The helpers use them in order to prevent the
  1189. * in-memory topology state from being changed in the middle of critical
  1190. * operations like changing the internal state of payload allocations. This
  1191. * means each branch and port will be considered to be connected to the rest
  1192. * of the topology until its topology refcount reaches zero. Additionally,
  1193. * for ports this means that their associated &struct drm_connector will stay
  1194. * registered with userspace until the port's refcount reaches 0.
  1195. *
  1196. * Malloc refcount overview
  1197. * ~~~~~~~~~~~~~~~~~~~~~~~~
  1198. *
  1199. * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
  1200. * drm_dp_mst_branch allocated even after all of its topology references have
  1201. * been dropped, so that the driver or MST helpers can safely access each
  1202. * branch's last known state before it was disconnected from the topology.
  1203. * When the malloc refcount of a port or branch reaches 0, the memory
  1204. * allocation containing the &struct drm_dp_mst_branch or &struct
  1205. * drm_dp_mst_port respectively will be freed.
  1206. *
  1207. * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
  1208. * to drivers. As of writing this documentation, there are no drivers that
  1209. * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
  1210. * helpers. Exposing this API to drivers in a race-free manner would take more
  1211. * tweaking of the refcounting scheme, however patches are welcome provided
  1212. * there is a legitimate driver usecase for this.
  1213. *
  1214. * Refcount relationships in a topology
  1215. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1216. *
  1217. * Let's take a look at why the relationship between topology and malloc
  1218. * refcounts is designed the way it is.
  1219. *
  1220. * .. kernel-figure:: dp-mst/topology-figure-1.dot
  1221. *
  1222. * An example of topology and malloc refs in a DP MST topology with two
  1223. * active payloads. Topology refcount increments are indicated by solid
  1224. * lines, and malloc refcount increments are indicated by dashed lines.
  1225. * Each starts from the branch which incremented the refcount, and ends at
  1226. * the branch to which the refcount belongs to, i.e. the arrow points the
  1227. * same way as the C pointers used to reference a structure.
  1228. *
  1229. * As you can see in the above figure, every branch increments the topology
  1230. * refcount of its children, and increments the malloc refcount of its
  1231. * parent. Additionally, every payload increments the malloc refcount of its
  1232. * assigned port by 1.
  1233. *
  1234. * So, what would happen if MSTB #3 from the above figure was unplugged from
  1235. * the system, but the driver hadn't yet removed payload #2 from port #3? The
  1236. * topology would start to look like the figure below.
  1237. *
  1238. * .. kernel-figure:: dp-mst/topology-figure-2.dot
  1239. *
  1240. * Ports and branch devices which have been released from memory are
  1241. * colored grey, and references which have been removed are colored red.
  1242. *
  1243. * Whenever a port or branch device's topology refcount reaches zero, it will
  1244. * decrement the topology refcounts of all its children, the malloc refcount
  1245. * of its parent, and finally its own malloc refcount. For MSTB #4 and port
  1246. * #4, this means they both have been disconnected from the topology and freed
  1247. * from memory. But, because payload #2 is still holding a reference to port
  1248. * #3, port #3 is removed from the topology but its &struct drm_dp_mst_port
  1249. * is still accessible from memory. This also means port #3 has not yet
  1250. * decremented the malloc refcount of MSTB #3, so its &struct
  1251. * drm_dp_mst_branch will also stay allocated in memory until port #3's
  1252. * malloc refcount reaches 0.
  1253. *
  1254. * This relationship is necessary because in order to release payload #2, we
  1255. * need to be able to figure out the last relative of port #3 that's still
  1256. * connected to the topology. In this case, we would travel up the topology as
  1257. * shown below.
  1258. *
  1259. * .. kernel-figure:: dp-mst/topology-figure-3.dot
  1260. *
  1261. * And finally, remove payload #2 by communicating with port #2 through
  1262. * sideband transactions.
  1263. */
  1264. /**
  1265. * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
  1266. * device
  1267. * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
  1268. *
  1269. * Increments &drm_dp_mst_branch.malloc_kref. When
  1270. * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
  1271. * will be released and @mstb may no longer be used.
  1272. *
  1273. * See also: drm_dp_mst_put_mstb_malloc()
  1274. */
  1275. static void
  1276. drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
  1277. {
  1278. kref_get(&mstb->malloc_kref);
  1279. drm_dbg(mstb->mgr->dev, "mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref));
  1280. }
  1281. /**
  1282. * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
  1283. * device
  1284. * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
  1285. *
  1286. * Decrements &drm_dp_mst_branch.malloc_kref. When
  1287. * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
  1288. * will be released and @mstb may no longer be used.
  1289. *
  1290. * See also: drm_dp_mst_get_mstb_malloc()
  1291. */
  1292. static void
  1293. drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
  1294. {
  1295. drm_dbg(mstb->mgr->dev, "mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1);
  1296. kref_put(&mstb->malloc_kref, drm_dp_free_mst_branch_device);
  1297. }
  1298. static void drm_dp_free_mst_port(struct kref *kref)
  1299. {
  1300. struct drm_dp_mst_port *port =
  1301. container_of(kref, struct drm_dp_mst_port, malloc_kref);
  1302. drm_dp_mst_put_mstb_malloc(port->parent);
  1303. kfree(port);
  1304. }
  1305. /**
  1306. * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
  1307. * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
  1308. *
  1309. * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
  1310. * reaches 0, the memory allocation for @port will be released and @port may
  1311. * no longer be used.
  1312. *
  1313. * Because @port could potentially be freed at any time by the DP MST helpers
  1314. * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
  1315. * function, drivers that which to make use of &struct drm_dp_mst_port should
  1316. * ensure that they grab at least one main malloc reference to their MST ports
  1317. * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
  1318. * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
  1319. *
  1320. * See also: drm_dp_mst_put_port_malloc()
  1321. */
  1322. void
  1323. drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
  1324. {
  1325. kref_get(&port->malloc_kref);
  1326. drm_dbg(port->mgr->dev, "port %p (%d)\n", port, kref_read(&port->malloc_kref));
  1327. }
  1328. EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
  1329. /**
  1330. * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
  1331. * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
  1332. *
  1333. * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
  1334. * reaches 0, the memory allocation for @port will be released and @port may
  1335. * no longer be used.
  1336. *
  1337. * See also: drm_dp_mst_get_port_malloc()
  1338. */
  1339. void
  1340. drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
  1341. {
  1342. drm_dbg(port->mgr->dev, "port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1);
  1343. kref_put(&port->malloc_kref, drm_dp_free_mst_port);
  1344. }
  1345. EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
  1346. #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
  1347. #define STACK_DEPTH 8
  1348. static noinline void
  1349. __topology_ref_save(struct drm_dp_mst_topology_mgr *mgr,
  1350. struct drm_dp_mst_topology_ref_history *history,
  1351. enum drm_dp_mst_topology_ref_type type)
  1352. {
  1353. struct drm_dp_mst_topology_ref_entry *entry = NULL;
  1354. depot_stack_handle_t backtrace;
  1355. ulong stack_entries[STACK_DEPTH];
  1356. uint n;
  1357. int i;
  1358. n = stack_trace_save(stack_entries, ARRAY_SIZE(stack_entries), 1);
  1359. backtrace = stack_depot_save(stack_entries, n, GFP_KERNEL);
  1360. if (!backtrace)
  1361. return;
  1362. /* Try to find an existing entry for this backtrace */
  1363. for (i = 0; i < history->len; i++) {
  1364. if (history->entries[i].backtrace == backtrace) {
  1365. entry = &history->entries[i];
  1366. break;
  1367. }
  1368. }
  1369. /* Otherwise add one */
  1370. if (!entry) {
  1371. struct drm_dp_mst_topology_ref_entry *new;
  1372. int new_len = history->len + 1;
  1373. new = krealloc(history->entries, sizeof(*new) * new_len,
  1374. GFP_KERNEL);
  1375. if (!new)
  1376. return;
  1377. entry = &new[history->len];
  1378. history->len = new_len;
  1379. history->entries = new;
  1380. entry->backtrace = backtrace;
  1381. entry->type = type;
  1382. entry->count = 0;
  1383. }
  1384. entry->count++;
  1385. entry->ts_nsec = ktime_get_ns();
  1386. }
  1387. static int
  1388. topology_ref_history_cmp(const void *a, const void *b)
  1389. {
  1390. const struct drm_dp_mst_topology_ref_entry *entry_a = a, *entry_b = b;
  1391. if (entry_a->ts_nsec > entry_b->ts_nsec)
  1392. return 1;
  1393. else if (entry_a->ts_nsec < entry_b->ts_nsec)
  1394. return -1;
  1395. else
  1396. return 0;
  1397. }
  1398. static inline const char *
  1399. topology_ref_type_to_str(enum drm_dp_mst_topology_ref_type type)
  1400. {
  1401. if (type == DRM_DP_MST_TOPOLOGY_REF_GET)
  1402. return "get";
  1403. else
  1404. return "put";
  1405. }
  1406. static void
  1407. __dump_topology_ref_history(struct drm_device *drm,
  1408. struct drm_dp_mst_topology_ref_history *history,
  1409. void *ptr, const char *type_str)
  1410. {
  1411. struct drm_printer p = drm_dbg_printer(drm, DRM_UT_DP, DBG_PREFIX);
  1412. char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  1413. int i;
  1414. if (!buf)
  1415. return;
  1416. if (!history->len)
  1417. goto out;
  1418. /* First, sort the list so that it goes from oldest to newest
  1419. * reference entry
  1420. */
  1421. sort(history->entries, history->len, sizeof(*history->entries),
  1422. topology_ref_history_cmp, NULL);
  1423. drm_printf(&p, "%s (%p) topology count reached 0, dumping history:\n",
  1424. type_str, ptr);
  1425. for (i = 0; i < history->len; i++) {
  1426. const struct drm_dp_mst_topology_ref_entry *entry =
  1427. &history->entries[i];
  1428. u64 ts_nsec = entry->ts_nsec;
  1429. u32 rem_nsec = do_div(ts_nsec, 1000000000);
  1430. stack_depot_snprint(entry->backtrace, buf, PAGE_SIZE, 4);
  1431. drm_printf(&p, " %d %ss (last at %5llu.%06u):\n%s",
  1432. entry->count,
  1433. topology_ref_type_to_str(entry->type),
  1434. ts_nsec, rem_nsec / 1000, buf);
  1435. }
  1436. /* Now free the history, since this is the only time we expose it */
  1437. kfree(history->entries);
  1438. out:
  1439. kfree(buf);
  1440. }
  1441. static __always_inline void
  1442. drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch *mstb)
  1443. {
  1444. __dump_topology_ref_history(mstb->mgr->dev, &mstb->topology_ref_history,
  1445. mstb, "MSTB");
  1446. }
  1447. static __always_inline void
  1448. drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port *port)
  1449. {
  1450. __dump_topology_ref_history(port->mgr->dev, &port->topology_ref_history,
  1451. port, "Port");
  1452. }
  1453. static __always_inline void
  1454. save_mstb_topology_ref(struct drm_dp_mst_branch *mstb,
  1455. enum drm_dp_mst_topology_ref_type type)
  1456. {
  1457. __topology_ref_save(mstb->mgr, &mstb->topology_ref_history, type);
  1458. }
  1459. static __always_inline void
  1460. save_port_topology_ref(struct drm_dp_mst_port *port,
  1461. enum drm_dp_mst_topology_ref_type type)
  1462. {
  1463. __topology_ref_save(port->mgr, &port->topology_ref_history, type);
  1464. }
  1465. static inline void
  1466. topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr)
  1467. {
  1468. mutex_lock(&mgr->topology_ref_history_lock);
  1469. }
  1470. static inline void
  1471. topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr)
  1472. {
  1473. mutex_unlock(&mgr->topology_ref_history_lock);
  1474. }
  1475. #else
  1476. static inline void
  1477. topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr) {}
  1478. static inline void
  1479. topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) {}
  1480. static inline void
  1481. drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch *mstb) {}
  1482. static inline void
  1483. drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port *port) {}
  1484. #define save_mstb_topology_ref(mstb, type)
  1485. #define save_port_topology_ref(port, type)
  1486. #endif
  1487. struct drm_dp_mst_atomic_payload *
  1488. drm_atomic_get_mst_payload_state(struct drm_dp_mst_topology_state *state,
  1489. struct drm_dp_mst_port *port)
  1490. {
  1491. struct drm_dp_mst_atomic_payload *payload;
  1492. list_for_each_entry(payload, &state->payloads, next)
  1493. if (payload->port == port)
  1494. return payload;
  1495. return NULL;
  1496. }
  1497. EXPORT_SYMBOL(drm_atomic_get_mst_payload_state);
  1498. static void drm_dp_destroy_mst_branch_device(struct kref *kref)
  1499. {
  1500. struct drm_dp_mst_branch *mstb =
  1501. container_of(kref, struct drm_dp_mst_branch, topology_kref);
  1502. struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
  1503. drm_dp_mst_dump_mstb_topology_history(mstb);
  1504. INIT_LIST_HEAD(&mstb->destroy_next);
  1505. /*
  1506. * This can get called under mgr->mutex, so we need to perform the
  1507. * actual destruction of the mstb in another worker
  1508. */
  1509. mutex_lock(&mgr->delayed_destroy_lock);
  1510. list_add(&mstb->destroy_next, &mgr->destroy_branch_device_list);
  1511. mutex_unlock(&mgr->delayed_destroy_lock);
  1512. queue_work(mgr->delayed_destroy_wq, &mgr->delayed_destroy_work);
  1513. }
  1514. /**
  1515. * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
  1516. * branch device unless it's zero
  1517. * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
  1518. *
  1519. * Attempts to grab a topology reference to @mstb, if it hasn't yet been
  1520. * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
  1521. * reached 0). Holding a topology reference implies that a malloc reference
  1522. * will be held to @mstb as long as the user holds the topology reference.
  1523. *
  1524. * Care should be taken to ensure that the user has at least one malloc
  1525. * reference to @mstb. If you already have a topology reference to @mstb, you
  1526. * should use drm_dp_mst_topology_get_mstb() instead.
  1527. *
  1528. * See also:
  1529. * drm_dp_mst_topology_get_mstb()
  1530. * drm_dp_mst_topology_put_mstb()
  1531. *
  1532. * Returns:
  1533. * * 1: A topology reference was grabbed successfully
  1534. * * 0: @port is no longer in the topology, no reference was grabbed
  1535. */
  1536. static int __must_check
  1537. drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
  1538. {
  1539. int ret;
  1540. topology_ref_history_lock(mstb->mgr);
  1541. ret = kref_get_unless_zero(&mstb->topology_kref);
  1542. if (ret) {
  1543. drm_dbg(mstb->mgr->dev, "mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
  1544. save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_GET);
  1545. }
  1546. topology_ref_history_unlock(mstb->mgr);
  1547. return ret;
  1548. }
  1549. /**
  1550. * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
  1551. * branch device
  1552. * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
  1553. *
  1554. * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
  1555. * not it's already reached 0. This is only valid to use in scenarios where
  1556. * you are already guaranteed to have at least one active topology reference
  1557. * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
  1558. *
  1559. * See also:
  1560. * drm_dp_mst_topology_try_get_mstb()
  1561. * drm_dp_mst_topology_put_mstb()
  1562. */
  1563. static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
  1564. {
  1565. topology_ref_history_lock(mstb->mgr);
  1566. save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_GET);
  1567. WARN_ON(kref_read(&mstb->topology_kref) == 0);
  1568. kref_get(&mstb->topology_kref);
  1569. drm_dbg(mstb->mgr->dev, "mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
  1570. topology_ref_history_unlock(mstb->mgr);
  1571. }
  1572. /**
  1573. * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
  1574. * device
  1575. * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
  1576. *
  1577. * Releases a topology reference from @mstb by decrementing
  1578. * &drm_dp_mst_branch.topology_kref.
  1579. *
  1580. * See also:
  1581. * drm_dp_mst_topology_try_get_mstb()
  1582. * drm_dp_mst_topology_get_mstb()
  1583. */
  1584. static void
  1585. drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
  1586. {
  1587. topology_ref_history_lock(mstb->mgr);
  1588. drm_dbg(mstb->mgr->dev, "mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref) - 1);
  1589. save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_PUT);
  1590. topology_ref_history_unlock(mstb->mgr);
  1591. kref_put(&mstb->topology_kref, drm_dp_destroy_mst_branch_device);
  1592. }
  1593. static void drm_dp_destroy_port(struct kref *kref)
  1594. {
  1595. struct drm_dp_mst_port *port =
  1596. container_of(kref, struct drm_dp_mst_port, topology_kref);
  1597. struct drm_dp_mst_topology_mgr *mgr = port->mgr;
  1598. drm_dp_mst_dump_port_topology_history(port);
  1599. /* There's nothing that needs locking to destroy an input port yet */
  1600. if (port->input) {
  1601. drm_dp_mst_put_port_malloc(port);
  1602. return;
  1603. }
  1604. drm_edid_free(port->cached_edid);
  1605. /*
  1606. * we can't destroy the connector here, as we might be holding the
  1607. * mode_config.mutex from an EDID retrieval
  1608. */
  1609. mutex_lock(&mgr->delayed_destroy_lock);
  1610. list_add(&port->next, &mgr->destroy_port_list);
  1611. mutex_unlock(&mgr->delayed_destroy_lock);
  1612. queue_work(mgr->delayed_destroy_wq, &mgr->delayed_destroy_work);
  1613. }
  1614. /**
  1615. * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
  1616. * port unless it's zero
  1617. * @port: &struct drm_dp_mst_port to increment the topology refcount of
  1618. *
  1619. * Attempts to grab a topology reference to @port, if it hasn't yet been
  1620. * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
  1621. * 0). Holding a topology reference implies that a malloc reference will be
  1622. * held to @port as long as the user holds the topology reference.
  1623. *
  1624. * Care should be taken to ensure that the user has at least one malloc
  1625. * reference to @port. If you already have a topology reference to @port, you
  1626. * should use drm_dp_mst_topology_get_port() instead.
  1627. *
  1628. * See also:
  1629. * drm_dp_mst_topology_get_port()
  1630. * drm_dp_mst_topology_put_port()
  1631. *
  1632. * Returns:
  1633. * * 1: A topology reference was grabbed successfully
  1634. * * 0: @port is no longer in the topology, no reference was grabbed
  1635. */
  1636. static int __must_check
  1637. drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
  1638. {
  1639. int ret;
  1640. topology_ref_history_lock(port->mgr);
  1641. ret = kref_get_unless_zero(&port->topology_kref);
  1642. if (ret) {
  1643. drm_dbg(port->mgr->dev, "port %p (%d)\n", port, kref_read(&port->topology_kref));
  1644. save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_GET);
  1645. }
  1646. topology_ref_history_unlock(port->mgr);
  1647. return ret;
  1648. }
  1649. /**
  1650. * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
  1651. * @port: The &struct drm_dp_mst_port to increment the topology refcount of
  1652. *
  1653. * Increments &drm_dp_mst_port.topology_refcount without checking whether or
  1654. * not it's already reached 0. This is only valid to use in scenarios where
  1655. * you are already guaranteed to have at least one active topology reference
  1656. * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
  1657. *
  1658. * See also:
  1659. * drm_dp_mst_topology_try_get_port()
  1660. * drm_dp_mst_topology_put_port()
  1661. */
  1662. static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
  1663. {
  1664. topology_ref_history_lock(port->mgr);
  1665. WARN_ON(kref_read(&port->topology_kref) == 0);
  1666. kref_get(&port->topology_kref);
  1667. drm_dbg(port->mgr->dev, "port %p (%d)\n", port, kref_read(&port->topology_kref));
  1668. save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_GET);
  1669. topology_ref_history_unlock(port->mgr);
  1670. }
  1671. /**
  1672. * drm_dp_mst_topology_put_port() - release a topology reference to a port
  1673. * @port: The &struct drm_dp_mst_port to release the topology reference from
  1674. *
  1675. * Releases a topology reference from @port by decrementing
  1676. * &drm_dp_mst_port.topology_kref.
  1677. *
  1678. * See also:
  1679. * drm_dp_mst_topology_try_get_port()
  1680. * drm_dp_mst_topology_get_port()
  1681. */
  1682. static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
  1683. {
  1684. topology_ref_history_lock(port->mgr);
  1685. drm_dbg(port->mgr->dev, "port %p (%d)\n", port, kref_read(&port->topology_kref) - 1);
  1686. save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_PUT);
  1687. topology_ref_history_unlock(port->mgr);
  1688. kref_put(&port->topology_kref, drm_dp_destroy_port);
  1689. }
  1690. static struct drm_dp_mst_branch *
  1691. drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
  1692. struct drm_dp_mst_branch *to_find)
  1693. {
  1694. struct drm_dp_mst_port *port;
  1695. struct drm_dp_mst_branch *rmstb;
  1696. if (to_find == mstb)
  1697. return mstb;
  1698. list_for_each_entry(port, &mstb->ports, next) {
  1699. if (port->mstb) {
  1700. rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
  1701. port->mstb, to_find);
  1702. if (rmstb)
  1703. return rmstb;
  1704. }
  1705. }
  1706. return NULL;
  1707. }
  1708. static struct drm_dp_mst_branch *
  1709. drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
  1710. struct drm_dp_mst_branch *mstb)
  1711. {
  1712. struct drm_dp_mst_branch *rmstb = NULL;
  1713. mutex_lock(&mgr->lock);
  1714. if (mgr->mst_primary) {
  1715. rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
  1716. mgr->mst_primary, mstb);
  1717. if (rmstb && !drm_dp_mst_topology_try_get_mstb(rmstb))
  1718. rmstb = NULL;
  1719. }
  1720. mutex_unlock(&mgr->lock);
  1721. return rmstb;
  1722. }
  1723. static struct drm_dp_mst_port *
  1724. drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
  1725. struct drm_dp_mst_port *to_find)
  1726. {
  1727. struct drm_dp_mst_port *port, *mport;
  1728. list_for_each_entry(port, &mstb->ports, next) {
  1729. if (port == to_find)
  1730. return port;
  1731. if (port->mstb) {
  1732. mport = drm_dp_mst_topology_get_port_validated_locked(
  1733. port->mstb, to_find);
  1734. if (mport)
  1735. return mport;
  1736. }
  1737. }
  1738. return NULL;
  1739. }
  1740. static struct drm_dp_mst_port *
  1741. drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
  1742. struct drm_dp_mst_port *port)
  1743. {
  1744. struct drm_dp_mst_port *rport = NULL;
  1745. mutex_lock(&mgr->lock);
  1746. if (mgr->mst_primary) {
  1747. rport = drm_dp_mst_topology_get_port_validated_locked(
  1748. mgr->mst_primary, port);
  1749. if (rport && !drm_dp_mst_topology_try_get_port(rport))
  1750. rport = NULL;
  1751. }
  1752. mutex_unlock(&mgr->lock);
  1753. return rport;
  1754. }
  1755. static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
  1756. {
  1757. struct drm_dp_mst_port *port;
  1758. int ret;
  1759. list_for_each_entry(port, &mstb->ports, next) {
  1760. if (port->port_num == port_num) {
  1761. ret = drm_dp_mst_topology_try_get_port(port);
  1762. return ret ? port : NULL;
  1763. }
  1764. }
  1765. return NULL;
  1766. }
  1767. /*
  1768. * calculate a new RAD for this MST branch device
  1769. * if parent has an LCT of 2 then it has 1 nibble of RAD,
  1770. * if parent has an LCT of 3 then it has 2 nibbles of RAD,
  1771. */
  1772. static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
  1773. u8 *rad)
  1774. {
  1775. int parent_lct = port->parent->lct;
  1776. int shift = 4;
  1777. int idx = (parent_lct - 1) / 2;
  1778. if (parent_lct > 1) {
  1779. memcpy(rad, port->parent->rad, idx + 1);
  1780. shift = (parent_lct % 2) ? 4 : 0;
  1781. } else
  1782. rad[0] = 0;
  1783. rad[idx] |= port->port_num << shift;
  1784. return parent_lct + 1;
  1785. }
  1786. static bool drm_dp_mst_is_end_device(u8 pdt, bool mcs)
  1787. {
  1788. switch (pdt) {
  1789. case DP_PEER_DEVICE_DP_LEGACY_CONV:
  1790. case DP_PEER_DEVICE_SST_SINK:
  1791. return true;
  1792. case DP_PEER_DEVICE_MST_BRANCHING:
  1793. /* For sst branch device */
  1794. if (!mcs)
  1795. return true;
  1796. return false;
  1797. }
  1798. return true;
  1799. }
  1800. static int
  1801. drm_dp_port_set_pdt(struct drm_dp_mst_port *port, u8 new_pdt,
  1802. bool new_mcs)
  1803. {
  1804. struct drm_dp_mst_topology_mgr *mgr = port->mgr;
  1805. struct drm_dp_mst_branch *mstb;
  1806. u8 rad[8], lct;
  1807. int ret = 0;
  1808. if (port->pdt == new_pdt && port->mcs == new_mcs)
  1809. return 0;
  1810. /* Teardown the old pdt, if there is one */
  1811. if (port->pdt != DP_PEER_DEVICE_NONE) {
  1812. if (drm_dp_mst_is_end_device(port->pdt, port->mcs)) {
  1813. /*
  1814. * If the new PDT would also have an i2c bus,
  1815. * don't bother with reregistering it
  1816. */
  1817. if (new_pdt != DP_PEER_DEVICE_NONE &&
  1818. drm_dp_mst_is_end_device(new_pdt, new_mcs)) {
  1819. port->pdt = new_pdt;
  1820. port->mcs = new_mcs;
  1821. return 0;
  1822. }
  1823. /* remove i2c over sideband */
  1824. drm_dp_mst_unregister_i2c_bus(port);
  1825. } else {
  1826. mutex_lock(&mgr->lock);
  1827. drm_dp_mst_topology_put_mstb(port->mstb);
  1828. port->mstb = NULL;
  1829. mutex_unlock(&mgr->lock);
  1830. }
  1831. }
  1832. port->pdt = new_pdt;
  1833. port->mcs = new_mcs;
  1834. if (port->pdt != DP_PEER_DEVICE_NONE) {
  1835. if (drm_dp_mst_is_end_device(port->pdt, port->mcs)) {
  1836. /* add i2c over sideband */
  1837. ret = drm_dp_mst_register_i2c_bus(port);
  1838. } else {
  1839. lct = drm_dp_calculate_rad(port, rad);
  1840. mstb = drm_dp_add_mst_branch_device(lct, rad);
  1841. if (!mstb) {
  1842. ret = -ENOMEM;
  1843. drm_err(mgr->dev, "Failed to create MSTB for port %p", port);
  1844. goto out;
  1845. }
  1846. mutex_lock(&mgr->lock);
  1847. port->mstb = mstb;
  1848. mstb->mgr = port->mgr;
  1849. mstb->port_parent = port;
  1850. /*
  1851. * Make sure this port's memory allocation stays
  1852. * around until its child MSTB releases it
  1853. */
  1854. drm_dp_mst_get_port_malloc(port);
  1855. mutex_unlock(&mgr->lock);
  1856. /* And make sure we send a link address for this */
  1857. ret = 1;
  1858. }
  1859. }
  1860. out:
  1861. if (ret < 0)
  1862. port->pdt = DP_PEER_DEVICE_NONE;
  1863. return ret;
  1864. }
  1865. /**
  1866. * drm_dp_mst_dpcd_read() - read a series of bytes from the DPCD via sideband
  1867. * @aux: Fake sideband AUX CH
  1868. * @offset: address of the (first) register to read
  1869. * @buffer: buffer to store the register values
  1870. * @size: number of bytes in @buffer
  1871. *
  1872. * Performs the same functionality for remote devices via
  1873. * sideband messaging as drm_dp_dpcd_read() does for local
  1874. * devices via actual AUX CH.
  1875. *
  1876. * Return: Number of bytes read, or negative error code on failure.
  1877. */
  1878. ssize_t drm_dp_mst_dpcd_read(struct drm_dp_aux *aux,
  1879. unsigned int offset, void *buffer, size_t size)
  1880. {
  1881. struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
  1882. aux);
  1883. return drm_dp_send_dpcd_read(port->mgr, port,
  1884. offset, size, buffer);
  1885. }
  1886. /**
  1887. * drm_dp_mst_dpcd_write() - write a series of bytes to the DPCD via sideband
  1888. * @aux: Fake sideband AUX CH
  1889. * @offset: address of the (first) register to write
  1890. * @buffer: buffer containing the values to write
  1891. * @size: number of bytes in @buffer
  1892. *
  1893. * Performs the same functionality for remote devices via
  1894. * sideband messaging as drm_dp_dpcd_write() does for local
  1895. * devices via actual AUX CH.
  1896. *
  1897. * Return: number of bytes written on success, negative error code on failure.
  1898. */
  1899. ssize_t drm_dp_mst_dpcd_write(struct drm_dp_aux *aux,
  1900. unsigned int offset, void *buffer, size_t size)
  1901. {
  1902. struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
  1903. aux);
  1904. return drm_dp_send_dpcd_write(port->mgr, port,
  1905. offset, size, buffer);
  1906. }
  1907. static int drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, guid_t *guid)
  1908. {
  1909. int ret = 0;
  1910. guid_copy(&mstb->guid, guid);
  1911. if (!drm_dp_validate_guid(mstb->mgr, &mstb->guid)) {
  1912. u8 buf[UUID_SIZE];
  1913. export_guid(buf, &mstb->guid);
  1914. if (mstb->port_parent) {
  1915. ret = drm_dp_send_dpcd_write(mstb->mgr,
  1916. mstb->port_parent,
  1917. DP_GUID, sizeof(buf), buf);
  1918. } else {
  1919. ret = drm_dp_dpcd_write(mstb->mgr->aux,
  1920. DP_GUID, buf, sizeof(buf));
  1921. }
  1922. }
  1923. if (ret < 16 && ret > 0)
  1924. return -EPROTO;
  1925. return ret == 16 ? 0 : ret;
  1926. }
  1927. static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
  1928. int pnum,
  1929. char *proppath,
  1930. size_t proppath_size)
  1931. {
  1932. int i;
  1933. char temp[8];
  1934. snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
  1935. for (i = 0; i < (mstb->lct - 1); i++) {
  1936. int shift = (i % 2) ? 0 : 4;
  1937. int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
  1938. snprintf(temp, sizeof(temp), "-%d", port_num);
  1939. strlcat(proppath, temp, proppath_size);
  1940. }
  1941. snprintf(temp, sizeof(temp), "-%d", pnum);
  1942. strlcat(proppath, temp, proppath_size);
  1943. }
  1944. /**
  1945. * drm_dp_mst_connector_late_register() - Late MST connector registration
  1946. * @connector: The MST connector
  1947. * @port: The MST port for this connector
  1948. *
  1949. * Helper to register the remote aux device for this MST port. Drivers should
  1950. * call this from their mst connector's late_register hook to enable MST aux
  1951. * devices.
  1952. *
  1953. * Return: 0 on success, negative error code on failure.
  1954. */
  1955. int drm_dp_mst_connector_late_register(struct drm_connector *connector,
  1956. struct drm_dp_mst_port *port)
  1957. {
  1958. drm_dbg_kms(port->mgr->dev, "registering %s remote bus for %s\n",
  1959. port->aux.name, connector->kdev->kobj.name);
  1960. port->aux.dev = connector->kdev;
  1961. return drm_dp_aux_register_devnode(&port->aux);
  1962. }
  1963. EXPORT_SYMBOL(drm_dp_mst_connector_late_register);
  1964. /**
  1965. * drm_dp_mst_connector_early_unregister() - Early MST connector unregistration
  1966. * @connector: The MST connector
  1967. * @port: The MST port for this connector
  1968. *
  1969. * Helper to unregister the remote aux device for this MST port, registered by
  1970. * drm_dp_mst_connector_late_register(). Drivers should call this from their mst
  1971. * connector's early_unregister hook.
  1972. */
  1973. void drm_dp_mst_connector_early_unregister(struct drm_connector *connector,
  1974. struct drm_dp_mst_port *port)
  1975. {
  1976. drm_dbg_kms(port->mgr->dev, "unregistering %s remote bus for %s\n",
  1977. port->aux.name, connector->kdev->kobj.name);
  1978. drm_dp_aux_unregister_devnode(&port->aux);
  1979. }
  1980. EXPORT_SYMBOL(drm_dp_mst_connector_early_unregister);
  1981. static void
  1982. drm_dp_mst_port_add_connector(struct drm_dp_mst_branch *mstb,
  1983. struct drm_dp_mst_port *port)
  1984. {
  1985. struct drm_dp_mst_topology_mgr *mgr = port->mgr;
  1986. char proppath[255];
  1987. int ret;
  1988. build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
  1989. port->connector = mgr->cbs->add_connector(mgr, port, proppath);
  1990. if (!port->connector) {
  1991. ret = -ENOMEM;
  1992. goto error;
  1993. }
  1994. if (port->pdt != DP_PEER_DEVICE_NONE &&
  1995. drm_dp_mst_is_end_device(port->pdt, port->mcs) &&
  1996. drm_dp_mst_port_is_logical(port))
  1997. port->cached_edid = drm_edid_read_ddc(port->connector,
  1998. &port->aux.ddc);
  1999. drm_connector_register(port->connector);
  2000. return;
  2001. error:
  2002. drm_err(mgr->dev, "Failed to create connector for port %p: %d\n", port, ret);
  2003. }
  2004. /*
  2005. * Drop a topology reference, and unlink the port from the in-memory topology
  2006. * layout
  2007. */
  2008. static void
  2009. drm_dp_mst_topology_unlink_port(struct drm_dp_mst_topology_mgr *mgr,
  2010. struct drm_dp_mst_port *port)
  2011. {
  2012. mutex_lock(&mgr->lock);
  2013. port->parent->num_ports--;
  2014. list_del(&port->next);
  2015. mutex_unlock(&mgr->lock);
  2016. drm_dp_mst_topology_put_port(port);
  2017. }
  2018. static struct drm_dp_mst_port *
  2019. drm_dp_mst_add_port(struct drm_device *dev,
  2020. struct drm_dp_mst_topology_mgr *mgr,
  2021. struct drm_dp_mst_branch *mstb, u8 port_number)
  2022. {
  2023. struct drm_dp_mst_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
  2024. if (!port)
  2025. return NULL;
  2026. kref_init(&port->topology_kref);
  2027. kref_init(&port->malloc_kref);
  2028. port->parent = mstb;
  2029. port->port_num = port_number;
  2030. port->mgr = mgr;
  2031. port->aux.name = "DPMST";
  2032. port->aux.dev = dev->dev;
  2033. port->aux.is_remote = true;
  2034. /* initialize the MST downstream port's AUX crc work queue */
  2035. port->aux.drm_dev = dev;
  2036. drm_dp_remote_aux_init(&port->aux);
  2037. /*
  2038. * Make sure the memory allocation for our parent branch stays
  2039. * around until our own memory allocation is released
  2040. */
  2041. drm_dp_mst_get_mstb_malloc(mstb);
  2042. return port;
  2043. }
  2044. static int
  2045. drm_dp_mst_handle_link_address_port(struct drm_dp_mst_branch *mstb,
  2046. struct drm_device *dev,
  2047. struct drm_dp_link_addr_reply_port *port_msg)
  2048. {
  2049. struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
  2050. struct drm_dp_mst_port *port;
  2051. int ret;
  2052. u8 new_pdt = DP_PEER_DEVICE_NONE;
  2053. bool new_mcs = 0;
  2054. bool created = false, send_link_addr = false, changed = false;
  2055. port = drm_dp_get_port(mstb, port_msg->port_number);
  2056. if (!port) {
  2057. port = drm_dp_mst_add_port(dev, mgr, mstb,
  2058. port_msg->port_number);
  2059. if (!port)
  2060. return -ENOMEM;
  2061. created = true;
  2062. changed = true;
  2063. } else if (!port->input && port_msg->input_port && port->connector) {
  2064. /* Since port->connector can't be changed here, we create a
  2065. * new port if input_port changes from 0 to 1
  2066. */
  2067. drm_dp_mst_topology_unlink_port(mgr, port);
  2068. drm_dp_mst_topology_put_port(port);
  2069. port = drm_dp_mst_add_port(dev, mgr, mstb,
  2070. port_msg->port_number);
  2071. if (!port)
  2072. return -ENOMEM;
  2073. changed = true;
  2074. created = true;
  2075. } else if (port->input && !port_msg->input_port) {
  2076. changed = true;
  2077. } else if (port->connector) {
  2078. /* We're updating a port that's exposed to userspace, so do it
  2079. * under lock
  2080. */
  2081. drm_modeset_lock(&mgr->base.lock, NULL);
  2082. changed = port->ddps != port_msg->ddps ||
  2083. (port->ddps &&
  2084. (port->ldps != port_msg->legacy_device_plug_status ||
  2085. port->dpcd_rev != port_msg->dpcd_revision ||
  2086. port->mcs != port_msg->mcs ||
  2087. port->pdt != port_msg->peer_device_type ||
  2088. port->num_sdp_stream_sinks !=
  2089. port_msg->num_sdp_stream_sinks));
  2090. }
  2091. port->input = port_msg->input_port;
  2092. if (!port->input)
  2093. new_pdt = port_msg->peer_device_type;
  2094. new_mcs = port_msg->mcs;
  2095. port->ddps = port_msg->ddps;
  2096. port->ldps = port_msg->legacy_device_plug_status;
  2097. port->dpcd_rev = port_msg->dpcd_revision;
  2098. port->num_sdp_streams = port_msg->num_sdp_streams;
  2099. port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
  2100. /* manage mstb port lists with mgr lock - take a reference
  2101. for this list */
  2102. if (created) {
  2103. mutex_lock(&mgr->lock);
  2104. drm_dp_mst_topology_get_port(port);
  2105. list_add(&port->next, &mstb->ports);
  2106. mstb->num_ports++;
  2107. mutex_unlock(&mgr->lock);
  2108. }
  2109. /*
  2110. * Reprobe PBN caps on both hotplug, and when re-probing the link
  2111. * for our parent mstb
  2112. */
  2113. if (port->ddps && !port->input) {
  2114. ret = drm_dp_send_enum_path_resources(mgr, mstb,
  2115. port);
  2116. if (ret == 1)
  2117. changed = true;
  2118. } else {
  2119. port->full_pbn = 0;
  2120. }
  2121. ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs);
  2122. if (ret == 1) {
  2123. send_link_addr = true;
  2124. } else if (ret < 0) {
  2125. drm_err(dev, "Failed to change PDT on port %p: %d\n", port, ret);
  2126. goto fail;
  2127. }
  2128. /*
  2129. * If this port wasn't just created, then we're reprobing because
  2130. * we're coming out of suspend. In this case, always resend the link
  2131. * address if there's an MSTB on this port
  2132. */
  2133. if (!created && port->pdt == DP_PEER_DEVICE_MST_BRANCHING &&
  2134. port->mcs)
  2135. send_link_addr = true;
  2136. if (port->connector)
  2137. drm_modeset_unlock(&mgr->base.lock);
  2138. else if (!port->input)
  2139. drm_dp_mst_port_add_connector(mstb, port);
  2140. if (send_link_addr && port->mstb) {
  2141. ret = drm_dp_send_link_address(mgr, port->mstb);
  2142. if (ret == 1) /* MSTB below us changed */
  2143. changed = true;
  2144. else if (ret < 0)
  2145. goto fail_put;
  2146. }
  2147. /* put reference to this port */
  2148. drm_dp_mst_topology_put_port(port);
  2149. return changed;
  2150. fail:
  2151. drm_dp_mst_topology_unlink_port(mgr, port);
  2152. if (port->connector)
  2153. drm_modeset_unlock(&mgr->base.lock);
  2154. fail_put:
  2155. drm_dp_mst_topology_put_port(port);
  2156. return ret;
  2157. }
  2158. static int
  2159. drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch *mstb,
  2160. struct drm_dp_connection_status_notify *conn_stat)
  2161. {
  2162. struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
  2163. struct drm_dp_mst_port *port;
  2164. int old_ddps, ret;
  2165. u8 new_pdt;
  2166. bool new_mcs;
  2167. bool dowork = false, create_connector = false;
  2168. port = drm_dp_get_port(mstb, conn_stat->port_number);
  2169. if (!port)
  2170. return 0;
  2171. if (port->connector) {
  2172. if (!port->input && conn_stat->input_port) {
  2173. /*
  2174. * We can't remove a connector from an already exposed
  2175. * port, so just throw the port out and make sure we
  2176. * reprobe the link address of it's parent MSTB
  2177. */
  2178. drm_dp_mst_topology_unlink_port(mgr, port);
  2179. mstb->link_address_sent = false;
  2180. dowork = true;
  2181. goto out;
  2182. }
  2183. /* Locking is only needed if the port's exposed to userspace */
  2184. drm_modeset_lock(&mgr->base.lock, NULL);
  2185. } else if (port->input && !conn_stat->input_port) {
  2186. create_connector = true;
  2187. /* Reprobe link address so we get num_sdp_streams */
  2188. mstb->link_address_sent = false;
  2189. dowork = true;
  2190. }
  2191. old_ddps = port->ddps;
  2192. port->input = conn_stat->input_port;
  2193. port->ldps = conn_stat->legacy_device_plug_status;
  2194. port->ddps = conn_stat->displayport_device_plug_status;
  2195. if (old_ddps != port->ddps) {
  2196. if (port->ddps && !port->input)
  2197. drm_dp_send_enum_path_resources(mgr, mstb, port);
  2198. else
  2199. port->full_pbn = 0;
  2200. }
  2201. new_pdt = port->input ? DP_PEER_DEVICE_NONE : conn_stat->peer_device_type;
  2202. new_mcs = conn_stat->message_capability_status;
  2203. ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs);
  2204. if (ret == 1) {
  2205. dowork = true;
  2206. } else if (ret < 0) {
  2207. drm_err(mgr->dev, "Failed to change PDT for port %p: %d\n", port, ret);
  2208. dowork = false;
  2209. }
  2210. if (port->connector)
  2211. drm_modeset_unlock(&mgr->base.lock);
  2212. else if (create_connector)
  2213. drm_dp_mst_port_add_connector(mstb, port);
  2214. out:
  2215. drm_dp_mst_topology_put_port(port);
  2216. return dowork;
  2217. }
  2218. static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
  2219. u8 lct, u8 *rad)
  2220. {
  2221. struct drm_dp_mst_branch *mstb;
  2222. struct drm_dp_mst_port *port;
  2223. int i, ret;
  2224. /* find the port by iterating down */
  2225. mutex_lock(&mgr->lock);
  2226. mstb = mgr->mst_primary;
  2227. if (!mstb)
  2228. goto out;
  2229. for (i = 0; i < lct - 1; i++) {
  2230. int shift = (i % 2) ? 0 : 4;
  2231. int port_num = (rad[i / 2] >> shift) & 0xf;
  2232. list_for_each_entry(port, &mstb->ports, next) {
  2233. if (port->port_num == port_num) {
  2234. mstb = port->mstb;
  2235. if (!mstb) {
  2236. drm_err(mgr->dev,
  2237. "failed to lookup MSTB with lct %d, rad %02x\n",
  2238. lct, rad[0]);
  2239. goto out;
  2240. }
  2241. break;
  2242. }
  2243. }
  2244. }
  2245. ret = drm_dp_mst_topology_try_get_mstb(mstb);
  2246. if (!ret)
  2247. mstb = NULL;
  2248. out:
  2249. mutex_unlock(&mgr->lock);
  2250. return mstb;
  2251. }
  2252. static struct drm_dp_mst_branch *
  2253. get_mst_branch_device_by_guid_helper(struct drm_dp_mst_branch *mstb,
  2254. const guid_t *guid)
  2255. {
  2256. struct drm_dp_mst_branch *found_mstb;
  2257. struct drm_dp_mst_port *port;
  2258. if (!mstb)
  2259. return NULL;
  2260. if (guid_equal(&mstb->guid, guid))
  2261. return mstb;
  2262. list_for_each_entry(port, &mstb->ports, next) {
  2263. found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
  2264. if (found_mstb)
  2265. return found_mstb;
  2266. }
  2267. return NULL;
  2268. }
  2269. static struct drm_dp_mst_branch *
  2270. drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
  2271. const guid_t *guid)
  2272. {
  2273. struct drm_dp_mst_branch *mstb;
  2274. int ret;
  2275. /* find the port by iterating down */
  2276. mutex_lock(&mgr->lock);
  2277. mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
  2278. if (mstb) {
  2279. ret = drm_dp_mst_topology_try_get_mstb(mstb);
  2280. if (!ret)
  2281. mstb = NULL;
  2282. }
  2283. mutex_unlock(&mgr->lock);
  2284. return mstb;
  2285. }
  2286. static int drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
  2287. struct drm_dp_mst_branch *mstb)
  2288. {
  2289. struct drm_dp_mst_port *port;
  2290. int ret;
  2291. bool changed = false;
  2292. if (!mstb->link_address_sent) {
  2293. ret = drm_dp_send_link_address(mgr, mstb);
  2294. if (ret == 1)
  2295. changed = true;
  2296. else if (ret < 0)
  2297. return ret;
  2298. }
  2299. list_for_each_entry(port, &mstb->ports, next) {
  2300. if (port->input || !port->ddps || !port->mstb)
  2301. continue;
  2302. ret = drm_dp_check_and_send_link_address(mgr, port->mstb);
  2303. if (ret == 1)
  2304. changed = true;
  2305. else if (ret < 0)
  2306. return ret;
  2307. }
  2308. return changed;
  2309. }
  2310. static void drm_dp_mst_link_probe_work(struct work_struct *work)
  2311. {
  2312. struct drm_dp_mst_topology_mgr *mgr =
  2313. container_of(work, struct drm_dp_mst_topology_mgr, work);
  2314. struct drm_device *dev = mgr->dev;
  2315. struct drm_dp_mst_branch *mstb;
  2316. int ret;
  2317. bool clear_payload_id_table;
  2318. mutex_lock(&mgr->probe_lock);
  2319. mutex_lock(&mgr->lock);
  2320. clear_payload_id_table = !mgr->payload_id_table_cleared;
  2321. mgr->payload_id_table_cleared = true;
  2322. mstb = mgr->mst_primary;
  2323. if (mstb) {
  2324. ret = drm_dp_mst_topology_try_get_mstb(mstb);
  2325. if (!ret)
  2326. mstb = NULL;
  2327. }
  2328. mutex_unlock(&mgr->lock);
  2329. if (!mstb) {
  2330. mutex_unlock(&mgr->probe_lock);
  2331. return;
  2332. }
  2333. /*
  2334. * Certain branch devices seem to incorrectly report an available_pbn
  2335. * of 0 on downstream sinks, even after clearing the
  2336. * DP_PAYLOAD_ALLOCATE_* registers in
  2337. * drm_dp_mst_topology_mgr_set_mst(). Namely, the CableMatters USB-C
  2338. * 2x DP hub. Sending a CLEAR_PAYLOAD_ID_TABLE message seems to make
  2339. * things work again.
  2340. */
  2341. if (clear_payload_id_table) {
  2342. drm_dbg_kms(dev, "Clearing payload ID table\n");
  2343. drm_dp_send_clear_payload_id_table(mgr, mstb);
  2344. }
  2345. ret = drm_dp_check_and_send_link_address(mgr, mstb);
  2346. drm_dp_mst_topology_put_mstb(mstb);
  2347. mutex_unlock(&mgr->probe_lock);
  2348. if (ret > 0)
  2349. drm_kms_helper_hotplug_event(dev);
  2350. }
  2351. static void drm_dp_mst_queue_probe_work(struct drm_dp_mst_topology_mgr *mgr)
  2352. {
  2353. queue_work(system_long_wq, &mgr->work);
  2354. }
  2355. static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
  2356. guid_t *guid)
  2357. {
  2358. if (!guid_is_null(guid))
  2359. return true;
  2360. guid_gen(guid);
  2361. return false;
  2362. }
  2363. static void build_dpcd_read(struct drm_dp_sideband_msg_tx *msg,
  2364. u8 port_num, u32 offset, u8 num_bytes)
  2365. {
  2366. struct drm_dp_sideband_msg_req_body req;
  2367. req.req_type = DP_REMOTE_DPCD_READ;
  2368. req.u.dpcd_read.port_number = port_num;
  2369. req.u.dpcd_read.dpcd_address = offset;
  2370. req.u.dpcd_read.num_bytes = num_bytes;
  2371. drm_dp_encode_sideband_req(&req, msg);
  2372. }
  2373. static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
  2374. bool up, u8 *msg, int len)
  2375. {
  2376. int ret;
  2377. int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
  2378. int tosend, total, offset;
  2379. int retries = 0;
  2380. retry:
  2381. total = len;
  2382. offset = 0;
  2383. do {
  2384. tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
  2385. ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
  2386. &msg[offset],
  2387. tosend);
  2388. if (ret != tosend) {
  2389. if (ret == -EIO && retries < 5) {
  2390. retries++;
  2391. goto retry;
  2392. }
  2393. drm_dbg_kms(mgr->dev, "failed to dpcd write %d %d\n", tosend, ret);
  2394. return -EIO;
  2395. }
  2396. offset += tosend;
  2397. total -= tosend;
  2398. } while (total > 0);
  2399. return 0;
  2400. }
  2401. static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
  2402. struct drm_dp_sideband_msg_tx *txmsg)
  2403. {
  2404. struct drm_dp_mst_branch *mstb = txmsg->dst;
  2405. u8 req_type;
  2406. req_type = txmsg->msg[0] & 0x7f;
  2407. if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
  2408. req_type == DP_RESOURCE_STATUS_NOTIFY ||
  2409. req_type == DP_CLEAR_PAYLOAD_ID_TABLE)
  2410. hdr->broadcast = 1;
  2411. else
  2412. hdr->broadcast = 0;
  2413. hdr->path_msg = txmsg->path_msg;
  2414. if (hdr->broadcast) {
  2415. hdr->lct = 1;
  2416. hdr->lcr = 6;
  2417. } else {
  2418. hdr->lct = mstb->lct;
  2419. hdr->lcr = mstb->lct - 1;
  2420. }
  2421. memcpy(hdr->rad, mstb->rad, hdr->lct / 2);
  2422. return 0;
  2423. }
  2424. /*
  2425. * process a single block of the next message in the sideband queue
  2426. */
  2427. static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
  2428. struct drm_dp_sideband_msg_tx *txmsg,
  2429. bool up)
  2430. {
  2431. u8 chunk[48];
  2432. struct drm_dp_sideband_msg_hdr hdr;
  2433. int len, space, idx, tosend;
  2434. int ret;
  2435. if (txmsg->state == DRM_DP_SIDEBAND_TX_SENT)
  2436. return 0;
  2437. memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
  2438. if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED)
  2439. txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
  2440. /* make hdr from dst mst */
  2441. ret = set_hdr_from_dst_qlock(&hdr, txmsg);
  2442. if (ret < 0)
  2443. return ret;
  2444. /* amount left to send in this message */
  2445. len = txmsg->cur_len - txmsg->cur_offset;
  2446. /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
  2447. space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
  2448. tosend = min(len, space);
  2449. if (len == txmsg->cur_len)
  2450. hdr.somt = 1;
  2451. if (space >= len)
  2452. hdr.eomt = 1;
  2453. hdr.msg_len = tosend + 1;
  2454. drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
  2455. memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
  2456. /* add crc at end */
  2457. drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
  2458. idx += tosend + 1;
  2459. ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
  2460. if (ret) {
  2461. if (drm_debug_enabled(DRM_UT_DP)) {
  2462. struct drm_printer p = drm_dbg_printer(mgr->dev,
  2463. DRM_UT_DP,
  2464. DBG_PREFIX);
  2465. drm_printf(&p, "sideband msg failed to send\n");
  2466. drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
  2467. }
  2468. return ret;
  2469. }
  2470. txmsg->cur_offset += tosend;
  2471. if (txmsg->cur_offset == txmsg->cur_len) {
  2472. txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
  2473. return 1;
  2474. }
  2475. return 0;
  2476. }
  2477. static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
  2478. {
  2479. struct drm_dp_sideband_msg_tx *txmsg;
  2480. int ret;
  2481. WARN_ON(!mutex_is_locked(&mgr->qlock));
  2482. /* construct a chunk from the first msg in the tx_msg queue */
  2483. if (list_empty(&mgr->tx_msg_downq))
  2484. return;
  2485. txmsg = list_first_entry(&mgr->tx_msg_downq,
  2486. struct drm_dp_sideband_msg_tx, next);
  2487. ret = process_single_tx_qlock(mgr, txmsg, false);
  2488. if (ret < 0) {
  2489. drm_dbg_kms(mgr->dev, "failed to send msg in q %d\n", ret);
  2490. list_del(&txmsg->next);
  2491. txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
  2492. wake_up_all(&mgr->tx_waitq);
  2493. }
  2494. }
  2495. static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
  2496. struct drm_dp_sideband_msg_tx *txmsg)
  2497. {
  2498. mutex_lock(&mgr->qlock);
  2499. list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
  2500. if (drm_debug_enabled(DRM_UT_DP)) {
  2501. struct drm_printer p = drm_dbg_printer(mgr->dev, DRM_UT_DP,
  2502. DBG_PREFIX);
  2503. drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
  2504. }
  2505. if (list_is_singular(&mgr->tx_msg_downq))
  2506. process_single_down_tx_qlock(mgr);
  2507. mutex_unlock(&mgr->qlock);
  2508. }
  2509. static void
  2510. drm_dp_dump_link_address(const struct drm_dp_mst_topology_mgr *mgr,
  2511. struct drm_dp_link_address_ack_reply *reply)
  2512. {
  2513. struct drm_dp_link_addr_reply_port *port_reply;
  2514. int i;
  2515. for (i = 0; i < reply->nports; i++) {
  2516. port_reply = &reply->ports[i];
  2517. drm_dbg_kms(mgr->dev,
  2518. "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n",
  2519. i,
  2520. port_reply->input_port,
  2521. port_reply->peer_device_type,
  2522. port_reply->port_number,
  2523. port_reply->dpcd_revision,
  2524. port_reply->mcs,
  2525. port_reply->ddps,
  2526. port_reply->legacy_device_plug_status,
  2527. port_reply->num_sdp_streams,
  2528. port_reply->num_sdp_stream_sinks);
  2529. }
  2530. }
  2531. static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
  2532. struct drm_dp_mst_branch *mstb)
  2533. {
  2534. struct drm_dp_sideband_msg_tx *txmsg;
  2535. struct drm_dp_link_address_ack_reply *reply;
  2536. struct drm_dp_mst_port *port, *tmp;
  2537. int i, ret, port_mask = 0;
  2538. bool changed = false;
  2539. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  2540. if (!txmsg)
  2541. return -ENOMEM;
  2542. txmsg->dst = mstb;
  2543. build_link_address(txmsg);
  2544. mstb->link_address_sent = true;
  2545. drm_dp_queue_down_tx(mgr, txmsg);
  2546. /* FIXME: Actually do some real error handling here */
  2547. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  2548. if (ret < 0) {
  2549. drm_err(mgr->dev, "Sending link address failed with %d\n", ret);
  2550. goto out;
  2551. }
  2552. if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
  2553. drm_err(mgr->dev, "link address NAK received\n");
  2554. ret = -EIO;
  2555. goto out;
  2556. }
  2557. reply = &txmsg->reply.u.link_addr;
  2558. drm_dbg_kms(mgr->dev, "link address reply: %d\n", reply->nports);
  2559. drm_dp_dump_link_address(mgr, reply);
  2560. ret = drm_dp_check_mstb_guid(mstb, &reply->guid);
  2561. if (ret) {
  2562. char buf[64];
  2563. drm_dp_mst_rad_to_str(mstb->rad, mstb->lct, buf, sizeof(buf));
  2564. drm_err(mgr->dev, "GUID check on %s failed: %d\n", buf, ret);
  2565. goto out;
  2566. }
  2567. for (i = 0; i < reply->nports; i++) {
  2568. port_mask |= BIT(reply->ports[i].port_number);
  2569. ret = drm_dp_mst_handle_link_address_port(mstb, mgr->dev,
  2570. &reply->ports[i]);
  2571. if (ret == 1)
  2572. changed = true;
  2573. else if (ret < 0)
  2574. goto out;
  2575. }
  2576. /* Prune any ports that are currently a part of mstb in our in-memory
  2577. * topology, but were not seen in this link address. Usually this
  2578. * means that they were removed while the topology was out of sync,
  2579. * e.g. during suspend/resume
  2580. */
  2581. mutex_lock(&mgr->lock);
  2582. list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
  2583. if (port_mask & BIT(port->port_num))
  2584. continue;
  2585. drm_dbg_kms(mgr->dev, "port %d was not in link address, removing\n",
  2586. port->port_num);
  2587. list_del(&port->next);
  2588. drm_dp_mst_topology_put_port(port);
  2589. changed = true;
  2590. }
  2591. mutex_unlock(&mgr->lock);
  2592. out:
  2593. if (ret < 0)
  2594. mstb->link_address_sent = false;
  2595. kfree(txmsg);
  2596. return ret < 0 ? ret : changed;
  2597. }
  2598. static void
  2599. drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
  2600. struct drm_dp_mst_branch *mstb)
  2601. {
  2602. struct drm_dp_sideband_msg_tx *txmsg;
  2603. int ret;
  2604. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  2605. if (!txmsg)
  2606. return;
  2607. txmsg->dst = mstb;
  2608. build_clear_payload_id_table(txmsg);
  2609. drm_dp_queue_down_tx(mgr, txmsg);
  2610. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  2611. if (ret > 0 && txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
  2612. drm_dbg_kms(mgr->dev, "clear payload table id nak received\n");
  2613. kfree(txmsg);
  2614. }
  2615. static int
  2616. drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
  2617. struct drm_dp_mst_branch *mstb,
  2618. struct drm_dp_mst_port *port)
  2619. {
  2620. struct drm_dp_enum_path_resources_ack_reply *path_res;
  2621. struct drm_dp_sideband_msg_tx *txmsg;
  2622. int ret;
  2623. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  2624. if (!txmsg)
  2625. return -ENOMEM;
  2626. txmsg->dst = mstb;
  2627. build_enum_path_resources(txmsg, port->port_num);
  2628. drm_dp_queue_down_tx(mgr, txmsg);
  2629. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  2630. if (ret > 0) {
  2631. ret = 0;
  2632. path_res = &txmsg->reply.u.path_resources;
  2633. if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
  2634. drm_dbg_kms(mgr->dev, "enum path resources nak received\n");
  2635. } else {
  2636. if (port->port_num != path_res->port_number)
  2637. DRM_ERROR("got incorrect port in response\n");
  2638. drm_dbg_kms(mgr->dev, "enum path resources %d: %d %d\n",
  2639. path_res->port_number,
  2640. path_res->full_payload_bw_number,
  2641. path_res->avail_payload_bw_number);
  2642. /*
  2643. * If something changed, make sure we send a
  2644. * hotplug
  2645. */
  2646. if (port->full_pbn != path_res->full_payload_bw_number ||
  2647. port->fec_capable != path_res->fec_capable)
  2648. ret = 1;
  2649. port->full_pbn = path_res->full_payload_bw_number;
  2650. port->fec_capable = path_res->fec_capable;
  2651. }
  2652. }
  2653. kfree(txmsg);
  2654. return ret;
  2655. }
  2656. static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
  2657. {
  2658. if (!mstb->port_parent)
  2659. return NULL;
  2660. if (mstb->port_parent->mstb != mstb)
  2661. return mstb->port_parent;
  2662. return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
  2663. }
  2664. /*
  2665. * Searches upwards in the topology starting from mstb to try to find the
  2666. * closest available parent of mstb that's still connected to the rest of the
  2667. * topology. This can be used in order to perform operations like releasing
  2668. * payloads, where the branch device which owned the payload may no longer be
  2669. * around and thus would require that the payload on the last living relative
  2670. * be freed instead.
  2671. */
  2672. static struct drm_dp_mst_branch *
  2673. drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
  2674. struct drm_dp_mst_branch *mstb,
  2675. int *port_num)
  2676. {
  2677. struct drm_dp_mst_branch *rmstb = NULL;
  2678. struct drm_dp_mst_port *found_port;
  2679. mutex_lock(&mgr->lock);
  2680. if (!mgr->mst_primary)
  2681. goto out;
  2682. do {
  2683. found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
  2684. if (!found_port)
  2685. break;
  2686. if (drm_dp_mst_topology_try_get_mstb(found_port->parent)) {
  2687. rmstb = found_port->parent;
  2688. *port_num = found_port->port_num;
  2689. } else {
  2690. /* Search again, starting from this parent */
  2691. mstb = found_port->parent;
  2692. }
  2693. } while (!rmstb);
  2694. out:
  2695. mutex_unlock(&mgr->lock);
  2696. return rmstb;
  2697. }
  2698. static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
  2699. struct drm_dp_mst_port *port,
  2700. int id,
  2701. int pbn)
  2702. {
  2703. struct drm_dp_sideband_msg_tx *txmsg;
  2704. struct drm_dp_mst_branch *mstb;
  2705. int ret, port_num;
  2706. u8 sinks[DRM_DP_MAX_SDP_STREAMS];
  2707. int i;
  2708. port_num = port->port_num;
  2709. mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
  2710. if (!mstb) {
  2711. mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
  2712. port->parent,
  2713. &port_num);
  2714. if (!mstb)
  2715. return -EINVAL;
  2716. }
  2717. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  2718. if (!txmsg) {
  2719. ret = -ENOMEM;
  2720. goto fail_put;
  2721. }
  2722. for (i = 0; i < port->num_sdp_streams; i++)
  2723. sinks[i] = i;
  2724. txmsg->dst = mstb;
  2725. build_allocate_payload(txmsg, port_num,
  2726. id,
  2727. pbn, port->num_sdp_streams, sinks);
  2728. drm_dp_queue_down_tx(mgr, txmsg);
  2729. /*
  2730. * FIXME: there is a small chance that between getting the last
  2731. * connected mstb and sending the payload message, the last connected
  2732. * mstb could also be removed from the topology. In the future, this
  2733. * needs to be fixed by restarting the
  2734. * drm_dp_get_last_connected_port_and_mstb() search in the event of a
  2735. * timeout if the topology is still connected to the system.
  2736. */
  2737. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  2738. if (ret > 0) {
  2739. if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
  2740. ret = -EINVAL;
  2741. else
  2742. ret = 0;
  2743. }
  2744. kfree(txmsg);
  2745. fail_put:
  2746. drm_dp_mst_topology_put_mstb(mstb);
  2747. return ret;
  2748. }
  2749. int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
  2750. struct drm_dp_mst_port *port, bool power_up)
  2751. {
  2752. struct drm_dp_sideband_msg_tx *txmsg;
  2753. int ret;
  2754. port = drm_dp_mst_topology_get_port_validated(mgr, port);
  2755. if (!port)
  2756. return -EINVAL;
  2757. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  2758. if (!txmsg) {
  2759. drm_dp_mst_topology_put_port(port);
  2760. return -ENOMEM;
  2761. }
  2762. txmsg->dst = port->parent;
  2763. build_power_updown_phy(txmsg, port->port_num, power_up);
  2764. drm_dp_queue_down_tx(mgr, txmsg);
  2765. ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
  2766. if (ret > 0) {
  2767. if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
  2768. ret = -EINVAL;
  2769. else
  2770. ret = 0;
  2771. }
  2772. kfree(txmsg);
  2773. drm_dp_mst_topology_put_port(port);
  2774. return ret;
  2775. }
  2776. EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
  2777. int drm_dp_send_query_stream_enc_status(struct drm_dp_mst_topology_mgr *mgr,
  2778. struct drm_dp_mst_port *port,
  2779. struct drm_dp_query_stream_enc_status_ack_reply *status)
  2780. {
  2781. struct drm_dp_mst_topology_state *state;
  2782. struct drm_dp_mst_atomic_payload *payload;
  2783. struct drm_dp_sideband_msg_tx *txmsg;
  2784. u8 nonce[7];
  2785. int ret;
  2786. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  2787. if (!txmsg)
  2788. return -ENOMEM;
  2789. port = drm_dp_mst_topology_get_port_validated(mgr, port);
  2790. if (!port) {
  2791. ret = -EINVAL;
  2792. goto out_get_port;
  2793. }
  2794. get_random_bytes(nonce, sizeof(nonce));
  2795. drm_modeset_lock(&mgr->base.lock, NULL);
  2796. state = to_drm_dp_mst_topology_state(mgr->base.state);
  2797. payload = drm_atomic_get_mst_payload_state(state, port);
  2798. /*
  2799. * "Source device targets the QUERY_STREAM_ENCRYPTION_STATUS message
  2800. * transaction at the MST Branch device directly connected to the
  2801. * Source"
  2802. */
  2803. txmsg->dst = mgr->mst_primary;
  2804. build_query_stream_enc_status(txmsg, payload->vcpi, nonce);
  2805. drm_dp_queue_down_tx(mgr, txmsg);
  2806. ret = drm_dp_mst_wait_tx_reply(mgr->mst_primary, txmsg);
  2807. if (ret < 0) {
  2808. goto out;
  2809. } else if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
  2810. drm_dbg_kms(mgr->dev, "query encryption status nak received\n");
  2811. ret = -ENXIO;
  2812. goto out;
  2813. }
  2814. ret = 0;
  2815. memcpy(status, &txmsg->reply.u.enc_status, sizeof(*status));
  2816. out:
  2817. drm_modeset_unlock(&mgr->base.lock);
  2818. drm_dp_mst_topology_put_port(port);
  2819. out_get_port:
  2820. kfree(txmsg);
  2821. return ret;
  2822. }
  2823. EXPORT_SYMBOL(drm_dp_send_query_stream_enc_status);
  2824. static int drm_dp_create_payload_at_dfp(struct drm_dp_mst_topology_mgr *mgr,
  2825. struct drm_dp_mst_atomic_payload *payload)
  2826. {
  2827. return drm_dp_dpcd_write_payload(mgr, payload->vcpi, payload->vc_start_slot,
  2828. payload->time_slots);
  2829. }
  2830. static int drm_dp_create_payload_to_remote(struct drm_dp_mst_topology_mgr *mgr,
  2831. struct drm_dp_mst_atomic_payload *payload)
  2832. {
  2833. int ret;
  2834. struct drm_dp_mst_port *port = drm_dp_mst_topology_get_port_validated(mgr, payload->port);
  2835. if (!port)
  2836. return -EIO;
  2837. ret = drm_dp_payload_send_msg(mgr, port, payload->vcpi, payload->pbn);
  2838. drm_dp_mst_topology_put_port(port);
  2839. return ret;
  2840. }
  2841. static void drm_dp_destroy_payload_at_remote_and_dfp(struct drm_dp_mst_topology_mgr *mgr,
  2842. struct drm_dp_mst_topology_state *mst_state,
  2843. struct drm_dp_mst_atomic_payload *payload)
  2844. {
  2845. drm_dbg_kms(mgr->dev, "\n");
  2846. /* it's okay for these to fail */
  2847. if (payload->payload_allocation_status == DRM_DP_MST_PAYLOAD_ALLOCATION_REMOTE) {
  2848. drm_dp_payload_send_msg(mgr, payload->port, payload->vcpi, 0);
  2849. payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_DFP;
  2850. }
  2851. if (payload->payload_allocation_status == DRM_DP_MST_PAYLOAD_ALLOCATION_DFP)
  2852. drm_dp_dpcd_write_payload(mgr, payload->vcpi, payload->vc_start_slot, 0);
  2853. }
  2854. /**
  2855. * drm_dp_add_payload_part1() - Execute payload update part 1
  2856. * @mgr: Manager to use.
  2857. * @mst_state: The MST atomic state
  2858. * @payload: The payload to write
  2859. *
  2860. * Determines the starting time slot for the given payload, and programs the VCPI for this payload
  2861. * into the DPCD of DPRX. After calling this, the driver should generate ACT and payload packets.
  2862. *
  2863. * Returns: 0 on success, error code on failure.
  2864. */
  2865. int drm_dp_add_payload_part1(struct drm_dp_mst_topology_mgr *mgr,
  2866. struct drm_dp_mst_topology_state *mst_state,
  2867. struct drm_dp_mst_atomic_payload *payload)
  2868. {
  2869. struct drm_dp_mst_port *port;
  2870. int ret;
  2871. /* Update mst mgr info */
  2872. if (mgr->payload_count == 0)
  2873. mgr->next_start_slot = mst_state->start_slot;
  2874. payload->vc_start_slot = mgr->next_start_slot;
  2875. mgr->payload_count++;
  2876. mgr->next_start_slot += payload->time_slots;
  2877. payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_LOCAL;
  2878. /* Allocate payload to immediate downstream facing port */
  2879. port = drm_dp_mst_topology_get_port_validated(mgr, payload->port);
  2880. if (!port) {
  2881. drm_dbg_kms(mgr->dev,
  2882. "VCPI %d for port %p not in topology, not creating a payload to remote\n",
  2883. payload->vcpi, payload->port);
  2884. return -EIO;
  2885. }
  2886. ret = drm_dp_create_payload_at_dfp(mgr, payload);
  2887. if (ret < 0) {
  2888. drm_dbg_kms(mgr->dev, "Failed to create MST payload for port %p: %d\n",
  2889. payload->port, ret);
  2890. goto put_port;
  2891. }
  2892. payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_DFP;
  2893. put_port:
  2894. drm_dp_mst_topology_put_port(port);
  2895. return ret;
  2896. }
  2897. EXPORT_SYMBOL(drm_dp_add_payload_part1);
  2898. /**
  2899. * drm_dp_remove_payload_part1() - Remove an MST payload along the virtual channel
  2900. * @mgr: Manager to use.
  2901. * @mst_state: The MST atomic state
  2902. * @payload: The payload to remove
  2903. *
  2904. * Removes a payload along the virtual channel if it was successfully allocated.
  2905. * After calling this, the driver should set HW to generate ACT and then switch to new
  2906. * payload allocation state.
  2907. */
  2908. void drm_dp_remove_payload_part1(struct drm_dp_mst_topology_mgr *mgr,
  2909. struct drm_dp_mst_topology_state *mst_state,
  2910. struct drm_dp_mst_atomic_payload *payload)
  2911. {
  2912. /* Remove remote payload allocation */
  2913. bool send_remove = false;
  2914. mutex_lock(&mgr->lock);
  2915. send_remove = drm_dp_mst_port_downstream_of_branch(payload->port, mgr->mst_primary);
  2916. mutex_unlock(&mgr->lock);
  2917. if (send_remove)
  2918. drm_dp_destroy_payload_at_remote_and_dfp(mgr, mst_state, payload);
  2919. else
  2920. drm_dbg_kms(mgr->dev, "Payload for VCPI %d not in topology, not sending remove\n",
  2921. payload->vcpi);
  2922. payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_LOCAL;
  2923. }
  2924. EXPORT_SYMBOL(drm_dp_remove_payload_part1);
  2925. /**
  2926. * drm_dp_remove_payload_part2() - Remove an MST payload locally
  2927. * @mgr: Manager to use.
  2928. * @mst_state: The MST atomic state
  2929. * @old_payload: The payload with its old state
  2930. * @new_payload: The payload with its latest state
  2931. *
  2932. * Updates the starting time slots of all other payloads which would have been shifted towards
  2933. * the start of the payload ID table as a result of removing a payload. Driver should call this
  2934. * function whenever it removes a payload in its HW. It's independent to the result of payload
  2935. * allocation/deallocation at branch devices along the virtual channel.
  2936. */
  2937. void drm_dp_remove_payload_part2(struct drm_dp_mst_topology_mgr *mgr,
  2938. struct drm_dp_mst_topology_state *mst_state,
  2939. const struct drm_dp_mst_atomic_payload *old_payload,
  2940. struct drm_dp_mst_atomic_payload *new_payload)
  2941. {
  2942. struct drm_dp_mst_atomic_payload *pos;
  2943. /* Remove local payload allocation */
  2944. list_for_each_entry(pos, &mst_state->payloads, next) {
  2945. if (pos != new_payload && pos->vc_start_slot > new_payload->vc_start_slot)
  2946. pos->vc_start_slot -= old_payload->time_slots;
  2947. }
  2948. new_payload->vc_start_slot = -1;
  2949. mgr->payload_count--;
  2950. mgr->next_start_slot -= old_payload->time_slots;
  2951. if (new_payload->delete)
  2952. drm_dp_mst_put_port_malloc(new_payload->port);
  2953. new_payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_NONE;
  2954. }
  2955. EXPORT_SYMBOL(drm_dp_remove_payload_part2);
  2956. /**
  2957. * drm_dp_add_payload_part2() - Execute payload update part 2
  2958. * @mgr: Manager to use.
  2959. * @payload: The payload to update
  2960. *
  2961. * If @payload was successfully assigned a starting time slot by drm_dp_add_payload_part1(), this
  2962. * function will send the sideband messages to finish allocating this payload.
  2963. *
  2964. * Returns: 0 on success, negative error code on failure.
  2965. */
  2966. int drm_dp_add_payload_part2(struct drm_dp_mst_topology_mgr *mgr,
  2967. struct drm_dp_mst_atomic_payload *payload)
  2968. {
  2969. int ret = 0;
  2970. /* Skip failed payloads */
  2971. if (payload->payload_allocation_status != DRM_DP_MST_PAYLOAD_ALLOCATION_DFP) {
  2972. drm_dbg_kms(mgr->dev, "Part 1 of payload creation for %s failed, skipping part 2\n",
  2973. payload->port->connector->name);
  2974. return -EIO;
  2975. }
  2976. /* Allocate payload to remote end */
  2977. ret = drm_dp_create_payload_to_remote(mgr, payload);
  2978. if (ret < 0)
  2979. drm_err(mgr->dev, "Step 2 of creating MST payload for %p failed: %d\n",
  2980. payload->port, ret);
  2981. else
  2982. payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_REMOTE;
  2983. return ret;
  2984. }
  2985. EXPORT_SYMBOL(drm_dp_add_payload_part2);
  2986. static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
  2987. struct drm_dp_mst_port *port,
  2988. int offset, int size, u8 *bytes)
  2989. {
  2990. int ret = 0;
  2991. struct drm_dp_sideband_msg_tx *txmsg;
  2992. struct drm_dp_mst_branch *mstb;
  2993. mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
  2994. if (!mstb)
  2995. return -EINVAL;
  2996. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  2997. if (!txmsg) {
  2998. ret = -ENOMEM;
  2999. goto fail_put;
  3000. }
  3001. build_dpcd_read(txmsg, port->port_num, offset, size);
  3002. txmsg->dst = port->parent;
  3003. drm_dp_queue_down_tx(mgr, txmsg);
  3004. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  3005. if (ret < 0)
  3006. goto fail_free;
  3007. if (txmsg->reply.reply_type == 1) {
  3008. drm_dbg_kms(mgr->dev, "mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n",
  3009. mstb, port->port_num, offset, size);
  3010. ret = -EIO;
  3011. goto fail_free;
  3012. }
  3013. if (txmsg->reply.u.remote_dpcd_read_ack.num_bytes != size) {
  3014. ret = -EPROTO;
  3015. goto fail_free;
  3016. }
  3017. ret = min_t(size_t, txmsg->reply.u.remote_dpcd_read_ack.num_bytes,
  3018. size);
  3019. memcpy(bytes, txmsg->reply.u.remote_dpcd_read_ack.bytes, ret);
  3020. fail_free:
  3021. kfree(txmsg);
  3022. fail_put:
  3023. drm_dp_mst_topology_put_mstb(mstb);
  3024. return ret;
  3025. }
  3026. static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
  3027. struct drm_dp_mst_port *port,
  3028. int offset, int size, u8 *bytes)
  3029. {
  3030. int ret;
  3031. struct drm_dp_sideband_msg_tx *txmsg;
  3032. struct drm_dp_mst_branch *mstb;
  3033. mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
  3034. if (!mstb)
  3035. return -EINVAL;
  3036. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  3037. if (!txmsg) {
  3038. ret = -ENOMEM;
  3039. goto fail_put;
  3040. }
  3041. build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
  3042. txmsg->dst = mstb;
  3043. drm_dp_queue_down_tx(mgr, txmsg);
  3044. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  3045. if (ret > 0) {
  3046. if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
  3047. ret = -EIO;
  3048. else
  3049. ret = size;
  3050. }
  3051. kfree(txmsg);
  3052. fail_put:
  3053. drm_dp_mst_topology_put_mstb(mstb);
  3054. return ret;
  3055. }
  3056. static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
  3057. {
  3058. struct drm_dp_sideband_msg_reply_body reply;
  3059. reply.reply_type = DP_SIDEBAND_REPLY_ACK;
  3060. reply.req_type = req_type;
  3061. drm_dp_encode_sideband_reply(&reply, msg);
  3062. return 0;
  3063. }
  3064. static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
  3065. struct drm_dp_mst_branch *mstb,
  3066. int req_type, bool broadcast)
  3067. {
  3068. struct drm_dp_sideband_msg_tx *txmsg;
  3069. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  3070. if (!txmsg)
  3071. return -ENOMEM;
  3072. txmsg->dst = mstb;
  3073. drm_dp_encode_up_ack_reply(txmsg, req_type);
  3074. mutex_lock(&mgr->qlock);
  3075. /* construct a chunk from the first msg in the tx_msg queue */
  3076. process_single_tx_qlock(mgr, txmsg, true);
  3077. mutex_unlock(&mgr->qlock);
  3078. kfree(txmsg);
  3079. return 0;
  3080. }
  3081. /**
  3082. * drm_dp_get_vc_payload_bw - get the VC payload BW for an MST link
  3083. * @mgr: The &drm_dp_mst_topology_mgr to use
  3084. * @link_rate: link rate in 10kbits/s units
  3085. * @link_lane_count: lane count
  3086. *
  3087. * Calculate the total bandwidth of a MultiStream Transport link. The returned
  3088. * value is in units of PBNs/(timeslots/1 MTP). This value can be used to
  3089. * convert the number of PBNs required for a given stream to the number of
  3090. * timeslots this stream requires in each MTP.
  3091. *
  3092. * Returns the BW / timeslot value in 20.12 fixed point format.
  3093. */
  3094. fixed20_12 drm_dp_get_vc_payload_bw(const struct drm_dp_mst_topology_mgr *mgr,
  3095. int link_rate, int link_lane_count)
  3096. {
  3097. int ch_coding_efficiency =
  3098. drm_dp_bw_channel_coding_efficiency(drm_dp_is_uhbr_rate(link_rate));
  3099. fixed20_12 ret;
  3100. if (link_rate == 0 || link_lane_count == 0)
  3101. drm_dbg_kms(mgr->dev, "invalid link rate/lane count: (%d / %d)\n",
  3102. link_rate, link_lane_count);
  3103. /* See DP v2.0 2.6.4.2, 2.7.6.3 VCPayload_Bandwidth_for_OneTimeSlotPer_MTP_Allocation */
  3104. ret.full = DIV_ROUND_DOWN_ULL(mul_u32_u32(link_rate * link_lane_count,
  3105. ch_coding_efficiency),
  3106. (1000000ULL * 8 * 5400) >> 12);
  3107. return ret;
  3108. }
  3109. EXPORT_SYMBOL(drm_dp_get_vc_payload_bw);
  3110. /**
  3111. * drm_dp_read_mst_cap() - Read the sink's MST mode capability
  3112. * @aux: The DP AUX channel to use
  3113. * @dpcd: A cached copy of the DPCD capabilities for this sink
  3114. *
  3115. * Returns: enum drm_dp_mst_mode to indicate MST mode capability
  3116. */
  3117. enum drm_dp_mst_mode drm_dp_read_mst_cap(struct drm_dp_aux *aux,
  3118. const u8 dpcd[DP_RECEIVER_CAP_SIZE])
  3119. {
  3120. u8 mstm_cap;
  3121. if (dpcd[DP_DPCD_REV] < DP_DPCD_REV_12)
  3122. return DRM_DP_SST;
  3123. if (drm_dp_dpcd_readb(aux, DP_MSTM_CAP, &mstm_cap) != 1)
  3124. return DRM_DP_SST;
  3125. if (mstm_cap & DP_MST_CAP)
  3126. return DRM_DP_MST;
  3127. if (mstm_cap & DP_SINGLE_STREAM_SIDEBAND_MSG)
  3128. return DRM_DP_SST_SIDEBAND_MSG;
  3129. return DRM_DP_SST;
  3130. }
  3131. EXPORT_SYMBOL(drm_dp_read_mst_cap);
  3132. /**
  3133. * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
  3134. * @mgr: manager to set state for
  3135. * @mst_state: true to enable MST on this connector - false to disable.
  3136. *
  3137. * This is called by the driver when it detects an MST capable device plugged
  3138. * into a DP MST capable port, or when a DP MST capable device is unplugged.
  3139. */
  3140. int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
  3141. {
  3142. int ret = 0;
  3143. struct drm_dp_mst_branch *mstb = NULL;
  3144. mutex_lock(&mgr->lock);
  3145. if (mst_state == mgr->mst_state)
  3146. goto out_unlock;
  3147. mgr->mst_state = mst_state;
  3148. /* set the device into MST mode */
  3149. if (mst_state) {
  3150. WARN_ON(mgr->mst_primary);
  3151. /* get dpcd info */
  3152. ret = drm_dp_read_dpcd_caps(mgr->aux, mgr->dpcd);
  3153. if (ret < 0) {
  3154. drm_dbg_kms(mgr->dev, "%s: failed to read DPCD, ret %d\n",
  3155. mgr->aux->name, ret);
  3156. goto out_unlock;
  3157. }
  3158. /* add initial branch device at LCT 1 */
  3159. mstb = drm_dp_add_mst_branch_device(1, NULL);
  3160. if (mstb == NULL) {
  3161. ret = -ENOMEM;
  3162. goto out_unlock;
  3163. }
  3164. mstb->mgr = mgr;
  3165. /* give this the main reference */
  3166. mgr->mst_primary = mstb;
  3167. drm_dp_mst_topology_get_mstb(mgr->mst_primary);
  3168. ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
  3169. DP_MST_EN |
  3170. DP_UP_REQ_EN |
  3171. DP_UPSTREAM_IS_SRC);
  3172. if (ret < 0)
  3173. goto out_unlock;
  3174. /* Write reset payload */
  3175. drm_dp_dpcd_write_payload(mgr, 0, 0, 0x3f);
  3176. drm_dp_mst_queue_probe_work(mgr);
  3177. ret = 0;
  3178. } else {
  3179. /* disable MST on the device */
  3180. mstb = mgr->mst_primary;
  3181. mgr->mst_primary = NULL;
  3182. /* this can fail if the device is gone */
  3183. drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
  3184. ret = 0;
  3185. mgr->payload_id_table_cleared = false;
  3186. mgr->reset_rx_state = true;
  3187. }
  3188. out_unlock:
  3189. mutex_unlock(&mgr->lock);
  3190. if (mstb)
  3191. drm_dp_mst_topology_put_mstb(mstb);
  3192. return ret;
  3193. }
  3194. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
  3195. static void
  3196. drm_dp_mst_topology_mgr_invalidate_mstb(struct drm_dp_mst_branch *mstb)
  3197. {
  3198. struct drm_dp_mst_port *port;
  3199. /* The link address will need to be re-sent on resume */
  3200. mstb->link_address_sent = false;
  3201. list_for_each_entry(port, &mstb->ports, next)
  3202. if (port->mstb)
  3203. drm_dp_mst_topology_mgr_invalidate_mstb(port->mstb);
  3204. }
  3205. /**
  3206. * drm_dp_mst_topology_queue_probe - Queue a topology probe
  3207. * @mgr: manager to probe
  3208. *
  3209. * Queue a work to probe the MST topology. Driver's should call this only to
  3210. * sync the topology's HW->SW state after the MST link's parameters have
  3211. * changed in a way the state could've become out-of-sync. This is the case
  3212. * for instance when the link rate between the source and first downstream
  3213. * branch device has switched between UHBR and non-UHBR rates. Except of those
  3214. * cases - for instance when a sink gets plugged/unplugged to a port - the SW
  3215. * state will get updated automatically via MST UP message notifications.
  3216. */
  3217. void drm_dp_mst_topology_queue_probe(struct drm_dp_mst_topology_mgr *mgr)
  3218. {
  3219. mutex_lock(&mgr->lock);
  3220. if (drm_WARN_ON(mgr->dev, !mgr->mst_state || !mgr->mst_primary))
  3221. goto out_unlock;
  3222. drm_dp_mst_topology_mgr_invalidate_mstb(mgr->mst_primary);
  3223. drm_dp_mst_queue_probe_work(mgr);
  3224. out_unlock:
  3225. mutex_unlock(&mgr->lock);
  3226. }
  3227. EXPORT_SYMBOL(drm_dp_mst_topology_queue_probe);
  3228. /**
  3229. * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
  3230. * @mgr: manager to suspend
  3231. *
  3232. * This function tells the MST device that we can't handle UP messages
  3233. * anymore. This should stop it from sending any since we are suspended.
  3234. */
  3235. void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
  3236. {
  3237. mutex_lock(&mgr->lock);
  3238. drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
  3239. DP_MST_EN | DP_UPSTREAM_IS_SRC);
  3240. mutex_unlock(&mgr->lock);
  3241. flush_work(&mgr->up_req_work);
  3242. flush_work(&mgr->work);
  3243. flush_work(&mgr->delayed_destroy_work);
  3244. mutex_lock(&mgr->lock);
  3245. if (mgr->mst_state && mgr->mst_primary)
  3246. drm_dp_mst_topology_mgr_invalidate_mstb(mgr->mst_primary);
  3247. mutex_unlock(&mgr->lock);
  3248. }
  3249. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
  3250. /**
  3251. * drm_dp_mst_topology_mgr_resume() - resume the MST manager
  3252. * @mgr: manager to resume
  3253. * @sync: whether or not to perform topology reprobing synchronously
  3254. *
  3255. * This will fetch DPCD and see if the device is still there,
  3256. * if it is, it will rewrite the MSTM control bits, and return.
  3257. *
  3258. * If the device fails this returns -1, and the driver should do
  3259. * a full MST reprobe, in case we were undocked.
  3260. *
  3261. * During system resume (where it is assumed that the driver will be calling
  3262. * drm_atomic_helper_resume()) this function should be called beforehand with
  3263. * @sync set to true. In contexts like runtime resume where the driver is not
  3264. * expected to be calling drm_atomic_helper_resume(), this function should be
  3265. * called with @sync set to false in order to avoid deadlocking.
  3266. *
  3267. * Returns: -1 if the MST topology was removed while we were suspended, 0
  3268. * otherwise.
  3269. */
  3270. int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr,
  3271. bool sync)
  3272. {
  3273. u8 buf[UUID_SIZE];
  3274. guid_t guid;
  3275. int ret;
  3276. mutex_lock(&mgr->lock);
  3277. if (!mgr->mst_primary)
  3278. goto out_fail;
  3279. if (drm_dp_read_dpcd_caps(mgr->aux, mgr->dpcd) < 0) {
  3280. drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
  3281. goto out_fail;
  3282. }
  3283. ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
  3284. DP_MST_EN |
  3285. DP_UP_REQ_EN |
  3286. DP_UPSTREAM_IS_SRC);
  3287. if (ret < 0) {
  3288. drm_dbg_kms(mgr->dev, "mst write failed - undocked during suspend?\n");
  3289. goto out_fail;
  3290. }
  3291. /* Some hubs forget their guids after they resume */
  3292. ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, buf, sizeof(buf));
  3293. if (ret != sizeof(buf)) {
  3294. drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
  3295. goto out_fail;
  3296. }
  3297. import_guid(&guid, buf);
  3298. ret = drm_dp_check_mstb_guid(mgr->mst_primary, &guid);
  3299. if (ret) {
  3300. drm_dbg_kms(mgr->dev, "check mstb failed - undocked during suspend?\n");
  3301. goto out_fail;
  3302. }
  3303. /*
  3304. * For the final step of resuming the topology, we need to bring the
  3305. * state of our in-memory topology back into sync with reality. So,
  3306. * restart the probing process as if we're probing a new hub
  3307. */
  3308. drm_dp_mst_queue_probe_work(mgr);
  3309. mutex_unlock(&mgr->lock);
  3310. if (sync) {
  3311. drm_dbg_kms(mgr->dev,
  3312. "Waiting for link probe work to finish re-syncing topology...\n");
  3313. flush_work(&mgr->work);
  3314. }
  3315. return 0;
  3316. out_fail:
  3317. mutex_unlock(&mgr->lock);
  3318. return -1;
  3319. }
  3320. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
  3321. static void reset_msg_rx_state(struct drm_dp_sideband_msg_rx *msg)
  3322. {
  3323. memset(msg, 0, sizeof(*msg));
  3324. }
  3325. static bool
  3326. drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up,
  3327. struct drm_dp_mst_branch **mstb)
  3328. {
  3329. int len;
  3330. u8 replyblock[32];
  3331. int replylen, curreply;
  3332. int ret;
  3333. u8 hdrlen;
  3334. struct drm_dp_sideband_msg_hdr hdr;
  3335. struct drm_dp_sideband_msg_rx *msg =
  3336. up ? &mgr->up_req_recv : &mgr->down_rep_recv;
  3337. int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE :
  3338. DP_SIDEBAND_MSG_DOWN_REP_BASE;
  3339. if (!up)
  3340. *mstb = NULL;
  3341. len = min(mgr->max_dpcd_transaction_bytes, 16);
  3342. ret = drm_dp_dpcd_read(mgr->aux, basereg, replyblock, len);
  3343. if (ret != len) {
  3344. drm_dbg_kms(mgr->dev, "failed to read DPCD down rep %d %d\n", len, ret);
  3345. return false;
  3346. }
  3347. ret = drm_dp_decode_sideband_msg_hdr(mgr, &hdr, replyblock, len, &hdrlen);
  3348. if (ret == false) {
  3349. print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16,
  3350. 1, replyblock, len, false);
  3351. drm_dbg_kms(mgr->dev, "ERROR: failed header\n");
  3352. return false;
  3353. }
  3354. if (!up) {
  3355. /* Caller is responsible for giving back this reference */
  3356. *mstb = drm_dp_get_mst_branch_device(mgr, hdr.lct, hdr.rad);
  3357. if (!*mstb) {
  3358. drm_dbg_kms(mgr->dev, "Got MST reply from unknown device %d\n", hdr.lct);
  3359. return false;
  3360. }
  3361. }
  3362. if (!drm_dp_sideband_msg_set_header(msg, &hdr, hdrlen)) {
  3363. drm_dbg_kms(mgr->dev, "sideband msg set header failed %d\n", replyblock[0]);
  3364. return false;
  3365. }
  3366. replylen = min(msg->curchunk_len, (u8)(len - hdrlen));
  3367. ret = drm_dp_sideband_append_payload(msg, replyblock + hdrlen, replylen);
  3368. if (!ret) {
  3369. drm_dbg_kms(mgr->dev, "sideband msg build failed %d\n", replyblock[0]);
  3370. return false;
  3371. }
  3372. replylen = msg->curchunk_len + msg->curchunk_hdrlen - len;
  3373. curreply = len;
  3374. while (replylen > 0) {
  3375. len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
  3376. ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
  3377. replyblock, len);
  3378. if (ret != len) {
  3379. drm_dbg_kms(mgr->dev, "failed to read a chunk (len %d, ret %d)\n",
  3380. len, ret);
  3381. return false;
  3382. }
  3383. ret = drm_dp_sideband_append_payload(msg, replyblock, len);
  3384. if (!ret) {
  3385. drm_dbg_kms(mgr->dev, "failed to build sideband msg\n");
  3386. return false;
  3387. }
  3388. curreply += len;
  3389. replylen -= len;
  3390. }
  3391. return true;
  3392. }
  3393. static int get_msg_request_type(u8 data)
  3394. {
  3395. return data & 0x7f;
  3396. }
  3397. static bool verify_rx_request_type(struct drm_dp_mst_topology_mgr *mgr,
  3398. const struct drm_dp_sideband_msg_tx *txmsg,
  3399. const struct drm_dp_sideband_msg_rx *rxmsg)
  3400. {
  3401. const struct drm_dp_sideband_msg_hdr *hdr = &rxmsg->initial_hdr;
  3402. const struct drm_dp_mst_branch *mstb = txmsg->dst;
  3403. int tx_req_type = get_msg_request_type(txmsg->msg[0]);
  3404. int rx_req_type = get_msg_request_type(rxmsg->msg[0]);
  3405. char rad_str[64];
  3406. if (tx_req_type == rx_req_type)
  3407. return true;
  3408. drm_dp_mst_rad_to_str(mstb->rad, mstb->lct, rad_str, sizeof(rad_str));
  3409. drm_dbg_kms(mgr->dev,
  3410. "Got unexpected MST reply, mstb: %p seqno: %d lct: %d rad: %s rx_req_type: %s (%02x) != tx_req_type: %s (%02x)\n",
  3411. mstb, hdr->seqno, mstb->lct, rad_str,
  3412. drm_dp_mst_req_type_str(rx_req_type), rx_req_type,
  3413. drm_dp_mst_req_type_str(tx_req_type), tx_req_type);
  3414. return false;
  3415. }
  3416. static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
  3417. {
  3418. struct drm_dp_sideband_msg_tx *txmsg;
  3419. struct drm_dp_mst_branch *mstb = NULL;
  3420. struct drm_dp_sideband_msg_rx *msg = &mgr->down_rep_recv;
  3421. if (!drm_dp_get_one_sb_msg(mgr, false, &mstb))
  3422. goto out_clear_reply;
  3423. /* Multi-packet message transmission, don't clear the reply */
  3424. if (!msg->have_eomt)
  3425. goto out;
  3426. /* find the message */
  3427. mutex_lock(&mgr->qlock);
  3428. txmsg = list_first_entry_or_null(&mgr->tx_msg_downq,
  3429. struct drm_dp_sideband_msg_tx, next);
  3430. mutex_unlock(&mgr->qlock);
  3431. /* Were we actually expecting a response, and from this mstb? */
  3432. if (!txmsg || txmsg->dst != mstb) {
  3433. struct drm_dp_sideband_msg_hdr *hdr;
  3434. hdr = &msg->initial_hdr;
  3435. drm_dbg_kms(mgr->dev, "Got MST reply with no msg %p %d %d %02x %02x\n",
  3436. mstb, hdr->seqno, hdr->lct, hdr->rad[0], msg->msg[0]);
  3437. goto out_clear_reply;
  3438. }
  3439. if (!verify_rx_request_type(mgr, txmsg, msg))
  3440. goto out_clear_reply;
  3441. drm_dp_sideband_parse_reply(mgr, msg, &txmsg->reply);
  3442. if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
  3443. drm_dbg_kms(mgr->dev,
  3444. "Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n",
  3445. txmsg->reply.req_type,
  3446. drm_dp_mst_req_type_str(txmsg->reply.req_type),
  3447. txmsg->reply.u.nak.reason,
  3448. drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason),
  3449. txmsg->reply.u.nak.nak_data);
  3450. }
  3451. memset(msg, 0, sizeof(struct drm_dp_sideband_msg_rx));
  3452. drm_dp_mst_topology_put_mstb(mstb);
  3453. mutex_lock(&mgr->qlock);
  3454. txmsg->state = DRM_DP_SIDEBAND_TX_RX;
  3455. list_del(&txmsg->next);
  3456. mutex_unlock(&mgr->qlock);
  3457. wake_up_all(&mgr->tx_waitq);
  3458. return 0;
  3459. out_clear_reply:
  3460. memset(msg, 0, sizeof(struct drm_dp_sideband_msg_rx));
  3461. out:
  3462. if (mstb)
  3463. drm_dp_mst_topology_put_mstb(mstb);
  3464. return 0;
  3465. }
  3466. static bool primary_mstb_probing_is_done(struct drm_dp_mst_topology_mgr *mgr)
  3467. {
  3468. bool probing_done = false;
  3469. mutex_lock(&mgr->lock);
  3470. if (mgr->mst_primary && drm_dp_mst_topology_try_get_mstb(mgr->mst_primary)) {
  3471. probing_done = mgr->mst_primary->link_address_sent;
  3472. drm_dp_mst_topology_put_mstb(mgr->mst_primary);
  3473. }
  3474. mutex_unlock(&mgr->lock);
  3475. return probing_done;
  3476. }
  3477. static inline bool
  3478. drm_dp_mst_process_up_req(struct drm_dp_mst_topology_mgr *mgr,
  3479. struct drm_dp_pending_up_req *up_req)
  3480. {
  3481. struct drm_dp_mst_branch *mstb = NULL;
  3482. struct drm_dp_sideband_msg_req_body *msg = &up_req->msg;
  3483. struct drm_dp_sideband_msg_hdr *hdr = &up_req->hdr;
  3484. bool hotplug = false, dowork = false;
  3485. if (hdr->broadcast) {
  3486. const guid_t *guid = NULL;
  3487. if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY)
  3488. guid = &msg->u.conn_stat.guid;
  3489. else if (msg->req_type == DP_RESOURCE_STATUS_NOTIFY)
  3490. guid = &msg->u.resource_stat.guid;
  3491. if (guid)
  3492. mstb = drm_dp_get_mst_branch_device_by_guid(mgr, guid);
  3493. } else {
  3494. mstb = drm_dp_get_mst_branch_device(mgr, hdr->lct, hdr->rad);
  3495. }
  3496. if (!mstb) {
  3497. drm_dbg_kms(mgr->dev, "Got MST reply from unknown device %d\n", hdr->lct);
  3498. return false;
  3499. }
  3500. /* TODO: Add missing handler for DP_RESOURCE_STATUS_NOTIFY events */
  3501. if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY) {
  3502. if (!primary_mstb_probing_is_done(mgr)) {
  3503. drm_dbg_kms(mgr->dev, "Got CSN before finish topology probing. Skip it.\n");
  3504. } else {
  3505. dowork = drm_dp_mst_handle_conn_stat(mstb, &msg->u.conn_stat);
  3506. hotplug = true;
  3507. }
  3508. }
  3509. drm_dp_mst_topology_put_mstb(mstb);
  3510. if (dowork)
  3511. queue_work(system_long_wq, &mgr->work);
  3512. return hotplug;
  3513. }
  3514. static void drm_dp_mst_up_req_work(struct work_struct *work)
  3515. {
  3516. struct drm_dp_mst_topology_mgr *mgr =
  3517. container_of(work, struct drm_dp_mst_topology_mgr,
  3518. up_req_work);
  3519. struct drm_dp_pending_up_req *up_req;
  3520. bool send_hotplug = false;
  3521. mutex_lock(&mgr->probe_lock);
  3522. while (true) {
  3523. mutex_lock(&mgr->up_req_lock);
  3524. up_req = list_first_entry_or_null(&mgr->up_req_list,
  3525. struct drm_dp_pending_up_req,
  3526. next);
  3527. if (up_req)
  3528. list_del(&up_req->next);
  3529. mutex_unlock(&mgr->up_req_lock);
  3530. if (!up_req)
  3531. break;
  3532. send_hotplug |= drm_dp_mst_process_up_req(mgr, up_req);
  3533. kfree(up_req);
  3534. }
  3535. mutex_unlock(&mgr->probe_lock);
  3536. if (send_hotplug)
  3537. drm_kms_helper_hotplug_event(mgr->dev);
  3538. }
  3539. static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
  3540. {
  3541. struct drm_dp_pending_up_req *up_req;
  3542. struct drm_dp_mst_branch *mst_primary;
  3543. if (!drm_dp_get_one_sb_msg(mgr, true, NULL))
  3544. goto out_clear_reply;
  3545. if (!mgr->up_req_recv.have_eomt)
  3546. return 0;
  3547. up_req = kzalloc(sizeof(*up_req), GFP_KERNEL);
  3548. if (!up_req)
  3549. return -ENOMEM;
  3550. INIT_LIST_HEAD(&up_req->next);
  3551. drm_dp_sideband_parse_req(mgr, &mgr->up_req_recv, &up_req->msg);
  3552. if (up_req->msg.req_type != DP_CONNECTION_STATUS_NOTIFY &&
  3553. up_req->msg.req_type != DP_RESOURCE_STATUS_NOTIFY) {
  3554. drm_dbg_kms(mgr->dev, "Received unknown up req type, ignoring: %x\n",
  3555. up_req->msg.req_type);
  3556. kfree(up_req);
  3557. goto out_clear_reply;
  3558. }
  3559. mutex_lock(&mgr->lock);
  3560. mst_primary = mgr->mst_primary;
  3561. if (!mst_primary || !drm_dp_mst_topology_try_get_mstb(mst_primary)) {
  3562. mutex_unlock(&mgr->lock);
  3563. kfree(up_req);
  3564. goto out_clear_reply;
  3565. }
  3566. mutex_unlock(&mgr->lock);
  3567. drm_dp_send_up_ack_reply(mgr, mst_primary, up_req->msg.req_type,
  3568. false);
  3569. drm_dp_mst_topology_put_mstb(mst_primary);
  3570. if (up_req->msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
  3571. const struct drm_dp_connection_status_notify *conn_stat =
  3572. &up_req->msg.u.conn_stat;
  3573. drm_dbg_kms(mgr->dev, "Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n",
  3574. conn_stat->port_number,
  3575. conn_stat->legacy_device_plug_status,
  3576. conn_stat->displayport_device_plug_status,
  3577. conn_stat->message_capability_status,
  3578. conn_stat->input_port,
  3579. conn_stat->peer_device_type);
  3580. } else if (up_req->msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
  3581. const struct drm_dp_resource_status_notify *res_stat =
  3582. &up_req->msg.u.resource_stat;
  3583. drm_dbg_kms(mgr->dev, "Got RSN: pn: %d avail_pbn %d\n",
  3584. res_stat->port_number,
  3585. res_stat->available_pbn);
  3586. }
  3587. up_req->hdr = mgr->up_req_recv.initial_hdr;
  3588. mutex_lock(&mgr->up_req_lock);
  3589. list_add_tail(&up_req->next, &mgr->up_req_list);
  3590. mutex_unlock(&mgr->up_req_lock);
  3591. queue_work(system_long_wq, &mgr->up_req_work);
  3592. out_clear_reply:
  3593. memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  3594. return 0;
  3595. }
  3596. static void update_msg_rx_state(struct drm_dp_mst_topology_mgr *mgr)
  3597. {
  3598. mutex_lock(&mgr->lock);
  3599. if (mgr->reset_rx_state) {
  3600. mgr->reset_rx_state = false;
  3601. reset_msg_rx_state(&mgr->down_rep_recv);
  3602. reset_msg_rx_state(&mgr->up_req_recv);
  3603. }
  3604. mutex_unlock(&mgr->lock);
  3605. }
  3606. /**
  3607. * drm_dp_mst_hpd_irq_handle_event() - MST hotplug IRQ handle MST event
  3608. * @mgr: manager to notify irq for.
  3609. * @esi: 4 bytes from SINK_COUNT_ESI
  3610. * @ack: 4 bytes used to ack events starting from SINK_COUNT_ESI
  3611. * @handled: whether the hpd interrupt was consumed or not
  3612. *
  3613. * This should be called from the driver when it detects a HPD IRQ,
  3614. * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
  3615. * topology manager will process the sideband messages received
  3616. * as indicated in the DEVICE_SERVICE_IRQ_VECTOR_ESI0 and set the
  3617. * corresponding flags that Driver has to ack the DP receiver later.
  3618. *
  3619. * Note that driver shall also call
  3620. * drm_dp_mst_hpd_irq_send_new_request() if the 'handled' is set
  3621. * after calling this function, to try to kick off a new request in
  3622. * the queue if the previous message transaction is completed.
  3623. *
  3624. * See also:
  3625. * drm_dp_mst_hpd_irq_send_new_request()
  3626. */
  3627. int drm_dp_mst_hpd_irq_handle_event(struct drm_dp_mst_topology_mgr *mgr, const u8 *esi,
  3628. u8 *ack, bool *handled)
  3629. {
  3630. int ret = 0;
  3631. int sc;
  3632. *handled = false;
  3633. sc = DP_GET_SINK_COUNT(esi[0]);
  3634. if (sc != mgr->sink_count) {
  3635. mgr->sink_count = sc;
  3636. *handled = true;
  3637. }
  3638. update_msg_rx_state(mgr);
  3639. if (esi[1] & DP_DOWN_REP_MSG_RDY) {
  3640. ret = drm_dp_mst_handle_down_rep(mgr);
  3641. *handled = true;
  3642. ack[1] |= DP_DOWN_REP_MSG_RDY;
  3643. }
  3644. if (esi[1] & DP_UP_REQ_MSG_RDY) {
  3645. ret |= drm_dp_mst_handle_up_req(mgr);
  3646. *handled = true;
  3647. ack[1] |= DP_UP_REQ_MSG_RDY;
  3648. }
  3649. return ret;
  3650. }
  3651. EXPORT_SYMBOL(drm_dp_mst_hpd_irq_handle_event);
  3652. /**
  3653. * drm_dp_mst_hpd_irq_send_new_request() - MST hotplug IRQ kick off new request
  3654. * @mgr: manager to notify irq for.
  3655. *
  3656. * This should be called from the driver when mst irq event is handled
  3657. * and acked. Note that new down request should only be sent when
  3658. * previous message transaction is completed. Source is not supposed to generate
  3659. * interleaved message transactions.
  3660. */
  3661. void drm_dp_mst_hpd_irq_send_new_request(struct drm_dp_mst_topology_mgr *mgr)
  3662. {
  3663. struct drm_dp_sideband_msg_tx *txmsg;
  3664. bool kick = true;
  3665. mutex_lock(&mgr->qlock);
  3666. txmsg = list_first_entry_or_null(&mgr->tx_msg_downq,
  3667. struct drm_dp_sideband_msg_tx, next);
  3668. /* If last transaction is not completed yet*/
  3669. if (!txmsg ||
  3670. txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
  3671. txmsg->state == DRM_DP_SIDEBAND_TX_SENT)
  3672. kick = false;
  3673. mutex_unlock(&mgr->qlock);
  3674. if (kick)
  3675. drm_dp_mst_kick_tx(mgr);
  3676. }
  3677. EXPORT_SYMBOL(drm_dp_mst_hpd_irq_send_new_request);
  3678. /**
  3679. * drm_dp_mst_detect_port() - get connection status for an MST port
  3680. * @connector: DRM connector for this port
  3681. * @ctx: The acquisition context to use for grabbing locks
  3682. * @mgr: manager for this port
  3683. * @port: pointer to a port
  3684. *
  3685. * This returns the current connection state for a port.
  3686. */
  3687. int
  3688. drm_dp_mst_detect_port(struct drm_connector *connector,
  3689. struct drm_modeset_acquire_ctx *ctx,
  3690. struct drm_dp_mst_topology_mgr *mgr,
  3691. struct drm_dp_mst_port *port)
  3692. {
  3693. int ret;
  3694. /* we need to search for the port in the mgr in case it's gone */
  3695. port = drm_dp_mst_topology_get_port_validated(mgr, port);
  3696. if (!port)
  3697. return connector_status_disconnected;
  3698. ret = drm_modeset_lock(&mgr->base.lock, ctx);
  3699. if (ret)
  3700. goto out;
  3701. ret = connector_status_disconnected;
  3702. if (!port->ddps)
  3703. goto out;
  3704. switch (port->pdt) {
  3705. case DP_PEER_DEVICE_NONE:
  3706. break;
  3707. case DP_PEER_DEVICE_MST_BRANCHING:
  3708. if (!port->mcs)
  3709. ret = connector_status_connected;
  3710. break;
  3711. case DP_PEER_DEVICE_SST_SINK:
  3712. ret = connector_status_connected;
  3713. /* for logical ports - cache the EDID */
  3714. if (drm_dp_mst_port_is_logical(port) && !port->cached_edid)
  3715. port->cached_edid = drm_edid_read_ddc(connector, &port->aux.ddc);
  3716. break;
  3717. case DP_PEER_DEVICE_DP_LEGACY_CONV:
  3718. if (port->ldps)
  3719. ret = connector_status_connected;
  3720. break;
  3721. }
  3722. out:
  3723. drm_dp_mst_topology_put_port(port);
  3724. return ret;
  3725. }
  3726. EXPORT_SYMBOL(drm_dp_mst_detect_port);
  3727. /**
  3728. * drm_dp_mst_edid_read() - get EDID for an MST port
  3729. * @connector: toplevel connector to get EDID for
  3730. * @mgr: manager for this port
  3731. * @port: unverified pointer to a port.
  3732. *
  3733. * This returns an EDID for the port connected to a connector,
  3734. * It validates the pointer still exists so the caller doesn't require a
  3735. * reference.
  3736. */
  3737. const struct drm_edid *drm_dp_mst_edid_read(struct drm_connector *connector,
  3738. struct drm_dp_mst_topology_mgr *mgr,
  3739. struct drm_dp_mst_port *port)
  3740. {
  3741. const struct drm_edid *drm_edid;
  3742. /* we need to search for the port in the mgr in case it's gone */
  3743. port = drm_dp_mst_topology_get_port_validated(mgr, port);
  3744. if (!port)
  3745. return NULL;
  3746. if (port->cached_edid)
  3747. drm_edid = drm_edid_dup(port->cached_edid);
  3748. else
  3749. drm_edid = drm_edid_read_ddc(connector, &port->aux.ddc);
  3750. drm_dp_mst_topology_put_port(port);
  3751. return drm_edid;
  3752. }
  3753. EXPORT_SYMBOL(drm_dp_mst_edid_read);
  3754. /**
  3755. * drm_dp_mst_get_edid() - get EDID for an MST port
  3756. * @connector: toplevel connector to get EDID for
  3757. * @mgr: manager for this port
  3758. * @port: unverified pointer to a port.
  3759. *
  3760. * This function is deprecated; please use drm_dp_mst_edid_read() instead.
  3761. *
  3762. * This returns an EDID for the port connected to a connector,
  3763. * It validates the pointer still exists so the caller doesn't require a
  3764. * reference.
  3765. */
  3766. struct edid *drm_dp_mst_get_edid(struct drm_connector *connector,
  3767. struct drm_dp_mst_topology_mgr *mgr,
  3768. struct drm_dp_mst_port *port)
  3769. {
  3770. const struct drm_edid *drm_edid;
  3771. struct edid *edid;
  3772. drm_edid = drm_dp_mst_edid_read(connector, mgr, port);
  3773. edid = drm_edid_duplicate(drm_edid_raw(drm_edid));
  3774. drm_edid_free(drm_edid);
  3775. return edid;
  3776. }
  3777. EXPORT_SYMBOL(drm_dp_mst_get_edid);
  3778. /**
  3779. * drm_dp_atomic_find_time_slots() - Find and add time slots to the state
  3780. * @state: global atomic state
  3781. * @mgr: MST topology manager for the port
  3782. * @port: port to find time slots for
  3783. * @pbn: bandwidth required for the mode in PBN
  3784. *
  3785. * Allocates time slots to @port, replacing any previous time slot allocations it may
  3786. * have had. Any atomic drivers which support MST must call this function in
  3787. * their &drm_encoder_helper_funcs.atomic_check() callback unconditionally to
  3788. * change the current time slot allocation for the new state, and ensure the MST
  3789. * atomic state is added whenever the state of payloads in the topology changes.
  3790. *
  3791. * Allocations set by this function are not checked against the bandwidth
  3792. * restraints of @mgr until the driver calls drm_dp_mst_atomic_check().
  3793. *
  3794. * Additionally, it is OK to call this function multiple times on the same
  3795. * @port as needed. It is not OK however, to call this function and
  3796. * drm_dp_atomic_release_time_slots() in the same atomic check phase.
  3797. *
  3798. * See also:
  3799. * drm_dp_atomic_release_time_slots()
  3800. * drm_dp_mst_atomic_check()
  3801. *
  3802. * Returns:
  3803. * Total slots in the atomic state assigned for this port, or a negative error
  3804. * code if the port no longer exists
  3805. */
  3806. int drm_dp_atomic_find_time_slots(struct drm_atomic_state *state,
  3807. struct drm_dp_mst_topology_mgr *mgr,
  3808. struct drm_dp_mst_port *port, int pbn)
  3809. {
  3810. struct drm_dp_mst_topology_state *topology_state;
  3811. struct drm_dp_mst_atomic_payload *payload = NULL;
  3812. struct drm_connector_state *conn_state;
  3813. int prev_slots = 0, prev_bw = 0, req_slots;
  3814. topology_state = drm_atomic_get_mst_topology_state(state, mgr);
  3815. if (IS_ERR(topology_state))
  3816. return PTR_ERR(topology_state);
  3817. conn_state = drm_atomic_get_new_connector_state(state, port->connector);
  3818. topology_state->pending_crtc_mask |= drm_crtc_mask(conn_state->crtc);
  3819. /* Find the current allocation for this port, if any */
  3820. payload = drm_atomic_get_mst_payload_state(topology_state, port);
  3821. if (payload) {
  3822. prev_slots = payload->time_slots;
  3823. prev_bw = payload->pbn;
  3824. /*
  3825. * This should never happen, unless the driver tries
  3826. * releasing and allocating the same timeslot allocation,
  3827. * which is an error
  3828. */
  3829. if (drm_WARN_ON(mgr->dev, payload->delete)) {
  3830. drm_err(mgr->dev,
  3831. "cannot allocate and release time slots on [MST PORT:%p] in the same state\n",
  3832. port);
  3833. return -EINVAL;
  3834. }
  3835. }
  3836. req_slots = DIV_ROUND_UP(dfixed_const(pbn), topology_state->pbn_div.full);
  3837. drm_dbg_atomic(mgr->dev, "[CONNECTOR:%d:%s] [MST PORT:%p] TU %d -> %d\n",
  3838. port->connector->base.id, port->connector->name,
  3839. port, prev_slots, req_slots);
  3840. drm_dbg_atomic(mgr->dev, "[CONNECTOR:%d:%s] [MST PORT:%p] PBN %d -> %d\n",
  3841. port->connector->base.id, port->connector->name,
  3842. port, prev_bw, pbn);
  3843. /* Add the new allocation to the state, note the VCPI isn't assigned until the end */
  3844. if (!payload) {
  3845. payload = kzalloc(sizeof(*payload), GFP_KERNEL);
  3846. if (!payload)
  3847. return -ENOMEM;
  3848. drm_dp_mst_get_port_malloc(port);
  3849. payload->port = port;
  3850. payload->vc_start_slot = -1;
  3851. payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_NONE;
  3852. list_add(&payload->next, &topology_state->payloads);
  3853. }
  3854. payload->time_slots = req_slots;
  3855. payload->pbn = pbn;
  3856. return req_slots;
  3857. }
  3858. EXPORT_SYMBOL(drm_dp_atomic_find_time_slots);
  3859. /**
  3860. * drm_dp_atomic_release_time_slots() - Release allocated time slots
  3861. * @state: global atomic state
  3862. * @mgr: MST topology manager for the port
  3863. * @port: The port to release the time slots from
  3864. *
  3865. * Releases any time slots that have been allocated to a port in the atomic
  3866. * state. Any atomic drivers which support MST must call this function
  3867. * unconditionally in their &drm_connector_helper_funcs.atomic_check() callback.
  3868. * This helper will check whether time slots would be released by the new state and
  3869. * respond accordingly, along with ensuring the MST state is always added to the
  3870. * atomic state whenever a new state would modify the state of payloads on the
  3871. * topology.
  3872. *
  3873. * It is OK to call this even if @port has been removed from the system.
  3874. * Additionally, it is OK to call this function multiple times on the same
  3875. * @port as needed. It is not OK however, to call this function and
  3876. * drm_dp_atomic_find_time_slots() on the same @port in a single atomic check
  3877. * phase.
  3878. *
  3879. * See also:
  3880. * drm_dp_atomic_find_time_slots()
  3881. * drm_dp_mst_atomic_check()
  3882. *
  3883. * Returns:
  3884. * 0 on success, negative error code otherwise
  3885. */
  3886. int drm_dp_atomic_release_time_slots(struct drm_atomic_state *state,
  3887. struct drm_dp_mst_topology_mgr *mgr,
  3888. struct drm_dp_mst_port *port)
  3889. {
  3890. struct drm_dp_mst_topology_state *topology_state;
  3891. struct drm_dp_mst_atomic_payload *payload;
  3892. struct drm_connector_state *old_conn_state, *new_conn_state;
  3893. bool update_payload = true;
  3894. old_conn_state = drm_atomic_get_old_connector_state(state, port->connector);
  3895. if (!old_conn_state->crtc)
  3896. return 0;
  3897. /* If the CRTC isn't disabled by this state, don't release it's payload */
  3898. new_conn_state = drm_atomic_get_new_connector_state(state, port->connector);
  3899. if (new_conn_state->crtc) {
  3900. struct drm_crtc_state *crtc_state =
  3901. drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  3902. /* No modeset means no payload changes, so it's safe to not pull in the MST state */
  3903. if (!crtc_state || !drm_atomic_crtc_needs_modeset(crtc_state))
  3904. return 0;
  3905. if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
  3906. update_payload = false;
  3907. }
  3908. topology_state = drm_atomic_get_mst_topology_state(state, mgr);
  3909. if (IS_ERR(topology_state))
  3910. return PTR_ERR(topology_state);
  3911. topology_state->pending_crtc_mask |= drm_crtc_mask(old_conn_state->crtc);
  3912. if (!update_payload)
  3913. return 0;
  3914. payload = drm_atomic_get_mst_payload_state(topology_state, port);
  3915. if (WARN_ON(!payload)) {
  3916. drm_err(mgr->dev, "No payload for [MST PORT:%p] found in mst state %p\n",
  3917. port, &topology_state->base);
  3918. return -EINVAL;
  3919. }
  3920. if (new_conn_state->crtc)
  3921. return 0;
  3922. drm_dbg_atomic(mgr->dev, "[MST PORT:%p] TU %d -> 0\n", port, payload->time_slots);
  3923. if (!payload->delete) {
  3924. payload->pbn = 0;
  3925. payload->delete = true;
  3926. topology_state->payload_mask &= ~BIT(payload->vcpi - 1);
  3927. }
  3928. return 0;
  3929. }
  3930. EXPORT_SYMBOL(drm_dp_atomic_release_time_slots);
  3931. /**
  3932. * drm_dp_mst_atomic_setup_commit() - setup_commit hook for MST helpers
  3933. * @state: global atomic state
  3934. *
  3935. * This function saves all of the &drm_crtc_commit structs in an atomic state that touch any CRTCs
  3936. * currently assigned to an MST topology. Drivers must call this hook from their
  3937. * &drm_mode_config_helper_funcs.atomic_commit_setup hook.
  3938. *
  3939. * Returns:
  3940. * 0 if all CRTC commits were retrieved successfully, negative error code otherwise
  3941. */
  3942. int drm_dp_mst_atomic_setup_commit(struct drm_atomic_state *state)
  3943. {
  3944. struct drm_dp_mst_topology_mgr *mgr;
  3945. struct drm_dp_mst_topology_state *mst_state;
  3946. struct drm_crtc *crtc;
  3947. struct drm_crtc_state *crtc_state;
  3948. int i, j, commit_idx, num_commit_deps;
  3949. for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
  3950. if (!mst_state->pending_crtc_mask)
  3951. continue;
  3952. num_commit_deps = hweight32(mst_state->pending_crtc_mask);
  3953. mst_state->commit_deps = kmalloc_array(num_commit_deps,
  3954. sizeof(*mst_state->commit_deps), GFP_KERNEL);
  3955. if (!mst_state->commit_deps)
  3956. return -ENOMEM;
  3957. mst_state->num_commit_deps = num_commit_deps;
  3958. commit_idx = 0;
  3959. for_each_new_crtc_in_state(state, crtc, crtc_state, j) {
  3960. if (mst_state->pending_crtc_mask & drm_crtc_mask(crtc)) {
  3961. mst_state->commit_deps[commit_idx++] =
  3962. drm_crtc_commit_get(crtc_state->commit);
  3963. }
  3964. }
  3965. }
  3966. return 0;
  3967. }
  3968. EXPORT_SYMBOL(drm_dp_mst_atomic_setup_commit);
  3969. /**
  3970. * drm_dp_mst_atomic_wait_for_dependencies() - Wait for all pending commits on MST topologies,
  3971. * prepare new MST state for commit
  3972. * @state: global atomic state
  3973. *
  3974. * Goes through any MST topologies in this atomic state, and waits for any pending commits which
  3975. * touched CRTCs that were/are on an MST topology to be programmed to hardware and flipped to before
  3976. * returning. This is to prevent multiple non-blocking commits affecting an MST topology from racing
  3977. * with eachother by forcing them to be executed sequentially in situations where the only resources
  3978. * the modeset objects in these commits share are an MST topology.
  3979. *
  3980. * This function also prepares the new MST state for commit by performing some state preparation
  3981. * which can't be done until this point, such as reading back the final VC start slots (which are
  3982. * determined at commit-time) from the previous state.
  3983. *
  3984. * All MST drivers must call this function after calling drm_atomic_helper_wait_for_dependencies(),
  3985. * or whatever their equivalent of that is.
  3986. */
  3987. void drm_dp_mst_atomic_wait_for_dependencies(struct drm_atomic_state *state)
  3988. {
  3989. struct drm_dp_mst_topology_state *old_mst_state, *new_mst_state;
  3990. struct drm_dp_mst_topology_mgr *mgr;
  3991. struct drm_dp_mst_atomic_payload *old_payload, *new_payload;
  3992. int i, j, ret;
  3993. for_each_oldnew_mst_mgr_in_state(state, mgr, old_mst_state, new_mst_state, i) {
  3994. for (j = 0; j < old_mst_state->num_commit_deps; j++) {
  3995. ret = drm_crtc_commit_wait(old_mst_state->commit_deps[j]);
  3996. if (ret < 0)
  3997. drm_err(state->dev, "Failed to wait for %s: %d\n",
  3998. old_mst_state->commit_deps[j]->crtc->name, ret);
  3999. }
  4000. /* Now that previous state is committed, it's safe to copy over the start slot
  4001. * and allocation status assignments
  4002. */
  4003. list_for_each_entry(old_payload, &old_mst_state->payloads, next) {
  4004. if (old_payload->delete)
  4005. continue;
  4006. new_payload = drm_atomic_get_mst_payload_state(new_mst_state,
  4007. old_payload->port);
  4008. new_payload->vc_start_slot = old_payload->vc_start_slot;
  4009. new_payload->payload_allocation_status =
  4010. old_payload->payload_allocation_status;
  4011. }
  4012. }
  4013. }
  4014. EXPORT_SYMBOL(drm_dp_mst_atomic_wait_for_dependencies);
  4015. /**
  4016. * drm_dp_mst_root_conn_atomic_check() - Serialize CRTC commits on MST-capable connectors operating
  4017. * in SST mode
  4018. * @new_conn_state: The new connector state of the &drm_connector
  4019. * @mgr: The MST topology manager for the &drm_connector
  4020. *
  4021. * Since MST uses fake &drm_encoder structs, the generic atomic modesetting code isn't able to
  4022. * serialize non-blocking commits happening on the real DP connector of an MST topology switching
  4023. * into/away from MST mode - as the CRTC on the real DP connector and the CRTCs on the connector's
  4024. * MST topology will never share the same &drm_encoder.
  4025. *
  4026. * This function takes care of this serialization issue, by checking a root MST connector's atomic
  4027. * state to determine if it is about to have a modeset - and then pulling in the MST topology state
  4028. * if so, along with adding any relevant CRTCs to &drm_dp_mst_topology_state.pending_crtc_mask.
  4029. *
  4030. * Drivers implementing MST must call this function from the
  4031. * &drm_connector_helper_funcs.atomic_check hook of any physical DP &drm_connector capable of
  4032. * driving MST sinks.
  4033. *
  4034. * Returns:
  4035. * 0 on success, negative error code otherwise
  4036. */
  4037. int drm_dp_mst_root_conn_atomic_check(struct drm_connector_state *new_conn_state,
  4038. struct drm_dp_mst_topology_mgr *mgr)
  4039. {
  4040. struct drm_atomic_state *state = new_conn_state->state;
  4041. struct drm_connector_state *old_conn_state =
  4042. drm_atomic_get_old_connector_state(state, new_conn_state->connector);
  4043. struct drm_crtc_state *crtc_state;
  4044. struct drm_dp_mst_topology_state *mst_state = NULL;
  4045. if (new_conn_state->crtc) {
  4046. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  4047. if (crtc_state && drm_atomic_crtc_needs_modeset(crtc_state)) {
  4048. mst_state = drm_atomic_get_mst_topology_state(state, mgr);
  4049. if (IS_ERR(mst_state))
  4050. return PTR_ERR(mst_state);
  4051. mst_state->pending_crtc_mask |= drm_crtc_mask(new_conn_state->crtc);
  4052. }
  4053. }
  4054. if (old_conn_state->crtc) {
  4055. crtc_state = drm_atomic_get_new_crtc_state(state, old_conn_state->crtc);
  4056. if (crtc_state && drm_atomic_crtc_needs_modeset(crtc_state)) {
  4057. if (!mst_state) {
  4058. mst_state = drm_atomic_get_mst_topology_state(state, mgr);
  4059. if (IS_ERR(mst_state))
  4060. return PTR_ERR(mst_state);
  4061. }
  4062. mst_state->pending_crtc_mask |= drm_crtc_mask(old_conn_state->crtc);
  4063. }
  4064. }
  4065. return 0;
  4066. }
  4067. EXPORT_SYMBOL(drm_dp_mst_root_conn_atomic_check);
  4068. /**
  4069. * drm_dp_mst_update_slots() - updates the slot info depending on the DP ecoding format
  4070. * @mst_state: mst_state to update
  4071. * @link_encoding_cap: the ecoding format on the link
  4072. */
  4073. void drm_dp_mst_update_slots(struct drm_dp_mst_topology_state *mst_state, uint8_t link_encoding_cap)
  4074. {
  4075. if (link_encoding_cap == DP_CAP_ANSI_128B132B) {
  4076. mst_state->total_avail_slots = 64;
  4077. mst_state->start_slot = 0;
  4078. } else {
  4079. mst_state->total_avail_slots = 63;
  4080. mst_state->start_slot = 1;
  4081. }
  4082. DRM_DEBUG_KMS("%s encoding format on mst_state 0x%p\n",
  4083. (link_encoding_cap == DP_CAP_ANSI_128B132B) ? "128b/132b":"8b/10b",
  4084. mst_state);
  4085. }
  4086. EXPORT_SYMBOL(drm_dp_mst_update_slots);
  4087. static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
  4088. int id, u8 start_slot, u8 num_slots)
  4089. {
  4090. u8 payload_alloc[3], status;
  4091. int ret;
  4092. int retries = 0;
  4093. drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
  4094. DP_PAYLOAD_TABLE_UPDATED);
  4095. payload_alloc[0] = id;
  4096. payload_alloc[1] = start_slot;
  4097. payload_alloc[2] = num_slots;
  4098. ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
  4099. if (ret != 3) {
  4100. drm_dbg_kms(mgr->dev, "failed to write payload allocation %d\n", ret);
  4101. goto fail;
  4102. }
  4103. retry:
  4104. ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
  4105. if (ret < 0) {
  4106. drm_dbg_kms(mgr->dev, "failed to read payload table status %d\n", ret);
  4107. goto fail;
  4108. }
  4109. if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
  4110. retries++;
  4111. if (retries < 20) {
  4112. usleep_range(10000, 20000);
  4113. goto retry;
  4114. }
  4115. drm_dbg_kms(mgr->dev, "status not set after read payload table status %d\n",
  4116. status);
  4117. ret = -EINVAL;
  4118. goto fail;
  4119. }
  4120. ret = 0;
  4121. fail:
  4122. return ret;
  4123. }
  4124. static int do_get_act_status(struct drm_dp_aux *aux)
  4125. {
  4126. int ret;
  4127. u8 status;
  4128. ret = drm_dp_dpcd_readb(aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
  4129. if (ret < 0)
  4130. return ret;
  4131. return status;
  4132. }
  4133. /**
  4134. * drm_dp_check_act_status() - Polls for ACT handled status.
  4135. * @mgr: manager to use
  4136. *
  4137. * Tries waiting for the MST hub to finish updating it's payload table by
  4138. * polling for the ACT handled bit for up to 3 seconds (yes-some hubs really
  4139. * take that long).
  4140. *
  4141. * Returns:
  4142. * 0 if the ACT was handled in time, negative error code on failure.
  4143. */
  4144. int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
  4145. {
  4146. /*
  4147. * There doesn't seem to be any recommended retry count or timeout in
  4148. * the MST specification. Since some hubs have been observed to take
  4149. * over 1 second to update their payload allocations under certain
  4150. * conditions, we use a rather large timeout value.
  4151. */
  4152. const int timeout_ms = 3000;
  4153. int ret, status;
  4154. ret = readx_poll_timeout(do_get_act_status, mgr->aux, status,
  4155. status & DP_PAYLOAD_ACT_HANDLED || status < 0,
  4156. 200, timeout_ms * USEC_PER_MSEC);
  4157. if (ret < 0 && status >= 0) {
  4158. drm_err(mgr->dev, "Failed to get ACT after %dms, last status: %02x\n",
  4159. timeout_ms, status);
  4160. return -EINVAL;
  4161. } else if (status < 0) {
  4162. /*
  4163. * Failure here isn't unexpected - the hub may have
  4164. * just been unplugged
  4165. */
  4166. drm_dbg_kms(mgr->dev, "Failed to read payload table status: %d\n", status);
  4167. return status;
  4168. }
  4169. return 0;
  4170. }
  4171. EXPORT_SYMBOL(drm_dp_check_act_status);
  4172. /**
  4173. * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
  4174. * @clock: dot clock
  4175. * @bpp: bpp as .4 binary fixed point
  4176. *
  4177. * This uses the formula in the spec to calculate the PBN value for a mode.
  4178. */
  4179. int drm_dp_calc_pbn_mode(int clock, int bpp)
  4180. {
  4181. /*
  4182. * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
  4183. * common multiplier to render an integer PBN for all link rate/lane
  4184. * counts combinations
  4185. * calculate
  4186. * peak_kbps = clock * bpp / 16
  4187. * peak_kbps *= SSC overhead / 1000000
  4188. * peak_kbps /= 8 convert to Kbytes
  4189. * peak_kBps *= (64/54) / 1000 convert to PBN
  4190. */
  4191. /*
  4192. * TODO: Use the actual link and mode parameters to calculate
  4193. * the overhead. For now it's assumed that these are
  4194. * 4 link lanes, 4096 hactive pixels, which don't add any
  4195. * significant data padding overhead and that there is no DSC
  4196. * or FEC overhead.
  4197. */
  4198. int overhead = drm_dp_bw_overhead(4, 4096, 0, bpp,
  4199. DRM_DP_BW_OVERHEAD_MST |
  4200. DRM_DP_BW_OVERHEAD_SSC_REF_CLK);
  4201. return DIV64_U64_ROUND_UP(mul_u32_u32(clock * bpp, 64 * overhead >> 4),
  4202. 1000000ULL * 8 * 54 * 1000);
  4203. }
  4204. EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
  4205. /* we want to kick the TX after we've ack the up/down IRQs. */
  4206. static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
  4207. {
  4208. queue_work(system_long_wq, &mgr->tx_work);
  4209. }
  4210. /*
  4211. * Helper function for parsing DP device types into convenient strings
  4212. * for use with dp_mst_topology
  4213. */
  4214. static const char *pdt_to_string(u8 pdt)
  4215. {
  4216. switch (pdt) {
  4217. case DP_PEER_DEVICE_NONE:
  4218. return "NONE";
  4219. case DP_PEER_DEVICE_SOURCE_OR_SST:
  4220. return "SOURCE OR SST";
  4221. case DP_PEER_DEVICE_MST_BRANCHING:
  4222. return "MST BRANCHING";
  4223. case DP_PEER_DEVICE_SST_SINK:
  4224. return "SST SINK";
  4225. case DP_PEER_DEVICE_DP_LEGACY_CONV:
  4226. return "DP LEGACY CONV";
  4227. default:
  4228. return "ERR";
  4229. }
  4230. }
  4231. static void drm_dp_mst_dump_mstb(struct seq_file *m,
  4232. struct drm_dp_mst_branch *mstb)
  4233. {
  4234. struct drm_dp_mst_port *port;
  4235. int tabs = mstb->lct;
  4236. char prefix[10];
  4237. int i;
  4238. for (i = 0; i < tabs; i++)
  4239. prefix[i] = '\t';
  4240. prefix[i] = '\0';
  4241. seq_printf(m, "%smstb - [%p]: num_ports: %d\n", prefix, mstb, mstb->num_ports);
  4242. list_for_each_entry(port, &mstb->ports, next) {
  4243. seq_printf(m, "%sport %d - [%p] (%s - %s): ddps: %d, ldps: %d, sdp: %d/%d, fec: %s, conn: %p\n",
  4244. prefix,
  4245. port->port_num,
  4246. port,
  4247. port->input ? "input" : "output",
  4248. pdt_to_string(port->pdt),
  4249. port->ddps,
  4250. port->ldps,
  4251. port->num_sdp_streams,
  4252. port->num_sdp_stream_sinks,
  4253. port->fec_capable ? "true" : "false",
  4254. port->connector);
  4255. if (port->mstb)
  4256. drm_dp_mst_dump_mstb(m, port->mstb);
  4257. }
  4258. }
  4259. #define DP_PAYLOAD_TABLE_SIZE 64
  4260. static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
  4261. char *buf)
  4262. {
  4263. int i;
  4264. for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
  4265. if (drm_dp_dpcd_read(mgr->aux,
  4266. DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
  4267. &buf[i], 16) != 16)
  4268. return false;
  4269. }
  4270. return true;
  4271. }
  4272. static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
  4273. struct drm_dp_mst_port *port, char *name,
  4274. int namelen)
  4275. {
  4276. struct edid *mst_edid;
  4277. mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
  4278. drm_edid_get_monitor_name(mst_edid, name, namelen);
  4279. kfree(mst_edid);
  4280. }
  4281. /**
  4282. * drm_dp_mst_dump_topology(): dump topology to seq file.
  4283. * @m: seq_file to dump output to
  4284. * @mgr: manager to dump current topology for.
  4285. *
  4286. * helper to dump MST topology to a seq file for debugfs.
  4287. */
  4288. void drm_dp_mst_dump_topology(struct seq_file *m,
  4289. struct drm_dp_mst_topology_mgr *mgr)
  4290. {
  4291. struct drm_dp_mst_topology_state *state;
  4292. struct drm_dp_mst_atomic_payload *payload;
  4293. int i, ret;
  4294. static const char *const status[] = {
  4295. "None",
  4296. "Local",
  4297. "DFP",
  4298. "Remote",
  4299. };
  4300. mutex_lock(&mgr->lock);
  4301. if (mgr->mst_primary)
  4302. drm_dp_mst_dump_mstb(m, mgr->mst_primary);
  4303. /* dump VCPIs */
  4304. mutex_unlock(&mgr->lock);
  4305. ret = drm_modeset_lock_single_interruptible(&mgr->base.lock);
  4306. if (ret < 0)
  4307. return;
  4308. state = to_drm_dp_mst_topology_state(mgr->base.state);
  4309. seq_printf(m, "\n*** Atomic state info ***\n");
  4310. seq_printf(m, "payload_mask: %x, max_payloads: %d, start_slot: %u, pbn_div: %d\n",
  4311. state->payload_mask, mgr->max_payloads, state->start_slot,
  4312. dfixed_trunc(state->pbn_div));
  4313. seq_printf(m, "\n| idx | port | vcpi | slots | pbn | dsc | status | sink name |\n");
  4314. for (i = 0; i < mgr->max_payloads; i++) {
  4315. list_for_each_entry(payload, &state->payloads, next) {
  4316. char name[14];
  4317. if (payload->vcpi != i || payload->delete)
  4318. continue;
  4319. fetch_monitor_name(mgr, payload->port, name, sizeof(name));
  4320. seq_printf(m, " %5d %6d %6d %02d - %02d %5d %5s %8s %19s\n",
  4321. i,
  4322. payload->port->port_num,
  4323. payload->vcpi,
  4324. payload->vc_start_slot,
  4325. payload->vc_start_slot + payload->time_slots - 1,
  4326. payload->pbn,
  4327. payload->dsc_enabled ? "Y" : "N",
  4328. status[payload->payload_allocation_status],
  4329. (*name != 0) ? name : "Unknown");
  4330. }
  4331. }
  4332. seq_printf(m, "\n*** DPCD Info ***\n");
  4333. mutex_lock(&mgr->lock);
  4334. if (mgr->mst_primary) {
  4335. u8 buf[DP_PAYLOAD_TABLE_SIZE];
  4336. int ret;
  4337. if (drm_dp_read_dpcd_caps(mgr->aux, buf) < 0) {
  4338. seq_printf(m, "dpcd read failed\n");
  4339. goto out;
  4340. }
  4341. seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
  4342. ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
  4343. if (ret != 2) {
  4344. seq_printf(m, "faux/mst read failed\n");
  4345. goto out;
  4346. }
  4347. seq_printf(m, "faux/mst: %*ph\n", 2, buf);
  4348. ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
  4349. if (ret != 1) {
  4350. seq_printf(m, "mst ctrl read failed\n");
  4351. goto out;
  4352. }
  4353. seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
  4354. /* dump the standard OUI branch header */
  4355. ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
  4356. if (ret != DP_BRANCH_OUI_HEADER_SIZE) {
  4357. seq_printf(m, "branch oui read failed\n");
  4358. goto out;
  4359. }
  4360. seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
  4361. for (i = 0x3; i < 0x8 && buf[i]; i++)
  4362. seq_putc(m, buf[i]);
  4363. seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
  4364. buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
  4365. if (dump_dp_payload_table(mgr, buf))
  4366. seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
  4367. }
  4368. out:
  4369. mutex_unlock(&mgr->lock);
  4370. drm_modeset_unlock(&mgr->base.lock);
  4371. }
  4372. EXPORT_SYMBOL(drm_dp_mst_dump_topology);
  4373. static void drm_dp_tx_work(struct work_struct *work)
  4374. {
  4375. struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
  4376. mutex_lock(&mgr->qlock);
  4377. if (!list_empty(&mgr->tx_msg_downq))
  4378. process_single_down_tx_qlock(mgr);
  4379. mutex_unlock(&mgr->qlock);
  4380. }
  4381. static inline void
  4382. drm_dp_delayed_destroy_port(struct drm_dp_mst_port *port)
  4383. {
  4384. drm_dp_port_set_pdt(port, DP_PEER_DEVICE_NONE, port->mcs);
  4385. if (port->connector) {
  4386. drm_connector_unregister(port->connector);
  4387. drm_connector_put(port->connector);
  4388. }
  4389. drm_dp_mst_put_port_malloc(port);
  4390. }
  4391. static inline void
  4392. drm_dp_delayed_destroy_mstb(struct drm_dp_mst_branch *mstb)
  4393. {
  4394. struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
  4395. struct drm_dp_mst_port *port, *port_tmp;
  4396. struct drm_dp_sideband_msg_tx *txmsg, *txmsg_tmp;
  4397. bool wake_tx = false;
  4398. mutex_lock(&mgr->lock);
  4399. list_for_each_entry_safe(port, port_tmp, &mstb->ports, next) {
  4400. list_del(&port->next);
  4401. drm_dp_mst_topology_put_port(port);
  4402. }
  4403. mutex_unlock(&mgr->lock);
  4404. /* drop any tx slot msg */
  4405. mutex_lock(&mstb->mgr->qlock);
  4406. list_for_each_entry_safe(txmsg, txmsg_tmp, &mgr->tx_msg_downq, next) {
  4407. if (txmsg->dst != mstb)
  4408. continue;
  4409. txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
  4410. list_del(&txmsg->next);
  4411. wake_tx = true;
  4412. }
  4413. mutex_unlock(&mstb->mgr->qlock);
  4414. if (wake_tx)
  4415. wake_up_all(&mstb->mgr->tx_waitq);
  4416. drm_dp_mst_put_mstb_malloc(mstb);
  4417. }
  4418. static void drm_dp_delayed_destroy_work(struct work_struct *work)
  4419. {
  4420. struct drm_dp_mst_topology_mgr *mgr =
  4421. container_of(work, struct drm_dp_mst_topology_mgr,
  4422. delayed_destroy_work);
  4423. bool send_hotplug = false, go_again;
  4424. /*
  4425. * Not a regular list traverse as we have to drop the destroy
  4426. * connector lock before destroying the mstb/port, to avoid AB->BA
  4427. * ordering between this lock and the config mutex.
  4428. */
  4429. do {
  4430. go_again = false;
  4431. for (;;) {
  4432. struct drm_dp_mst_branch *mstb;
  4433. mutex_lock(&mgr->delayed_destroy_lock);
  4434. mstb = list_first_entry_or_null(&mgr->destroy_branch_device_list,
  4435. struct drm_dp_mst_branch,
  4436. destroy_next);
  4437. if (mstb)
  4438. list_del(&mstb->destroy_next);
  4439. mutex_unlock(&mgr->delayed_destroy_lock);
  4440. if (!mstb)
  4441. break;
  4442. drm_dp_delayed_destroy_mstb(mstb);
  4443. go_again = true;
  4444. }
  4445. for (;;) {
  4446. struct drm_dp_mst_port *port;
  4447. mutex_lock(&mgr->delayed_destroy_lock);
  4448. port = list_first_entry_or_null(&mgr->destroy_port_list,
  4449. struct drm_dp_mst_port,
  4450. next);
  4451. if (port)
  4452. list_del(&port->next);
  4453. mutex_unlock(&mgr->delayed_destroy_lock);
  4454. if (!port)
  4455. break;
  4456. drm_dp_delayed_destroy_port(port);
  4457. send_hotplug = true;
  4458. go_again = true;
  4459. }
  4460. } while (go_again);
  4461. if (send_hotplug)
  4462. drm_kms_helper_hotplug_event(mgr->dev);
  4463. }
  4464. static struct drm_private_state *
  4465. drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
  4466. {
  4467. struct drm_dp_mst_topology_state *state, *old_state =
  4468. to_dp_mst_topology_state(obj->state);
  4469. struct drm_dp_mst_atomic_payload *pos, *payload;
  4470. state = kmemdup(old_state, sizeof(*state), GFP_KERNEL);
  4471. if (!state)
  4472. return NULL;
  4473. __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
  4474. INIT_LIST_HEAD(&state->payloads);
  4475. state->commit_deps = NULL;
  4476. state->num_commit_deps = 0;
  4477. state->pending_crtc_mask = 0;
  4478. list_for_each_entry(pos, &old_state->payloads, next) {
  4479. /* Prune leftover freed timeslot allocations */
  4480. if (pos->delete)
  4481. continue;
  4482. payload = kmemdup(pos, sizeof(*payload), GFP_KERNEL);
  4483. if (!payload)
  4484. goto fail;
  4485. drm_dp_mst_get_port_malloc(payload->port);
  4486. list_add(&payload->next, &state->payloads);
  4487. }
  4488. return &state->base;
  4489. fail:
  4490. list_for_each_entry_safe(pos, payload, &state->payloads, next) {
  4491. drm_dp_mst_put_port_malloc(pos->port);
  4492. kfree(pos);
  4493. }
  4494. kfree(state);
  4495. return NULL;
  4496. }
  4497. static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
  4498. struct drm_private_state *state)
  4499. {
  4500. struct drm_dp_mst_topology_state *mst_state =
  4501. to_dp_mst_topology_state(state);
  4502. struct drm_dp_mst_atomic_payload *pos, *tmp;
  4503. int i;
  4504. list_for_each_entry_safe(pos, tmp, &mst_state->payloads, next) {
  4505. /* We only keep references to ports with active payloads */
  4506. if (!pos->delete)
  4507. drm_dp_mst_put_port_malloc(pos->port);
  4508. kfree(pos);
  4509. }
  4510. for (i = 0; i < mst_state->num_commit_deps; i++)
  4511. drm_crtc_commit_put(mst_state->commit_deps[i]);
  4512. kfree(mst_state->commit_deps);
  4513. kfree(mst_state);
  4514. }
  4515. static bool drm_dp_mst_port_downstream_of_branch(struct drm_dp_mst_port *port,
  4516. struct drm_dp_mst_branch *branch)
  4517. {
  4518. while (port->parent) {
  4519. if (port->parent == branch)
  4520. return true;
  4521. if (port->parent->port_parent)
  4522. port = port->parent->port_parent;
  4523. else
  4524. break;
  4525. }
  4526. return false;
  4527. }
  4528. static bool
  4529. drm_dp_mst_port_downstream_of_parent_locked(struct drm_dp_mst_topology_mgr *mgr,
  4530. struct drm_dp_mst_port *port,
  4531. struct drm_dp_mst_port *parent)
  4532. {
  4533. if (!mgr->mst_primary)
  4534. return false;
  4535. port = drm_dp_mst_topology_get_port_validated_locked(mgr->mst_primary,
  4536. port);
  4537. if (!port)
  4538. return false;
  4539. if (!parent)
  4540. return true;
  4541. parent = drm_dp_mst_topology_get_port_validated_locked(mgr->mst_primary,
  4542. parent);
  4543. if (!parent)
  4544. return false;
  4545. if (!parent->mstb)
  4546. return false;
  4547. return drm_dp_mst_port_downstream_of_branch(port, parent->mstb);
  4548. }
  4549. /**
  4550. * drm_dp_mst_port_downstream_of_parent - check if a port is downstream of a parent port
  4551. * @mgr: MST topology manager
  4552. * @port: the port being looked up
  4553. * @parent: the parent port
  4554. *
  4555. * The function returns %true if @port is downstream of @parent. If @parent is
  4556. * %NULL - denoting the root port - the function returns %true if @port is in
  4557. * @mgr's topology.
  4558. */
  4559. bool
  4560. drm_dp_mst_port_downstream_of_parent(struct drm_dp_mst_topology_mgr *mgr,
  4561. struct drm_dp_mst_port *port,
  4562. struct drm_dp_mst_port *parent)
  4563. {
  4564. bool ret;
  4565. mutex_lock(&mgr->lock);
  4566. ret = drm_dp_mst_port_downstream_of_parent_locked(mgr, port, parent);
  4567. mutex_unlock(&mgr->lock);
  4568. return ret;
  4569. }
  4570. EXPORT_SYMBOL(drm_dp_mst_port_downstream_of_parent);
  4571. static int
  4572. drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
  4573. struct drm_dp_mst_topology_state *state,
  4574. struct drm_dp_mst_port **failing_port);
  4575. static int
  4576. drm_dp_mst_atomic_check_mstb_bw_limit(struct drm_dp_mst_branch *mstb,
  4577. struct drm_dp_mst_topology_state *state,
  4578. struct drm_dp_mst_port **failing_port)
  4579. {
  4580. struct drm_dp_mst_atomic_payload *payload;
  4581. struct drm_dp_mst_port *port;
  4582. int pbn_used = 0, ret;
  4583. bool found = false;
  4584. /* Check that we have at least one port in our state that's downstream
  4585. * of this branch, otherwise we can skip this branch
  4586. */
  4587. list_for_each_entry(payload, &state->payloads, next) {
  4588. if (!payload->pbn ||
  4589. !drm_dp_mst_port_downstream_of_branch(payload->port, mstb))
  4590. continue;
  4591. found = true;
  4592. break;
  4593. }
  4594. if (!found)
  4595. return 0;
  4596. if (mstb->port_parent)
  4597. drm_dbg_atomic(mstb->mgr->dev,
  4598. "[MSTB:%p] [MST PORT:%p] Checking bandwidth limits on [MSTB:%p]\n",
  4599. mstb->port_parent->parent, mstb->port_parent, mstb);
  4600. else
  4601. drm_dbg_atomic(mstb->mgr->dev, "[MSTB:%p] Checking bandwidth limits\n", mstb);
  4602. list_for_each_entry(port, &mstb->ports, next) {
  4603. ret = drm_dp_mst_atomic_check_port_bw_limit(port, state, failing_port);
  4604. if (ret < 0)
  4605. return ret;
  4606. pbn_used += ret;
  4607. }
  4608. return pbn_used;
  4609. }
  4610. static int
  4611. drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
  4612. struct drm_dp_mst_topology_state *state,
  4613. struct drm_dp_mst_port **failing_port)
  4614. {
  4615. struct drm_dp_mst_atomic_payload *payload;
  4616. int pbn_used = 0;
  4617. if (port->pdt == DP_PEER_DEVICE_NONE)
  4618. return 0;
  4619. if (drm_dp_mst_is_end_device(port->pdt, port->mcs)) {
  4620. payload = drm_atomic_get_mst_payload_state(state, port);
  4621. if (!payload)
  4622. return 0;
  4623. /*
  4624. * This could happen if the sink deasserted its HPD line, but
  4625. * the branch device still reports it as attached (PDT != NONE).
  4626. */
  4627. if (!port->full_pbn) {
  4628. drm_dbg_atomic(port->mgr->dev,
  4629. "[MSTB:%p] [MST PORT:%p] no BW available for the port\n",
  4630. port->parent, port);
  4631. *failing_port = port;
  4632. return -EINVAL;
  4633. }
  4634. pbn_used = payload->pbn;
  4635. } else {
  4636. pbn_used = drm_dp_mst_atomic_check_mstb_bw_limit(port->mstb,
  4637. state,
  4638. failing_port);
  4639. if (pbn_used <= 0)
  4640. return pbn_used;
  4641. }
  4642. if (pbn_used > port->full_pbn) {
  4643. drm_dbg_atomic(port->mgr->dev,
  4644. "[MSTB:%p] [MST PORT:%p] required PBN of %d exceeds port limit of %d\n",
  4645. port->parent, port, pbn_used, port->full_pbn);
  4646. *failing_port = port;
  4647. return -ENOSPC;
  4648. }
  4649. drm_dbg_atomic(port->mgr->dev, "[MSTB:%p] [MST PORT:%p] uses %d out of %d PBN\n",
  4650. port->parent, port, pbn_used, port->full_pbn);
  4651. return pbn_used;
  4652. }
  4653. static inline int
  4654. drm_dp_mst_atomic_check_payload_alloc_limits(struct drm_dp_mst_topology_mgr *mgr,
  4655. struct drm_dp_mst_topology_state *mst_state)
  4656. {
  4657. struct drm_dp_mst_atomic_payload *payload;
  4658. int avail_slots = mst_state->total_avail_slots, payload_count = 0;
  4659. list_for_each_entry(payload, &mst_state->payloads, next) {
  4660. /* Releasing payloads is always OK-even if the port is gone */
  4661. if (payload->delete) {
  4662. drm_dbg_atomic(mgr->dev, "[MST PORT:%p] releases all time slots\n",
  4663. payload->port);
  4664. continue;
  4665. }
  4666. drm_dbg_atomic(mgr->dev, "[MST PORT:%p] requires %d time slots\n",
  4667. payload->port, payload->time_slots);
  4668. avail_slots -= payload->time_slots;
  4669. if (avail_slots < 0) {
  4670. drm_dbg_atomic(mgr->dev,
  4671. "[MST PORT:%p] not enough time slots in mst state %p (avail=%d)\n",
  4672. payload->port, mst_state, avail_slots + payload->time_slots);
  4673. return -ENOSPC;
  4674. }
  4675. if (++payload_count > mgr->max_payloads) {
  4676. drm_dbg_atomic(mgr->dev,
  4677. "[MST MGR:%p] state %p has too many payloads (max=%d)\n",
  4678. mgr, mst_state, mgr->max_payloads);
  4679. return -EINVAL;
  4680. }
  4681. /* Assign a VCPI */
  4682. if (!payload->vcpi) {
  4683. payload->vcpi = ffz(mst_state->payload_mask) + 1;
  4684. drm_dbg_atomic(mgr->dev, "[MST PORT:%p] assigned VCPI #%d\n",
  4685. payload->port, payload->vcpi);
  4686. mst_state->payload_mask |= BIT(payload->vcpi - 1);
  4687. }
  4688. }
  4689. if (!payload_count)
  4690. mst_state->pbn_div.full = dfixed_const(0);
  4691. drm_dbg_atomic(mgr->dev, "[MST MGR:%p] mst state %p TU pbn_div=%d avail=%d used=%d\n",
  4692. mgr, mst_state, dfixed_trunc(mst_state->pbn_div), avail_slots,
  4693. mst_state->total_avail_slots - avail_slots);
  4694. return 0;
  4695. }
  4696. /**
  4697. * drm_dp_mst_add_affected_dsc_crtcs
  4698. * @state: Pointer to the new struct drm_dp_mst_topology_state
  4699. * @mgr: MST topology manager
  4700. *
  4701. * Whenever there is a change in mst topology
  4702. * DSC configuration would have to be recalculated
  4703. * therefore we need to trigger modeset on all affected
  4704. * CRTCs in that topology
  4705. *
  4706. * See also:
  4707. * drm_dp_mst_atomic_enable_dsc()
  4708. */
  4709. int drm_dp_mst_add_affected_dsc_crtcs(struct drm_atomic_state *state, struct drm_dp_mst_topology_mgr *mgr)
  4710. {
  4711. struct drm_dp_mst_topology_state *mst_state;
  4712. struct drm_dp_mst_atomic_payload *pos;
  4713. struct drm_connector *connector;
  4714. struct drm_connector_state *conn_state;
  4715. struct drm_crtc *crtc;
  4716. struct drm_crtc_state *crtc_state;
  4717. mst_state = drm_atomic_get_mst_topology_state(state, mgr);
  4718. if (IS_ERR(mst_state))
  4719. return PTR_ERR(mst_state);
  4720. list_for_each_entry(pos, &mst_state->payloads, next) {
  4721. connector = pos->port->connector;
  4722. if (!connector)
  4723. return -EINVAL;
  4724. conn_state = drm_atomic_get_connector_state(state, connector);
  4725. if (IS_ERR(conn_state))
  4726. return PTR_ERR(conn_state);
  4727. crtc = conn_state->crtc;
  4728. if (!crtc)
  4729. continue;
  4730. if (!drm_dp_mst_dsc_aux_for_port(pos->port))
  4731. continue;
  4732. crtc_state = drm_atomic_get_crtc_state(mst_state->base.state, crtc);
  4733. if (IS_ERR(crtc_state))
  4734. return PTR_ERR(crtc_state);
  4735. drm_dbg_atomic(mgr->dev, "[MST MGR:%p] Setting mode_changed flag on CRTC %p\n",
  4736. mgr, crtc);
  4737. crtc_state->mode_changed = true;
  4738. }
  4739. return 0;
  4740. }
  4741. EXPORT_SYMBOL(drm_dp_mst_add_affected_dsc_crtcs);
  4742. /**
  4743. * drm_dp_mst_atomic_enable_dsc - Set DSC Enable Flag to On/Off
  4744. * @state: Pointer to the new drm_atomic_state
  4745. * @port: Pointer to the affected MST Port
  4746. * @pbn: Newly recalculated bw required for link with DSC enabled
  4747. * @enable: Boolean flag to enable or disable DSC on the port
  4748. *
  4749. * This function enables DSC on the given Port
  4750. * by recalculating its vcpi from pbn provided
  4751. * and sets dsc_enable flag to keep track of which
  4752. * ports have DSC enabled
  4753. *
  4754. */
  4755. int drm_dp_mst_atomic_enable_dsc(struct drm_atomic_state *state,
  4756. struct drm_dp_mst_port *port,
  4757. int pbn, bool enable)
  4758. {
  4759. struct drm_dp_mst_topology_state *mst_state;
  4760. struct drm_dp_mst_atomic_payload *payload;
  4761. int time_slots = 0;
  4762. mst_state = drm_atomic_get_mst_topology_state(state, port->mgr);
  4763. if (IS_ERR(mst_state))
  4764. return PTR_ERR(mst_state);
  4765. payload = drm_atomic_get_mst_payload_state(mst_state, port);
  4766. if (!payload) {
  4767. drm_dbg_atomic(state->dev,
  4768. "[MST PORT:%p] Couldn't find payload in mst state %p\n",
  4769. port, mst_state);
  4770. return -EINVAL;
  4771. }
  4772. if (payload->dsc_enabled == enable) {
  4773. drm_dbg_atomic(state->dev,
  4774. "[MST PORT:%p] DSC flag is already set to %d, returning %d time slots\n",
  4775. port, enable, payload->time_slots);
  4776. time_slots = payload->time_slots;
  4777. }
  4778. if (enable) {
  4779. time_slots = drm_dp_atomic_find_time_slots(state, port->mgr, port, pbn);
  4780. drm_dbg_atomic(state->dev,
  4781. "[MST PORT:%p] Enabling DSC flag, reallocating %d time slots on the port\n",
  4782. port, time_slots);
  4783. if (time_slots < 0)
  4784. return -EINVAL;
  4785. }
  4786. payload->dsc_enabled = enable;
  4787. return time_slots;
  4788. }
  4789. EXPORT_SYMBOL(drm_dp_mst_atomic_enable_dsc);
  4790. /**
  4791. * drm_dp_mst_atomic_check_mgr - Check the atomic state of an MST topology manager
  4792. * @state: The global atomic state
  4793. * @mgr: Manager to check
  4794. * @mst_state: The MST atomic state for @mgr
  4795. * @failing_port: Returns the port with a BW limitation
  4796. *
  4797. * Checks the given MST manager's topology state for an atomic update to ensure
  4798. * that it's valid. This includes checking whether there's enough bandwidth to
  4799. * support the new timeslot allocations in the atomic update.
  4800. *
  4801. * Any atomic drivers supporting DP MST must make sure to call this or
  4802. * the drm_dp_mst_atomic_check() function after checking the rest of their state
  4803. * in their &drm_mode_config_funcs.atomic_check() callback.
  4804. *
  4805. * See also:
  4806. * drm_dp_mst_atomic_check()
  4807. * drm_dp_atomic_find_time_slots()
  4808. * drm_dp_atomic_release_time_slots()
  4809. *
  4810. * Returns:
  4811. * - 0 if the new state is valid
  4812. * - %-ENOSPC, if the new state is invalid, because of BW limitation
  4813. * @failing_port is set to:
  4814. *
  4815. * - The non-root port where a BW limit check failed
  4816. * with all the ports downstream of @failing_port passing
  4817. * the BW limit check.
  4818. * The returned port pointer is valid until at least
  4819. * one payload downstream of it exists.
  4820. * - %NULL if the BW limit check failed at the root port
  4821. * with all the ports downstream of the root port passing
  4822. * the BW limit check.
  4823. *
  4824. * - %-EINVAL, if the new state is invalid, because the root port has
  4825. * too many payloads.
  4826. */
  4827. int drm_dp_mst_atomic_check_mgr(struct drm_atomic_state *state,
  4828. struct drm_dp_mst_topology_mgr *mgr,
  4829. struct drm_dp_mst_topology_state *mst_state,
  4830. struct drm_dp_mst_port **failing_port)
  4831. {
  4832. int ret;
  4833. *failing_port = NULL;
  4834. if (!mgr->mst_state)
  4835. return 0;
  4836. mutex_lock(&mgr->lock);
  4837. ret = drm_dp_mst_atomic_check_mstb_bw_limit(mgr->mst_primary,
  4838. mst_state,
  4839. failing_port);
  4840. mutex_unlock(&mgr->lock);
  4841. if (ret < 0)
  4842. return ret;
  4843. return drm_dp_mst_atomic_check_payload_alloc_limits(mgr, mst_state);
  4844. }
  4845. EXPORT_SYMBOL(drm_dp_mst_atomic_check_mgr);
  4846. /**
  4847. * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an
  4848. * atomic update is valid
  4849. * @state: Pointer to the new &struct drm_dp_mst_topology_state
  4850. *
  4851. * Checks the given topology state for an atomic update to ensure that it's
  4852. * valid, calling drm_dp_mst_atomic_check_mgr() for all MST manager in the
  4853. * atomic state. This includes checking whether there's enough bandwidth to
  4854. * support the new timeslot allocations in the atomic update.
  4855. *
  4856. * Any atomic drivers supporting DP MST must make sure to call this after
  4857. * checking the rest of their state in their
  4858. * &drm_mode_config_funcs.atomic_check() callback.
  4859. *
  4860. * See also:
  4861. * drm_dp_mst_atomic_check_mgr()
  4862. * drm_dp_atomic_find_time_slots()
  4863. * drm_dp_atomic_release_time_slots()
  4864. *
  4865. * Returns:
  4866. * 0 if the new state is valid, negative error code otherwise.
  4867. */
  4868. int drm_dp_mst_atomic_check(struct drm_atomic_state *state)
  4869. {
  4870. struct drm_dp_mst_topology_mgr *mgr;
  4871. struct drm_dp_mst_topology_state *mst_state;
  4872. int i, ret = 0;
  4873. for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
  4874. struct drm_dp_mst_port *tmp_port;
  4875. ret = drm_dp_mst_atomic_check_mgr(state, mgr, mst_state, &tmp_port);
  4876. if (ret)
  4877. break;
  4878. }
  4879. return ret;
  4880. }
  4881. EXPORT_SYMBOL(drm_dp_mst_atomic_check);
  4882. const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = {
  4883. .atomic_duplicate_state = drm_dp_mst_duplicate_state,
  4884. .atomic_destroy_state = drm_dp_mst_destroy_state,
  4885. };
  4886. EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs);
  4887. /**
  4888. * drm_atomic_get_mst_topology_state: get MST topology state
  4889. * @state: global atomic state
  4890. * @mgr: MST topology manager, also the private object in this case
  4891. *
  4892. * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
  4893. * state vtable so that the private object state returned is that of a MST
  4894. * topology object.
  4895. *
  4896. * RETURNS:
  4897. * The MST topology state or error pointer.
  4898. */
  4899. struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
  4900. struct drm_dp_mst_topology_mgr *mgr)
  4901. {
  4902. return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
  4903. }
  4904. EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
  4905. /**
  4906. * drm_atomic_get_old_mst_topology_state: get old MST topology state in atomic state, if any
  4907. * @state: global atomic state
  4908. * @mgr: MST topology manager, also the private object in this case
  4909. *
  4910. * This function wraps drm_atomic_get_old_private_obj_state() passing in the MST atomic
  4911. * state vtable so that the private object state returned is that of a MST
  4912. * topology object.
  4913. *
  4914. * Returns:
  4915. * The old MST topology state, or NULL if there's no topology state for this MST mgr
  4916. * in the global atomic state
  4917. */
  4918. struct drm_dp_mst_topology_state *
  4919. drm_atomic_get_old_mst_topology_state(struct drm_atomic_state *state,
  4920. struct drm_dp_mst_topology_mgr *mgr)
  4921. {
  4922. struct drm_private_state *old_priv_state =
  4923. drm_atomic_get_old_private_obj_state(state, &mgr->base);
  4924. return old_priv_state ? to_dp_mst_topology_state(old_priv_state) : NULL;
  4925. }
  4926. EXPORT_SYMBOL(drm_atomic_get_old_mst_topology_state);
  4927. /**
  4928. * drm_atomic_get_new_mst_topology_state: get new MST topology state in atomic state, if any
  4929. * @state: global atomic state
  4930. * @mgr: MST topology manager, also the private object in this case
  4931. *
  4932. * This function wraps drm_atomic_get_new_private_obj_state() passing in the MST atomic
  4933. * state vtable so that the private object state returned is that of a MST
  4934. * topology object.
  4935. *
  4936. * Returns:
  4937. * The new MST topology state, or NULL if there's no topology state for this MST mgr
  4938. * in the global atomic state
  4939. */
  4940. struct drm_dp_mst_topology_state *
  4941. drm_atomic_get_new_mst_topology_state(struct drm_atomic_state *state,
  4942. struct drm_dp_mst_topology_mgr *mgr)
  4943. {
  4944. struct drm_private_state *new_priv_state =
  4945. drm_atomic_get_new_private_obj_state(state, &mgr->base);
  4946. return new_priv_state ? to_dp_mst_topology_state(new_priv_state) : NULL;
  4947. }
  4948. EXPORT_SYMBOL(drm_atomic_get_new_mst_topology_state);
  4949. /**
  4950. * drm_dp_mst_topology_mgr_init - initialise a topology manager
  4951. * @mgr: manager struct to initialise
  4952. * @dev: device providing this structure - for i2c addition.
  4953. * @aux: DP helper aux channel to talk to this device
  4954. * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
  4955. * @max_payloads: maximum number of payloads this GPU can source
  4956. * @conn_base_id: the connector object ID the MST device is connected to.
  4957. *
  4958. * Return 0 for success, or negative error code on failure
  4959. */
  4960. int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
  4961. struct drm_device *dev, struct drm_dp_aux *aux,
  4962. int max_dpcd_transaction_bytes, int max_payloads,
  4963. int conn_base_id)
  4964. {
  4965. struct drm_dp_mst_topology_state *mst_state;
  4966. mutex_init(&mgr->lock);
  4967. mutex_init(&mgr->qlock);
  4968. mutex_init(&mgr->delayed_destroy_lock);
  4969. mutex_init(&mgr->up_req_lock);
  4970. mutex_init(&mgr->probe_lock);
  4971. #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
  4972. mutex_init(&mgr->topology_ref_history_lock);
  4973. stack_depot_init();
  4974. #endif
  4975. INIT_LIST_HEAD(&mgr->tx_msg_downq);
  4976. INIT_LIST_HEAD(&mgr->destroy_port_list);
  4977. INIT_LIST_HEAD(&mgr->destroy_branch_device_list);
  4978. INIT_LIST_HEAD(&mgr->up_req_list);
  4979. /*
  4980. * delayed_destroy_work will be queued on a dedicated WQ, so that any
  4981. * requeuing will be also flushed when deiniting the topology manager.
  4982. */
  4983. mgr->delayed_destroy_wq = alloc_ordered_workqueue("drm_dp_mst_wq", 0);
  4984. if (mgr->delayed_destroy_wq == NULL)
  4985. return -ENOMEM;
  4986. INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
  4987. INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
  4988. INIT_WORK(&mgr->delayed_destroy_work, drm_dp_delayed_destroy_work);
  4989. INIT_WORK(&mgr->up_req_work, drm_dp_mst_up_req_work);
  4990. init_waitqueue_head(&mgr->tx_waitq);
  4991. mgr->dev = dev;
  4992. mgr->aux = aux;
  4993. mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
  4994. mgr->max_payloads = max_payloads;
  4995. mgr->conn_base_id = conn_base_id;
  4996. mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
  4997. if (mst_state == NULL)
  4998. return -ENOMEM;
  4999. mst_state->total_avail_slots = 63;
  5000. mst_state->start_slot = 1;
  5001. mst_state->mgr = mgr;
  5002. INIT_LIST_HEAD(&mst_state->payloads);
  5003. drm_atomic_private_obj_init(dev, &mgr->base,
  5004. &mst_state->base,
  5005. &drm_dp_mst_topology_state_funcs);
  5006. return 0;
  5007. }
  5008. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
  5009. /**
  5010. * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
  5011. * @mgr: manager to destroy
  5012. */
  5013. void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
  5014. {
  5015. drm_dp_mst_topology_mgr_set_mst(mgr, false);
  5016. flush_work(&mgr->work);
  5017. /* The following will also drain any requeued work on the WQ. */
  5018. if (mgr->delayed_destroy_wq) {
  5019. destroy_workqueue(mgr->delayed_destroy_wq);
  5020. mgr->delayed_destroy_wq = NULL;
  5021. }
  5022. mgr->dev = NULL;
  5023. mgr->aux = NULL;
  5024. drm_atomic_private_obj_fini(&mgr->base);
  5025. mgr->funcs = NULL;
  5026. mutex_destroy(&mgr->delayed_destroy_lock);
  5027. mutex_destroy(&mgr->qlock);
  5028. mutex_destroy(&mgr->lock);
  5029. mutex_destroy(&mgr->up_req_lock);
  5030. mutex_destroy(&mgr->probe_lock);
  5031. #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
  5032. mutex_destroy(&mgr->topology_ref_history_lock);
  5033. #endif
  5034. }
  5035. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
  5036. static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
  5037. {
  5038. int i;
  5039. if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
  5040. return false;
  5041. for (i = 0; i < num - 1; i++) {
  5042. if (msgs[i].flags & I2C_M_RD ||
  5043. msgs[i].len > 0xff)
  5044. return false;
  5045. }
  5046. return msgs[num - 1].flags & I2C_M_RD &&
  5047. msgs[num - 1].len <= 0xff;
  5048. }
  5049. static bool remote_i2c_write_ok(const struct i2c_msg msgs[], int num)
  5050. {
  5051. int i;
  5052. for (i = 0; i < num - 1; i++) {
  5053. if (msgs[i].flags & I2C_M_RD || !(msgs[i].flags & I2C_M_STOP) ||
  5054. msgs[i].len > 0xff)
  5055. return false;
  5056. }
  5057. return !(msgs[num - 1].flags & I2C_M_RD) && msgs[num - 1].len <= 0xff;
  5058. }
  5059. static int drm_dp_mst_i2c_read(struct drm_dp_mst_branch *mstb,
  5060. struct drm_dp_mst_port *port,
  5061. struct i2c_msg *msgs, int num)
  5062. {
  5063. struct drm_dp_mst_topology_mgr *mgr = port->mgr;
  5064. unsigned int i;
  5065. struct drm_dp_sideband_msg_req_body msg;
  5066. struct drm_dp_sideband_msg_tx *txmsg = NULL;
  5067. int ret;
  5068. memset(&msg, 0, sizeof(msg));
  5069. msg.req_type = DP_REMOTE_I2C_READ;
  5070. msg.u.i2c_read.num_transactions = num - 1;
  5071. msg.u.i2c_read.port_number = port->port_num;
  5072. for (i = 0; i < num - 1; i++) {
  5073. msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
  5074. msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
  5075. msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
  5076. msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
  5077. }
  5078. msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
  5079. msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
  5080. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  5081. if (!txmsg) {
  5082. ret = -ENOMEM;
  5083. goto out;
  5084. }
  5085. txmsg->dst = mstb;
  5086. drm_dp_encode_sideband_req(&msg, txmsg);
  5087. drm_dp_queue_down_tx(mgr, txmsg);
  5088. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  5089. if (ret > 0) {
  5090. if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
  5091. ret = -EREMOTEIO;
  5092. goto out;
  5093. }
  5094. if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
  5095. ret = -EIO;
  5096. goto out;
  5097. }
  5098. memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
  5099. ret = num;
  5100. }
  5101. out:
  5102. kfree(txmsg);
  5103. return ret;
  5104. }
  5105. static int drm_dp_mst_i2c_write(struct drm_dp_mst_branch *mstb,
  5106. struct drm_dp_mst_port *port,
  5107. struct i2c_msg *msgs, int num)
  5108. {
  5109. struct drm_dp_mst_topology_mgr *mgr = port->mgr;
  5110. unsigned int i;
  5111. struct drm_dp_sideband_msg_req_body msg;
  5112. struct drm_dp_sideband_msg_tx *txmsg = NULL;
  5113. int ret;
  5114. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  5115. if (!txmsg) {
  5116. ret = -ENOMEM;
  5117. goto out;
  5118. }
  5119. for (i = 0; i < num; i++) {
  5120. memset(&msg, 0, sizeof(msg));
  5121. msg.req_type = DP_REMOTE_I2C_WRITE;
  5122. msg.u.i2c_write.port_number = port->port_num;
  5123. msg.u.i2c_write.write_i2c_device_id = msgs[i].addr;
  5124. msg.u.i2c_write.num_bytes = msgs[i].len;
  5125. msg.u.i2c_write.bytes = msgs[i].buf;
  5126. memset(txmsg, 0, sizeof(*txmsg));
  5127. txmsg->dst = mstb;
  5128. drm_dp_encode_sideband_req(&msg, txmsg);
  5129. drm_dp_queue_down_tx(mgr, txmsg);
  5130. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  5131. if (ret > 0) {
  5132. if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
  5133. ret = -EREMOTEIO;
  5134. goto out;
  5135. }
  5136. } else {
  5137. goto out;
  5138. }
  5139. }
  5140. ret = num;
  5141. out:
  5142. kfree(txmsg);
  5143. return ret;
  5144. }
  5145. /* I2C device */
  5146. static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter,
  5147. struct i2c_msg *msgs, int num)
  5148. {
  5149. struct drm_dp_aux *aux = adapter->algo_data;
  5150. struct drm_dp_mst_port *port =
  5151. container_of(aux, struct drm_dp_mst_port, aux);
  5152. struct drm_dp_mst_branch *mstb;
  5153. struct drm_dp_mst_topology_mgr *mgr = port->mgr;
  5154. int ret;
  5155. mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
  5156. if (!mstb)
  5157. return -EREMOTEIO;
  5158. if (remote_i2c_read_ok(msgs, num)) {
  5159. ret = drm_dp_mst_i2c_read(mstb, port, msgs, num);
  5160. } else if (remote_i2c_write_ok(msgs, num)) {
  5161. ret = drm_dp_mst_i2c_write(mstb, port, msgs, num);
  5162. } else {
  5163. drm_dbg_kms(mgr->dev, "Unsupported I2C transaction for MST device\n");
  5164. ret = -EIO;
  5165. }
  5166. drm_dp_mst_topology_put_mstb(mstb);
  5167. return ret;
  5168. }
  5169. static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
  5170. {
  5171. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  5172. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  5173. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  5174. I2C_FUNC_10BIT_ADDR;
  5175. }
  5176. static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
  5177. .functionality = drm_dp_mst_i2c_functionality,
  5178. .master_xfer = drm_dp_mst_i2c_xfer,
  5179. };
  5180. /**
  5181. * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
  5182. * @port: The port to add the I2C bus on
  5183. *
  5184. * Returns 0 on success or a negative error code on failure.
  5185. */
  5186. static int drm_dp_mst_register_i2c_bus(struct drm_dp_mst_port *port)
  5187. {
  5188. struct drm_dp_aux *aux = &port->aux;
  5189. struct device *parent_dev = port->mgr->dev->dev;
  5190. aux->ddc.algo = &drm_dp_mst_i2c_algo;
  5191. aux->ddc.algo_data = aux;
  5192. aux->ddc.retries = 3;
  5193. aux->ddc.owner = THIS_MODULE;
  5194. /* FIXME: set the kdev of the port's connector as parent */
  5195. aux->ddc.dev.parent = parent_dev;
  5196. aux->ddc.dev.of_node = parent_dev->of_node;
  5197. strscpy(aux->ddc.name, aux->name ? aux->name : dev_name(parent_dev),
  5198. sizeof(aux->ddc.name));
  5199. return i2c_add_adapter(&aux->ddc);
  5200. }
  5201. /**
  5202. * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
  5203. * @port: The port to remove the I2C bus from
  5204. */
  5205. static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_mst_port *port)
  5206. {
  5207. i2c_del_adapter(&port->aux.ddc);
  5208. }
  5209. /**
  5210. * drm_dp_mst_is_virtual_dpcd() - Is the given port a virtual DP Peer Device
  5211. * @port: The port to check
  5212. *
  5213. * A single physical MST hub object can be represented in the topology
  5214. * by multiple branches, with virtual ports between those branches.
  5215. *
  5216. * As of DP1.4, An MST hub with internal (virtual) ports must expose
  5217. * certain DPCD registers over those ports. See sections 2.6.1.1.1
  5218. * and 2.6.1.1.2 of Display Port specification v1.4 for details.
  5219. *
  5220. * May acquire mgr->lock
  5221. *
  5222. * Returns:
  5223. * true if the port is a virtual DP peer device, false otherwise
  5224. */
  5225. static bool drm_dp_mst_is_virtual_dpcd(struct drm_dp_mst_port *port)
  5226. {
  5227. struct drm_dp_mst_port *downstream_port;
  5228. if (!port || port->dpcd_rev < DP_DPCD_REV_14)
  5229. return false;
  5230. /* Virtual DP Sink (Internal Display Panel) */
  5231. if (drm_dp_mst_port_is_logical(port))
  5232. return true;
  5233. /* DP-to-HDMI Protocol Converter */
  5234. if (port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV &&
  5235. !port->mcs &&
  5236. port->ldps)
  5237. return true;
  5238. /* DP-to-DP */
  5239. mutex_lock(&port->mgr->lock);
  5240. if (port->pdt == DP_PEER_DEVICE_MST_BRANCHING &&
  5241. port->mstb &&
  5242. port->mstb->num_ports == 2) {
  5243. list_for_each_entry(downstream_port, &port->mstb->ports, next) {
  5244. if (downstream_port->pdt == DP_PEER_DEVICE_SST_SINK &&
  5245. !downstream_port->input) {
  5246. mutex_unlock(&port->mgr->lock);
  5247. return true;
  5248. }
  5249. }
  5250. }
  5251. mutex_unlock(&port->mgr->lock);
  5252. return false;
  5253. }
  5254. /**
  5255. * drm_dp_mst_aux_for_parent() - Get the AUX device for an MST port's parent
  5256. * @port: MST port whose parent's AUX device is returned
  5257. *
  5258. * Return the AUX device for @port's parent or NULL if port's parent is the
  5259. * root port.
  5260. */
  5261. struct drm_dp_aux *drm_dp_mst_aux_for_parent(struct drm_dp_mst_port *port)
  5262. {
  5263. if (!port->parent || !port->parent->port_parent)
  5264. return NULL;
  5265. return &port->parent->port_parent->aux;
  5266. }
  5267. EXPORT_SYMBOL(drm_dp_mst_aux_for_parent);
  5268. /**
  5269. * drm_dp_mst_dsc_aux_for_port() - Find the correct aux for DSC
  5270. * @port: The port to check. A leaf of the MST tree with an attached display.
  5271. *
  5272. * Depending on the situation, DSC may be enabled via the endpoint aux,
  5273. * the immediately upstream aux, or the connector's physical aux.
  5274. *
  5275. * This is both the correct aux to read DSC_CAPABILITY and the
  5276. * correct aux to write DSC_ENABLED.
  5277. *
  5278. * This operation can be expensive (up to four aux reads), so
  5279. * the caller should cache the return.
  5280. *
  5281. * Returns:
  5282. * NULL if DSC cannot be enabled on this port, otherwise the aux device
  5283. */
  5284. struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port)
  5285. {
  5286. struct drm_dp_mst_port *immediate_upstream_port;
  5287. struct drm_dp_aux *immediate_upstream_aux;
  5288. struct drm_dp_mst_port *fec_port;
  5289. struct drm_dp_desc desc = {};
  5290. u8 upstream_dsc;
  5291. u8 endpoint_fec;
  5292. u8 endpoint_dsc;
  5293. if (!port)
  5294. return NULL;
  5295. if (port->parent->port_parent)
  5296. immediate_upstream_port = port->parent->port_parent;
  5297. else
  5298. immediate_upstream_port = NULL;
  5299. fec_port = immediate_upstream_port;
  5300. while (fec_port) {
  5301. /*
  5302. * Each physical link (i.e. not a virtual port) between the
  5303. * output and the primary device must support FEC
  5304. */
  5305. if (!drm_dp_mst_is_virtual_dpcd(fec_port) &&
  5306. !fec_port->fec_capable)
  5307. return NULL;
  5308. fec_port = fec_port->parent->port_parent;
  5309. }
  5310. /* DP-to-DP peer device */
  5311. if (drm_dp_mst_is_virtual_dpcd(immediate_upstream_port)) {
  5312. if (drm_dp_dpcd_read(&port->aux,
  5313. DP_DSC_SUPPORT, &endpoint_dsc, 1) != 1)
  5314. return NULL;
  5315. if (drm_dp_dpcd_read(&port->aux,
  5316. DP_FEC_CAPABILITY, &endpoint_fec, 1) != 1)
  5317. return NULL;
  5318. if (drm_dp_dpcd_read(&immediate_upstream_port->aux,
  5319. DP_DSC_SUPPORT, &upstream_dsc, 1) != 1)
  5320. return NULL;
  5321. /* Enpoint decompression with DP-to-DP peer device */
  5322. if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) &&
  5323. (endpoint_fec & DP_FEC_CAPABLE) &&
  5324. (upstream_dsc & DP_DSC_PASSTHROUGH_IS_SUPPORTED)) {
  5325. port->passthrough_aux = &immediate_upstream_port->aux;
  5326. return &port->aux;
  5327. }
  5328. /* Virtual DPCD decompression with DP-to-DP peer device */
  5329. return &immediate_upstream_port->aux;
  5330. }
  5331. /* Virtual DPCD decompression with DP-to-HDMI or Virtual DP Sink */
  5332. if (drm_dp_mst_is_virtual_dpcd(port))
  5333. return &port->aux;
  5334. /*
  5335. * Synaptics quirk
  5336. * Applies to ports for which:
  5337. * - Physical aux has Synaptics OUI
  5338. * - DPv1.4 or higher
  5339. * - Port is on primary branch device
  5340. * - Not a VGA adapter (DP_DWN_STRM_PORT_TYPE_ANALOG)
  5341. */
  5342. if (immediate_upstream_port)
  5343. immediate_upstream_aux = &immediate_upstream_port->aux;
  5344. else
  5345. immediate_upstream_aux = port->mgr->aux;
  5346. if (drm_dp_read_desc(immediate_upstream_aux, &desc, true))
  5347. return NULL;
  5348. if (drm_dp_has_quirk(&desc, DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD)) {
  5349. u8 dpcd_ext[DP_RECEIVER_CAP_SIZE];
  5350. if (drm_dp_dpcd_read(immediate_upstream_aux,
  5351. DP_DSC_SUPPORT, &upstream_dsc, 1) != 1)
  5352. return NULL;
  5353. if (!(upstream_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED))
  5354. return NULL;
  5355. if (drm_dp_read_dpcd_caps(immediate_upstream_aux, dpcd_ext) < 0)
  5356. return NULL;
  5357. if (dpcd_ext[DP_DPCD_REV] >= DP_DPCD_REV_14 &&
  5358. ((dpcd_ext[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT) &&
  5359. ((dpcd_ext[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK)
  5360. != DP_DWN_STRM_PORT_TYPE_ANALOG)))
  5361. return immediate_upstream_aux;
  5362. }
  5363. /*
  5364. * The check below verifies if the MST sink
  5365. * connected to the GPU is capable of DSC -
  5366. * therefore the endpoint needs to be
  5367. * both DSC and FEC capable.
  5368. */
  5369. if (drm_dp_dpcd_read(&port->aux,
  5370. DP_DSC_SUPPORT, &endpoint_dsc, 1) != 1)
  5371. return NULL;
  5372. if (drm_dp_dpcd_read(&port->aux,
  5373. DP_FEC_CAPABILITY, &endpoint_fec, 1) != 1)
  5374. return NULL;
  5375. if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) &&
  5376. (endpoint_fec & DP_FEC_CAPABLE))
  5377. return &port->aux;
  5378. return NULL;
  5379. }
  5380. EXPORT_SYMBOL(drm_dp_mst_dsc_aux_for_port);