intel_dp.c 173 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Keith Packard <keithp@keithp.com>
  25. *
  26. */
  27. #include <linux/i2c.h>
  28. #include <linux/slab.h>
  29. #include <linux/export.h>
  30. #include <linux/notifier.h>
  31. #include <linux/reboot.h>
  32. #include <drm/drmP.h>
  33. #include <drm/drm_atomic_helper.h>
  34. #include <drm/drm_crtc.h>
  35. #include <drm/drm_crtc_helper.h>
  36. #include <drm/drm_edid.h>
  37. #include "intel_drv.h"
  38. #include <drm/i915_drm.h>
  39. #include "i915_drv.h"
  40. #define DP_LINK_CHECK_TIMEOUT (10 * 1000)
  41. /* Compliance test status bits */
  42. #define INTEL_DP_RESOLUTION_SHIFT_MASK 0
  43. #define INTEL_DP_RESOLUTION_PREFERRED (1 << INTEL_DP_RESOLUTION_SHIFT_MASK)
  44. #define INTEL_DP_RESOLUTION_STANDARD (2 << INTEL_DP_RESOLUTION_SHIFT_MASK)
  45. #define INTEL_DP_RESOLUTION_FAILSAFE (3 << INTEL_DP_RESOLUTION_SHIFT_MASK)
  46. struct dp_link_dpll {
  47. int clock;
  48. struct dpll dpll;
  49. };
  50. static const struct dp_link_dpll gen4_dpll[] = {
  51. { 162000,
  52. { .p1 = 2, .p2 = 10, .n = 2, .m1 = 23, .m2 = 8 } },
  53. { 270000,
  54. { .p1 = 1, .p2 = 10, .n = 1, .m1 = 14, .m2 = 2 } }
  55. };
  56. static const struct dp_link_dpll pch_dpll[] = {
  57. { 162000,
  58. { .p1 = 2, .p2 = 10, .n = 1, .m1 = 12, .m2 = 9 } },
  59. { 270000,
  60. { .p1 = 1, .p2 = 10, .n = 2, .m1 = 14, .m2 = 8 } }
  61. };
  62. static const struct dp_link_dpll vlv_dpll[] = {
  63. { 162000,
  64. { .p1 = 3, .p2 = 2, .n = 5, .m1 = 3, .m2 = 81 } },
  65. { 270000,
  66. { .p1 = 2, .p2 = 2, .n = 1, .m1 = 2, .m2 = 27 } }
  67. };
  68. /*
  69. * CHV supports eDP 1.4 that have more link rates.
  70. * Below only provides the fixed rate but exclude variable rate.
  71. */
  72. static const struct dp_link_dpll chv_dpll[] = {
  73. /*
  74. * CHV requires to program fractional division for m2.
  75. * m2 is stored in fixed point format using formula below
  76. * (m2_int << 22) | m2_fraction
  77. */
  78. { 162000, /* m2_int = 32, m2_fraction = 1677722 */
  79. { .p1 = 4, .p2 = 2, .n = 1, .m1 = 2, .m2 = 0x819999a } },
  80. { 270000, /* m2_int = 27, m2_fraction = 0 */
  81. { .p1 = 4, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } },
  82. { 540000, /* m2_int = 27, m2_fraction = 0 */
  83. { .p1 = 2, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } }
  84. };
  85. static const int bxt_rates[] = { 162000, 216000, 243000, 270000,
  86. 324000, 432000, 540000 };
  87. static const int skl_rates[] = { 162000, 216000, 270000,
  88. 324000, 432000, 540000 };
  89. static const int default_rates[] = { 162000, 270000, 540000 };
  90. /**
  91. * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
  92. * @intel_dp: DP struct
  93. *
  94. * If a CPU or PCH DP output is attached to an eDP panel, this function
  95. * will return true, and false otherwise.
  96. */
  97. static bool is_edp(struct intel_dp *intel_dp)
  98. {
  99. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  100. return intel_dig_port->base.type == INTEL_OUTPUT_EDP;
  101. }
  102. static struct drm_device *intel_dp_to_dev(struct intel_dp *intel_dp)
  103. {
  104. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  105. return intel_dig_port->base.base.dev;
  106. }
  107. static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
  108. {
  109. return enc_to_intel_dp(&intel_attached_encoder(connector)->base);
  110. }
  111. static void intel_dp_link_down(struct intel_dp *intel_dp);
  112. static bool edp_panel_vdd_on(struct intel_dp *intel_dp);
  113. static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync);
  114. static void vlv_init_panel_power_sequencer(struct intel_dp *intel_dp);
  115. static void vlv_steal_power_sequencer(struct drm_device *dev,
  116. enum pipe pipe);
  117. static unsigned int intel_dp_unused_lane_mask(int lane_count)
  118. {
  119. return ~((1 << lane_count) - 1) & 0xf;
  120. }
  121. static int
  122. intel_dp_max_link_bw(struct intel_dp *intel_dp)
  123. {
  124. int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
  125. switch (max_link_bw) {
  126. case DP_LINK_BW_1_62:
  127. case DP_LINK_BW_2_7:
  128. case DP_LINK_BW_5_4:
  129. break;
  130. default:
  131. WARN(1, "invalid max DP link bw val %x, using 1.62Gbps\n",
  132. max_link_bw);
  133. max_link_bw = DP_LINK_BW_1_62;
  134. break;
  135. }
  136. return max_link_bw;
  137. }
  138. static u8 intel_dp_max_lane_count(struct intel_dp *intel_dp)
  139. {
  140. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  141. struct drm_device *dev = intel_dig_port->base.base.dev;
  142. u8 source_max, sink_max;
  143. source_max = 4;
  144. if (HAS_DDI(dev) && intel_dig_port->port == PORT_A &&
  145. (intel_dig_port->saved_port_bits & DDI_A_4_LANES) == 0)
  146. source_max = 2;
  147. sink_max = drm_dp_max_lane_count(intel_dp->dpcd);
  148. return min(source_max, sink_max);
  149. }
  150. /*
  151. * The units on the numbers in the next two are... bizarre. Examples will
  152. * make it clearer; this one parallels an example in the eDP spec.
  153. *
  154. * intel_dp_max_data_rate for one lane of 2.7GHz evaluates as:
  155. *
  156. * 270000 * 1 * 8 / 10 == 216000
  157. *
  158. * The actual data capacity of that configuration is 2.16Gbit/s, so the
  159. * units are decakilobits. ->clock in a drm_display_mode is in kilohertz -
  160. * or equivalently, kilopixels per second - so for 1680x1050R it'd be
  161. * 119000. At 18bpp that's 2142000 kilobits per second.
  162. *
  163. * Thus the strange-looking division by 10 in intel_dp_link_required, to
  164. * get the result in decakilobits instead of kilobits.
  165. */
  166. static int
  167. intel_dp_link_required(int pixel_clock, int bpp)
  168. {
  169. return (pixel_clock * bpp + 9) / 10;
  170. }
  171. static int
  172. intel_dp_max_data_rate(int max_link_clock, int max_lanes)
  173. {
  174. return (max_link_clock * max_lanes * 8) / 10;
  175. }
  176. static enum drm_mode_status
  177. intel_dp_mode_valid(struct drm_connector *connector,
  178. struct drm_display_mode *mode)
  179. {
  180. struct intel_dp *intel_dp = intel_attached_dp(connector);
  181. struct intel_connector *intel_connector = to_intel_connector(connector);
  182. struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
  183. int target_clock = mode->clock;
  184. int max_rate, mode_rate, max_lanes, max_link_clock;
  185. if (is_edp(intel_dp) && fixed_mode) {
  186. if (mode->hdisplay > fixed_mode->hdisplay)
  187. return MODE_PANEL;
  188. if (mode->vdisplay > fixed_mode->vdisplay)
  189. return MODE_PANEL;
  190. target_clock = fixed_mode->clock;
  191. }
  192. max_link_clock = intel_dp_max_link_rate(intel_dp);
  193. max_lanes = intel_dp_max_lane_count(intel_dp);
  194. max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
  195. mode_rate = intel_dp_link_required(target_clock, 18);
  196. if (mode_rate > max_rate)
  197. return MODE_CLOCK_HIGH;
  198. if (mode->clock < 10000)
  199. return MODE_CLOCK_LOW;
  200. if (mode->flags & DRM_MODE_FLAG_DBLCLK)
  201. return MODE_H_ILLEGAL;
  202. return MODE_OK;
  203. }
  204. uint32_t intel_dp_pack_aux(const uint8_t *src, int src_bytes)
  205. {
  206. int i;
  207. uint32_t v = 0;
  208. if (src_bytes > 4)
  209. src_bytes = 4;
  210. for (i = 0; i < src_bytes; i++)
  211. v |= ((uint32_t) src[i]) << ((3-i) * 8);
  212. return v;
  213. }
  214. static void intel_dp_unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
  215. {
  216. int i;
  217. if (dst_bytes > 4)
  218. dst_bytes = 4;
  219. for (i = 0; i < dst_bytes; i++)
  220. dst[i] = src >> ((3-i) * 8);
  221. }
  222. static void
  223. intel_dp_init_panel_power_sequencer(struct drm_device *dev,
  224. struct intel_dp *intel_dp);
  225. static void
  226. intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
  227. struct intel_dp *intel_dp);
  228. static void pps_lock(struct intel_dp *intel_dp)
  229. {
  230. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  231. struct intel_encoder *encoder = &intel_dig_port->base;
  232. struct drm_device *dev = encoder->base.dev;
  233. struct drm_i915_private *dev_priv = dev->dev_private;
  234. enum intel_display_power_domain power_domain;
  235. /*
  236. * See vlv_power_sequencer_reset() why we need
  237. * a power domain reference here.
  238. */
  239. power_domain = intel_display_port_aux_power_domain(encoder);
  240. intel_display_power_get(dev_priv, power_domain);
  241. mutex_lock(&dev_priv->pps_mutex);
  242. }
  243. static void pps_unlock(struct intel_dp *intel_dp)
  244. {
  245. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  246. struct intel_encoder *encoder = &intel_dig_port->base;
  247. struct drm_device *dev = encoder->base.dev;
  248. struct drm_i915_private *dev_priv = dev->dev_private;
  249. enum intel_display_power_domain power_domain;
  250. mutex_unlock(&dev_priv->pps_mutex);
  251. power_domain = intel_display_port_aux_power_domain(encoder);
  252. intel_display_power_put(dev_priv, power_domain);
  253. }
  254. static void
  255. vlv_power_sequencer_kick(struct intel_dp *intel_dp)
  256. {
  257. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  258. struct drm_device *dev = intel_dig_port->base.base.dev;
  259. struct drm_i915_private *dev_priv = dev->dev_private;
  260. enum pipe pipe = intel_dp->pps_pipe;
  261. bool pll_enabled, release_cl_override = false;
  262. enum dpio_phy phy = DPIO_PHY(pipe);
  263. enum dpio_channel ch = vlv_pipe_to_channel(pipe);
  264. uint32_t DP;
  265. if (WARN(I915_READ(intel_dp->output_reg) & DP_PORT_EN,
  266. "skipping pipe %c power seqeuncer kick due to port %c being active\n",
  267. pipe_name(pipe), port_name(intel_dig_port->port)))
  268. return;
  269. DRM_DEBUG_KMS("kicking pipe %c power sequencer for port %c\n",
  270. pipe_name(pipe), port_name(intel_dig_port->port));
  271. /* Preserve the BIOS-computed detected bit. This is
  272. * supposed to be read-only.
  273. */
  274. DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
  275. DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
  276. DP |= DP_PORT_WIDTH(1);
  277. DP |= DP_LINK_TRAIN_PAT_1;
  278. if (IS_CHERRYVIEW(dev))
  279. DP |= DP_PIPE_SELECT_CHV(pipe);
  280. else if (pipe == PIPE_B)
  281. DP |= DP_PIPEB_SELECT;
  282. pll_enabled = I915_READ(DPLL(pipe)) & DPLL_VCO_ENABLE;
  283. /*
  284. * The DPLL for the pipe must be enabled for this to work.
  285. * So enable temporarily it if it's not already enabled.
  286. */
  287. if (!pll_enabled) {
  288. release_cl_override = IS_CHERRYVIEW(dev) &&
  289. !chv_phy_powergate_ch(dev_priv, phy, ch, true);
  290. vlv_force_pll_on(dev, pipe, IS_CHERRYVIEW(dev) ?
  291. &chv_dpll[0].dpll : &vlv_dpll[0].dpll);
  292. }
  293. /*
  294. * Similar magic as in intel_dp_enable_port().
  295. * We _must_ do this port enable + disable trick
  296. * to make this power seqeuencer lock onto the port.
  297. * Otherwise even VDD force bit won't work.
  298. */
  299. I915_WRITE(intel_dp->output_reg, DP);
  300. POSTING_READ(intel_dp->output_reg);
  301. I915_WRITE(intel_dp->output_reg, DP | DP_PORT_EN);
  302. POSTING_READ(intel_dp->output_reg);
  303. I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
  304. POSTING_READ(intel_dp->output_reg);
  305. if (!pll_enabled) {
  306. vlv_force_pll_off(dev, pipe);
  307. if (release_cl_override)
  308. chv_phy_powergate_ch(dev_priv, phy, ch, false);
  309. }
  310. }
  311. static enum pipe
  312. vlv_power_sequencer_pipe(struct intel_dp *intel_dp)
  313. {
  314. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  315. struct drm_device *dev = intel_dig_port->base.base.dev;
  316. struct drm_i915_private *dev_priv = dev->dev_private;
  317. struct intel_encoder *encoder;
  318. unsigned int pipes = (1 << PIPE_A) | (1 << PIPE_B);
  319. enum pipe pipe;
  320. lockdep_assert_held(&dev_priv->pps_mutex);
  321. /* We should never land here with regular DP ports */
  322. WARN_ON(!is_edp(intel_dp));
  323. if (intel_dp->pps_pipe != INVALID_PIPE)
  324. return intel_dp->pps_pipe;
  325. /*
  326. * We don't have power sequencer currently.
  327. * Pick one that's not used by other ports.
  328. */
  329. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  330. base.head) {
  331. struct intel_dp *tmp;
  332. if (encoder->type != INTEL_OUTPUT_EDP)
  333. continue;
  334. tmp = enc_to_intel_dp(&encoder->base);
  335. if (tmp->pps_pipe != INVALID_PIPE)
  336. pipes &= ~(1 << tmp->pps_pipe);
  337. }
  338. /*
  339. * Didn't find one. This should not happen since there
  340. * are two power sequencers and up to two eDP ports.
  341. */
  342. if (WARN_ON(pipes == 0))
  343. pipe = PIPE_A;
  344. else
  345. pipe = ffs(pipes) - 1;
  346. vlv_steal_power_sequencer(dev, pipe);
  347. intel_dp->pps_pipe = pipe;
  348. DRM_DEBUG_KMS("picked pipe %c power sequencer for port %c\n",
  349. pipe_name(intel_dp->pps_pipe),
  350. port_name(intel_dig_port->port));
  351. /* init power sequencer on this pipe and port */
  352. intel_dp_init_panel_power_sequencer(dev, intel_dp);
  353. intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
  354. /*
  355. * Even vdd force doesn't work until we've made
  356. * the power sequencer lock in on the port.
  357. */
  358. vlv_power_sequencer_kick(intel_dp);
  359. return intel_dp->pps_pipe;
  360. }
  361. typedef bool (*vlv_pipe_check)(struct drm_i915_private *dev_priv,
  362. enum pipe pipe);
  363. static bool vlv_pipe_has_pp_on(struct drm_i915_private *dev_priv,
  364. enum pipe pipe)
  365. {
  366. return I915_READ(VLV_PIPE_PP_STATUS(pipe)) & PP_ON;
  367. }
  368. static bool vlv_pipe_has_vdd_on(struct drm_i915_private *dev_priv,
  369. enum pipe pipe)
  370. {
  371. return I915_READ(VLV_PIPE_PP_CONTROL(pipe)) & EDP_FORCE_VDD;
  372. }
  373. static bool vlv_pipe_any(struct drm_i915_private *dev_priv,
  374. enum pipe pipe)
  375. {
  376. return true;
  377. }
  378. static enum pipe
  379. vlv_initial_pps_pipe(struct drm_i915_private *dev_priv,
  380. enum port port,
  381. vlv_pipe_check pipe_check)
  382. {
  383. enum pipe pipe;
  384. for (pipe = PIPE_A; pipe <= PIPE_B; pipe++) {
  385. u32 port_sel = I915_READ(VLV_PIPE_PP_ON_DELAYS(pipe)) &
  386. PANEL_PORT_SELECT_MASK;
  387. if (port_sel != PANEL_PORT_SELECT_VLV(port))
  388. continue;
  389. if (!pipe_check(dev_priv, pipe))
  390. continue;
  391. return pipe;
  392. }
  393. return INVALID_PIPE;
  394. }
  395. static void
  396. vlv_initial_power_sequencer_setup(struct intel_dp *intel_dp)
  397. {
  398. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  399. struct drm_device *dev = intel_dig_port->base.base.dev;
  400. struct drm_i915_private *dev_priv = dev->dev_private;
  401. enum port port = intel_dig_port->port;
  402. lockdep_assert_held(&dev_priv->pps_mutex);
  403. /* try to find a pipe with this port selected */
  404. /* first pick one where the panel is on */
  405. intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
  406. vlv_pipe_has_pp_on);
  407. /* didn't find one? pick one where vdd is on */
  408. if (intel_dp->pps_pipe == INVALID_PIPE)
  409. intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
  410. vlv_pipe_has_vdd_on);
  411. /* didn't find one? pick one with just the correct port */
  412. if (intel_dp->pps_pipe == INVALID_PIPE)
  413. intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
  414. vlv_pipe_any);
  415. /* didn't find one? just let vlv_power_sequencer_pipe() pick one when needed */
  416. if (intel_dp->pps_pipe == INVALID_PIPE) {
  417. DRM_DEBUG_KMS("no initial power sequencer for port %c\n",
  418. port_name(port));
  419. return;
  420. }
  421. DRM_DEBUG_KMS("initial power sequencer for port %c: pipe %c\n",
  422. port_name(port), pipe_name(intel_dp->pps_pipe));
  423. intel_dp_init_panel_power_sequencer(dev, intel_dp);
  424. intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
  425. }
  426. void vlv_power_sequencer_reset(struct drm_i915_private *dev_priv)
  427. {
  428. struct drm_device *dev = dev_priv->dev;
  429. struct intel_encoder *encoder;
  430. if (WARN_ON(!IS_VALLEYVIEW(dev)))
  431. return;
  432. /*
  433. * We can't grab pps_mutex here due to deadlock with power_domain
  434. * mutex when power_domain functions are called while holding pps_mutex.
  435. * That also means that in order to use pps_pipe the code needs to
  436. * hold both a power domain reference and pps_mutex, and the power domain
  437. * reference get/put must be done while _not_ holding pps_mutex.
  438. * pps_{lock,unlock}() do these steps in the correct order, so one
  439. * should use them always.
  440. */
  441. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  442. struct intel_dp *intel_dp;
  443. if (encoder->type != INTEL_OUTPUT_EDP)
  444. continue;
  445. intel_dp = enc_to_intel_dp(&encoder->base);
  446. intel_dp->pps_pipe = INVALID_PIPE;
  447. }
  448. }
  449. static u32 _pp_ctrl_reg(struct intel_dp *intel_dp)
  450. {
  451. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  452. if (IS_BROXTON(dev))
  453. return BXT_PP_CONTROL(0);
  454. else if (HAS_PCH_SPLIT(dev))
  455. return PCH_PP_CONTROL;
  456. else
  457. return VLV_PIPE_PP_CONTROL(vlv_power_sequencer_pipe(intel_dp));
  458. }
  459. static u32 _pp_stat_reg(struct intel_dp *intel_dp)
  460. {
  461. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  462. if (IS_BROXTON(dev))
  463. return BXT_PP_STATUS(0);
  464. else if (HAS_PCH_SPLIT(dev))
  465. return PCH_PP_STATUS;
  466. else
  467. return VLV_PIPE_PP_STATUS(vlv_power_sequencer_pipe(intel_dp));
  468. }
  469. /* Reboot notifier handler to shutdown panel power to guarantee T12 timing
  470. This function only applicable when panel PM state is not to be tracked */
  471. static int edp_notify_handler(struct notifier_block *this, unsigned long code,
  472. void *unused)
  473. {
  474. struct intel_dp *intel_dp = container_of(this, typeof(* intel_dp),
  475. edp_notifier);
  476. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  477. struct drm_i915_private *dev_priv = dev->dev_private;
  478. if (!is_edp(intel_dp) || code != SYS_RESTART)
  479. return 0;
  480. pps_lock(intel_dp);
  481. if (IS_VALLEYVIEW(dev)) {
  482. enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
  483. u32 pp_ctrl_reg, pp_div_reg;
  484. u32 pp_div;
  485. pp_ctrl_reg = VLV_PIPE_PP_CONTROL(pipe);
  486. pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
  487. pp_div = I915_READ(pp_div_reg);
  488. pp_div &= PP_REFERENCE_DIVIDER_MASK;
  489. /* 0x1F write to PP_DIV_REG sets max cycle delay */
  490. I915_WRITE(pp_div_reg, pp_div | 0x1F);
  491. I915_WRITE(pp_ctrl_reg, PANEL_UNLOCK_REGS | PANEL_POWER_OFF);
  492. msleep(intel_dp->panel_power_cycle_delay);
  493. }
  494. pps_unlock(intel_dp);
  495. return 0;
  496. }
  497. static bool edp_have_panel_power(struct intel_dp *intel_dp)
  498. {
  499. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  500. struct drm_i915_private *dev_priv = dev->dev_private;
  501. lockdep_assert_held(&dev_priv->pps_mutex);
  502. if (IS_VALLEYVIEW(dev) &&
  503. intel_dp->pps_pipe == INVALID_PIPE)
  504. return false;
  505. return (I915_READ(_pp_stat_reg(intel_dp)) & PP_ON) != 0;
  506. }
  507. static bool edp_have_panel_vdd(struct intel_dp *intel_dp)
  508. {
  509. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  510. struct drm_i915_private *dev_priv = dev->dev_private;
  511. lockdep_assert_held(&dev_priv->pps_mutex);
  512. if (IS_VALLEYVIEW(dev) &&
  513. intel_dp->pps_pipe == INVALID_PIPE)
  514. return false;
  515. return I915_READ(_pp_ctrl_reg(intel_dp)) & EDP_FORCE_VDD;
  516. }
  517. static void
  518. intel_dp_check_edp(struct intel_dp *intel_dp)
  519. {
  520. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  521. struct drm_i915_private *dev_priv = dev->dev_private;
  522. if (!is_edp(intel_dp))
  523. return;
  524. if (!edp_have_panel_power(intel_dp) && !edp_have_panel_vdd(intel_dp)) {
  525. WARN(1, "eDP powered off while attempting aux channel communication.\n");
  526. DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
  527. I915_READ(_pp_stat_reg(intel_dp)),
  528. I915_READ(_pp_ctrl_reg(intel_dp)));
  529. }
  530. }
  531. static uint32_t
  532. intel_dp_aux_wait_done(struct intel_dp *intel_dp, bool has_aux_irq)
  533. {
  534. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  535. struct drm_device *dev = intel_dig_port->base.base.dev;
  536. struct drm_i915_private *dev_priv = dev->dev_private;
  537. uint32_t ch_ctl = intel_dp->aux_ch_ctl_reg;
  538. uint32_t status;
  539. bool done;
  540. #define C (((status = I915_READ_NOTRACE(ch_ctl)) & DP_AUX_CH_CTL_SEND_BUSY) == 0)
  541. if (has_aux_irq)
  542. done = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
  543. msecs_to_jiffies_timeout(10));
  544. else
  545. done = wait_for_atomic(C, 10) == 0;
  546. if (!done)
  547. DRM_ERROR("dp aux hw did not signal timeout (has irq: %i)!\n",
  548. has_aux_irq);
  549. #undef C
  550. return status;
  551. }
  552. static uint32_t i9xx_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
  553. {
  554. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  555. struct drm_device *dev = intel_dig_port->base.base.dev;
  556. /*
  557. * The clock divider is based off the hrawclk, and would like to run at
  558. * 2MHz. So, take the hrawclk value and divide by 2 and use that
  559. */
  560. return index ? 0 : intel_hrawclk(dev) / 2;
  561. }
  562. static uint32_t ilk_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
  563. {
  564. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  565. struct drm_device *dev = intel_dig_port->base.base.dev;
  566. struct drm_i915_private *dev_priv = dev->dev_private;
  567. if (index)
  568. return 0;
  569. if (intel_dig_port->port == PORT_A) {
  570. return DIV_ROUND_UP(dev_priv->cdclk_freq, 2000);
  571. } else {
  572. return DIV_ROUND_UP(intel_pch_rawclk(dev), 2);
  573. }
  574. }
  575. static uint32_t hsw_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
  576. {
  577. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  578. struct drm_device *dev = intel_dig_port->base.base.dev;
  579. struct drm_i915_private *dev_priv = dev->dev_private;
  580. if (intel_dig_port->port == PORT_A) {
  581. if (index)
  582. return 0;
  583. return DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 2000);
  584. } else if (dev_priv->pch_id == INTEL_PCH_LPT_DEVICE_ID_TYPE) {
  585. /* Workaround for non-ULT HSW */
  586. switch (index) {
  587. case 0: return 63;
  588. case 1: return 72;
  589. default: return 0;
  590. }
  591. } else {
  592. return index ? 0 : DIV_ROUND_UP(intel_pch_rawclk(dev), 2);
  593. }
  594. }
  595. static uint32_t vlv_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
  596. {
  597. return index ? 0 : 100;
  598. }
  599. static uint32_t skl_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
  600. {
  601. /*
  602. * SKL doesn't need us to program the AUX clock divider (Hardware will
  603. * derive the clock from CDCLK automatically). We still implement the
  604. * get_aux_clock_divider vfunc to plug-in into the existing code.
  605. */
  606. return index ? 0 : 1;
  607. }
  608. static uint32_t i9xx_get_aux_send_ctl(struct intel_dp *intel_dp,
  609. bool has_aux_irq,
  610. int send_bytes,
  611. uint32_t aux_clock_divider)
  612. {
  613. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  614. struct drm_device *dev = intel_dig_port->base.base.dev;
  615. uint32_t precharge, timeout;
  616. if (IS_GEN6(dev))
  617. precharge = 3;
  618. else
  619. precharge = 5;
  620. if (IS_BROADWELL(dev) && intel_dp->aux_ch_ctl_reg == DPA_AUX_CH_CTL)
  621. timeout = DP_AUX_CH_CTL_TIME_OUT_600us;
  622. else
  623. timeout = DP_AUX_CH_CTL_TIME_OUT_400us;
  624. return DP_AUX_CH_CTL_SEND_BUSY |
  625. DP_AUX_CH_CTL_DONE |
  626. (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
  627. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  628. timeout |
  629. DP_AUX_CH_CTL_RECEIVE_ERROR |
  630. (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
  631. (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
  632. (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT);
  633. }
  634. static uint32_t skl_get_aux_send_ctl(struct intel_dp *intel_dp,
  635. bool has_aux_irq,
  636. int send_bytes,
  637. uint32_t unused)
  638. {
  639. return DP_AUX_CH_CTL_SEND_BUSY |
  640. DP_AUX_CH_CTL_DONE |
  641. (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
  642. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  643. DP_AUX_CH_CTL_TIME_OUT_1600us |
  644. DP_AUX_CH_CTL_RECEIVE_ERROR |
  645. (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
  646. DP_AUX_CH_CTL_SYNC_PULSE_SKL(32);
  647. }
  648. static int
  649. intel_dp_aux_ch(struct intel_dp *intel_dp,
  650. const uint8_t *send, int send_bytes,
  651. uint8_t *recv, int recv_size)
  652. {
  653. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  654. struct drm_device *dev = intel_dig_port->base.base.dev;
  655. struct drm_i915_private *dev_priv = dev->dev_private;
  656. uint32_t ch_ctl = intel_dp->aux_ch_ctl_reg;
  657. uint32_t ch_data = ch_ctl + 4;
  658. uint32_t aux_clock_divider;
  659. int i, ret, recv_bytes;
  660. uint32_t status;
  661. int try, clock = 0;
  662. bool has_aux_irq = HAS_AUX_IRQ(dev);
  663. bool vdd;
  664. pps_lock(intel_dp);
  665. /*
  666. * We will be called with VDD already enabled for dpcd/edid/oui reads.
  667. * In such cases we want to leave VDD enabled and it's up to upper layers
  668. * to turn it off. But for eg. i2c-dev access we need to turn it on/off
  669. * ourselves.
  670. */
  671. vdd = edp_panel_vdd_on(intel_dp);
  672. /* dp aux is extremely sensitive to irq latency, hence request the
  673. * lowest possible wakeup latency and so prevent the cpu from going into
  674. * deep sleep states.
  675. */
  676. pm_qos_update_request(&dev_priv->pm_qos, 0);
  677. intel_dp_check_edp(intel_dp);
  678. /* Try to wait for any previous AUX channel activity */
  679. for (try = 0; try < 3; try++) {
  680. status = I915_READ_NOTRACE(ch_ctl);
  681. if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
  682. break;
  683. msleep(1);
  684. }
  685. if (try == 3) {
  686. static u32 last_status = -1;
  687. const u32 status = I915_READ(ch_ctl);
  688. if (status != last_status) {
  689. WARN(1, "dp_aux_ch not started status 0x%08x\n",
  690. status);
  691. last_status = status;
  692. }
  693. ret = -EBUSY;
  694. goto out;
  695. }
  696. /* Only 5 data registers! */
  697. if (WARN_ON(send_bytes > 20 || recv_size > 20)) {
  698. ret = -E2BIG;
  699. goto out;
  700. }
  701. while ((aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, clock++))) {
  702. u32 send_ctl = intel_dp->get_aux_send_ctl(intel_dp,
  703. has_aux_irq,
  704. send_bytes,
  705. aux_clock_divider);
  706. /* Must try at least 3 times according to DP spec */
  707. for (try = 0; try < 5; try++) {
  708. /* Load the send data into the aux channel data registers */
  709. for (i = 0; i < send_bytes; i += 4)
  710. I915_WRITE(ch_data + i,
  711. intel_dp_pack_aux(send + i,
  712. send_bytes - i));
  713. /* Send the command and wait for it to complete */
  714. I915_WRITE(ch_ctl, send_ctl);
  715. status = intel_dp_aux_wait_done(intel_dp, has_aux_irq);
  716. /* Clear done status and any errors */
  717. I915_WRITE(ch_ctl,
  718. status |
  719. DP_AUX_CH_CTL_DONE |
  720. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  721. DP_AUX_CH_CTL_RECEIVE_ERROR);
  722. if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR)
  723. continue;
  724. /* DP CTS 1.2 Core Rev 1.1, 4.2.1.1 & 4.2.1.2
  725. * 400us delay required for errors and timeouts
  726. * Timeout errors from the HW already meet this
  727. * requirement so skip to next iteration
  728. */
  729. if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
  730. usleep_range(400, 500);
  731. continue;
  732. }
  733. if (status & DP_AUX_CH_CTL_DONE)
  734. goto done;
  735. }
  736. }
  737. if ((status & DP_AUX_CH_CTL_DONE) == 0) {
  738. DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
  739. ret = -EBUSY;
  740. goto out;
  741. }
  742. done:
  743. /* Check for timeout or receive error.
  744. * Timeouts occur when the sink is not connected
  745. */
  746. if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
  747. DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
  748. ret = -EIO;
  749. goto out;
  750. }
  751. /* Timeouts occur when the device isn't connected, so they're
  752. * "normal" -- don't fill the kernel log with these */
  753. if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
  754. DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
  755. ret = -ETIMEDOUT;
  756. goto out;
  757. }
  758. /* Unload any bytes sent back from the other side */
  759. recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
  760. DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
  761. if (recv_bytes > recv_size)
  762. recv_bytes = recv_size;
  763. for (i = 0; i < recv_bytes; i += 4)
  764. intel_dp_unpack_aux(I915_READ(ch_data + i),
  765. recv + i, recv_bytes - i);
  766. ret = recv_bytes;
  767. out:
  768. pm_qos_update_request(&dev_priv->pm_qos, PM_QOS_DEFAULT_VALUE);
  769. if (vdd)
  770. edp_panel_vdd_off(intel_dp, false);
  771. pps_unlock(intel_dp);
  772. return ret;
  773. }
  774. #define BARE_ADDRESS_SIZE 3
  775. #define HEADER_SIZE (BARE_ADDRESS_SIZE + 1)
  776. static ssize_t
  777. intel_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
  778. {
  779. struct intel_dp *intel_dp = container_of(aux, struct intel_dp, aux);
  780. uint8_t txbuf[20], rxbuf[20];
  781. size_t txsize, rxsize;
  782. int ret;
  783. txbuf[0] = (msg->request << 4) |
  784. ((msg->address >> 16) & 0xf);
  785. txbuf[1] = (msg->address >> 8) & 0xff;
  786. txbuf[2] = msg->address & 0xff;
  787. txbuf[3] = msg->size - 1;
  788. switch (msg->request & ~DP_AUX_I2C_MOT) {
  789. case DP_AUX_NATIVE_WRITE:
  790. case DP_AUX_I2C_WRITE:
  791. case DP_AUX_I2C_WRITE_STATUS_UPDATE:
  792. txsize = msg->size ? HEADER_SIZE + msg->size : BARE_ADDRESS_SIZE;
  793. rxsize = 2; /* 0 or 1 data bytes */
  794. if (WARN_ON(txsize > 20))
  795. return -E2BIG;
  796. memcpy(txbuf + HEADER_SIZE, msg->buffer, msg->size);
  797. ret = intel_dp_aux_ch(intel_dp, txbuf, txsize, rxbuf, rxsize);
  798. if (ret > 0) {
  799. msg->reply = rxbuf[0] >> 4;
  800. if (ret > 1) {
  801. /* Number of bytes written in a short write. */
  802. ret = clamp_t(int, rxbuf[1], 0, msg->size);
  803. } else {
  804. /* Return payload size. */
  805. ret = msg->size;
  806. }
  807. }
  808. break;
  809. case DP_AUX_NATIVE_READ:
  810. case DP_AUX_I2C_READ:
  811. txsize = msg->size ? HEADER_SIZE : BARE_ADDRESS_SIZE;
  812. rxsize = msg->size + 1;
  813. if (WARN_ON(rxsize > 20))
  814. return -E2BIG;
  815. ret = intel_dp_aux_ch(intel_dp, txbuf, txsize, rxbuf, rxsize);
  816. if (ret > 0) {
  817. msg->reply = rxbuf[0] >> 4;
  818. /*
  819. * Assume happy day, and copy the data. The caller is
  820. * expected to check msg->reply before touching it.
  821. *
  822. * Return payload size.
  823. */
  824. ret--;
  825. memcpy(msg->buffer, rxbuf + 1, ret);
  826. }
  827. break;
  828. default:
  829. ret = -EINVAL;
  830. break;
  831. }
  832. return ret;
  833. }
  834. static void
  835. intel_dp_aux_init(struct intel_dp *intel_dp, struct intel_connector *connector)
  836. {
  837. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  838. struct drm_i915_private *dev_priv = dev->dev_private;
  839. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  840. enum port port = intel_dig_port->port;
  841. struct ddi_vbt_port_info *info = &dev_priv->vbt.ddi_port_info[port];
  842. const char *name = NULL;
  843. uint32_t porte_aux_ctl_reg = DPA_AUX_CH_CTL;
  844. int ret;
  845. /* On SKL we don't have Aux for port E so we rely on VBT to set
  846. * a proper alternate aux channel.
  847. */
  848. if (IS_SKYLAKE(dev) && port == PORT_E) {
  849. switch (info->alternate_aux_channel) {
  850. case DP_AUX_B:
  851. porte_aux_ctl_reg = DPB_AUX_CH_CTL;
  852. break;
  853. case DP_AUX_C:
  854. porte_aux_ctl_reg = DPC_AUX_CH_CTL;
  855. break;
  856. case DP_AUX_D:
  857. porte_aux_ctl_reg = DPD_AUX_CH_CTL;
  858. break;
  859. case DP_AUX_A:
  860. default:
  861. porte_aux_ctl_reg = DPA_AUX_CH_CTL;
  862. }
  863. }
  864. switch (port) {
  865. case PORT_A:
  866. intel_dp->aux_ch_ctl_reg = DPA_AUX_CH_CTL;
  867. name = "DPDDC-A";
  868. break;
  869. case PORT_B:
  870. intel_dp->aux_ch_ctl_reg = PCH_DPB_AUX_CH_CTL;
  871. name = "DPDDC-B";
  872. break;
  873. case PORT_C:
  874. intel_dp->aux_ch_ctl_reg = PCH_DPC_AUX_CH_CTL;
  875. name = "DPDDC-C";
  876. break;
  877. case PORT_D:
  878. intel_dp->aux_ch_ctl_reg = PCH_DPD_AUX_CH_CTL;
  879. name = "DPDDC-D";
  880. break;
  881. case PORT_E:
  882. intel_dp->aux_ch_ctl_reg = porte_aux_ctl_reg;
  883. name = "DPDDC-E";
  884. break;
  885. default:
  886. BUG();
  887. }
  888. /*
  889. * The AUX_CTL register is usually DP_CTL + 0x10.
  890. *
  891. * On Haswell and Broadwell though:
  892. * - Both port A DDI_BUF_CTL and DDI_AUX_CTL are on the CPU
  893. * - Port B/C/D AUX channels are on the PCH, DDI_BUF_CTL on the CPU
  894. *
  895. * Skylake moves AUX_CTL back next to DDI_BUF_CTL, on the CPU.
  896. */
  897. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev) && port != PORT_E)
  898. intel_dp->aux_ch_ctl_reg = intel_dp->output_reg + 0x10;
  899. intel_dp->aux.name = name;
  900. intel_dp->aux.dev = dev->dev;
  901. intel_dp->aux.transfer = intel_dp_aux_transfer;
  902. DRM_DEBUG_KMS("registering %s bus for %s\n", name,
  903. connector->base.kdev->kobj.name);
  904. ret = drm_dp_aux_register(&intel_dp->aux);
  905. if (ret < 0) {
  906. DRM_ERROR("drm_dp_aux_register() for %s failed (%d)\n",
  907. name, ret);
  908. return;
  909. }
  910. ret = sysfs_create_link(&connector->base.kdev->kobj,
  911. &intel_dp->aux.ddc.dev.kobj,
  912. intel_dp->aux.ddc.dev.kobj.name);
  913. if (ret < 0) {
  914. DRM_ERROR("sysfs_create_link() for %s failed (%d)\n", name, ret);
  915. drm_dp_aux_unregister(&intel_dp->aux);
  916. }
  917. }
  918. static void
  919. intel_dp_connector_unregister(struct intel_connector *intel_connector)
  920. {
  921. struct intel_dp *intel_dp = intel_attached_dp(&intel_connector->base);
  922. if (!intel_connector->mst_port)
  923. sysfs_remove_link(&intel_connector->base.kdev->kobj,
  924. intel_dp->aux.ddc.dev.kobj.name);
  925. intel_connector_unregister(intel_connector);
  926. }
  927. static void
  928. skl_edp_set_pll_config(struct intel_crtc_state *pipe_config)
  929. {
  930. u32 ctrl1;
  931. memset(&pipe_config->dpll_hw_state, 0,
  932. sizeof(pipe_config->dpll_hw_state));
  933. pipe_config->ddi_pll_sel = SKL_DPLL0;
  934. pipe_config->dpll_hw_state.cfgcr1 = 0;
  935. pipe_config->dpll_hw_state.cfgcr2 = 0;
  936. ctrl1 = DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  937. switch (pipe_config->port_clock / 2) {
  938. case 81000:
  939. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  940. SKL_DPLL0);
  941. break;
  942. case 135000:
  943. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350,
  944. SKL_DPLL0);
  945. break;
  946. case 270000:
  947. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700,
  948. SKL_DPLL0);
  949. break;
  950. case 162000:
  951. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620,
  952. SKL_DPLL0);
  953. break;
  954. /* TBD: For DP link rates 2.16 GHz and 4.32 GHz, VCO is 8640 which
  955. results in CDCLK change. Need to handle the change of CDCLK by
  956. disabling pipes and re-enabling them */
  957. case 108000:
  958. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  959. SKL_DPLL0);
  960. break;
  961. case 216000:
  962. ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160,
  963. SKL_DPLL0);
  964. break;
  965. }
  966. pipe_config->dpll_hw_state.ctrl1 = ctrl1;
  967. }
  968. void
  969. hsw_dp_set_ddi_pll_sel(struct intel_crtc_state *pipe_config)
  970. {
  971. memset(&pipe_config->dpll_hw_state, 0,
  972. sizeof(pipe_config->dpll_hw_state));
  973. switch (pipe_config->port_clock / 2) {
  974. case 81000:
  975. pipe_config->ddi_pll_sel = PORT_CLK_SEL_LCPLL_810;
  976. break;
  977. case 135000:
  978. pipe_config->ddi_pll_sel = PORT_CLK_SEL_LCPLL_1350;
  979. break;
  980. case 270000:
  981. pipe_config->ddi_pll_sel = PORT_CLK_SEL_LCPLL_2700;
  982. break;
  983. }
  984. }
  985. static int
  986. intel_dp_sink_rates(struct intel_dp *intel_dp, const int **sink_rates)
  987. {
  988. if (intel_dp->num_sink_rates) {
  989. *sink_rates = intel_dp->sink_rates;
  990. return intel_dp->num_sink_rates;
  991. }
  992. *sink_rates = default_rates;
  993. return (intel_dp_max_link_bw(intel_dp) >> 3) + 1;
  994. }
  995. static bool intel_dp_source_supports_hbr2(struct drm_device *dev)
  996. {
  997. /* WaDisableHBR2:skl */
  998. if (IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_B0)
  999. return false;
  1000. if ((IS_HASWELL(dev) && !IS_HSW_ULX(dev)) || IS_BROADWELL(dev) ||
  1001. (INTEL_INFO(dev)->gen >= 9))
  1002. return true;
  1003. else
  1004. return false;
  1005. }
  1006. static int
  1007. intel_dp_source_rates(struct drm_device *dev, const int **source_rates)
  1008. {
  1009. int size;
  1010. if (IS_BROXTON(dev)) {
  1011. *source_rates = bxt_rates;
  1012. size = ARRAY_SIZE(bxt_rates);
  1013. } else if (IS_SKYLAKE(dev)) {
  1014. *source_rates = skl_rates;
  1015. size = ARRAY_SIZE(skl_rates);
  1016. } else {
  1017. *source_rates = default_rates;
  1018. size = ARRAY_SIZE(default_rates);
  1019. }
  1020. /* This depends on the fact that 5.4 is last value in the array */
  1021. if (!intel_dp_source_supports_hbr2(dev))
  1022. size--;
  1023. return size;
  1024. }
  1025. static void
  1026. intel_dp_set_clock(struct intel_encoder *encoder,
  1027. struct intel_crtc_state *pipe_config)
  1028. {
  1029. struct drm_device *dev = encoder->base.dev;
  1030. const struct dp_link_dpll *divisor = NULL;
  1031. int i, count = 0;
  1032. if (IS_G4X(dev)) {
  1033. divisor = gen4_dpll;
  1034. count = ARRAY_SIZE(gen4_dpll);
  1035. } else if (HAS_PCH_SPLIT(dev)) {
  1036. divisor = pch_dpll;
  1037. count = ARRAY_SIZE(pch_dpll);
  1038. } else if (IS_CHERRYVIEW(dev)) {
  1039. divisor = chv_dpll;
  1040. count = ARRAY_SIZE(chv_dpll);
  1041. } else if (IS_VALLEYVIEW(dev)) {
  1042. divisor = vlv_dpll;
  1043. count = ARRAY_SIZE(vlv_dpll);
  1044. }
  1045. if (divisor && count) {
  1046. for (i = 0; i < count; i++) {
  1047. if (pipe_config->port_clock == divisor[i].clock) {
  1048. pipe_config->dpll = divisor[i].dpll;
  1049. pipe_config->clock_set = true;
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. }
  1055. static int intersect_rates(const int *source_rates, int source_len,
  1056. const int *sink_rates, int sink_len,
  1057. int *common_rates)
  1058. {
  1059. int i = 0, j = 0, k = 0;
  1060. while (i < source_len && j < sink_len) {
  1061. if (source_rates[i] == sink_rates[j]) {
  1062. if (WARN_ON(k >= DP_MAX_SUPPORTED_RATES))
  1063. return k;
  1064. common_rates[k] = source_rates[i];
  1065. ++k;
  1066. ++i;
  1067. ++j;
  1068. } else if (source_rates[i] < sink_rates[j]) {
  1069. ++i;
  1070. } else {
  1071. ++j;
  1072. }
  1073. }
  1074. return k;
  1075. }
  1076. static int intel_dp_common_rates(struct intel_dp *intel_dp,
  1077. int *common_rates)
  1078. {
  1079. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1080. const int *source_rates, *sink_rates;
  1081. int source_len, sink_len;
  1082. sink_len = intel_dp_sink_rates(intel_dp, &sink_rates);
  1083. source_len = intel_dp_source_rates(dev, &source_rates);
  1084. return intersect_rates(source_rates, source_len,
  1085. sink_rates, sink_len,
  1086. common_rates);
  1087. }
  1088. static void snprintf_int_array(char *str, size_t len,
  1089. const int *array, int nelem)
  1090. {
  1091. int i;
  1092. str[0] = '\0';
  1093. for (i = 0; i < nelem; i++) {
  1094. int r = snprintf(str, len, "%s%d", i ? ", " : "", array[i]);
  1095. if (r >= len)
  1096. return;
  1097. str += r;
  1098. len -= r;
  1099. }
  1100. }
  1101. static void intel_dp_print_rates(struct intel_dp *intel_dp)
  1102. {
  1103. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1104. const int *source_rates, *sink_rates;
  1105. int source_len, sink_len, common_len;
  1106. int common_rates[DP_MAX_SUPPORTED_RATES];
  1107. char str[128]; /* FIXME: too big for stack? */
  1108. if ((drm_debug & DRM_UT_KMS) == 0)
  1109. return;
  1110. source_len = intel_dp_source_rates(dev, &source_rates);
  1111. snprintf_int_array(str, sizeof(str), source_rates, source_len);
  1112. DRM_DEBUG_KMS("source rates: %s\n", str);
  1113. sink_len = intel_dp_sink_rates(intel_dp, &sink_rates);
  1114. snprintf_int_array(str, sizeof(str), sink_rates, sink_len);
  1115. DRM_DEBUG_KMS("sink rates: %s\n", str);
  1116. common_len = intel_dp_common_rates(intel_dp, common_rates);
  1117. snprintf_int_array(str, sizeof(str), common_rates, common_len);
  1118. DRM_DEBUG_KMS("common rates: %s\n", str);
  1119. }
  1120. static int rate_to_index(int find, const int *rates)
  1121. {
  1122. int i = 0;
  1123. for (i = 0; i < DP_MAX_SUPPORTED_RATES; ++i)
  1124. if (find == rates[i])
  1125. break;
  1126. return i;
  1127. }
  1128. int
  1129. intel_dp_max_link_rate(struct intel_dp *intel_dp)
  1130. {
  1131. int rates[DP_MAX_SUPPORTED_RATES] = {};
  1132. int len;
  1133. len = intel_dp_common_rates(intel_dp, rates);
  1134. if (WARN_ON(len <= 0))
  1135. return 162000;
  1136. return rates[rate_to_index(0, rates) - 1];
  1137. }
  1138. int intel_dp_rate_select(struct intel_dp *intel_dp, int rate)
  1139. {
  1140. return rate_to_index(rate, intel_dp->sink_rates);
  1141. }
  1142. static void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
  1143. uint8_t *link_bw, uint8_t *rate_select)
  1144. {
  1145. if (intel_dp->num_sink_rates) {
  1146. *link_bw = 0;
  1147. *rate_select =
  1148. intel_dp_rate_select(intel_dp, port_clock);
  1149. } else {
  1150. *link_bw = drm_dp_link_rate_to_bw_code(port_clock);
  1151. *rate_select = 0;
  1152. }
  1153. }
  1154. bool
  1155. intel_dp_compute_config(struct intel_encoder *encoder,
  1156. struct intel_crtc_state *pipe_config)
  1157. {
  1158. struct drm_device *dev = encoder->base.dev;
  1159. struct drm_i915_private *dev_priv = dev->dev_private;
  1160. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  1161. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  1162. enum port port = dp_to_dig_port(intel_dp)->port;
  1163. struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
  1164. struct intel_connector *intel_connector = intel_dp->attached_connector;
  1165. int lane_count, clock;
  1166. int min_lane_count = 1;
  1167. int max_lane_count = intel_dp_max_lane_count(intel_dp);
  1168. /* Conveniently, the link BW constants become indices with a shift...*/
  1169. int min_clock = 0;
  1170. int max_clock;
  1171. int bpp, mode_rate;
  1172. int link_avail, link_clock;
  1173. int common_rates[DP_MAX_SUPPORTED_RATES] = {};
  1174. int common_len;
  1175. uint8_t link_bw, rate_select;
  1176. common_len = intel_dp_common_rates(intel_dp, common_rates);
  1177. /* No common link rates between source and sink */
  1178. WARN_ON(common_len <= 0);
  1179. max_clock = common_len - 1;
  1180. if (HAS_PCH_SPLIT(dev) && !HAS_DDI(dev) && port != PORT_A)
  1181. pipe_config->has_pch_encoder = true;
  1182. pipe_config->has_dp_encoder = true;
  1183. pipe_config->has_drrs = false;
  1184. pipe_config->has_audio = intel_dp->has_audio && port != PORT_A;
  1185. if (is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
  1186. intel_fixed_panel_mode(intel_connector->panel.fixed_mode,
  1187. adjusted_mode);
  1188. if (INTEL_INFO(dev)->gen >= 9) {
  1189. int ret;
  1190. ret = skl_update_scaler_crtc(pipe_config);
  1191. if (ret)
  1192. return ret;
  1193. }
  1194. if (!HAS_PCH_SPLIT(dev))
  1195. intel_gmch_panel_fitting(intel_crtc, pipe_config,
  1196. intel_connector->panel.fitting_mode);
  1197. else
  1198. intel_pch_panel_fitting(intel_crtc, pipe_config,
  1199. intel_connector->panel.fitting_mode);
  1200. }
  1201. if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
  1202. return false;
  1203. DRM_DEBUG_KMS("DP link computation with max lane count %i "
  1204. "max bw %d pixel clock %iKHz\n",
  1205. max_lane_count, common_rates[max_clock],
  1206. adjusted_mode->crtc_clock);
  1207. /* Walk through all bpp values. Luckily they're all nicely spaced with 2
  1208. * bpc in between. */
  1209. bpp = pipe_config->pipe_bpp;
  1210. if (is_edp(intel_dp)) {
  1211. /* Get bpp from vbt only for panels that dont have bpp in edid */
  1212. if (intel_connector->base.display_info.bpc == 0 &&
  1213. (dev_priv->vbt.edp_bpp && dev_priv->vbt.edp_bpp < bpp)) {
  1214. DRM_DEBUG_KMS("clamping bpp for eDP panel to BIOS-provided %i\n",
  1215. dev_priv->vbt.edp_bpp);
  1216. bpp = dev_priv->vbt.edp_bpp;
  1217. }
  1218. /*
  1219. * Use the maximum clock and number of lanes the eDP panel
  1220. * advertizes being capable of. The panels are generally
  1221. * designed to support only a single clock and lane
  1222. * configuration, and typically these values correspond to the
  1223. * native resolution of the panel.
  1224. */
  1225. min_lane_count = max_lane_count;
  1226. min_clock = max_clock;
  1227. }
  1228. for (; bpp >= 6*3; bpp -= 2*3) {
  1229. mode_rate = intel_dp_link_required(adjusted_mode->crtc_clock,
  1230. bpp);
  1231. for (clock = min_clock; clock <= max_clock; clock++) {
  1232. for (lane_count = min_lane_count;
  1233. lane_count <= max_lane_count;
  1234. lane_count <<= 1) {
  1235. link_clock = common_rates[clock];
  1236. link_avail = intel_dp_max_data_rate(link_clock,
  1237. lane_count);
  1238. if (mode_rate <= link_avail) {
  1239. goto found;
  1240. }
  1241. }
  1242. }
  1243. }
  1244. return false;
  1245. found:
  1246. if (intel_dp->color_range_auto) {
  1247. /*
  1248. * See:
  1249. * CEA-861-E - 5.1 Default Encoding Parameters
  1250. * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
  1251. */
  1252. pipe_config->limited_color_range =
  1253. bpp != 18 && drm_match_cea_mode(adjusted_mode) > 1;
  1254. } else {
  1255. pipe_config->limited_color_range =
  1256. intel_dp->limited_color_range;
  1257. }
  1258. pipe_config->lane_count = lane_count;
  1259. pipe_config->pipe_bpp = bpp;
  1260. pipe_config->port_clock = common_rates[clock];
  1261. intel_dp_compute_rate(intel_dp, pipe_config->port_clock,
  1262. &link_bw, &rate_select);
  1263. DRM_DEBUG_KMS("DP link bw %02x rate select %02x lane count %d clock %d bpp %d\n",
  1264. link_bw, rate_select, pipe_config->lane_count,
  1265. pipe_config->port_clock, bpp);
  1266. DRM_DEBUG_KMS("DP link bw required %i available %i\n",
  1267. mode_rate, link_avail);
  1268. intel_link_compute_m_n(bpp, lane_count,
  1269. adjusted_mode->crtc_clock,
  1270. pipe_config->port_clock,
  1271. &pipe_config->dp_m_n);
  1272. if (intel_connector->panel.downclock_mode != NULL &&
  1273. dev_priv->drrs.type == SEAMLESS_DRRS_SUPPORT) {
  1274. pipe_config->has_drrs = true;
  1275. intel_link_compute_m_n(bpp, lane_count,
  1276. intel_connector->panel.downclock_mode->clock,
  1277. pipe_config->port_clock,
  1278. &pipe_config->dp_m2_n2);
  1279. }
  1280. if (IS_SKYLAKE(dev) && is_edp(intel_dp))
  1281. skl_edp_set_pll_config(pipe_config);
  1282. else if (IS_BROXTON(dev))
  1283. /* handled in ddi */;
  1284. else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  1285. hsw_dp_set_ddi_pll_sel(pipe_config);
  1286. else
  1287. intel_dp_set_clock(encoder, pipe_config);
  1288. return true;
  1289. }
  1290. static void ironlake_set_pll_cpu_edp(struct intel_dp *intel_dp)
  1291. {
  1292. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  1293. struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
  1294. struct drm_device *dev = crtc->base.dev;
  1295. struct drm_i915_private *dev_priv = dev->dev_private;
  1296. u32 dpa_ctl;
  1297. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n",
  1298. crtc->config->port_clock);
  1299. dpa_ctl = I915_READ(DP_A);
  1300. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1301. if (crtc->config->port_clock == 162000) {
  1302. /* For a long time we've carried around a ILK-DevA w/a for the
  1303. * 160MHz clock. If we're really unlucky, it's still required.
  1304. */
  1305. DRM_DEBUG_KMS("160MHz cpu eDP clock, might need ilk devA w/a\n");
  1306. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1307. intel_dp->DP |= DP_PLL_FREQ_160MHZ;
  1308. } else {
  1309. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1310. intel_dp->DP |= DP_PLL_FREQ_270MHZ;
  1311. }
  1312. I915_WRITE(DP_A, dpa_ctl);
  1313. POSTING_READ(DP_A);
  1314. udelay(500);
  1315. }
  1316. void intel_dp_set_link_params(struct intel_dp *intel_dp,
  1317. const struct intel_crtc_state *pipe_config)
  1318. {
  1319. intel_dp->link_rate = pipe_config->port_clock;
  1320. intel_dp->lane_count = pipe_config->lane_count;
  1321. }
  1322. static void intel_dp_prepare(struct intel_encoder *encoder)
  1323. {
  1324. struct drm_device *dev = encoder->base.dev;
  1325. struct drm_i915_private *dev_priv = dev->dev_private;
  1326. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  1327. enum port port = dp_to_dig_port(intel_dp)->port;
  1328. struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  1329. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  1330. intel_dp_set_link_params(intel_dp, crtc->config);
  1331. /*
  1332. * There are four kinds of DP registers:
  1333. *
  1334. * IBX PCH
  1335. * SNB CPU
  1336. * IVB CPU
  1337. * CPT PCH
  1338. *
  1339. * IBX PCH and CPU are the same for almost everything,
  1340. * except that the CPU DP PLL is configured in this
  1341. * register
  1342. *
  1343. * CPT PCH is quite different, having many bits moved
  1344. * to the TRANS_DP_CTL register instead. That
  1345. * configuration happens (oddly) in ironlake_pch_enable
  1346. */
  1347. /* Preserve the BIOS-computed detected bit. This is
  1348. * supposed to be read-only.
  1349. */
  1350. intel_dp->DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
  1351. /* Handle DP bits in common between all three register formats */
  1352. intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
  1353. intel_dp->DP |= DP_PORT_WIDTH(crtc->config->lane_count);
  1354. if (crtc->config->has_audio)
  1355. intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
  1356. /* Split out the IBX/CPU vs CPT settings */
  1357. if (IS_GEN7(dev) && port == PORT_A) {
  1358. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  1359. intel_dp->DP |= DP_SYNC_HS_HIGH;
  1360. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  1361. intel_dp->DP |= DP_SYNC_VS_HIGH;
  1362. intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
  1363. if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
  1364. intel_dp->DP |= DP_ENHANCED_FRAMING;
  1365. intel_dp->DP |= crtc->pipe << 29;
  1366. } else if (HAS_PCH_CPT(dev) && port != PORT_A) {
  1367. u32 trans_dp;
  1368. intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
  1369. trans_dp = I915_READ(TRANS_DP_CTL(crtc->pipe));
  1370. if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
  1371. trans_dp |= TRANS_DP_ENH_FRAMING;
  1372. else
  1373. trans_dp &= ~TRANS_DP_ENH_FRAMING;
  1374. I915_WRITE(TRANS_DP_CTL(crtc->pipe), trans_dp);
  1375. } else {
  1376. if (!HAS_PCH_SPLIT(dev) && !IS_VALLEYVIEW(dev) &&
  1377. crtc->config->limited_color_range)
  1378. intel_dp->DP |= DP_COLOR_RANGE_16_235;
  1379. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  1380. intel_dp->DP |= DP_SYNC_HS_HIGH;
  1381. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  1382. intel_dp->DP |= DP_SYNC_VS_HIGH;
  1383. intel_dp->DP |= DP_LINK_TRAIN_OFF;
  1384. if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
  1385. intel_dp->DP |= DP_ENHANCED_FRAMING;
  1386. if (IS_CHERRYVIEW(dev))
  1387. intel_dp->DP |= DP_PIPE_SELECT_CHV(crtc->pipe);
  1388. else if (crtc->pipe == PIPE_B)
  1389. intel_dp->DP |= DP_PIPEB_SELECT;
  1390. }
  1391. }
  1392. #define IDLE_ON_MASK (PP_ON | PP_SEQUENCE_MASK | 0 | PP_SEQUENCE_STATE_MASK)
  1393. #define IDLE_ON_VALUE (PP_ON | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_ON_IDLE)
  1394. #define IDLE_OFF_MASK (PP_ON | PP_SEQUENCE_MASK | 0 | 0)
  1395. #define IDLE_OFF_VALUE (0 | PP_SEQUENCE_NONE | 0 | 0)
  1396. #define IDLE_CYCLE_MASK (PP_ON | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
  1397. #define IDLE_CYCLE_VALUE (0 | PP_SEQUENCE_NONE | 0 | PP_SEQUENCE_STATE_OFF_IDLE)
  1398. static void wait_panel_status(struct intel_dp *intel_dp,
  1399. u32 mask,
  1400. u32 value)
  1401. {
  1402. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1403. struct drm_i915_private *dev_priv = dev->dev_private;
  1404. u32 pp_stat_reg, pp_ctrl_reg;
  1405. lockdep_assert_held(&dev_priv->pps_mutex);
  1406. pp_stat_reg = _pp_stat_reg(intel_dp);
  1407. pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  1408. DRM_DEBUG_KMS("mask %08x value %08x status %08x control %08x\n",
  1409. mask, value,
  1410. I915_READ(pp_stat_reg),
  1411. I915_READ(pp_ctrl_reg));
  1412. if (_wait_for((I915_READ(pp_stat_reg) & mask) == value, 5000, 10)) {
  1413. DRM_ERROR("Panel status timeout: status %08x control %08x\n",
  1414. I915_READ(pp_stat_reg),
  1415. I915_READ(pp_ctrl_reg));
  1416. }
  1417. DRM_DEBUG_KMS("Wait complete\n");
  1418. }
  1419. static void wait_panel_on(struct intel_dp *intel_dp)
  1420. {
  1421. DRM_DEBUG_KMS("Wait for panel power on\n");
  1422. wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
  1423. }
  1424. static void wait_panel_off(struct intel_dp *intel_dp)
  1425. {
  1426. DRM_DEBUG_KMS("Wait for panel power off time\n");
  1427. wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
  1428. }
  1429. static void wait_panel_power_cycle(struct intel_dp *intel_dp)
  1430. {
  1431. DRM_DEBUG_KMS("Wait for panel power cycle\n");
  1432. /* When we disable the VDD override bit last we have to do the manual
  1433. * wait. */
  1434. wait_remaining_ms_from_jiffies(intel_dp->last_power_cycle,
  1435. intel_dp->panel_power_cycle_delay);
  1436. wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
  1437. }
  1438. static void wait_backlight_on(struct intel_dp *intel_dp)
  1439. {
  1440. wait_remaining_ms_from_jiffies(intel_dp->last_power_on,
  1441. intel_dp->backlight_on_delay);
  1442. }
  1443. static void edp_wait_backlight_off(struct intel_dp *intel_dp)
  1444. {
  1445. wait_remaining_ms_from_jiffies(intel_dp->last_backlight_off,
  1446. intel_dp->backlight_off_delay);
  1447. }
  1448. /* Read the current pp_control value, unlocking the register if it
  1449. * is locked
  1450. */
  1451. static u32 ironlake_get_pp_control(struct intel_dp *intel_dp)
  1452. {
  1453. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1454. struct drm_i915_private *dev_priv = dev->dev_private;
  1455. u32 control;
  1456. lockdep_assert_held(&dev_priv->pps_mutex);
  1457. control = I915_READ(_pp_ctrl_reg(intel_dp));
  1458. if (!IS_BROXTON(dev)) {
  1459. control &= ~PANEL_UNLOCK_MASK;
  1460. control |= PANEL_UNLOCK_REGS;
  1461. }
  1462. return control;
  1463. }
  1464. /*
  1465. * Must be paired with edp_panel_vdd_off().
  1466. * Must hold pps_mutex around the whole on/off sequence.
  1467. * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
  1468. */
  1469. static bool edp_panel_vdd_on(struct intel_dp *intel_dp)
  1470. {
  1471. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1472. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  1473. struct intel_encoder *intel_encoder = &intel_dig_port->base;
  1474. struct drm_i915_private *dev_priv = dev->dev_private;
  1475. enum intel_display_power_domain power_domain;
  1476. u32 pp;
  1477. u32 pp_stat_reg, pp_ctrl_reg;
  1478. bool need_to_disable = !intel_dp->want_panel_vdd;
  1479. lockdep_assert_held(&dev_priv->pps_mutex);
  1480. if (!is_edp(intel_dp))
  1481. return false;
  1482. cancel_delayed_work(&intel_dp->panel_vdd_work);
  1483. intel_dp->want_panel_vdd = true;
  1484. if (edp_have_panel_vdd(intel_dp))
  1485. return need_to_disable;
  1486. power_domain = intel_display_port_aux_power_domain(intel_encoder);
  1487. intel_display_power_get(dev_priv, power_domain);
  1488. DRM_DEBUG_KMS("Turning eDP port %c VDD on\n",
  1489. port_name(intel_dig_port->port));
  1490. if (!edp_have_panel_power(intel_dp))
  1491. wait_panel_power_cycle(intel_dp);
  1492. pp = ironlake_get_pp_control(intel_dp);
  1493. pp |= EDP_FORCE_VDD;
  1494. pp_stat_reg = _pp_stat_reg(intel_dp);
  1495. pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  1496. I915_WRITE(pp_ctrl_reg, pp);
  1497. POSTING_READ(pp_ctrl_reg);
  1498. DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
  1499. I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
  1500. /*
  1501. * If the panel wasn't on, delay before accessing aux channel
  1502. */
  1503. if (!edp_have_panel_power(intel_dp)) {
  1504. DRM_DEBUG_KMS("eDP port %c panel power wasn't enabled\n",
  1505. port_name(intel_dig_port->port));
  1506. msleep(intel_dp->panel_power_up_delay);
  1507. }
  1508. return need_to_disable;
  1509. }
  1510. /*
  1511. * Must be paired with intel_edp_panel_vdd_off() or
  1512. * intel_edp_panel_off().
  1513. * Nested calls to these functions are not allowed since
  1514. * we drop the lock. Caller must use some higher level
  1515. * locking to prevent nested calls from other threads.
  1516. */
  1517. void intel_edp_panel_vdd_on(struct intel_dp *intel_dp)
  1518. {
  1519. bool vdd;
  1520. if (!is_edp(intel_dp))
  1521. return;
  1522. pps_lock(intel_dp);
  1523. vdd = edp_panel_vdd_on(intel_dp);
  1524. pps_unlock(intel_dp);
  1525. I915_STATE_WARN(!vdd, "eDP port %c VDD already requested on\n",
  1526. port_name(dp_to_dig_port(intel_dp)->port));
  1527. }
  1528. static void edp_panel_vdd_off_sync(struct intel_dp *intel_dp)
  1529. {
  1530. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1531. struct drm_i915_private *dev_priv = dev->dev_private;
  1532. struct intel_digital_port *intel_dig_port =
  1533. dp_to_dig_port(intel_dp);
  1534. struct intel_encoder *intel_encoder = &intel_dig_port->base;
  1535. enum intel_display_power_domain power_domain;
  1536. u32 pp;
  1537. u32 pp_stat_reg, pp_ctrl_reg;
  1538. lockdep_assert_held(&dev_priv->pps_mutex);
  1539. WARN_ON(intel_dp->want_panel_vdd);
  1540. if (!edp_have_panel_vdd(intel_dp))
  1541. return;
  1542. DRM_DEBUG_KMS("Turning eDP port %c VDD off\n",
  1543. port_name(intel_dig_port->port));
  1544. pp = ironlake_get_pp_control(intel_dp);
  1545. pp &= ~EDP_FORCE_VDD;
  1546. pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  1547. pp_stat_reg = _pp_stat_reg(intel_dp);
  1548. I915_WRITE(pp_ctrl_reg, pp);
  1549. POSTING_READ(pp_ctrl_reg);
  1550. /* Make sure sequencer is idle before allowing subsequent activity */
  1551. DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
  1552. I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
  1553. if ((pp & POWER_TARGET_ON) == 0)
  1554. intel_dp->last_power_cycle = jiffies;
  1555. power_domain = intel_display_port_aux_power_domain(intel_encoder);
  1556. intel_display_power_put(dev_priv, power_domain);
  1557. }
  1558. static void edp_panel_vdd_work(struct work_struct *__work)
  1559. {
  1560. struct intel_dp *intel_dp = container_of(to_delayed_work(__work),
  1561. struct intel_dp, panel_vdd_work);
  1562. pps_lock(intel_dp);
  1563. if (!intel_dp->want_panel_vdd)
  1564. edp_panel_vdd_off_sync(intel_dp);
  1565. pps_unlock(intel_dp);
  1566. }
  1567. static void edp_panel_vdd_schedule_off(struct intel_dp *intel_dp)
  1568. {
  1569. unsigned long delay;
  1570. /*
  1571. * Queue the timer to fire a long time from now (relative to the power
  1572. * down delay) to keep the panel power up across a sequence of
  1573. * operations.
  1574. */
  1575. delay = msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5);
  1576. schedule_delayed_work(&intel_dp->panel_vdd_work, delay);
  1577. }
  1578. /*
  1579. * Must be paired with edp_panel_vdd_on().
  1580. * Must hold pps_mutex around the whole on/off sequence.
  1581. * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
  1582. */
  1583. static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
  1584. {
  1585. struct drm_i915_private *dev_priv =
  1586. intel_dp_to_dev(intel_dp)->dev_private;
  1587. lockdep_assert_held(&dev_priv->pps_mutex);
  1588. if (!is_edp(intel_dp))
  1589. return;
  1590. I915_STATE_WARN(!intel_dp->want_panel_vdd, "eDP port %c VDD not forced on",
  1591. port_name(dp_to_dig_port(intel_dp)->port));
  1592. intel_dp->want_panel_vdd = false;
  1593. if (sync)
  1594. edp_panel_vdd_off_sync(intel_dp);
  1595. else
  1596. edp_panel_vdd_schedule_off(intel_dp);
  1597. }
  1598. static void edp_panel_on(struct intel_dp *intel_dp)
  1599. {
  1600. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1601. struct drm_i915_private *dev_priv = dev->dev_private;
  1602. u32 pp;
  1603. u32 pp_ctrl_reg;
  1604. lockdep_assert_held(&dev_priv->pps_mutex);
  1605. if (!is_edp(intel_dp))
  1606. return;
  1607. DRM_DEBUG_KMS("Turn eDP port %c panel power on\n",
  1608. port_name(dp_to_dig_port(intel_dp)->port));
  1609. if (WARN(edp_have_panel_power(intel_dp),
  1610. "eDP port %c panel power already on\n",
  1611. port_name(dp_to_dig_port(intel_dp)->port)))
  1612. return;
  1613. wait_panel_power_cycle(intel_dp);
  1614. pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  1615. pp = ironlake_get_pp_control(intel_dp);
  1616. if (IS_GEN5(dev)) {
  1617. /* ILK workaround: disable reset around power sequence */
  1618. pp &= ~PANEL_POWER_RESET;
  1619. I915_WRITE(pp_ctrl_reg, pp);
  1620. POSTING_READ(pp_ctrl_reg);
  1621. }
  1622. pp |= POWER_TARGET_ON;
  1623. if (!IS_GEN5(dev))
  1624. pp |= PANEL_POWER_RESET;
  1625. I915_WRITE(pp_ctrl_reg, pp);
  1626. POSTING_READ(pp_ctrl_reg);
  1627. wait_panel_on(intel_dp);
  1628. intel_dp->last_power_on = jiffies;
  1629. if (IS_GEN5(dev)) {
  1630. pp |= PANEL_POWER_RESET; /* restore panel reset bit */
  1631. I915_WRITE(pp_ctrl_reg, pp);
  1632. POSTING_READ(pp_ctrl_reg);
  1633. }
  1634. }
  1635. void intel_edp_panel_on(struct intel_dp *intel_dp)
  1636. {
  1637. if (!is_edp(intel_dp))
  1638. return;
  1639. pps_lock(intel_dp);
  1640. edp_panel_on(intel_dp);
  1641. pps_unlock(intel_dp);
  1642. }
  1643. static void edp_panel_off(struct intel_dp *intel_dp)
  1644. {
  1645. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  1646. struct intel_encoder *intel_encoder = &intel_dig_port->base;
  1647. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1648. struct drm_i915_private *dev_priv = dev->dev_private;
  1649. enum intel_display_power_domain power_domain;
  1650. u32 pp;
  1651. u32 pp_ctrl_reg;
  1652. lockdep_assert_held(&dev_priv->pps_mutex);
  1653. if (!is_edp(intel_dp))
  1654. return;
  1655. DRM_DEBUG_KMS("Turn eDP port %c panel power off\n",
  1656. port_name(dp_to_dig_port(intel_dp)->port));
  1657. WARN(!intel_dp->want_panel_vdd, "Need eDP port %c VDD to turn off panel\n",
  1658. port_name(dp_to_dig_port(intel_dp)->port));
  1659. pp = ironlake_get_pp_control(intel_dp);
  1660. /* We need to switch off panel power _and_ force vdd, for otherwise some
  1661. * panels get very unhappy and cease to work. */
  1662. pp &= ~(POWER_TARGET_ON | PANEL_POWER_RESET | EDP_FORCE_VDD |
  1663. EDP_BLC_ENABLE);
  1664. pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  1665. intel_dp->want_panel_vdd = false;
  1666. I915_WRITE(pp_ctrl_reg, pp);
  1667. POSTING_READ(pp_ctrl_reg);
  1668. intel_dp->last_power_cycle = jiffies;
  1669. wait_panel_off(intel_dp);
  1670. /* We got a reference when we enabled the VDD. */
  1671. power_domain = intel_display_port_aux_power_domain(intel_encoder);
  1672. intel_display_power_put(dev_priv, power_domain);
  1673. }
  1674. void intel_edp_panel_off(struct intel_dp *intel_dp)
  1675. {
  1676. if (!is_edp(intel_dp))
  1677. return;
  1678. pps_lock(intel_dp);
  1679. edp_panel_off(intel_dp);
  1680. pps_unlock(intel_dp);
  1681. }
  1682. /* Enable backlight in the panel power control. */
  1683. static void _intel_edp_backlight_on(struct intel_dp *intel_dp)
  1684. {
  1685. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  1686. struct drm_device *dev = intel_dig_port->base.base.dev;
  1687. struct drm_i915_private *dev_priv = dev->dev_private;
  1688. u32 pp;
  1689. u32 pp_ctrl_reg;
  1690. /*
  1691. * If we enable the backlight right away following a panel power
  1692. * on, we may see slight flicker as the panel syncs with the eDP
  1693. * link. So delay a bit to make sure the image is solid before
  1694. * allowing it to appear.
  1695. */
  1696. wait_backlight_on(intel_dp);
  1697. pps_lock(intel_dp);
  1698. pp = ironlake_get_pp_control(intel_dp);
  1699. pp |= EDP_BLC_ENABLE;
  1700. pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  1701. I915_WRITE(pp_ctrl_reg, pp);
  1702. POSTING_READ(pp_ctrl_reg);
  1703. pps_unlock(intel_dp);
  1704. }
  1705. /* Enable backlight PWM and backlight PP control. */
  1706. void intel_edp_backlight_on(struct intel_dp *intel_dp)
  1707. {
  1708. if (!is_edp(intel_dp))
  1709. return;
  1710. DRM_DEBUG_KMS("\n");
  1711. intel_panel_enable_backlight(intel_dp->attached_connector);
  1712. _intel_edp_backlight_on(intel_dp);
  1713. }
  1714. /* Disable backlight in the panel power control. */
  1715. static void _intel_edp_backlight_off(struct intel_dp *intel_dp)
  1716. {
  1717. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1718. struct drm_i915_private *dev_priv = dev->dev_private;
  1719. u32 pp;
  1720. u32 pp_ctrl_reg;
  1721. if (!is_edp(intel_dp))
  1722. return;
  1723. pps_lock(intel_dp);
  1724. pp = ironlake_get_pp_control(intel_dp);
  1725. pp &= ~EDP_BLC_ENABLE;
  1726. pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  1727. I915_WRITE(pp_ctrl_reg, pp);
  1728. POSTING_READ(pp_ctrl_reg);
  1729. pps_unlock(intel_dp);
  1730. intel_dp->last_backlight_off = jiffies;
  1731. edp_wait_backlight_off(intel_dp);
  1732. }
  1733. /* Disable backlight PP control and backlight PWM. */
  1734. void intel_edp_backlight_off(struct intel_dp *intel_dp)
  1735. {
  1736. if (!is_edp(intel_dp))
  1737. return;
  1738. DRM_DEBUG_KMS("\n");
  1739. _intel_edp_backlight_off(intel_dp);
  1740. intel_panel_disable_backlight(intel_dp->attached_connector);
  1741. }
  1742. /*
  1743. * Hook for controlling the panel power control backlight through the bl_power
  1744. * sysfs attribute. Take care to handle multiple calls.
  1745. */
  1746. static void intel_edp_backlight_power(struct intel_connector *connector,
  1747. bool enable)
  1748. {
  1749. struct intel_dp *intel_dp = intel_attached_dp(&connector->base);
  1750. bool is_enabled;
  1751. pps_lock(intel_dp);
  1752. is_enabled = ironlake_get_pp_control(intel_dp) & EDP_BLC_ENABLE;
  1753. pps_unlock(intel_dp);
  1754. if (is_enabled == enable)
  1755. return;
  1756. DRM_DEBUG_KMS("panel power control backlight %s\n",
  1757. enable ? "enable" : "disable");
  1758. if (enable)
  1759. _intel_edp_backlight_on(intel_dp);
  1760. else
  1761. _intel_edp_backlight_off(intel_dp);
  1762. }
  1763. static void ironlake_edp_pll_on(struct intel_dp *intel_dp)
  1764. {
  1765. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  1766. struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
  1767. struct drm_device *dev = crtc->dev;
  1768. struct drm_i915_private *dev_priv = dev->dev_private;
  1769. u32 dpa_ctl;
  1770. assert_pipe_disabled(dev_priv,
  1771. to_intel_crtc(crtc)->pipe);
  1772. DRM_DEBUG_KMS("\n");
  1773. dpa_ctl = I915_READ(DP_A);
  1774. WARN(dpa_ctl & DP_PLL_ENABLE, "dp pll on, should be off\n");
  1775. WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
  1776. /* We don't adjust intel_dp->DP while tearing down the link, to
  1777. * facilitate link retraining (e.g. after hotplug). Hence clear all
  1778. * enable bits here to ensure that we don't enable too much. */
  1779. intel_dp->DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
  1780. intel_dp->DP |= DP_PLL_ENABLE;
  1781. I915_WRITE(DP_A, intel_dp->DP);
  1782. POSTING_READ(DP_A);
  1783. udelay(200);
  1784. }
  1785. static void ironlake_edp_pll_off(struct intel_dp *intel_dp)
  1786. {
  1787. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  1788. struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
  1789. struct drm_device *dev = crtc->dev;
  1790. struct drm_i915_private *dev_priv = dev->dev_private;
  1791. u32 dpa_ctl;
  1792. assert_pipe_disabled(dev_priv,
  1793. to_intel_crtc(crtc)->pipe);
  1794. dpa_ctl = I915_READ(DP_A);
  1795. WARN((dpa_ctl & DP_PLL_ENABLE) == 0,
  1796. "dp pll off, should be on\n");
  1797. WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
  1798. /* We can't rely on the value tracked for the DP register in
  1799. * intel_dp->DP because link_down must not change that (otherwise link
  1800. * re-training will fail. */
  1801. dpa_ctl &= ~DP_PLL_ENABLE;
  1802. I915_WRITE(DP_A, dpa_ctl);
  1803. POSTING_READ(DP_A);
  1804. udelay(200);
  1805. }
  1806. /* If the sink supports it, try to set the power state appropriately */
  1807. void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
  1808. {
  1809. int ret, i;
  1810. /* Should have a valid DPCD by this point */
  1811. if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
  1812. return;
  1813. if (mode != DRM_MODE_DPMS_ON) {
  1814. ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
  1815. DP_SET_POWER_D3);
  1816. } else {
  1817. /*
  1818. * When turning on, we need to retry for 1ms to give the sink
  1819. * time to wake up.
  1820. */
  1821. for (i = 0; i < 3; i++) {
  1822. ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
  1823. DP_SET_POWER_D0);
  1824. if (ret == 1)
  1825. break;
  1826. msleep(1);
  1827. }
  1828. }
  1829. if (ret != 1)
  1830. DRM_DEBUG_KMS("failed to %s sink power state\n",
  1831. mode == DRM_MODE_DPMS_ON ? "enable" : "disable");
  1832. }
  1833. static bool intel_dp_get_hw_state(struct intel_encoder *encoder,
  1834. enum pipe *pipe)
  1835. {
  1836. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  1837. enum port port = dp_to_dig_port(intel_dp)->port;
  1838. struct drm_device *dev = encoder->base.dev;
  1839. struct drm_i915_private *dev_priv = dev->dev_private;
  1840. enum intel_display_power_domain power_domain;
  1841. u32 tmp;
  1842. power_domain = intel_display_port_power_domain(encoder);
  1843. if (!intel_display_power_is_enabled(dev_priv, power_domain))
  1844. return false;
  1845. tmp = I915_READ(intel_dp->output_reg);
  1846. if (!(tmp & DP_PORT_EN))
  1847. return false;
  1848. if (IS_GEN7(dev) && port == PORT_A) {
  1849. *pipe = PORT_TO_PIPE_CPT(tmp);
  1850. } else if (HAS_PCH_CPT(dev) && port != PORT_A) {
  1851. enum pipe p;
  1852. for_each_pipe(dev_priv, p) {
  1853. u32 trans_dp = I915_READ(TRANS_DP_CTL(p));
  1854. if (TRANS_DP_PIPE_TO_PORT(trans_dp) == port) {
  1855. *pipe = p;
  1856. return true;
  1857. }
  1858. }
  1859. DRM_DEBUG_KMS("No pipe for dp port 0x%x found\n",
  1860. intel_dp->output_reg);
  1861. } else if (IS_CHERRYVIEW(dev)) {
  1862. *pipe = DP_PORT_TO_PIPE_CHV(tmp);
  1863. } else {
  1864. *pipe = PORT_TO_PIPE(tmp);
  1865. }
  1866. return true;
  1867. }
  1868. static void intel_dp_get_config(struct intel_encoder *encoder,
  1869. struct intel_crtc_state *pipe_config)
  1870. {
  1871. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  1872. u32 tmp, flags = 0;
  1873. struct drm_device *dev = encoder->base.dev;
  1874. struct drm_i915_private *dev_priv = dev->dev_private;
  1875. enum port port = dp_to_dig_port(intel_dp)->port;
  1876. struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  1877. int dotclock;
  1878. tmp = I915_READ(intel_dp->output_reg);
  1879. pipe_config->has_audio = tmp & DP_AUDIO_OUTPUT_ENABLE && port != PORT_A;
  1880. if (HAS_PCH_CPT(dev) && port != PORT_A) {
  1881. u32 trans_dp = I915_READ(TRANS_DP_CTL(crtc->pipe));
  1882. if (trans_dp & TRANS_DP_HSYNC_ACTIVE_HIGH)
  1883. flags |= DRM_MODE_FLAG_PHSYNC;
  1884. else
  1885. flags |= DRM_MODE_FLAG_NHSYNC;
  1886. if (trans_dp & TRANS_DP_VSYNC_ACTIVE_HIGH)
  1887. flags |= DRM_MODE_FLAG_PVSYNC;
  1888. else
  1889. flags |= DRM_MODE_FLAG_NVSYNC;
  1890. } else {
  1891. if (tmp & DP_SYNC_HS_HIGH)
  1892. flags |= DRM_MODE_FLAG_PHSYNC;
  1893. else
  1894. flags |= DRM_MODE_FLAG_NHSYNC;
  1895. if (tmp & DP_SYNC_VS_HIGH)
  1896. flags |= DRM_MODE_FLAG_PVSYNC;
  1897. else
  1898. flags |= DRM_MODE_FLAG_NVSYNC;
  1899. }
  1900. pipe_config->base.adjusted_mode.flags |= flags;
  1901. if (!HAS_PCH_SPLIT(dev) && !IS_VALLEYVIEW(dev) &&
  1902. tmp & DP_COLOR_RANGE_16_235)
  1903. pipe_config->limited_color_range = true;
  1904. pipe_config->has_dp_encoder = true;
  1905. pipe_config->lane_count =
  1906. ((tmp & DP_PORT_WIDTH_MASK) >> DP_PORT_WIDTH_SHIFT) + 1;
  1907. intel_dp_get_m_n(crtc, pipe_config);
  1908. if (port == PORT_A) {
  1909. if ((I915_READ(DP_A) & DP_PLL_FREQ_MASK) == DP_PLL_FREQ_160MHZ)
  1910. pipe_config->port_clock = 162000;
  1911. else
  1912. pipe_config->port_clock = 270000;
  1913. }
  1914. dotclock = intel_dotclock_calculate(pipe_config->port_clock,
  1915. &pipe_config->dp_m_n);
  1916. if (HAS_PCH_SPLIT(dev_priv->dev) && port != PORT_A)
  1917. ironlake_check_encoder_dotclock(pipe_config, dotclock);
  1918. pipe_config->base.adjusted_mode.crtc_clock = dotclock;
  1919. if (is_edp(intel_dp) && dev_priv->vbt.edp_bpp &&
  1920. pipe_config->pipe_bpp > dev_priv->vbt.edp_bpp) {
  1921. /*
  1922. * This is a big fat ugly hack.
  1923. *
  1924. * Some machines in UEFI boot mode provide us a VBT that has 18
  1925. * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
  1926. * unknown we fail to light up. Yet the same BIOS boots up with
  1927. * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
  1928. * max, not what it tells us to use.
  1929. *
  1930. * Note: This will still be broken if the eDP panel is not lit
  1931. * up by the BIOS, and thus we can't get the mode at module
  1932. * load.
  1933. */
  1934. DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
  1935. pipe_config->pipe_bpp, dev_priv->vbt.edp_bpp);
  1936. dev_priv->vbt.edp_bpp = pipe_config->pipe_bpp;
  1937. }
  1938. }
  1939. static void intel_disable_dp(struct intel_encoder *encoder)
  1940. {
  1941. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  1942. struct drm_device *dev = encoder->base.dev;
  1943. struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  1944. if (crtc->config->has_audio)
  1945. intel_audio_codec_disable(encoder);
  1946. if (HAS_PSR(dev) && !HAS_DDI(dev))
  1947. intel_psr_disable(intel_dp);
  1948. /* Make sure the panel is off before trying to change the mode. But also
  1949. * ensure that we have vdd while we switch off the panel. */
  1950. intel_edp_panel_vdd_on(intel_dp);
  1951. intel_edp_backlight_off(intel_dp);
  1952. intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
  1953. intel_edp_panel_off(intel_dp);
  1954. /* disable the port before the pipe on g4x */
  1955. if (INTEL_INFO(dev)->gen < 5)
  1956. intel_dp_link_down(intel_dp);
  1957. }
  1958. static void ilk_post_disable_dp(struct intel_encoder *encoder)
  1959. {
  1960. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  1961. enum port port = dp_to_dig_port(intel_dp)->port;
  1962. intel_dp_link_down(intel_dp);
  1963. if (port == PORT_A)
  1964. ironlake_edp_pll_off(intel_dp);
  1965. }
  1966. static void vlv_post_disable_dp(struct intel_encoder *encoder)
  1967. {
  1968. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  1969. intel_dp_link_down(intel_dp);
  1970. }
  1971. static void chv_data_lane_soft_reset(struct intel_encoder *encoder,
  1972. bool reset)
  1973. {
  1974. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  1975. enum dpio_channel ch = vlv_dport_to_channel(enc_to_dig_port(&encoder->base));
  1976. struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  1977. enum pipe pipe = crtc->pipe;
  1978. uint32_t val;
  1979. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW0(ch));
  1980. if (reset)
  1981. val &= ~(DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
  1982. else
  1983. val |= DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET;
  1984. vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW0(ch), val);
  1985. if (crtc->config->lane_count > 2) {
  1986. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW0(ch));
  1987. if (reset)
  1988. val &= ~(DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
  1989. else
  1990. val |= DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET;
  1991. vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW0(ch), val);
  1992. }
  1993. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW1(ch));
  1994. val |= CHV_PCS_REQ_SOFTRESET_EN;
  1995. if (reset)
  1996. val &= ~DPIO_PCS_CLK_SOFT_RESET;
  1997. else
  1998. val |= DPIO_PCS_CLK_SOFT_RESET;
  1999. vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW1(ch), val);
  2000. if (crtc->config->lane_count > 2) {
  2001. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW1(ch));
  2002. val |= CHV_PCS_REQ_SOFTRESET_EN;
  2003. if (reset)
  2004. val &= ~DPIO_PCS_CLK_SOFT_RESET;
  2005. else
  2006. val |= DPIO_PCS_CLK_SOFT_RESET;
  2007. vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW1(ch), val);
  2008. }
  2009. }
  2010. static void chv_post_disable_dp(struct intel_encoder *encoder)
  2011. {
  2012. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2013. struct drm_device *dev = encoder->base.dev;
  2014. struct drm_i915_private *dev_priv = dev->dev_private;
  2015. intel_dp_link_down(intel_dp);
  2016. mutex_lock(&dev_priv->sb_lock);
  2017. /* Assert data lane reset */
  2018. chv_data_lane_soft_reset(encoder, true);
  2019. mutex_unlock(&dev_priv->sb_lock);
  2020. }
  2021. static void
  2022. _intel_dp_set_link_train(struct intel_dp *intel_dp,
  2023. uint32_t *DP,
  2024. uint8_t dp_train_pat)
  2025. {
  2026. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  2027. struct drm_device *dev = intel_dig_port->base.base.dev;
  2028. struct drm_i915_private *dev_priv = dev->dev_private;
  2029. enum port port = intel_dig_port->port;
  2030. if (HAS_DDI(dev)) {
  2031. uint32_t temp = I915_READ(DP_TP_CTL(port));
  2032. if (dp_train_pat & DP_LINK_SCRAMBLING_DISABLE)
  2033. temp |= DP_TP_CTL_SCRAMBLE_DISABLE;
  2034. else
  2035. temp &= ~DP_TP_CTL_SCRAMBLE_DISABLE;
  2036. temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
  2037. switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
  2038. case DP_TRAINING_PATTERN_DISABLE:
  2039. temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
  2040. break;
  2041. case DP_TRAINING_PATTERN_1:
  2042. temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
  2043. break;
  2044. case DP_TRAINING_PATTERN_2:
  2045. temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
  2046. break;
  2047. case DP_TRAINING_PATTERN_3:
  2048. temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
  2049. break;
  2050. }
  2051. I915_WRITE(DP_TP_CTL(port), temp);
  2052. } else if ((IS_GEN7(dev) && port == PORT_A) ||
  2053. (HAS_PCH_CPT(dev) && port != PORT_A)) {
  2054. *DP &= ~DP_LINK_TRAIN_MASK_CPT;
  2055. switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
  2056. case DP_TRAINING_PATTERN_DISABLE:
  2057. *DP |= DP_LINK_TRAIN_OFF_CPT;
  2058. break;
  2059. case DP_TRAINING_PATTERN_1:
  2060. *DP |= DP_LINK_TRAIN_PAT_1_CPT;
  2061. break;
  2062. case DP_TRAINING_PATTERN_2:
  2063. *DP |= DP_LINK_TRAIN_PAT_2_CPT;
  2064. break;
  2065. case DP_TRAINING_PATTERN_3:
  2066. DRM_ERROR("DP training pattern 3 not supported\n");
  2067. *DP |= DP_LINK_TRAIN_PAT_2_CPT;
  2068. break;
  2069. }
  2070. } else {
  2071. if (IS_CHERRYVIEW(dev))
  2072. *DP &= ~DP_LINK_TRAIN_MASK_CHV;
  2073. else
  2074. *DP &= ~DP_LINK_TRAIN_MASK;
  2075. switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
  2076. case DP_TRAINING_PATTERN_DISABLE:
  2077. *DP |= DP_LINK_TRAIN_OFF;
  2078. break;
  2079. case DP_TRAINING_PATTERN_1:
  2080. *DP |= DP_LINK_TRAIN_PAT_1;
  2081. break;
  2082. case DP_TRAINING_PATTERN_2:
  2083. *DP |= DP_LINK_TRAIN_PAT_2;
  2084. break;
  2085. case DP_TRAINING_PATTERN_3:
  2086. if (IS_CHERRYVIEW(dev)) {
  2087. *DP |= DP_LINK_TRAIN_PAT_3_CHV;
  2088. } else {
  2089. DRM_ERROR("DP training pattern 3 not supported\n");
  2090. *DP |= DP_LINK_TRAIN_PAT_2;
  2091. }
  2092. break;
  2093. }
  2094. }
  2095. }
  2096. static void intel_dp_enable_port(struct intel_dp *intel_dp)
  2097. {
  2098. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  2099. struct drm_i915_private *dev_priv = dev->dev_private;
  2100. /* enable with pattern 1 (as per spec) */
  2101. _intel_dp_set_link_train(intel_dp, &intel_dp->DP,
  2102. DP_TRAINING_PATTERN_1);
  2103. I915_WRITE(intel_dp->output_reg, intel_dp->DP);
  2104. POSTING_READ(intel_dp->output_reg);
  2105. /*
  2106. * Magic for VLV/CHV. We _must_ first set up the register
  2107. * without actually enabling the port, and then do another
  2108. * write to enable the port. Otherwise link training will
  2109. * fail when the power sequencer is freshly used for this port.
  2110. */
  2111. intel_dp->DP |= DP_PORT_EN;
  2112. I915_WRITE(intel_dp->output_reg, intel_dp->DP);
  2113. POSTING_READ(intel_dp->output_reg);
  2114. }
  2115. static void intel_enable_dp(struct intel_encoder *encoder)
  2116. {
  2117. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2118. struct drm_device *dev = encoder->base.dev;
  2119. struct drm_i915_private *dev_priv = dev->dev_private;
  2120. struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  2121. uint32_t dp_reg = I915_READ(intel_dp->output_reg);
  2122. if (WARN_ON(dp_reg & DP_PORT_EN))
  2123. return;
  2124. pps_lock(intel_dp);
  2125. if (IS_VALLEYVIEW(dev))
  2126. vlv_init_panel_power_sequencer(intel_dp);
  2127. intel_dp_enable_port(intel_dp);
  2128. edp_panel_vdd_on(intel_dp);
  2129. edp_panel_on(intel_dp);
  2130. edp_panel_vdd_off(intel_dp, true);
  2131. pps_unlock(intel_dp);
  2132. if (IS_VALLEYVIEW(dev)) {
  2133. unsigned int lane_mask = 0x0;
  2134. if (IS_CHERRYVIEW(dev))
  2135. lane_mask = intel_dp_unused_lane_mask(crtc->config->lane_count);
  2136. vlv_wait_port_ready(dev_priv, dp_to_dig_port(intel_dp),
  2137. lane_mask);
  2138. }
  2139. intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
  2140. intel_dp_start_link_train(intel_dp);
  2141. intel_dp_stop_link_train(intel_dp);
  2142. if (crtc->config->has_audio) {
  2143. DRM_DEBUG_DRIVER("Enabling DP audio on pipe %c\n",
  2144. pipe_name(crtc->pipe));
  2145. intel_audio_codec_enable(encoder);
  2146. }
  2147. }
  2148. static void g4x_enable_dp(struct intel_encoder *encoder)
  2149. {
  2150. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2151. intel_enable_dp(encoder);
  2152. intel_edp_backlight_on(intel_dp);
  2153. }
  2154. static void vlv_enable_dp(struct intel_encoder *encoder)
  2155. {
  2156. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2157. intel_edp_backlight_on(intel_dp);
  2158. intel_psr_enable(intel_dp);
  2159. }
  2160. static void g4x_pre_enable_dp(struct intel_encoder *encoder)
  2161. {
  2162. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2163. struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
  2164. intel_dp_prepare(encoder);
  2165. /* Only ilk+ has port A */
  2166. if (dport->port == PORT_A) {
  2167. ironlake_set_pll_cpu_edp(intel_dp);
  2168. ironlake_edp_pll_on(intel_dp);
  2169. }
  2170. }
  2171. static void vlv_detach_power_sequencer(struct intel_dp *intel_dp)
  2172. {
  2173. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  2174. struct drm_i915_private *dev_priv = intel_dig_port->base.base.dev->dev_private;
  2175. enum pipe pipe = intel_dp->pps_pipe;
  2176. int pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
  2177. edp_panel_vdd_off_sync(intel_dp);
  2178. /*
  2179. * VLV seems to get confused when multiple power seqeuencers
  2180. * have the same port selected (even if only one has power/vdd
  2181. * enabled). The failure manifests as vlv_wait_port_ready() failing
  2182. * CHV on the other hand doesn't seem to mind having the same port
  2183. * selected in multiple power seqeuencers, but let's clear the
  2184. * port select always when logically disconnecting a power sequencer
  2185. * from a port.
  2186. */
  2187. DRM_DEBUG_KMS("detaching pipe %c power sequencer from port %c\n",
  2188. pipe_name(pipe), port_name(intel_dig_port->port));
  2189. I915_WRITE(pp_on_reg, 0);
  2190. POSTING_READ(pp_on_reg);
  2191. intel_dp->pps_pipe = INVALID_PIPE;
  2192. }
  2193. static void vlv_steal_power_sequencer(struct drm_device *dev,
  2194. enum pipe pipe)
  2195. {
  2196. struct drm_i915_private *dev_priv = dev->dev_private;
  2197. struct intel_encoder *encoder;
  2198. lockdep_assert_held(&dev_priv->pps_mutex);
  2199. if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  2200. return;
  2201. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  2202. base.head) {
  2203. struct intel_dp *intel_dp;
  2204. enum port port;
  2205. if (encoder->type != INTEL_OUTPUT_EDP)
  2206. continue;
  2207. intel_dp = enc_to_intel_dp(&encoder->base);
  2208. port = dp_to_dig_port(intel_dp)->port;
  2209. if (intel_dp->pps_pipe != pipe)
  2210. continue;
  2211. DRM_DEBUG_KMS("stealing pipe %c power sequencer from port %c\n",
  2212. pipe_name(pipe), port_name(port));
  2213. WARN(encoder->base.crtc,
  2214. "stealing pipe %c power sequencer from active eDP port %c\n",
  2215. pipe_name(pipe), port_name(port));
  2216. /* make sure vdd is off before we steal it */
  2217. vlv_detach_power_sequencer(intel_dp);
  2218. }
  2219. }
  2220. static void vlv_init_panel_power_sequencer(struct intel_dp *intel_dp)
  2221. {
  2222. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  2223. struct intel_encoder *encoder = &intel_dig_port->base;
  2224. struct drm_device *dev = encoder->base.dev;
  2225. struct drm_i915_private *dev_priv = dev->dev_private;
  2226. struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  2227. lockdep_assert_held(&dev_priv->pps_mutex);
  2228. if (!is_edp(intel_dp))
  2229. return;
  2230. if (intel_dp->pps_pipe == crtc->pipe)
  2231. return;
  2232. /*
  2233. * If another power sequencer was being used on this
  2234. * port previously make sure to turn off vdd there while
  2235. * we still have control of it.
  2236. */
  2237. if (intel_dp->pps_pipe != INVALID_PIPE)
  2238. vlv_detach_power_sequencer(intel_dp);
  2239. /*
  2240. * We may be stealing the power
  2241. * sequencer from another port.
  2242. */
  2243. vlv_steal_power_sequencer(dev, crtc->pipe);
  2244. /* now it's all ours */
  2245. intel_dp->pps_pipe = crtc->pipe;
  2246. DRM_DEBUG_KMS("initializing pipe %c power sequencer for port %c\n",
  2247. pipe_name(intel_dp->pps_pipe), port_name(intel_dig_port->port));
  2248. /* init power sequencer on this pipe and port */
  2249. intel_dp_init_panel_power_sequencer(dev, intel_dp);
  2250. intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
  2251. }
  2252. static void vlv_pre_enable_dp(struct intel_encoder *encoder)
  2253. {
  2254. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2255. struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
  2256. struct drm_device *dev = encoder->base.dev;
  2257. struct drm_i915_private *dev_priv = dev->dev_private;
  2258. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  2259. enum dpio_channel port = vlv_dport_to_channel(dport);
  2260. int pipe = intel_crtc->pipe;
  2261. u32 val;
  2262. mutex_lock(&dev_priv->sb_lock);
  2263. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW8(port));
  2264. val = 0;
  2265. if (pipe)
  2266. val |= (1<<21);
  2267. else
  2268. val &= ~(1<<21);
  2269. val |= 0x001000c4;
  2270. vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW8(port), val);
  2271. vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW14(port), 0x00760018);
  2272. vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW23(port), 0x00400888);
  2273. mutex_unlock(&dev_priv->sb_lock);
  2274. intel_enable_dp(encoder);
  2275. }
  2276. static void vlv_dp_pre_pll_enable(struct intel_encoder *encoder)
  2277. {
  2278. struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
  2279. struct drm_device *dev = encoder->base.dev;
  2280. struct drm_i915_private *dev_priv = dev->dev_private;
  2281. struct intel_crtc *intel_crtc =
  2282. to_intel_crtc(encoder->base.crtc);
  2283. enum dpio_channel port = vlv_dport_to_channel(dport);
  2284. int pipe = intel_crtc->pipe;
  2285. intel_dp_prepare(encoder);
  2286. /* Program Tx lane resets to default */
  2287. mutex_lock(&dev_priv->sb_lock);
  2288. vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW0(port),
  2289. DPIO_PCS_TX_LANE2_RESET |
  2290. DPIO_PCS_TX_LANE1_RESET);
  2291. vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW1(port),
  2292. DPIO_PCS_CLK_CRI_RXEB_EIOS_EN |
  2293. DPIO_PCS_CLK_CRI_RXDIGFILTSG_EN |
  2294. (1<<DPIO_PCS_CLK_DATAWIDTH_SHIFT) |
  2295. DPIO_PCS_CLK_SOFT_RESET);
  2296. /* Fix up inter-pair skew failure */
  2297. vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW12(port), 0x00750f00);
  2298. vlv_dpio_write(dev_priv, pipe, VLV_TX_DW11(port), 0x00001500);
  2299. vlv_dpio_write(dev_priv, pipe, VLV_TX_DW14(port), 0x40400000);
  2300. mutex_unlock(&dev_priv->sb_lock);
  2301. }
  2302. static void chv_pre_enable_dp(struct intel_encoder *encoder)
  2303. {
  2304. struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2305. struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
  2306. struct drm_device *dev = encoder->base.dev;
  2307. struct drm_i915_private *dev_priv = dev->dev_private;
  2308. struct intel_crtc *intel_crtc =
  2309. to_intel_crtc(encoder->base.crtc);
  2310. enum dpio_channel ch = vlv_dport_to_channel(dport);
  2311. int pipe = intel_crtc->pipe;
  2312. int data, i, stagger;
  2313. u32 val;
  2314. mutex_lock(&dev_priv->sb_lock);
  2315. /* allow hardware to manage TX FIFO reset source */
  2316. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW11(ch));
  2317. val &= ~DPIO_LANEDESKEW_STRAP_OVRD;
  2318. vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW11(ch), val);
  2319. if (intel_crtc->config->lane_count > 2) {
  2320. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW11(ch));
  2321. val &= ~DPIO_LANEDESKEW_STRAP_OVRD;
  2322. vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW11(ch), val);
  2323. }
  2324. /* Program Tx lane latency optimal setting*/
  2325. for (i = 0; i < intel_crtc->config->lane_count; i++) {
  2326. /* Set the upar bit */
  2327. if (intel_crtc->config->lane_count == 1)
  2328. data = 0x0;
  2329. else
  2330. data = (i == 1) ? 0x0 : 0x1;
  2331. vlv_dpio_write(dev_priv, pipe, CHV_TX_DW14(ch, i),
  2332. data << DPIO_UPAR_SHIFT);
  2333. }
  2334. /* Data lane stagger programming */
  2335. if (intel_crtc->config->port_clock > 270000)
  2336. stagger = 0x18;
  2337. else if (intel_crtc->config->port_clock > 135000)
  2338. stagger = 0xd;
  2339. else if (intel_crtc->config->port_clock > 67500)
  2340. stagger = 0x7;
  2341. else if (intel_crtc->config->port_clock > 33750)
  2342. stagger = 0x4;
  2343. else
  2344. stagger = 0x2;
  2345. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW11(ch));
  2346. val |= DPIO_TX2_STAGGER_MASK(0x1f);
  2347. vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW11(ch), val);
  2348. if (intel_crtc->config->lane_count > 2) {
  2349. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW11(ch));
  2350. val |= DPIO_TX2_STAGGER_MASK(0x1f);
  2351. vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW11(ch), val);
  2352. }
  2353. vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW12(ch),
  2354. DPIO_LANESTAGGER_STRAP(stagger) |
  2355. DPIO_LANESTAGGER_STRAP_OVRD |
  2356. DPIO_TX1_STAGGER_MASK(0x1f) |
  2357. DPIO_TX1_STAGGER_MULT(6) |
  2358. DPIO_TX2_STAGGER_MULT(0));
  2359. if (intel_crtc->config->lane_count > 2) {
  2360. vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW12(ch),
  2361. DPIO_LANESTAGGER_STRAP(stagger) |
  2362. DPIO_LANESTAGGER_STRAP_OVRD |
  2363. DPIO_TX1_STAGGER_MASK(0x1f) |
  2364. DPIO_TX1_STAGGER_MULT(7) |
  2365. DPIO_TX2_STAGGER_MULT(5));
  2366. }
  2367. /* Deassert data lane reset */
  2368. chv_data_lane_soft_reset(encoder, false);
  2369. mutex_unlock(&dev_priv->sb_lock);
  2370. intel_enable_dp(encoder);
  2371. /* Second common lane will stay alive on its own now */
  2372. if (dport->release_cl2_override) {
  2373. chv_phy_powergate_ch(dev_priv, DPIO_PHY0, DPIO_CH1, false);
  2374. dport->release_cl2_override = false;
  2375. }
  2376. }
  2377. static void chv_dp_pre_pll_enable(struct intel_encoder *encoder)
  2378. {
  2379. struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
  2380. struct drm_device *dev = encoder->base.dev;
  2381. struct drm_i915_private *dev_priv = dev->dev_private;
  2382. struct intel_crtc *intel_crtc =
  2383. to_intel_crtc(encoder->base.crtc);
  2384. enum dpio_channel ch = vlv_dport_to_channel(dport);
  2385. enum pipe pipe = intel_crtc->pipe;
  2386. unsigned int lane_mask =
  2387. intel_dp_unused_lane_mask(intel_crtc->config->lane_count);
  2388. u32 val;
  2389. intel_dp_prepare(encoder);
  2390. /*
  2391. * Must trick the second common lane into life.
  2392. * Otherwise we can't even access the PLL.
  2393. */
  2394. if (ch == DPIO_CH0 && pipe == PIPE_B)
  2395. dport->release_cl2_override =
  2396. !chv_phy_powergate_ch(dev_priv, DPIO_PHY0, DPIO_CH1, true);
  2397. chv_phy_powergate_lanes(encoder, true, lane_mask);
  2398. mutex_lock(&dev_priv->sb_lock);
  2399. /* Assert data lane reset */
  2400. chv_data_lane_soft_reset(encoder, true);
  2401. /* program left/right clock distribution */
  2402. if (pipe != PIPE_B) {
  2403. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  2404. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  2405. if (ch == DPIO_CH0)
  2406. val |= CHV_BUFLEFTENA1_FORCE;
  2407. if (ch == DPIO_CH1)
  2408. val |= CHV_BUFRIGHTENA1_FORCE;
  2409. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  2410. } else {
  2411. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  2412. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  2413. if (ch == DPIO_CH0)
  2414. val |= CHV_BUFLEFTENA2_FORCE;
  2415. if (ch == DPIO_CH1)
  2416. val |= CHV_BUFRIGHTENA2_FORCE;
  2417. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  2418. }
  2419. /* program clock channel usage */
  2420. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW8(ch));
  2421. val |= CHV_PCS_USEDCLKCHANNEL_OVRRIDE;
  2422. if (pipe != PIPE_B)
  2423. val &= ~CHV_PCS_USEDCLKCHANNEL;
  2424. else
  2425. val |= CHV_PCS_USEDCLKCHANNEL;
  2426. vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW8(ch), val);
  2427. if (intel_crtc->config->lane_count > 2) {
  2428. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW8(ch));
  2429. val |= CHV_PCS_USEDCLKCHANNEL_OVRRIDE;
  2430. if (pipe != PIPE_B)
  2431. val &= ~CHV_PCS_USEDCLKCHANNEL;
  2432. else
  2433. val |= CHV_PCS_USEDCLKCHANNEL;
  2434. vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW8(ch), val);
  2435. }
  2436. /*
  2437. * This a a bit weird since generally CL
  2438. * matches the pipe, but here we need to
  2439. * pick the CL based on the port.
  2440. */
  2441. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW19(ch));
  2442. if (pipe != PIPE_B)
  2443. val &= ~CHV_CMN_USEDCLKCHANNEL;
  2444. else
  2445. val |= CHV_CMN_USEDCLKCHANNEL;
  2446. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW19(ch), val);
  2447. mutex_unlock(&dev_priv->sb_lock);
  2448. }
  2449. static void chv_dp_post_pll_disable(struct intel_encoder *encoder)
  2450. {
  2451. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  2452. enum pipe pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  2453. u32 val;
  2454. mutex_lock(&dev_priv->sb_lock);
  2455. /* disable left/right clock distribution */
  2456. if (pipe != PIPE_B) {
  2457. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  2458. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  2459. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  2460. } else {
  2461. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  2462. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  2463. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  2464. }
  2465. mutex_unlock(&dev_priv->sb_lock);
  2466. /*
  2467. * Leave the power down bit cleared for at least one
  2468. * lane so that chv_powergate_phy_ch() will power
  2469. * on something when the channel is otherwise unused.
  2470. * When the port is off and the override is removed
  2471. * the lanes power down anyway, so otherwise it doesn't
  2472. * really matter what the state of power down bits is
  2473. * after this.
  2474. */
  2475. chv_phy_powergate_lanes(encoder, false, 0x0);
  2476. }
  2477. /*
  2478. * Native read with retry for link status and receiver capability reads for
  2479. * cases where the sink may still be asleep.
  2480. *
  2481. * Sinks are *supposed* to come up within 1ms from an off state, but we're also
  2482. * supposed to retry 3 times per the spec.
  2483. */
  2484. static ssize_t
  2485. intel_dp_dpcd_read_wake(struct drm_dp_aux *aux, unsigned int offset,
  2486. void *buffer, size_t size)
  2487. {
  2488. ssize_t ret;
  2489. int i;
  2490. /*
  2491. * Sometime we just get the same incorrect byte repeated
  2492. * over the entire buffer. Doing just one throw away read
  2493. * initially seems to "solve" it.
  2494. */
  2495. drm_dp_dpcd_read(aux, DP_DPCD_REV, buffer, 1);
  2496. for (i = 0; i < 3; i++) {
  2497. ret = drm_dp_dpcd_read(aux, offset, buffer, size);
  2498. if (ret == size)
  2499. return ret;
  2500. msleep(1);
  2501. }
  2502. return ret;
  2503. }
  2504. /*
  2505. * Fetch AUX CH registers 0x202 - 0x207 which contain
  2506. * link status information
  2507. */
  2508. static bool
  2509. intel_dp_get_link_status(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
  2510. {
  2511. return intel_dp_dpcd_read_wake(&intel_dp->aux,
  2512. DP_LANE0_1_STATUS,
  2513. link_status,
  2514. DP_LINK_STATUS_SIZE) == DP_LINK_STATUS_SIZE;
  2515. }
  2516. /* These are source-specific values. */
  2517. static uint8_t
  2518. intel_dp_voltage_max(struct intel_dp *intel_dp)
  2519. {
  2520. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  2521. struct drm_i915_private *dev_priv = dev->dev_private;
  2522. enum port port = dp_to_dig_port(intel_dp)->port;
  2523. if (IS_BROXTON(dev))
  2524. return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
  2525. else if (INTEL_INFO(dev)->gen >= 9) {
  2526. if (dev_priv->edp_low_vswing && port == PORT_A)
  2527. return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
  2528. return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
  2529. } else if (IS_VALLEYVIEW(dev))
  2530. return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
  2531. else if (IS_GEN7(dev) && port == PORT_A)
  2532. return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
  2533. else if (HAS_PCH_CPT(dev) && port != PORT_A)
  2534. return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
  2535. else
  2536. return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
  2537. }
  2538. static uint8_t
  2539. intel_dp_pre_emphasis_max(struct intel_dp *intel_dp, uint8_t voltage_swing)
  2540. {
  2541. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  2542. enum port port = dp_to_dig_port(intel_dp)->port;
  2543. if (INTEL_INFO(dev)->gen >= 9) {
  2544. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2545. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2546. return DP_TRAIN_PRE_EMPH_LEVEL_3;
  2547. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2548. return DP_TRAIN_PRE_EMPH_LEVEL_2;
  2549. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  2550. return DP_TRAIN_PRE_EMPH_LEVEL_1;
  2551. case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  2552. return DP_TRAIN_PRE_EMPH_LEVEL_0;
  2553. default:
  2554. return DP_TRAIN_PRE_EMPH_LEVEL_0;
  2555. }
  2556. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2557. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2558. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2559. return DP_TRAIN_PRE_EMPH_LEVEL_3;
  2560. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2561. return DP_TRAIN_PRE_EMPH_LEVEL_2;
  2562. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  2563. return DP_TRAIN_PRE_EMPH_LEVEL_1;
  2564. case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  2565. default:
  2566. return DP_TRAIN_PRE_EMPH_LEVEL_0;
  2567. }
  2568. } else if (IS_VALLEYVIEW(dev)) {
  2569. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2570. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2571. return DP_TRAIN_PRE_EMPH_LEVEL_3;
  2572. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2573. return DP_TRAIN_PRE_EMPH_LEVEL_2;
  2574. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  2575. return DP_TRAIN_PRE_EMPH_LEVEL_1;
  2576. case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  2577. default:
  2578. return DP_TRAIN_PRE_EMPH_LEVEL_0;
  2579. }
  2580. } else if (IS_GEN7(dev) && port == PORT_A) {
  2581. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2582. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2583. return DP_TRAIN_PRE_EMPH_LEVEL_2;
  2584. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2585. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  2586. return DP_TRAIN_PRE_EMPH_LEVEL_1;
  2587. default:
  2588. return DP_TRAIN_PRE_EMPH_LEVEL_0;
  2589. }
  2590. } else {
  2591. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2592. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2593. return DP_TRAIN_PRE_EMPH_LEVEL_2;
  2594. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2595. return DP_TRAIN_PRE_EMPH_LEVEL_2;
  2596. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  2597. return DP_TRAIN_PRE_EMPH_LEVEL_1;
  2598. case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  2599. default:
  2600. return DP_TRAIN_PRE_EMPH_LEVEL_0;
  2601. }
  2602. }
  2603. }
  2604. static uint32_t vlv_signal_levels(struct intel_dp *intel_dp)
  2605. {
  2606. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  2607. struct drm_i915_private *dev_priv = dev->dev_private;
  2608. struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
  2609. struct intel_crtc *intel_crtc =
  2610. to_intel_crtc(dport->base.base.crtc);
  2611. unsigned long demph_reg_value, preemph_reg_value,
  2612. uniqtranscale_reg_value;
  2613. uint8_t train_set = intel_dp->train_set[0];
  2614. enum dpio_channel port = vlv_dport_to_channel(dport);
  2615. int pipe = intel_crtc->pipe;
  2616. switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  2617. case DP_TRAIN_PRE_EMPH_LEVEL_0:
  2618. preemph_reg_value = 0x0004000;
  2619. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2620. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2621. demph_reg_value = 0x2B405555;
  2622. uniqtranscale_reg_value = 0x552AB83A;
  2623. break;
  2624. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2625. demph_reg_value = 0x2B404040;
  2626. uniqtranscale_reg_value = 0x5548B83A;
  2627. break;
  2628. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  2629. demph_reg_value = 0x2B245555;
  2630. uniqtranscale_reg_value = 0x5560B83A;
  2631. break;
  2632. case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  2633. demph_reg_value = 0x2B405555;
  2634. uniqtranscale_reg_value = 0x5598DA3A;
  2635. break;
  2636. default:
  2637. return 0;
  2638. }
  2639. break;
  2640. case DP_TRAIN_PRE_EMPH_LEVEL_1:
  2641. preemph_reg_value = 0x0002000;
  2642. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2643. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2644. demph_reg_value = 0x2B404040;
  2645. uniqtranscale_reg_value = 0x5552B83A;
  2646. break;
  2647. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2648. demph_reg_value = 0x2B404848;
  2649. uniqtranscale_reg_value = 0x5580B83A;
  2650. break;
  2651. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  2652. demph_reg_value = 0x2B404040;
  2653. uniqtranscale_reg_value = 0x55ADDA3A;
  2654. break;
  2655. default:
  2656. return 0;
  2657. }
  2658. break;
  2659. case DP_TRAIN_PRE_EMPH_LEVEL_2:
  2660. preemph_reg_value = 0x0000000;
  2661. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2662. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2663. demph_reg_value = 0x2B305555;
  2664. uniqtranscale_reg_value = 0x5570B83A;
  2665. break;
  2666. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2667. demph_reg_value = 0x2B2B4040;
  2668. uniqtranscale_reg_value = 0x55ADDA3A;
  2669. break;
  2670. default:
  2671. return 0;
  2672. }
  2673. break;
  2674. case DP_TRAIN_PRE_EMPH_LEVEL_3:
  2675. preemph_reg_value = 0x0006000;
  2676. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2677. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2678. demph_reg_value = 0x1B405555;
  2679. uniqtranscale_reg_value = 0x55ADDA3A;
  2680. break;
  2681. default:
  2682. return 0;
  2683. }
  2684. break;
  2685. default:
  2686. return 0;
  2687. }
  2688. mutex_lock(&dev_priv->sb_lock);
  2689. vlv_dpio_write(dev_priv, pipe, VLV_TX_DW5(port), 0x00000000);
  2690. vlv_dpio_write(dev_priv, pipe, VLV_TX_DW4(port), demph_reg_value);
  2691. vlv_dpio_write(dev_priv, pipe, VLV_TX_DW2(port),
  2692. uniqtranscale_reg_value);
  2693. vlv_dpio_write(dev_priv, pipe, VLV_TX_DW3(port), 0x0C782040);
  2694. vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW11(port), 0x00030000);
  2695. vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW9(port), preemph_reg_value);
  2696. vlv_dpio_write(dev_priv, pipe, VLV_TX_DW5(port), 0x80000000);
  2697. mutex_unlock(&dev_priv->sb_lock);
  2698. return 0;
  2699. }
  2700. static bool chv_need_uniq_trans_scale(uint8_t train_set)
  2701. {
  2702. return (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) == DP_TRAIN_PRE_EMPH_LEVEL_0 &&
  2703. (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) == DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
  2704. }
  2705. static uint32_t chv_signal_levels(struct intel_dp *intel_dp)
  2706. {
  2707. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  2708. struct drm_i915_private *dev_priv = dev->dev_private;
  2709. struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
  2710. struct intel_crtc *intel_crtc = to_intel_crtc(dport->base.base.crtc);
  2711. u32 deemph_reg_value, margin_reg_value, val;
  2712. uint8_t train_set = intel_dp->train_set[0];
  2713. enum dpio_channel ch = vlv_dport_to_channel(dport);
  2714. enum pipe pipe = intel_crtc->pipe;
  2715. int i;
  2716. switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  2717. case DP_TRAIN_PRE_EMPH_LEVEL_0:
  2718. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2719. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2720. deemph_reg_value = 128;
  2721. margin_reg_value = 52;
  2722. break;
  2723. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2724. deemph_reg_value = 128;
  2725. margin_reg_value = 77;
  2726. break;
  2727. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  2728. deemph_reg_value = 128;
  2729. margin_reg_value = 102;
  2730. break;
  2731. case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  2732. deemph_reg_value = 128;
  2733. margin_reg_value = 154;
  2734. /* FIXME extra to set for 1200 */
  2735. break;
  2736. default:
  2737. return 0;
  2738. }
  2739. break;
  2740. case DP_TRAIN_PRE_EMPH_LEVEL_1:
  2741. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2742. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2743. deemph_reg_value = 85;
  2744. margin_reg_value = 78;
  2745. break;
  2746. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2747. deemph_reg_value = 85;
  2748. margin_reg_value = 116;
  2749. break;
  2750. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  2751. deemph_reg_value = 85;
  2752. margin_reg_value = 154;
  2753. break;
  2754. default:
  2755. return 0;
  2756. }
  2757. break;
  2758. case DP_TRAIN_PRE_EMPH_LEVEL_2:
  2759. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2760. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2761. deemph_reg_value = 64;
  2762. margin_reg_value = 104;
  2763. break;
  2764. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2765. deemph_reg_value = 64;
  2766. margin_reg_value = 154;
  2767. break;
  2768. default:
  2769. return 0;
  2770. }
  2771. break;
  2772. case DP_TRAIN_PRE_EMPH_LEVEL_3:
  2773. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2774. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2775. deemph_reg_value = 43;
  2776. margin_reg_value = 154;
  2777. break;
  2778. default:
  2779. return 0;
  2780. }
  2781. break;
  2782. default:
  2783. return 0;
  2784. }
  2785. mutex_lock(&dev_priv->sb_lock);
  2786. /* Clear calc init */
  2787. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW10(ch));
  2788. val &= ~(DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3);
  2789. val &= ~(DPIO_PCS_TX1DEEMP_MASK | DPIO_PCS_TX2DEEMP_MASK);
  2790. val |= DPIO_PCS_TX1DEEMP_9P5 | DPIO_PCS_TX2DEEMP_9P5;
  2791. vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW10(ch), val);
  2792. if (intel_crtc->config->lane_count > 2) {
  2793. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW10(ch));
  2794. val &= ~(DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3);
  2795. val &= ~(DPIO_PCS_TX1DEEMP_MASK | DPIO_PCS_TX2DEEMP_MASK);
  2796. val |= DPIO_PCS_TX1DEEMP_9P5 | DPIO_PCS_TX2DEEMP_9P5;
  2797. vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW10(ch), val);
  2798. }
  2799. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW9(ch));
  2800. val &= ~(DPIO_PCS_TX1MARGIN_MASK | DPIO_PCS_TX2MARGIN_MASK);
  2801. val |= DPIO_PCS_TX1MARGIN_000 | DPIO_PCS_TX2MARGIN_000;
  2802. vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW9(ch), val);
  2803. if (intel_crtc->config->lane_count > 2) {
  2804. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW9(ch));
  2805. val &= ~(DPIO_PCS_TX1MARGIN_MASK | DPIO_PCS_TX2MARGIN_MASK);
  2806. val |= DPIO_PCS_TX1MARGIN_000 | DPIO_PCS_TX2MARGIN_000;
  2807. vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW9(ch), val);
  2808. }
  2809. /* Program swing deemph */
  2810. for (i = 0; i < intel_crtc->config->lane_count; i++) {
  2811. val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW4(ch, i));
  2812. val &= ~DPIO_SWING_DEEMPH9P5_MASK;
  2813. val |= deemph_reg_value << DPIO_SWING_DEEMPH9P5_SHIFT;
  2814. vlv_dpio_write(dev_priv, pipe, CHV_TX_DW4(ch, i), val);
  2815. }
  2816. /* Program swing margin */
  2817. for (i = 0; i < intel_crtc->config->lane_count; i++) {
  2818. val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW2(ch, i));
  2819. val &= ~DPIO_SWING_MARGIN000_MASK;
  2820. val |= margin_reg_value << DPIO_SWING_MARGIN000_SHIFT;
  2821. /*
  2822. * Supposedly this value shouldn't matter when unique transition
  2823. * scale is disabled, but in fact it does matter. Let's just
  2824. * always program the same value and hope it's OK.
  2825. */
  2826. val &= ~(0xff << DPIO_UNIQ_TRANS_SCALE_SHIFT);
  2827. val |= 0x9a << DPIO_UNIQ_TRANS_SCALE_SHIFT;
  2828. vlv_dpio_write(dev_priv, pipe, CHV_TX_DW2(ch, i), val);
  2829. }
  2830. /*
  2831. * The document said it needs to set bit 27 for ch0 and bit 26
  2832. * for ch1. Might be a typo in the doc.
  2833. * For now, for this unique transition scale selection, set bit
  2834. * 27 for ch0 and ch1.
  2835. */
  2836. for (i = 0; i < intel_crtc->config->lane_count; i++) {
  2837. val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW3(ch, i));
  2838. if (chv_need_uniq_trans_scale(train_set))
  2839. val |= DPIO_TX_UNIQ_TRANS_SCALE_EN;
  2840. else
  2841. val &= ~DPIO_TX_UNIQ_TRANS_SCALE_EN;
  2842. vlv_dpio_write(dev_priv, pipe, CHV_TX_DW3(ch, i), val);
  2843. }
  2844. /* Start swing calculation */
  2845. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW10(ch));
  2846. val |= DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3;
  2847. vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW10(ch), val);
  2848. if (intel_crtc->config->lane_count > 2) {
  2849. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW10(ch));
  2850. val |= DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3;
  2851. vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW10(ch), val);
  2852. }
  2853. mutex_unlock(&dev_priv->sb_lock);
  2854. return 0;
  2855. }
  2856. static void
  2857. intel_get_adjust_train(struct intel_dp *intel_dp,
  2858. const uint8_t link_status[DP_LINK_STATUS_SIZE])
  2859. {
  2860. uint8_t v = 0;
  2861. uint8_t p = 0;
  2862. int lane;
  2863. uint8_t voltage_max;
  2864. uint8_t preemph_max;
  2865. for (lane = 0; lane < intel_dp->lane_count; lane++) {
  2866. uint8_t this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
  2867. uint8_t this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
  2868. if (this_v > v)
  2869. v = this_v;
  2870. if (this_p > p)
  2871. p = this_p;
  2872. }
  2873. voltage_max = intel_dp_voltage_max(intel_dp);
  2874. if (v >= voltage_max)
  2875. v = voltage_max | DP_TRAIN_MAX_SWING_REACHED;
  2876. preemph_max = intel_dp_pre_emphasis_max(intel_dp, v);
  2877. if (p >= preemph_max)
  2878. p = preemph_max | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  2879. for (lane = 0; lane < 4; lane++)
  2880. intel_dp->train_set[lane] = v | p;
  2881. }
  2882. static uint32_t
  2883. gen4_signal_levels(uint8_t train_set)
  2884. {
  2885. uint32_t signal_levels = 0;
  2886. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  2887. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  2888. default:
  2889. signal_levels |= DP_VOLTAGE_0_4;
  2890. break;
  2891. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  2892. signal_levels |= DP_VOLTAGE_0_6;
  2893. break;
  2894. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  2895. signal_levels |= DP_VOLTAGE_0_8;
  2896. break;
  2897. case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  2898. signal_levels |= DP_VOLTAGE_1_2;
  2899. break;
  2900. }
  2901. switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  2902. case DP_TRAIN_PRE_EMPH_LEVEL_0:
  2903. default:
  2904. signal_levels |= DP_PRE_EMPHASIS_0;
  2905. break;
  2906. case DP_TRAIN_PRE_EMPH_LEVEL_1:
  2907. signal_levels |= DP_PRE_EMPHASIS_3_5;
  2908. break;
  2909. case DP_TRAIN_PRE_EMPH_LEVEL_2:
  2910. signal_levels |= DP_PRE_EMPHASIS_6;
  2911. break;
  2912. case DP_TRAIN_PRE_EMPH_LEVEL_3:
  2913. signal_levels |= DP_PRE_EMPHASIS_9_5;
  2914. break;
  2915. }
  2916. return signal_levels;
  2917. }
  2918. /* Gen6's DP voltage swing and pre-emphasis control */
  2919. static uint32_t
  2920. gen6_edp_signal_levels(uint8_t train_set)
  2921. {
  2922. int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
  2923. DP_TRAIN_PRE_EMPHASIS_MASK);
  2924. switch (signal_levels) {
  2925. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  2926. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  2927. return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
  2928. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  2929. return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
  2930. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
  2931. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2:
  2932. return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
  2933. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  2934. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  2935. return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
  2936. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  2937. case DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  2938. return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
  2939. default:
  2940. DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
  2941. "0x%x\n", signal_levels);
  2942. return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
  2943. }
  2944. }
  2945. /* Gen7's DP voltage swing and pre-emphasis control */
  2946. static uint32_t
  2947. gen7_edp_signal_levels(uint8_t train_set)
  2948. {
  2949. int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
  2950. DP_TRAIN_PRE_EMPHASIS_MASK);
  2951. switch (signal_levels) {
  2952. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  2953. return EDP_LINK_TRAIN_400MV_0DB_IVB;
  2954. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  2955. return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
  2956. case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
  2957. return EDP_LINK_TRAIN_400MV_6DB_IVB;
  2958. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  2959. return EDP_LINK_TRAIN_600MV_0DB_IVB;
  2960. case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  2961. return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
  2962. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  2963. return EDP_LINK_TRAIN_800MV_0DB_IVB;
  2964. case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  2965. return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
  2966. default:
  2967. DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
  2968. "0x%x\n", signal_levels);
  2969. return EDP_LINK_TRAIN_500MV_0DB_IVB;
  2970. }
  2971. }
  2972. /* Properly updates "DP" with the correct signal levels. */
  2973. static void
  2974. intel_dp_set_signal_levels(struct intel_dp *intel_dp, uint32_t *DP)
  2975. {
  2976. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  2977. enum port port = intel_dig_port->port;
  2978. struct drm_device *dev = intel_dig_port->base.base.dev;
  2979. uint32_t signal_levels, mask = 0;
  2980. uint8_t train_set = intel_dp->train_set[0];
  2981. if (HAS_DDI(dev)) {
  2982. signal_levels = ddi_signal_levels(intel_dp);
  2983. if (IS_BROXTON(dev))
  2984. signal_levels = 0;
  2985. else
  2986. mask = DDI_BUF_EMP_MASK;
  2987. } else if (IS_CHERRYVIEW(dev)) {
  2988. signal_levels = chv_signal_levels(intel_dp);
  2989. } else if (IS_VALLEYVIEW(dev)) {
  2990. signal_levels = vlv_signal_levels(intel_dp);
  2991. } else if (IS_GEN7(dev) && port == PORT_A) {
  2992. signal_levels = gen7_edp_signal_levels(train_set);
  2993. mask = EDP_LINK_TRAIN_VOL_EMP_MASK_IVB;
  2994. } else if (IS_GEN6(dev) && port == PORT_A) {
  2995. signal_levels = gen6_edp_signal_levels(train_set);
  2996. mask = EDP_LINK_TRAIN_VOL_EMP_MASK_SNB;
  2997. } else {
  2998. signal_levels = gen4_signal_levels(train_set);
  2999. mask = DP_VOLTAGE_MASK | DP_PRE_EMPHASIS_MASK;
  3000. }
  3001. if (mask)
  3002. DRM_DEBUG_KMS("Using signal levels %08x\n", signal_levels);
  3003. DRM_DEBUG_KMS("Using vswing level %d\n",
  3004. train_set & DP_TRAIN_VOLTAGE_SWING_MASK);
  3005. DRM_DEBUG_KMS("Using pre-emphasis level %d\n",
  3006. (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) >>
  3007. DP_TRAIN_PRE_EMPHASIS_SHIFT);
  3008. *DP = (*DP & ~mask) | signal_levels;
  3009. }
  3010. static bool
  3011. intel_dp_set_link_train(struct intel_dp *intel_dp,
  3012. uint32_t *DP,
  3013. uint8_t dp_train_pat)
  3014. {
  3015. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3016. struct drm_i915_private *dev_priv =
  3017. to_i915(intel_dig_port->base.base.dev);
  3018. uint8_t buf[sizeof(intel_dp->train_set) + 1];
  3019. int ret, len;
  3020. _intel_dp_set_link_train(intel_dp, DP, dp_train_pat);
  3021. I915_WRITE(intel_dp->output_reg, *DP);
  3022. POSTING_READ(intel_dp->output_reg);
  3023. buf[0] = dp_train_pat;
  3024. if ((dp_train_pat & DP_TRAINING_PATTERN_MASK) ==
  3025. DP_TRAINING_PATTERN_DISABLE) {
  3026. /* don't write DP_TRAINING_LANEx_SET on disable */
  3027. len = 1;
  3028. } else {
  3029. /* DP_TRAINING_LANEx_SET follow DP_TRAINING_PATTERN_SET */
  3030. memcpy(buf + 1, intel_dp->train_set, intel_dp->lane_count);
  3031. len = intel_dp->lane_count + 1;
  3032. }
  3033. ret = drm_dp_dpcd_write(&intel_dp->aux, DP_TRAINING_PATTERN_SET,
  3034. buf, len);
  3035. return ret == len;
  3036. }
  3037. static bool
  3038. intel_dp_reset_link_train(struct intel_dp *intel_dp, uint32_t *DP,
  3039. uint8_t dp_train_pat)
  3040. {
  3041. memset(intel_dp->train_set, 0, sizeof(intel_dp->train_set));
  3042. intel_dp_set_signal_levels(intel_dp, DP);
  3043. return intel_dp_set_link_train(intel_dp, DP, dp_train_pat);
  3044. }
  3045. static bool
  3046. intel_dp_update_link_train(struct intel_dp *intel_dp, uint32_t *DP,
  3047. const uint8_t link_status[DP_LINK_STATUS_SIZE])
  3048. {
  3049. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3050. struct drm_i915_private *dev_priv =
  3051. to_i915(intel_dig_port->base.base.dev);
  3052. int ret;
  3053. intel_get_adjust_train(intel_dp, link_status);
  3054. intel_dp_set_signal_levels(intel_dp, DP);
  3055. I915_WRITE(intel_dp->output_reg, *DP);
  3056. POSTING_READ(intel_dp->output_reg);
  3057. ret = drm_dp_dpcd_write(&intel_dp->aux, DP_TRAINING_LANE0_SET,
  3058. intel_dp->train_set, intel_dp->lane_count);
  3059. return ret == intel_dp->lane_count;
  3060. }
  3061. static void intel_dp_set_idle_link_train(struct intel_dp *intel_dp)
  3062. {
  3063. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3064. struct drm_device *dev = intel_dig_port->base.base.dev;
  3065. struct drm_i915_private *dev_priv = dev->dev_private;
  3066. enum port port = intel_dig_port->port;
  3067. uint32_t val;
  3068. if (!HAS_DDI(dev))
  3069. return;
  3070. val = I915_READ(DP_TP_CTL(port));
  3071. val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
  3072. val |= DP_TP_CTL_LINK_TRAIN_IDLE;
  3073. I915_WRITE(DP_TP_CTL(port), val);
  3074. /*
  3075. * On PORT_A we can have only eDP in SST mode. There the only reason
  3076. * we need to set idle transmission mode is to work around a HW issue
  3077. * where we enable the pipe while not in idle link-training mode.
  3078. * In this case there is requirement to wait for a minimum number of
  3079. * idle patterns to be sent.
  3080. */
  3081. if (port == PORT_A)
  3082. return;
  3083. if (wait_for((I915_READ(DP_TP_STATUS(port)) & DP_TP_STATUS_IDLE_DONE),
  3084. 1))
  3085. DRM_ERROR("Timed out waiting for DP idle patterns\n");
  3086. }
  3087. /* Enable corresponding port and start training pattern 1 */
  3088. static void
  3089. intel_dp_link_training_clock_recovery(struct intel_dp *intel_dp)
  3090. {
  3091. struct drm_encoder *encoder = &dp_to_dig_port(intel_dp)->base.base;
  3092. struct drm_device *dev = encoder->dev;
  3093. int i;
  3094. uint8_t voltage;
  3095. int voltage_tries, loop_tries;
  3096. uint32_t DP = intel_dp->DP;
  3097. uint8_t link_config[2];
  3098. uint8_t link_bw, rate_select;
  3099. if (HAS_DDI(dev))
  3100. intel_ddi_prepare_link_retrain(encoder);
  3101. intel_dp_compute_rate(intel_dp, intel_dp->link_rate,
  3102. &link_bw, &rate_select);
  3103. /* Write the link configuration data */
  3104. link_config[0] = link_bw;
  3105. link_config[1] = intel_dp->lane_count;
  3106. if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
  3107. link_config[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  3108. drm_dp_dpcd_write(&intel_dp->aux, DP_LINK_BW_SET, link_config, 2);
  3109. if (intel_dp->num_sink_rates)
  3110. drm_dp_dpcd_write(&intel_dp->aux, DP_LINK_RATE_SET,
  3111. &rate_select, 1);
  3112. link_config[0] = 0;
  3113. link_config[1] = DP_SET_ANSI_8B10B;
  3114. drm_dp_dpcd_write(&intel_dp->aux, DP_DOWNSPREAD_CTRL, link_config, 2);
  3115. DP |= DP_PORT_EN;
  3116. /* clock recovery */
  3117. if (!intel_dp_reset_link_train(intel_dp, &DP,
  3118. DP_TRAINING_PATTERN_1 |
  3119. DP_LINK_SCRAMBLING_DISABLE)) {
  3120. DRM_ERROR("failed to enable link training\n");
  3121. return;
  3122. }
  3123. voltage = 0xff;
  3124. voltage_tries = 0;
  3125. loop_tries = 0;
  3126. for (;;) {
  3127. uint8_t link_status[DP_LINK_STATUS_SIZE];
  3128. drm_dp_link_train_clock_recovery_delay(intel_dp->dpcd);
  3129. if (!intel_dp_get_link_status(intel_dp, link_status)) {
  3130. DRM_ERROR("failed to get link status\n");
  3131. break;
  3132. }
  3133. if (drm_dp_clock_recovery_ok(link_status, intel_dp->lane_count)) {
  3134. DRM_DEBUG_KMS("clock recovery OK\n");
  3135. break;
  3136. }
  3137. /* Check to see if we've tried the max voltage */
  3138. for (i = 0; i < intel_dp->lane_count; i++)
  3139. if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  3140. break;
  3141. if (i == intel_dp->lane_count) {
  3142. ++loop_tries;
  3143. if (loop_tries == 5) {
  3144. DRM_ERROR("too many full retries, give up\n");
  3145. break;
  3146. }
  3147. intel_dp_reset_link_train(intel_dp, &DP,
  3148. DP_TRAINING_PATTERN_1 |
  3149. DP_LINK_SCRAMBLING_DISABLE);
  3150. voltage_tries = 0;
  3151. continue;
  3152. }
  3153. /* Check to see if we've tried the same voltage 5 times */
  3154. if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  3155. ++voltage_tries;
  3156. if (voltage_tries == 5) {
  3157. DRM_ERROR("too many voltage retries, give up\n");
  3158. break;
  3159. }
  3160. } else
  3161. voltage_tries = 0;
  3162. voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  3163. /* Update training set as requested by target */
  3164. if (!intel_dp_update_link_train(intel_dp, &DP, link_status)) {
  3165. DRM_ERROR("failed to update link training\n");
  3166. break;
  3167. }
  3168. }
  3169. intel_dp->DP = DP;
  3170. }
  3171. static void
  3172. intel_dp_link_training_channel_equalization(struct intel_dp *intel_dp)
  3173. {
  3174. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  3175. struct drm_device *dev = dig_port->base.base.dev;
  3176. bool channel_eq = false;
  3177. int tries, cr_tries;
  3178. uint32_t DP = intel_dp->DP;
  3179. uint32_t training_pattern = DP_TRAINING_PATTERN_2;
  3180. /*
  3181. * Training Pattern 3 for HBR2 or 1.2 devices that support it.
  3182. *
  3183. * Intel platforms that support HBR2 also support TPS3. TPS3 support is
  3184. * also mandatory for downstream devices that support HBR2.
  3185. *
  3186. * Due to WaDisableHBR2 SKL < B0 is the only exception where TPS3 is
  3187. * supported but still not enabled.
  3188. */
  3189. if (intel_dp_source_supports_hbr2(dev) &&
  3190. drm_dp_tps3_supported(intel_dp->dpcd))
  3191. training_pattern = DP_TRAINING_PATTERN_3;
  3192. else if (intel_dp->link_rate == 540000)
  3193. DRM_ERROR("5.4 Gbps link rate without HBR2/TPS3 support\n");
  3194. /* channel equalization */
  3195. if (!intel_dp_set_link_train(intel_dp, &DP,
  3196. training_pattern |
  3197. DP_LINK_SCRAMBLING_DISABLE)) {
  3198. DRM_ERROR("failed to start channel equalization\n");
  3199. return;
  3200. }
  3201. tries = 0;
  3202. cr_tries = 0;
  3203. channel_eq = false;
  3204. for (;;) {
  3205. uint8_t link_status[DP_LINK_STATUS_SIZE];
  3206. if (cr_tries > 5) {
  3207. DRM_ERROR("failed to train DP, aborting\n");
  3208. break;
  3209. }
  3210. drm_dp_link_train_channel_eq_delay(intel_dp->dpcd);
  3211. if (!intel_dp_get_link_status(intel_dp, link_status)) {
  3212. DRM_ERROR("failed to get link status\n");
  3213. break;
  3214. }
  3215. /* Make sure clock is still ok */
  3216. if (!drm_dp_clock_recovery_ok(link_status,
  3217. intel_dp->lane_count)) {
  3218. intel_dp_link_training_clock_recovery(intel_dp);
  3219. intel_dp_set_link_train(intel_dp, &DP,
  3220. training_pattern |
  3221. DP_LINK_SCRAMBLING_DISABLE);
  3222. cr_tries++;
  3223. continue;
  3224. }
  3225. if (drm_dp_channel_eq_ok(link_status,
  3226. intel_dp->lane_count)) {
  3227. channel_eq = true;
  3228. break;
  3229. }
  3230. /* Try 5 times, then try clock recovery if that fails */
  3231. if (tries > 5) {
  3232. intel_dp_link_training_clock_recovery(intel_dp);
  3233. intel_dp_set_link_train(intel_dp, &DP,
  3234. training_pattern |
  3235. DP_LINK_SCRAMBLING_DISABLE);
  3236. tries = 0;
  3237. cr_tries++;
  3238. continue;
  3239. }
  3240. /* Update training set as requested by target */
  3241. if (!intel_dp_update_link_train(intel_dp, &DP, link_status)) {
  3242. DRM_ERROR("failed to update link training\n");
  3243. break;
  3244. }
  3245. ++tries;
  3246. }
  3247. intel_dp_set_idle_link_train(intel_dp);
  3248. intel_dp->DP = DP;
  3249. if (channel_eq)
  3250. DRM_DEBUG_KMS("Channel EQ done. DP Training successful\n");
  3251. }
  3252. void intel_dp_stop_link_train(struct intel_dp *intel_dp)
  3253. {
  3254. intel_dp_set_link_train(intel_dp, &intel_dp->DP,
  3255. DP_TRAINING_PATTERN_DISABLE);
  3256. }
  3257. void
  3258. intel_dp_start_link_train(struct intel_dp *intel_dp)
  3259. {
  3260. intel_dp_link_training_clock_recovery(intel_dp);
  3261. intel_dp_link_training_channel_equalization(intel_dp);
  3262. }
  3263. static void
  3264. intel_dp_link_down(struct intel_dp *intel_dp)
  3265. {
  3266. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3267. struct intel_crtc *crtc = to_intel_crtc(intel_dig_port->base.base.crtc);
  3268. enum port port = intel_dig_port->port;
  3269. struct drm_device *dev = intel_dig_port->base.base.dev;
  3270. struct drm_i915_private *dev_priv = dev->dev_private;
  3271. uint32_t DP = intel_dp->DP;
  3272. if (WARN_ON(HAS_DDI(dev)))
  3273. return;
  3274. if (WARN_ON((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0))
  3275. return;
  3276. DRM_DEBUG_KMS("\n");
  3277. if ((IS_GEN7(dev) && port == PORT_A) ||
  3278. (HAS_PCH_CPT(dev) && port != PORT_A)) {
  3279. DP &= ~DP_LINK_TRAIN_MASK_CPT;
  3280. DP |= DP_LINK_TRAIN_PAT_IDLE_CPT;
  3281. } else {
  3282. if (IS_CHERRYVIEW(dev))
  3283. DP &= ~DP_LINK_TRAIN_MASK_CHV;
  3284. else
  3285. DP &= ~DP_LINK_TRAIN_MASK;
  3286. DP |= DP_LINK_TRAIN_PAT_IDLE;
  3287. }
  3288. I915_WRITE(intel_dp->output_reg, DP);
  3289. POSTING_READ(intel_dp->output_reg);
  3290. DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
  3291. I915_WRITE(intel_dp->output_reg, DP);
  3292. POSTING_READ(intel_dp->output_reg);
  3293. /*
  3294. * HW workaround for IBX, we need to move the port
  3295. * to transcoder A after disabling it to allow the
  3296. * matching HDMI port to be enabled on transcoder A.
  3297. */
  3298. if (HAS_PCH_IBX(dev) && crtc->pipe == PIPE_B && port != PORT_A) {
  3299. /* always enable with pattern 1 (as per spec) */
  3300. DP &= ~(DP_PIPEB_SELECT | DP_LINK_TRAIN_MASK);
  3301. DP |= DP_PORT_EN | DP_LINK_TRAIN_PAT_1;
  3302. I915_WRITE(intel_dp->output_reg, DP);
  3303. POSTING_READ(intel_dp->output_reg);
  3304. DP &= ~DP_PORT_EN;
  3305. I915_WRITE(intel_dp->output_reg, DP);
  3306. POSTING_READ(intel_dp->output_reg);
  3307. }
  3308. msleep(intel_dp->panel_power_down_delay);
  3309. }
  3310. static bool
  3311. intel_dp_get_dpcd(struct intel_dp *intel_dp)
  3312. {
  3313. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  3314. struct drm_device *dev = dig_port->base.base.dev;
  3315. struct drm_i915_private *dev_priv = dev->dev_private;
  3316. uint8_t rev;
  3317. if (intel_dp_dpcd_read_wake(&intel_dp->aux, 0x000, intel_dp->dpcd,
  3318. sizeof(intel_dp->dpcd)) < 0)
  3319. return false; /* aux transfer failed */
  3320. DRM_DEBUG_KMS("DPCD: %*ph\n", (int) sizeof(intel_dp->dpcd), intel_dp->dpcd);
  3321. if (intel_dp->dpcd[DP_DPCD_REV] == 0)
  3322. return false; /* DPCD not present */
  3323. /* Check if the panel supports PSR */
  3324. memset(intel_dp->psr_dpcd, 0, sizeof(intel_dp->psr_dpcd));
  3325. if (is_edp(intel_dp)) {
  3326. intel_dp_dpcd_read_wake(&intel_dp->aux, DP_PSR_SUPPORT,
  3327. intel_dp->psr_dpcd,
  3328. sizeof(intel_dp->psr_dpcd));
  3329. if (intel_dp->psr_dpcd[0] & DP_PSR_IS_SUPPORTED) {
  3330. dev_priv->psr.sink_support = true;
  3331. DRM_DEBUG_KMS("Detected EDP PSR Panel.\n");
  3332. }
  3333. if (INTEL_INFO(dev)->gen >= 9 &&
  3334. (intel_dp->psr_dpcd[0] & DP_PSR2_IS_SUPPORTED)) {
  3335. uint8_t frame_sync_cap;
  3336. dev_priv->psr.sink_support = true;
  3337. intel_dp_dpcd_read_wake(&intel_dp->aux,
  3338. DP_SINK_DEVICE_AUX_FRAME_SYNC_CAP,
  3339. &frame_sync_cap, 1);
  3340. dev_priv->psr.aux_frame_sync = frame_sync_cap ? true : false;
  3341. /* PSR2 needs frame sync as well */
  3342. dev_priv->psr.psr2_support = dev_priv->psr.aux_frame_sync;
  3343. DRM_DEBUG_KMS("PSR2 %s on sink",
  3344. dev_priv->psr.psr2_support ? "supported" : "not supported");
  3345. }
  3346. }
  3347. DRM_DEBUG_KMS("Display Port TPS3 support: source %s, sink %s\n",
  3348. yesno(intel_dp_source_supports_hbr2(dev)),
  3349. yesno(drm_dp_tps3_supported(intel_dp->dpcd)));
  3350. /* Intermediate frequency support */
  3351. if (is_edp(intel_dp) &&
  3352. (intel_dp->dpcd[DP_EDP_CONFIGURATION_CAP] & DP_DPCD_DISPLAY_CONTROL_CAPABLE) &&
  3353. (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_EDP_DPCD_REV, &rev, 1) == 1) &&
  3354. (rev >= 0x03)) { /* eDp v1.4 or higher */
  3355. __le16 sink_rates[DP_MAX_SUPPORTED_RATES];
  3356. int i;
  3357. intel_dp_dpcd_read_wake(&intel_dp->aux,
  3358. DP_SUPPORTED_LINK_RATES,
  3359. sink_rates,
  3360. sizeof(sink_rates));
  3361. for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
  3362. int val = le16_to_cpu(sink_rates[i]);
  3363. if (val == 0)
  3364. break;
  3365. /* Value read is in kHz while drm clock is saved in deca-kHz */
  3366. intel_dp->sink_rates[i] = (val * 200) / 10;
  3367. }
  3368. intel_dp->num_sink_rates = i;
  3369. }
  3370. intel_dp_print_rates(intel_dp);
  3371. if (!(intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  3372. DP_DWN_STRM_PORT_PRESENT))
  3373. return true; /* native DP sink */
  3374. if (intel_dp->dpcd[DP_DPCD_REV] == 0x10)
  3375. return true; /* no per-port downstream info */
  3376. if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_DOWNSTREAM_PORT_0,
  3377. intel_dp->downstream_ports,
  3378. DP_MAX_DOWNSTREAM_PORTS) < 0)
  3379. return false; /* downstream port status fetch failed */
  3380. return true;
  3381. }
  3382. static void
  3383. intel_dp_probe_oui(struct intel_dp *intel_dp)
  3384. {
  3385. u8 buf[3];
  3386. if (!(intel_dp->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
  3387. return;
  3388. if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_SINK_OUI, buf, 3) == 3)
  3389. DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
  3390. buf[0], buf[1], buf[2]);
  3391. if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_BRANCH_OUI, buf, 3) == 3)
  3392. DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
  3393. buf[0], buf[1], buf[2]);
  3394. }
  3395. static bool
  3396. intel_dp_probe_mst(struct intel_dp *intel_dp)
  3397. {
  3398. u8 buf[1];
  3399. if (!intel_dp->can_mst)
  3400. return false;
  3401. if (intel_dp->dpcd[DP_DPCD_REV] < 0x12)
  3402. return false;
  3403. if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_MSTM_CAP, buf, 1)) {
  3404. if (buf[0] & DP_MST_CAP) {
  3405. DRM_DEBUG_KMS("Sink is MST capable\n");
  3406. intel_dp->is_mst = true;
  3407. } else {
  3408. DRM_DEBUG_KMS("Sink is not MST capable\n");
  3409. intel_dp->is_mst = false;
  3410. }
  3411. }
  3412. drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
  3413. return intel_dp->is_mst;
  3414. }
  3415. static int intel_dp_sink_crc_stop(struct intel_dp *intel_dp)
  3416. {
  3417. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  3418. struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
  3419. u8 buf;
  3420. int ret = 0;
  3421. if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0) {
  3422. DRM_DEBUG_KMS("Sink CRC couldn't be stopped properly\n");
  3423. ret = -EIO;
  3424. goto out;
  3425. }
  3426. if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
  3427. buf & ~DP_TEST_SINK_START) < 0) {
  3428. DRM_DEBUG_KMS("Sink CRC couldn't be stopped properly\n");
  3429. ret = -EIO;
  3430. goto out;
  3431. }
  3432. intel_dp->sink_crc.started = false;
  3433. out:
  3434. hsw_enable_ips(intel_crtc);
  3435. return ret;
  3436. }
  3437. static int intel_dp_sink_crc_start(struct intel_dp *intel_dp)
  3438. {
  3439. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  3440. struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
  3441. u8 buf;
  3442. int ret;
  3443. if (intel_dp->sink_crc.started) {
  3444. ret = intel_dp_sink_crc_stop(intel_dp);
  3445. if (ret)
  3446. return ret;
  3447. }
  3448. if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK_MISC, &buf) < 0)
  3449. return -EIO;
  3450. if (!(buf & DP_TEST_CRC_SUPPORTED))
  3451. return -ENOTTY;
  3452. intel_dp->sink_crc.last_count = buf & DP_TEST_COUNT_MASK;
  3453. if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0)
  3454. return -EIO;
  3455. hsw_disable_ips(intel_crtc);
  3456. if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
  3457. buf | DP_TEST_SINK_START) < 0) {
  3458. hsw_enable_ips(intel_crtc);
  3459. return -EIO;
  3460. }
  3461. intel_dp->sink_crc.started = true;
  3462. return 0;
  3463. }
  3464. int intel_dp_sink_crc(struct intel_dp *intel_dp, u8 *crc)
  3465. {
  3466. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  3467. struct drm_device *dev = dig_port->base.base.dev;
  3468. struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
  3469. u8 buf;
  3470. int count, ret;
  3471. int attempts = 6;
  3472. bool old_equal_new;
  3473. ret = intel_dp_sink_crc_start(intel_dp);
  3474. if (ret)
  3475. return ret;
  3476. do {
  3477. intel_wait_for_vblank(dev, intel_crtc->pipe);
  3478. if (drm_dp_dpcd_readb(&intel_dp->aux,
  3479. DP_TEST_SINK_MISC, &buf) < 0) {
  3480. ret = -EIO;
  3481. goto stop;
  3482. }
  3483. count = buf & DP_TEST_COUNT_MASK;
  3484. /*
  3485. * Count might be reset during the loop. In this case
  3486. * last known count needs to be reset as well.
  3487. */
  3488. if (count == 0)
  3489. intel_dp->sink_crc.last_count = 0;
  3490. if (drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_CRC_R_CR, crc, 6) < 0) {
  3491. ret = -EIO;
  3492. goto stop;
  3493. }
  3494. old_equal_new = (count == intel_dp->sink_crc.last_count &&
  3495. !memcmp(intel_dp->sink_crc.last_crc, crc,
  3496. 6 * sizeof(u8)));
  3497. } while (--attempts && (count == 0 || old_equal_new));
  3498. intel_dp->sink_crc.last_count = buf & DP_TEST_COUNT_MASK;
  3499. memcpy(intel_dp->sink_crc.last_crc, crc, 6 * sizeof(u8));
  3500. if (attempts == 0) {
  3501. if (old_equal_new) {
  3502. DRM_DEBUG_KMS("Unreliable Sink CRC counter: Current returned CRC is identical to the previous one\n");
  3503. } else {
  3504. DRM_ERROR("Panel is unable to calculate any CRC after 6 vblanks\n");
  3505. ret = -ETIMEDOUT;
  3506. goto stop;
  3507. }
  3508. }
  3509. stop:
  3510. intel_dp_sink_crc_stop(intel_dp);
  3511. return ret;
  3512. }
  3513. static bool
  3514. intel_dp_get_sink_irq(struct intel_dp *intel_dp, u8 *sink_irq_vector)
  3515. {
  3516. return intel_dp_dpcd_read_wake(&intel_dp->aux,
  3517. DP_DEVICE_SERVICE_IRQ_VECTOR,
  3518. sink_irq_vector, 1) == 1;
  3519. }
  3520. static bool
  3521. intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *sink_irq_vector)
  3522. {
  3523. int ret;
  3524. ret = intel_dp_dpcd_read_wake(&intel_dp->aux,
  3525. DP_SINK_COUNT_ESI,
  3526. sink_irq_vector, 14);
  3527. if (ret != 14)
  3528. return false;
  3529. return true;
  3530. }
  3531. static uint8_t intel_dp_autotest_link_training(struct intel_dp *intel_dp)
  3532. {
  3533. uint8_t test_result = DP_TEST_ACK;
  3534. return test_result;
  3535. }
  3536. static uint8_t intel_dp_autotest_video_pattern(struct intel_dp *intel_dp)
  3537. {
  3538. uint8_t test_result = DP_TEST_NAK;
  3539. return test_result;
  3540. }
  3541. static uint8_t intel_dp_autotest_edid(struct intel_dp *intel_dp)
  3542. {
  3543. uint8_t test_result = DP_TEST_NAK;
  3544. struct intel_connector *intel_connector = intel_dp->attached_connector;
  3545. struct drm_connector *connector = &intel_connector->base;
  3546. if (intel_connector->detect_edid == NULL ||
  3547. connector->edid_corrupt ||
  3548. intel_dp->aux.i2c_defer_count > 6) {
  3549. /* Check EDID read for NACKs, DEFERs and corruption
  3550. * (DP CTS 1.2 Core r1.1)
  3551. * 4.2.2.4 : Failed EDID read, I2C_NAK
  3552. * 4.2.2.5 : Failed EDID read, I2C_DEFER
  3553. * 4.2.2.6 : EDID corruption detected
  3554. * Use failsafe mode for all cases
  3555. */
  3556. if (intel_dp->aux.i2c_nack_count > 0 ||
  3557. intel_dp->aux.i2c_defer_count > 0)
  3558. DRM_DEBUG_KMS("EDID read had %d NACKs, %d DEFERs\n",
  3559. intel_dp->aux.i2c_nack_count,
  3560. intel_dp->aux.i2c_defer_count);
  3561. intel_dp->compliance_test_data = INTEL_DP_RESOLUTION_FAILSAFE;
  3562. } else {
  3563. struct edid *block = intel_connector->detect_edid;
  3564. /* We have to write the checksum
  3565. * of the last block read
  3566. */
  3567. block += intel_connector->detect_edid->extensions;
  3568. if (!drm_dp_dpcd_write(&intel_dp->aux,
  3569. DP_TEST_EDID_CHECKSUM,
  3570. &block->checksum,
  3571. 1))
  3572. DRM_DEBUG_KMS("Failed to write EDID checksum\n");
  3573. test_result = DP_TEST_ACK | DP_TEST_EDID_CHECKSUM_WRITE;
  3574. intel_dp->compliance_test_data = INTEL_DP_RESOLUTION_STANDARD;
  3575. }
  3576. /* Set test active flag here so userspace doesn't interrupt things */
  3577. intel_dp->compliance_test_active = 1;
  3578. return test_result;
  3579. }
  3580. static uint8_t intel_dp_autotest_phy_pattern(struct intel_dp *intel_dp)
  3581. {
  3582. uint8_t test_result = DP_TEST_NAK;
  3583. return test_result;
  3584. }
  3585. static void intel_dp_handle_test_request(struct intel_dp *intel_dp)
  3586. {
  3587. uint8_t response = DP_TEST_NAK;
  3588. uint8_t rxdata = 0;
  3589. int status = 0;
  3590. intel_dp->compliance_test_active = 0;
  3591. intel_dp->compliance_test_type = 0;
  3592. intel_dp->compliance_test_data = 0;
  3593. intel_dp->aux.i2c_nack_count = 0;
  3594. intel_dp->aux.i2c_defer_count = 0;
  3595. status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_REQUEST, &rxdata, 1);
  3596. if (status <= 0) {
  3597. DRM_DEBUG_KMS("Could not read test request from sink\n");
  3598. goto update_status;
  3599. }
  3600. switch (rxdata) {
  3601. case DP_TEST_LINK_TRAINING:
  3602. DRM_DEBUG_KMS("LINK_TRAINING test requested\n");
  3603. intel_dp->compliance_test_type = DP_TEST_LINK_TRAINING;
  3604. response = intel_dp_autotest_link_training(intel_dp);
  3605. break;
  3606. case DP_TEST_LINK_VIDEO_PATTERN:
  3607. DRM_DEBUG_KMS("TEST_PATTERN test requested\n");
  3608. intel_dp->compliance_test_type = DP_TEST_LINK_VIDEO_PATTERN;
  3609. response = intel_dp_autotest_video_pattern(intel_dp);
  3610. break;
  3611. case DP_TEST_LINK_EDID_READ:
  3612. DRM_DEBUG_KMS("EDID test requested\n");
  3613. intel_dp->compliance_test_type = DP_TEST_LINK_EDID_READ;
  3614. response = intel_dp_autotest_edid(intel_dp);
  3615. break;
  3616. case DP_TEST_LINK_PHY_TEST_PATTERN:
  3617. DRM_DEBUG_KMS("PHY_PATTERN test requested\n");
  3618. intel_dp->compliance_test_type = DP_TEST_LINK_PHY_TEST_PATTERN;
  3619. response = intel_dp_autotest_phy_pattern(intel_dp);
  3620. break;
  3621. default:
  3622. DRM_DEBUG_KMS("Invalid test request '%02x'\n", rxdata);
  3623. break;
  3624. }
  3625. update_status:
  3626. status = drm_dp_dpcd_write(&intel_dp->aux,
  3627. DP_TEST_RESPONSE,
  3628. &response, 1);
  3629. if (status <= 0)
  3630. DRM_DEBUG_KMS("Could not write test response to sink\n");
  3631. }
  3632. static int
  3633. intel_dp_check_mst_status(struct intel_dp *intel_dp)
  3634. {
  3635. bool bret;
  3636. if (intel_dp->is_mst) {
  3637. u8 esi[16] = { 0 };
  3638. int ret = 0;
  3639. int retry;
  3640. bool handled;
  3641. bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
  3642. go_again:
  3643. if (bret == true) {
  3644. /* check link status - esi[10] = 0x200c */
  3645. if (intel_dp->active_mst_links &&
  3646. !drm_dp_channel_eq_ok(&esi[10], intel_dp->lane_count)) {
  3647. DRM_DEBUG_KMS("channel EQ not ok, retraining\n");
  3648. intel_dp_start_link_train(intel_dp);
  3649. intel_dp_stop_link_train(intel_dp);
  3650. }
  3651. DRM_DEBUG_KMS("got esi %3ph\n", esi);
  3652. ret = drm_dp_mst_hpd_irq(&intel_dp->mst_mgr, esi, &handled);
  3653. if (handled) {
  3654. for (retry = 0; retry < 3; retry++) {
  3655. int wret;
  3656. wret = drm_dp_dpcd_write(&intel_dp->aux,
  3657. DP_SINK_COUNT_ESI+1,
  3658. &esi[1], 3);
  3659. if (wret == 3) {
  3660. break;
  3661. }
  3662. }
  3663. bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
  3664. if (bret == true) {
  3665. DRM_DEBUG_KMS("got esi2 %3ph\n", esi);
  3666. goto go_again;
  3667. }
  3668. } else
  3669. ret = 0;
  3670. return ret;
  3671. } else {
  3672. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3673. DRM_DEBUG_KMS("failed to get ESI - device may have failed\n");
  3674. intel_dp->is_mst = false;
  3675. drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
  3676. /* send a hotplug event */
  3677. drm_kms_helper_hotplug_event(intel_dig_port->base.base.dev);
  3678. }
  3679. }
  3680. return -EINVAL;
  3681. }
  3682. /*
  3683. * According to DP spec
  3684. * 5.1.2:
  3685. * 1. Read DPCD
  3686. * 2. Configure link according to Receiver Capabilities
  3687. * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
  3688. * 4. Check link status on receipt of hot-plug interrupt
  3689. */
  3690. static void
  3691. intel_dp_check_link_status(struct intel_dp *intel_dp)
  3692. {
  3693. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  3694. struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
  3695. u8 sink_irq_vector;
  3696. u8 link_status[DP_LINK_STATUS_SIZE];
  3697. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  3698. if (!intel_encoder->base.crtc)
  3699. return;
  3700. if (!to_intel_crtc(intel_encoder->base.crtc)->active)
  3701. return;
  3702. /* Try to read receiver status if the link appears to be up */
  3703. if (!intel_dp_get_link_status(intel_dp, link_status)) {
  3704. return;
  3705. }
  3706. /* Now read the DPCD to see if it's actually running */
  3707. if (!intel_dp_get_dpcd(intel_dp)) {
  3708. return;
  3709. }
  3710. /* Try to read the source of the interrupt */
  3711. if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
  3712. intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
  3713. /* Clear interrupt source */
  3714. drm_dp_dpcd_writeb(&intel_dp->aux,
  3715. DP_DEVICE_SERVICE_IRQ_VECTOR,
  3716. sink_irq_vector);
  3717. if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
  3718. DRM_DEBUG_DRIVER("Test request in short pulse not handled\n");
  3719. if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
  3720. DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
  3721. }
  3722. if (!drm_dp_channel_eq_ok(link_status, intel_dp->lane_count)) {
  3723. DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
  3724. intel_encoder->base.name);
  3725. intel_dp_start_link_train(intel_dp);
  3726. intel_dp_stop_link_train(intel_dp);
  3727. }
  3728. }
  3729. /* XXX this is probably wrong for multiple downstream ports */
  3730. static enum drm_connector_status
  3731. intel_dp_detect_dpcd(struct intel_dp *intel_dp)
  3732. {
  3733. uint8_t *dpcd = intel_dp->dpcd;
  3734. uint8_t type;
  3735. if (!intel_dp_get_dpcd(intel_dp))
  3736. return connector_status_disconnected;
  3737. /* if there's no downstream port, we're done */
  3738. if (!(dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT))
  3739. return connector_status_connected;
  3740. /* If we're HPD-aware, SINK_COUNT changes dynamically */
  3741. if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
  3742. intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
  3743. uint8_t reg;
  3744. if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_SINK_COUNT,
  3745. &reg, 1) < 0)
  3746. return connector_status_unknown;
  3747. return DP_GET_SINK_COUNT(reg) ? connector_status_connected
  3748. : connector_status_disconnected;
  3749. }
  3750. /* If no HPD, poke DDC gently */
  3751. if (drm_probe_ddc(&intel_dp->aux.ddc))
  3752. return connector_status_connected;
  3753. /* Well we tried, say unknown for unreliable port types */
  3754. if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
  3755. type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
  3756. if (type == DP_DS_PORT_TYPE_VGA ||
  3757. type == DP_DS_PORT_TYPE_NON_EDID)
  3758. return connector_status_unknown;
  3759. } else {
  3760. type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  3761. DP_DWN_STRM_PORT_TYPE_MASK;
  3762. if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
  3763. type == DP_DWN_STRM_PORT_TYPE_OTHER)
  3764. return connector_status_unknown;
  3765. }
  3766. /* Anything else is out of spec, warn and ignore */
  3767. DRM_DEBUG_KMS("Broken DP branch device, ignoring\n");
  3768. return connector_status_disconnected;
  3769. }
  3770. static enum drm_connector_status
  3771. edp_detect(struct intel_dp *intel_dp)
  3772. {
  3773. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  3774. enum drm_connector_status status;
  3775. status = intel_panel_detect(dev);
  3776. if (status == connector_status_unknown)
  3777. status = connector_status_connected;
  3778. return status;
  3779. }
  3780. static bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  3781. struct intel_digital_port *port)
  3782. {
  3783. u32 bit;
  3784. switch (port->port) {
  3785. case PORT_A:
  3786. return true;
  3787. case PORT_B:
  3788. bit = SDE_PORTB_HOTPLUG;
  3789. break;
  3790. case PORT_C:
  3791. bit = SDE_PORTC_HOTPLUG;
  3792. break;
  3793. case PORT_D:
  3794. bit = SDE_PORTD_HOTPLUG;
  3795. break;
  3796. default:
  3797. MISSING_CASE(port->port);
  3798. return false;
  3799. }
  3800. return I915_READ(SDEISR) & bit;
  3801. }
  3802. static bool cpt_digital_port_connected(struct drm_i915_private *dev_priv,
  3803. struct intel_digital_port *port)
  3804. {
  3805. u32 bit;
  3806. switch (port->port) {
  3807. case PORT_A:
  3808. return true;
  3809. case PORT_B:
  3810. bit = SDE_PORTB_HOTPLUG_CPT;
  3811. break;
  3812. case PORT_C:
  3813. bit = SDE_PORTC_HOTPLUG_CPT;
  3814. break;
  3815. case PORT_D:
  3816. bit = SDE_PORTD_HOTPLUG_CPT;
  3817. break;
  3818. case PORT_E:
  3819. bit = SDE_PORTE_HOTPLUG_SPT;
  3820. break;
  3821. default:
  3822. MISSING_CASE(port->port);
  3823. return false;
  3824. }
  3825. return I915_READ(SDEISR) & bit;
  3826. }
  3827. static bool g4x_digital_port_connected(struct drm_i915_private *dev_priv,
  3828. struct intel_digital_port *port)
  3829. {
  3830. u32 bit;
  3831. switch (port->port) {
  3832. case PORT_B:
  3833. bit = PORTB_HOTPLUG_LIVE_STATUS_G4X;
  3834. break;
  3835. case PORT_C:
  3836. bit = PORTC_HOTPLUG_LIVE_STATUS_G4X;
  3837. break;
  3838. case PORT_D:
  3839. bit = PORTD_HOTPLUG_LIVE_STATUS_G4X;
  3840. break;
  3841. default:
  3842. MISSING_CASE(port->port);
  3843. return false;
  3844. }
  3845. return I915_READ(PORT_HOTPLUG_STAT) & bit;
  3846. }
  3847. static bool gm45_digital_port_connected(struct drm_i915_private *dev_priv,
  3848. struct intel_digital_port *port)
  3849. {
  3850. u32 bit;
  3851. switch (port->port) {
  3852. case PORT_B:
  3853. bit = PORTB_HOTPLUG_LIVE_STATUS_GM45;
  3854. break;
  3855. case PORT_C:
  3856. bit = PORTC_HOTPLUG_LIVE_STATUS_GM45;
  3857. break;
  3858. case PORT_D:
  3859. bit = PORTD_HOTPLUG_LIVE_STATUS_GM45;
  3860. break;
  3861. default:
  3862. MISSING_CASE(port->port);
  3863. return false;
  3864. }
  3865. return I915_READ(PORT_HOTPLUG_STAT) & bit;
  3866. }
  3867. static bool bxt_digital_port_connected(struct drm_i915_private *dev_priv,
  3868. struct intel_digital_port *intel_dig_port)
  3869. {
  3870. struct intel_encoder *intel_encoder = &intel_dig_port->base;
  3871. enum port port;
  3872. u32 bit;
  3873. intel_hpd_pin_to_port(intel_encoder->hpd_pin, &port);
  3874. switch (port) {
  3875. case PORT_A:
  3876. bit = BXT_DE_PORT_HP_DDIA;
  3877. break;
  3878. case PORT_B:
  3879. bit = BXT_DE_PORT_HP_DDIB;
  3880. break;
  3881. case PORT_C:
  3882. bit = BXT_DE_PORT_HP_DDIC;
  3883. break;
  3884. default:
  3885. MISSING_CASE(port);
  3886. return false;
  3887. }
  3888. return I915_READ(GEN8_DE_PORT_ISR) & bit;
  3889. }
  3890. /*
  3891. * intel_digital_port_connected - is the specified port connected?
  3892. * @dev_priv: i915 private structure
  3893. * @port: the port to test
  3894. *
  3895. * Return %true if @port is connected, %false otherwise.
  3896. */
  3897. static bool intel_digital_port_connected(struct drm_i915_private *dev_priv,
  3898. struct intel_digital_port *port)
  3899. {
  3900. if (HAS_PCH_IBX(dev_priv))
  3901. return ibx_digital_port_connected(dev_priv, port);
  3902. if (HAS_PCH_SPLIT(dev_priv))
  3903. return cpt_digital_port_connected(dev_priv, port);
  3904. else if (IS_BROXTON(dev_priv))
  3905. return bxt_digital_port_connected(dev_priv, port);
  3906. else if (IS_GM45(dev_priv))
  3907. return gm45_digital_port_connected(dev_priv, port);
  3908. else
  3909. return g4x_digital_port_connected(dev_priv, port);
  3910. }
  3911. static enum drm_connector_status
  3912. ironlake_dp_detect(struct intel_dp *intel_dp)
  3913. {
  3914. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  3915. struct drm_i915_private *dev_priv = dev->dev_private;
  3916. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3917. if (!intel_digital_port_connected(dev_priv, intel_dig_port))
  3918. return connector_status_disconnected;
  3919. return intel_dp_detect_dpcd(intel_dp);
  3920. }
  3921. static enum drm_connector_status
  3922. g4x_dp_detect(struct intel_dp *intel_dp)
  3923. {
  3924. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  3925. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3926. /* Can't disconnect eDP, but you can close the lid... */
  3927. if (is_edp(intel_dp)) {
  3928. enum drm_connector_status status;
  3929. status = intel_panel_detect(dev);
  3930. if (status == connector_status_unknown)
  3931. status = connector_status_connected;
  3932. return status;
  3933. }
  3934. if (!intel_digital_port_connected(dev->dev_private, intel_dig_port))
  3935. return connector_status_disconnected;
  3936. return intel_dp_detect_dpcd(intel_dp);
  3937. }
  3938. static struct edid *
  3939. intel_dp_get_edid(struct intel_dp *intel_dp)
  3940. {
  3941. struct intel_connector *intel_connector = intel_dp->attached_connector;
  3942. /* use cached edid if we have one */
  3943. if (intel_connector->edid) {
  3944. /* invalid edid */
  3945. if (IS_ERR(intel_connector->edid))
  3946. return NULL;
  3947. return drm_edid_duplicate(intel_connector->edid);
  3948. } else
  3949. return drm_get_edid(&intel_connector->base,
  3950. &intel_dp->aux.ddc);
  3951. }
  3952. static void
  3953. intel_dp_set_edid(struct intel_dp *intel_dp)
  3954. {
  3955. struct intel_connector *intel_connector = intel_dp->attached_connector;
  3956. struct edid *edid;
  3957. edid = intel_dp_get_edid(intel_dp);
  3958. intel_connector->detect_edid = edid;
  3959. if (intel_dp->force_audio != HDMI_AUDIO_AUTO)
  3960. intel_dp->has_audio = intel_dp->force_audio == HDMI_AUDIO_ON;
  3961. else
  3962. intel_dp->has_audio = drm_detect_monitor_audio(edid);
  3963. }
  3964. static void
  3965. intel_dp_unset_edid(struct intel_dp *intel_dp)
  3966. {
  3967. struct intel_connector *intel_connector = intel_dp->attached_connector;
  3968. kfree(intel_connector->detect_edid);
  3969. intel_connector->detect_edid = NULL;
  3970. intel_dp->has_audio = false;
  3971. }
  3972. static enum drm_connector_status
  3973. intel_dp_detect(struct drm_connector *connector, bool force)
  3974. {
  3975. struct intel_dp *intel_dp = intel_attached_dp(connector);
  3976. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3977. struct intel_encoder *intel_encoder = &intel_dig_port->base;
  3978. struct drm_device *dev = connector->dev;
  3979. enum drm_connector_status status;
  3980. enum intel_display_power_domain power_domain;
  3981. bool ret;
  3982. u8 sink_irq_vector;
  3983. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  3984. connector->base.id, connector->name);
  3985. intel_dp_unset_edid(intel_dp);
  3986. if (intel_dp->is_mst) {
  3987. /* MST devices are disconnected from a monitor POV */
  3988. if (intel_encoder->type != INTEL_OUTPUT_EDP)
  3989. intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  3990. return connector_status_disconnected;
  3991. }
  3992. power_domain = intel_display_port_aux_power_domain(intel_encoder);
  3993. intel_display_power_get(to_i915(dev), power_domain);
  3994. /* Can't disconnect eDP, but you can close the lid... */
  3995. if (is_edp(intel_dp))
  3996. status = edp_detect(intel_dp);
  3997. else if (HAS_PCH_SPLIT(dev))
  3998. status = ironlake_dp_detect(intel_dp);
  3999. else
  4000. status = g4x_dp_detect(intel_dp);
  4001. if (status != connector_status_connected)
  4002. goto out;
  4003. intel_dp_probe_oui(intel_dp);
  4004. ret = intel_dp_probe_mst(intel_dp);
  4005. if (ret) {
  4006. /* if we are in MST mode then this connector
  4007. won't appear connected or have anything with EDID on it */
  4008. if (intel_encoder->type != INTEL_OUTPUT_EDP)
  4009. intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  4010. status = connector_status_disconnected;
  4011. goto out;
  4012. }
  4013. intel_dp_set_edid(intel_dp);
  4014. if (intel_encoder->type != INTEL_OUTPUT_EDP)
  4015. intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  4016. status = connector_status_connected;
  4017. /* Try to read the source of the interrupt */
  4018. if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
  4019. intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
  4020. /* Clear interrupt source */
  4021. drm_dp_dpcd_writeb(&intel_dp->aux,
  4022. DP_DEVICE_SERVICE_IRQ_VECTOR,
  4023. sink_irq_vector);
  4024. if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
  4025. intel_dp_handle_test_request(intel_dp);
  4026. if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
  4027. DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
  4028. }
  4029. out:
  4030. intel_display_power_put(to_i915(dev), power_domain);
  4031. return status;
  4032. }
  4033. static void
  4034. intel_dp_force(struct drm_connector *connector)
  4035. {
  4036. struct intel_dp *intel_dp = intel_attached_dp(connector);
  4037. struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
  4038. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4039. enum intel_display_power_domain power_domain;
  4040. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4041. connector->base.id, connector->name);
  4042. intel_dp_unset_edid(intel_dp);
  4043. if (connector->status != connector_status_connected)
  4044. return;
  4045. power_domain = intel_display_port_aux_power_domain(intel_encoder);
  4046. intel_display_power_get(dev_priv, power_domain);
  4047. intel_dp_set_edid(intel_dp);
  4048. intel_display_power_put(dev_priv, power_domain);
  4049. if (intel_encoder->type != INTEL_OUTPUT_EDP)
  4050. intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  4051. }
  4052. static int intel_dp_get_modes(struct drm_connector *connector)
  4053. {
  4054. struct intel_connector *intel_connector = to_intel_connector(connector);
  4055. struct edid *edid;
  4056. edid = intel_connector->detect_edid;
  4057. if (edid) {
  4058. int ret = intel_connector_update_modes(connector, edid);
  4059. if (ret)
  4060. return ret;
  4061. }
  4062. /* if eDP has no EDID, fall back to fixed mode */
  4063. if (is_edp(intel_attached_dp(connector)) &&
  4064. intel_connector->panel.fixed_mode) {
  4065. struct drm_display_mode *mode;
  4066. mode = drm_mode_duplicate(connector->dev,
  4067. intel_connector->panel.fixed_mode);
  4068. if (mode) {
  4069. drm_mode_probed_add(connector, mode);
  4070. return 1;
  4071. }
  4072. }
  4073. return 0;
  4074. }
  4075. static bool
  4076. intel_dp_detect_audio(struct drm_connector *connector)
  4077. {
  4078. bool has_audio = false;
  4079. struct edid *edid;
  4080. edid = to_intel_connector(connector)->detect_edid;
  4081. if (edid)
  4082. has_audio = drm_detect_monitor_audio(edid);
  4083. return has_audio;
  4084. }
  4085. static int
  4086. intel_dp_set_property(struct drm_connector *connector,
  4087. struct drm_property *property,
  4088. uint64_t val)
  4089. {
  4090. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4091. struct intel_connector *intel_connector = to_intel_connector(connector);
  4092. struct intel_encoder *intel_encoder = intel_attached_encoder(connector);
  4093. struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
  4094. int ret;
  4095. ret = drm_object_property_set_value(&connector->base, property, val);
  4096. if (ret)
  4097. return ret;
  4098. if (property == dev_priv->force_audio_property) {
  4099. int i = val;
  4100. bool has_audio;
  4101. if (i == intel_dp->force_audio)
  4102. return 0;
  4103. intel_dp->force_audio = i;
  4104. if (i == HDMI_AUDIO_AUTO)
  4105. has_audio = intel_dp_detect_audio(connector);
  4106. else
  4107. has_audio = (i == HDMI_AUDIO_ON);
  4108. if (has_audio == intel_dp->has_audio)
  4109. return 0;
  4110. intel_dp->has_audio = has_audio;
  4111. goto done;
  4112. }
  4113. if (property == dev_priv->broadcast_rgb_property) {
  4114. bool old_auto = intel_dp->color_range_auto;
  4115. bool old_range = intel_dp->limited_color_range;
  4116. switch (val) {
  4117. case INTEL_BROADCAST_RGB_AUTO:
  4118. intel_dp->color_range_auto = true;
  4119. break;
  4120. case INTEL_BROADCAST_RGB_FULL:
  4121. intel_dp->color_range_auto = false;
  4122. intel_dp->limited_color_range = false;
  4123. break;
  4124. case INTEL_BROADCAST_RGB_LIMITED:
  4125. intel_dp->color_range_auto = false;
  4126. intel_dp->limited_color_range = true;
  4127. break;
  4128. default:
  4129. return -EINVAL;
  4130. }
  4131. if (old_auto == intel_dp->color_range_auto &&
  4132. old_range == intel_dp->limited_color_range)
  4133. return 0;
  4134. goto done;
  4135. }
  4136. if (is_edp(intel_dp) &&
  4137. property == connector->dev->mode_config.scaling_mode_property) {
  4138. if (val == DRM_MODE_SCALE_NONE) {
  4139. DRM_DEBUG_KMS("no scaling not supported\n");
  4140. return -EINVAL;
  4141. }
  4142. if (intel_connector->panel.fitting_mode == val) {
  4143. /* the eDP scaling property is not changed */
  4144. return 0;
  4145. }
  4146. intel_connector->panel.fitting_mode = val;
  4147. goto done;
  4148. }
  4149. return -EINVAL;
  4150. done:
  4151. if (intel_encoder->base.crtc)
  4152. intel_crtc_restore_mode(intel_encoder->base.crtc);
  4153. return 0;
  4154. }
  4155. static void
  4156. intel_dp_connector_destroy(struct drm_connector *connector)
  4157. {
  4158. struct intel_connector *intel_connector = to_intel_connector(connector);
  4159. kfree(intel_connector->detect_edid);
  4160. if (!IS_ERR_OR_NULL(intel_connector->edid))
  4161. kfree(intel_connector->edid);
  4162. /* Can't call is_edp() since the encoder may have been destroyed
  4163. * already. */
  4164. if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  4165. intel_panel_fini(&intel_connector->panel);
  4166. drm_connector_cleanup(connector);
  4167. kfree(connector);
  4168. }
  4169. void intel_dp_encoder_destroy(struct drm_encoder *encoder)
  4170. {
  4171. struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  4172. struct intel_dp *intel_dp = &intel_dig_port->dp;
  4173. drm_dp_aux_unregister(&intel_dp->aux);
  4174. intel_dp_mst_encoder_cleanup(intel_dig_port);
  4175. if (is_edp(intel_dp)) {
  4176. cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
  4177. /*
  4178. * vdd might still be enabled do to the delayed vdd off.
  4179. * Make sure vdd is actually turned off here.
  4180. */
  4181. pps_lock(intel_dp);
  4182. edp_panel_vdd_off_sync(intel_dp);
  4183. pps_unlock(intel_dp);
  4184. if (intel_dp->edp_notifier.notifier_call) {
  4185. unregister_reboot_notifier(&intel_dp->edp_notifier);
  4186. intel_dp->edp_notifier.notifier_call = NULL;
  4187. }
  4188. }
  4189. drm_encoder_cleanup(encoder);
  4190. kfree(intel_dig_port);
  4191. }
  4192. void intel_dp_encoder_suspend(struct intel_encoder *intel_encoder)
  4193. {
  4194. struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
  4195. if (!is_edp(intel_dp))
  4196. return;
  4197. /*
  4198. * vdd might still be enabled do to the delayed vdd off.
  4199. * Make sure vdd is actually turned off here.
  4200. */
  4201. cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
  4202. pps_lock(intel_dp);
  4203. edp_panel_vdd_off_sync(intel_dp);
  4204. pps_unlock(intel_dp);
  4205. }
  4206. static void intel_edp_panel_vdd_sanitize(struct intel_dp *intel_dp)
  4207. {
  4208. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  4209. struct drm_device *dev = intel_dig_port->base.base.dev;
  4210. struct drm_i915_private *dev_priv = dev->dev_private;
  4211. enum intel_display_power_domain power_domain;
  4212. lockdep_assert_held(&dev_priv->pps_mutex);
  4213. if (!edp_have_panel_vdd(intel_dp))
  4214. return;
  4215. /*
  4216. * The VDD bit needs a power domain reference, so if the bit is
  4217. * already enabled when we boot or resume, grab this reference and
  4218. * schedule a vdd off, so we don't hold on to the reference
  4219. * indefinitely.
  4220. */
  4221. DRM_DEBUG_KMS("VDD left on by BIOS, adjusting state tracking\n");
  4222. power_domain = intel_display_port_aux_power_domain(&intel_dig_port->base);
  4223. intel_display_power_get(dev_priv, power_domain);
  4224. edp_panel_vdd_schedule_off(intel_dp);
  4225. }
  4226. void intel_dp_encoder_reset(struct drm_encoder *encoder)
  4227. {
  4228. struct drm_i915_private *dev_priv = to_i915(encoder->dev);
  4229. struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
  4230. if (!HAS_DDI(dev_priv))
  4231. intel_dp->DP = I915_READ(intel_dp->output_reg);
  4232. if (to_intel_encoder(encoder)->type != INTEL_OUTPUT_EDP)
  4233. return;
  4234. pps_lock(intel_dp);
  4235. /*
  4236. * Read out the current power sequencer assignment,
  4237. * in case the BIOS did something with it.
  4238. */
  4239. if (IS_VALLEYVIEW(encoder->dev))
  4240. vlv_initial_power_sequencer_setup(intel_dp);
  4241. intel_edp_panel_vdd_sanitize(intel_dp);
  4242. pps_unlock(intel_dp);
  4243. }
  4244. static const struct drm_connector_funcs intel_dp_connector_funcs = {
  4245. .dpms = drm_atomic_helper_connector_dpms,
  4246. .detect = intel_dp_detect,
  4247. .force = intel_dp_force,
  4248. .fill_modes = drm_helper_probe_single_connector_modes,
  4249. .set_property = intel_dp_set_property,
  4250. .atomic_get_property = intel_connector_atomic_get_property,
  4251. .destroy = intel_dp_connector_destroy,
  4252. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  4253. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  4254. };
  4255. static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
  4256. .get_modes = intel_dp_get_modes,
  4257. .mode_valid = intel_dp_mode_valid,
  4258. .best_encoder = intel_best_encoder,
  4259. };
  4260. static const struct drm_encoder_funcs intel_dp_enc_funcs = {
  4261. .reset = intel_dp_encoder_reset,
  4262. .destroy = intel_dp_encoder_destroy,
  4263. };
  4264. enum irqreturn
  4265. intel_dp_hpd_pulse(struct intel_digital_port *intel_dig_port, bool long_hpd)
  4266. {
  4267. struct intel_dp *intel_dp = &intel_dig_port->dp;
  4268. struct intel_encoder *intel_encoder = &intel_dig_port->base;
  4269. struct drm_device *dev = intel_dig_port->base.base.dev;
  4270. struct drm_i915_private *dev_priv = dev->dev_private;
  4271. enum intel_display_power_domain power_domain;
  4272. enum irqreturn ret = IRQ_NONE;
  4273. if (intel_dig_port->base.type != INTEL_OUTPUT_EDP &&
  4274. intel_dig_port->base.type != INTEL_OUTPUT_HDMI)
  4275. intel_dig_port->base.type = INTEL_OUTPUT_DISPLAYPORT;
  4276. if (long_hpd && intel_dig_port->base.type == INTEL_OUTPUT_EDP) {
  4277. /*
  4278. * vdd off can generate a long pulse on eDP which
  4279. * would require vdd on to handle it, and thus we
  4280. * would end up in an endless cycle of
  4281. * "vdd off -> long hpd -> vdd on -> detect -> vdd off -> ..."
  4282. */
  4283. DRM_DEBUG_KMS("ignoring long hpd on eDP port %c\n",
  4284. port_name(intel_dig_port->port));
  4285. return IRQ_HANDLED;
  4286. }
  4287. DRM_DEBUG_KMS("got hpd irq on port %c - %s\n",
  4288. port_name(intel_dig_port->port),
  4289. long_hpd ? "long" : "short");
  4290. power_domain = intel_display_port_aux_power_domain(intel_encoder);
  4291. intel_display_power_get(dev_priv, power_domain);
  4292. if (long_hpd) {
  4293. if (!intel_digital_port_connected(dev_priv, intel_dig_port))
  4294. goto mst_fail;
  4295. if (!intel_dp_get_dpcd(intel_dp)) {
  4296. goto mst_fail;
  4297. }
  4298. intel_dp_probe_oui(intel_dp);
  4299. if (!intel_dp_probe_mst(intel_dp)) {
  4300. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  4301. intel_dp_check_link_status(intel_dp);
  4302. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  4303. goto mst_fail;
  4304. }
  4305. } else {
  4306. if (intel_dp->is_mst) {
  4307. if (intel_dp_check_mst_status(intel_dp) == -EINVAL)
  4308. goto mst_fail;
  4309. }
  4310. if (!intel_dp->is_mst) {
  4311. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  4312. intel_dp_check_link_status(intel_dp);
  4313. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  4314. }
  4315. }
  4316. ret = IRQ_HANDLED;
  4317. goto put_power;
  4318. mst_fail:
  4319. /* if we were in MST mode, and device is not there get out of MST mode */
  4320. if (intel_dp->is_mst) {
  4321. DRM_DEBUG_KMS("MST device may have disappeared %d vs %d\n", intel_dp->is_mst, intel_dp->mst_mgr.mst_state);
  4322. intel_dp->is_mst = false;
  4323. drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
  4324. }
  4325. put_power:
  4326. intel_display_power_put(dev_priv, power_domain);
  4327. return ret;
  4328. }
  4329. /* Return which DP Port should be selected for Transcoder DP control */
  4330. int
  4331. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  4332. {
  4333. struct drm_device *dev = crtc->dev;
  4334. struct intel_encoder *intel_encoder;
  4335. struct intel_dp *intel_dp;
  4336. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4337. intel_dp = enc_to_intel_dp(&intel_encoder->base);
  4338. if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  4339. intel_encoder->type == INTEL_OUTPUT_EDP)
  4340. return intel_dp->output_reg;
  4341. }
  4342. return -1;
  4343. }
  4344. /* check the VBT to see whether the eDP is on another port */
  4345. bool intel_dp_is_edp(struct drm_device *dev, enum port port)
  4346. {
  4347. struct drm_i915_private *dev_priv = dev->dev_private;
  4348. union child_device_config *p_child;
  4349. int i;
  4350. static const short port_mapping[] = {
  4351. [PORT_B] = DVO_PORT_DPB,
  4352. [PORT_C] = DVO_PORT_DPC,
  4353. [PORT_D] = DVO_PORT_DPD,
  4354. [PORT_E] = DVO_PORT_DPE,
  4355. };
  4356. /*
  4357. * eDP not supported on g4x. so bail out early just
  4358. * for a bit extra safety in case the VBT is bonkers.
  4359. */
  4360. if (INTEL_INFO(dev)->gen < 5)
  4361. return false;
  4362. if (port == PORT_A)
  4363. return true;
  4364. if (!dev_priv->vbt.child_dev_num)
  4365. return false;
  4366. for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
  4367. p_child = dev_priv->vbt.child_dev + i;
  4368. if (p_child->common.dvo_port == port_mapping[port] &&
  4369. (p_child->common.device_type & DEVICE_TYPE_eDP_BITS) ==
  4370. (DEVICE_TYPE_eDP & DEVICE_TYPE_eDP_BITS))
  4371. return true;
  4372. }
  4373. return false;
  4374. }
  4375. void
  4376. intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
  4377. {
  4378. struct intel_connector *intel_connector = to_intel_connector(connector);
  4379. intel_attach_force_audio_property(connector);
  4380. intel_attach_broadcast_rgb_property(connector);
  4381. intel_dp->color_range_auto = true;
  4382. if (is_edp(intel_dp)) {
  4383. drm_mode_create_scaling_mode_property(connector->dev);
  4384. drm_object_attach_property(
  4385. &connector->base,
  4386. connector->dev->mode_config.scaling_mode_property,
  4387. DRM_MODE_SCALE_ASPECT);
  4388. intel_connector->panel.fitting_mode = DRM_MODE_SCALE_ASPECT;
  4389. }
  4390. }
  4391. static void intel_dp_init_panel_power_timestamps(struct intel_dp *intel_dp)
  4392. {
  4393. intel_dp->last_power_cycle = jiffies;
  4394. intel_dp->last_power_on = jiffies;
  4395. intel_dp->last_backlight_off = jiffies;
  4396. }
  4397. static void
  4398. intel_dp_init_panel_power_sequencer(struct drm_device *dev,
  4399. struct intel_dp *intel_dp)
  4400. {
  4401. struct drm_i915_private *dev_priv = dev->dev_private;
  4402. struct edp_power_seq cur, vbt, spec,
  4403. *final = &intel_dp->pps_delays;
  4404. u32 pp_on, pp_off, pp_div = 0, pp_ctl = 0;
  4405. int pp_ctrl_reg, pp_on_reg, pp_off_reg, pp_div_reg = 0;
  4406. lockdep_assert_held(&dev_priv->pps_mutex);
  4407. /* already initialized? */
  4408. if (final->t11_t12 != 0)
  4409. return;
  4410. if (IS_BROXTON(dev)) {
  4411. /*
  4412. * TODO: BXT has 2 sets of PPS registers.
  4413. * Correct Register for Broxton need to be identified
  4414. * using VBT. hardcoding for now
  4415. */
  4416. pp_ctrl_reg = BXT_PP_CONTROL(0);
  4417. pp_on_reg = BXT_PP_ON_DELAYS(0);
  4418. pp_off_reg = BXT_PP_OFF_DELAYS(0);
  4419. } else if (HAS_PCH_SPLIT(dev)) {
  4420. pp_ctrl_reg = PCH_PP_CONTROL;
  4421. pp_on_reg = PCH_PP_ON_DELAYS;
  4422. pp_off_reg = PCH_PP_OFF_DELAYS;
  4423. pp_div_reg = PCH_PP_DIVISOR;
  4424. } else {
  4425. enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
  4426. pp_ctrl_reg = VLV_PIPE_PP_CONTROL(pipe);
  4427. pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
  4428. pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
  4429. pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
  4430. }
  4431. /* Workaround: Need to write PP_CONTROL with the unlock key as
  4432. * the very first thing. */
  4433. pp_ctl = ironlake_get_pp_control(intel_dp);
  4434. pp_on = I915_READ(pp_on_reg);
  4435. pp_off = I915_READ(pp_off_reg);
  4436. if (!IS_BROXTON(dev)) {
  4437. I915_WRITE(pp_ctrl_reg, pp_ctl);
  4438. pp_div = I915_READ(pp_div_reg);
  4439. }
  4440. /* Pull timing values out of registers */
  4441. cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
  4442. PANEL_POWER_UP_DELAY_SHIFT;
  4443. cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
  4444. PANEL_LIGHT_ON_DELAY_SHIFT;
  4445. cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
  4446. PANEL_LIGHT_OFF_DELAY_SHIFT;
  4447. cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
  4448. PANEL_POWER_DOWN_DELAY_SHIFT;
  4449. if (IS_BROXTON(dev)) {
  4450. u16 tmp = (pp_ctl & BXT_POWER_CYCLE_DELAY_MASK) >>
  4451. BXT_POWER_CYCLE_DELAY_SHIFT;
  4452. if (tmp > 0)
  4453. cur.t11_t12 = (tmp - 1) * 1000;
  4454. else
  4455. cur.t11_t12 = 0;
  4456. } else {
  4457. cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
  4458. PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
  4459. }
  4460. DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
  4461. cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
  4462. vbt = dev_priv->vbt.edp_pps;
  4463. /* Upper limits from eDP 1.3 spec. Note that we use the clunky units of
  4464. * our hw here, which are all in 100usec. */
  4465. spec.t1_t3 = 210 * 10;
  4466. spec.t8 = 50 * 10; /* no limit for t8, use t7 instead */
  4467. spec.t9 = 50 * 10; /* no limit for t9, make it symmetric with t8 */
  4468. spec.t10 = 500 * 10;
  4469. /* This one is special and actually in units of 100ms, but zero
  4470. * based in the hw (so we need to add 100 ms). But the sw vbt
  4471. * table multiplies it with 1000 to make it in units of 100usec,
  4472. * too. */
  4473. spec.t11_t12 = (510 + 100) * 10;
  4474. DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
  4475. vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
  4476. /* Use the max of the register settings and vbt. If both are
  4477. * unset, fall back to the spec limits. */
  4478. #define assign_final(field) final->field = (max(cur.field, vbt.field) == 0 ? \
  4479. spec.field : \
  4480. max(cur.field, vbt.field))
  4481. assign_final(t1_t3);
  4482. assign_final(t8);
  4483. assign_final(t9);
  4484. assign_final(t10);
  4485. assign_final(t11_t12);
  4486. #undef assign_final
  4487. #define get_delay(field) (DIV_ROUND_UP(final->field, 10))
  4488. intel_dp->panel_power_up_delay = get_delay(t1_t3);
  4489. intel_dp->backlight_on_delay = get_delay(t8);
  4490. intel_dp->backlight_off_delay = get_delay(t9);
  4491. intel_dp->panel_power_down_delay = get_delay(t10);
  4492. intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
  4493. #undef get_delay
  4494. DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
  4495. intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
  4496. intel_dp->panel_power_cycle_delay);
  4497. DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
  4498. intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
  4499. }
  4500. static void
  4501. intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
  4502. struct intel_dp *intel_dp)
  4503. {
  4504. struct drm_i915_private *dev_priv = dev->dev_private;
  4505. u32 pp_on, pp_off, pp_div, port_sel = 0;
  4506. int div = HAS_PCH_SPLIT(dev) ? intel_pch_rawclk(dev) : intel_hrawclk(dev);
  4507. int pp_on_reg, pp_off_reg, pp_div_reg = 0, pp_ctrl_reg;
  4508. enum port port = dp_to_dig_port(intel_dp)->port;
  4509. const struct edp_power_seq *seq = &intel_dp->pps_delays;
  4510. lockdep_assert_held(&dev_priv->pps_mutex);
  4511. if (IS_BROXTON(dev)) {
  4512. /*
  4513. * TODO: BXT has 2 sets of PPS registers.
  4514. * Correct Register for Broxton need to be identified
  4515. * using VBT. hardcoding for now
  4516. */
  4517. pp_ctrl_reg = BXT_PP_CONTROL(0);
  4518. pp_on_reg = BXT_PP_ON_DELAYS(0);
  4519. pp_off_reg = BXT_PP_OFF_DELAYS(0);
  4520. } else if (HAS_PCH_SPLIT(dev)) {
  4521. pp_on_reg = PCH_PP_ON_DELAYS;
  4522. pp_off_reg = PCH_PP_OFF_DELAYS;
  4523. pp_div_reg = PCH_PP_DIVISOR;
  4524. } else {
  4525. enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
  4526. pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
  4527. pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
  4528. pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
  4529. }
  4530. /*
  4531. * And finally store the new values in the power sequencer. The
  4532. * backlight delays are set to 1 because we do manual waits on them. For
  4533. * T8, even BSpec recommends doing it. For T9, if we don't do this,
  4534. * we'll end up waiting for the backlight off delay twice: once when we
  4535. * do the manual sleep, and once when we disable the panel and wait for
  4536. * the PP_STATUS bit to become zero.
  4537. */
  4538. pp_on = (seq->t1_t3 << PANEL_POWER_UP_DELAY_SHIFT) |
  4539. (1 << PANEL_LIGHT_ON_DELAY_SHIFT);
  4540. pp_off = (1 << PANEL_LIGHT_OFF_DELAY_SHIFT) |
  4541. (seq->t10 << PANEL_POWER_DOWN_DELAY_SHIFT);
  4542. /* Compute the divisor for the pp clock, simply match the Bspec
  4543. * formula. */
  4544. if (IS_BROXTON(dev)) {
  4545. pp_div = I915_READ(pp_ctrl_reg);
  4546. pp_div &= ~BXT_POWER_CYCLE_DELAY_MASK;
  4547. pp_div |= (DIV_ROUND_UP((seq->t11_t12 + 1), 1000)
  4548. << BXT_POWER_CYCLE_DELAY_SHIFT);
  4549. } else {
  4550. pp_div = ((100 * div)/2 - 1) << PP_REFERENCE_DIVIDER_SHIFT;
  4551. pp_div |= (DIV_ROUND_UP(seq->t11_t12, 1000)
  4552. << PANEL_POWER_CYCLE_DELAY_SHIFT);
  4553. }
  4554. /* Haswell doesn't have any port selection bits for the panel
  4555. * power sequencer any more. */
  4556. if (IS_VALLEYVIEW(dev)) {
  4557. port_sel = PANEL_PORT_SELECT_VLV(port);
  4558. } else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  4559. if (port == PORT_A)
  4560. port_sel = PANEL_PORT_SELECT_DPA;
  4561. else
  4562. port_sel = PANEL_PORT_SELECT_DPD;
  4563. }
  4564. pp_on |= port_sel;
  4565. I915_WRITE(pp_on_reg, pp_on);
  4566. I915_WRITE(pp_off_reg, pp_off);
  4567. if (IS_BROXTON(dev))
  4568. I915_WRITE(pp_ctrl_reg, pp_div);
  4569. else
  4570. I915_WRITE(pp_div_reg, pp_div);
  4571. DRM_DEBUG_KMS("panel power sequencer register settings: PP_ON %#x, PP_OFF %#x, PP_DIV %#x\n",
  4572. I915_READ(pp_on_reg),
  4573. I915_READ(pp_off_reg),
  4574. IS_BROXTON(dev) ?
  4575. (I915_READ(pp_ctrl_reg) & BXT_POWER_CYCLE_DELAY_MASK) :
  4576. I915_READ(pp_div_reg));
  4577. }
  4578. /**
  4579. * intel_dp_set_drrs_state - program registers for RR switch to take effect
  4580. * @dev: DRM device
  4581. * @refresh_rate: RR to be programmed
  4582. *
  4583. * This function gets called when refresh rate (RR) has to be changed from
  4584. * one frequency to another. Switches can be between high and low RR
  4585. * supported by the panel or to any other RR based on media playback (in
  4586. * this case, RR value needs to be passed from user space).
  4587. *
  4588. * The caller of this function needs to take a lock on dev_priv->drrs.
  4589. */
  4590. static void intel_dp_set_drrs_state(struct drm_device *dev, int refresh_rate)
  4591. {
  4592. struct drm_i915_private *dev_priv = dev->dev_private;
  4593. struct intel_encoder *encoder;
  4594. struct intel_digital_port *dig_port = NULL;
  4595. struct intel_dp *intel_dp = dev_priv->drrs.dp;
  4596. struct intel_crtc_state *config = NULL;
  4597. struct intel_crtc *intel_crtc = NULL;
  4598. enum drrs_refresh_rate_type index = DRRS_HIGH_RR;
  4599. if (refresh_rate <= 0) {
  4600. DRM_DEBUG_KMS("Refresh rate should be positive non-zero.\n");
  4601. return;
  4602. }
  4603. if (intel_dp == NULL) {
  4604. DRM_DEBUG_KMS("DRRS not supported.\n");
  4605. return;
  4606. }
  4607. /*
  4608. * FIXME: This needs proper synchronization with psr state for some
  4609. * platforms that cannot have PSR and DRRS enabled at the same time.
  4610. */
  4611. dig_port = dp_to_dig_port(intel_dp);
  4612. encoder = &dig_port->base;
  4613. intel_crtc = to_intel_crtc(encoder->base.crtc);
  4614. if (!intel_crtc) {
  4615. DRM_DEBUG_KMS("DRRS: intel_crtc not initialized\n");
  4616. return;
  4617. }
  4618. config = intel_crtc->config;
  4619. if (dev_priv->drrs.type < SEAMLESS_DRRS_SUPPORT) {
  4620. DRM_DEBUG_KMS("Only Seamless DRRS supported.\n");
  4621. return;
  4622. }
  4623. if (intel_dp->attached_connector->panel.downclock_mode->vrefresh ==
  4624. refresh_rate)
  4625. index = DRRS_LOW_RR;
  4626. if (index == dev_priv->drrs.refresh_rate_type) {
  4627. DRM_DEBUG_KMS(
  4628. "DRRS requested for previously set RR...ignoring\n");
  4629. return;
  4630. }
  4631. if (!intel_crtc->active) {
  4632. DRM_DEBUG_KMS("eDP encoder disabled. CRTC not Active\n");
  4633. return;
  4634. }
  4635. if (INTEL_INFO(dev)->gen >= 8 && !IS_CHERRYVIEW(dev)) {
  4636. switch (index) {
  4637. case DRRS_HIGH_RR:
  4638. intel_dp_set_m_n(intel_crtc, M1_N1);
  4639. break;
  4640. case DRRS_LOW_RR:
  4641. intel_dp_set_m_n(intel_crtc, M2_N2);
  4642. break;
  4643. case DRRS_MAX_RR:
  4644. default:
  4645. DRM_ERROR("Unsupported refreshrate type\n");
  4646. }
  4647. } else if (INTEL_INFO(dev)->gen > 6) {
  4648. u32 reg = PIPECONF(intel_crtc->config->cpu_transcoder);
  4649. u32 val;
  4650. val = I915_READ(reg);
  4651. if (index > DRRS_HIGH_RR) {
  4652. if (IS_VALLEYVIEW(dev))
  4653. val |= PIPECONF_EDP_RR_MODE_SWITCH_VLV;
  4654. else
  4655. val |= PIPECONF_EDP_RR_MODE_SWITCH;
  4656. } else {
  4657. if (IS_VALLEYVIEW(dev))
  4658. val &= ~PIPECONF_EDP_RR_MODE_SWITCH_VLV;
  4659. else
  4660. val &= ~PIPECONF_EDP_RR_MODE_SWITCH;
  4661. }
  4662. I915_WRITE(reg, val);
  4663. }
  4664. dev_priv->drrs.refresh_rate_type = index;
  4665. DRM_DEBUG_KMS("eDP Refresh Rate set to : %dHz\n", refresh_rate);
  4666. }
  4667. /**
  4668. * intel_edp_drrs_enable - init drrs struct if supported
  4669. * @intel_dp: DP struct
  4670. *
  4671. * Initializes frontbuffer_bits and drrs.dp
  4672. */
  4673. void intel_edp_drrs_enable(struct intel_dp *intel_dp)
  4674. {
  4675. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  4676. struct drm_i915_private *dev_priv = dev->dev_private;
  4677. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  4678. struct drm_crtc *crtc = dig_port->base.base.crtc;
  4679. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4680. if (!intel_crtc->config->has_drrs) {
  4681. DRM_DEBUG_KMS("Panel doesn't support DRRS\n");
  4682. return;
  4683. }
  4684. mutex_lock(&dev_priv->drrs.mutex);
  4685. if (WARN_ON(dev_priv->drrs.dp)) {
  4686. DRM_ERROR("DRRS already enabled\n");
  4687. goto unlock;
  4688. }
  4689. dev_priv->drrs.busy_frontbuffer_bits = 0;
  4690. dev_priv->drrs.dp = intel_dp;
  4691. unlock:
  4692. mutex_unlock(&dev_priv->drrs.mutex);
  4693. }
  4694. /**
  4695. * intel_edp_drrs_disable - Disable DRRS
  4696. * @intel_dp: DP struct
  4697. *
  4698. */
  4699. void intel_edp_drrs_disable(struct intel_dp *intel_dp)
  4700. {
  4701. struct drm_device *dev = intel_dp_to_dev(intel_dp);
  4702. struct drm_i915_private *dev_priv = dev->dev_private;
  4703. struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  4704. struct drm_crtc *crtc = dig_port->base.base.crtc;
  4705. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4706. if (!intel_crtc->config->has_drrs)
  4707. return;
  4708. mutex_lock(&dev_priv->drrs.mutex);
  4709. if (!dev_priv->drrs.dp) {
  4710. mutex_unlock(&dev_priv->drrs.mutex);
  4711. return;
  4712. }
  4713. if (dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
  4714. intel_dp_set_drrs_state(dev_priv->dev,
  4715. intel_dp->attached_connector->panel.
  4716. fixed_mode->vrefresh);
  4717. dev_priv->drrs.dp = NULL;
  4718. mutex_unlock(&dev_priv->drrs.mutex);
  4719. cancel_delayed_work_sync(&dev_priv->drrs.work);
  4720. }
  4721. static void intel_edp_drrs_downclock_work(struct work_struct *work)
  4722. {
  4723. struct drm_i915_private *dev_priv =
  4724. container_of(work, typeof(*dev_priv), drrs.work.work);
  4725. struct intel_dp *intel_dp;
  4726. mutex_lock(&dev_priv->drrs.mutex);
  4727. intel_dp = dev_priv->drrs.dp;
  4728. if (!intel_dp)
  4729. goto unlock;
  4730. /*
  4731. * The delayed work can race with an invalidate hence we need to
  4732. * recheck.
  4733. */
  4734. if (dev_priv->drrs.busy_frontbuffer_bits)
  4735. goto unlock;
  4736. if (dev_priv->drrs.refresh_rate_type != DRRS_LOW_RR)
  4737. intel_dp_set_drrs_state(dev_priv->dev,
  4738. intel_dp->attached_connector->panel.
  4739. downclock_mode->vrefresh);
  4740. unlock:
  4741. mutex_unlock(&dev_priv->drrs.mutex);
  4742. }
  4743. /**
  4744. * intel_edp_drrs_invalidate - Disable Idleness DRRS
  4745. * @dev: DRM device
  4746. * @frontbuffer_bits: frontbuffer plane tracking bits
  4747. *
  4748. * This function gets called everytime rendering on the given planes start.
  4749. * Hence DRRS needs to be Upclocked, i.e. (LOW_RR -> HIGH_RR).
  4750. *
  4751. * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
  4752. */
  4753. void intel_edp_drrs_invalidate(struct drm_device *dev,
  4754. unsigned frontbuffer_bits)
  4755. {
  4756. struct drm_i915_private *dev_priv = dev->dev_private;
  4757. struct drm_crtc *crtc;
  4758. enum pipe pipe;
  4759. if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
  4760. return;
  4761. cancel_delayed_work(&dev_priv->drrs.work);
  4762. mutex_lock(&dev_priv->drrs.mutex);
  4763. if (!dev_priv->drrs.dp) {
  4764. mutex_unlock(&dev_priv->drrs.mutex);
  4765. return;
  4766. }
  4767. crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
  4768. pipe = to_intel_crtc(crtc)->pipe;
  4769. frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
  4770. dev_priv->drrs.busy_frontbuffer_bits |= frontbuffer_bits;
  4771. /* invalidate means busy screen hence upclock */
  4772. if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
  4773. intel_dp_set_drrs_state(dev_priv->dev,
  4774. dev_priv->drrs.dp->attached_connector->panel.
  4775. fixed_mode->vrefresh);
  4776. mutex_unlock(&dev_priv->drrs.mutex);
  4777. }
  4778. /**
  4779. * intel_edp_drrs_flush - Restart Idleness DRRS
  4780. * @dev: DRM device
  4781. * @frontbuffer_bits: frontbuffer plane tracking bits
  4782. *
  4783. * This function gets called every time rendering on the given planes has
  4784. * completed or flip on a crtc is completed. So DRRS should be upclocked
  4785. * (LOW_RR -> HIGH_RR). And also Idleness detection should be started again,
  4786. * if no other planes are dirty.
  4787. *
  4788. * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
  4789. */
  4790. void intel_edp_drrs_flush(struct drm_device *dev,
  4791. unsigned frontbuffer_bits)
  4792. {
  4793. struct drm_i915_private *dev_priv = dev->dev_private;
  4794. struct drm_crtc *crtc;
  4795. enum pipe pipe;
  4796. if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
  4797. return;
  4798. cancel_delayed_work(&dev_priv->drrs.work);
  4799. mutex_lock(&dev_priv->drrs.mutex);
  4800. if (!dev_priv->drrs.dp) {
  4801. mutex_unlock(&dev_priv->drrs.mutex);
  4802. return;
  4803. }
  4804. crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
  4805. pipe = to_intel_crtc(crtc)->pipe;
  4806. frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
  4807. dev_priv->drrs.busy_frontbuffer_bits &= ~frontbuffer_bits;
  4808. /* flush means busy screen hence upclock */
  4809. if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
  4810. intel_dp_set_drrs_state(dev_priv->dev,
  4811. dev_priv->drrs.dp->attached_connector->panel.
  4812. fixed_mode->vrefresh);
  4813. /*
  4814. * flush also means no more activity hence schedule downclock, if all
  4815. * other fbs are quiescent too
  4816. */
  4817. if (!dev_priv->drrs.busy_frontbuffer_bits)
  4818. schedule_delayed_work(&dev_priv->drrs.work,
  4819. msecs_to_jiffies(1000));
  4820. mutex_unlock(&dev_priv->drrs.mutex);
  4821. }
  4822. /**
  4823. * DOC: Display Refresh Rate Switching (DRRS)
  4824. *
  4825. * Display Refresh Rate Switching (DRRS) is a power conservation feature
  4826. * which enables swtching between low and high refresh rates,
  4827. * dynamically, based on the usage scenario. This feature is applicable
  4828. * for internal panels.
  4829. *
  4830. * Indication that the panel supports DRRS is given by the panel EDID, which
  4831. * would list multiple refresh rates for one resolution.
  4832. *
  4833. * DRRS is of 2 types - static and seamless.
  4834. * Static DRRS involves changing refresh rate (RR) by doing a full modeset
  4835. * (may appear as a blink on screen) and is used in dock-undock scenario.
  4836. * Seamless DRRS involves changing RR without any visual effect to the user
  4837. * and can be used during normal system usage. This is done by programming
  4838. * certain registers.
  4839. *
  4840. * Support for static/seamless DRRS may be indicated in the VBT based on
  4841. * inputs from the panel spec.
  4842. *
  4843. * DRRS saves power by switching to low RR based on usage scenarios.
  4844. *
  4845. * eDP DRRS:-
  4846. * The implementation is based on frontbuffer tracking implementation.
  4847. * When there is a disturbance on the screen triggered by user activity or a
  4848. * periodic system activity, DRRS is disabled (RR is changed to high RR).
  4849. * When there is no movement on screen, after a timeout of 1 second, a switch
  4850. * to low RR is made.
  4851. * For integration with frontbuffer tracking code,
  4852. * intel_edp_drrs_invalidate() and intel_edp_drrs_flush() are called.
  4853. *
  4854. * DRRS can be further extended to support other internal panels and also
  4855. * the scenario of video playback wherein RR is set based on the rate
  4856. * requested by userspace.
  4857. */
  4858. /**
  4859. * intel_dp_drrs_init - Init basic DRRS work and mutex.
  4860. * @intel_connector: eDP connector
  4861. * @fixed_mode: preferred mode of panel
  4862. *
  4863. * This function is called only once at driver load to initialize basic
  4864. * DRRS stuff.
  4865. *
  4866. * Returns:
  4867. * Downclock mode if panel supports it, else return NULL.
  4868. * DRRS support is determined by the presence of downclock mode (apart
  4869. * from VBT setting).
  4870. */
  4871. static struct drm_display_mode *
  4872. intel_dp_drrs_init(struct intel_connector *intel_connector,
  4873. struct drm_display_mode *fixed_mode)
  4874. {
  4875. struct drm_connector *connector = &intel_connector->base;
  4876. struct drm_device *dev = connector->dev;
  4877. struct drm_i915_private *dev_priv = dev->dev_private;
  4878. struct drm_display_mode *downclock_mode = NULL;
  4879. INIT_DELAYED_WORK(&dev_priv->drrs.work, intel_edp_drrs_downclock_work);
  4880. mutex_init(&dev_priv->drrs.mutex);
  4881. if (INTEL_INFO(dev)->gen <= 6) {
  4882. DRM_DEBUG_KMS("DRRS supported for Gen7 and above\n");
  4883. return NULL;
  4884. }
  4885. if (dev_priv->vbt.drrs_type != SEAMLESS_DRRS_SUPPORT) {
  4886. DRM_DEBUG_KMS("VBT doesn't support DRRS\n");
  4887. return NULL;
  4888. }
  4889. downclock_mode = intel_find_panel_downclock
  4890. (dev, fixed_mode, connector);
  4891. if (!downclock_mode) {
  4892. DRM_DEBUG_KMS("Downclock mode is not found. DRRS not supported\n");
  4893. return NULL;
  4894. }
  4895. dev_priv->drrs.type = dev_priv->vbt.drrs_type;
  4896. dev_priv->drrs.refresh_rate_type = DRRS_HIGH_RR;
  4897. DRM_DEBUG_KMS("seamless DRRS supported for eDP panel.\n");
  4898. return downclock_mode;
  4899. }
  4900. static bool intel_edp_init_connector(struct intel_dp *intel_dp,
  4901. struct intel_connector *intel_connector)
  4902. {
  4903. struct drm_connector *connector = &intel_connector->base;
  4904. struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  4905. struct intel_encoder *intel_encoder = &intel_dig_port->base;
  4906. struct drm_device *dev = intel_encoder->base.dev;
  4907. struct drm_i915_private *dev_priv = dev->dev_private;
  4908. struct drm_display_mode *fixed_mode = NULL;
  4909. struct drm_display_mode *downclock_mode = NULL;
  4910. bool has_dpcd;
  4911. struct drm_display_mode *scan;
  4912. struct edid *edid;
  4913. enum pipe pipe = INVALID_PIPE;
  4914. if (!is_edp(intel_dp))
  4915. return true;
  4916. pps_lock(intel_dp);
  4917. intel_edp_panel_vdd_sanitize(intel_dp);
  4918. pps_unlock(intel_dp);
  4919. /* Cache DPCD and EDID for edp. */
  4920. has_dpcd = intel_dp_get_dpcd(intel_dp);
  4921. if (has_dpcd) {
  4922. if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
  4923. dev_priv->no_aux_handshake =
  4924. intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
  4925. DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
  4926. } else {
  4927. /* if this fails, presume the device is a ghost */
  4928. DRM_INFO("failed to retrieve link info, disabling eDP\n");
  4929. return false;
  4930. }
  4931. /* We now know it's not a ghost, init power sequence regs. */
  4932. pps_lock(intel_dp);
  4933. intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
  4934. pps_unlock(intel_dp);
  4935. mutex_lock(&dev->mode_config.mutex);
  4936. edid = drm_get_edid(connector, &intel_dp->aux.ddc);
  4937. if (edid) {
  4938. if (drm_add_edid_modes(connector, edid)) {
  4939. drm_mode_connector_update_edid_property(connector,
  4940. edid);
  4941. drm_edid_to_eld(connector, edid);
  4942. } else {
  4943. kfree(edid);
  4944. edid = ERR_PTR(-EINVAL);
  4945. }
  4946. } else {
  4947. edid = ERR_PTR(-ENOENT);
  4948. }
  4949. intel_connector->edid = edid;
  4950. /* prefer fixed mode from EDID if available */
  4951. list_for_each_entry(scan, &connector->probed_modes, head) {
  4952. if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
  4953. fixed_mode = drm_mode_duplicate(dev, scan);
  4954. downclock_mode = intel_dp_drrs_init(
  4955. intel_connector, fixed_mode);
  4956. break;
  4957. }
  4958. }
  4959. /* fallback to VBT if available for eDP */
  4960. if (!fixed_mode && dev_priv->vbt.lfp_lvds_vbt_mode) {
  4961. fixed_mode = drm_mode_duplicate(dev,
  4962. dev_priv->vbt.lfp_lvds_vbt_mode);
  4963. if (fixed_mode)
  4964. fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
  4965. }
  4966. mutex_unlock(&dev->mode_config.mutex);
  4967. if (IS_VALLEYVIEW(dev)) {
  4968. intel_dp->edp_notifier.notifier_call = edp_notify_handler;
  4969. register_reboot_notifier(&intel_dp->edp_notifier);
  4970. /*
  4971. * Figure out the current pipe for the initial backlight setup.
  4972. * If the current pipe isn't valid, try the PPS pipe, and if that
  4973. * fails just assume pipe A.
  4974. */
  4975. if (IS_CHERRYVIEW(dev))
  4976. pipe = DP_PORT_TO_PIPE_CHV(intel_dp->DP);
  4977. else
  4978. pipe = PORT_TO_PIPE(intel_dp->DP);
  4979. if (pipe != PIPE_A && pipe != PIPE_B)
  4980. pipe = intel_dp->pps_pipe;
  4981. if (pipe != PIPE_A && pipe != PIPE_B)
  4982. pipe = PIPE_A;
  4983. DRM_DEBUG_KMS("using pipe %c for initial backlight setup\n",
  4984. pipe_name(pipe));
  4985. }
  4986. intel_panel_init(&intel_connector->panel, fixed_mode, downclock_mode);
  4987. intel_connector->panel.backlight.power = intel_edp_backlight_power;
  4988. intel_panel_setup_backlight(connector, pipe);
  4989. return true;
  4990. }
  4991. bool
  4992. intel_dp_init_connector(struct intel_digital_port *intel_dig_port,
  4993. struct intel_connector *intel_connector)
  4994. {
  4995. struct drm_connector *connector = &intel_connector->base;
  4996. struct intel_dp *intel_dp = &intel_dig_port->dp;
  4997. struct intel_encoder *intel_encoder = &intel_dig_port->base;
  4998. struct drm_device *dev = intel_encoder->base.dev;
  4999. struct drm_i915_private *dev_priv = dev->dev_private;
  5000. enum port port = intel_dig_port->port;
  5001. int type;
  5002. intel_dp->pps_pipe = INVALID_PIPE;
  5003. /* intel_dp vfuncs */
  5004. if (INTEL_INFO(dev)->gen >= 9)
  5005. intel_dp->get_aux_clock_divider = skl_get_aux_clock_divider;
  5006. else if (IS_VALLEYVIEW(dev))
  5007. intel_dp->get_aux_clock_divider = vlv_get_aux_clock_divider;
  5008. else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  5009. intel_dp->get_aux_clock_divider = hsw_get_aux_clock_divider;
  5010. else if (HAS_PCH_SPLIT(dev))
  5011. intel_dp->get_aux_clock_divider = ilk_get_aux_clock_divider;
  5012. else
  5013. intel_dp->get_aux_clock_divider = i9xx_get_aux_clock_divider;
  5014. if (INTEL_INFO(dev)->gen >= 9)
  5015. intel_dp->get_aux_send_ctl = skl_get_aux_send_ctl;
  5016. else
  5017. intel_dp->get_aux_send_ctl = i9xx_get_aux_send_ctl;
  5018. /* Preserve the current hw state. */
  5019. intel_dp->DP = I915_READ(intel_dp->output_reg);
  5020. intel_dp->attached_connector = intel_connector;
  5021. if (intel_dp_is_edp(dev, port))
  5022. type = DRM_MODE_CONNECTOR_eDP;
  5023. else
  5024. type = DRM_MODE_CONNECTOR_DisplayPort;
  5025. /*
  5026. * For eDP we always set the encoder type to INTEL_OUTPUT_EDP, but
  5027. * for DP the encoder type can be set by the caller to
  5028. * INTEL_OUTPUT_UNKNOWN for DDI, so don't rewrite it.
  5029. */
  5030. if (type == DRM_MODE_CONNECTOR_eDP)
  5031. intel_encoder->type = INTEL_OUTPUT_EDP;
  5032. /* eDP only on port B and/or C on vlv/chv */
  5033. if (WARN_ON(IS_VALLEYVIEW(dev) && is_edp(intel_dp) &&
  5034. port != PORT_B && port != PORT_C))
  5035. return false;
  5036. DRM_DEBUG_KMS("Adding %s connector on port %c\n",
  5037. type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
  5038. port_name(port));
  5039. drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
  5040. drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
  5041. connector->interlace_allowed = true;
  5042. connector->doublescan_allowed = 0;
  5043. INIT_DELAYED_WORK(&intel_dp->panel_vdd_work,
  5044. edp_panel_vdd_work);
  5045. intel_connector_attach_encoder(intel_connector, intel_encoder);
  5046. drm_connector_register(connector);
  5047. if (HAS_DDI(dev))
  5048. intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
  5049. else
  5050. intel_connector->get_hw_state = intel_connector_get_hw_state;
  5051. intel_connector->unregister = intel_dp_connector_unregister;
  5052. /* Set up the hotplug pin. */
  5053. switch (port) {
  5054. case PORT_A:
  5055. intel_encoder->hpd_pin = HPD_PORT_A;
  5056. break;
  5057. case PORT_B:
  5058. intel_encoder->hpd_pin = HPD_PORT_B;
  5059. if (IS_BROXTON(dev_priv) && (INTEL_REVID(dev) < BXT_REVID_B0))
  5060. intel_encoder->hpd_pin = HPD_PORT_A;
  5061. break;
  5062. case PORT_C:
  5063. intel_encoder->hpd_pin = HPD_PORT_C;
  5064. break;
  5065. case PORT_D:
  5066. intel_encoder->hpd_pin = HPD_PORT_D;
  5067. break;
  5068. case PORT_E:
  5069. intel_encoder->hpd_pin = HPD_PORT_E;
  5070. break;
  5071. default:
  5072. BUG();
  5073. }
  5074. if (is_edp(intel_dp)) {
  5075. pps_lock(intel_dp);
  5076. intel_dp_init_panel_power_timestamps(intel_dp);
  5077. if (IS_VALLEYVIEW(dev))
  5078. vlv_initial_power_sequencer_setup(intel_dp);
  5079. else
  5080. intel_dp_init_panel_power_sequencer(dev, intel_dp);
  5081. pps_unlock(intel_dp);
  5082. }
  5083. intel_dp_aux_init(intel_dp, intel_connector);
  5084. /* init MST on ports that can support it */
  5085. if (HAS_DP_MST(dev) &&
  5086. (port == PORT_B || port == PORT_C || port == PORT_D))
  5087. intel_dp_mst_encoder_init(intel_dig_port,
  5088. intel_connector->base.base.id);
  5089. if (!intel_edp_init_connector(intel_dp, intel_connector)) {
  5090. drm_dp_aux_unregister(&intel_dp->aux);
  5091. if (is_edp(intel_dp)) {
  5092. cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
  5093. /*
  5094. * vdd might still be enabled do to the delayed vdd off.
  5095. * Make sure vdd is actually turned off here.
  5096. */
  5097. pps_lock(intel_dp);
  5098. edp_panel_vdd_off_sync(intel_dp);
  5099. pps_unlock(intel_dp);
  5100. }
  5101. drm_connector_unregister(connector);
  5102. drm_connector_cleanup(connector);
  5103. return false;
  5104. }
  5105. intel_dp_add_properties(intel_dp, connector);
  5106. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  5107. * 0xd. Failure to do so will result in spurious interrupts being
  5108. * generated on the port when a cable is not attached.
  5109. */
  5110. if (IS_G4X(dev) && !IS_GM45(dev)) {
  5111. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  5112. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  5113. }
  5114. i915_debugfs_connector_add(connector);
  5115. return true;
  5116. }
  5117. bool intel_dp_init(struct drm_device *dev,
  5118. int output_reg,
  5119. enum port port)
  5120. {
  5121. struct drm_i915_private *dev_priv = dev->dev_private;
  5122. struct intel_digital_port *intel_dig_port;
  5123. struct intel_encoder *intel_encoder;
  5124. struct drm_encoder *encoder;
  5125. struct intel_connector *intel_connector;
  5126. intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
  5127. if (!intel_dig_port)
  5128. return false;
  5129. intel_connector = intel_connector_alloc();
  5130. if (!intel_connector)
  5131. goto err_connector_alloc;
  5132. intel_encoder = &intel_dig_port->base;
  5133. encoder = &intel_encoder->base;
  5134. drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
  5135. DRM_MODE_ENCODER_TMDS);
  5136. intel_encoder->compute_config = intel_dp_compute_config;
  5137. intel_encoder->disable = intel_disable_dp;
  5138. intel_encoder->get_hw_state = intel_dp_get_hw_state;
  5139. intel_encoder->get_config = intel_dp_get_config;
  5140. intel_encoder->suspend = intel_dp_encoder_suspend;
  5141. if (IS_CHERRYVIEW(dev)) {
  5142. intel_encoder->pre_pll_enable = chv_dp_pre_pll_enable;
  5143. intel_encoder->pre_enable = chv_pre_enable_dp;
  5144. intel_encoder->enable = vlv_enable_dp;
  5145. intel_encoder->post_disable = chv_post_disable_dp;
  5146. intel_encoder->post_pll_disable = chv_dp_post_pll_disable;
  5147. } else if (IS_VALLEYVIEW(dev)) {
  5148. intel_encoder->pre_pll_enable = vlv_dp_pre_pll_enable;
  5149. intel_encoder->pre_enable = vlv_pre_enable_dp;
  5150. intel_encoder->enable = vlv_enable_dp;
  5151. intel_encoder->post_disable = vlv_post_disable_dp;
  5152. } else {
  5153. intel_encoder->pre_enable = g4x_pre_enable_dp;
  5154. intel_encoder->enable = g4x_enable_dp;
  5155. if (INTEL_INFO(dev)->gen >= 5)
  5156. intel_encoder->post_disable = ilk_post_disable_dp;
  5157. }
  5158. intel_dig_port->port = port;
  5159. intel_dig_port->dp.output_reg = output_reg;
  5160. intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  5161. if (IS_CHERRYVIEW(dev)) {
  5162. if (port == PORT_D)
  5163. intel_encoder->crtc_mask = 1 << 2;
  5164. else
  5165. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  5166. } else {
  5167. intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
  5168. }
  5169. intel_encoder->cloneable = 0;
  5170. intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
  5171. dev_priv->hotplug.irq_port[port] = intel_dig_port;
  5172. if (!intel_dp_init_connector(intel_dig_port, intel_connector))
  5173. goto err_init_connector;
  5174. return true;
  5175. err_init_connector:
  5176. drm_encoder_cleanup(encoder);
  5177. kfree(intel_connector);
  5178. err_connector_alloc:
  5179. kfree(intel_dig_port);
  5180. return false;
  5181. }
  5182. void intel_dp_mst_suspend(struct drm_device *dev)
  5183. {
  5184. struct drm_i915_private *dev_priv = dev->dev_private;
  5185. int i;
  5186. /* disable MST */
  5187. for (i = 0; i < I915_MAX_PORTS; i++) {
  5188. struct intel_digital_port *intel_dig_port = dev_priv->hotplug.irq_port[i];
  5189. if (!intel_dig_port)
  5190. continue;
  5191. if (intel_dig_port->base.type == INTEL_OUTPUT_DISPLAYPORT) {
  5192. if (!intel_dig_port->dp.can_mst)
  5193. continue;
  5194. if (intel_dig_port->dp.is_mst)
  5195. drm_dp_mst_topology_mgr_suspend(&intel_dig_port->dp.mst_mgr);
  5196. }
  5197. }
  5198. }
  5199. void intel_dp_mst_resume(struct drm_device *dev)
  5200. {
  5201. struct drm_i915_private *dev_priv = dev->dev_private;
  5202. int i;
  5203. for (i = 0; i < I915_MAX_PORTS; i++) {
  5204. struct intel_digital_port *intel_dig_port = dev_priv->hotplug.irq_port[i];
  5205. if (!intel_dig_port)
  5206. continue;
  5207. if (intel_dig_port->base.type == INTEL_OUTPUT_DISPLAYPORT) {
  5208. int ret;
  5209. if (!intel_dig_port->dp.can_mst)
  5210. continue;
  5211. ret = drm_dp_mst_topology_mgr_resume(&intel_dig_port->dp.mst_mgr);
  5212. if (ret != 0) {
  5213. intel_dp_check_mst_status(&intel_dig_port->dp);
  5214. }
  5215. }
  5216. }
  5217. }