intel_display.c 403 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613
  1. /*
  2. * Copyright © 2006-2007 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
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. /* Primary plane formats supported by all gen */
  47. #define COMMON_PRIMARY_FORMATS \
  48. DRM_FORMAT_C8, \
  49. DRM_FORMAT_RGB565, \
  50. DRM_FORMAT_XRGB8888, \
  51. DRM_FORMAT_ARGB8888
  52. /* Primary plane formats for gen <= 3 */
  53. static const uint32_t intel_primary_formats_gen2[] = {
  54. COMMON_PRIMARY_FORMATS,
  55. DRM_FORMAT_XRGB1555,
  56. DRM_FORMAT_ARGB1555,
  57. };
  58. /* Primary plane formats for gen >= 4 */
  59. static const uint32_t intel_primary_formats_gen4[] = {
  60. COMMON_PRIMARY_FORMATS, \
  61. DRM_FORMAT_XBGR8888,
  62. DRM_FORMAT_ABGR8888,
  63. DRM_FORMAT_XRGB2101010,
  64. DRM_FORMAT_ARGB2101010,
  65. DRM_FORMAT_XBGR2101010,
  66. DRM_FORMAT_ABGR2101010,
  67. };
  68. /* Cursor formats */
  69. static const uint32_t intel_cursor_formats[] = {
  70. DRM_FORMAT_ARGB8888,
  71. };
  72. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  73. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  74. struct intel_crtc_state *pipe_config);
  75. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  76. struct intel_crtc_state *pipe_config);
  77. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  78. int x, int y, struct drm_framebuffer *old_fb,
  79. struct drm_atomic_state *state);
  80. static int intel_framebuffer_init(struct drm_device *dev,
  81. struct intel_framebuffer *ifb,
  82. struct drm_mode_fb_cmd2 *mode_cmd,
  83. struct drm_i915_gem_object *obj);
  84. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  85. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  86. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  87. struct intel_link_m_n *m_n,
  88. struct intel_link_m_n *m2_n2);
  89. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  90. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  91. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  92. static void vlv_prepare_pll(struct intel_crtc *crtc,
  93. const struct intel_crtc_state *pipe_config);
  94. static void chv_prepare_pll(struct intel_crtc *crtc,
  95. const struct intel_crtc_state *pipe_config);
  96. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  97. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  98. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  99. {
  100. if (!connector->mst_port)
  101. return connector->encoder;
  102. else
  103. return &connector->mst_port->mst_encoders[pipe]->base;
  104. }
  105. typedef struct {
  106. int min, max;
  107. } intel_range_t;
  108. typedef struct {
  109. int dot_limit;
  110. int p2_slow, p2_fast;
  111. } intel_p2_t;
  112. typedef struct intel_limit intel_limit_t;
  113. struct intel_limit {
  114. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  115. intel_p2_t p2;
  116. };
  117. int
  118. intel_pch_rawclk(struct drm_device *dev)
  119. {
  120. struct drm_i915_private *dev_priv = dev->dev_private;
  121. WARN_ON(!HAS_PCH_SPLIT(dev));
  122. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  123. }
  124. static inline u32 /* units of 100MHz */
  125. intel_fdi_link_freq(struct drm_device *dev)
  126. {
  127. if (IS_GEN5(dev)) {
  128. struct drm_i915_private *dev_priv = dev->dev_private;
  129. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  130. } else
  131. return 27;
  132. }
  133. static const intel_limit_t intel_limits_i8xx_dac = {
  134. .dot = { .min = 25000, .max = 350000 },
  135. .vco = { .min = 908000, .max = 1512000 },
  136. .n = { .min = 2, .max = 16 },
  137. .m = { .min = 96, .max = 140 },
  138. .m1 = { .min = 18, .max = 26 },
  139. .m2 = { .min = 6, .max = 16 },
  140. .p = { .min = 4, .max = 128 },
  141. .p1 = { .min = 2, .max = 33 },
  142. .p2 = { .dot_limit = 165000,
  143. .p2_slow = 4, .p2_fast = 2 },
  144. };
  145. static const intel_limit_t intel_limits_i8xx_dvo = {
  146. .dot = { .min = 25000, .max = 350000 },
  147. .vco = { .min = 908000, .max = 1512000 },
  148. .n = { .min = 2, .max = 16 },
  149. .m = { .min = 96, .max = 140 },
  150. .m1 = { .min = 18, .max = 26 },
  151. .m2 = { .min = 6, .max = 16 },
  152. .p = { .min = 4, .max = 128 },
  153. .p1 = { .min = 2, .max = 33 },
  154. .p2 = { .dot_limit = 165000,
  155. .p2_slow = 4, .p2_fast = 4 },
  156. };
  157. static const intel_limit_t intel_limits_i8xx_lvds = {
  158. .dot = { .min = 25000, .max = 350000 },
  159. .vco = { .min = 908000, .max = 1512000 },
  160. .n = { .min = 2, .max = 16 },
  161. .m = { .min = 96, .max = 140 },
  162. .m1 = { .min = 18, .max = 26 },
  163. .m2 = { .min = 6, .max = 16 },
  164. .p = { .min = 4, .max = 128 },
  165. .p1 = { .min = 1, .max = 6 },
  166. .p2 = { .dot_limit = 165000,
  167. .p2_slow = 14, .p2_fast = 7 },
  168. };
  169. static const intel_limit_t intel_limits_i9xx_sdvo = {
  170. .dot = { .min = 20000, .max = 400000 },
  171. .vco = { .min = 1400000, .max = 2800000 },
  172. .n = { .min = 1, .max = 6 },
  173. .m = { .min = 70, .max = 120 },
  174. .m1 = { .min = 8, .max = 18 },
  175. .m2 = { .min = 3, .max = 7 },
  176. .p = { .min = 5, .max = 80 },
  177. .p1 = { .min = 1, .max = 8 },
  178. .p2 = { .dot_limit = 200000,
  179. .p2_slow = 10, .p2_fast = 5 },
  180. };
  181. static const intel_limit_t intel_limits_i9xx_lvds = {
  182. .dot = { .min = 20000, .max = 400000 },
  183. .vco = { .min = 1400000, .max = 2800000 },
  184. .n = { .min = 1, .max = 6 },
  185. .m = { .min = 70, .max = 120 },
  186. .m1 = { .min = 8, .max = 18 },
  187. .m2 = { .min = 3, .max = 7 },
  188. .p = { .min = 7, .max = 98 },
  189. .p1 = { .min = 1, .max = 8 },
  190. .p2 = { .dot_limit = 112000,
  191. .p2_slow = 14, .p2_fast = 7 },
  192. };
  193. static const intel_limit_t intel_limits_g4x_sdvo = {
  194. .dot = { .min = 25000, .max = 270000 },
  195. .vco = { .min = 1750000, .max = 3500000},
  196. .n = { .min = 1, .max = 4 },
  197. .m = { .min = 104, .max = 138 },
  198. .m1 = { .min = 17, .max = 23 },
  199. .m2 = { .min = 5, .max = 11 },
  200. .p = { .min = 10, .max = 30 },
  201. .p1 = { .min = 1, .max = 3},
  202. .p2 = { .dot_limit = 270000,
  203. .p2_slow = 10,
  204. .p2_fast = 10
  205. },
  206. };
  207. static const intel_limit_t intel_limits_g4x_hdmi = {
  208. .dot = { .min = 22000, .max = 400000 },
  209. .vco = { .min = 1750000, .max = 3500000},
  210. .n = { .min = 1, .max = 4 },
  211. .m = { .min = 104, .max = 138 },
  212. .m1 = { .min = 16, .max = 23 },
  213. .m2 = { .min = 5, .max = 11 },
  214. .p = { .min = 5, .max = 80 },
  215. .p1 = { .min = 1, .max = 8},
  216. .p2 = { .dot_limit = 165000,
  217. .p2_slow = 10, .p2_fast = 5 },
  218. };
  219. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  220. .dot = { .min = 20000, .max = 115000 },
  221. .vco = { .min = 1750000, .max = 3500000 },
  222. .n = { .min = 1, .max = 3 },
  223. .m = { .min = 104, .max = 138 },
  224. .m1 = { .min = 17, .max = 23 },
  225. .m2 = { .min = 5, .max = 11 },
  226. .p = { .min = 28, .max = 112 },
  227. .p1 = { .min = 2, .max = 8 },
  228. .p2 = { .dot_limit = 0,
  229. .p2_slow = 14, .p2_fast = 14
  230. },
  231. };
  232. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  233. .dot = { .min = 80000, .max = 224000 },
  234. .vco = { .min = 1750000, .max = 3500000 },
  235. .n = { .min = 1, .max = 3 },
  236. .m = { .min = 104, .max = 138 },
  237. .m1 = { .min = 17, .max = 23 },
  238. .m2 = { .min = 5, .max = 11 },
  239. .p = { .min = 14, .max = 42 },
  240. .p1 = { .min = 2, .max = 6 },
  241. .p2 = { .dot_limit = 0,
  242. .p2_slow = 7, .p2_fast = 7
  243. },
  244. };
  245. static const intel_limit_t intel_limits_pineview_sdvo = {
  246. .dot = { .min = 20000, .max = 400000},
  247. .vco = { .min = 1700000, .max = 3500000 },
  248. /* Pineview's Ncounter is a ring counter */
  249. .n = { .min = 3, .max = 6 },
  250. .m = { .min = 2, .max = 256 },
  251. /* Pineview only has one combined m divider, which we treat as m2. */
  252. .m1 = { .min = 0, .max = 0 },
  253. .m2 = { .min = 0, .max = 254 },
  254. .p = { .min = 5, .max = 80 },
  255. .p1 = { .min = 1, .max = 8 },
  256. .p2 = { .dot_limit = 200000,
  257. .p2_slow = 10, .p2_fast = 5 },
  258. };
  259. static const intel_limit_t intel_limits_pineview_lvds = {
  260. .dot = { .min = 20000, .max = 400000 },
  261. .vco = { .min = 1700000, .max = 3500000 },
  262. .n = { .min = 3, .max = 6 },
  263. .m = { .min = 2, .max = 256 },
  264. .m1 = { .min = 0, .max = 0 },
  265. .m2 = { .min = 0, .max = 254 },
  266. .p = { .min = 7, .max = 112 },
  267. .p1 = { .min = 1, .max = 8 },
  268. .p2 = { .dot_limit = 112000,
  269. .p2_slow = 14, .p2_fast = 14 },
  270. };
  271. /* Ironlake / Sandybridge
  272. *
  273. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  274. * the range value for them is (actual_value - 2).
  275. */
  276. static const intel_limit_t intel_limits_ironlake_dac = {
  277. .dot = { .min = 25000, .max = 350000 },
  278. .vco = { .min = 1760000, .max = 3510000 },
  279. .n = { .min = 1, .max = 5 },
  280. .m = { .min = 79, .max = 127 },
  281. .m1 = { .min = 12, .max = 22 },
  282. .m2 = { .min = 5, .max = 9 },
  283. .p = { .min = 5, .max = 80 },
  284. .p1 = { .min = 1, .max = 8 },
  285. .p2 = { .dot_limit = 225000,
  286. .p2_slow = 10, .p2_fast = 5 },
  287. };
  288. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  289. .dot = { .min = 25000, .max = 350000 },
  290. .vco = { .min = 1760000, .max = 3510000 },
  291. .n = { .min = 1, .max = 3 },
  292. .m = { .min = 79, .max = 118 },
  293. .m1 = { .min = 12, .max = 22 },
  294. .m2 = { .min = 5, .max = 9 },
  295. .p = { .min = 28, .max = 112 },
  296. .p1 = { .min = 2, .max = 8 },
  297. .p2 = { .dot_limit = 225000,
  298. .p2_slow = 14, .p2_fast = 14 },
  299. };
  300. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  301. .dot = { .min = 25000, .max = 350000 },
  302. .vco = { .min = 1760000, .max = 3510000 },
  303. .n = { .min = 1, .max = 3 },
  304. .m = { .min = 79, .max = 127 },
  305. .m1 = { .min = 12, .max = 22 },
  306. .m2 = { .min = 5, .max = 9 },
  307. .p = { .min = 14, .max = 56 },
  308. .p1 = { .min = 2, .max = 8 },
  309. .p2 = { .dot_limit = 225000,
  310. .p2_slow = 7, .p2_fast = 7 },
  311. };
  312. /* LVDS 100mhz refclk limits. */
  313. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  314. .dot = { .min = 25000, .max = 350000 },
  315. .vco = { .min = 1760000, .max = 3510000 },
  316. .n = { .min = 1, .max = 2 },
  317. .m = { .min = 79, .max = 126 },
  318. .m1 = { .min = 12, .max = 22 },
  319. .m2 = { .min = 5, .max = 9 },
  320. .p = { .min = 28, .max = 112 },
  321. .p1 = { .min = 2, .max = 8 },
  322. .p2 = { .dot_limit = 225000,
  323. .p2_slow = 14, .p2_fast = 14 },
  324. };
  325. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  326. .dot = { .min = 25000, .max = 350000 },
  327. .vco = { .min = 1760000, .max = 3510000 },
  328. .n = { .min = 1, .max = 3 },
  329. .m = { .min = 79, .max = 126 },
  330. .m1 = { .min = 12, .max = 22 },
  331. .m2 = { .min = 5, .max = 9 },
  332. .p = { .min = 14, .max = 42 },
  333. .p1 = { .min = 2, .max = 6 },
  334. .p2 = { .dot_limit = 225000,
  335. .p2_slow = 7, .p2_fast = 7 },
  336. };
  337. static const intel_limit_t intel_limits_vlv = {
  338. /*
  339. * These are the data rate limits (measured in fast clocks)
  340. * since those are the strictest limits we have. The fast
  341. * clock and actual rate limits are more relaxed, so checking
  342. * them would make no difference.
  343. */
  344. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  345. .vco = { .min = 4000000, .max = 6000000 },
  346. .n = { .min = 1, .max = 7 },
  347. .m1 = { .min = 2, .max = 3 },
  348. .m2 = { .min = 11, .max = 156 },
  349. .p1 = { .min = 2, .max = 3 },
  350. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  351. };
  352. static const intel_limit_t intel_limits_chv = {
  353. /*
  354. * These are the data rate limits (measured in fast clocks)
  355. * since those are the strictest limits we have. The fast
  356. * clock and actual rate limits are more relaxed, so checking
  357. * them would make no difference.
  358. */
  359. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  360. .vco = { .min = 4800000, .max = 6480000 },
  361. .n = { .min = 1, .max = 1 },
  362. .m1 = { .min = 2, .max = 2 },
  363. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  364. .p1 = { .min = 2, .max = 4 },
  365. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  366. };
  367. static void vlv_clock(int refclk, intel_clock_t *clock)
  368. {
  369. clock->m = clock->m1 * clock->m2;
  370. clock->p = clock->p1 * clock->p2;
  371. if (WARN_ON(clock->n == 0 || clock->p == 0))
  372. return;
  373. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  374. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  375. }
  376. /**
  377. * Returns whether any output on the specified pipe is of the specified type
  378. */
  379. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  380. {
  381. struct drm_device *dev = crtc->base.dev;
  382. struct intel_encoder *encoder;
  383. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  384. if (encoder->type == type)
  385. return true;
  386. return false;
  387. }
  388. /**
  389. * Returns whether any output on the specified pipe will have the specified
  390. * type after a staged modeset is complete, i.e., the same as
  391. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  392. * encoder->crtc.
  393. */
  394. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  395. int type)
  396. {
  397. struct drm_atomic_state *state = crtc_state->base.state;
  398. struct drm_connector_state *connector_state;
  399. struct intel_encoder *encoder;
  400. int i, num_connectors = 0;
  401. for (i = 0; i < state->num_connector; i++) {
  402. if (!state->connectors[i])
  403. continue;
  404. connector_state = state->connector_states[i];
  405. if (connector_state->crtc != crtc_state->base.crtc)
  406. continue;
  407. num_connectors++;
  408. encoder = to_intel_encoder(connector_state->best_encoder);
  409. if (encoder->type == type)
  410. return true;
  411. }
  412. WARN_ON(num_connectors == 0);
  413. return false;
  414. }
  415. static const intel_limit_t *
  416. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  417. {
  418. struct drm_device *dev = crtc_state->base.crtc->dev;
  419. const intel_limit_t *limit;
  420. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  421. if (intel_is_dual_link_lvds(dev)) {
  422. if (refclk == 100000)
  423. limit = &intel_limits_ironlake_dual_lvds_100m;
  424. else
  425. limit = &intel_limits_ironlake_dual_lvds;
  426. } else {
  427. if (refclk == 100000)
  428. limit = &intel_limits_ironlake_single_lvds_100m;
  429. else
  430. limit = &intel_limits_ironlake_single_lvds;
  431. }
  432. } else
  433. limit = &intel_limits_ironlake_dac;
  434. return limit;
  435. }
  436. static const intel_limit_t *
  437. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  438. {
  439. struct drm_device *dev = crtc_state->base.crtc->dev;
  440. const intel_limit_t *limit;
  441. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  442. if (intel_is_dual_link_lvds(dev))
  443. limit = &intel_limits_g4x_dual_channel_lvds;
  444. else
  445. limit = &intel_limits_g4x_single_channel_lvds;
  446. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  447. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  448. limit = &intel_limits_g4x_hdmi;
  449. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  450. limit = &intel_limits_g4x_sdvo;
  451. } else /* The option is for other outputs */
  452. limit = &intel_limits_i9xx_sdvo;
  453. return limit;
  454. }
  455. static const intel_limit_t *
  456. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  457. {
  458. struct drm_device *dev = crtc_state->base.crtc->dev;
  459. const intel_limit_t *limit;
  460. if (HAS_PCH_SPLIT(dev))
  461. limit = intel_ironlake_limit(crtc_state, refclk);
  462. else if (IS_G4X(dev)) {
  463. limit = intel_g4x_limit(crtc_state);
  464. } else if (IS_PINEVIEW(dev)) {
  465. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  466. limit = &intel_limits_pineview_lvds;
  467. else
  468. limit = &intel_limits_pineview_sdvo;
  469. } else if (IS_CHERRYVIEW(dev)) {
  470. limit = &intel_limits_chv;
  471. } else if (IS_VALLEYVIEW(dev)) {
  472. limit = &intel_limits_vlv;
  473. } else if (!IS_GEN2(dev)) {
  474. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  475. limit = &intel_limits_i9xx_lvds;
  476. else
  477. limit = &intel_limits_i9xx_sdvo;
  478. } else {
  479. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  480. limit = &intel_limits_i8xx_lvds;
  481. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  482. limit = &intel_limits_i8xx_dvo;
  483. else
  484. limit = &intel_limits_i8xx_dac;
  485. }
  486. return limit;
  487. }
  488. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  489. static void pineview_clock(int refclk, intel_clock_t *clock)
  490. {
  491. clock->m = clock->m2 + 2;
  492. clock->p = clock->p1 * clock->p2;
  493. if (WARN_ON(clock->n == 0 || clock->p == 0))
  494. return;
  495. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  496. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  497. }
  498. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  499. {
  500. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  501. }
  502. static void i9xx_clock(int refclk, intel_clock_t *clock)
  503. {
  504. clock->m = i9xx_dpll_compute_m(clock);
  505. clock->p = clock->p1 * clock->p2;
  506. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  507. return;
  508. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  509. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  510. }
  511. static void chv_clock(int refclk, intel_clock_t *clock)
  512. {
  513. clock->m = clock->m1 * clock->m2;
  514. clock->p = clock->p1 * clock->p2;
  515. if (WARN_ON(clock->n == 0 || clock->p == 0))
  516. return;
  517. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  518. clock->n << 22);
  519. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  520. }
  521. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  522. /**
  523. * Returns whether the given set of divisors are valid for a given refclk with
  524. * the given connectors.
  525. */
  526. static bool intel_PLL_is_valid(struct drm_device *dev,
  527. const intel_limit_t *limit,
  528. const intel_clock_t *clock)
  529. {
  530. if (clock->n < limit->n.min || limit->n.max < clock->n)
  531. INTELPllInvalid("n out of range\n");
  532. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  533. INTELPllInvalid("p1 out of range\n");
  534. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  535. INTELPllInvalid("m2 out of range\n");
  536. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  537. INTELPllInvalid("m1 out of range\n");
  538. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  539. if (clock->m1 <= clock->m2)
  540. INTELPllInvalid("m1 <= m2\n");
  541. if (!IS_VALLEYVIEW(dev)) {
  542. if (clock->p < limit->p.min || limit->p.max < clock->p)
  543. INTELPllInvalid("p out of range\n");
  544. if (clock->m < limit->m.min || limit->m.max < clock->m)
  545. INTELPllInvalid("m out of range\n");
  546. }
  547. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  548. INTELPllInvalid("vco out of range\n");
  549. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  550. * connector, etc., rather than just a single range.
  551. */
  552. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  553. INTELPllInvalid("dot out of range\n");
  554. return true;
  555. }
  556. static bool
  557. i9xx_find_best_dpll(const intel_limit_t *limit,
  558. struct intel_crtc_state *crtc_state,
  559. int target, int refclk, intel_clock_t *match_clock,
  560. intel_clock_t *best_clock)
  561. {
  562. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  563. struct drm_device *dev = crtc->base.dev;
  564. intel_clock_t clock;
  565. int err = target;
  566. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  567. /*
  568. * For LVDS just rely on its current settings for dual-channel.
  569. * We haven't figured out how to reliably set up different
  570. * single/dual channel state, if we even can.
  571. */
  572. if (intel_is_dual_link_lvds(dev))
  573. clock.p2 = limit->p2.p2_fast;
  574. else
  575. clock.p2 = limit->p2.p2_slow;
  576. } else {
  577. if (target < limit->p2.dot_limit)
  578. clock.p2 = limit->p2.p2_slow;
  579. else
  580. clock.p2 = limit->p2.p2_fast;
  581. }
  582. memset(best_clock, 0, sizeof(*best_clock));
  583. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  584. clock.m1++) {
  585. for (clock.m2 = limit->m2.min;
  586. clock.m2 <= limit->m2.max; clock.m2++) {
  587. if (clock.m2 >= clock.m1)
  588. break;
  589. for (clock.n = limit->n.min;
  590. clock.n <= limit->n.max; clock.n++) {
  591. for (clock.p1 = limit->p1.min;
  592. clock.p1 <= limit->p1.max; clock.p1++) {
  593. int this_err;
  594. i9xx_clock(refclk, &clock);
  595. if (!intel_PLL_is_valid(dev, limit,
  596. &clock))
  597. continue;
  598. if (match_clock &&
  599. clock.p != match_clock->p)
  600. continue;
  601. this_err = abs(clock.dot - target);
  602. if (this_err < err) {
  603. *best_clock = clock;
  604. err = this_err;
  605. }
  606. }
  607. }
  608. }
  609. }
  610. return (err != target);
  611. }
  612. static bool
  613. pnv_find_best_dpll(const intel_limit_t *limit,
  614. struct intel_crtc_state *crtc_state,
  615. int target, int refclk, intel_clock_t *match_clock,
  616. intel_clock_t *best_clock)
  617. {
  618. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  619. struct drm_device *dev = crtc->base.dev;
  620. intel_clock_t clock;
  621. int err = target;
  622. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  623. /*
  624. * For LVDS just rely on its current settings for dual-channel.
  625. * We haven't figured out how to reliably set up different
  626. * single/dual channel state, if we even can.
  627. */
  628. if (intel_is_dual_link_lvds(dev))
  629. clock.p2 = limit->p2.p2_fast;
  630. else
  631. clock.p2 = limit->p2.p2_slow;
  632. } else {
  633. if (target < limit->p2.dot_limit)
  634. clock.p2 = limit->p2.p2_slow;
  635. else
  636. clock.p2 = limit->p2.p2_fast;
  637. }
  638. memset(best_clock, 0, sizeof(*best_clock));
  639. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  640. clock.m1++) {
  641. for (clock.m2 = limit->m2.min;
  642. clock.m2 <= limit->m2.max; clock.m2++) {
  643. for (clock.n = limit->n.min;
  644. clock.n <= limit->n.max; clock.n++) {
  645. for (clock.p1 = limit->p1.min;
  646. clock.p1 <= limit->p1.max; clock.p1++) {
  647. int this_err;
  648. pineview_clock(refclk, &clock);
  649. if (!intel_PLL_is_valid(dev, limit,
  650. &clock))
  651. continue;
  652. if (match_clock &&
  653. clock.p != match_clock->p)
  654. continue;
  655. this_err = abs(clock.dot - target);
  656. if (this_err < err) {
  657. *best_clock = clock;
  658. err = this_err;
  659. }
  660. }
  661. }
  662. }
  663. }
  664. return (err != target);
  665. }
  666. static bool
  667. g4x_find_best_dpll(const intel_limit_t *limit,
  668. struct intel_crtc_state *crtc_state,
  669. int target, int refclk, intel_clock_t *match_clock,
  670. intel_clock_t *best_clock)
  671. {
  672. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  673. struct drm_device *dev = crtc->base.dev;
  674. intel_clock_t clock;
  675. int max_n;
  676. bool found;
  677. /* approximately equals target * 0.00585 */
  678. int err_most = (target >> 8) + (target >> 9);
  679. found = false;
  680. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  681. if (intel_is_dual_link_lvds(dev))
  682. clock.p2 = limit->p2.p2_fast;
  683. else
  684. clock.p2 = limit->p2.p2_slow;
  685. } else {
  686. if (target < limit->p2.dot_limit)
  687. clock.p2 = limit->p2.p2_slow;
  688. else
  689. clock.p2 = limit->p2.p2_fast;
  690. }
  691. memset(best_clock, 0, sizeof(*best_clock));
  692. max_n = limit->n.max;
  693. /* based on hardware requirement, prefer smaller n to precision */
  694. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  695. /* based on hardware requirement, prefere larger m1,m2 */
  696. for (clock.m1 = limit->m1.max;
  697. clock.m1 >= limit->m1.min; clock.m1--) {
  698. for (clock.m2 = limit->m2.max;
  699. clock.m2 >= limit->m2.min; clock.m2--) {
  700. for (clock.p1 = limit->p1.max;
  701. clock.p1 >= limit->p1.min; clock.p1--) {
  702. int this_err;
  703. i9xx_clock(refclk, &clock);
  704. if (!intel_PLL_is_valid(dev, limit,
  705. &clock))
  706. continue;
  707. this_err = abs(clock.dot - target);
  708. if (this_err < err_most) {
  709. *best_clock = clock;
  710. err_most = this_err;
  711. max_n = clock.n;
  712. found = true;
  713. }
  714. }
  715. }
  716. }
  717. }
  718. return found;
  719. }
  720. /*
  721. * Check if the calculated PLL configuration is more optimal compared to the
  722. * best configuration and error found so far. Return the calculated error.
  723. */
  724. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  725. const intel_clock_t *calculated_clock,
  726. const intel_clock_t *best_clock,
  727. unsigned int best_error_ppm,
  728. unsigned int *error_ppm)
  729. {
  730. /*
  731. * For CHV ignore the error and consider only the P value.
  732. * Prefer a bigger P value based on HW requirements.
  733. */
  734. if (IS_CHERRYVIEW(dev)) {
  735. *error_ppm = 0;
  736. return calculated_clock->p > best_clock->p;
  737. }
  738. if (WARN_ON_ONCE(!target_freq))
  739. return false;
  740. *error_ppm = div_u64(1000000ULL *
  741. abs(target_freq - calculated_clock->dot),
  742. target_freq);
  743. /*
  744. * Prefer a better P value over a better (smaller) error if the error
  745. * is small. Ensure this preference for future configurations too by
  746. * setting the error to 0.
  747. */
  748. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  749. *error_ppm = 0;
  750. return true;
  751. }
  752. return *error_ppm + 10 < best_error_ppm;
  753. }
  754. static bool
  755. vlv_find_best_dpll(const intel_limit_t *limit,
  756. struct intel_crtc_state *crtc_state,
  757. int target, int refclk, intel_clock_t *match_clock,
  758. intel_clock_t *best_clock)
  759. {
  760. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  761. struct drm_device *dev = crtc->base.dev;
  762. intel_clock_t clock;
  763. unsigned int bestppm = 1000000;
  764. /* min update 19.2 MHz */
  765. int max_n = min(limit->n.max, refclk / 19200);
  766. bool found = false;
  767. target *= 5; /* fast clock */
  768. memset(best_clock, 0, sizeof(*best_clock));
  769. /* based on hardware requirement, prefer smaller n to precision */
  770. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  771. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  772. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  773. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  774. clock.p = clock.p1 * clock.p2;
  775. /* based on hardware requirement, prefer bigger m1,m2 values */
  776. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  777. unsigned int ppm;
  778. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  779. refclk * clock.m1);
  780. vlv_clock(refclk, &clock);
  781. if (!intel_PLL_is_valid(dev, limit,
  782. &clock))
  783. continue;
  784. if (!vlv_PLL_is_optimal(dev, target,
  785. &clock,
  786. best_clock,
  787. bestppm, &ppm))
  788. continue;
  789. *best_clock = clock;
  790. bestppm = ppm;
  791. found = true;
  792. }
  793. }
  794. }
  795. }
  796. return found;
  797. }
  798. static bool
  799. chv_find_best_dpll(const intel_limit_t *limit,
  800. struct intel_crtc_state *crtc_state,
  801. int target, int refclk, intel_clock_t *match_clock,
  802. intel_clock_t *best_clock)
  803. {
  804. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  805. struct drm_device *dev = crtc->base.dev;
  806. unsigned int best_error_ppm;
  807. intel_clock_t clock;
  808. uint64_t m2;
  809. int found = false;
  810. memset(best_clock, 0, sizeof(*best_clock));
  811. best_error_ppm = 1000000;
  812. /*
  813. * Based on hardware doc, the n always set to 1, and m1 always
  814. * set to 2. If requires to support 200Mhz refclk, we need to
  815. * revisit this because n may not 1 anymore.
  816. */
  817. clock.n = 1, clock.m1 = 2;
  818. target *= 5; /* fast clock */
  819. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  820. for (clock.p2 = limit->p2.p2_fast;
  821. clock.p2 >= limit->p2.p2_slow;
  822. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  823. unsigned int error_ppm;
  824. clock.p = clock.p1 * clock.p2;
  825. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  826. clock.n) << 22, refclk * clock.m1);
  827. if (m2 > INT_MAX/clock.m1)
  828. continue;
  829. clock.m2 = m2;
  830. chv_clock(refclk, &clock);
  831. if (!intel_PLL_is_valid(dev, limit, &clock))
  832. continue;
  833. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  834. best_error_ppm, &error_ppm))
  835. continue;
  836. *best_clock = clock;
  837. best_error_ppm = error_ppm;
  838. found = true;
  839. }
  840. }
  841. return found;
  842. }
  843. bool intel_crtc_active(struct drm_crtc *crtc)
  844. {
  845. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  846. /* Be paranoid as we can arrive here with only partial
  847. * state retrieved from the hardware during setup.
  848. *
  849. * We can ditch the adjusted_mode.crtc_clock check as soon
  850. * as Haswell has gained clock readout/fastboot support.
  851. *
  852. * We can ditch the crtc->primary->fb check as soon as we can
  853. * properly reconstruct framebuffers.
  854. *
  855. * FIXME: The intel_crtc->active here should be switched to
  856. * crtc->state->active once we have proper CRTC states wired up
  857. * for atomic.
  858. */
  859. return intel_crtc->active && crtc->primary->state->fb &&
  860. intel_crtc->config->base.adjusted_mode.crtc_clock;
  861. }
  862. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  863. enum pipe pipe)
  864. {
  865. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  867. return intel_crtc->config->cpu_transcoder;
  868. }
  869. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  870. {
  871. struct drm_i915_private *dev_priv = dev->dev_private;
  872. u32 reg = PIPEDSL(pipe);
  873. u32 line1, line2;
  874. u32 line_mask;
  875. if (IS_GEN2(dev))
  876. line_mask = DSL_LINEMASK_GEN2;
  877. else
  878. line_mask = DSL_LINEMASK_GEN3;
  879. line1 = I915_READ(reg) & line_mask;
  880. mdelay(5);
  881. line2 = I915_READ(reg) & line_mask;
  882. return line1 == line2;
  883. }
  884. /*
  885. * intel_wait_for_pipe_off - wait for pipe to turn off
  886. * @crtc: crtc whose pipe to wait for
  887. *
  888. * After disabling a pipe, we can't wait for vblank in the usual way,
  889. * spinning on the vblank interrupt status bit, since we won't actually
  890. * see an interrupt when the pipe is disabled.
  891. *
  892. * On Gen4 and above:
  893. * wait for the pipe register state bit to turn off
  894. *
  895. * Otherwise:
  896. * wait for the display line value to settle (it usually
  897. * ends up stopping at the start of the next frame).
  898. *
  899. */
  900. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  901. {
  902. struct drm_device *dev = crtc->base.dev;
  903. struct drm_i915_private *dev_priv = dev->dev_private;
  904. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  905. enum pipe pipe = crtc->pipe;
  906. if (INTEL_INFO(dev)->gen >= 4) {
  907. int reg = PIPECONF(cpu_transcoder);
  908. /* Wait for the Pipe State to go off */
  909. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  910. 100))
  911. WARN(1, "pipe_off wait timed out\n");
  912. } else {
  913. /* Wait for the display line to settle */
  914. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  915. WARN(1, "pipe_off wait timed out\n");
  916. }
  917. }
  918. /*
  919. * ibx_digital_port_connected - is the specified port connected?
  920. * @dev_priv: i915 private structure
  921. * @port: the port to test
  922. *
  923. * Returns true if @port is connected, false otherwise.
  924. */
  925. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  926. struct intel_digital_port *port)
  927. {
  928. u32 bit;
  929. if (HAS_PCH_IBX(dev_priv->dev)) {
  930. switch (port->port) {
  931. case PORT_B:
  932. bit = SDE_PORTB_HOTPLUG;
  933. break;
  934. case PORT_C:
  935. bit = SDE_PORTC_HOTPLUG;
  936. break;
  937. case PORT_D:
  938. bit = SDE_PORTD_HOTPLUG;
  939. break;
  940. default:
  941. return true;
  942. }
  943. } else {
  944. switch (port->port) {
  945. case PORT_B:
  946. bit = SDE_PORTB_HOTPLUG_CPT;
  947. break;
  948. case PORT_C:
  949. bit = SDE_PORTC_HOTPLUG_CPT;
  950. break;
  951. case PORT_D:
  952. bit = SDE_PORTD_HOTPLUG_CPT;
  953. break;
  954. default:
  955. return true;
  956. }
  957. }
  958. return I915_READ(SDEISR) & bit;
  959. }
  960. static const char *state_string(bool enabled)
  961. {
  962. return enabled ? "on" : "off";
  963. }
  964. /* Only for pre-ILK configs */
  965. void assert_pll(struct drm_i915_private *dev_priv,
  966. enum pipe pipe, bool state)
  967. {
  968. int reg;
  969. u32 val;
  970. bool cur_state;
  971. reg = DPLL(pipe);
  972. val = I915_READ(reg);
  973. cur_state = !!(val & DPLL_VCO_ENABLE);
  974. I915_STATE_WARN(cur_state != state,
  975. "PLL state assertion failure (expected %s, current %s)\n",
  976. state_string(state), state_string(cur_state));
  977. }
  978. /* XXX: the dsi pll is shared between MIPI DSI ports */
  979. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  980. {
  981. u32 val;
  982. bool cur_state;
  983. mutex_lock(&dev_priv->dpio_lock);
  984. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  985. mutex_unlock(&dev_priv->dpio_lock);
  986. cur_state = val & DSI_PLL_VCO_EN;
  987. I915_STATE_WARN(cur_state != state,
  988. "DSI PLL state assertion failure (expected %s, current %s)\n",
  989. state_string(state), state_string(cur_state));
  990. }
  991. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  992. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  993. struct intel_shared_dpll *
  994. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  995. {
  996. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  997. if (crtc->config->shared_dpll < 0)
  998. return NULL;
  999. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1000. }
  1001. /* For ILK+ */
  1002. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1003. struct intel_shared_dpll *pll,
  1004. bool state)
  1005. {
  1006. bool cur_state;
  1007. struct intel_dpll_hw_state hw_state;
  1008. if (WARN (!pll,
  1009. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1010. return;
  1011. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1012. I915_STATE_WARN(cur_state != state,
  1013. "%s assertion failure (expected %s, current %s)\n",
  1014. pll->name, state_string(state), state_string(cur_state));
  1015. }
  1016. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1017. enum pipe pipe, bool state)
  1018. {
  1019. int reg;
  1020. u32 val;
  1021. bool cur_state;
  1022. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1023. pipe);
  1024. if (HAS_DDI(dev_priv->dev)) {
  1025. /* DDI does not have a specific FDI_TX register */
  1026. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1027. val = I915_READ(reg);
  1028. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1029. } else {
  1030. reg = FDI_TX_CTL(pipe);
  1031. val = I915_READ(reg);
  1032. cur_state = !!(val & FDI_TX_ENABLE);
  1033. }
  1034. I915_STATE_WARN(cur_state != state,
  1035. "FDI TX state assertion failure (expected %s, current %s)\n",
  1036. state_string(state), state_string(cur_state));
  1037. }
  1038. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1039. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1040. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1041. enum pipe pipe, bool state)
  1042. {
  1043. int reg;
  1044. u32 val;
  1045. bool cur_state;
  1046. reg = FDI_RX_CTL(pipe);
  1047. val = I915_READ(reg);
  1048. cur_state = !!(val & FDI_RX_ENABLE);
  1049. I915_STATE_WARN(cur_state != state,
  1050. "FDI RX state assertion failure (expected %s, current %s)\n",
  1051. state_string(state), state_string(cur_state));
  1052. }
  1053. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1054. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1055. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe)
  1057. {
  1058. int reg;
  1059. u32 val;
  1060. /* ILK FDI PLL is always enabled */
  1061. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1062. return;
  1063. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1064. if (HAS_DDI(dev_priv->dev))
  1065. return;
  1066. reg = FDI_TX_CTL(pipe);
  1067. val = I915_READ(reg);
  1068. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1069. }
  1070. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1071. enum pipe pipe, bool state)
  1072. {
  1073. int reg;
  1074. u32 val;
  1075. bool cur_state;
  1076. reg = FDI_RX_CTL(pipe);
  1077. val = I915_READ(reg);
  1078. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1079. I915_STATE_WARN(cur_state != state,
  1080. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1081. state_string(state), state_string(cur_state));
  1082. }
  1083. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1084. enum pipe pipe)
  1085. {
  1086. struct drm_device *dev = dev_priv->dev;
  1087. int pp_reg;
  1088. u32 val;
  1089. enum pipe panel_pipe = PIPE_A;
  1090. bool locked = true;
  1091. if (WARN_ON(HAS_DDI(dev)))
  1092. return;
  1093. if (HAS_PCH_SPLIT(dev)) {
  1094. u32 port_sel;
  1095. pp_reg = PCH_PP_CONTROL;
  1096. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1097. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1098. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1099. panel_pipe = PIPE_B;
  1100. /* XXX: else fix for eDP */
  1101. } else if (IS_VALLEYVIEW(dev)) {
  1102. /* presumably write lock depends on pipe, not port select */
  1103. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1104. panel_pipe = pipe;
  1105. } else {
  1106. pp_reg = PP_CONTROL;
  1107. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1108. panel_pipe = PIPE_B;
  1109. }
  1110. val = I915_READ(pp_reg);
  1111. if (!(val & PANEL_POWER_ON) ||
  1112. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1113. locked = false;
  1114. I915_STATE_WARN(panel_pipe == pipe && locked,
  1115. "panel assertion failure, pipe %c regs locked\n",
  1116. pipe_name(pipe));
  1117. }
  1118. static void assert_cursor(struct drm_i915_private *dev_priv,
  1119. enum pipe pipe, bool state)
  1120. {
  1121. struct drm_device *dev = dev_priv->dev;
  1122. bool cur_state;
  1123. if (IS_845G(dev) || IS_I865G(dev))
  1124. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1125. else
  1126. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1127. I915_STATE_WARN(cur_state != state,
  1128. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1129. pipe_name(pipe), state_string(state), state_string(cur_state));
  1130. }
  1131. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1132. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1133. void assert_pipe(struct drm_i915_private *dev_priv,
  1134. enum pipe pipe, bool state)
  1135. {
  1136. int reg;
  1137. u32 val;
  1138. bool cur_state;
  1139. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1140. pipe);
  1141. /* if we need the pipe quirk it must be always on */
  1142. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1143. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1144. state = true;
  1145. if (!intel_display_power_is_enabled(dev_priv,
  1146. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1147. cur_state = false;
  1148. } else {
  1149. reg = PIPECONF(cpu_transcoder);
  1150. val = I915_READ(reg);
  1151. cur_state = !!(val & PIPECONF_ENABLE);
  1152. }
  1153. I915_STATE_WARN(cur_state != state,
  1154. "pipe %c assertion failure (expected %s, current %s)\n",
  1155. pipe_name(pipe), state_string(state), state_string(cur_state));
  1156. }
  1157. static void assert_plane(struct drm_i915_private *dev_priv,
  1158. enum plane plane, bool state)
  1159. {
  1160. int reg;
  1161. u32 val;
  1162. bool cur_state;
  1163. reg = DSPCNTR(plane);
  1164. val = I915_READ(reg);
  1165. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1166. I915_STATE_WARN(cur_state != state,
  1167. "plane %c assertion failure (expected %s, current %s)\n",
  1168. plane_name(plane), state_string(state), state_string(cur_state));
  1169. }
  1170. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1171. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1172. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1173. enum pipe pipe)
  1174. {
  1175. struct drm_device *dev = dev_priv->dev;
  1176. int reg, i;
  1177. u32 val;
  1178. int cur_pipe;
  1179. /* Primary planes are fixed to pipes on gen4+ */
  1180. if (INTEL_INFO(dev)->gen >= 4) {
  1181. reg = DSPCNTR(pipe);
  1182. val = I915_READ(reg);
  1183. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1184. "plane %c assertion failure, should be disabled but not\n",
  1185. plane_name(pipe));
  1186. return;
  1187. }
  1188. /* Need to check both planes against the pipe */
  1189. for_each_pipe(dev_priv, i) {
  1190. reg = DSPCNTR(i);
  1191. val = I915_READ(reg);
  1192. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1193. DISPPLANE_SEL_PIPE_SHIFT;
  1194. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1195. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1196. plane_name(i), pipe_name(pipe));
  1197. }
  1198. }
  1199. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1200. enum pipe pipe)
  1201. {
  1202. struct drm_device *dev = dev_priv->dev;
  1203. int reg, sprite;
  1204. u32 val;
  1205. if (INTEL_INFO(dev)->gen >= 9) {
  1206. for_each_sprite(dev_priv, pipe, sprite) {
  1207. val = I915_READ(PLANE_CTL(pipe, sprite));
  1208. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1209. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1210. sprite, pipe_name(pipe));
  1211. }
  1212. } else if (IS_VALLEYVIEW(dev)) {
  1213. for_each_sprite(dev_priv, pipe, sprite) {
  1214. reg = SPCNTR(pipe, sprite);
  1215. val = I915_READ(reg);
  1216. I915_STATE_WARN(val & SP_ENABLE,
  1217. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1218. sprite_name(pipe, sprite), pipe_name(pipe));
  1219. }
  1220. } else if (INTEL_INFO(dev)->gen >= 7) {
  1221. reg = SPRCTL(pipe);
  1222. val = I915_READ(reg);
  1223. I915_STATE_WARN(val & SPRITE_ENABLE,
  1224. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1225. plane_name(pipe), pipe_name(pipe));
  1226. } else if (INTEL_INFO(dev)->gen >= 5) {
  1227. reg = DVSCNTR(pipe);
  1228. val = I915_READ(reg);
  1229. I915_STATE_WARN(val & DVS_ENABLE,
  1230. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1231. plane_name(pipe), pipe_name(pipe));
  1232. }
  1233. }
  1234. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1235. {
  1236. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1237. drm_crtc_vblank_put(crtc);
  1238. }
  1239. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1240. {
  1241. u32 val;
  1242. bool enabled;
  1243. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1244. val = I915_READ(PCH_DREF_CONTROL);
  1245. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1246. DREF_SUPERSPREAD_SOURCE_MASK));
  1247. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1248. }
  1249. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe)
  1251. {
  1252. int reg;
  1253. u32 val;
  1254. bool enabled;
  1255. reg = PCH_TRANSCONF(pipe);
  1256. val = I915_READ(reg);
  1257. enabled = !!(val & TRANS_ENABLE);
  1258. I915_STATE_WARN(enabled,
  1259. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1260. pipe_name(pipe));
  1261. }
  1262. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1263. enum pipe pipe, u32 port_sel, u32 val)
  1264. {
  1265. if ((val & DP_PORT_EN) == 0)
  1266. return false;
  1267. if (HAS_PCH_CPT(dev_priv->dev)) {
  1268. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1269. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1270. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1271. return false;
  1272. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1273. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1274. return false;
  1275. } else {
  1276. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1277. return false;
  1278. }
  1279. return true;
  1280. }
  1281. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1282. enum pipe pipe, u32 val)
  1283. {
  1284. if ((val & SDVO_ENABLE) == 0)
  1285. return false;
  1286. if (HAS_PCH_CPT(dev_priv->dev)) {
  1287. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1288. return false;
  1289. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1290. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1291. return false;
  1292. } else {
  1293. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1294. return false;
  1295. }
  1296. return true;
  1297. }
  1298. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1299. enum pipe pipe, u32 val)
  1300. {
  1301. if ((val & LVDS_PORT_EN) == 0)
  1302. return false;
  1303. if (HAS_PCH_CPT(dev_priv->dev)) {
  1304. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1305. return false;
  1306. } else {
  1307. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1308. return false;
  1309. }
  1310. return true;
  1311. }
  1312. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1313. enum pipe pipe, u32 val)
  1314. {
  1315. if ((val & ADPA_DAC_ENABLE) == 0)
  1316. return false;
  1317. if (HAS_PCH_CPT(dev_priv->dev)) {
  1318. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1319. return false;
  1320. } else {
  1321. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1322. return false;
  1323. }
  1324. return true;
  1325. }
  1326. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1327. enum pipe pipe, int reg, u32 port_sel)
  1328. {
  1329. u32 val = I915_READ(reg);
  1330. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1331. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1332. reg, pipe_name(pipe));
  1333. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1334. && (val & DP_PIPEB_SELECT),
  1335. "IBX PCH dp port still using transcoder B\n");
  1336. }
  1337. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1338. enum pipe pipe, int reg)
  1339. {
  1340. u32 val = I915_READ(reg);
  1341. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1342. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1343. reg, pipe_name(pipe));
  1344. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1345. && (val & SDVO_PIPE_B_SELECT),
  1346. "IBX PCH hdmi port still using transcoder B\n");
  1347. }
  1348. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1349. enum pipe pipe)
  1350. {
  1351. int reg;
  1352. u32 val;
  1353. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1354. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1355. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1356. reg = PCH_ADPA;
  1357. val = I915_READ(reg);
  1358. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1359. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1360. pipe_name(pipe));
  1361. reg = PCH_LVDS;
  1362. val = I915_READ(reg);
  1363. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1364. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1365. pipe_name(pipe));
  1366. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1367. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1368. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1369. }
  1370. static void intel_init_dpio(struct drm_device *dev)
  1371. {
  1372. struct drm_i915_private *dev_priv = dev->dev_private;
  1373. if (!IS_VALLEYVIEW(dev))
  1374. return;
  1375. /*
  1376. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1377. * CHV x1 PHY (DP/HDMI D)
  1378. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1379. */
  1380. if (IS_CHERRYVIEW(dev)) {
  1381. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1382. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1383. } else {
  1384. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1385. }
  1386. }
  1387. static void vlv_enable_pll(struct intel_crtc *crtc,
  1388. const struct intel_crtc_state *pipe_config)
  1389. {
  1390. struct drm_device *dev = crtc->base.dev;
  1391. struct drm_i915_private *dev_priv = dev->dev_private;
  1392. int reg = DPLL(crtc->pipe);
  1393. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1394. assert_pipe_disabled(dev_priv, crtc->pipe);
  1395. /* No really, not for ILK+ */
  1396. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1397. /* PLL is protected by panel, make sure we can write it */
  1398. if (IS_MOBILE(dev_priv->dev))
  1399. assert_panel_unlocked(dev_priv, crtc->pipe);
  1400. I915_WRITE(reg, dpll);
  1401. POSTING_READ(reg);
  1402. udelay(150);
  1403. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1404. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1405. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1406. POSTING_READ(DPLL_MD(crtc->pipe));
  1407. /* We do this three times for luck */
  1408. I915_WRITE(reg, dpll);
  1409. POSTING_READ(reg);
  1410. udelay(150); /* wait for warmup */
  1411. I915_WRITE(reg, dpll);
  1412. POSTING_READ(reg);
  1413. udelay(150); /* wait for warmup */
  1414. I915_WRITE(reg, dpll);
  1415. POSTING_READ(reg);
  1416. udelay(150); /* wait for warmup */
  1417. }
  1418. static void chv_enable_pll(struct intel_crtc *crtc,
  1419. const struct intel_crtc_state *pipe_config)
  1420. {
  1421. struct drm_device *dev = crtc->base.dev;
  1422. struct drm_i915_private *dev_priv = dev->dev_private;
  1423. int pipe = crtc->pipe;
  1424. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1425. u32 tmp;
  1426. assert_pipe_disabled(dev_priv, crtc->pipe);
  1427. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1428. mutex_lock(&dev_priv->dpio_lock);
  1429. /* Enable back the 10bit clock to display controller */
  1430. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1431. tmp |= DPIO_DCLKP_EN;
  1432. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1433. /*
  1434. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1435. */
  1436. udelay(1);
  1437. /* Enable PLL */
  1438. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1439. /* Check PLL is locked */
  1440. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1441. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1442. /* not sure when this should be written */
  1443. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1444. POSTING_READ(DPLL_MD(pipe));
  1445. mutex_unlock(&dev_priv->dpio_lock);
  1446. }
  1447. static int intel_num_dvo_pipes(struct drm_device *dev)
  1448. {
  1449. struct intel_crtc *crtc;
  1450. int count = 0;
  1451. for_each_intel_crtc(dev, crtc)
  1452. count += crtc->active &&
  1453. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1454. return count;
  1455. }
  1456. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1457. {
  1458. struct drm_device *dev = crtc->base.dev;
  1459. struct drm_i915_private *dev_priv = dev->dev_private;
  1460. int reg = DPLL(crtc->pipe);
  1461. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1462. assert_pipe_disabled(dev_priv, crtc->pipe);
  1463. /* No really, not for ILK+ */
  1464. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1465. /* PLL is protected by panel, make sure we can write it */
  1466. if (IS_MOBILE(dev) && !IS_I830(dev))
  1467. assert_panel_unlocked(dev_priv, crtc->pipe);
  1468. /* Enable DVO 2x clock on both PLLs if necessary */
  1469. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1470. /*
  1471. * It appears to be important that we don't enable this
  1472. * for the current pipe before otherwise configuring the
  1473. * PLL. No idea how this should be handled if multiple
  1474. * DVO outputs are enabled simultaneosly.
  1475. */
  1476. dpll |= DPLL_DVO_2X_MODE;
  1477. I915_WRITE(DPLL(!crtc->pipe),
  1478. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1479. }
  1480. /* Wait for the clocks to stabilize. */
  1481. POSTING_READ(reg);
  1482. udelay(150);
  1483. if (INTEL_INFO(dev)->gen >= 4) {
  1484. I915_WRITE(DPLL_MD(crtc->pipe),
  1485. crtc->config->dpll_hw_state.dpll_md);
  1486. } else {
  1487. /* The pixel multiplier can only be updated once the
  1488. * DPLL is enabled and the clocks are stable.
  1489. *
  1490. * So write it again.
  1491. */
  1492. I915_WRITE(reg, dpll);
  1493. }
  1494. /* We do this three times for luck */
  1495. I915_WRITE(reg, dpll);
  1496. POSTING_READ(reg);
  1497. udelay(150); /* wait for warmup */
  1498. I915_WRITE(reg, dpll);
  1499. POSTING_READ(reg);
  1500. udelay(150); /* wait for warmup */
  1501. I915_WRITE(reg, dpll);
  1502. POSTING_READ(reg);
  1503. udelay(150); /* wait for warmup */
  1504. }
  1505. /**
  1506. * i9xx_disable_pll - disable a PLL
  1507. * @dev_priv: i915 private structure
  1508. * @pipe: pipe PLL to disable
  1509. *
  1510. * Disable the PLL for @pipe, making sure the pipe is off first.
  1511. *
  1512. * Note! This is for pre-ILK only.
  1513. */
  1514. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1515. {
  1516. struct drm_device *dev = crtc->base.dev;
  1517. struct drm_i915_private *dev_priv = dev->dev_private;
  1518. enum pipe pipe = crtc->pipe;
  1519. /* Disable DVO 2x clock on both PLLs if necessary */
  1520. if (IS_I830(dev) &&
  1521. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1522. intel_num_dvo_pipes(dev) == 1) {
  1523. I915_WRITE(DPLL(PIPE_B),
  1524. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1525. I915_WRITE(DPLL(PIPE_A),
  1526. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1527. }
  1528. /* Don't disable pipe or pipe PLLs if needed */
  1529. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1530. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1531. return;
  1532. /* Make sure the pipe isn't still relying on us */
  1533. assert_pipe_disabled(dev_priv, pipe);
  1534. I915_WRITE(DPLL(pipe), 0);
  1535. POSTING_READ(DPLL(pipe));
  1536. }
  1537. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1538. {
  1539. u32 val = 0;
  1540. /* Make sure the pipe isn't still relying on us */
  1541. assert_pipe_disabled(dev_priv, pipe);
  1542. /*
  1543. * Leave integrated clock source and reference clock enabled for pipe B.
  1544. * The latter is needed for VGA hotplug / manual detection.
  1545. */
  1546. if (pipe == PIPE_B)
  1547. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1548. I915_WRITE(DPLL(pipe), val);
  1549. POSTING_READ(DPLL(pipe));
  1550. }
  1551. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1552. {
  1553. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1554. u32 val;
  1555. /* Make sure the pipe isn't still relying on us */
  1556. assert_pipe_disabled(dev_priv, pipe);
  1557. /* Set PLL en = 0 */
  1558. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1559. if (pipe != PIPE_A)
  1560. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1561. I915_WRITE(DPLL(pipe), val);
  1562. POSTING_READ(DPLL(pipe));
  1563. mutex_lock(&dev_priv->dpio_lock);
  1564. /* Disable 10bit clock to display controller */
  1565. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1566. val &= ~DPIO_DCLKP_EN;
  1567. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1568. /* disable left/right clock distribution */
  1569. if (pipe != PIPE_B) {
  1570. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1571. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1572. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1573. } else {
  1574. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1575. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1576. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1577. }
  1578. mutex_unlock(&dev_priv->dpio_lock);
  1579. }
  1580. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1581. struct intel_digital_port *dport)
  1582. {
  1583. u32 port_mask;
  1584. int dpll_reg;
  1585. switch (dport->port) {
  1586. case PORT_B:
  1587. port_mask = DPLL_PORTB_READY_MASK;
  1588. dpll_reg = DPLL(0);
  1589. break;
  1590. case PORT_C:
  1591. port_mask = DPLL_PORTC_READY_MASK;
  1592. dpll_reg = DPLL(0);
  1593. break;
  1594. case PORT_D:
  1595. port_mask = DPLL_PORTD_READY_MASK;
  1596. dpll_reg = DPIO_PHY_STATUS;
  1597. break;
  1598. default:
  1599. BUG();
  1600. }
  1601. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1602. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1603. port_name(dport->port), I915_READ(dpll_reg));
  1604. }
  1605. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1606. {
  1607. struct drm_device *dev = crtc->base.dev;
  1608. struct drm_i915_private *dev_priv = dev->dev_private;
  1609. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1610. if (WARN_ON(pll == NULL))
  1611. return;
  1612. WARN_ON(!pll->config.crtc_mask);
  1613. if (pll->active == 0) {
  1614. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1615. WARN_ON(pll->on);
  1616. assert_shared_dpll_disabled(dev_priv, pll);
  1617. pll->mode_set(dev_priv, pll);
  1618. }
  1619. }
  1620. /**
  1621. * intel_enable_shared_dpll - enable PCH PLL
  1622. * @dev_priv: i915 private structure
  1623. * @pipe: pipe PLL to enable
  1624. *
  1625. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1626. * drives the transcoder clock.
  1627. */
  1628. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1629. {
  1630. struct drm_device *dev = crtc->base.dev;
  1631. struct drm_i915_private *dev_priv = dev->dev_private;
  1632. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1633. if (WARN_ON(pll == NULL))
  1634. return;
  1635. if (WARN_ON(pll->config.crtc_mask == 0))
  1636. return;
  1637. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1638. pll->name, pll->active, pll->on,
  1639. crtc->base.base.id);
  1640. if (pll->active++) {
  1641. WARN_ON(!pll->on);
  1642. assert_shared_dpll_enabled(dev_priv, pll);
  1643. return;
  1644. }
  1645. WARN_ON(pll->on);
  1646. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1647. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1648. pll->enable(dev_priv, pll);
  1649. pll->on = true;
  1650. }
  1651. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1652. {
  1653. struct drm_device *dev = crtc->base.dev;
  1654. struct drm_i915_private *dev_priv = dev->dev_private;
  1655. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1656. /* PCH only available on ILK+ */
  1657. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1658. if (WARN_ON(pll == NULL))
  1659. return;
  1660. if (WARN_ON(pll->config.crtc_mask == 0))
  1661. return;
  1662. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1663. pll->name, pll->active, pll->on,
  1664. crtc->base.base.id);
  1665. if (WARN_ON(pll->active == 0)) {
  1666. assert_shared_dpll_disabled(dev_priv, pll);
  1667. return;
  1668. }
  1669. assert_shared_dpll_enabled(dev_priv, pll);
  1670. WARN_ON(!pll->on);
  1671. if (--pll->active)
  1672. return;
  1673. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1674. pll->disable(dev_priv, pll);
  1675. pll->on = false;
  1676. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1677. }
  1678. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1679. enum pipe pipe)
  1680. {
  1681. struct drm_device *dev = dev_priv->dev;
  1682. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1683. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1684. uint32_t reg, val, pipeconf_val;
  1685. /* PCH only available on ILK+ */
  1686. BUG_ON(!HAS_PCH_SPLIT(dev));
  1687. /* Make sure PCH DPLL is enabled */
  1688. assert_shared_dpll_enabled(dev_priv,
  1689. intel_crtc_to_shared_dpll(intel_crtc));
  1690. /* FDI must be feeding us bits for PCH ports */
  1691. assert_fdi_tx_enabled(dev_priv, pipe);
  1692. assert_fdi_rx_enabled(dev_priv, pipe);
  1693. if (HAS_PCH_CPT(dev)) {
  1694. /* Workaround: Set the timing override bit before enabling the
  1695. * pch transcoder. */
  1696. reg = TRANS_CHICKEN2(pipe);
  1697. val = I915_READ(reg);
  1698. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1699. I915_WRITE(reg, val);
  1700. }
  1701. reg = PCH_TRANSCONF(pipe);
  1702. val = I915_READ(reg);
  1703. pipeconf_val = I915_READ(PIPECONF(pipe));
  1704. if (HAS_PCH_IBX(dev_priv->dev)) {
  1705. /*
  1706. * make the BPC in transcoder be consistent with
  1707. * that in pipeconf reg.
  1708. */
  1709. val &= ~PIPECONF_BPC_MASK;
  1710. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1711. }
  1712. val &= ~TRANS_INTERLACE_MASK;
  1713. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1714. if (HAS_PCH_IBX(dev_priv->dev) &&
  1715. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1716. val |= TRANS_LEGACY_INTERLACED_ILK;
  1717. else
  1718. val |= TRANS_INTERLACED;
  1719. else
  1720. val |= TRANS_PROGRESSIVE;
  1721. I915_WRITE(reg, val | TRANS_ENABLE);
  1722. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1723. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1724. }
  1725. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1726. enum transcoder cpu_transcoder)
  1727. {
  1728. u32 val, pipeconf_val;
  1729. /* PCH only available on ILK+ */
  1730. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1731. /* FDI must be feeding us bits for PCH ports */
  1732. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1733. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1734. /* Workaround: set timing override bit. */
  1735. val = I915_READ(_TRANSA_CHICKEN2);
  1736. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1737. I915_WRITE(_TRANSA_CHICKEN2, val);
  1738. val = TRANS_ENABLE;
  1739. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1740. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1741. PIPECONF_INTERLACED_ILK)
  1742. val |= TRANS_INTERLACED;
  1743. else
  1744. val |= TRANS_PROGRESSIVE;
  1745. I915_WRITE(LPT_TRANSCONF, val);
  1746. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1747. DRM_ERROR("Failed to enable PCH transcoder\n");
  1748. }
  1749. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1750. enum pipe pipe)
  1751. {
  1752. struct drm_device *dev = dev_priv->dev;
  1753. uint32_t reg, val;
  1754. /* FDI relies on the transcoder */
  1755. assert_fdi_tx_disabled(dev_priv, pipe);
  1756. assert_fdi_rx_disabled(dev_priv, pipe);
  1757. /* Ports must be off as well */
  1758. assert_pch_ports_disabled(dev_priv, pipe);
  1759. reg = PCH_TRANSCONF(pipe);
  1760. val = I915_READ(reg);
  1761. val &= ~TRANS_ENABLE;
  1762. I915_WRITE(reg, val);
  1763. /* wait for PCH transcoder off, transcoder state */
  1764. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1765. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1766. if (!HAS_PCH_IBX(dev)) {
  1767. /* Workaround: Clear the timing override chicken bit again. */
  1768. reg = TRANS_CHICKEN2(pipe);
  1769. val = I915_READ(reg);
  1770. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1771. I915_WRITE(reg, val);
  1772. }
  1773. }
  1774. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1775. {
  1776. u32 val;
  1777. val = I915_READ(LPT_TRANSCONF);
  1778. val &= ~TRANS_ENABLE;
  1779. I915_WRITE(LPT_TRANSCONF, val);
  1780. /* wait for PCH transcoder off, transcoder state */
  1781. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1782. DRM_ERROR("Failed to disable PCH transcoder\n");
  1783. /* Workaround: clear timing override bit. */
  1784. val = I915_READ(_TRANSA_CHICKEN2);
  1785. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1786. I915_WRITE(_TRANSA_CHICKEN2, val);
  1787. }
  1788. /**
  1789. * intel_enable_pipe - enable a pipe, asserting requirements
  1790. * @crtc: crtc responsible for the pipe
  1791. *
  1792. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1793. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1794. */
  1795. static void intel_enable_pipe(struct intel_crtc *crtc)
  1796. {
  1797. struct drm_device *dev = crtc->base.dev;
  1798. struct drm_i915_private *dev_priv = dev->dev_private;
  1799. enum pipe pipe = crtc->pipe;
  1800. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1801. pipe);
  1802. enum pipe pch_transcoder;
  1803. int reg;
  1804. u32 val;
  1805. assert_planes_disabled(dev_priv, pipe);
  1806. assert_cursor_disabled(dev_priv, pipe);
  1807. assert_sprites_disabled(dev_priv, pipe);
  1808. if (HAS_PCH_LPT(dev_priv->dev))
  1809. pch_transcoder = TRANSCODER_A;
  1810. else
  1811. pch_transcoder = pipe;
  1812. /*
  1813. * A pipe without a PLL won't actually be able to drive bits from
  1814. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1815. * need the check.
  1816. */
  1817. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1818. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1819. assert_dsi_pll_enabled(dev_priv);
  1820. else
  1821. assert_pll_enabled(dev_priv, pipe);
  1822. else {
  1823. if (crtc->config->has_pch_encoder) {
  1824. /* if driving the PCH, we need FDI enabled */
  1825. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1826. assert_fdi_tx_pll_enabled(dev_priv,
  1827. (enum pipe) cpu_transcoder);
  1828. }
  1829. /* FIXME: assert CPU port conditions for SNB+ */
  1830. }
  1831. reg = PIPECONF(cpu_transcoder);
  1832. val = I915_READ(reg);
  1833. if (val & PIPECONF_ENABLE) {
  1834. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1835. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1836. return;
  1837. }
  1838. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1839. POSTING_READ(reg);
  1840. }
  1841. /**
  1842. * intel_disable_pipe - disable a pipe, asserting requirements
  1843. * @crtc: crtc whose pipes is to be disabled
  1844. *
  1845. * Disable the pipe of @crtc, making sure that various hardware
  1846. * specific requirements are met, if applicable, e.g. plane
  1847. * disabled, panel fitter off, etc.
  1848. *
  1849. * Will wait until the pipe has shut down before returning.
  1850. */
  1851. static void intel_disable_pipe(struct intel_crtc *crtc)
  1852. {
  1853. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1854. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1855. enum pipe pipe = crtc->pipe;
  1856. int reg;
  1857. u32 val;
  1858. /*
  1859. * Make sure planes won't keep trying to pump pixels to us,
  1860. * or we might hang the display.
  1861. */
  1862. assert_planes_disabled(dev_priv, pipe);
  1863. assert_cursor_disabled(dev_priv, pipe);
  1864. assert_sprites_disabled(dev_priv, pipe);
  1865. reg = PIPECONF(cpu_transcoder);
  1866. val = I915_READ(reg);
  1867. if ((val & PIPECONF_ENABLE) == 0)
  1868. return;
  1869. /*
  1870. * Double wide has implications for planes
  1871. * so best keep it disabled when not needed.
  1872. */
  1873. if (crtc->config->double_wide)
  1874. val &= ~PIPECONF_DOUBLE_WIDE;
  1875. /* Don't disable pipe or pipe PLLs if needed */
  1876. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1877. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1878. val &= ~PIPECONF_ENABLE;
  1879. I915_WRITE(reg, val);
  1880. if ((val & PIPECONF_ENABLE) == 0)
  1881. intel_wait_for_pipe_off(crtc);
  1882. }
  1883. /*
  1884. * Plane regs are double buffered, going from enabled->disabled needs a
  1885. * trigger in order to latch. The display address reg provides this.
  1886. */
  1887. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1888. enum plane plane)
  1889. {
  1890. struct drm_device *dev = dev_priv->dev;
  1891. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1892. I915_WRITE(reg, I915_READ(reg));
  1893. POSTING_READ(reg);
  1894. }
  1895. /**
  1896. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1897. * @plane: plane to be enabled
  1898. * @crtc: crtc for the plane
  1899. *
  1900. * Enable @plane on @crtc, making sure that the pipe is running first.
  1901. */
  1902. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1903. struct drm_crtc *crtc)
  1904. {
  1905. struct drm_device *dev = plane->dev;
  1906. struct drm_i915_private *dev_priv = dev->dev_private;
  1907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1908. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1909. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1910. if (intel_crtc->primary_enabled)
  1911. return;
  1912. intel_crtc->primary_enabled = true;
  1913. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1914. crtc->x, crtc->y);
  1915. /*
  1916. * BDW signals flip done immediately if the plane
  1917. * is disabled, even if the plane enable is already
  1918. * armed to occur at the next vblank :(
  1919. */
  1920. if (IS_BROADWELL(dev))
  1921. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1922. }
  1923. /**
  1924. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1925. * @plane: plane to be disabled
  1926. * @crtc: crtc for the plane
  1927. *
  1928. * Disable @plane on @crtc, making sure that the pipe is running first.
  1929. */
  1930. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1931. struct drm_crtc *crtc)
  1932. {
  1933. struct drm_device *dev = plane->dev;
  1934. struct drm_i915_private *dev_priv = dev->dev_private;
  1935. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1936. if (WARN_ON(!intel_crtc->active))
  1937. return;
  1938. if (!intel_crtc->primary_enabled)
  1939. return;
  1940. intel_crtc->primary_enabled = false;
  1941. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1942. crtc->x, crtc->y);
  1943. }
  1944. static bool need_vtd_wa(struct drm_device *dev)
  1945. {
  1946. #ifdef CONFIG_INTEL_IOMMU
  1947. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1948. return true;
  1949. #endif
  1950. return false;
  1951. }
  1952. unsigned int
  1953. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1954. uint64_t fb_format_modifier)
  1955. {
  1956. unsigned int tile_height;
  1957. uint32_t pixel_bytes;
  1958. switch (fb_format_modifier) {
  1959. case DRM_FORMAT_MOD_NONE:
  1960. tile_height = 1;
  1961. break;
  1962. case I915_FORMAT_MOD_X_TILED:
  1963. tile_height = IS_GEN2(dev) ? 16 : 8;
  1964. break;
  1965. case I915_FORMAT_MOD_Y_TILED:
  1966. tile_height = 32;
  1967. break;
  1968. case I915_FORMAT_MOD_Yf_TILED:
  1969. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1970. switch (pixel_bytes) {
  1971. default:
  1972. case 1:
  1973. tile_height = 64;
  1974. break;
  1975. case 2:
  1976. case 4:
  1977. tile_height = 32;
  1978. break;
  1979. case 8:
  1980. tile_height = 16;
  1981. break;
  1982. case 16:
  1983. WARN_ONCE(1,
  1984. "128-bit pixels are not supported for display!");
  1985. tile_height = 16;
  1986. break;
  1987. }
  1988. break;
  1989. default:
  1990. MISSING_CASE(fb_format_modifier);
  1991. tile_height = 1;
  1992. break;
  1993. }
  1994. return tile_height;
  1995. }
  1996. unsigned int
  1997. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1998. uint32_t pixel_format, uint64_t fb_format_modifier)
  1999. {
  2000. return ALIGN(height, intel_tile_height(dev, pixel_format,
  2001. fb_format_modifier));
  2002. }
  2003. static int
  2004. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  2005. const struct drm_plane_state *plane_state)
  2006. {
  2007. struct intel_rotation_info *info = &view->rotation_info;
  2008. *view = i915_ggtt_view_normal;
  2009. if (!plane_state)
  2010. return 0;
  2011. if (!intel_rotation_90_or_270(plane_state->rotation))
  2012. return 0;
  2013. *view = i915_ggtt_view_rotated;
  2014. info->height = fb->height;
  2015. info->pixel_format = fb->pixel_format;
  2016. info->pitch = fb->pitches[0];
  2017. info->fb_modifier = fb->modifier[0];
  2018. if (!(info->fb_modifier == I915_FORMAT_MOD_Y_TILED ||
  2019. info->fb_modifier == I915_FORMAT_MOD_Yf_TILED)) {
  2020. DRM_DEBUG_KMS(
  2021. "Y or Yf tiling is needed for 90/270 rotation!\n");
  2022. return -EINVAL;
  2023. }
  2024. return 0;
  2025. }
  2026. int
  2027. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2028. struct drm_framebuffer *fb,
  2029. const struct drm_plane_state *plane_state,
  2030. struct intel_engine_cs *pipelined)
  2031. {
  2032. struct drm_device *dev = fb->dev;
  2033. struct drm_i915_private *dev_priv = dev->dev_private;
  2034. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2035. struct i915_ggtt_view view;
  2036. u32 alignment;
  2037. int ret;
  2038. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2039. switch (fb->modifier[0]) {
  2040. case DRM_FORMAT_MOD_NONE:
  2041. if (INTEL_INFO(dev)->gen >= 9)
  2042. alignment = 256 * 1024;
  2043. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  2044. alignment = 128 * 1024;
  2045. else if (INTEL_INFO(dev)->gen >= 4)
  2046. alignment = 4 * 1024;
  2047. else
  2048. alignment = 64 * 1024;
  2049. break;
  2050. case I915_FORMAT_MOD_X_TILED:
  2051. if (INTEL_INFO(dev)->gen >= 9)
  2052. alignment = 256 * 1024;
  2053. else {
  2054. /* pin() will align the object as required by fence */
  2055. alignment = 0;
  2056. }
  2057. break;
  2058. case I915_FORMAT_MOD_Y_TILED:
  2059. case I915_FORMAT_MOD_Yf_TILED:
  2060. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2061. "Y tiling bo slipped through, driver bug!\n"))
  2062. return -EINVAL;
  2063. alignment = 1 * 1024 * 1024;
  2064. break;
  2065. default:
  2066. MISSING_CASE(fb->modifier[0]);
  2067. return -EINVAL;
  2068. }
  2069. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2070. if (ret)
  2071. return ret;
  2072. /* Note that the w/a also requires 64 PTE of padding following the
  2073. * bo. We currently fill all unused PTE with the shadow page and so
  2074. * we should always have valid PTE following the scanout preventing
  2075. * the VT-d warning.
  2076. */
  2077. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2078. alignment = 256 * 1024;
  2079. /*
  2080. * Global gtt pte registers are special registers which actually forward
  2081. * writes to a chunk of system memory. Which means that there is no risk
  2082. * that the register values disappear as soon as we call
  2083. * intel_runtime_pm_put(), so it is correct to wrap only the
  2084. * pin/unpin/fence and not more.
  2085. */
  2086. intel_runtime_pm_get(dev_priv);
  2087. dev_priv->mm.interruptible = false;
  2088. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2089. &view);
  2090. if (ret)
  2091. goto err_interruptible;
  2092. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2093. * fence, whereas 965+ only requires a fence if using
  2094. * framebuffer compression. For simplicity, we always install
  2095. * a fence as the cost is not that onerous.
  2096. */
  2097. ret = i915_gem_object_get_fence(obj);
  2098. if (ret)
  2099. goto err_unpin;
  2100. i915_gem_object_pin_fence(obj);
  2101. dev_priv->mm.interruptible = true;
  2102. intel_runtime_pm_put(dev_priv);
  2103. return 0;
  2104. err_unpin:
  2105. i915_gem_object_unpin_from_display_plane(obj, &view);
  2106. err_interruptible:
  2107. dev_priv->mm.interruptible = true;
  2108. intel_runtime_pm_put(dev_priv);
  2109. return ret;
  2110. }
  2111. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2112. const struct drm_plane_state *plane_state)
  2113. {
  2114. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2115. struct i915_ggtt_view view;
  2116. int ret;
  2117. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2118. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2119. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2120. i915_gem_object_unpin_fence(obj);
  2121. i915_gem_object_unpin_from_display_plane(obj, &view);
  2122. }
  2123. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2124. * is assumed to be a power-of-two. */
  2125. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2126. unsigned int tiling_mode,
  2127. unsigned int cpp,
  2128. unsigned int pitch)
  2129. {
  2130. if (tiling_mode != I915_TILING_NONE) {
  2131. unsigned int tile_rows, tiles;
  2132. tile_rows = *y / 8;
  2133. *y %= 8;
  2134. tiles = *x / (512/cpp);
  2135. *x %= 512/cpp;
  2136. return tile_rows * pitch * 8 + tiles * 4096;
  2137. } else {
  2138. unsigned int offset;
  2139. offset = *y * pitch + *x * cpp;
  2140. *y = 0;
  2141. *x = (offset & 4095) / cpp;
  2142. return offset & -4096;
  2143. }
  2144. }
  2145. static int i9xx_format_to_fourcc(int format)
  2146. {
  2147. switch (format) {
  2148. case DISPPLANE_8BPP:
  2149. return DRM_FORMAT_C8;
  2150. case DISPPLANE_BGRX555:
  2151. return DRM_FORMAT_XRGB1555;
  2152. case DISPPLANE_BGRX565:
  2153. return DRM_FORMAT_RGB565;
  2154. default:
  2155. case DISPPLANE_BGRX888:
  2156. return DRM_FORMAT_XRGB8888;
  2157. case DISPPLANE_RGBX888:
  2158. return DRM_FORMAT_XBGR8888;
  2159. case DISPPLANE_BGRX101010:
  2160. return DRM_FORMAT_XRGB2101010;
  2161. case DISPPLANE_RGBX101010:
  2162. return DRM_FORMAT_XBGR2101010;
  2163. }
  2164. }
  2165. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2166. {
  2167. switch (format) {
  2168. case PLANE_CTL_FORMAT_RGB_565:
  2169. return DRM_FORMAT_RGB565;
  2170. default:
  2171. case PLANE_CTL_FORMAT_XRGB_8888:
  2172. if (rgb_order) {
  2173. if (alpha)
  2174. return DRM_FORMAT_ABGR8888;
  2175. else
  2176. return DRM_FORMAT_XBGR8888;
  2177. } else {
  2178. if (alpha)
  2179. return DRM_FORMAT_ARGB8888;
  2180. else
  2181. return DRM_FORMAT_XRGB8888;
  2182. }
  2183. case PLANE_CTL_FORMAT_XRGB_2101010:
  2184. if (rgb_order)
  2185. return DRM_FORMAT_XBGR2101010;
  2186. else
  2187. return DRM_FORMAT_XRGB2101010;
  2188. }
  2189. }
  2190. static bool
  2191. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2192. struct intel_initial_plane_config *plane_config)
  2193. {
  2194. struct drm_device *dev = crtc->base.dev;
  2195. struct drm_i915_gem_object *obj = NULL;
  2196. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2197. struct drm_framebuffer *fb = &plane_config->fb->base;
  2198. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2199. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2200. PAGE_SIZE);
  2201. size_aligned -= base_aligned;
  2202. if (plane_config->size == 0)
  2203. return false;
  2204. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2205. base_aligned,
  2206. base_aligned,
  2207. size_aligned);
  2208. if (!obj)
  2209. return false;
  2210. obj->tiling_mode = plane_config->tiling;
  2211. if (obj->tiling_mode == I915_TILING_X)
  2212. obj->stride = fb->pitches[0];
  2213. mode_cmd.pixel_format = fb->pixel_format;
  2214. mode_cmd.width = fb->width;
  2215. mode_cmd.height = fb->height;
  2216. mode_cmd.pitches[0] = fb->pitches[0];
  2217. mode_cmd.modifier[0] = fb->modifier[0];
  2218. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2219. mutex_lock(&dev->struct_mutex);
  2220. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2221. &mode_cmd, obj)) {
  2222. DRM_DEBUG_KMS("intel fb init failed\n");
  2223. goto out_unref_obj;
  2224. }
  2225. mutex_unlock(&dev->struct_mutex);
  2226. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2227. return true;
  2228. out_unref_obj:
  2229. drm_gem_object_unreference(&obj->base);
  2230. mutex_unlock(&dev->struct_mutex);
  2231. return false;
  2232. }
  2233. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2234. static void
  2235. update_state_fb(struct drm_plane *plane)
  2236. {
  2237. if (plane->fb == plane->state->fb)
  2238. return;
  2239. if (plane->state->fb)
  2240. drm_framebuffer_unreference(plane->state->fb);
  2241. plane->state->fb = plane->fb;
  2242. if (plane->state->fb)
  2243. drm_framebuffer_reference(plane->state->fb);
  2244. }
  2245. static void
  2246. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2247. struct intel_initial_plane_config *plane_config)
  2248. {
  2249. struct drm_device *dev = intel_crtc->base.dev;
  2250. struct drm_i915_private *dev_priv = dev->dev_private;
  2251. struct drm_crtc *c;
  2252. struct intel_crtc *i;
  2253. struct drm_i915_gem_object *obj;
  2254. struct drm_plane *primary = intel_crtc->base.primary;
  2255. struct drm_framebuffer *fb;
  2256. if (!plane_config->fb)
  2257. return;
  2258. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2259. fb = &plane_config->fb->base;
  2260. goto valid_fb;
  2261. }
  2262. kfree(plane_config->fb);
  2263. /*
  2264. * Failed to alloc the obj, check to see if we should share
  2265. * an fb with another CRTC instead
  2266. */
  2267. for_each_crtc(dev, c) {
  2268. i = to_intel_crtc(c);
  2269. if (c == &intel_crtc->base)
  2270. continue;
  2271. if (!i->active)
  2272. continue;
  2273. fb = c->primary->fb;
  2274. if (!fb)
  2275. continue;
  2276. obj = intel_fb_obj(fb);
  2277. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2278. drm_framebuffer_reference(fb);
  2279. goto valid_fb;
  2280. }
  2281. }
  2282. return;
  2283. valid_fb:
  2284. obj = intel_fb_obj(fb);
  2285. if (obj->tiling_mode != I915_TILING_NONE)
  2286. dev_priv->preserve_bios_swizzle = true;
  2287. primary->fb = fb;
  2288. primary->state->crtc = &intel_crtc->base;
  2289. primary->crtc = &intel_crtc->base;
  2290. update_state_fb(primary);
  2291. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2292. }
  2293. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2294. struct drm_framebuffer *fb,
  2295. int x, int y)
  2296. {
  2297. struct drm_device *dev = crtc->dev;
  2298. struct drm_i915_private *dev_priv = dev->dev_private;
  2299. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2300. struct drm_i915_gem_object *obj;
  2301. int plane = intel_crtc->plane;
  2302. unsigned long linear_offset;
  2303. u32 dspcntr;
  2304. u32 reg = DSPCNTR(plane);
  2305. int pixel_size;
  2306. if (!intel_crtc->primary_enabled) {
  2307. I915_WRITE(reg, 0);
  2308. if (INTEL_INFO(dev)->gen >= 4)
  2309. I915_WRITE(DSPSURF(plane), 0);
  2310. else
  2311. I915_WRITE(DSPADDR(plane), 0);
  2312. POSTING_READ(reg);
  2313. return;
  2314. }
  2315. obj = intel_fb_obj(fb);
  2316. if (WARN_ON(obj == NULL))
  2317. return;
  2318. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2319. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2320. dspcntr |= DISPLAY_PLANE_ENABLE;
  2321. if (INTEL_INFO(dev)->gen < 4) {
  2322. if (intel_crtc->pipe == PIPE_B)
  2323. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2324. /* pipesrc and dspsize control the size that is scaled from,
  2325. * which should always be the user's requested size.
  2326. */
  2327. I915_WRITE(DSPSIZE(plane),
  2328. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2329. (intel_crtc->config->pipe_src_w - 1));
  2330. I915_WRITE(DSPPOS(plane), 0);
  2331. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2332. I915_WRITE(PRIMSIZE(plane),
  2333. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2334. (intel_crtc->config->pipe_src_w - 1));
  2335. I915_WRITE(PRIMPOS(plane), 0);
  2336. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2337. }
  2338. switch (fb->pixel_format) {
  2339. case DRM_FORMAT_C8:
  2340. dspcntr |= DISPPLANE_8BPP;
  2341. break;
  2342. case DRM_FORMAT_XRGB1555:
  2343. case DRM_FORMAT_ARGB1555:
  2344. dspcntr |= DISPPLANE_BGRX555;
  2345. break;
  2346. case DRM_FORMAT_RGB565:
  2347. dspcntr |= DISPPLANE_BGRX565;
  2348. break;
  2349. case DRM_FORMAT_XRGB8888:
  2350. case DRM_FORMAT_ARGB8888:
  2351. dspcntr |= DISPPLANE_BGRX888;
  2352. break;
  2353. case DRM_FORMAT_XBGR8888:
  2354. case DRM_FORMAT_ABGR8888:
  2355. dspcntr |= DISPPLANE_RGBX888;
  2356. break;
  2357. case DRM_FORMAT_XRGB2101010:
  2358. case DRM_FORMAT_ARGB2101010:
  2359. dspcntr |= DISPPLANE_BGRX101010;
  2360. break;
  2361. case DRM_FORMAT_XBGR2101010:
  2362. case DRM_FORMAT_ABGR2101010:
  2363. dspcntr |= DISPPLANE_RGBX101010;
  2364. break;
  2365. default:
  2366. BUG();
  2367. }
  2368. if (INTEL_INFO(dev)->gen >= 4 &&
  2369. obj->tiling_mode != I915_TILING_NONE)
  2370. dspcntr |= DISPPLANE_TILED;
  2371. if (IS_G4X(dev))
  2372. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2373. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2374. if (INTEL_INFO(dev)->gen >= 4) {
  2375. intel_crtc->dspaddr_offset =
  2376. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2377. pixel_size,
  2378. fb->pitches[0]);
  2379. linear_offset -= intel_crtc->dspaddr_offset;
  2380. } else {
  2381. intel_crtc->dspaddr_offset = linear_offset;
  2382. }
  2383. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2384. dspcntr |= DISPPLANE_ROTATE_180;
  2385. x += (intel_crtc->config->pipe_src_w - 1);
  2386. y += (intel_crtc->config->pipe_src_h - 1);
  2387. /* Finding the last pixel of the last line of the display
  2388. data and adding to linear_offset*/
  2389. linear_offset +=
  2390. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2391. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2392. }
  2393. I915_WRITE(reg, dspcntr);
  2394. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2395. if (INTEL_INFO(dev)->gen >= 4) {
  2396. I915_WRITE(DSPSURF(plane),
  2397. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2398. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2399. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2400. } else
  2401. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2402. POSTING_READ(reg);
  2403. }
  2404. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2405. struct drm_framebuffer *fb,
  2406. int x, int y)
  2407. {
  2408. struct drm_device *dev = crtc->dev;
  2409. struct drm_i915_private *dev_priv = dev->dev_private;
  2410. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2411. struct drm_i915_gem_object *obj;
  2412. int plane = intel_crtc->plane;
  2413. unsigned long linear_offset;
  2414. u32 dspcntr;
  2415. u32 reg = DSPCNTR(plane);
  2416. int pixel_size;
  2417. if (!intel_crtc->primary_enabled) {
  2418. I915_WRITE(reg, 0);
  2419. I915_WRITE(DSPSURF(plane), 0);
  2420. POSTING_READ(reg);
  2421. return;
  2422. }
  2423. obj = intel_fb_obj(fb);
  2424. if (WARN_ON(obj == NULL))
  2425. return;
  2426. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2427. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2428. dspcntr |= DISPLAY_PLANE_ENABLE;
  2429. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2430. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2431. switch (fb->pixel_format) {
  2432. case DRM_FORMAT_C8:
  2433. dspcntr |= DISPPLANE_8BPP;
  2434. break;
  2435. case DRM_FORMAT_RGB565:
  2436. dspcntr |= DISPPLANE_BGRX565;
  2437. break;
  2438. case DRM_FORMAT_XRGB8888:
  2439. case DRM_FORMAT_ARGB8888:
  2440. dspcntr |= DISPPLANE_BGRX888;
  2441. break;
  2442. case DRM_FORMAT_XBGR8888:
  2443. case DRM_FORMAT_ABGR8888:
  2444. dspcntr |= DISPPLANE_RGBX888;
  2445. break;
  2446. case DRM_FORMAT_XRGB2101010:
  2447. case DRM_FORMAT_ARGB2101010:
  2448. dspcntr |= DISPPLANE_BGRX101010;
  2449. break;
  2450. case DRM_FORMAT_XBGR2101010:
  2451. case DRM_FORMAT_ABGR2101010:
  2452. dspcntr |= DISPPLANE_RGBX101010;
  2453. break;
  2454. default:
  2455. BUG();
  2456. }
  2457. if (obj->tiling_mode != I915_TILING_NONE)
  2458. dspcntr |= DISPPLANE_TILED;
  2459. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2460. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2461. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2462. intel_crtc->dspaddr_offset =
  2463. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2464. pixel_size,
  2465. fb->pitches[0]);
  2466. linear_offset -= intel_crtc->dspaddr_offset;
  2467. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2468. dspcntr |= DISPPLANE_ROTATE_180;
  2469. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2470. x += (intel_crtc->config->pipe_src_w - 1);
  2471. y += (intel_crtc->config->pipe_src_h - 1);
  2472. /* Finding the last pixel of the last line of the display
  2473. data and adding to linear_offset*/
  2474. linear_offset +=
  2475. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2476. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2477. }
  2478. }
  2479. I915_WRITE(reg, dspcntr);
  2480. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2481. I915_WRITE(DSPSURF(plane),
  2482. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2483. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2484. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2485. } else {
  2486. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2487. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2488. }
  2489. POSTING_READ(reg);
  2490. }
  2491. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2492. uint32_t pixel_format)
  2493. {
  2494. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2495. /*
  2496. * The stride is either expressed as a multiple of 64 bytes
  2497. * chunks for linear buffers or in number of tiles for tiled
  2498. * buffers.
  2499. */
  2500. switch (fb_modifier) {
  2501. case DRM_FORMAT_MOD_NONE:
  2502. return 64;
  2503. case I915_FORMAT_MOD_X_TILED:
  2504. if (INTEL_INFO(dev)->gen == 2)
  2505. return 128;
  2506. return 512;
  2507. case I915_FORMAT_MOD_Y_TILED:
  2508. /* No need to check for old gens and Y tiling since this is
  2509. * about the display engine and those will be blocked before
  2510. * we get here.
  2511. */
  2512. return 128;
  2513. case I915_FORMAT_MOD_Yf_TILED:
  2514. if (bits_per_pixel == 8)
  2515. return 64;
  2516. else
  2517. return 128;
  2518. default:
  2519. MISSING_CASE(fb_modifier);
  2520. return 64;
  2521. }
  2522. }
  2523. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2524. struct drm_i915_gem_object *obj)
  2525. {
  2526. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2527. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2528. view = &i915_ggtt_view_rotated;
  2529. return i915_gem_obj_ggtt_offset_view(obj, view);
  2530. }
  2531. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2532. struct drm_framebuffer *fb,
  2533. int x, int y)
  2534. {
  2535. struct drm_device *dev = crtc->dev;
  2536. struct drm_i915_private *dev_priv = dev->dev_private;
  2537. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2538. struct drm_i915_gem_object *obj;
  2539. int pipe = intel_crtc->pipe;
  2540. u32 plane_ctl, stride_div;
  2541. unsigned long surf_addr;
  2542. if (!intel_crtc->primary_enabled) {
  2543. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2544. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2545. POSTING_READ(PLANE_CTL(pipe, 0));
  2546. return;
  2547. }
  2548. plane_ctl = PLANE_CTL_ENABLE |
  2549. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2550. PLANE_CTL_PIPE_CSC_ENABLE;
  2551. switch (fb->pixel_format) {
  2552. case DRM_FORMAT_RGB565:
  2553. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2554. break;
  2555. case DRM_FORMAT_XRGB8888:
  2556. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2557. break;
  2558. case DRM_FORMAT_ARGB8888:
  2559. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2560. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2561. break;
  2562. case DRM_FORMAT_XBGR8888:
  2563. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2564. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2565. break;
  2566. case DRM_FORMAT_ABGR8888:
  2567. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2568. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2569. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2570. break;
  2571. case DRM_FORMAT_XRGB2101010:
  2572. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2573. break;
  2574. case DRM_FORMAT_XBGR2101010:
  2575. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2576. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2577. break;
  2578. default:
  2579. BUG();
  2580. }
  2581. switch (fb->modifier[0]) {
  2582. case DRM_FORMAT_MOD_NONE:
  2583. break;
  2584. case I915_FORMAT_MOD_X_TILED:
  2585. plane_ctl |= PLANE_CTL_TILED_X;
  2586. break;
  2587. case I915_FORMAT_MOD_Y_TILED:
  2588. plane_ctl |= PLANE_CTL_TILED_Y;
  2589. break;
  2590. case I915_FORMAT_MOD_Yf_TILED:
  2591. plane_ctl |= PLANE_CTL_TILED_YF;
  2592. break;
  2593. default:
  2594. MISSING_CASE(fb->modifier[0]);
  2595. }
  2596. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2597. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
  2598. plane_ctl |= PLANE_CTL_ROTATE_180;
  2599. obj = intel_fb_obj(fb);
  2600. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2601. fb->pixel_format);
  2602. surf_addr = intel_plane_obj_offset(to_intel_plane(crtc->primary), obj);
  2603. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2604. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2605. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2606. I915_WRITE(PLANE_SIZE(pipe, 0),
  2607. (intel_crtc->config->pipe_src_h - 1) << 16 |
  2608. (intel_crtc->config->pipe_src_w - 1));
  2609. I915_WRITE(PLANE_STRIDE(pipe, 0), fb->pitches[0] / stride_div);
  2610. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2611. POSTING_READ(PLANE_SURF(pipe, 0));
  2612. }
  2613. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2614. static int
  2615. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2616. int x, int y, enum mode_set_atomic state)
  2617. {
  2618. struct drm_device *dev = crtc->dev;
  2619. struct drm_i915_private *dev_priv = dev->dev_private;
  2620. if (dev_priv->display.disable_fbc)
  2621. dev_priv->display.disable_fbc(dev);
  2622. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2623. return 0;
  2624. }
  2625. static void intel_complete_page_flips(struct drm_device *dev)
  2626. {
  2627. struct drm_crtc *crtc;
  2628. for_each_crtc(dev, crtc) {
  2629. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2630. enum plane plane = intel_crtc->plane;
  2631. intel_prepare_page_flip(dev, plane);
  2632. intel_finish_page_flip_plane(dev, plane);
  2633. }
  2634. }
  2635. static void intel_update_primary_planes(struct drm_device *dev)
  2636. {
  2637. struct drm_i915_private *dev_priv = dev->dev_private;
  2638. struct drm_crtc *crtc;
  2639. for_each_crtc(dev, crtc) {
  2640. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2641. drm_modeset_lock(&crtc->mutex, NULL);
  2642. /*
  2643. * FIXME: Once we have proper support for primary planes (and
  2644. * disabling them without disabling the entire crtc) allow again
  2645. * a NULL crtc->primary->fb.
  2646. */
  2647. if (intel_crtc->active && crtc->primary->fb)
  2648. dev_priv->display.update_primary_plane(crtc,
  2649. crtc->primary->fb,
  2650. crtc->x,
  2651. crtc->y);
  2652. drm_modeset_unlock(&crtc->mutex);
  2653. }
  2654. }
  2655. void intel_prepare_reset(struct drm_device *dev)
  2656. {
  2657. struct drm_i915_private *dev_priv = to_i915(dev);
  2658. struct intel_crtc *crtc;
  2659. /* no reset support for gen2 */
  2660. if (IS_GEN2(dev))
  2661. return;
  2662. /* reset doesn't touch the display */
  2663. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2664. return;
  2665. drm_modeset_lock_all(dev);
  2666. /*
  2667. * Disabling the crtcs gracefully seems nicer. Also the
  2668. * g33 docs say we should at least disable all the planes.
  2669. */
  2670. for_each_intel_crtc(dev, crtc) {
  2671. if (crtc->active)
  2672. dev_priv->display.crtc_disable(&crtc->base);
  2673. }
  2674. }
  2675. void intel_finish_reset(struct drm_device *dev)
  2676. {
  2677. struct drm_i915_private *dev_priv = to_i915(dev);
  2678. /*
  2679. * Flips in the rings will be nuked by the reset,
  2680. * so complete all pending flips so that user space
  2681. * will get its events and not get stuck.
  2682. */
  2683. intel_complete_page_flips(dev);
  2684. /* no reset support for gen2 */
  2685. if (IS_GEN2(dev))
  2686. return;
  2687. /* reset doesn't touch the display */
  2688. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2689. /*
  2690. * Flips in the rings have been nuked by the reset,
  2691. * so update the base address of all primary
  2692. * planes to the the last fb to make sure we're
  2693. * showing the correct fb after a reset.
  2694. */
  2695. intel_update_primary_planes(dev);
  2696. return;
  2697. }
  2698. /*
  2699. * The display has been reset as well,
  2700. * so need a full re-initialization.
  2701. */
  2702. intel_runtime_pm_disable_interrupts(dev_priv);
  2703. intel_runtime_pm_enable_interrupts(dev_priv);
  2704. intel_modeset_init_hw(dev);
  2705. spin_lock_irq(&dev_priv->irq_lock);
  2706. if (dev_priv->display.hpd_irq_setup)
  2707. dev_priv->display.hpd_irq_setup(dev);
  2708. spin_unlock_irq(&dev_priv->irq_lock);
  2709. intel_modeset_setup_hw_state(dev, true);
  2710. intel_hpd_init(dev_priv);
  2711. drm_modeset_unlock_all(dev);
  2712. }
  2713. static int
  2714. intel_finish_fb(struct drm_framebuffer *old_fb)
  2715. {
  2716. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2717. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2718. bool was_interruptible = dev_priv->mm.interruptible;
  2719. int ret;
  2720. /* Big Hammer, we also need to ensure that any pending
  2721. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2722. * current scanout is retired before unpinning the old
  2723. * framebuffer.
  2724. *
  2725. * This should only fail upon a hung GPU, in which case we
  2726. * can safely continue.
  2727. */
  2728. dev_priv->mm.interruptible = false;
  2729. ret = i915_gem_object_finish_gpu(obj);
  2730. dev_priv->mm.interruptible = was_interruptible;
  2731. return ret;
  2732. }
  2733. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2734. {
  2735. struct drm_device *dev = crtc->dev;
  2736. struct drm_i915_private *dev_priv = dev->dev_private;
  2737. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2738. bool pending;
  2739. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2740. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2741. return false;
  2742. spin_lock_irq(&dev->event_lock);
  2743. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2744. spin_unlock_irq(&dev->event_lock);
  2745. return pending;
  2746. }
  2747. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2748. {
  2749. struct drm_device *dev = crtc->base.dev;
  2750. struct drm_i915_private *dev_priv = dev->dev_private;
  2751. const struct drm_display_mode *adjusted_mode;
  2752. if (!i915.fastboot)
  2753. return;
  2754. /*
  2755. * Update pipe size and adjust fitter if needed: the reason for this is
  2756. * that in compute_mode_changes we check the native mode (not the pfit
  2757. * mode) to see if we can flip rather than do a full mode set. In the
  2758. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2759. * pfit state, we'll end up with a big fb scanned out into the wrong
  2760. * sized surface.
  2761. *
  2762. * To fix this properly, we need to hoist the checks up into
  2763. * compute_mode_changes (or above), check the actual pfit state and
  2764. * whether the platform allows pfit disable with pipe active, and only
  2765. * then update the pipesrc and pfit state, even on the flip path.
  2766. */
  2767. adjusted_mode = &crtc->config->base.adjusted_mode;
  2768. I915_WRITE(PIPESRC(crtc->pipe),
  2769. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2770. (adjusted_mode->crtc_vdisplay - 1));
  2771. if (!crtc->config->pch_pfit.enabled &&
  2772. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2773. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2774. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2775. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2776. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2777. }
  2778. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2779. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2780. }
  2781. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2782. {
  2783. struct drm_device *dev = crtc->dev;
  2784. struct drm_i915_private *dev_priv = dev->dev_private;
  2785. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2786. int pipe = intel_crtc->pipe;
  2787. u32 reg, temp;
  2788. /* enable normal train */
  2789. reg = FDI_TX_CTL(pipe);
  2790. temp = I915_READ(reg);
  2791. if (IS_IVYBRIDGE(dev)) {
  2792. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2793. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2794. } else {
  2795. temp &= ~FDI_LINK_TRAIN_NONE;
  2796. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2797. }
  2798. I915_WRITE(reg, temp);
  2799. reg = FDI_RX_CTL(pipe);
  2800. temp = I915_READ(reg);
  2801. if (HAS_PCH_CPT(dev)) {
  2802. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2803. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2804. } else {
  2805. temp &= ~FDI_LINK_TRAIN_NONE;
  2806. temp |= FDI_LINK_TRAIN_NONE;
  2807. }
  2808. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2809. /* wait one idle pattern time */
  2810. POSTING_READ(reg);
  2811. udelay(1000);
  2812. /* IVB wants error correction enabled */
  2813. if (IS_IVYBRIDGE(dev))
  2814. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2815. FDI_FE_ERRC_ENABLE);
  2816. }
  2817. /* The FDI link training functions for ILK/Ibexpeak. */
  2818. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2819. {
  2820. struct drm_device *dev = crtc->dev;
  2821. struct drm_i915_private *dev_priv = dev->dev_private;
  2822. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2823. int pipe = intel_crtc->pipe;
  2824. u32 reg, temp, tries;
  2825. /* FDI needs bits from pipe first */
  2826. assert_pipe_enabled(dev_priv, pipe);
  2827. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2828. for train result */
  2829. reg = FDI_RX_IMR(pipe);
  2830. temp = I915_READ(reg);
  2831. temp &= ~FDI_RX_SYMBOL_LOCK;
  2832. temp &= ~FDI_RX_BIT_LOCK;
  2833. I915_WRITE(reg, temp);
  2834. I915_READ(reg);
  2835. udelay(150);
  2836. /* enable CPU FDI TX and PCH FDI RX */
  2837. reg = FDI_TX_CTL(pipe);
  2838. temp = I915_READ(reg);
  2839. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2840. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2841. temp &= ~FDI_LINK_TRAIN_NONE;
  2842. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2843. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2844. reg = FDI_RX_CTL(pipe);
  2845. temp = I915_READ(reg);
  2846. temp &= ~FDI_LINK_TRAIN_NONE;
  2847. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2848. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2849. POSTING_READ(reg);
  2850. udelay(150);
  2851. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2852. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2853. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2854. FDI_RX_PHASE_SYNC_POINTER_EN);
  2855. reg = FDI_RX_IIR(pipe);
  2856. for (tries = 0; tries < 5; tries++) {
  2857. temp = I915_READ(reg);
  2858. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2859. if ((temp & FDI_RX_BIT_LOCK)) {
  2860. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2861. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2862. break;
  2863. }
  2864. }
  2865. if (tries == 5)
  2866. DRM_ERROR("FDI train 1 fail!\n");
  2867. /* Train 2 */
  2868. reg = FDI_TX_CTL(pipe);
  2869. temp = I915_READ(reg);
  2870. temp &= ~FDI_LINK_TRAIN_NONE;
  2871. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2872. I915_WRITE(reg, temp);
  2873. reg = FDI_RX_CTL(pipe);
  2874. temp = I915_READ(reg);
  2875. temp &= ~FDI_LINK_TRAIN_NONE;
  2876. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2877. I915_WRITE(reg, temp);
  2878. POSTING_READ(reg);
  2879. udelay(150);
  2880. reg = FDI_RX_IIR(pipe);
  2881. for (tries = 0; tries < 5; tries++) {
  2882. temp = I915_READ(reg);
  2883. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2884. if (temp & FDI_RX_SYMBOL_LOCK) {
  2885. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2886. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2887. break;
  2888. }
  2889. }
  2890. if (tries == 5)
  2891. DRM_ERROR("FDI train 2 fail!\n");
  2892. DRM_DEBUG_KMS("FDI train done\n");
  2893. }
  2894. static const int snb_b_fdi_train_param[] = {
  2895. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2896. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2897. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2898. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2899. };
  2900. /* The FDI link training functions for SNB/Cougarpoint. */
  2901. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2902. {
  2903. struct drm_device *dev = crtc->dev;
  2904. struct drm_i915_private *dev_priv = dev->dev_private;
  2905. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2906. int pipe = intel_crtc->pipe;
  2907. u32 reg, temp, i, retry;
  2908. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2909. for train result */
  2910. reg = FDI_RX_IMR(pipe);
  2911. temp = I915_READ(reg);
  2912. temp &= ~FDI_RX_SYMBOL_LOCK;
  2913. temp &= ~FDI_RX_BIT_LOCK;
  2914. I915_WRITE(reg, temp);
  2915. POSTING_READ(reg);
  2916. udelay(150);
  2917. /* enable CPU FDI TX and PCH FDI RX */
  2918. reg = FDI_TX_CTL(pipe);
  2919. temp = I915_READ(reg);
  2920. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2921. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2922. temp &= ~FDI_LINK_TRAIN_NONE;
  2923. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2924. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2925. /* SNB-B */
  2926. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2927. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2928. I915_WRITE(FDI_RX_MISC(pipe),
  2929. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2930. reg = FDI_RX_CTL(pipe);
  2931. temp = I915_READ(reg);
  2932. if (HAS_PCH_CPT(dev)) {
  2933. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2934. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2935. } else {
  2936. temp &= ~FDI_LINK_TRAIN_NONE;
  2937. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2938. }
  2939. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2940. POSTING_READ(reg);
  2941. udelay(150);
  2942. for (i = 0; i < 4; i++) {
  2943. reg = FDI_TX_CTL(pipe);
  2944. temp = I915_READ(reg);
  2945. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2946. temp |= snb_b_fdi_train_param[i];
  2947. I915_WRITE(reg, temp);
  2948. POSTING_READ(reg);
  2949. udelay(500);
  2950. for (retry = 0; retry < 5; retry++) {
  2951. reg = FDI_RX_IIR(pipe);
  2952. temp = I915_READ(reg);
  2953. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2954. if (temp & FDI_RX_BIT_LOCK) {
  2955. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2956. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2957. break;
  2958. }
  2959. udelay(50);
  2960. }
  2961. if (retry < 5)
  2962. break;
  2963. }
  2964. if (i == 4)
  2965. DRM_ERROR("FDI train 1 fail!\n");
  2966. /* Train 2 */
  2967. reg = FDI_TX_CTL(pipe);
  2968. temp = I915_READ(reg);
  2969. temp &= ~FDI_LINK_TRAIN_NONE;
  2970. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2971. if (IS_GEN6(dev)) {
  2972. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2973. /* SNB-B */
  2974. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2975. }
  2976. I915_WRITE(reg, temp);
  2977. reg = FDI_RX_CTL(pipe);
  2978. temp = I915_READ(reg);
  2979. if (HAS_PCH_CPT(dev)) {
  2980. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2981. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2982. } else {
  2983. temp &= ~FDI_LINK_TRAIN_NONE;
  2984. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2985. }
  2986. I915_WRITE(reg, temp);
  2987. POSTING_READ(reg);
  2988. udelay(150);
  2989. for (i = 0; i < 4; i++) {
  2990. reg = FDI_TX_CTL(pipe);
  2991. temp = I915_READ(reg);
  2992. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2993. temp |= snb_b_fdi_train_param[i];
  2994. I915_WRITE(reg, temp);
  2995. POSTING_READ(reg);
  2996. udelay(500);
  2997. for (retry = 0; retry < 5; retry++) {
  2998. reg = FDI_RX_IIR(pipe);
  2999. temp = I915_READ(reg);
  3000. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3001. if (temp & FDI_RX_SYMBOL_LOCK) {
  3002. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3003. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3004. break;
  3005. }
  3006. udelay(50);
  3007. }
  3008. if (retry < 5)
  3009. break;
  3010. }
  3011. if (i == 4)
  3012. DRM_ERROR("FDI train 2 fail!\n");
  3013. DRM_DEBUG_KMS("FDI train done.\n");
  3014. }
  3015. /* Manual link training for Ivy Bridge A0 parts */
  3016. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3017. {
  3018. struct drm_device *dev = crtc->dev;
  3019. struct drm_i915_private *dev_priv = dev->dev_private;
  3020. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3021. int pipe = intel_crtc->pipe;
  3022. u32 reg, temp, i, j;
  3023. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3024. for train result */
  3025. reg = FDI_RX_IMR(pipe);
  3026. temp = I915_READ(reg);
  3027. temp &= ~FDI_RX_SYMBOL_LOCK;
  3028. temp &= ~FDI_RX_BIT_LOCK;
  3029. I915_WRITE(reg, temp);
  3030. POSTING_READ(reg);
  3031. udelay(150);
  3032. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3033. I915_READ(FDI_RX_IIR(pipe)));
  3034. /* Try each vswing and preemphasis setting twice before moving on */
  3035. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3036. /* disable first in case we need to retry */
  3037. reg = FDI_TX_CTL(pipe);
  3038. temp = I915_READ(reg);
  3039. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3040. temp &= ~FDI_TX_ENABLE;
  3041. I915_WRITE(reg, temp);
  3042. reg = FDI_RX_CTL(pipe);
  3043. temp = I915_READ(reg);
  3044. temp &= ~FDI_LINK_TRAIN_AUTO;
  3045. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3046. temp &= ~FDI_RX_ENABLE;
  3047. I915_WRITE(reg, temp);
  3048. /* enable CPU FDI TX and PCH FDI RX */
  3049. reg = FDI_TX_CTL(pipe);
  3050. temp = I915_READ(reg);
  3051. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3052. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3053. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3054. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3055. temp |= snb_b_fdi_train_param[j/2];
  3056. temp |= FDI_COMPOSITE_SYNC;
  3057. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3058. I915_WRITE(FDI_RX_MISC(pipe),
  3059. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3060. reg = FDI_RX_CTL(pipe);
  3061. temp = I915_READ(reg);
  3062. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3063. temp |= FDI_COMPOSITE_SYNC;
  3064. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3065. POSTING_READ(reg);
  3066. udelay(1); /* should be 0.5us */
  3067. for (i = 0; i < 4; i++) {
  3068. reg = FDI_RX_IIR(pipe);
  3069. temp = I915_READ(reg);
  3070. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3071. if (temp & FDI_RX_BIT_LOCK ||
  3072. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3073. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3074. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3075. i);
  3076. break;
  3077. }
  3078. udelay(1); /* should be 0.5us */
  3079. }
  3080. if (i == 4) {
  3081. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3082. continue;
  3083. }
  3084. /* Train 2 */
  3085. reg = FDI_TX_CTL(pipe);
  3086. temp = I915_READ(reg);
  3087. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3088. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3089. I915_WRITE(reg, temp);
  3090. reg = FDI_RX_CTL(pipe);
  3091. temp = I915_READ(reg);
  3092. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3093. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3094. I915_WRITE(reg, temp);
  3095. POSTING_READ(reg);
  3096. udelay(2); /* should be 1.5us */
  3097. for (i = 0; i < 4; i++) {
  3098. reg = FDI_RX_IIR(pipe);
  3099. temp = I915_READ(reg);
  3100. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3101. if (temp & FDI_RX_SYMBOL_LOCK ||
  3102. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3103. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3104. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3105. i);
  3106. goto train_done;
  3107. }
  3108. udelay(2); /* should be 1.5us */
  3109. }
  3110. if (i == 4)
  3111. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3112. }
  3113. train_done:
  3114. DRM_DEBUG_KMS("FDI train done.\n");
  3115. }
  3116. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3117. {
  3118. struct drm_device *dev = intel_crtc->base.dev;
  3119. struct drm_i915_private *dev_priv = dev->dev_private;
  3120. int pipe = intel_crtc->pipe;
  3121. u32 reg, temp;
  3122. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3123. reg = FDI_RX_CTL(pipe);
  3124. temp = I915_READ(reg);
  3125. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3126. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3127. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3128. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3129. POSTING_READ(reg);
  3130. udelay(200);
  3131. /* Switch from Rawclk to PCDclk */
  3132. temp = I915_READ(reg);
  3133. I915_WRITE(reg, temp | FDI_PCDCLK);
  3134. POSTING_READ(reg);
  3135. udelay(200);
  3136. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3137. reg = FDI_TX_CTL(pipe);
  3138. temp = I915_READ(reg);
  3139. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3140. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3141. POSTING_READ(reg);
  3142. udelay(100);
  3143. }
  3144. }
  3145. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3146. {
  3147. struct drm_device *dev = intel_crtc->base.dev;
  3148. struct drm_i915_private *dev_priv = dev->dev_private;
  3149. int pipe = intel_crtc->pipe;
  3150. u32 reg, temp;
  3151. /* Switch from PCDclk to Rawclk */
  3152. reg = FDI_RX_CTL(pipe);
  3153. temp = I915_READ(reg);
  3154. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3155. /* Disable CPU FDI TX PLL */
  3156. reg = FDI_TX_CTL(pipe);
  3157. temp = I915_READ(reg);
  3158. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3159. POSTING_READ(reg);
  3160. udelay(100);
  3161. reg = FDI_RX_CTL(pipe);
  3162. temp = I915_READ(reg);
  3163. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3164. /* Wait for the clocks to turn off. */
  3165. POSTING_READ(reg);
  3166. udelay(100);
  3167. }
  3168. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3169. {
  3170. struct drm_device *dev = crtc->dev;
  3171. struct drm_i915_private *dev_priv = dev->dev_private;
  3172. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3173. int pipe = intel_crtc->pipe;
  3174. u32 reg, temp;
  3175. /* disable CPU FDI tx and PCH FDI rx */
  3176. reg = FDI_TX_CTL(pipe);
  3177. temp = I915_READ(reg);
  3178. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3179. POSTING_READ(reg);
  3180. reg = FDI_RX_CTL(pipe);
  3181. temp = I915_READ(reg);
  3182. temp &= ~(0x7 << 16);
  3183. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3184. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3185. POSTING_READ(reg);
  3186. udelay(100);
  3187. /* Ironlake workaround, disable clock pointer after downing FDI */
  3188. if (HAS_PCH_IBX(dev))
  3189. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3190. /* still set train pattern 1 */
  3191. reg = FDI_TX_CTL(pipe);
  3192. temp = I915_READ(reg);
  3193. temp &= ~FDI_LINK_TRAIN_NONE;
  3194. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3195. I915_WRITE(reg, temp);
  3196. reg = FDI_RX_CTL(pipe);
  3197. temp = I915_READ(reg);
  3198. if (HAS_PCH_CPT(dev)) {
  3199. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3200. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3201. } else {
  3202. temp &= ~FDI_LINK_TRAIN_NONE;
  3203. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3204. }
  3205. /* BPC in FDI rx is consistent with that in PIPECONF */
  3206. temp &= ~(0x07 << 16);
  3207. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3208. I915_WRITE(reg, temp);
  3209. POSTING_READ(reg);
  3210. udelay(100);
  3211. }
  3212. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3213. {
  3214. struct intel_crtc *crtc;
  3215. /* Note that we don't need to be called with mode_config.lock here
  3216. * as our list of CRTC objects is static for the lifetime of the
  3217. * device and so cannot disappear as we iterate. Similarly, we can
  3218. * happily treat the predicates as racy, atomic checks as userspace
  3219. * cannot claim and pin a new fb without at least acquring the
  3220. * struct_mutex and so serialising with us.
  3221. */
  3222. for_each_intel_crtc(dev, crtc) {
  3223. if (atomic_read(&crtc->unpin_work_count) == 0)
  3224. continue;
  3225. if (crtc->unpin_work)
  3226. intel_wait_for_vblank(dev, crtc->pipe);
  3227. return true;
  3228. }
  3229. return false;
  3230. }
  3231. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3232. {
  3233. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3234. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3235. /* ensure that the unpin work is consistent wrt ->pending. */
  3236. smp_rmb();
  3237. intel_crtc->unpin_work = NULL;
  3238. if (work->event)
  3239. drm_send_vblank_event(intel_crtc->base.dev,
  3240. intel_crtc->pipe,
  3241. work->event);
  3242. drm_crtc_vblank_put(&intel_crtc->base);
  3243. wake_up_all(&dev_priv->pending_flip_queue);
  3244. queue_work(dev_priv->wq, &work->work);
  3245. trace_i915_flip_complete(intel_crtc->plane,
  3246. work->pending_flip_obj);
  3247. }
  3248. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3249. {
  3250. struct drm_device *dev = crtc->dev;
  3251. struct drm_i915_private *dev_priv = dev->dev_private;
  3252. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3253. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3254. !intel_crtc_has_pending_flip(crtc),
  3255. 60*HZ) == 0)) {
  3256. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3257. spin_lock_irq(&dev->event_lock);
  3258. if (intel_crtc->unpin_work) {
  3259. WARN_ONCE(1, "Removing stuck page flip\n");
  3260. page_flip_completed(intel_crtc);
  3261. }
  3262. spin_unlock_irq(&dev->event_lock);
  3263. }
  3264. if (crtc->primary->fb) {
  3265. mutex_lock(&dev->struct_mutex);
  3266. intel_finish_fb(crtc->primary->fb);
  3267. mutex_unlock(&dev->struct_mutex);
  3268. }
  3269. }
  3270. /* Program iCLKIP clock to the desired frequency */
  3271. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3272. {
  3273. struct drm_device *dev = crtc->dev;
  3274. struct drm_i915_private *dev_priv = dev->dev_private;
  3275. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3276. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3277. u32 temp;
  3278. mutex_lock(&dev_priv->dpio_lock);
  3279. /* It is necessary to ungate the pixclk gate prior to programming
  3280. * the divisors, and gate it back when it is done.
  3281. */
  3282. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3283. /* Disable SSCCTL */
  3284. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3285. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3286. SBI_SSCCTL_DISABLE,
  3287. SBI_ICLK);
  3288. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3289. if (clock == 20000) {
  3290. auxdiv = 1;
  3291. divsel = 0x41;
  3292. phaseinc = 0x20;
  3293. } else {
  3294. /* The iCLK virtual clock root frequency is in MHz,
  3295. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3296. * divisors, it is necessary to divide one by another, so we
  3297. * convert the virtual clock precision to KHz here for higher
  3298. * precision.
  3299. */
  3300. u32 iclk_virtual_root_freq = 172800 * 1000;
  3301. u32 iclk_pi_range = 64;
  3302. u32 desired_divisor, msb_divisor_value, pi_value;
  3303. desired_divisor = (iclk_virtual_root_freq / clock);
  3304. msb_divisor_value = desired_divisor / iclk_pi_range;
  3305. pi_value = desired_divisor % iclk_pi_range;
  3306. auxdiv = 0;
  3307. divsel = msb_divisor_value - 2;
  3308. phaseinc = pi_value;
  3309. }
  3310. /* This should not happen with any sane values */
  3311. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3312. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3313. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3314. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3315. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3316. clock,
  3317. auxdiv,
  3318. divsel,
  3319. phasedir,
  3320. phaseinc);
  3321. /* Program SSCDIVINTPHASE6 */
  3322. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3323. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3324. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3325. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3326. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3327. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3328. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3329. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3330. /* Program SSCAUXDIV */
  3331. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3332. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3333. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3334. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3335. /* Enable modulator and associated divider */
  3336. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3337. temp &= ~SBI_SSCCTL_DISABLE;
  3338. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3339. /* Wait for initialization time */
  3340. udelay(24);
  3341. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3342. mutex_unlock(&dev_priv->dpio_lock);
  3343. }
  3344. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3345. enum pipe pch_transcoder)
  3346. {
  3347. struct drm_device *dev = crtc->base.dev;
  3348. struct drm_i915_private *dev_priv = dev->dev_private;
  3349. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3350. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3351. I915_READ(HTOTAL(cpu_transcoder)));
  3352. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3353. I915_READ(HBLANK(cpu_transcoder)));
  3354. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3355. I915_READ(HSYNC(cpu_transcoder)));
  3356. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3357. I915_READ(VTOTAL(cpu_transcoder)));
  3358. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3359. I915_READ(VBLANK(cpu_transcoder)));
  3360. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3361. I915_READ(VSYNC(cpu_transcoder)));
  3362. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3363. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3364. }
  3365. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3366. {
  3367. struct drm_i915_private *dev_priv = dev->dev_private;
  3368. uint32_t temp;
  3369. temp = I915_READ(SOUTH_CHICKEN1);
  3370. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3371. return;
  3372. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3373. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3374. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3375. if (enable)
  3376. temp |= FDI_BC_BIFURCATION_SELECT;
  3377. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3378. I915_WRITE(SOUTH_CHICKEN1, temp);
  3379. POSTING_READ(SOUTH_CHICKEN1);
  3380. }
  3381. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3382. {
  3383. struct drm_device *dev = intel_crtc->base.dev;
  3384. switch (intel_crtc->pipe) {
  3385. case PIPE_A:
  3386. break;
  3387. case PIPE_B:
  3388. if (intel_crtc->config->fdi_lanes > 2)
  3389. cpt_set_fdi_bc_bifurcation(dev, false);
  3390. else
  3391. cpt_set_fdi_bc_bifurcation(dev, true);
  3392. break;
  3393. case PIPE_C:
  3394. cpt_set_fdi_bc_bifurcation(dev, true);
  3395. break;
  3396. default:
  3397. BUG();
  3398. }
  3399. }
  3400. /*
  3401. * Enable PCH resources required for PCH ports:
  3402. * - PCH PLLs
  3403. * - FDI training & RX/TX
  3404. * - update transcoder timings
  3405. * - DP transcoding bits
  3406. * - transcoder
  3407. */
  3408. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3409. {
  3410. struct drm_device *dev = crtc->dev;
  3411. struct drm_i915_private *dev_priv = dev->dev_private;
  3412. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3413. int pipe = intel_crtc->pipe;
  3414. u32 reg, temp;
  3415. assert_pch_transcoder_disabled(dev_priv, pipe);
  3416. if (IS_IVYBRIDGE(dev))
  3417. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3418. /* Write the TU size bits before fdi link training, so that error
  3419. * detection works. */
  3420. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3421. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3422. /* For PCH output, training FDI link */
  3423. dev_priv->display.fdi_link_train(crtc);
  3424. /* We need to program the right clock selection before writing the pixel
  3425. * mutliplier into the DPLL. */
  3426. if (HAS_PCH_CPT(dev)) {
  3427. u32 sel;
  3428. temp = I915_READ(PCH_DPLL_SEL);
  3429. temp |= TRANS_DPLL_ENABLE(pipe);
  3430. sel = TRANS_DPLLB_SEL(pipe);
  3431. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3432. temp |= sel;
  3433. else
  3434. temp &= ~sel;
  3435. I915_WRITE(PCH_DPLL_SEL, temp);
  3436. }
  3437. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3438. * transcoder, and we actually should do this to not upset any PCH
  3439. * transcoder that already use the clock when we share it.
  3440. *
  3441. * Note that enable_shared_dpll tries to do the right thing, but
  3442. * get_shared_dpll unconditionally resets the pll - we need that to have
  3443. * the right LVDS enable sequence. */
  3444. intel_enable_shared_dpll(intel_crtc);
  3445. /* set transcoder timing, panel must allow it */
  3446. assert_panel_unlocked(dev_priv, pipe);
  3447. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3448. intel_fdi_normal_train(crtc);
  3449. /* For PCH DP, enable TRANS_DP_CTL */
  3450. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3451. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3452. reg = TRANS_DP_CTL(pipe);
  3453. temp = I915_READ(reg);
  3454. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3455. TRANS_DP_SYNC_MASK |
  3456. TRANS_DP_BPC_MASK);
  3457. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3458. TRANS_DP_ENH_FRAMING);
  3459. temp |= bpc << 9; /* same format but at 11:9 */
  3460. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3461. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3462. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3463. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3464. switch (intel_trans_dp_port_sel(crtc)) {
  3465. case PCH_DP_B:
  3466. temp |= TRANS_DP_PORT_SEL_B;
  3467. break;
  3468. case PCH_DP_C:
  3469. temp |= TRANS_DP_PORT_SEL_C;
  3470. break;
  3471. case PCH_DP_D:
  3472. temp |= TRANS_DP_PORT_SEL_D;
  3473. break;
  3474. default:
  3475. BUG();
  3476. }
  3477. I915_WRITE(reg, temp);
  3478. }
  3479. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3480. }
  3481. static void lpt_pch_enable(struct drm_crtc *crtc)
  3482. {
  3483. struct drm_device *dev = crtc->dev;
  3484. struct drm_i915_private *dev_priv = dev->dev_private;
  3485. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3486. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3487. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3488. lpt_program_iclkip(crtc);
  3489. /* Set transcoder timing. */
  3490. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3491. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3492. }
  3493. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3494. {
  3495. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3496. if (pll == NULL)
  3497. return;
  3498. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3499. WARN(1, "bad %s crtc mask\n", pll->name);
  3500. return;
  3501. }
  3502. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3503. if (pll->config.crtc_mask == 0) {
  3504. WARN_ON(pll->on);
  3505. WARN_ON(pll->active);
  3506. }
  3507. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3508. }
  3509. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3510. struct intel_crtc_state *crtc_state)
  3511. {
  3512. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3513. struct intel_shared_dpll *pll;
  3514. enum intel_dpll_id i;
  3515. if (HAS_PCH_IBX(dev_priv->dev)) {
  3516. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3517. i = (enum intel_dpll_id) crtc->pipe;
  3518. pll = &dev_priv->shared_dplls[i];
  3519. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3520. crtc->base.base.id, pll->name);
  3521. WARN_ON(pll->new_config->crtc_mask);
  3522. goto found;
  3523. }
  3524. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3525. pll = &dev_priv->shared_dplls[i];
  3526. /* Only want to check enabled timings first */
  3527. if (pll->new_config->crtc_mask == 0)
  3528. continue;
  3529. if (memcmp(&crtc_state->dpll_hw_state,
  3530. &pll->new_config->hw_state,
  3531. sizeof(pll->new_config->hw_state)) == 0) {
  3532. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3533. crtc->base.base.id, pll->name,
  3534. pll->new_config->crtc_mask,
  3535. pll->active);
  3536. goto found;
  3537. }
  3538. }
  3539. /* Ok no matching timings, maybe there's a free one? */
  3540. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3541. pll = &dev_priv->shared_dplls[i];
  3542. if (pll->new_config->crtc_mask == 0) {
  3543. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3544. crtc->base.base.id, pll->name);
  3545. goto found;
  3546. }
  3547. }
  3548. return NULL;
  3549. found:
  3550. if (pll->new_config->crtc_mask == 0)
  3551. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3552. crtc_state->shared_dpll = i;
  3553. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3554. pipe_name(crtc->pipe));
  3555. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3556. return pll;
  3557. }
  3558. /**
  3559. * intel_shared_dpll_start_config - start a new PLL staged config
  3560. * @dev_priv: DRM device
  3561. * @clear_pipes: mask of pipes that will have their PLLs freed
  3562. *
  3563. * Starts a new PLL staged config, copying the current config but
  3564. * releasing the references of pipes specified in clear_pipes.
  3565. */
  3566. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3567. unsigned clear_pipes)
  3568. {
  3569. struct intel_shared_dpll *pll;
  3570. enum intel_dpll_id i;
  3571. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3572. pll = &dev_priv->shared_dplls[i];
  3573. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3574. GFP_KERNEL);
  3575. if (!pll->new_config)
  3576. goto cleanup;
  3577. pll->new_config->crtc_mask &= ~clear_pipes;
  3578. }
  3579. return 0;
  3580. cleanup:
  3581. while (--i >= 0) {
  3582. pll = &dev_priv->shared_dplls[i];
  3583. kfree(pll->new_config);
  3584. pll->new_config = NULL;
  3585. }
  3586. return -ENOMEM;
  3587. }
  3588. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3589. {
  3590. struct intel_shared_dpll *pll;
  3591. enum intel_dpll_id i;
  3592. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3593. pll = &dev_priv->shared_dplls[i];
  3594. WARN_ON(pll->new_config == &pll->config);
  3595. pll->config = *pll->new_config;
  3596. kfree(pll->new_config);
  3597. pll->new_config = NULL;
  3598. }
  3599. }
  3600. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3601. {
  3602. struct intel_shared_dpll *pll;
  3603. enum intel_dpll_id i;
  3604. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3605. pll = &dev_priv->shared_dplls[i];
  3606. WARN_ON(pll->new_config == &pll->config);
  3607. kfree(pll->new_config);
  3608. pll->new_config = NULL;
  3609. }
  3610. }
  3611. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3612. {
  3613. struct drm_i915_private *dev_priv = dev->dev_private;
  3614. int dslreg = PIPEDSL(pipe);
  3615. u32 temp;
  3616. temp = I915_READ(dslreg);
  3617. udelay(500);
  3618. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3619. if (wait_for(I915_READ(dslreg) != temp, 5))
  3620. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3621. }
  3622. }
  3623. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3624. {
  3625. struct drm_device *dev = crtc->base.dev;
  3626. struct drm_i915_private *dev_priv = dev->dev_private;
  3627. int pipe = crtc->pipe;
  3628. if (crtc->config->pch_pfit.enabled) {
  3629. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3630. I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3631. I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3632. }
  3633. }
  3634. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3635. {
  3636. struct drm_device *dev = crtc->base.dev;
  3637. struct drm_i915_private *dev_priv = dev->dev_private;
  3638. int pipe = crtc->pipe;
  3639. if (crtc->config->pch_pfit.enabled) {
  3640. /* Force use of hard-coded filter coefficients
  3641. * as some pre-programmed values are broken,
  3642. * e.g. x201.
  3643. */
  3644. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3645. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3646. PF_PIPE_SEL_IVB(pipe));
  3647. else
  3648. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3649. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3650. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3651. }
  3652. }
  3653. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3654. {
  3655. struct drm_device *dev = crtc->dev;
  3656. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3657. struct drm_plane *plane;
  3658. struct intel_plane *intel_plane;
  3659. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3660. intel_plane = to_intel_plane(plane);
  3661. if (intel_plane->pipe == pipe)
  3662. intel_plane_restore(&intel_plane->base);
  3663. }
  3664. }
  3665. /*
  3666. * Disable a plane internally without actually modifying the plane's state.
  3667. * This will allow us to easily restore the plane later by just reprogramming
  3668. * its state.
  3669. */
  3670. static void disable_plane_internal(struct drm_plane *plane)
  3671. {
  3672. struct intel_plane *intel_plane = to_intel_plane(plane);
  3673. struct drm_plane_state *state =
  3674. plane->funcs->atomic_duplicate_state(plane);
  3675. struct intel_plane_state *intel_state = to_intel_plane_state(state);
  3676. intel_state->visible = false;
  3677. intel_plane->commit_plane(plane, intel_state);
  3678. intel_plane_destroy_state(plane, state);
  3679. }
  3680. static void intel_disable_sprite_planes(struct drm_crtc *crtc)
  3681. {
  3682. struct drm_device *dev = crtc->dev;
  3683. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3684. struct drm_plane *plane;
  3685. struct intel_plane *intel_plane;
  3686. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3687. intel_plane = to_intel_plane(plane);
  3688. if (plane->fb && intel_plane->pipe == pipe)
  3689. disable_plane_internal(plane);
  3690. }
  3691. }
  3692. void hsw_enable_ips(struct intel_crtc *crtc)
  3693. {
  3694. struct drm_device *dev = crtc->base.dev;
  3695. struct drm_i915_private *dev_priv = dev->dev_private;
  3696. if (!crtc->config->ips_enabled)
  3697. return;
  3698. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3699. intel_wait_for_vblank(dev, crtc->pipe);
  3700. assert_plane_enabled(dev_priv, crtc->plane);
  3701. if (IS_BROADWELL(dev)) {
  3702. mutex_lock(&dev_priv->rps.hw_lock);
  3703. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3704. mutex_unlock(&dev_priv->rps.hw_lock);
  3705. /* Quoting Art Runyan: "its not safe to expect any particular
  3706. * value in IPS_CTL bit 31 after enabling IPS through the
  3707. * mailbox." Moreover, the mailbox may return a bogus state,
  3708. * so we need to just enable it and continue on.
  3709. */
  3710. } else {
  3711. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3712. /* The bit only becomes 1 in the next vblank, so this wait here
  3713. * is essentially intel_wait_for_vblank. If we don't have this
  3714. * and don't wait for vblanks until the end of crtc_enable, then
  3715. * the HW state readout code will complain that the expected
  3716. * IPS_CTL value is not the one we read. */
  3717. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3718. DRM_ERROR("Timed out waiting for IPS enable\n");
  3719. }
  3720. }
  3721. void hsw_disable_ips(struct intel_crtc *crtc)
  3722. {
  3723. struct drm_device *dev = crtc->base.dev;
  3724. struct drm_i915_private *dev_priv = dev->dev_private;
  3725. if (!crtc->config->ips_enabled)
  3726. return;
  3727. assert_plane_enabled(dev_priv, crtc->plane);
  3728. if (IS_BROADWELL(dev)) {
  3729. mutex_lock(&dev_priv->rps.hw_lock);
  3730. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3731. mutex_unlock(&dev_priv->rps.hw_lock);
  3732. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3733. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3734. DRM_ERROR("Timed out waiting for IPS disable\n");
  3735. } else {
  3736. I915_WRITE(IPS_CTL, 0);
  3737. POSTING_READ(IPS_CTL);
  3738. }
  3739. /* We need to wait for a vblank before we can disable the plane. */
  3740. intel_wait_for_vblank(dev, crtc->pipe);
  3741. }
  3742. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3743. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3744. {
  3745. struct drm_device *dev = crtc->dev;
  3746. struct drm_i915_private *dev_priv = dev->dev_private;
  3747. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3748. enum pipe pipe = intel_crtc->pipe;
  3749. int palreg = PALETTE(pipe);
  3750. int i;
  3751. bool reenable_ips = false;
  3752. /* The clocks have to be on to load the palette. */
  3753. if (!crtc->state->enable || !intel_crtc->active)
  3754. return;
  3755. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3756. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3757. assert_dsi_pll_enabled(dev_priv);
  3758. else
  3759. assert_pll_enabled(dev_priv, pipe);
  3760. }
  3761. /* use legacy palette for Ironlake */
  3762. if (!HAS_GMCH_DISPLAY(dev))
  3763. palreg = LGC_PALETTE(pipe);
  3764. /* Workaround : Do not read or write the pipe palette/gamma data while
  3765. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3766. */
  3767. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3768. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3769. GAMMA_MODE_MODE_SPLIT)) {
  3770. hsw_disable_ips(intel_crtc);
  3771. reenable_ips = true;
  3772. }
  3773. for (i = 0; i < 256; i++) {
  3774. I915_WRITE(palreg + 4 * i,
  3775. (intel_crtc->lut_r[i] << 16) |
  3776. (intel_crtc->lut_g[i] << 8) |
  3777. intel_crtc->lut_b[i]);
  3778. }
  3779. if (reenable_ips)
  3780. hsw_enable_ips(intel_crtc);
  3781. }
  3782. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3783. {
  3784. if (!enable && intel_crtc->overlay) {
  3785. struct drm_device *dev = intel_crtc->base.dev;
  3786. struct drm_i915_private *dev_priv = dev->dev_private;
  3787. mutex_lock(&dev->struct_mutex);
  3788. dev_priv->mm.interruptible = false;
  3789. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3790. dev_priv->mm.interruptible = true;
  3791. mutex_unlock(&dev->struct_mutex);
  3792. }
  3793. /* Let userspace switch the overlay on again. In most cases userspace
  3794. * has to recompute where to put it anyway.
  3795. */
  3796. }
  3797. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3798. {
  3799. struct drm_device *dev = crtc->dev;
  3800. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3801. int pipe = intel_crtc->pipe;
  3802. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3803. intel_enable_sprite_planes(crtc);
  3804. intel_crtc_update_cursor(crtc, true);
  3805. intel_crtc_dpms_overlay(intel_crtc, true);
  3806. hsw_enable_ips(intel_crtc);
  3807. mutex_lock(&dev->struct_mutex);
  3808. intel_fbc_update(dev);
  3809. mutex_unlock(&dev->struct_mutex);
  3810. /*
  3811. * FIXME: Once we grow proper nuclear flip support out of this we need
  3812. * to compute the mask of flip planes precisely. For the time being
  3813. * consider this a flip from a NULL plane.
  3814. */
  3815. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3816. }
  3817. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3818. {
  3819. struct drm_device *dev = crtc->dev;
  3820. struct drm_i915_private *dev_priv = dev->dev_private;
  3821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3822. int pipe = intel_crtc->pipe;
  3823. intel_crtc_wait_for_pending_flips(crtc);
  3824. if (dev_priv->fbc.crtc == intel_crtc)
  3825. intel_fbc_disable(dev);
  3826. hsw_disable_ips(intel_crtc);
  3827. intel_crtc_dpms_overlay(intel_crtc, false);
  3828. intel_crtc_update_cursor(crtc, false);
  3829. intel_disable_sprite_planes(crtc);
  3830. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3831. /*
  3832. * FIXME: Once we grow proper nuclear flip support out of this we need
  3833. * to compute the mask of flip planes precisely. For the time being
  3834. * consider this a flip to a NULL plane.
  3835. */
  3836. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3837. }
  3838. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3839. {
  3840. struct drm_device *dev = crtc->dev;
  3841. struct drm_i915_private *dev_priv = dev->dev_private;
  3842. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3843. struct intel_encoder *encoder;
  3844. int pipe = intel_crtc->pipe;
  3845. WARN_ON(!crtc->state->enable);
  3846. if (intel_crtc->active)
  3847. return;
  3848. if (intel_crtc->config->has_pch_encoder)
  3849. intel_prepare_shared_dpll(intel_crtc);
  3850. if (intel_crtc->config->has_dp_encoder)
  3851. intel_dp_set_m_n(intel_crtc, M1_N1);
  3852. intel_set_pipe_timings(intel_crtc);
  3853. if (intel_crtc->config->has_pch_encoder) {
  3854. intel_cpu_transcoder_set_m_n(intel_crtc,
  3855. &intel_crtc->config->fdi_m_n, NULL);
  3856. }
  3857. ironlake_set_pipeconf(crtc);
  3858. intel_crtc->active = true;
  3859. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3860. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3861. for_each_encoder_on_crtc(dev, crtc, encoder)
  3862. if (encoder->pre_enable)
  3863. encoder->pre_enable(encoder);
  3864. if (intel_crtc->config->has_pch_encoder) {
  3865. /* Note: FDI PLL enabling _must_ be done before we enable the
  3866. * cpu pipes, hence this is separate from all the other fdi/pch
  3867. * enabling. */
  3868. ironlake_fdi_pll_enable(intel_crtc);
  3869. } else {
  3870. assert_fdi_tx_disabled(dev_priv, pipe);
  3871. assert_fdi_rx_disabled(dev_priv, pipe);
  3872. }
  3873. ironlake_pfit_enable(intel_crtc);
  3874. /*
  3875. * On ILK+ LUT must be loaded before the pipe is running but with
  3876. * clocks enabled
  3877. */
  3878. intel_crtc_load_lut(crtc);
  3879. intel_update_watermarks(crtc);
  3880. intel_enable_pipe(intel_crtc);
  3881. if (intel_crtc->config->has_pch_encoder)
  3882. ironlake_pch_enable(crtc);
  3883. assert_vblank_disabled(crtc);
  3884. drm_crtc_vblank_on(crtc);
  3885. for_each_encoder_on_crtc(dev, crtc, encoder)
  3886. encoder->enable(encoder);
  3887. if (HAS_PCH_CPT(dev))
  3888. cpt_verify_modeset(dev, intel_crtc->pipe);
  3889. intel_crtc_enable_planes(crtc);
  3890. }
  3891. /* IPS only exists on ULT machines and is tied to pipe A. */
  3892. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3893. {
  3894. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3895. }
  3896. /*
  3897. * This implements the workaround described in the "notes" section of the mode
  3898. * set sequence documentation. When going from no pipes or single pipe to
  3899. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3900. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3901. */
  3902. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3903. {
  3904. struct drm_device *dev = crtc->base.dev;
  3905. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3906. /* We want to get the other_active_crtc only if there's only 1 other
  3907. * active crtc. */
  3908. for_each_intel_crtc(dev, crtc_it) {
  3909. if (!crtc_it->active || crtc_it == crtc)
  3910. continue;
  3911. if (other_active_crtc)
  3912. return;
  3913. other_active_crtc = crtc_it;
  3914. }
  3915. if (!other_active_crtc)
  3916. return;
  3917. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3918. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3919. }
  3920. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3921. {
  3922. struct drm_device *dev = crtc->dev;
  3923. struct drm_i915_private *dev_priv = dev->dev_private;
  3924. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3925. struct intel_encoder *encoder;
  3926. int pipe = intel_crtc->pipe;
  3927. WARN_ON(!crtc->state->enable);
  3928. if (intel_crtc->active)
  3929. return;
  3930. if (intel_crtc_to_shared_dpll(intel_crtc))
  3931. intel_enable_shared_dpll(intel_crtc);
  3932. if (intel_crtc->config->has_dp_encoder)
  3933. intel_dp_set_m_n(intel_crtc, M1_N1);
  3934. intel_set_pipe_timings(intel_crtc);
  3935. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  3936. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  3937. intel_crtc->config->pixel_multiplier - 1);
  3938. }
  3939. if (intel_crtc->config->has_pch_encoder) {
  3940. intel_cpu_transcoder_set_m_n(intel_crtc,
  3941. &intel_crtc->config->fdi_m_n, NULL);
  3942. }
  3943. haswell_set_pipeconf(crtc);
  3944. intel_set_pipe_csc(crtc);
  3945. intel_crtc->active = true;
  3946. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3947. for_each_encoder_on_crtc(dev, crtc, encoder)
  3948. if (encoder->pre_enable)
  3949. encoder->pre_enable(encoder);
  3950. if (intel_crtc->config->has_pch_encoder) {
  3951. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3952. true);
  3953. dev_priv->display.fdi_link_train(crtc);
  3954. }
  3955. intel_ddi_enable_pipe_clock(intel_crtc);
  3956. if (IS_SKYLAKE(dev))
  3957. skylake_pfit_enable(intel_crtc);
  3958. else
  3959. ironlake_pfit_enable(intel_crtc);
  3960. /*
  3961. * On ILK+ LUT must be loaded before the pipe is running but with
  3962. * clocks enabled
  3963. */
  3964. intel_crtc_load_lut(crtc);
  3965. intel_ddi_set_pipe_settings(crtc);
  3966. intel_ddi_enable_transcoder_func(crtc);
  3967. intel_update_watermarks(crtc);
  3968. intel_enable_pipe(intel_crtc);
  3969. if (intel_crtc->config->has_pch_encoder)
  3970. lpt_pch_enable(crtc);
  3971. if (intel_crtc->config->dp_encoder_is_mst)
  3972. intel_ddi_set_vc_payload_alloc(crtc, true);
  3973. assert_vblank_disabled(crtc);
  3974. drm_crtc_vblank_on(crtc);
  3975. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3976. encoder->enable(encoder);
  3977. intel_opregion_notify_encoder(encoder, true);
  3978. }
  3979. /* If we change the relative order between pipe/planes enabling, we need
  3980. * to change the workaround. */
  3981. haswell_mode_set_planes_workaround(intel_crtc);
  3982. intel_crtc_enable_planes(crtc);
  3983. }
  3984. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3985. {
  3986. struct drm_device *dev = crtc->base.dev;
  3987. struct drm_i915_private *dev_priv = dev->dev_private;
  3988. int pipe = crtc->pipe;
  3989. /* To avoid upsetting the power well on haswell only disable the pfit if
  3990. * it's in use. The hw state code will make sure we get this right. */
  3991. if (crtc->config->pch_pfit.enabled) {
  3992. I915_WRITE(PS_CTL(pipe), 0);
  3993. I915_WRITE(PS_WIN_POS(pipe), 0);
  3994. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3995. }
  3996. }
  3997. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3998. {
  3999. struct drm_device *dev = crtc->base.dev;
  4000. struct drm_i915_private *dev_priv = dev->dev_private;
  4001. int pipe = crtc->pipe;
  4002. /* To avoid upsetting the power well on haswell only disable the pfit if
  4003. * it's in use. The hw state code will make sure we get this right. */
  4004. if (crtc->config->pch_pfit.enabled) {
  4005. I915_WRITE(PF_CTL(pipe), 0);
  4006. I915_WRITE(PF_WIN_POS(pipe), 0);
  4007. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4008. }
  4009. }
  4010. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4011. {
  4012. struct drm_device *dev = crtc->dev;
  4013. struct drm_i915_private *dev_priv = dev->dev_private;
  4014. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4015. struct intel_encoder *encoder;
  4016. int pipe = intel_crtc->pipe;
  4017. u32 reg, temp;
  4018. if (!intel_crtc->active)
  4019. return;
  4020. intel_crtc_disable_planes(crtc);
  4021. for_each_encoder_on_crtc(dev, crtc, encoder)
  4022. encoder->disable(encoder);
  4023. drm_crtc_vblank_off(crtc);
  4024. assert_vblank_disabled(crtc);
  4025. if (intel_crtc->config->has_pch_encoder)
  4026. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4027. intel_disable_pipe(intel_crtc);
  4028. ironlake_pfit_disable(intel_crtc);
  4029. for_each_encoder_on_crtc(dev, crtc, encoder)
  4030. if (encoder->post_disable)
  4031. encoder->post_disable(encoder);
  4032. if (intel_crtc->config->has_pch_encoder) {
  4033. ironlake_fdi_disable(crtc);
  4034. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4035. if (HAS_PCH_CPT(dev)) {
  4036. /* disable TRANS_DP_CTL */
  4037. reg = TRANS_DP_CTL(pipe);
  4038. temp = I915_READ(reg);
  4039. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4040. TRANS_DP_PORT_SEL_MASK);
  4041. temp |= TRANS_DP_PORT_SEL_NONE;
  4042. I915_WRITE(reg, temp);
  4043. /* disable DPLL_SEL */
  4044. temp = I915_READ(PCH_DPLL_SEL);
  4045. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4046. I915_WRITE(PCH_DPLL_SEL, temp);
  4047. }
  4048. /* disable PCH DPLL */
  4049. intel_disable_shared_dpll(intel_crtc);
  4050. ironlake_fdi_pll_disable(intel_crtc);
  4051. }
  4052. intel_crtc->active = false;
  4053. intel_update_watermarks(crtc);
  4054. mutex_lock(&dev->struct_mutex);
  4055. intel_fbc_update(dev);
  4056. mutex_unlock(&dev->struct_mutex);
  4057. }
  4058. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4059. {
  4060. struct drm_device *dev = crtc->dev;
  4061. struct drm_i915_private *dev_priv = dev->dev_private;
  4062. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4063. struct intel_encoder *encoder;
  4064. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4065. if (!intel_crtc->active)
  4066. return;
  4067. intel_crtc_disable_planes(crtc);
  4068. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4069. intel_opregion_notify_encoder(encoder, false);
  4070. encoder->disable(encoder);
  4071. }
  4072. drm_crtc_vblank_off(crtc);
  4073. assert_vblank_disabled(crtc);
  4074. if (intel_crtc->config->has_pch_encoder)
  4075. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4076. false);
  4077. intel_disable_pipe(intel_crtc);
  4078. if (intel_crtc->config->dp_encoder_is_mst)
  4079. intel_ddi_set_vc_payload_alloc(crtc, false);
  4080. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4081. if (IS_SKYLAKE(dev))
  4082. skylake_pfit_disable(intel_crtc);
  4083. else
  4084. ironlake_pfit_disable(intel_crtc);
  4085. intel_ddi_disable_pipe_clock(intel_crtc);
  4086. if (intel_crtc->config->has_pch_encoder) {
  4087. lpt_disable_pch_transcoder(dev_priv);
  4088. intel_ddi_fdi_disable(crtc);
  4089. }
  4090. for_each_encoder_on_crtc(dev, crtc, encoder)
  4091. if (encoder->post_disable)
  4092. encoder->post_disable(encoder);
  4093. intel_crtc->active = false;
  4094. intel_update_watermarks(crtc);
  4095. mutex_lock(&dev->struct_mutex);
  4096. intel_fbc_update(dev);
  4097. mutex_unlock(&dev->struct_mutex);
  4098. if (intel_crtc_to_shared_dpll(intel_crtc))
  4099. intel_disable_shared_dpll(intel_crtc);
  4100. }
  4101. static void ironlake_crtc_off(struct drm_crtc *crtc)
  4102. {
  4103. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4104. intel_put_shared_dpll(intel_crtc);
  4105. }
  4106. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4107. {
  4108. struct drm_device *dev = crtc->base.dev;
  4109. struct drm_i915_private *dev_priv = dev->dev_private;
  4110. struct intel_crtc_state *pipe_config = crtc->config;
  4111. if (!pipe_config->gmch_pfit.control)
  4112. return;
  4113. /*
  4114. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4115. * according to register description and PRM.
  4116. */
  4117. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4118. assert_pipe_disabled(dev_priv, crtc->pipe);
  4119. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4120. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4121. /* Border color in case we don't scale up to the full screen. Black by
  4122. * default, change to something else for debugging. */
  4123. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4124. }
  4125. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4126. {
  4127. switch (port) {
  4128. case PORT_A:
  4129. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4130. case PORT_B:
  4131. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4132. case PORT_C:
  4133. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4134. case PORT_D:
  4135. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4136. default:
  4137. WARN_ON_ONCE(1);
  4138. return POWER_DOMAIN_PORT_OTHER;
  4139. }
  4140. }
  4141. #define for_each_power_domain(domain, mask) \
  4142. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4143. if ((1 << (domain)) & (mask))
  4144. enum intel_display_power_domain
  4145. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4146. {
  4147. struct drm_device *dev = intel_encoder->base.dev;
  4148. struct intel_digital_port *intel_dig_port;
  4149. switch (intel_encoder->type) {
  4150. case INTEL_OUTPUT_UNKNOWN:
  4151. /* Only DDI platforms should ever use this output type */
  4152. WARN_ON_ONCE(!HAS_DDI(dev));
  4153. case INTEL_OUTPUT_DISPLAYPORT:
  4154. case INTEL_OUTPUT_HDMI:
  4155. case INTEL_OUTPUT_EDP:
  4156. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4157. return port_to_power_domain(intel_dig_port->port);
  4158. case INTEL_OUTPUT_DP_MST:
  4159. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4160. return port_to_power_domain(intel_dig_port->port);
  4161. case INTEL_OUTPUT_ANALOG:
  4162. return POWER_DOMAIN_PORT_CRT;
  4163. case INTEL_OUTPUT_DSI:
  4164. return POWER_DOMAIN_PORT_DSI;
  4165. default:
  4166. return POWER_DOMAIN_PORT_OTHER;
  4167. }
  4168. }
  4169. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4170. {
  4171. struct drm_device *dev = crtc->dev;
  4172. struct intel_encoder *intel_encoder;
  4173. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4174. enum pipe pipe = intel_crtc->pipe;
  4175. unsigned long mask;
  4176. enum transcoder transcoder;
  4177. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4178. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4179. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4180. if (intel_crtc->config->pch_pfit.enabled ||
  4181. intel_crtc->config->pch_pfit.force_thru)
  4182. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4183. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4184. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4185. return mask;
  4186. }
  4187. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4188. {
  4189. struct drm_device *dev = state->dev;
  4190. struct drm_i915_private *dev_priv = dev->dev_private;
  4191. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4192. struct intel_crtc *crtc;
  4193. /*
  4194. * First get all needed power domains, then put all unneeded, to avoid
  4195. * any unnecessary toggling of the power wells.
  4196. */
  4197. for_each_intel_crtc(dev, crtc) {
  4198. enum intel_display_power_domain domain;
  4199. if (!crtc->base.state->enable)
  4200. continue;
  4201. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4202. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4203. intel_display_power_get(dev_priv, domain);
  4204. }
  4205. if (dev_priv->display.modeset_global_resources)
  4206. dev_priv->display.modeset_global_resources(state);
  4207. for_each_intel_crtc(dev, crtc) {
  4208. enum intel_display_power_domain domain;
  4209. for_each_power_domain(domain, crtc->enabled_power_domains)
  4210. intel_display_power_put(dev_priv, domain);
  4211. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4212. }
  4213. intel_display_set_init_power(dev_priv, false);
  4214. }
  4215. /* returns HPLL frequency in kHz */
  4216. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4217. {
  4218. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4219. /* Obtain SKU information */
  4220. mutex_lock(&dev_priv->dpio_lock);
  4221. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4222. CCK_FUSE_HPLL_FREQ_MASK;
  4223. mutex_unlock(&dev_priv->dpio_lock);
  4224. return vco_freq[hpll_freq] * 1000;
  4225. }
  4226. static void vlv_update_cdclk(struct drm_device *dev)
  4227. {
  4228. struct drm_i915_private *dev_priv = dev->dev_private;
  4229. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4230. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4231. dev_priv->vlv_cdclk_freq);
  4232. /*
  4233. * Program the gmbus_freq based on the cdclk frequency.
  4234. * BSpec erroneously claims we should aim for 4MHz, but
  4235. * in fact 1MHz is the correct frequency.
  4236. */
  4237. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4238. }
  4239. /* Adjust CDclk dividers to allow high res or save power if possible */
  4240. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4241. {
  4242. struct drm_i915_private *dev_priv = dev->dev_private;
  4243. u32 val, cmd;
  4244. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4245. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4246. cmd = 2;
  4247. else if (cdclk == 266667)
  4248. cmd = 1;
  4249. else
  4250. cmd = 0;
  4251. mutex_lock(&dev_priv->rps.hw_lock);
  4252. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4253. val &= ~DSPFREQGUAR_MASK;
  4254. val |= (cmd << DSPFREQGUAR_SHIFT);
  4255. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4256. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4257. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4258. 50)) {
  4259. DRM_ERROR("timed out waiting for CDclk change\n");
  4260. }
  4261. mutex_unlock(&dev_priv->rps.hw_lock);
  4262. if (cdclk == 400000) {
  4263. u32 divider;
  4264. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4265. mutex_lock(&dev_priv->dpio_lock);
  4266. /* adjust cdclk divider */
  4267. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4268. val &= ~DISPLAY_FREQUENCY_VALUES;
  4269. val |= divider;
  4270. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4271. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4272. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4273. 50))
  4274. DRM_ERROR("timed out waiting for CDclk change\n");
  4275. mutex_unlock(&dev_priv->dpio_lock);
  4276. }
  4277. mutex_lock(&dev_priv->dpio_lock);
  4278. /* adjust self-refresh exit latency value */
  4279. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4280. val &= ~0x7f;
  4281. /*
  4282. * For high bandwidth configs, we set a higher latency in the bunit
  4283. * so that the core display fetch happens in time to avoid underruns.
  4284. */
  4285. if (cdclk == 400000)
  4286. val |= 4500 / 250; /* 4.5 usec */
  4287. else
  4288. val |= 3000 / 250; /* 3.0 usec */
  4289. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4290. mutex_unlock(&dev_priv->dpio_lock);
  4291. vlv_update_cdclk(dev);
  4292. }
  4293. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4294. {
  4295. struct drm_i915_private *dev_priv = dev->dev_private;
  4296. u32 val, cmd;
  4297. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4298. switch (cdclk) {
  4299. case 333333:
  4300. case 320000:
  4301. case 266667:
  4302. case 200000:
  4303. break;
  4304. default:
  4305. MISSING_CASE(cdclk);
  4306. return;
  4307. }
  4308. /*
  4309. * Specs are full of misinformation, but testing on actual
  4310. * hardware has shown that we just need to write the desired
  4311. * CCK divider into the Punit register.
  4312. */
  4313. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4314. mutex_lock(&dev_priv->rps.hw_lock);
  4315. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4316. val &= ~DSPFREQGUAR_MASK_CHV;
  4317. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4318. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4319. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4320. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4321. 50)) {
  4322. DRM_ERROR("timed out waiting for CDclk change\n");
  4323. }
  4324. mutex_unlock(&dev_priv->rps.hw_lock);
  4325. vlv_update_cdclk(dev);
  4326. }
  4327. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4328. int max_pixclk)
  4329. {
  4330. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4331. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4332. /*
  4333. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4334. * 200MHz
  4335. * 267MHz
  4336. * 320/333MHz (depends on HPLL freq)
  4337. * 400MHz (VLV only)
  4338. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4339. * of the lower bin and adjust if needed.
  4340. *
  4341. * We seem to get an unstable or solid color picture at 200MHz.
  4342. * Not sure what's wrong. For now use 200MHz only when all pipes
  4343. * are off.
  4344. */
  4345. if (!IS_CHERRYVIEW(dev_priv) &&
  4346. max_pixclk > freq_320*limit/100)
  4347. return 400000;
  4348. else if (max_pixclk > 266667*limit/100)
  4349. return freq_320;
  4350. else if (max_pixclk > 0)
  4351. return 266667;
  4352. else
  4353. return 200000;
  4354. }
  4355. /* compute the max pixel clock for new configuration */
  4356. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4357. {
  4358. struct drm_device *dev = dev_priv->dev;
  4359. struct intel_crtc *intel_crtc;
  4360. int max_pixclk = 0;
  4361. for_each_intel_crtc(dev, intel_crtc) {
  4362. if (intel_crtc->new_enabled)
  4363. max_pixclk = max(max_pixclk,
  4364. intel_crtc->new_config->base.adjusted_mode.crtc_clock);
  4365. }
  4366. return max_pixclk;
  4367. }
  4368. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4369. unsigned *prepare_pipes)
  4370. {
  4371. struct drm_i915_private *dev_priv = dev->dev_private;
  4372. struct intel_crtc *intel_crtc;
  4373. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4374. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4375. dev_priv->vlv_cdclk_freq)
  4376. return;
  4377. /* disable/enable all currently active pipes while we change cdclk */
  4378. for_each_intel_crtc(dev, intel_crtc)
  4379. if (intel_crtc->base.state->enable)
  4380. *prepare_pipes |= (1 << intel_crtc->pipe);
  4381. }
  4382. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  4383. {
  4384. unsigned int credits, default_credits;
  4385. if (IS_CHERRYVIEW(dev_priv))
  4386. default_credits = PFI_CREDIT(12);
  4387. else
  4388. default_credits = PFI_CREDIT(8);
  4389. if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  4390. /* CHV suggested value is 31 or 63 */
  4391. if (IS_CHERRYVIEW(dev_priv))
  4392. credits = PFI_CREDIT_31;
  4393. else
  4394. credits = PFI_CREDIT(15);
  4395. } else {
  4396. credits = default_credits;
  4397. }
  4398. /*
  4399. * WA - write default credits before re-programming
  4400. * FIXME: should we also set the resend bit here?
  4401. */
  4402. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4403. default_credits);
  4404. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4405. credits | PFI_CREDIT_RESEND);
  4406. /*
  4407. * FIXME is this guaranteed to clear
  4408. * immediately or should we poll for it?
  4409. */
  4410. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  4411. }
  4412. static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
  4413. {
  4414. struct drm_device *dev = state->dev;
  4415. struct drm_i915_private *dev_priv = dev->dev_private;
  4416. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4417. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4418. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4419. /*
  4420. * FIXME: We can end up here with all power domains off, yet
  4421. * with a CDCLK frequency other than the minimum. To account
  4422. * for this take the PIPE-A power domain, which covers the HW
  4423. * blocks needed for the following programming. This can be
  4424. * removed once it's guaranteed that we get here either with
  4425. * the minimum CDCLK set, or the required power domains
  4426. * enabled.
  4427. */
  4428. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4429. if (IS_CHERRYVIEW(dev))
  4430. cherryview_set_cdclk(dev, req_cdclk);
  4431. else
  4432. valleyview_set_cdclk(dev, req_cdclk);
  4433. vlv_program_pfi_credits(dev_priv);
  4434. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4435. }
  4436. }
  4437. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4438. {
  4439. struct drm_device *dev = crtc->dev;
  4440. struct drm_i915_private *dev_priv = to_i915(dev);
  4441. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4442. struct intel_encoder *encoder;
  4443. int pipe = intel_crtc->pipe;
  4444. bool is_dsi;
  4445. WARN_ON(!crtc->state->enable);
  4446. if (intel_crtc->active)
  4447. return;
  4448. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4449. if (!is_dsi) {
  4450. if (IS_CHERRYVIEW(dev))
  4451. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4452. else
  4453. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4454. }
  4455. if (intel_crtc->config->has_dp_encoder)
  4456. intel_dp_set_m_n(intel_crtc, M1_N1);
  4457. intel_set_pipe_timings(intel_crtc);
  4458. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4459. struct drm_i915_private *dev_priv = dev->dev_private;
  4460. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4461. I915_WRITE(CHV_CANVAS(pipe), 0);
  4462. }
  4463. i9xx_set_pipeconf(intel_crtc);
  4464. intel_crtc->active = true;
  4465. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4466. for_each_encoder_on_crtc(dev, crtc, encoder)
  4467. if (encoder->pre_pll_enable)
  4468. encoder->pre_pll_enable(encoder);
  4469. if (!is_dsi) {
  4470. if (IS_CHERRYVIEW(dev))
  4471. chv_enable_pll(intel_crtc, intel_crtc->config);
  4472. else
  4473. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4474. }
  4475. for_each_encoder_on_crtc(dev, crtc, encoder)
  4476. if (encoder->pre_enable)
  4477. encoder->pre_enable(encoder);
  4478. i9xx_pfit_enable(intel_crtc);
  4479. intel_crtc_load_lut(crtc);
  4480. intel_update_watermarks(crtc);
  4481. intel_enable_pipe(intel_crtc);
  4482. assert_vblank_disabled(crtc);
  4483. drm_crtc_vblank_on(crtc);
  4484. for_each_encoder_on_crtc(dev, crtc, encoder)
  4485. encoder->enable(encoder);
  4486. intel_crtc_enable_planes(crtc);
  4487. /* Underruns don't raise interrupts, so check manually. */
  4488. i9xx_check_fifo_underruns(dev_priv);
  4489. }
  4490. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4491. {
  4492. struct drm_device *dev = crtc->base.dev;
  4493. struct drm_i915_private *dev_priv = dev->dev_private;
  4494. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4495. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4496. }
  4497. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4498. {
  4499. struct drm_device *dev = crtc->dev;
  4500. struct drm_i915_private *dev_priv = to_i915(dev);
  4501. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4502. struct intel_encoder *encoder;
  4503. int pipe = intel_crtc->pipe;
  4504. WARN_ON(!crtc->state->enable);
  4505. if (intel_crtc->active)
  4506. return;
  4507. i9xx_set_pll_dividers(intel_crtc);
  4508. if (intel_crtc->config->has_dp_encoder)
  4509. intel_dp_set_m_n(intel_crtc, M1_N1);
  4510. intel_set_pipe_timings(intel_crtc);
  4511. i9xx_set_pipeconf(intel_crtc);
  4512. intel_crtc->active = true;
  4513. if (!IS_GEN2(dev))
  4514. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4515. for_each_encoder_on_crtc(dev, crtc, encoder)
  4516. if (encoder->pre_enable)
  4517. encoder->pre_enable(encoder);
  4518. i9xx_enable_pll(intel_crtc);
  4519. i9xx_pfit_enable(intel_crtc);
  4520. intel_crtc_load_lut(crtc);
  4521. intel_update_watermarks(crtc);
  4522. intel_enable_pipe(intel_crtc);
  4523. assert_vblank_disabled(crtc);
  4524. drm_crtc_vblank_on(crtc);
  4525. for_each_encoder_on_crtc(dev, crtc, encoder)
  4526. encoder->enable(encoder);
  4527. intel_crtc_enable_planes(crtc);
  4528. /*
  4529. * Gen2 reports pipe underruns whenever all planes are disabled.
  4530. * So don't enable underrun reporting before at least some planes
  4531. * are enabled.
  4532. * FIXME: Need to fix the logic to work when we turn off all planes
  4533. * but leave the pipe running.
  4534. */
  4535. if (IS_GEN2(dev))
  4536. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4537. /* Underruns don't raise interrupts, so check manually. */
  4538. i9xx_check_fifo_underruns(dev_priv);
  4539. }
  4540. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4541. {
  4542. struct drm_device *dev = crtc->base.dev;
  4543. struct drm_i915_private *dev_priv = dev->dev_private;
  4544. if (!crtc->config->gmch_pfit.control)
  4545. return;
  4546. assert_pipe_disabled(dev_priv, crtc->pipe);
  4547. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4548. I915_READ(PFIT_CONTROL));
  4549. I915_WRITE(PFIT_CONTROL, 0);
  4550. }
  4551. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4552. {
  4553. struct drm_device *dev = crtc->dev;
  4554. struct drm_i915_private *dev_priv = dev->dev_private;
  4555. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4556. struct intel_encoder *encoder;
  4557. int pipe = intel_crtc->pipe;
  4558. if (!intel_crtc->active)
  4559. return;
  4560. /*
  4561. * Gen2 reports pipe underruns whenever all planes are disabled.
  4562. * So diasble underrun reporting before all the planes get disabled.
  4563. * FIXME: Need to fix the logic to work when we turn off all planes
  4564. * but leave the pipe running.
  4565. */
  4566. if (IS_GEN2(dev))
  4567. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4568. /*
  4569. * Vblank time updates from the shadow to live plane control register
  4570. * are blocked if the memory self-refresh mode is active at that
  4571. * moment. So to make sure the plane gets truly disabled, disable
  4572. * first the self-refresh mode. The self-refresh enable bit in turn
  4573. * will be checked/applied by the HW only at the next frame start
  4574. * event which is after the vblank start event, so we need to have a
  4575. * wait-for-vblank between disabling the plane and the pipe.
  4576. */
  4577. intel_set_memory_cxsr(dev_priv, false);
  4578. intel_crtc_disable_planes(crtc);
  4579. /*
  4580. * On gen2 planes are double buffered but the pipe isn't, so we must
  4581. * wait for planes to fully turn off before disabling the pipe.
  4582. * We also need to wait on all gmch platforms because of the
  4583. * self-refresh mode constraint explained above.
  4584. */
  4585. intel_wait_for_vblank(dev, pipe);
  4586. for_each_encoder_on_crtc(dev, crtc, encoder)
  4587. encoder->disable(encoder);
  4588. drm_crtc_vblank_off(crtc);
  4589. assert_vblank_disabled(crtc);
  4590. intel_disable_pipe(intel_crtc);
  4591. i9xx_pfit_disable(intel_crtc);
  4592. for_each_encoder_on_crtc(dev, crtc, encoder)
  4593. if (encoder->post_disable)
  4594. encoder->post_disable(encoder);
  4595. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4596. if (IS_CHERRYVIEW(dev))
  4597. chv_disable_pll(dev_priv, pipe);
  4598. else if (IS_VALLEYVIEW(dev))
  4599. vlv_disable_pll(dev_priv, pipe);
  4600. else
  4601. i9xx_disable_pll(intel_crtc);
  4602. }
  4603. if (!IS_GEN2(dev))
  4604. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4605. intel_crtc->active = false;
  4606. intel_update_watermarks(crtc);
  4607. mutex_lock(&dev->struct_mutex);
  4608. intel_fbc_update(dev);
  4609. mutex_unlock(&dev->struct_mutex);
  4610. }
  4611. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4612. {
  4613. }
  4614. /* Master function to enable/disable CRTC and corresponding power wells */
  4615. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4616. {
  4617. struct drm_device *dev = crtc->dev;
  4618. struct drm_i915_private *dev_priv = dev->dev_private;
  4619. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4620. enum intel_display_power_domain domain;
  4621. unsigned long domains;
  4622. if (enable) {
  4623. if (!intel_crtc->active) {
  4624. domains = get_crtc_power_domains(crtc);
  4625. for_each_power_domain(domain, domains)
  4626. intel_display_power_get(dev_priv, domain);
  4627. intel_crtc->enabled_power_domains = domains;
  4628. dev_priv->display.crtc_enable(crtc);
  4629. }
  4630. } else {
  4631. if (intel_crtc->active) {
  4632. dev_priv->display.crtc_disable(crtc);
  4633. domains = intel_crtc->enabled_power_domains;
  4634. for_each_power_domain(domain, domains)
  4635. intel_display_power_put(dev_priv, domain);
  4636. intel_crtc->enabled_power_domains = 0;
  4637. }
  4638. }
  4639. }
  4640. /**
  4641. * Sets the power management mode of the pipe and plane.
  4642. */
  4643. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4644. {
  4645. struct drm_device *dev = crtc->dev;
  4646. struct intel_encoder *intel_encoder;
  4647. bool enable = false;
  4648. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4649. enable |= intel_encoder->connectors_active;
  4650. intel_crtc_control(crtc, enable);
  4651. }
  4652. static void intel_crtc_disable(struct drm_crtc *crtc)
  4653. {
  4654. struct drm_device *dev = crtc->dev;
  4655. struct drm_connector *connector;
  4656. struct drm_i915_private *dev_priv = dev->dev_private;
  4657. /* crtc should still be enabled when we disable it. */
  4658. WARN_ON(!crtc->state->enable);
  4659. dev_priv->display.crtc_disable(crtc);
  4660. dev_priv->display.off(crtc);
  4661. crtc->primary->funcs->disable_plane(crtc->primary);
  4662. /* Update computed state. */
  4663. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4664. if (!connector->encoder || !connector->encoder->crtc)
  4665. continue;
  4666. if (connector->encoder->crtc != crtc)
  4667. continue;
  4668. connector->dpms = DRM_MODE_DPMS_OFF;
  4669. to_intel_encoder(connector->encoder)->connectors_active = false;
  4670. }
  4671. }
  4672. void intel_encoder_destroy(struct drm_encoder *encoder)
  4673. {
  4674. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4675. drm_encoder_cleanup(encoder);
  4676. kfree(intel_encoder);
  4677. }
  4678. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4679. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4680. * state of the entire output pipe. */
  4681. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4682. {
  4683. if (mode == DRM_MODE_DPMS_ON) {
  4684. encoder->connectors_active = true;
  4685. intel_crtc_update_dpms(encoder->base.crtc);
  4686. } else {
  4687. encoder->connectors_active = false;
  4688. intel_crtc_update_dpms(encoder->base.crtc);
  4689. }
  4690. }
  4691. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4692. * internal consistency). */
  4693. static void intel_connector_check_state(struct intel_connector *connector)
  4694. {
  4695. if (connector->get_hw_state(connector)) {
  4696. struct intel_encoder *encoder = connector->encoder;
  4697. struct drm_crtc *crtc;
  4698. bool encoder_enabled;
  4699. enum pipe pipe;
  4700. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4701. connector->base.base.id,
  4702. connector->base.name);
  4703. /* there is no real hw state for MST connectors */
  4704. if (connector->mst_port)
  4705. return;
  4706. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4707. "wrong connector dpms state\n");
  4708. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  4709. "active connector not linked to encoder\n");
  4710. if (encoder) {
  4711. I915_STATE_WARN(!encoder->connectors_active,
  4712. "encoder->connectors_active not set\n");
  4713. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4714. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  4715. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  4716. return;
  4717. crtc = encoder->base.crtc;
  4718. I915_STATE_WARN(!crtc->state->enable,
  4719. "crtc not enabled\n");
  4720. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4721. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  4722. "encoder active on the wrong pipe\n");
  4723. }
  4724. }
  4725. }
  4726. int intel_connector_init(struct intel_connector *connector)
  4727. {
  4728. struct drm_connector_state *connector_state;
  4729. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  4730. if (!connector_state)
  4731. return -ENOMEM;
  4732. connector->base.state = connector_state;
  4733. return 0;
  4734. }
  4735. struct intel_connector *intel_connector_alloc(void)
  4736. {
  4737. struct intel_connector *connector;
  4738. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  4739. if (!connector)
  4740. return NULL;
  4741. if (intel_connector_init(connector) < 0) {
  4742. kfree(connector);
  4743. return NULL;
  4744. }
  4745. return connector;
  4746. }
  4747. /* Even simpler default implementation, if there's really no special case to
  4748. * consider. */
  4749. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4750. {
  4751. /* All the simple cases only support two dpms states. */
  4752. if (mode != DRM_MODE_DPMS_ON)
  4753. mode = DRM_MODE_DPMS_OFF;
  4754. if (mode == connector->dpms)
  4755. return;
  4756. connector->dpms = mode;
  4757. /* Only need to change hw state when actually enabled */
  4758. if (connector->encoder)
  4759. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4760. intel_modeset_check_state(connector->dev);
  4761. }
  4762. /* Simple connector->get_hw_state implementation for encoders that support only
  4763. * one connector and no cloning and hence the encoder state determines the state
  4764. * of the connector. */
  4765. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4766. {
  4767. enum pipe pipe = 0;
  4768. struct intel_encoder *encoder = connector->encoder;
  4769. return encoder->get_hw_state(encoder, &pipe);
  4770. }
  4771. static int pipe_required_fdi_lanes(struct drm_device *dev, enum pipe pipe)
  4772. {
  4773. struct intel_crtc *crtc =
  4774. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  4775. if (crtc->base.state->enable &&
  4776. crtc->config->has_pch_encoder)
  4777. return crtc->config->fdi_lanes;
  4778. return 0;
  4779. }
  4780. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4781. struct intel_crtc_state *pipe_config)
  4782. {
  4783. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4784. pipe_name(pipe), pipe_config->fdi_lanes);
  4785. if (pipe_config->fdi_lanes > 4) {
  4786. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4787. pipe_name(pipe), pipe_config->fdi_lanes);
  4788. return false;
  4789. }
  4790. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4791. if (pipe_config->fdi_lanes > 2) {
  4792. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4793. pipe_config->fdi_lanes);
  4794. return false;
  4795. } else {
  4796. return true;
  4797. }
  4798. }
  4799. if (INTEL_INFO(dev)->num_pipes == 2)
  4800. return true;
  4801. /* Ivybridge 3 pipe is really complicated */
  4802. switch (pipe) {
  4803. case PIPE_A:
  4804. return true;
  4805. case PIPE_B:
  4806. if (pipe_config->fdi_lanes > 2 &&
  4807. pipe_required_fdi_lanes(dev, PIPE_C) > 0) {
  4808. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4809. pipe_name(pipe), pipe_config->fdi_lanes);
  4810. return false;
  4811. }
  4812. return true;
  4813. case PIPE_C:
  4814. if (pipe_config->fdi_lanes > 2) {
  4815. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  4816. pipe_name(pipe), pipe_config->fdi_lanes);
  4817. return false;
  4818. }
  4819. if (pipe_required_fdi_lanes(dev, PIPE_B) > 2) {
  4820. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4821. return false;
  4822. }
  4823. return true;
  4824. default:
  4825. BUG();
  4826. }
  4827. }
  4828. #define RETRY 1
  4829. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4830. struct intel_crtc_state *pipe_config)
  4831. {
  4832. struct drm_device *dev = intel_crtc->base.dev;
  4833. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4834. int lane, link_bw, fdi_dotclock;
  4835. bool setup_ok, needs_recompute = false;
  4836. retry:
  4837. /* FDI is a binary signal running at ~2.7GHz, encoding
  4838. * each output octet as 10 bits. The actual frequency
  4839. * is stored as a divider into a 100MHz clock, and the
  4840. * mode pixel clock is stored in units of 1KHz.
  4841. * Hence the bw of each lane in terms of the mode signal
  4842. * is:
  4843. */
  4844. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4845. fdi_dotclock = adjusted_mode->crtc_clock;
  4846. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4847. pipe_config->pipe_bpp);
  4848. pipe_config->fdi_lanes = lane;
  4849. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4850. link_bw, &pipe_config->fdi_m_n);
  4851. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4852. intel_crtc->pipe, pipe_config);
  4853. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4854. pipe_config->pipe_bpp -= 2*3;
  4855. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4856. pipe_config->pipe_bpp);
  4857. needs_recompute = true;
  4858. pipe_config->bw_constrained = true;
  4859. goto retry;
  4860. }
  4861. if (needs_recompute)
  4862. return RETRY;
  4863. return setup_ok ? 0 : -EINVAL;
  4864. }
  4865. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4866. struct intel_crtc_state *pipe_config)
  4867. {
  4868. pipe_config->ips_enabled = i915.enable_ips &&
  4869. hsw_crtc_supports_ips(crtc) &&
  4870. pipe_config->pipe_bpp <= 24;
  4871. }
  4872. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4873. struct intel_crtc_state *pipe_config)
  4874. {
  4875. struct drm_device *dev = crtc->base.dev;
  4876. struct drm_i915_private *dev_priv = dev->dev_private;
  4877. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4878. /* FIXME should check pixel clock limits on all platforms */
  4879. if (INTEL_INFO(dev)->gen < 4) {
  4880. int clock_limit =
  4881. dev_priv->display.get_display_clock_speed(dev);
  4882. /*
  4883. * Enable pixel doubling when the dot clock
  4884. * is > 90% of the (display) core speed.
  4885. *
  4886. * GDG double wide on either pipe,
  4887. * otherwise pipe A only.
  4888. */
  4889. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4890. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4891. clock_limit *= 2;
  4892. pipe_config->double_wide = true;
  4893. }
  4894. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4895. return -EINVAL;
  4896. }
  4897. /*
  4898. * Pipe horizontal size must be even in:
  4899. * - DVO ganged mode
  4900. * - LVDS dual channel mode
  4901. * - Double wide pipe
  4902. */
  4903. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  4904. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4905. pipe_config->pipe_src_w &= ~1;
  4906. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4907. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4908. */
  4909. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4910. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4911. return -EINVAL;
  4912. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4913. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4914. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4915. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4916. * for lvds. */
  4917. pipe_config->pipe_bpp = 8*3;
  4918. }
  4919. if (HAS_IPS(dev))
  4920. hsw_compute_ips_config(crtc, pipe_config);
  4921. if (pipe_config->has_pch_encoder)
  4922. return ironlake_fdi_compute_config(crtc, pipe_config);
  4923. return 0;
  4924. }
  4925. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4926. {
  4927. struct drm_i915_private *dev_priv = dev->dev_private;
  4928. u32 val;
  4929. int divider;
  4930. if (dev_priv->hpll_freq == 0)
  4931. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4932. mutex_lock(&dev_priv->dpio_lock);
  4933. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4934. mutex_unlock(&dev_priv->dpio_lock);
  4935. divider = val & DISPLAY_FREQUENCY_VALUES;
  4936. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4937. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4938. "cdclk change in progress\n");
  4939. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4940. }
  4941. static int i945_get_display_clock_speed(struct drm_device *dev)
  4942. {
  4943. return 400000;
  4944. }
  4945. static int i915_get_display_clock_speed(struct drm_device *dev)
  4946. {
  4947. return 333000;
  4948. }
  4949. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4950. {
  4951. return 200000;
  4952. }
  4953. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4954. {
  4955. u16 gcfgc = 0;
  4956. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4957. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4958. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4959. return 267000;
  4960. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4961. return 333000;
  4962. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4963. return 444000;
  4964. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4965. return 200000;
  4966. default:
  4967. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4968. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4969. return 133000;
  4970. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4971. return 167000;
  4972. }
  4973. }
  4974. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4975. {
  4976. u16 gcfgc = 0;
  4977. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4978. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4979. return 133000;
  4980. else {
  4981. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4982. case GC_DISPLAY_CLOCK_333_MHZ:
  4983. return 333000;
  4984. default:
  4985. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4986. return 190000;
  4987. }
  4988. }
  4989. }
  4990. static int i865_get_display_clock_speed(struct drm_device *dev)
  4991. {
  4992. return 266000;
  4993. }
  4994. static int i855_get_display_clock_speed(struct drm_device *dev)
  4995. {
  4996. u16 hpllcc = 0;
  4997. /* Assume that the hardware is in the high speed state. This
  4998. * should be the default.
  4999. */
  5000. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5001. case GC_CLOCK_133_200:
  5002. case GC_CLOCK_100_200:
  5003. return 200000;
  5004. case GC_CLOCK_166_250:
  5005. return 250000;
  5006. case GC_CLOCK_100_133:
  5007. return 133000;
  5008. }
  5009. /* Shouldn't happen */
  5010. return 0;
  5011. }
  5012. static int i830_get_display_clock_speed(struct drm_device *dev)
  5013. {
  5014. return 133000;
  5015. }
  5016. static void
  5017. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5018. {
  5019. while (*num > DATA_LINK_M_N_MASK ||
  5020. *den > DATA_LINK_M_N_MASK) {
  5021. *num >>= 1;
  5022. *den >>= 1;
  5023. }
  5024. }
  5025. static void compute_m_n(unsigned int m, unsigned int n,
  5026. uint32_t *ret_m, uint32_t *ret_n)
  5027. {
  5028. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5029. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5030. intel_reduce_m_n_ratio(ret_m, ret_n);
  5031. }
  5032. void
  5033. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5034. int pixel_clock, int link_clock,
  5035. struct intel_link_m_n *m_n)
  5036. {
  5037. m_n->tu = 64;
  5038. compute_m_n(bits_per_pixel * pixel_clock,
  5039. link_clock * nlanes * 8,
  5040. &m_n->gmch_m, &m_n->gmch_n);
  5041. compute_m_n(pixel_clock, link_clock,
  5042. &m_n->link_m, &m_n->link_n);
  5043. }
  5044. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5045. {
  5046. if (i915.panel_use_ssc >= 0)
  5047. return i915.panel_use_ssc != 0;
  5048. return dev_priv->vbt.lvds_use_ssc
  5049. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5050. }
  5051. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5052. int num_connectors)
  5053. {
  5054. struct drm_device *dev = crtc_state->base.crtc->dev;
  5055. struct drm_i915_private *dev_priv = dev->dev_private;
  5056. int refclk;
  5057. WARN_ON(!crtc_state->base.state);
  5058. if (IS_VALLEYVIEW(dev)) {
  5059. refclk = 100000;
  5060. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5061. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5062. refclk = dev_priv->vbt.lvds_ssc_freq;
  5063. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5064. } else if (!IS_GEN2(dev)) {
  5065. refclk = 96000;
  5066. } else {
  5067. refclk = 48000;
  5068. }
  5069. return refclk;
  5070. }
  5071. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5072. {
  5073. return (1 << dpll->n) << 16 | dpll->m2;
  5074. }
  5075. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5076. {
  5077. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5078. }
  5079. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5080. struct intel_crtc_state *crtc_state,
  5081. intel_clock_t *reduced_clock)
  5082. {
  5083. struct drm_device *dev = crtc->base.dev;
  5084. u32 fp, fp2 = 0;
  5085. if (IS_PINEVIEW(dev)) {
  5086. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5087. if (reduced_clock)
  5088. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5089. } else {
  5090. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5091. if (reduced_clock)
  5092. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5093. }
  5094. crtc_state->dpll_hw_state.fp0 = fp;
  5095. crtc->lowfreq_avail = false;
  5096. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5097. reduced_clock) {
  5098. crtc_state->dpll_hw_state.fp1 = fp2;
  5099. crtc->lowfreq_avail = true;
  5100. } else {
  5101. crtc_state->dpll_hw_state.fp1 = fp;
  5102. }
  5103. }
  5104. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5105. pipe)
  5106. {
  5107. u32 reg_val;
  5108. /*
  5109. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5110. * and set it to a reasonable value instead.
  5111. */
  5112. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5113. reg_val &= 0xffffff00;
  5114. reg_val |= 0x00000030;
  5115. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5116. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5117. reg_val &= 0x8cffffff;
  5118. reg_val = 0x8c000000;
  5119. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5120. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5121. reg_val &= 0xffffff00;
  5122. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5123. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5124. reg_val &= 0x00ffffff;
  5125. reg_val |= 0xb0000000;
  5126. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5127. }
  5128. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5129. struct intel_link_m_n *m_n)
  5130. {
  5131. struct drm_device *dev = crtc->base.dev;
  5132. struct drm_i915_private *dev_priv = dev->dev_private;
  5133. int pipe = crtc->pipe;
  5134. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5135. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5136. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5137. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5138. }
  5139. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5140. struct intel_link_m_n *m_n,
  5141. struct intel_link_m_n *m2_n2)
  5142. {
  5143. struct drm_device *dev = crtc->base.dev;
  5144. struct drm_i915_private *dev_priv = dev->dev_private;
  5145. int pipe = crtc->pipe;
  5146. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5147. if (INTEL_INFO(dev)->gen >= 5) {
  5148. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5149. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5150. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5151. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5152. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5153. * for gen < 8) and if DRRS is supported (to make sure the
  5154. * registers are not unnecessarily accessed).
  5155. */
  5156. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  5157. crtc->config->has_drrs) {
  5158. I915_WRITE(PIPE_DATA_M2(transcoder),
  5159. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5160. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5161. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5162. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5163. }
  5164. } else {
  5165. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5166. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5167. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5168. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5169. }
  5170. }
  5171. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5172. {
  5173. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5174. if (m_n == M1_N1) {
  5175. dp_m_n = &crtc->config->dp_m_n;
  5176. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5177. } else if (m_n == M2_N2) {
  5178. /*
  5179. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5180. * needs to be programmed into M1_N1.
  5181. */
  5182. dp_m_n = &crtc->config->dp_m2_n2;
  5183. } else {
  5184. DRM_ERROR("Unsupported divider value\n");
  5185. return;
  5186. }
  5187. if (crtc->config->has_pch_encoder)
  5188. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5189. else
  5190. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5191. }
  5192. static void vlv_update_pll(struct intel_crtc *crtc,
  5193. struct intel_crtc_state *pipe_config)
  5194. {
  5195. u32 dpll, dpll_md;
  5196. /*
  5197. * Enable DPIO clock input. We should never disable the reference
  5198. * clock for pipe B, since VGA hotplug / manual detection depends
  5199. * on it.
  5200. */
  5201. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  5202. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  5203. /* We should never disable this, set it here for state tracking */
  5204. if (crtc->pipe == PIPE_B)
  5205. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5206. dpll |= DPLL_VCO_ENABLE;
  5207. pipe_config->dpll_hw_state.dpll = dpll;
  5208. dpll_md = (pipe_config->pixel_multiplier - 1)
  5209. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5210. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  5211. }
  5212. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5213. const struct intel_crtc_state *pipe_config)
  5214. {
  5215. struct drm_device *dev = crtc->base.dev;
  5216. struct drm_i915_private *dev_priv = dev->dev_private;
  5217. int pipe = crtc->pipe;
  5218. u32 mdiv;
  5219. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5220. u32 coreclk, reg_val;
  5221. mutex_lock(&dev_priv->dpio_lock);
  5222. bestn = pipe_config->dpll.n;
  5223. bestm1 = pipe_config->dpll.m1;
  5224. bestm2 = pipe_config->dpll.m2;
  5225. bestp1 = pipe_config->dpll.p1;
  5226. bestp2 = pipe_config->dpll.p2;
  5227. /* See eDP HDMI DPIO driver vbios notes doc */
  5228. /* PLL B needs special handling */
  5229. if (pipe == PIPE_B)
  5230. vlv_pllb_recal_opamp(dev_priv, pipe);
  5231. /* Set up Tx target for periodic Rcomp update */
  5232. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5233. /* Disable target IRef on PLL */
  5234. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5235. reg_val &= 0x00ffffff;
  5236. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5237. /* Disable fast lock */
  5238. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5239. /* Set idtafcrecal before PLL is enabled */
  5240. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5241. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5242. mdiv |= ((bestn << DPIO_N_SHIFT));
  5243. mdiv |= (1 << DPIO_K_SHIFT);
  5244. /*
  5245. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5246. * but we don't support that).
  5247. * Note: don't use the DAC post divider as it seems unstable.
  5248. */
  5249. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5250. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5251. mdiv |= DPIO_ENABLE_CALIBRATION;
  5252. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5253. /* Set HBR and RBR LPF coefficients */
  5254. if (pipe_config->port_clock == 162000 ||
  5255. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  5256. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  5257. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5258. 0x009f0003);
  5259. else
  5260. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5261. 0x00d0000f);
  5262. if (pipe_config->has_dp_encoder) {
  5263. /* Use SSC source */
  5264. if (pipe == PIPE_A)
  5265. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5266. 0x0df40000);
  5267. else
  5268. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5269. 0x0df70000);
  5270. } else { /* HDMI or VGA */
  5271. /* Use bend source */
  5272. if (pipe == PIPE_A)
  5273. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5274. 0x0df70000);
  5275. else
  5276. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5277. 0x0df40000);
  5278. }
  5279. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5280. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5281. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5282. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5283. coreclk |= 0x01000000;
  5284. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5285. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5286. mutex_unlock(&dev_priv->dpio_lock);
  5287. }
  5288. static void chv_update_pll(struct intel_crtc *crtc,
  5289. struct intel_crtc_state *pipe_config)
  5290. {
  5291. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5292. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5293. DPLL_VCO_ENABLE;
  5294. if (crtc->pipe != PIPE_A)
  5295. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5296. pipe_config->dpll_hw_state.dpll_md =
  5297. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5298. }
  5299. static void chv_prepare_pll(struct intel_crtc *crtc,
  5300. const struct intel_crtc_state *pipe_config)
  5301. {
  5302. struct drm_device *dev = crtc->base.dev;
  5303. struct drm_i915_private *dev_priv = dev->dev_private;
  5304. int pipe = crtc->pipe;
  5305. int dpll_reg = DPLL(crtc->pipe);
  5306. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5307. u32 loopfilter, tribuf_calcntr;
  5308. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5309. u32 dpio_val;
  5310. int vco;
  5311. bestn = pipe_config->dpll.n;
  5312. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5313. bestm1 = pipe_config->dpll.m1;
  5314. bestm2 = pipe_config->dpll.m2 >> 22;
  5315. bestp1 = pipe_config->dpll.p1;
  5316. bestp2 = pipe_config->dpll.p2;
  5317. vco = pipe_config->dpll.vco;
  5318. dpio_val = 0;
  5319. loopfilter = 0;
  5320. /*
  5321. * Enable Refclk and SSC
  5322. */
  5323. I915_WRITE(dpll_reg,
  5324. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5325. mutex_lock(&dev_priv->dpio_lock);
  5326. /* p1 and p2 divider */
  5327. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5328. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5329. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5330. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5331. 1 << DPIO_CHV_K_DIV_SHIFT);
  5332. /* Feedback post-divider - m2 */
  5333. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5334. /* Feedback refclk divider - n and m1 */
  5335. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5336. DPIO_CHV_M1_DIV_BY_2 |
  5337. 1 << DPIO_CHV_N_DIV_SHIFT);
  5338. /* M2 fraction division */
  5339. if (bestm2_frac)
  5340. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5341. /* M2 fraction division enable */
  5342. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5343. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5344. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5345. if (bestm2_frac)
  5346. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5347. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5348. /* Program digital lock detect threshold */
  5349. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5350. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5351. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5352. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5353. if (!bestm2_frac)
  5354. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5355. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5356. /* Loop filter */
  5357. if (vco == 5400000) {
  5358. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5359. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5360. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5361. tribuf_calcntr = 0x9;
  5362. } else if (vco <= 6200000) {
  5363. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5364. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5365. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5366. tribuf_calcntr = 0x9;
  5367. } else if (vco <= 6480000) {
  5368. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5369. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5370. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5371. tribuf_calcntr = 0x8;
  5372. } else {
  5373. /* Not supported. Apply the same limits as in the max case */
  5374. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5375. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5376. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5377. tribuf_calcntr = 0;
  5378. }
  5379. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5380. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5381. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5382. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5383. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5384. /* AFC Recal */
  5385. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5386. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5387. DPIO_AFC_RECAL);
  5388. mutex_unlock(&dev_priv->dpio_lock);
  5389. }
  5390. /**
  5391. * vlv_force_pll_on - forcibly enable just the PLL
  5392. * @dev_priv: i915 private structure
  5393. * @pipe: pipe PLL to enable
  5394. * @dpll: PLL configuration
  5395. *
  5396. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5397. * in cases where we need the PLL enabled even when @pipe is not going to
  5398. * be enabled.
  5399. */
  5400. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5401. const struct dpll *dpll)
  5402. {
  5403. struct intel_crtc *crtc =
  5404. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5405. struct intel_crtc_state pipe_config = {
  5406. .base.crtc = &crtc->base,
  5407. .pixel_multiplier = 1,
  5408. .dpll = *dpll,
  5409. };
  5410. if (IS_CHERRYVIEW(dev)) {
  5411. chv_update_pll(crtc, &pipe_config);
  5412. chv_prepare_pll(crtc, &pipe_config);
  5413. chv_enable_pll(crtc, &pipe_config);
  5414. } else {
  5415. vlv_update_pll(crtc, &pipe_config);
  5416. vlv_prepare_pll(crtc, &pipe_config);
  5417. vlv_enable_pll(crtc, &pipe_config);
  5418. }
  5419. }
  5420. /**
  5421. * vlv_force_pll_off - forcibly disable just the PLL
  5422. * @dev_priv: i915 private structure
  5423. * @pipe: pipe PLL to disable
  5424. *
  5425. * Disable the PLL for @pipe. To be used in cases where we need
  5426. * the PLL enabled even when @pipe is not going to be enabled.
  5427. */
  5428. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5429. {
  5430. if (IS_CHERRYVIEW(dev))
  5431. chv_disable_pll(to_i915(dev), pipe);
  5432. else
  5433. vlv_disable_pll(to_i915(dev), pipe);
  5434. }
  5435. static void i9xx_update_pll(struct intel_crtc *crtc,
  5436. struct intel_crtc_state *crtc_state,
  5437. intel_clock_t *reduced_clock,
  5438. int num_connectors)
  5439. {
  5440. struct drm_device *dev = crtc->base.dev;
  5441. struct drm_i915_private *dev_priv = dev->dev_private;
  5442. u32 dpll;
  5443. bool is_sdvo;
  5444. struct dpll *clock = &crtc_state->dpll;
  5445. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5446. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5447. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  5448. dpll = DPLL_VGA_MODE_DIS;
  5449. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  5450. dpll |= DPLLB_MODE_LVDS;
  5451. else
  5452. dpll |= DPLLB_MODE_DAC_SERIAL;
  5453. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5454. dpll |= (crtc_state->pixel_multiplier - 1)
  5455. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5456. }
  5457. if (is_sdvo)
  5458. dpll |= DPLL_SDVO_HIGH_SPEED;
  5459. if (crtc_state->has_dp_encoder)
  5460. dpll |= DPLL_SDVO_HIGH_SPEED;
  5461. /* compute bitmask from p1 value */
  5462. if (IS_PINEVIEW(dev))
  5463. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5464. else {
  5465. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5466. if (IS_G4X(dev) && reduced_clock)
  5467. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5468. }
  5469. switch (clock->p2) {
  5470. case 5:
  5471. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5472. break;
  5473. case 7:
  5474. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5475. break;
  5476. case 10:
  5477. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5478. break;
  5479. case 14:
  5480. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5481. break;
  5482. }
  5483. if (INTEL_INFO(dev)->gen >= 4)
  5484. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5485. if (crtc_state->sdvo_tv_clock)
  5486. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5487. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5488. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5489. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5490. else
  5491. dpll |= PLL_REF_INPUT_DREFCLK;
  5492. dpll |= DPLL_VCO_ENABLE;
  5493. crtc_state->dpll_hw_state.dpll = dpll;
  5494. if (INTEL_INFO(dev)->gen >= 4) {
  5495. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5496. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5497. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5498. }
  5499. }
  5500. static void i8xx_update_pll(struct intel_crtc *crtc,
  5501. struct intel_crtc_state *crtc_state,
  5502. intel_clock_t *reduced_clock,
  5503. int num_connectors)
  5504. {
  5505. struct drm_device *dev = crtc->base.dev;
  5506. struct drm_i915_private *dev_priv = dev->dev_private;
  5507. u32 dpll;
  5508. struct dpll *clock = &crtc_state->dpll;
  5509. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5510. dpll = DPLL_VGA_MODE_DIS;
  5511. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5512. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5513. } else {
  5514. if (clock->p1 == 2)
  5515. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5516. else
  5517. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5518. if (clock->p2 == 4)
  5519. dpll |= PLL_P2_DIVIDE_BY_4;
  5520. }
  5521. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  5522. dpll |= DPLL_DVO_2X_MODE;
  5523. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5524. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5525. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5526. else
  5527. dpll |= PLL_REF_INPUT_DREFCLK;
  5528. dpll |= DPLL_VCO_ENABLE;
  5529. crtc_state->dpll_hw_state.dpll = dpll;
  5530. }
  5531. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5532. {
  5533. struct drm_device *dev = intel_crtc->base.dev;
  5534. struct drm_i915_private *dev_priv = dev->dev_private;
  5535. enum pipe pipe = intel_crtc->pipe;
  5536. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5537. struct drm_display_mode *adjusted_mode =
  5538. &intel_crtc->config->base.adjusted_mode;
  5539. uint32_t crtc_vtotal, crtc_vblank_end;
  5540. int vsyncshift = 0;
  5541. /* We need to be careful not to changed the adjusted mode, for otherwise
  5542. * the hw state checker will get angry at the mismatch. */
  5543. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5544. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5545. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5546. /* the chip adds 2 halflines automatically */
  5547. crtc_vtotal -= 1;
  5548. crtc_vblank_end -= 1;
  5549. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5550. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5551. else
  5552. vsyncshift = adjusted_mode->crtc_hsync_start -
  5553. adjusted_mode->crtc_htotal / 2;
  5554. if (vsyncshift < 0)
  5555. vsyncshift += adjusted_mode->crtc_htotal;
  5556. }
  5557. if (INTEL_INFO(dev)->gen > 3)
  5558. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5559. I915_WRITE(HTOTAL(cpu_transcoder),
  5560. (adjusted_mode->crtc_hdisplay - 1) |
  5561. ((adjusted_mode->crtc_htotal - 1) << 16));
  5562. I915_WRITE(HBLANK(cpu_transcoder),
  5563. (adjusted_mode->crtc_hblank_start - 1) |
  5564. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5565. I915_WRITE(HSYNC(cpu_transcoder),
  5566. (adjusted_mode->crtc_hsync_start - 1) |
  5567. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5568. I915_WRITE(VTOTAL(cpu_transcoder),
  5569. (adjusted_mode->crtc_vdisplay - 1) |
  5570. ((crtc_vtotal - 1) << 16));
  5571. I915_WRITE(VBLANK(cpu_transcoder),
  5572. (adjusted_mode->crtc_vblank_start - 1) |
  5573. ((crtc_vblank_end - 1) << 16));
  5574. I915_WRITE(VSYNC(cpu_transcoder),
  5575. (adjusted_mode->crtc_vsync_start - 1) |
  5576. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5577. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5578. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5579. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5580. * bits. */
  5581. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5582. (pipe == PIPE_B || pipe == PIPE_C))
  5583. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5584. /* pipesrc controls the size that is scaled from, which should
  5585. * always be the user's requested size.
  5586. */
  5587. I915_WRITE(PIPESRC(pipe),
  5588. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5589. (intel_crtc->config->pipe_src_h - 1));
  5590. }
  5591. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5592. struct intel_crtc_state *pipe_config)
  5593. {
  5594. struct drm_device *dev = crtc->base.dev;
  5595. struct drm_i915_private *dev_priv = dev->dev_private;
  5596. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5597. uint32_t tmp;
  5598. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5599. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5600. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5601. tmp = I915_READ(HBLANK(cpu_transcoder));
  5602. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5603. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5604. tmp = I915_READ(HSYNC(cpu_transcoder));
  5605. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5606. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5607. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5608. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5609. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5610. tmp = I915_READ(VBLANK(cpu_transcoder));
  5611. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5612. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5613. tmp = I915_READ(VSYNC(cpu_transcoder));
  5614. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5615. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5616. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5617. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5618. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5619. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5620. }
  5621. tmp = I915_READ(PIPESRC(crtc->pipe));
  5622. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5623. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5624. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5625. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5626. }
  5627. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5628. struct intel_crtc_state *pipe_config)
  5629. {
  5630. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5631. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5632. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5633. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5634. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5635. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5636. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5637. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5638. mode->flags = pipe_config->base.adjusted_mode.flags;
  5639. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5640. mode->flags |= pipe_config->base.adjusted_mode.flags;
  5641. }
  5642. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5643. {
  5644. struct drm_device *dev = intel_crtc->base.dev;
  5645. struct drm_i915_private *dev_priv = dev->dev_private;
  5646. uint32_t pipeconf;
  5647. pipeconf = 0;
  5648. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5649. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5650. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5651. if (intel_crtc->config->double_wide)
  5652. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5653. /* only g4x and later have fancy bpc/dither controls */
  5654. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5655. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5656. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5657. pipeconf |= PIPECONF_DITHER_EN |
  5658. PIPECONF_DITHER_TYPE_SP;
  5659. switch (intel_crtc->config->pipe_bpp) {
  5660. case 18:
  5661. pipeconf |= PIPECONF_6BPC;
  5662. break;
  5663. case 24:
  5664. pipeconf |= PIPECONF_8BPC;
  5665. break;
  5666. case 30:
  5667. pipeconf |= PIPECONF_10BPC;
  5668. break;
  5669. default:
  5670. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5671. BUG();
  5672. }
  5673. }
  5674. if (HAS_PIPE_CXSR(dev)) {
  5675. if (intel_crtc->lowfreq_avail) {
  5676. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5677. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5678. } else {
  5679. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5680. }
  5681. }
  5682. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5683. if (INTEL_INFO(dev)->gen < 4 ||
  5684. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5685. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5686. else
  5687. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5688. } else
  5689. pipeconf |= PIPECONF_PROGRESSIVE;
  5690. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  5691. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5692. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5693. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5694. }
  5695. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  5696. struct intel_crtc_state *crtc_state)
  5697. {
  5698. struct drm_device *dev = crtc->base.dev;
  5699. struct drm_i915_private *dev_priv = dev->dev_private;
  5700. int refclk, num_connectors = 0;
  5701. intel_clock_t clock, reduced_clock;
  5702. bool ok, has_reduced_clock = false;
  5703. bool is_lvds = false, is_dsi = false;
  5704. struct intel_encoder *encoder;
  5705. const intel_limit_t *limit;
  5706. struct drm_atomic_state *state = crtc_state->base.state;
  5707. struct drm_connector_state *connector_state;
  5708. int i;
  5709. for (i = 0; i < state->num_connector; i++) {
  5710. if (!state->connectors[i])
  5711. continue;
  5712. connector_state = state->connector_states[i];
  5713. if (connector_state->crtc != &crtc->base)
  5714. continue;
  5715. encoder = to_intel_encoder(connector_state->best_encoder);
  5716. switch (encoder->type) {
  5717. case INTEL_OUTPUT_LVDS:
  5718. is_lvds = true;
  5719. break;
  5720. case INTEL_OUTPUT_DSI:
  5721. is_dsi = true;
  5722. break;
  5723. default:
  5724. break;
  5725. }
  5726. num_connectors++;
  5727. }
  5728. if (is_dsi)
  5729. return 0;
  5730. if (!crtc_state->clock_set) {
  5731. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  5732. /*
  5733. * Returns a set of divisors for the desired target clock with
  5734. * the given refclk, or FALSE. The returned values represent
  5735. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5736. * 2) / p1 / p2.
  5737. */
  5738. limit = intel_limit(crtc_state, refclk);
  5739. ok = dev_priv->display.find_dpll(limit, crtc_state,
  5740. crtc_state->port_clock,
  5741. refclk, NULL, &clock);
  5742. if (!ok) {
  5743. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5744. return -EINVAL;
  5745. }
  5746. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5747. /*
  5748. * Ensure we match the reduced clock's P to the target
  5749. * clock. If the clocks don't match, we can't switch
  5750. * the display clock by using the FP0/FP1. In such case
  5751. * we will disable the LVDS downclock feature.
  5752. */
  5753. has_reduced_clock =
  5754. dev_priv->display.find_dpll(limit, crtc_state,
  5755. dev_priv->lvds_downclock,
  5756. refclk, &clock,
  5757. &reduced_clock);
  5758. }
  5759. /* Compat-code for transition, will disappear. */
  5760. crtc_state->dpll.n = clock.n;
  5761. crtc_state->dpll.m1 = clock.m1;
  5762. crtc_state->dpll.m2 = clock.m2;
  5763. crtc_state->dpll.p1 = clock.p1;
  5764. crtc_state->dpll.p2 = clock.p2;
  5765. }
  5766. if (IS_GEN2(dev)) {
  5767. i8xx_update_pll(crtc, crtc_state,
  5768. has_reduced_clock ? &reduced_clock : NULL,
  5769. num_connectors);
  5770. } else if (IS_CHERRYVIEW(dev)) {
  5771. chv_update_pll(crtc, crtc_state);
  5772. } else if (IS_VALLEYVIEW(dev)) {
  5773. vlv_update_pll(crtc, crtc_state);
  5774. } else {
  5775. i9xx_update_pll(crtc, crtc_state,
  5776. has_reduced_clock ? &reduced_clock : NULL,
  5777. num_connectors);
  5778. }
  5779. return 0;
  5780. }
  5781. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5782. struct intel_crtc_state *pipe_config)
  5783. {
  5784. struct drm_device *dev = crtc->base.dev;
  5785. struct drm_i915_private *dev_priv = dev->dev_private;
  5786. uint32_t tmp;
  5787. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5788. return;
  5789. tmp = I915_READ(PFIT_CONTROL);
  5790. if (!(tmp & PFIT_ENABLE))
  5791. return;
  5792. /* Check whether the pfit is attached to our pipe. */
  5793. if (INTEL_INFO(dev)->gen < 4) {
  5794. if (crtc->pipe != PIPE_B)
  5795. return;
  5796. } else {
  5797. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5798. return;
  5799. }
  5800. pipe_config->gmch_pfit.control = tmp;
  5801. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5802. if (INTEL_INFO(dev)->gen < 5)
  5803. pipe_config->gmch_pfit.lvds_border_bits =
  5804. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5805. }
  5806. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5807. struct intel_crtc_state *pipe_config)
  5808. {
  5809. struct drm_device *dev = crtc->base.dev;
  5810. struct drm_i915_private *dev_priv = dev->dev_private;
  5811. int pipe = pipe_config->cpu_transcoder;
  5812. intel_clock_t clock;
  5813. u32 mdiv;
  5814. int refclk = 100000;
  5815. /* In case of MIPI DPLL will not even be used */
  5816. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5817. return;
  5818. mutex_lock(&dev_priv->dpio_lock);
  5819. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5820. mutex_unlock(&dev_priv->dpio_lock);
  5821. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5822. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5823. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5824. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5825. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5826. vlv_clock(refclk, &clock);
  5827. /* clock.dot is the fast clock */
  5828. pipe_config->port_clock = clock.dot / 5;
  5829. }
  5830. static void
  5831. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  5832. struct intel_initial_plane_config *plane_config)
  5833. {
  5834. struct drm_device *dev = crtc->base.dev;
  5835. struct drm_i915_private *dev_priv = dev->dev_private;
  5836. u32 val, base, offset;
  5837. int pipe = crtc->pipe, plane = crtc->plane;
  5838. int fourcc, pixel_format;
  5839. unsigned int aligned_height;
  5840. struct drm_framebuffer *fb;
  5841. struct intel_framebuffer *intel_fb;
  5842. val = I915_READ(DSPCNTR(plane));
  5843. if (!(val & DISPLAY_PLANE_ENABLE))
  5844. return;
  5845. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5846. if (!intel_fb) {
  5847. DRM_DEBUG_KMS("failed to alloc fb\n");
  5848. return;
  5849. }
  5850. fb = &intel_fb->base;
  5851. if (INTEL_INFO(dev)->gen >= 4) {
  5852. if (val & DISPPLANE_TILED) {
  5853. plane_config->tiling = I915_TILING_X;
  5854. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  5855. }
  5856. }
  5857. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5858. fourcc = i9xx_format_to_fourcc(pixel_format);
  5859. fb->pixel_format = fourcc;
  5860. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  5861. if (INTEL_INFO(dev)->gen >= 4) {
  5862. if (plane_config->tiling)
  5863. offset = I915_READ(DSPTILEOFF(plane));
  5864. else
  5865. offset = I915_READ(DSPLINOFF(plane));
  5866. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5867. } else {
  5868. base = I915_READ(DSPADDR(plane));
  5869. }
  5870. plane_config->base = base;
  5871. val = I915_READ(PIPESRC(pipe));
  5872. fb->width = ((val >> 16) & 0xfff) + 1;
  5873. fb->height = ((val >> 0) & 0xfff) + 1;
  5874. val = I915_READ(DSPSTRIDE(pipe));
  5875. fb->pitches[0] = val & 0xffffffc0;
  5876. aligned_height = intel_fb_align_height(dev, fb->height,
  5877. fb->pixel_format,
  5878. fb->modifier[0]);
  5879. plane_config->size = fb->pitches[0] * aligned_height;
  5880. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5881. pipe_name(pipe), plane, fb->width, fb->height,
  5882. fb->bits_per_pixel, base, fb->pitches[0],
  5883. plane_config->size);
  5884. plane_config->fb = intel_fb;
  5885. }
  5886. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5887. struct intel_crtc_state *pipe_config)
  5888. {
  5889. struct drm_device *dev = crtc->base.dev;
  5890. struct drm_i915_private *dev_priv = dev->dev_private;
  5891. int pipe = pipe_config->cpu_transcoder;
  5892. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5893. intel_clock_t clock;
  5894. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5895. int refclk = 100000;
  5896. mutex_lock(&dev_priv->dpio_lock);
  5897. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5898. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5899. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5900. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5901. mutex_unlock(&dev_priv->dpio_lock);
  5902. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5903. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5904. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5905. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5906. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5907. chv_clock(refclk, &clock);
  5908. /* clock.dot is the fast clock */
  5909. pipe_config->port_clock = clock.dot / 5;
  5910. }
  5911. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5912. struct intel_crtc_state *pipe_config)
  5913. {
  5914. struct drm_device *dev = crtc->base.dev;
  5915. struct drm_i915_private *dev_priv = dev->dev_private;
  5916. uint32_t tmp;
  5917. if (!intel_display_power_is_enabled(dev_priv,
  5918. POWER_DOMAIN_PIPE(crtc->pipe)))
  5919. return false;
  5920. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5921. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5922. tmp = I915_READ(PIPECONF(crtc->pipe));
  5923. if (!(tmp & PIPECONF_ENABLE))
  5924. return false;
  5925. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5926. switch (tmp & PIPECONF_BPC_MASK) {
  5927. case PIPECONF_6BPC:
  5928. pipe_config->pipe_bpp = 18;
  5929. break;
  5930. case PIPECONF_8BPC:
  5931. pipe_config->pipe_bpp = 24;
  5932. break;
  5933. case PIPECONF_10BPC:
  5934. pipe_config->pipe_bpp = 30;
  5935. break;
  5936. default:
  5937. break;
  5938. }
  5939. }
  5940. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5941. pipe_config->limited_color_range = true;
  5942. if (INTEL_INFO(dev)->gen < 4)
  5943. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5944. intel_get_pipe_timings(crtc, pipe_config);
  5945. i9xx_get_pfit_config(crtc, pipe_config);
  5946. if (INTEL_INFO(dev)->gen >= 4) {
  5947. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5948. pipe_config->pixel_multiplier =
  5949. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5950. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5951. pipe_config->dpll_hw_state.dpll_md = tmp;
  5952. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5953. tmp = I915_READ(DPLL(crtc->pipe));
  5954. pipe_config->pixel_multiplier =
  5955. ((tmp & SDVO_MULTIPLIER_MASK)
  5956. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5957. } else {
  5958. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5959. * port and will be fixed up in the encoder->get_config
  5960. * function. */
  5961. pipe_config->pixel_multiplier = 1;
  5962. }
  5963. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5964. if (!IS_VALLEYVIEW(dev)) {
  5965. /*
  5966. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5967. * on 830. Filter it out here so that we don't
  5968. * report errors due to that.
  5969. */
  5970. if (IS_I830(dev))
  5971. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5972. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5973. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5974. } else {
  5975. /* Mask out read-only status bits. */
  5976. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5977. DPLL_PORTC_READY_MASK |
  5978. DPLL_PORTB_READY_MASK);
  5979. }
  5980. if (IS_CHERRYVIEW(dev))
  5981. chv_crtc_clock_get(crtc, pipe_config);
  5982. else if (IS_VALLEYVIEW(dev))
  5983. vlv_crtc_clock_get(crtc, pipe_config);
  5984. else
  5985. i9xx_crtc_clock_get(crtc, pipe_config);
  5986. return true;
  5987. }
  5988. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5989. {
  5990. struct drm_i915_private *dev_priv = dev->dev_private;
  5991. struct intel_encoder *encoder;
  5992. u32 val, final;
  5993. bool has_lvds = false;
  5994. bool has_cpu_edp = false;
  5995. bool has_panel = false;
  5996. bool has_ck505 = false;
  5997. bool can_ssc = false;
  5998. /* We need to take the global config into account */
  5999. for_each_intel_encoder(dev, encoder) {
  6000. switch (encoder->type) {
  6001. case INTEL_OUTPUT_LVDS:
  6002. has_panel = true;
  6003. has_lvds = true;
  6004. break;
  6005. case INTEL_OUTPUT_EDP:
  6006. has_panel = true;
  6007. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6008. has_cpu_edp = true;
  6009. break;
  6010. default:
  6011. break;
  6012. }
  6013. }
  6014. if (HAS_PCH_IBX(dev)) {
  6015. has_ck505 = dev_priv->vbt.display_clock_mode;
  6016. can_ssc = has_ck505;
  6017. } else {
  6018. has_ck505 = false;
  6019. can_ssc = true;
  6020. }
  6021. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6022. has_panel, has_lvds, has_ck505);
  6023. /* Ironlake: try to setup display ref clock before DPLL
  6024. * enabling. This is only under driver's control after
  6025. * PCH B stepping, previous chipset stepping should be
  6026. * ignoring this setting.
  6027. */
  6028. val = I915_READ(PCH_DREF_CONTROL);
  6029. /* As we must carefully and slowly disable/enable each source in turn,
  6030. * compute the final state we want first and check if we need to
  6031. * make any changes at all.
  6032. */
  6033. final = val;
  6034. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6035. if (has_ck505)
  6036. final |= DREF_NONSPREAD_CK505_ENABLE;
  6037. else
  6038. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6039. final &= ~DREF_SSC_SOURCE_MASK;
  6040. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6041. final &= ~DREF_SSC1_ENABLE;
  6042. if (has_panel) {
  6043. final |= DREF_SSC_SOURCE_ENABLE;
  6044. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6045. final |= DREF_SSC1_ENABLE;
  6046. if (has_cpu_edp) {
  6047. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6048. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6049. else
  6050. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6051. } else
  6052. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6053. } else {
  6054. final |= DREF_SSC_SOURCE_DISABLE;
  6055. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6056. }
  6057. if (final == val)
  6058. return;
  6059. /* Always enable nonspread source */
  6060. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6061. if (has_ck505)
  6062. val |= DREF_NONSPREAD_CK505_ENABLE;
  6063. else
  6064. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6065. if (has_panel) {
  6066. val &= ~DREF_SSC_SOURCE_MASK;
  6067. val |= DREF_SSC_SOURCE_ENABLE;
  6068. /* SSC must be turned on before enabling the CPU output */
  6069. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6070. DRM_DEBUG_KMS("Using SSC on panel\n");
  6071. val |= DREF_SSC1_ENABLE;
  6072. } else
  6073. val &= ~DREF_SSC1_ENABLE;
  6074. /* Get SSC going before enabling the outputs */
  6075. I915_WRITE(PCH_DREF_CONTROL, val);
  6076. POSTING_READ(PCH_DREF_CONTROL);
  6077. udelay(200);
  6078. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6079. /* Enable CPU source on CPU attached eDP */
  6080. if (has_cpu_edp) {
  6081. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6082. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6083. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6084. } else
  6085. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6086. } else
  6087. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6088. I915_WRITE(PCH_DREF_CONTROL, val);
  6089. POSTING_READ(PCH_DREF_CONTROL);
  6090. udelay(200);
  6091. } else {
  6092. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6093. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6094. /* Turn off CPU output */
  6095. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6096. I915_WRITE(PCH_DREF_CONTROL, val);
  6097. POSTING_READ(PCH_DREF_CONTROL);
  6098. udelay(200);
  6099. /* Turn off the SSC source */
  6100. val &= ~DREF_SSC_SOURCE_MASK;
  6101. val |= DREF_SSC_SOURCE_DISABLE;
  6102. /* Turn off SSC1 */
  6103. val &= ~DREF_SSC1_ENABLE;
  6104. I915_WRITE(PCH_DREF_CONTROL, val);
  6105. POSTING_READ(PCH_DREF_CONTROL);
  6106. udelay(200);
  6107. }
  6108. BUG_ON(val != final);
  6109. }
  6110. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6111. {
  6112. uint32_t tmp;
  6113. tmp = I915_READ(SOUTH_CHICKEN2);
  6114. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6115. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6116. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6117. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6118. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6119. tmp = I915_READ(SOUTH_CHICKEN2);
  6120. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6121. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6122. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6123. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6124. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6125. }
  6126. /* WaMPhyProgramming:hsw */
  6127. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6128. {
  6129. uint32_t tmp;
  6130. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6131. tmp &= ~(0xFF << 24);
  6132. tmp |= (0x12 << 24);
  6133. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6134. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6135. tmp |= (1 << 11);
  6136. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6137. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6138. tmp |= (1 << 11);
  6139. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6140. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6141. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6142. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6143. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6144. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6145. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6146. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6147. tmp &= ~(7 << 13);
  6148. tmp |= (5 << 13);
  6149. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6150. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6151. tmp &= ~(7 << 13);
  6152. tmp |= (5 << 13);
  6153. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6154. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6155. tmp &= ~0xFF;
  6156. tmp |= 0x1C;
  6157. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6158. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6159. tmp &= ~0xFF;
  6160. tmp |= 0x1C;
  6161. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6162. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6163. tmp &= ~(0xFF << 16);
  6164. tmp |= (0x1C << 16);
  6165. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6166. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6167. tmp &= ~(0xFF << 16);
  6168. tmp |= (0x1C << 16);
  6169. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6170. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6171. tmp |= (1 << 27);
  6172. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6173. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6174. tmp |= (1 << 27);
  6175. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6176. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6177. tmp &= ~(0xF << 28);
  6178. tmp |= (4 << 28);
  6179. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6180. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6181. tmp &= ~(0xF << 28);
  6182. tmp |= (4 << 28);
  6183. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6184. }
  6185. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6186. * Programming" based on the parameters passed:
  6187. * - Sequence to enable CLKOUT_DP
  6188. * - Sequence to enable CLKOUT_DP without spread
  6189. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6190. */
  6191. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  6192. bool with_fdi)
  6193. {
  6194. struct drm_i915_private *dev_priv = dev->dev_private;
  6195. uint32_t reg, tmp;
  6196. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6197. with_spread = true;
  6198. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  6199. with_fdi, "LP PCH doesn't have FDI\n"))
  6200. with_fdi = false;
  6201. mutex_lock(&dev_priv->dpio_lock);
  6202. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6203. tmp &= ~SBI_SSCCTL_DISABLE;
  6204. tmp |= SBI_SSCCTL_PATHALT;
  6205. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6206. udelay(24);
  6207. if (with_spread) {
  6208. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6209. tmp &= ~SBI_SSCCTL_PATHALT;
  6210. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6211. if (with_fdi) {
  6212. lpt_reset_fdi_mphy(dev_priv);
  6213. lpt_program_fdi_mphy(dev_priv);
  6214. }
  6215. }
  6216. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6217. SBI_GEN0 : SBI_DBUFF0;
  6218. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6219. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6220. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6221. mutex_unlock(&dev_priv->dpio_lock);
  6222. }
  6223. /* Sequence to disable CLKOUT_DP */
  6224. static void lpt_disable_clkout_dp(struct drm_device *dev)
  6225. {
  6226. struct drm_i915_private *dev_priv = dev->dev_private;
  6227. uint32_t reg, tmp;
  6228. mutex_lock(&dev_priv->dpio_lock);
  6229. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6230. SBI_GEN0 : SBI_DBUFF0;
  6231. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6232. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6233. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6234. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6235. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6236. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6237. tmp |= SBI_SSCCTL_PATHALT;
  6238. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6239. udelay(32);
  6240. }
  6241. tmp |= SBI_SSCCTL_DISABLE;
  6242. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6243. }
  6244. mutex_unlock(&dev_priv->dpio_lock);
  6245. }
  6246. static void lpt_init_pch_refclk(struct drm_device *dev)
  6247. {
  6248. struct intel_encoder *encoder;
  6249. bool has_vga = false;
  6250. for_each_intel_encoder(dev, encoder) {
  6251. switch (encoder->type) {
  6252. case INTEL_OUTPUT_ANALOG:
  6253. has_vga = true;
  6254. break;
  6255. default:
  6256. break;
  6257. }
  6258. }
  6259. if (has_vga)
  6260. lpt_enable_clkout_dp(dev, true, true);
  6261. else
  6262. lpt_disable_clkout_dp(dev);
  6263. }
  6264. /*
  6265. * Initialize reference clocks when the driver loads
  6266. */
  6267. void intel_init_pch_refclk(struct drm_device *dev)
  6268. {
  6269. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  6270. ironlake_init_pch_refclk(dev);
  6271. else if (HAS_PCH_LPT(dev))
  6272. lpt_init_pch_refclk(dev);
  6273. }
  6274. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  6275. {
  6276. struct drm_device *dev = crtc_state->base.crtc->dev;
  6277. struct drm_i915_private *dev_priv = dev->dev_private;
  6278. struct drm_atomic_state *state = crtc_state->base.state;
  6279. struct drm_connector_state *connector_state;
  6280. struct intel_encoder *encoder;
  6281. int num_connectors = 0, i;
  6282. bool is_lvds = false;
  6283. for (i = 0; i < state->num_connector; i++) {
  6284. if (!state->connectors[i])
  6285. continue;
  6286. connector_state = state->connector_states[i];
  6287. if (connector_state->crtc != crtc_state->base.crtc)
  6288. continue;
  6289. encoder = to_intel_encoder(connector_state->best_encoder);
  6290. switch (encoder->type) {
  6291. case INTEL_OUTPUT_LVDS:
  6292. is_lvds = true;
  6293. break;
  6294. default:
  6295. break;
  6296. }
  6297. num_connectors++;
  6298. }
  6299. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6300. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6301. dev_priv->vbt.lvds_ssc_freq);
  6302. return dev_priv->vbt.lvds_ssc_freq;
  6303. }
  6304. return 120000;
  6305. }
  6306. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6307. {
  6308. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  6309. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6310. int pipe = intel_crtc->pipe;
  6311. uint32_t val;
  6312. val = 0;
  6313. switch (intel_crtc->config->pipe_bpp) {
  6314. case 18:
  6315. val |= PIPECONF_6BPC;
  6316. break;
  6317. case 24:
  6318. val |= PIPECONF_8BPC;
  6319. break;
  6320. case 30:
  6321. val |= PIPECONF_10BPC;
  6322. break;
  6323. case 36:
  6324. val |= PIPECONF_12BPC;
  6325. break;
  6326. default:
  6327. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6328. BUG();
  6329. }
  6330. if (intel_crtc->config->dither)
  6331. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6332. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6333. val |= PIPECONF_INTERLACED_ILK;
  6334. else
  6335. val |= PIPECONF_PROGRESSIVE;
  6336. if (intel_crtc->config->limited_color_range)
  6337. val |= PIPECONF_COLOR_RANGE_SELECT;
  6338. I915_WRITE(PIPECONF(pipe), val);
  6339. POSTING_READ(PIPECONF(pipe));
  6340. }
  6341. /*
  6342. * Set up the pipe CSC unit.
  6343. *
  6344. * Currently only full range RGB to limited range RGB conversion
  6345. * is supported, but eventually this should handle various
  6346. * RGB<->YCbCr scenarios as well.
  6347. */
  6348. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6349. {
  6350. struct drm_device *dev = crtc->dev;
  6351. struct drm_i915_private *dev_priv = dev->dev_private;
  6352. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6353. int pipe = intel_crtc->pipe;
  6354. uint16_t coeff = 0x7800; /* 1.0 */
  6355. /*
  6356. * TODO: Check what kind of values actually come out of the pipe
  6357. * with these coeff/postoff values and adjust to get the best
  6358. * accuracy. Perhaps we even need to take the bpc value into
  6359. * consideration.
  6360. */
  6361. if (intel_crtc->config->limited_color_range)
  6362. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6363. /*
  6364. * GY/GU and RY/RU should be the other way around according
  6365. * to BSpec, but reality doesn't agree. Just set them up in
  6366. * a way that results in the correct picture.
  6367. */
  6368. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6369. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6370. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6371. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6372. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6373. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6374. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6375. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6376. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6377. if (INTEL_INFO(dev)->gen > 6) {
  6378. uint16_t postoff = 0;
  6379. if (intel_crtc->config->limited_color_range)
  6380. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6381. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6382. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6383. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6384. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6385. } else {
  6386. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6387. if (intel_crtc->config->limited_color_range)
  6388. mode |= CSC_BLACK_SCREEN_OFFSET;
  6389. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6390. }
  6391. }
  6392. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6393. {
  6394. struct drm_device *dev = crtc->dev;
  6395. struct drm_i915_private *dev_priv = dev->dev_private;
  6396. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6397. enum pipe pipe = intel_crtc->pipe;
  6398. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6399. uint32_t val;
  6400. val = 0;
  6401. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  6402. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6403. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6404. val |= PIPECONF_INTERLACED_ILK;
  6405. else
  6406. val |= PIPECONF_PROGRESSIVE;
  6407. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6408. POSTING_READ(PIPECONF(cpu_transcoder));
  6409. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6410. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6411. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6412. val = 0;
  6413. switch (intel_crtc->config->pipe_bpp) {
  6414. case 18:
  6415. val |= PIPEMISC_DITHER_6_BPC;
  6416. break;
  6417. case 24:
  6418. val |= PIPEMISC_DITHER_8_BPC;
  6419. break;
  6420. case 30:
  6421. val |= PIPEMISC_DITHER_10_BPC;
  6422. break;
  6423. case 36:
  6424. val |= PIPEMISC_DITHER_12_BPC;
  6425. break;
  6426. default:
  6427. /* Case prevented by pipe_config_set_bpp. */
  6428. BUG();
  6429. }
  6430. if (intel_crtc->config->dither)
  6431. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6432. I915_WRITE(PIPEMISC(pipe), val);
  6433. }
  6434. }
  6435. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6436. struct intel_crtc_state *crtc_state,
  6437. intel_clock_t *clock,
  6438. bool *has_reduced_clock,
  6439. intel_clock_t *reduced_clock)
  6440. {
  6441. struct drm_device *dev = crtc->dev;
  6442. struct drm_i915_private *dev_priv = dev->dev_private;
  6443. int refclk;
  6444. const intel_limit_t *limit;
  6445. bool ret, is_lvds = false;
  6446. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  6447. refclk = ironlake_get_refclk(crtc_state);
  6448. /*
  6449. * Returns a set of divisors for the desired target clock with the given
  6450. * refclk, or FALSE. The returned values represent the clock equation:
  6451. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6452. */
  6453. limit = intel_limit(crtc_state, refclk);
  6454. ret = dev_priv->display.find_dpll(limit, crtc_state,
  6455. crtc_state->port_clock,
  6456. refclk, NULL, clock);
  6457. if (!ret)
  6458. return false;
  6459. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6460. /*
  6461. * Ensure we match the reduced clock's P to the target clock.
  6462. * If the clocks don't match, we can't switch the display clock
  6463. * by using the FP0/FP1. In such case we will disable the LVDS
  6464. * downclock feature.
  6465. */
  6466. *has_reduced_clock =
  6467. dev_priv->display.find_dpll(limit, crtc_state,
  6468. dev_priv->lvds_downclock,
  6469. refclk, clock,
  6470. reduced_clock);
  6471. }
  6472. return true;
  6473. }
  6474. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6475. {
  6476. /*
  6477. * Account for spread spectrum to avoid
  6478. * oversubscribing the link. Max center spread
  6479. * is 2.5%; use 5% for safety's sake.
  6480. */
  6481. u32 bps = target_clock * bpp * 21 / 20;
  6482. return DIV_ROUND_UP(bps, link_bw * 8);
  6483. }
  6484. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6485. {
  6486. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6487. }
  6488. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6489. struct intel_crtc_state *crtc_state,
  6490. u32 *fp,
  6491. intel_clock_t *reduced_clock, u32 *fp2)
  6492. {
  6493. struct drm_crtc *crtc = &intel_crtc->base;
  6494. struct drm_device *dev = crtc->dev;
  6495. struct drm_i915_private *dev_priv = dev->dev_private;
  6496. struct drm_atomic_state *state = crtc_state->base.state;
  6497. struct drm_connector_state *connector_state;
  6498. struct intel_encoder *encoder;
  6499. uint32_t dpll;
  6500. int factor, num_connectors = 0, i;
  6501. bool is_lvds = false, is_sdvo = false;
  6502. for (i = 0; i < state->num_connector; i++) {
  6503. if (!state->connectors[i])
  6504. continue;
  6505. connector_state = state->connector_states[i];
  6506. if (connector_state->crtc != crtc_state->base.crtc)
  6507. continue;
  6508. encoder = to_intel_encoder(connector_state->best_encoder);
  6509. switch (encoder->type) {
  6510. case INTEL_OUTPUT_LVDS:
  6511. is_lvds = true;
  6512. break;
  6513. case INTEL_OUTPUT_SDVO:
  6514. case INTEL_OUTPUT_HDMI:
  6515. is_sdvo = true;
  6516. break;
  6517. default:
  6518. break;
  6519. }
  6520. num_connectors++;
  6521. }
  6522. /* Enable autotuning of the PLL clock (if permissible) */
  6523. factor = 21;
  6524. if (is_lvds) {
  6525. if ((intel_panel_use_ssc(dev_priv) &&
  6526. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6527. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6528. factor = 25;
  6529. } else if (crtc_state->sdvo_tv_clock)
  6530. factor = 20;
  6531. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6532. *fp |= FP_CB_TUNE;
  6533. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6534. *fp2 |= FP_CB_TUNE;
  6535. dpll = 0;
  6536. if (is_lvds)
  6537. dpll |= DPLLB_MODE_LVDS;
  6538. else
  6539. dpll |= DPLLB_MODE_DAC_SERIAL;
  6540. dpll |= (crtc_state->pixel_multiplier - 1)
  6541. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6542. if (is_sdvo)
  6543. dpll |= DPLL_SDVO_HIGH_SPEED;
  6544. if (crtc_state->has_dp_encoder)
  6545. dpll |= DPLL_SDVO_HIGH_SPEED;
  6546. /* compute bitmask from p1 value */
  6547. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6548. /* also FPA1 */
  6549. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6550. switch (crtc_state->dpll.p2) {
  6551. case 5:
  6552. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6553. break;
  6554. case 7:
  6555. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6556. break;
  6557. case 10:
  6558. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6559. break;
  6560. case 14:
  6561. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6562. break;
  6563. }
  6564. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6565. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6566. else
  6567. dpll |= PLL_REF_INPUT_DREFCLK;
  6568. return dpll | DPLL_VCO_ENABLE;
  6569. }
  6570. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6571. struct intel_crtc_state *crtc_state)
  6572. {
  6573. struct drm_device *dev = crtc->base.dev;
  6574. intel_clock_t clock, reduced_clock;
  6575. u32 dpll = 0, fp = 0, fp2 = 0;
  6576. bool ok, has_reduced_clock = false;
  6577. bool is_lvds = false;
  6578. struct intel_shared_dpll *pll;
  6579. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6580. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6581. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6582. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  6583. &has_reduced_clock, &reduced_clock);
  6584. if (!ok && !crtc_state->clock_set) {
  6585. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6586. return -EINVAL;
  6587. }
  6588. /* Compat-code for transition, will disappear. */
  6589. if (!crtc_state->clock_set) {
  6590. crtc_state->dpll.n = clock.n;
  6591. crtc_state->dpll.m1 = clock.m1;
  6592. crtc_state->dpll.m2 = clock.m2;
  6593. crtc_state->dpll.p1 = clock.p1;
  6594. crtc_state->dpll.p2 = clock.p2;
  6595. }
  6596. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6597. if (crtc_state->has_pch_encoder) {
  6598. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6599. if (has_reduced_clock)
  6600. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6601. dpll = ironlake_compute_dpll(crtc, crtc_state,
  6602. &fp, &reduced_clock,
  6603. has_reduced_clock ? &fp2 : NULL);
  6604. crtc_state->dpll_hw_state.dpll = dpll;
  6605. crtc_state->dpll_hw_state.fp0 = fp;
  6606. if (has_reduced_clock)
  6607. crtc_state->dpll_hw_state.fp1 = fp2;
  6608. else
  6609. crtc_state->dpll_hw_state.fp1 = fp;
  6610. pll = intel_get_shared_dpll(crtc, crtc_state);
  6611. if (pll == NULL) {
  6612. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6613. pipe_name(crtc->pipe));
  6614. return -EINVAL;
  6615. }
  6616. }
  6617. if (is_lvds && has_reduced_clock)
  6618. crtc->lowfreq_avail = true;
  6619. else
  6620. crtc->lowfreq_avail = false;
  6621. return 0;
  6622. }
  6623. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6624. struct intel_link_m_n *m_n)
  6625. {
  6626. struct drm_device *dev = crtc->base.dev;
  6627. struct drm_i915_private *dev_priv = dev->dev_private;
  6628. enum pipe pipe = crtc->pipe;
  6629. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6630. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6631. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6632. & ~TU_SIZE_MASK;
  6633. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6634. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6635. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6636. }
  6637. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6638. enum transcoder transcoder,
  6639. struct intel_link_m_n *m_n,
  6640. struct intel_link_m_n *m2_n2)
  6641. {
  6642. struct drm_device *dev = crtc->base.dev;
  6643. struct drm_i915_private *dev_priv = dev->dev_private;
  6644. enum pipe pipe = crtc->pipe;
  6645. if (INTEL_INFO(dev)->gen >= 5) {
  6646. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6647. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6648. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6649. & ~TU_SIZE_MASK;
  6650. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6651. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6652. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6653. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6654. * gen < 8) and if DRRS is supported (to make sure the
  6655. * registers are not unnecessarily read).
  6656. */
  6657. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6658. crtc->config->has_drrs) {
  6659. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6660. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6661. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6662. & ~TU_SIZE_MASK;
  6663. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6664. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6665. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6666. }
  6667. } else {
  6668. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6669. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6670. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6671. & ~TU_SIZE_MASK;
  6672. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6673. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6674. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6675. }
  6676. }
  6677. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6678. struct intel_crtc_state *pipe_config)
  6679. {
  6680. if (pipe_config->has_pch_encoder)
  6681. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6682. else
  6683. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6684. &pipe_config->dp_m_n,
  6685. &pipe_config->dp_m2_n2);
  6686. }
  6687. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6688. struct intel_crtc_state *pipe_config)
  6689. {
  6690. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6691. &pipe_config->fdi_m_n, NULL);
  6692. }
  6693. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6694. struct intel_crtc_state *pipe_config)
  6695. {
  6696. struct drm_device *dev = crtc->base.dev;
  6697. struct drm_i915_private *dev_priv = dev->dev_private;
  6698. uint32_t tmp;
  6699. tmp = I915_READ(PS_CTL(crtc->pipe));
  6700. if (tmp & PS_ENABLE) {
  6701. pipe_config->pch_pfit.enabled = true;
  6702. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6703. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6704. }
  6705. }
  6706. static void
  6707. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6708. struct intel_initial_plane_config *plane_config)
  6709. {
  6710. struct drm_device *dev = crtc->base.dev;
  6711. struct drm_i915_private *dev_priv = dev->dev_private;
  6712. u32 val, base, offset, stride_mult, tiling;
  6713. int pipe = crtc->pipe;
  6714. int fourcc, pixel_format;
  6715. unsigned int aligned_height;
  6716. struct drm_framebuffer *fb;
  6717. struct intel_framebuffer *intel_fb;
  6718. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6719. if (!intel_fb) {
  6720. DRM_DEBUG_KMS("failed to alloc fb\n");
  6721. return;
  6722. }
  6723. fb = &intel_fb->base;
  6724. val = I915_READ(PLANE_CTL(pipe, 0));
  6725. if (!(val & PLANE_CTL_ENABLE))
  6726. goto error;
  6727. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  6728. fourcc = skl_format_to_fourcc(pixel_format,
  6729. val & PLANE_CTL_ORDER_RGBX,
  6730. val & PLANE_CTL_ALPHA_MASK);
  6731. fb->pixel_format = fourcc;
  6732. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6733. tiling = val & PLANE_CTL_TILED_MASK;
  6734. switch (tiling) {
  6735. case PLANE_CTL_TILED_LINEAR:
  6736. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  6737. break;
  6738. case PLANE_CTL_TILED_X:
  6739. plane_config->tiling = I915_TILING_X;
  6740. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6741. break;
  6742. case PLANE_CTL_TILED_Y:
  6743. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  6744. break;
  6745. case PLANE_CTL_TILED_YF:
  6746. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  6747. break;
  6748. default:
  6749. MISSING_CASE(tiling);
  6750. goto error;
  6751. }
  6752. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  6753. plane_config->base = base;
  6754. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  6755. val = I915_READ(PLANE_SIZE(pipe, 0));
  6756. fb->height = ((val >> 16) & 0xfff) + 1;
  6757. fb->width = ((val >> 0) & 0x1fff) + 1;
  6758. val = I915_READ(PLANE_STRIDE(pipe, 0));
  6759. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  6760. fb->pixel_format);
  6761. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  6762. aligned_height = intel_fb_align_height(dev, fb->height,
  6763. fb->pixel_format,
  6764. fb->modifier[0]);
  6765. plane_config->size = fb->pitches[0] * aligned_height;
  6766. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6767. pipe_name(pipe), fb->width, fb->height,
  6768. fb->bits_per_pixel, base, fb->pitches[0],
  6769. plane_config->size);
  6770. plane_config->fb = intel_fb;
  6771. return;
  6772. error:
  6773. kfree(fb);
  6774. }
  6775. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6776. struct intel_crtc_state *pipe_config)
  6777. {
  6778. struct drm_device *dev = crtc->base.dev;
  6779. struct drm_i915_private *dev_priv = dev->dev_private;
  6780. uint32_t tmp;
  6781. tmp = I915_READ(PF_CTL(crtc->pipe));
  6782. if (tmp & PF_ENABLE) {
  6783. pipe_config->pch_pfit.enabled = true;
  6784. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6785. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6786. /* We currently do not free assignements of panel fitters on
  6787. * ivb/hsw (since we don't use the higher upscaling modes which
  6788. * differentiates them) so just WARN about this case for now. */
  6789. if (IS_GEN7(dev)) {
  6790. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6791. PF_PIPE_SEL_IVB(crtc->pipe));
  6792. }
  6793. }
  6794. }
  6795. static void
  6796. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  6797. struct intel_initial_plane_config *plane_config)
  6798. {
  6799. struct drm_device *dev = crtc->base.dev;
  6800. struct drm_i915_private *dev_priv = dev->dev_private;
  6801. u32 val, base, offset;
  6802. int pipe = crtc->pipe;
  6803. int fourcc, pixel_format;
  6804. unsigned int aligned_height;
  6805. struct drm_framebuffer *fb;
  6806. struct intel_framebuffer *intel_fb;
  6807. val = I915_READ(DSPCNTR(pipe));
  6808. if (!(val & DISPLAY_PLANE_ENABLE))
  6809. return;
  6810. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6811. if (!intel_fb) {
  6812. DRM_DEBUG_KMS("failed to alloc fb\n");
  6813. return;
  6814. }
  6815. fb = &intel_fb->base;
  6816. if (INTEL_INFO(dev)->gen >= 4) {
  6817. if (val & DISPPLANE_TILED) {
  6818. plane_config->tiling = I915_TILING_X;
  6819. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6820. }
  6821. }
  6822. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6823. fourcc = i9xx_format_to_fourcc(pixel_format);
  6824. fb->pixel_format = fourcc;
  6825. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6826. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  6827. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6828. offset = I915_READ(DSPOFFSET(pipe));
  6829. } else {
  6830. if (plane_config->tiling)
  6831. offset = I915_READ(DSPTILEOFF(pipe));
  6832. else
  6833. offset = I915_READ(DSPLINOFF(pipe));
  6834. }
  6835. plane_config->base = base;
  6836. val = I915_READ(PIPESRC(pipe));
  6837. fb->width = ((val >> 16) & 0xfff) + 1;
  6838. fb->height = ((val >> 0) & 0xfff) + 1;
  6839. val = I915_READ(DSPSTRIDE(pipe));
  6840. fb->pitches[0] = val & 0xffffffc0;
  6841. aligned_height = intel_fb_align_height(dev, fb->height,
  6842. fb->pixel_format,
  6843. fb->modifier[0]);
  6844. plane_config->size = fb->pitches[0] * aligned_height;
  6845. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6846. pipe_name(pipe), fb->width, fb->height,
  6847. fb->bits_per_pixel, base, fb->pitches[0],
  6848. plane_config->size);
  6849. plane_config->fb = intel_fb;
  6850. }
  6851. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6852. struct intel_crtc_state *pipe_config)
  6853. {
  6854. struct drm_device *dev = crtc->base.dev;
  6855. struct drm_i915_private *dev_priv = dev->dev_private;
  6856. uint32_t tmp;
  6857. if (!intel_display_power_is_enabled(dev_priv,
  6858. POWER_DOMAIN_PIPE(crtc->pipe)))
  6859. return false;
  6860. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6861. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6862. tmp = I915_READ(PIPECONF(crtc->pipe));
  6863. if (!(tmp & PIPECONF_ENABLE))
  6864. return false;
  6865. switch (tmp & PIPECONF_BPC_MASK) {
  6866. case PIPECONF_6BPC:
  6867. pipe_config->pipe_bpp = 18;
  6868. break;
  6869. case PIPECONF_8BPC:
  6870. pipe_config->pipe_bpp = 24;
  6871. break;
  6872. case PIPECONF_10BPC:
  6873. pipe_config->pipe_bpp = 30;
  6874. break;
  6875. case PIPECONF_12BPC:
  6876. pipe_config->pipe_bpp = 36;
  6877. break;
  6878. default:
  6879. break;
  6880. }
  6881. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6882. pipe_config->limited_color_range = true;
  6883. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6884. struct intel_shared_dpll *pll;
  6885. pipe_config->has_pch_encoder = true;
  6886. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6887. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6888. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6889. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6890. if (HAS_PCH_IBX(dev_priv->dev)) {
  6891. pipe_config->shared_dpll =
  6892. (enum intel_dpll_id) crtc->pipe;
  6893. } else {
  6894. tmp = I915_READ(PCH_DPLL_SEL);
  6895. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6896. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6897. else
  6898. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6899. }
  6900. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6901. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6902. &pipe_config->dpll_hw_state));
  6903. tmp = pipe_config->dpll_hw_state.dpll;
  6904. pipe_config->pixel_multiplier =
  6905. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6906. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6907. ironlake_pch_clock_get(crtc, pipe_config);
  6908. } else {
  6909. pipe_config->pixel_multiplier = 1;
  6910. }
  6911. intel_get_pipe_timings(crtc, pipe_config);
  6912. ironlake_get_pfit_config(crtc, pipe_config);
  6913. return true;
  6914. }
  6915. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6916. {
  6917. struct drm_device *dev = dev_priv->dev;
  6918. struct intel_crtc *crtc;
  6919. for_each_intel_crtc(dev, crtc)
  6920. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6921. pipe_name(crtc->pipe));
  6922. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6923. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6924. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6925. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6926. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6927. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6928. "CPU PWM1 enabled\n");
  6929. if (IS_HASWELL(dev))
  6930. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6931. "CPU PWM2 enabled\n");
  6932. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6933. "PCH PWM1 enabled\n");
  6934. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6935. "Utility pin enabled\n");
  6936. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6937. /*
  6938. * In theory we can still leave IRQs enabled, as long as only the HPD
  6939. * interrupts remain enabled. We used to check for that, but since it's
  6940. * gen-specific and since we only disable LCPLL after we fully disable
  6941. * the interrupts, the check below should be enough.
  6942. */
  6943. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6944. }
  6945. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6946. {
  6947. struct drm_device *dev = dev_priv->dev;
  6948. if (IS_HASWELL(dev))
  6949. return I915_READ(D_COMP_HSW);
  6950. else
  6951. return I915_READ(D_COMP_BDW);
  6952. }
  6953. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6954. {
  6955. struct drm_device *dev = dev_priv->dev;
  6956. if (IS_HASWELL(dev)) {
  6957. mutex_lock(&dev_priv->rps.hw_lock);
  6958. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6959. val))
  6960. DRM_ERROR("Failed to write to D_COMP\n");
  6961. mutex_unlock(&dev_priv->rps.hw_lock);
  6962. } else {
  6963. I915_WRITE(D_COMP_BDW, val);
  6964. POSTING_READ(D_COMP_BDW);
  6965. }
  6966. }
  6967. /*
  6968. * This function implements pieces of two sequences from BSpec:
  6969. * - Sequence for display software to disable LCPLL
  6970. * - Sequence for display software to allow package C8+
  6971. * The steps implemented here are just the steps that actually touch the LCPLL
  6972. * register. Callers should take care of disabling all the display engine
  6973. * functions, doing the mode unset, fixing interrupts, etc.
  6974. */
  6975. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6976. bool switch_to_fclk, bool allow_power_down)
  6977. {
  6978. uint32_t val;
  6979. assert_can_disable_lcpll(dev_priv);
  6980. val = I915_READ(LCPLL_CTL);
  6981. if (switch_to_fclk) {
  6982. val |= LCPLL_CD_SOURCE_FCLK;
  6983. I915_WRITE(LCPLL_CTL, val);
  6984. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6985. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6986. DRM_ERROR("Switching to FCLK failed\n");
  6987. val = I915_READ(LCPLL_CTL);
  6988. }
  6989. val |= LCPLL_PLL_DISABLE;
  6990. I915_WRITE(LCPLL_CTL, val);
  6991. POSTING_READ(LCPLL_CTL);
  6992. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6993. DRM_ERROR("LCPLL still locked\n");
  6994. val = hsw_read_dcomp(dev_priv);
  6995. val |= D_COMP_COMP_DISABLE;
  6996. hsw_write_dcomp(dev_priv, val);
  6997. ndelay(100);
  6998. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6999. 1))
  7000. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7001. if (allow_power_down) {
  7002. val = I915_READ(LCPLL_CTL);
  7003. val |= LCPLL_POWER_DOWN_ALLOW;
  7004. I915_WRITE(LCPLL_CTL, val);
  7005. POSTING_READ(LCPLL_CTL);
  7006. }
  7007. }
  7008. /*
  7009. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7010. * source.
  7011. */
  7012. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7013. {
  7014. uint32_t val;
  7015. val = I915_READ(LCPLL_CTL);
  7016. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7017. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7018. return;
  7019. /*
  7020. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7021. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7022. */
  7023. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7024. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7025. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7026. I915_WRITE(LCPLL_CTL, val);
  7027. POSTING_READ(LCPLL_CTL);
  7028. }
  7029. val = hsw_read_dcomp(dev_priv);
  7030. val |= D_COMP_COMP_FORCE;
  7031. val &= ~D_COMP_COMP_DISABLE;
  7032. hsw_write_dcomp(dev_priv, val);
  7033. val = I915_READ(LCPLL_CTL);
  7034. val &= ~LCPLL_PLL_DISABLE;
  7035. I915_WRITE(LCPLL_CTL, val);
  7036. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7037. DRM_ERROR("LCPLL not locked yet\n");
  7038. if (val & LCPLL_CD_SOURCE_FCLK) {
  7039. val = I915_READ(LCPLL_CTL);
  7040. val &= ~LCPLL_CD_SOURCE_FCLK;
  7041. I915_WRITE(LCPLL_CTL, val);
  7042. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7043. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7044. DRM_ERROR("Switching back to LCPLL failed\n");
  7045. }
  7046. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7047. }
  7048. /*
  7049. * Package states C8 and deeper are really deep PC states that can only be
  7050. * reached when all the devices on the system allow it, so even if the graphics
  7051. * device allows PC8+, it doesn't mean the system will actually get to these
  7052. * states. Our driver only allows PC8+ when going into runtime PM.
  7053. *
  7054. * The requirements for PC8+ are that all the outputs are disabled, the power
  7055. * well is disabled and most interrupts are disabled, and these are also
  7056. * requirements for runtime PM. When these conditions are met, we manually do
  7057. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7058. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7059. * hang the machine.
  7060. *
  7061. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7062. * the state of some registers, so when we come back from PC8+ we need to
  7063. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7064. * need to take care of the registers kept by RC6. Notice that this happens even
  7065. * if we don't put the device in PCI D3 state (which is what currently happens
  7066. * because of the runtime PM support).
  7067. *
  7068. * For more, read "Display Sequences for Package C8" on the hardware
  7069. * documentation.
  7070. */
  7071. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7072. {
  7073. struct drm_device *dev = dev_priv->dev;
  7074. uint32_t val;
  7075. DRM_DEBUG_KMS("Enabling package C8+\n");
  7076. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7077. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7078. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7079. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7080. }
  7081. lpt_disable_clkout_dp(dev);
  7082. hsw_disable_lcpll(dev_priv, true, true);
  7083. }
  7084. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7085. {
  7086. struct drm_device *dev = dev_priv->dev;
  7087. uint32_t val;
  7088. DRM_DEBUG_KMS("Disabling package C8+\n");
  7089. hsw_restore_lcpll(dev_priv);
  7090. lpt_init_pch_refclk(dev);
  7091. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7092. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7093. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7094. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7095. }
  7096. intel_prepare_ddi(dev);
  7097. }
  7098. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7099. struct intel_crtc_state *crtc_state)
  7100. {
  7101. if (!intel_ddi_pll_select(crtc, crtc_state))
  7102. return -EINVAL;
  7103. crtc->lowfreq_avail = false;
  7104. return 0;
  7105. }
  7106. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7107. enum port port,
  7108. struct intel_crtc_state *pipe_config)
  7109. {
  7110. u32 temp, dpll_ctl1;
  7111. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7112. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  7113. switch (pipe_config->ddi_pll_sel) {
  7114. case SKL_DPLL0:
  7115. /*
  7116. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  7117. * of the shared DPLL framework and thus needs to be read out
  7118. * separately
  7119. */
  7120. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  7121. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  7122. break;
  7123. case SKL_DPLL1:
  7124. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  7125. break;
  7126. case SKL_DPLL2:
  7127. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  7128. break;
  7129. case SKL_DPLL3:
  7130. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  7131. break;
  7132. }
  7133. }
  7134. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7135. enum port port,
  7136. struct intel_crtc_state *pipe_config)
  7137. {
  7138. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7139. switch (pipe_config->ddi_pll_sel) {
  7140. case PORT_CLK_SEL_WRPLL1:
  7141. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  7142. break;
  7143. case PORT_CLK_SEL_WRPLL2:
  7144. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  7145. break;
  7146. }
  7147. }
  7148. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7149. struct intel_crtc_state *pipe_config)
  7150. {
  7151. struct drm_device *dev = crtc->base.dev;
  7152. struct drm_i915_private *dev_priv = dev->dev_private;
  7153. struct intel_shared_dpll *pll;
  7154. enum port port;
  7155. uint32_t tmp;
  7156. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7157. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7158. if (IS_SKYLAKE(dev))
  7159. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7160. else
  7161. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7162. if (pipe_config->shared_dpll >= 0) {
  7163. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7164. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7165. &pipe_config->dpll_hw_state));
  7166. }
  7167. /*
  7168. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7169. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7170. * the PCH transcoder is on.
  7171. */
  7172. if (INTEL_INFO(dev)->gen < 9 &&
  7173. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7174. pipe_config->has_pch_encoder = true;
  7175. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7176. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7177. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7178. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7179. }
  7180. }
  7181. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7182. struct intel_crtc_state *pipe_config)
  7183. {
  7184. struct drm_device *dev = crtc->base.dev;
  7185. struct drm_i915_private *dev_priv = dev->dev_private;
  7186. enum intel_display_power_domain pfit_domain;
  7187. uint32_t tmp;
  7188. if (!intel_display_power_is_enabled(dev_priv,
  7189. POWER_DOMAIN_PIPE(crtc->pipe)))
  7190. return false;
  7191. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7192. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7193. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7194. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7195. enum pipe trans_edp_pipe;
  7196. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7197. default:
  7198. WARN(1, "unknown pipe linked to edp transcoder\n");
  7199. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7200. case TRANS_DDI_EDP_INPUT_A_ON:
  7201. trans_edp_pipe = PIPE_A;
  7202. break;
  7203. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7204. trans_edp_pipe = PIPE_B;
  7205. break;
  7206. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7207. trans_edp_pipe = PIPE_C;
  7208. break;
  7209. }
  7210. if (trans_edp_pipe == crtc->pipe)
  7211. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7212. }
  7213. if (!intel_display_power_is_enabled(dev_priv,
  7214. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  7215. return false;
  7216. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7217. if (!(tmp & PIPECONF_ENABLE))
  7218. return false;
  7219. haswell_get_ddi_port_state(crtc, pipe_config);
  7220. intel_get_pipe_timings(crtc, pipe_config);
  7221. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7222. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  7223. if (IS_SKYLAKE(dev))
  7224. skylake_get_pfit_config(crtc, pipe_config);
  7225. else
  7226. ironlake_get_pfit_config(crtc, pipe_config);
  7227. }
  7228. if (IS_HASWELL(dev))
  7229. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7230. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7231. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  7232. pipe_config->pixel_multiplier =
  7233. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7234. } else {
  7235. pipe_config->pixel_multiplier = 1;
  7236. }
  7237. return true;
  7238. }
  7239. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  7240. {
  7241. struct drm_device *dev = crtc->dev;
  7242. struct drm_i915_private *dev_priv = dev->dev_private;
  7243. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7244. uint32_t cntl = 0, size = 0;
  7245. if (base) {
  7246. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  7247. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  7248. unsigned int stride = roundup_pow_of_two(width) * 4;
  7249. switch (stride) {
  7250. default:
  7251. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7252. width, stride);
  7253. stride = 256;
  7254. /* fallthrough */
  7255. case 256:
  7256. case 512:
  7257. case 1024:
  7258. case 2048:
  7259. break;
  7260. }
  7261. cntl |= CURSOR_ENABLE |
  7262. CURSOR_GAMMA_ENABLE |
  7263. CURSOR_FORMAT_ARGB |
  7264. CURSOR_STRIDE(stride);
  7265. size = (height << 12) | width;
  7266. }
  7267. if (intel_crtc->cursor_cntl != 0 &&
  7268. (intel_crtc->cursor_base != base ||
  7269. intel_crtc->cursor_size != size ||
  7270. intel_crtc->cursor_cntl != cntl)) {
  7271. /* On these chipsets we can only modify the base/size/stride
  7272. * whilst the cursor is disabled.
  7273. */
  7274. I915_WRITE(_CURACNTR, 0);
  7275. POSTING_READ(_CURACNTR);
  7276. intel_crtc->cursor_cntl = 0;
  7277. }
  7278. if (intel_crtc->cursor_base != base) {
  7279. I915_WRITE(_CURABASE, base);
  7280. intel_crtc->cursor_base = base;
  7281. }
  7282. if (intel_crtc->cursor_size != size) {
  7283. I915_WRITE(CURSIZE, size);
  7284. intel_crtc->cursor_size = size;
  7285. }
  7286. if (intel_crtc->cursor_cntl != cntl) {
  7287. I915_WRITE(_CURACNTR, cntl);
  7288. POSTING_READ(_CURACNTR);
  7289. intel_crtc->cursor_cntl = cntl;
  7290. }
  7291. }
  7292. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  7293. {
  7294. struct drm_device *dev = crtc->dev;
  7295. struct drm_i915_private *dev_priv = dev->dev_private;
  7296. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7297. int pipe = intel_crtc->pipe;
  7298. uint32_t cntl;
  7299. cntl = 0;
  7300. if (base) {
  7301. cntl = MCURSOR_GAMMA_ENABLE;
  7302. switch (intel_crtc->base.cursor->state->crtc_w) {
  7303. case 64:
  7304. cntl |= CURSOR_MODE_64_ARGB_AX;
  7305. break;
  7306. case 128:
  7307. cntl |= CURSOR_MODE_128_ARGB_AX;
  7308. break;
  7309. case 256:
  7310. cntl |= CURSOR_MODE_256_ARGB_AX;
  7311. break;
  7312. default:
  7313. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  7314. return;
  7315. }
  7316. cntl |= pipe << 28; /* Connect to correct pipe */
  7317. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  7318. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7319. }
  7320. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  7321. cntl |= CURSOR_ROTATE_180;
  7322. if (intel_crtc->cursor_cntl != cntl) {
  7323. I915_WRITE(CURCNTR(pipe), cntl);
  7324. POSTING_READ(CURCNTR(pipe));
  7325. intel_crtc->cursor_cntl = cntl;
  7326. }
  7327. /* and commit changes on next vblank */
  7328. I915_WRITE(CURBASE(pipe), base);
  7329. POSTING_READ(CURBASE(pipe));
  7330. intel_crtc->cursor_base = base;
  7331. }
  7332. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7333. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7334. bool on)
  7335. {
  7336. struct drm_device *dev = crtc->dev;
  7337. struct drm_i915_private *dev_priv = dev->dev_private;
  7338. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7339. int pipe = intel_crtc->pipe;
  7340. int x = crtc->cursor_x;
  7341. int y = crtc->cursor_y;
  7342. u32 base = 0, pos = 0;
  7343. if (on)
  7344. base = intel_crtc->cursor_addr;
  7345. if (x >= intel_crtc->config->pipe_src_w)
  7346. base = 0;
  7347. if (y >= intel_crtc->config->pipe_src_h)
  7348. base = 0;
  7349. if (x < 0) {
  7350. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  7351. base = 0;
  7352. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7353. x = -x;
  7354. }
  7355. pos |= x << CURSOR_X_SHIFT;
  7356. if (y < 0) {
  7357. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  7358. base = 0;
  7359. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7360. y = -y;
  7361. }
  7362. pos |= y << CURSOR_Y_SHIFT;
  7363. if (base == 0 && intel_crtc->cursor_base == 0)
  7364. return;
  7365. I915_WRITE(CURPOS(pipe), pos);
  7366. /* ILK+ do this automagically */
  7367. if (HAS_GMCH_DISPLAY(dev) &&
  7368. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  7369. base += (intel_crtc->base.cursor->state->crtc_h *
  7370. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  7371. }
  7372. if (IS_845G(dev) || IS_I865G(dev))
  7373. i845_update_cursor(crtc, base);
  7374. else
  7375. i9xx_update_cursor(crtc, base);
  7376. }
  7377. static bool cursor_size_ok(struct drm_device *dev,
  7378. uint32_t width, uint32_t height)
  7379. {
  7380. if (width == 0 || height == 0)
  7381. return false;
  7382. /*
  7383. * 845g/865g are special in that they are only limited by
  7384. * the width of their cursors, the height is arbitrary up to
  7385. * the precision of the register. Everything else requires
  7386. * square cursors, limited to a few power-of-two sizes.
  7387. */
  7388. if (IS_845G(dev) || IS_I865G(dev)) {
  7389. if ((width & 63) != 0)
  7390. return false;
  7391. if (width > (IS_845G(dev) ? 64 : 512))
  7392. return false;
  7393. if (height > 1023)
  7394. return false;
  7395. } else {
  7396. switch (width | height) {
  7397. case 256:
  7398. case 128:
  7399. if (IS_GEN2(dev))
  7400. return false;
  7401. case 64:
  7402. break;
  7403. default:
  7404. return false;
  7405. }
  7406. }
  7407. return true;
  7408. }
  7409. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7410. u16 *blue, uint32_t start, uint32_t size)
  7411. {
  7412. int end = (start + size > 256) ? 256 : start + size, i;
  7413. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7414. for (i = start; i < end; i++) {
  7415. intel_crtc->lut_r[i] = red[i] >> 8;
  7416. intel_crtc->lut_g[i] = green[i] >> 8;
  7417. intel_crtc->lut_b[i] = blue[i] >> 8;
  7418. }
  7419. intel_crtc_load_lut(crtc);
  7420. }
  7421. /* VESA 640x480x72Hz mode to set on the pipe */
  7422. static struct drm_display_mode load_detect_mode = {
  7423. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7424. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7425. };
  7426. struct drm_framebuffer *
  7427. __intel_framebuffer_create(struct drm_device *dev,
  7428. struct drm_mode_fb_cmd2 *mode_cmd,
  7429. struct drm_i915_gem_object *obj)
  7430. {
  7431. struct intel_framebuffer *intel_fb;
  7432. int ret;
  7433. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7434. if (!intel_fb) {
  7435. drm_gem_object_unreference(&obj->base);
  7436. return ERR_PTR(-ENOMEM);
  7437. }
  7438. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7439. if (ret)
  7440. goto err;
  7441. return &intel_fb->base;
  7442. err:
  7443. drm_gem_object_unreference(&obj->base);
  7444. kfree(intel_fb);
  7445. return ERR_PTR(ret);
  7446. }
  7447. static struct drm_framebuffer *
  7448. intel_framebuffer_create(struct drm_device *dev,
  7449. struct drm_mode_fb_cmd2 *mode_cmd,
  7450. struct drm_i915_gem_object *obj)
  7451. {
  7452. struct drm_framebuffer *fb;
  7453. int ret;
  7454. ret = i915_mutex_lock_interruptible(dev);
  7455. if (ret)
  7456. return ERR_PTR(ret);
  7457. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7458. mutex_unlock(&dev->struct_mutex);
  7459. return fb;
  7460. }
  7461. static u32
  7462. intel_framebuffer_pitch_for_width(int width, int bpp)
  7463. {
  7464. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7465. return ALIGN(pitch, 64);
  7466. }
  7467. static u32
  7468. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7469. {
  7470. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7471. return PAGE_ALIGN(pitch * mode->vdisplay);
  7472. }
  7473. static struct drm_framebuffer *
  7474. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7475. struct drm_display_mode *mode,
  7476. int depth, int bpp)
  7477. {
  7478. struct drm_i915_gem_object *obj;
  7479. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7480. obj = i915_gem_alloc_object(dev,
  7481. intel_framebuffer_size_for_mode(mode, bpp));
  7482. if (obj == NULL)
  7483. return ERR_PTR(-ENOMEM);
  7484. mode_cmd.width = mode->hdisplay;
  7485. mode_cmd.height = mode->vdisplay;
  7486. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7487. bpp);
  7488. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7489. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7490. }
  7491. static struct drm_framebuffer *
  7492. mode_fits_in_fbdev(struct drm_device *dev,
  7493. struct drm_display_mode *mode)
  7494. {
  7495. #ifdef CONFIG_DRM_I915_FBDEV
  7496. struct drm_i915_private *dev_priv = dev->dev_private;
  7497. struct drm_i915_gem_object *obj;
  7498. struct drm_framebuffer *fb;
  7499. if (!dev_priv->fbdev)
  7500. return NULL;
  7501. if (!dev_priv->fbdev->fb)
  7502. return NULL;
  7503. obj = dev_priv->fbdev->fb->obj;
  7504. BUG_ON(!obj);
  7505. fb = &dev_priv->fbdev->fb->base;
  7506. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7507. fb->bits_per_pixel))
  7508. return NULL;
  7509. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7510. return NULL;
  7511. return fb;
  7512. #else
  7513. return NULL;
  7514. #endif
  7515. }
  7516. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7517. struct drm_display_mode *mode,
  7518. struct intel_load_detect_pipe *old,
  7519. struct drm_modeset_acquire_ctx *ctx)
  7520. {
  7521. struct intel_crtc *intel_crtc;
  7522. struct intel_encoder *intel_encoder =
  7523. intel_attached_encoder(connector);
  7524. struct drm_crtc *possible_crtc;
  7525. struct drm_encoder *encoder = &intel_encoder->base;
  7526. struct drm_crtc *crtc = NULL;
  7527. struct drm_device *dev = encoder->dev;
  7528. struct drm_framebuffer *fb;
  7529. struct drm_mode_config *config = &dev->mode_config;
  7530. struct drm_atomic_state *state = NULL;
  7531. struct drm_connector_state *connector_state;
  7532. int ret, i = -1;
  7533. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7534. connector->base.id, connector->name,
  7535. encoder->base.id, encoder->name);
  7536. retry:
  7537. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7538. if (ret)
  7539. goto fail_unlock;
  7540. /*
  7541. * Algorithm gets a little messy:
  7542. *
  7543. * - if the connector already has an assigned crtc, use it (but make
  7544. * sure it's on first)
  7545. *
  7546. * - try to find the first unused crtc that can drive this connector,
  7547. * and use that if we find one
  7548. */
  7549. /* See if we already have a CRTC for this connector */
  7550. if (encoder->crtc) {
  7551. crtc = encoder->crtc;
  7552. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7553. if (ret)
  7554. goto fail_unlock;
  7555. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7556. if (ret)
  7557. goto fail_unlock;
  7558. old->dpms_mode = connector->dpms;
  7559. old->load_detect_temp = false;
  7560. /* Make sure the crtc and connector are running */
  7561. if (connector->dpms != DRM_MODE_DPMS_ON)
  7562. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7563. return true;
  7564. }
  7565. /* Find an unused one (if possible) */
  7566. for_each_crtc(dev, possible_crtc) {
  7567. i++;
  7568. if (!(encoder->possible_crtcs & (1 << i)))
  7569. continue;
  7570. if (possible_crtc->state->enable)
  7571. continue;
  7572. /* This can occur when applying the pipe A quirk on resume. */
  7573. if (to_intel_crtc(possible_crtc)->new_enabled)
  7574. continue;
  7575. crtc = possible_crtc;
  7576. break;
  7577. }
  7578. /*
  7579. * If we didn't find an unused CRTC, don't use any.
  7580. */
  7581. if (!crtc) {
  7582. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7583. goto fail_unlock;
  7584. }
  7585. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7586. if (ret)
  7587. goto fail_unlock;
  7588. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7589. if (ret)
  7590. goto fail_unlock;
  7591. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7592. to_intel_connector(connector)->new_encoder = intel_encoder;
  7593. intel_crtc = to_intel_crtc(crtc);
  7594. intel_crtc->new_enabled = true;
  7595. intel_crtc->new_config = intel_crtc->config;
  7596. old->dpms_mode = connector->dpms;
  7597. old->load_detect_temp = true;
  7598. old->release_fb = NULL;
  7599. state = drm_atomic_state_alloc(dev);
  7600. if (!state)
  7601. return false;
  7602. state->acquire_ctx = ctx;
  7603. connector_state = drm_atomic_get_connector_state(state, connector);
  7604. if (IS_ERR(connector_state)) {
  7605. ret = PTR_ERR(connector_state);
  7606. goto fail;
  7607. }
  7608. connector_state->crtc = crtc;
  7609. connector_state->best_encoder = &intel_encoder->base;
  7610. if (!mode)
  7611. mode = &load_detect_mode;
  7612. /* We need a framebuffer large enough to accommodate all accesses
  7613. * that the plane may generate whilst we perform load detection.
  7614. * We can not rely on the fbcon either being present (we get called
  7615. * during its initialisation to detect all boot displays, or it may
  7616. * not even exist) or that it is large enough to satisfy the
  7617. * requested mode.
  7618. */
  7619. fb = mode_fits_in_fbdev(dev, mode);
  7620. if (fb == NULL) {
  7621. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7622. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7623. old->release_fb = fb;
  7624. } else
  7625. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7626. if (IS_ERR(fb)) {
  7627. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7628. goto fail;
  7629. }
  7630. if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
  7631. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7632. if (old->release_fb)
  7633. old->release_fb->funcs->destroy(old->release_fb);
  7634. goto fail;
  7635. }
  7636. crtc->primary->crtc = crtc;
  7637. /* let the connector get through one full cycle before testing */
  7638. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7639. return true;
  7640. fail:
  7641. intel_crtc->new_enabled = crtc->state->enable;
  7642. if (intel_crtc->new_enabled)
  7643. intel_crtc->new_config = intel_crtc->config;
  7644. else
  7645. intel_crtc->new_config = NULL;
  7646. fail_unlock:
  7647. if (state) {
  7648. drm_atomic_state_free(state);
  7649. state = NULL;
  7650. }
  7651. if (ret == -EDEADLK) {
  7652. drm_modeset_backoff(ctx);
  7653. goto retry;
  7654. }
  7655. return false;
  7656. }
  7657. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7658. struct intel_load_detect_pipe *old,
  7659. struct drm_modeset_acquire_ctx *ctx)
  7660. {
  7661. struct drm_device *dev = connector->dev;
  7662. struct intel_encoder *intel_encoder =
  7663. intel_attached_encoder(connector);
  7664. struct drm_encoder *encoder = &intel_encoder->base;
  7665. struct drm_crtc *crtc = encoder->crtc;
  7666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7667. struct drm_atomic_state *state;
  7668. struct drm_connector_state *connector_state;
  7669. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7670. connector->base.id, connector->name,
  7671. encoder->base.id, encoder->name);
  7672. if (old->load_detect_temp) {
  7673. state = drm_atomic_state_alloc(dev);
  7674. if (!state)
  7675. goto fail;
  7676. state->acquire_ctx = ctx;
  7677. connector_state = drm_atomic_get_connector_state(state, connector);
  7678. if (IS_ERR(connector_state))
  7679. goto fail;
  7680. to_intel_connector(connector)->new_encoder = NULL;
  7681. intel_encoder->new_crtc = NULL;
  7682. intel_crtc->new_enabled = false;
  7683. intel_crtc->new_config = NULL;
  7684. connector_state->best_encoder = NULL;
  7685. connector_state->crtc = NULL;
  7686. intel_set_mode(crtc, NULL, 0, 0, NULL, state);
  7687. drm_atomic_state_free(state);
  7688. if (old->release_fb) {
  7689. drm_framebuffer_unregister_private(old->release_fb);
  7690. drm_framebuffer_unreference(old->release_fb);
  7691. }
  7692. return;
  7693. }
  7694. /* Switch crtc and encoder back off if necessary */
  7695. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7696. connector->funcs->dpms(connector, old->dpms_mode);
  7697. return;
  7698. fail:
  7699. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  7700. drm_atomic_state_free(state);
  7701. }
  7702. static int i9xx_pll_refclk(struct drm_device *dev,
  7703. const struct intel_crtc_state *pipe_config)
  7704. {
  7705. struct drm_i915_private *dev_priv = dev->dev_private;
  7706. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7707. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7708. return dev_priv->vbt.lvds_ssc_freq;
  7709. else if (HAS_PCH_SPLIT(dev))
  7710. return 120000;
  7711. else if (!IS_GEN2(dev))
  7712. return 96000;
  7713. else
  7714. return 48000;
  7715. }
  7716. /* Returns the clock of the currently programmed mode of the given pipe. */
  7717. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7718. struct intel_crtc_state *pipe_config)
  7719. {
  7720. struct drm_device *dev = crtc->base.dev;
  7721. struct drm_i915_private *dev_priv = dev->dev_private;
  7722. int pipe = pipe_config->cpu_transcoder;
  7723. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7724. u32 fp;
  7725. intel_clock_t clock;
  7726. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7727. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7728. fp = pipe_config->dpll_hw_state.fp0;
  7729. else
  7730. fp = pipe_config->dpll_hw_state.fp1;
  7731. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7732. if (IS_PINEVIEW(dev)) {
  7733. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7734. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7735. } else {
  7736. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7737. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7738. }
  7739. if (!IS_GEN2(dev)) {
  7740. if (IS_PINEVIEW(dev))
  7741. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7742. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7743. else
  7744. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7745. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7746. switch (dpll & DPLL_MODE_MASK) {
  7747. case DPLLB_MODE_DAC_SERIAL:
  7748. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7749. 5 : 10;
  7750. break;
  7751. case DPLLB_MODE_LVDS:
  7752. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7753. 7 : 14;
  7754. break;
  7755. default:
  7756. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7757. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7758. return;
  7759. }
  7760. if (IS_PINEVIEW(dev))
  7761. pineview_clock(refclk, &clock);
  7762. else
  7763. i9xx_clock(refclk, &clock);
  7764. } else {
  7765. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7766. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7767. if (is_lvds) {
  7768. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7769. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7770. if (lvds & LVDS_CLKB_POWER_UP)
  7771. clock.p2 = 7;
  7772. else
  7773. clock.p2 = 14;
  7774. } else {
  7775. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7776. clock.p1 = 2;
  7777. else {
  7778. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7779. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7780. }
  7781. if (dpll & PLL_P2_DIVIDE_BY_4)
  7782. clock.p2 = 4;
  7783. else
  7784. clock.p2 = 2;
  7785. }
  7786. i9xx_clock(refclk, &clock);
  7787. }
  7788. /*
  7789. * This value includes pixel_multiplier. We will use
  7790. * port_clock to compute adjusted_mode.crtc_clock in the
  7791. * encoder's get_config() function.
  7792. */
  7793. pipe_config->port_clock = clock.dot;
  7794. }
  7795. int intel_dotclock_calculate(int link_freq,
  7796. const struct intel_link_m_n *m_n)
  7797. {
  7798. /*
  7799. * The calculation for the data clock is:
  7800. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7801. * But we want to avoid losing precison if possible, so:
  7802. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7803. *
  7804. * and the link clock is simpler:
  7805. * link_clock = (m * link_clock) / n
  7806. */
  7807. if (!m_n->link_n)
  7808. return 0;
  7809. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7810. }
  7811. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7812. struct intel_crtc_state *pipe_config)
  7813. {
  7814. struct drm_device *dev = crtc->base.dev;
  7815. /* read out port_clock from the DPLL */
  7816. i9xx_crtc_clock_get(crtc, pipe_config);
  7817. /*
  7818. * This value does not include pixel_multiplier.
  7819. * We will check that port_clock and adjusted_mode.crtc_clock
  7820. * agree once we know their relationship in the encoder's
  7821. * get_config() function.
  7822. */
  7823. pipe_config->base.adjusted_mode.crtc_clock =
  7824. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7825. &pipe_config->fdi_m_n);
  7826. }
  7827. /** Returns the currently programmed mode of the given pipe. */
  7828. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7829. struct drm_crtc *crtc)
  7830. {
  7831. struct drm_i915_private *dev_priv = dev->dev_private;
  7832. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7833. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7834. struct drm_display_mode *mode;
  7835. struct intel_crtc_state pipe_config;
  7836. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7837. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7838. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7839. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7840. enum pipe pipe = intel_crtc->pipe;
  7841. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7842. if (!mode)
  7843. return NULL;
  7844. /*
  7845. * Construct a pipe_config sufficient for getting the clock info
  7846. * back out of crtc_clock_get.
  7847. *
  7848. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7849. * to use a real value here instead.
  7850. */
  7851. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7852. pipe_config.pixel_multiplier = 1;
  7853. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7854. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7855. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7856. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7857. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7858. mode->hdisplay = (htot & 0xffff) + 1;
  7859. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7860. mode->hsync_start = (hsync & 0xffff) + 1;
  7861. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7862. mode->vdisplay = (vtot & 0xffff) + 1;
  7863. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7864. mode->vsync_start = (vsync & 0xffff) + 1;
  7865. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7866. drm_mode_set_name(mode);
  7867. return mode;
  7868. }
  7869. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7870. {
  7871. struct drm_device *dev = crtc->dev;
  7872. struct drm_i915_private *dev_priv = dev->dev_private;
  7873. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7874. if (!HAS_GMCH_DISPLAY(dev))
  7875. return;
  7876. if (!dev_priv->lvds_downclock_avail)
  7877. return;
  7878. /*
  7879. * Since this is called by a timer, we should never get here in
  7880. * the manual case.
  7881. */
  7882. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7883. int pipe = intel_crtc->pipe;
  7884. int dpll_reg = DPLL(pipe);
  7885. int dpll;
  7886. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7887. assert_panel_unlocked(dev_priv, pipe);
  7888. dpll = I915_READ(dpll_reg);
  7889. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7890. I915_WRITE(dpll_reg, dpll);
  7891. intel_wait_for_vblank(dev, pipe);
  7892. dpll = I915_READ(dpll_reg);
  7893. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7894. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7895. }
  7896. }
  7897. void intel_mark_busy(struct drm_device *dev)
  7898. {
  7899. struct drm_i915_private *dev_priv = dev->dev_private;
  7900. if (dev_priv->mm.busy)
  7901. return;
  7902. intel_runtime_pm_get(dev_priv);
  7903. i915_update_gfx_val(dev_priv);
  7904. if (INTEL_INFO(dev)->gen >= 6)
  7905. gen6_rps_busy(dev_priv);
  7906. dev_priv->mm.busy = true;
  7907. }
  7908. void intel_mark_idle(struct drm_device *dev)
  7909. {
  7910. struct drm_i915_private *dev_priv = dev->dev_private;
  7911. struct drm_crtc *crtc;
  7912. if (!dev_priv->mm.busy)
  7913. return;
  7914. dev_priv->mm.busy = false;
  7915. for_each_crtc(dev, crtc) {
  7916. if (!crtc->primary->fb)
  7917. continue;
  7918. intel_decrease_pllclock(crtc);
  7919. }
  7920. if (INTEL_INFO(dev)->gen >= 6)
  7921. gen6_rps_idle(dev->dev_private);
  7922. intel_runtime_pm_put(dev_priv);
  7923. }
  7924. static void intel_crtc_set_state(struct intel_crtc *crtc,
  7925. struct intel_crtc_state *crtc_state)
  7926. {
  7927. kfree(crtc->config);
  7928. crtc->config = crtc_state;
  7929. crtc->base.state = &crtc_state->base;
  7930. }
  7931. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7932. {
  7933. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7934. struct drm_device *dev = crtc->dev;
  7935. struct intel_unpin_work *work;
  7936. spin_lock_irq(&dev->event_lock);
  7937. work = intel_crtc->unpin_work;
  7938. intel_crtc->unpin_work = NULL;
  7939. spin_unlock_irq(&dev->event_lock);
  7940. if (work) {
  7941. cancel_work_sync(&work->work);
  7942. kfree(work);
  7943. }
  7944. intel_crtc_set_state(intel_crtc, NULL);
  7945. drm_crtc_cleanup(crtc);
  7946. kfree(intel_crtc);
  7947. }
  7948. static void intel_unpin_work_fn(struct work_struct *__work)
  7949. {
  7950. struct intel_unpin_work *work =
  7951. container_of(__work, struct intel_unpin_work, work);
  7952. struct drm_device *dev = work->crtc->dev;
  7953. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7954. mutex_lock(&dev->struct_mutex);
  7955. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  7956. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7957. intel_fbc_update(dev);
  7958. if (work->flip_queued_req)
  7959. i915_gem_request_assign(&work->flip_queued_req, NULL);
  7960. mutex_unlock(&dev->struct_mutex);
  7961. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7962. drm_framebuffer_unreference(work->old_fb);
  7963. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7964. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7965. kfree(work);
  7966. }
  7967. static void do_intel_finish_page_flip(struct drm_device *dev,
  7968. struct drm_crtc *crtc)
  7969. {
  7970. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7971. struct intel_unpin_work *work;
  7972. unsigned long flags;
  7973. /* Ignore early vblank irqs */
  7974. if (intel_crtc == NULL)
  7975. return;
  7976. /*
  7977. * This is called both by irq handlers and the reset code (to complete
  7978. * lost pageflips) so needs the full irqsave spinlocks.
  7979. */
  7980. spin_lock_irqsave(&dev->event_lock, flags);
  7981. work = intel_crtc->unpin_work;
  7982. /* Ensure we don't miss a work->pending update ... */
  7983. smp_rmb();
  7984. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7985. spin_unlock_irqrestore(&dev->event_lock, flags);
  7986. return;
  7987. }
  7988. page_flip_completed(intel_crtc);
  7989. spin_unlock_irqrestore(&dev->event_lock, flags);
  7990. }
  7991. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7992. {
  7993. struct drm_i915_private *dev_priv = dev->dev_private;
  7994. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7995. do_intel_finish_page_flip(dev, crtc);
  7996. }
  7997. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7998. {
  7999. struct drm_i915_private *dev_priv = dev->dev_private;
  8000. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  8001. do_intel_finish_page_flip(dev, crtc);
  8002. }
  8003. /* Is 'a' after or equal to 'b'? */
  8004. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8005. {
  8006. return !((a - b) & 0x80000000);
  8007. }
  8008. static bool page_flip_finished(struct intel_crtc *crtc)
  8009. {
  8010. struct drm_device *dev = crtc->base.dev;
  8011. struct drm_i915_private *dev_priv = dev->dev_private;
  8012. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  8013. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  8014. return true;
  8015. /*
  8016. * The relevant registers doen't exist on pre-ctg.
  8017. * As the flip done interrupt doesn't trigger for mmio
  8018. * flips on gmch platforms, a flip count check isn't
  8019. * really needed there. But since ctg has the registers,
  8020. * include it in the check anyway.
  8021. */
  8022. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  8023. return true;
  8024. /*
  8025. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8026. * used the same base address. In that case the mmio flip might
  8027. * have completed, but the CS hasn't even executed the flip yet.
  8028. *
  8029. * A flip count check isn't enough as the CS might have updated
  8030. * the base address just after start of vblank, but before we
  8031. * managed to process the interrupt. This means we'd complete the
  8032. * CS flip too soon.
  8033. *
  8034. * Combining both checks should get us a good enough result. It may
  8035. * still happen that the CS flip has been executed, but has not
  8036. * yet actually completed. But in case the base address is the same
  8037. * anyway, we don't really care.
  8038. */
  8039. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8040. crtc->unpin_work->gtt_offset &&
  8041. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  8042. crtc->unpin_work->flip_count);
  8043. }
  8044. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  8045. {
  8046. struct drm_i915_private *dev_priv = dev->dev_private;
  8047. struct intel_crtc *intel_crtc =
  8048. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  8049. unsigned long flags;
  8050. /*
  8051. * This is called both by irq handlers and the reset code (to complete
  8052. * lost pageflips) so needs the full irqsave spinlocks.
  8053. *
  8054. * NB: An MMIO update of the plane base pointer will also
  8055. * generate a page-flip completion irq, i.e. every modeset
  8056. * is also accompanied by a spurious intel_prepare_page_flip().
  8057. */
  8058. spin_lock_irqsave(&dev->event_lock, flags);
  8059. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  8060. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  8061. spin_unlock_irqrestore(&dev->event_lock, flags);
  8062. }
  8063. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  8064. {
  8065. /* Ensure that the work item is consistent when activating it ... */
  8066. smp_wmb();
  8067. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  8068. /* and that it is marked active as soon as the irq could fire. */
  8069. smp_wmb();
  8070. }
  8071. static int intel_gen2_queue_flip(struct drm_device *dev,
  8072. struct drm_crtc *crtc,
  8073. struct drm_framebuffer *fb,
  8074. struct drm_i915_gem_object *obj,
  8075. struct intel_engine_cs *ring,
  8076. uint32_t flags)
  8077. {
  8078. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8079. u32 flip_mask;
  8080. int ret;
  8081. ret = intel_ring_begin(ring, 6);
  8082. if (ret)
  8083. return ret;
  8084. /* Can't queue multiple flips, so wait for the previous
  8085. * one to finish before executing the next.
  8086. */
  8087. if (intel_crtc->plane)
  8088. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8089. else
  8090. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8091. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8092. intel_ring_emit(ring, MI_NOOP);
  8093. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8094. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8095. intel_ring_emit(ring, fb->pitches[0]);
  8096. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8097. intel_ring_emit(ring, 0); /* aux display base address, unused */
  8098. intel_mark_page_flip_active(intel_crtc);
  8099. __intel_ring_advance(ring);
  8100. return 0;
  8101. }
  8102. static int intel_gen3_queue_flip(struct drm_device *dev,
  8103. struct drm_crtc *crtc,
  8104. struct drm_framebuffer *fb,
  8105. struct drm_i915_gem_object *obj,
  8106. struct intel_engine_cs *ring,
  8107. uint32_t flags)
  8108. {
  8109. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8110. u32 flip_mask;
  8111. int ret;
  8112. ret = intel_ring_begin(ring, 6);
  8113. if (ret)
  8114. return ret;
  8115. if (intel_crtc->plane)
  8116. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8117. else
  8118. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8119. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8120. intel_ring_emit(ring, MI_NOOP);
  8121. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  8122. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8123. intel_ring_emit(ring, fb->pitches[0]);
  8124. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8125. intel_ring_emit(ring, MI_NOOP);
  8126. intel_mark_page_flip_active(intel_crtc);
  8127. __intel_ring_advance(ring);
  8128. return 0;
  8129. }
  8130. static int intel_gen4_queue_flip(struct drm_device *dev,
  8131. struct drm_crtc *crtc,
  8132. struct drm_framebuffer *fb,
  8133. struct drm_i915_gem_object *obj,
  8134. struct intel_engine_cs *ring,
  8135. uint32_t flags)
  8136. {
  8137. struct drm_i915_private *dev_priv = dev->dev_private;
  8138. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8139. uint32_t pf, pipesrc;
  8140. int ret;
  8141. ret = intel_ring_begin(ring, 4);
  8142. if (ret)
  8143. return ret;
  8144. /* i965+ uses the linear or tiled offsets from the
  8145. * Display Registers (which do not change across a page-flip)
  8146. * so we need only reprogram the base address.
  8147. */
  8148. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8149. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8150. intel_ring_emit(ring, fb->pitches[0]);
  8151. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  8152. obj->tiling_mode);
  8153. /* XXX Enabling the panel-fitter across page-flip is so far
  8154. * untested on non-native modes, so ignore it for now.
  8155. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8156. */
  8157. pf = 0;
  8158. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8159. intel_ring_emit(ring, pf | pipesrc);
  8160. intel_mark_page_flip_active(intel_crtc);
  8161. __intel_ring_advance(ring);
  8162. return 0;
  8163. }
  8164. static int intel_gen6_queue_flip(struct drm_device *dev,
  8165. struct drm_crtc *crtc,
  8166. struct drm_framebuffer *fb,
  8167. struct drm_i915_gem_object *obj,
  8168. struct intel_engine_cs *ring,
  8169. uint32_t flags)
  8170. {
  8171. struct drm_i915_private *dev_priv = dev->dev_private;
  8172. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8173. uint32_t pf, pipesrc;
  8174. int ret;
  8175. ret = intel_ring_begin(ring, 4);
  8176. if (ret)
  8177. return ret;
  8178. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8179. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8180. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  8181. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8182. /* Contrary to the suggestions in the documentation,
  8183. * "Enable Panel Fitter" does not seem to be required when page
  8184. * flipping with a non-native mode, and worse causes a normal
  8185. * modeset to fail.
  8186. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8187. */
  8188. pf = 0;
  8189. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8190. intel_ring_emit(ring, pf | pipesrc);
  8191. intel_mark_page_flip_active(intel_crtc);
  8192. __intel_ring_advance(ring);
  8193. return 0;
  8194. }
  8195. static int intel_gen7_queue_flip(struct drm_device *dev,
  8196. struct drm_crtc *crtc,
  8197. struct drm_framebuffer *fb,
  8198. struct drm_i915_gem_object *obj,
  8199. struct intel_engine_cs *ring,
  8200. uint32_t flags)
  8201. {
  8202. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8203. uint32_t plane_bit = 0;
  8204. int len, ret;
  8205. switch (intel_crtc->plane) {
  8206. case PLANE_A:
  8207. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8208. break;
  8209. case PLANE_B:
  8210. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8211. break;
  8212. case PLANE_C:
  8213. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8214. break;
  8215. default:
  8216. WARN_ONCE(1, "unknown plane in flip command\n");
  8217. return -ENODEV;
  8218. }
  8219. len = 4;
  8220. if (ring->id == RCS) {
  8221. len += 6;
  8222. /*
  8223. * On Gen 8, SRM is now taking an extra dword to accommodate
  8224. * 48bits addresses, and we need a NOOP for the batch size to
  8225. * stay even.
  8226. */
  8227. if (IS_GEN8(dev))
  8228. len += 2;
  8229. }
  8230. /*
  8231. * BSpec MI_DISPLAY_FLIP for IVB:
  8232. * "The full packet must be contained within the same cache line."
  8233. *
  8234. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8235. * cacheline, if we ever start emitting more commands before
  8236. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8237. * then do the cacheline alignment, and finally emit the
  8238. * MI_DISPLAY_FLIP.
  8239. */
  8240. ret = intel_ring_cacheline_align(ring);
  8241. if (ret)
  8242. return ret;
  8243. ret = intel_ring_begin(ring, len);
  8244. if (ret)
  8245. return ret;
  8246. /* Unmask the flip-done completion message. Note that the bspec says that
  8247. * we should do this for both the BCS and RCS, and that we must not unmask
  8248. * more than one flip event at any time (or ensure that one flip message
  8249. * can be sent by waiting for flip-done prior to queueing new flips).
  8250. * Experimentation says that BCS works despite DERRMR masking all
  8251. * flip-done completion events and that unmasking all planes at once
  8252. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8253. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8254. */
  8255. if (ring->id == RCS) {
  8256. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8257. intel_ring_emit(ring, DERRMR);
  8258. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8259. DERRMR_PIPEB_PRI_FLIP_DONE |
  8260. DERRMR_PIPEC_PRI_FLIP_DONE));
  8261. if (IS_GEN8(dev))
  8262. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8263. MI_SRM_LRM_GLOBAL_GTT);
  8264. else
  8265. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  8266. MI_SRM_LRM_GLOBAL_GTT);
  8267. intel_ring_emit(ring, DERRMR);
  8268. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8269. if (IS_GEN8(dev)) {
  8270. intel_ring_emit(ring, 0);
  8271. intel_ring_emit(ring, MI_NOOP);
  8272. }
  8273. }
  8274. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  8275. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  8276. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8277. intel_ring_emit(ring, (MI_NOOP));
  8278. intel_mark_page_flip_active(intel_crtc);
  8279. __intel_ring_advance(ring);
  8280. return 0;
  8281. }
  8282. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8283. struct drm_i915_gem_object *obj)
  8284. {
  8285. /*
  8286. * This is not being used for older platforms, because
  8287. * non-availability of flip done interrupt forces us to use
  8288. * CS flips. Older platforms derive flip done using some clever
  8289. * tricks involving the flip_pending status bits and vblank irqs.
  8290. * So using MMIO flips there would disrupt this mechanism.
  8291. */
  8292. if (ring == NULL)
  8293. return true;
  8294. if (INTEL_INFO(ring->dev)->gen < 5)
  8295. return false;
  8296. if (i915.use_mmio_flip < 0)
  8297. return false;
  8298. else if (i915.use_mmio_flip > 0)
  8299. return true;
  8300. else if (i915.enable_execlists)
  8301. return true;
  8302. else
  8303. return ring != i915_gem_request_get_ring(obj->last_read_req);
  8304. }
  8305. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  8306. {
  8307. struct drm_device *dev = intel_crtc->base.dev;
  8308. struct drm_i915_private *dev_priv = dev->dev_private;
  8309. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8310. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  8311. struct drm_i915_gem_object *obj = intel_fb->obj;
  8312. const enum pipe pipe = intel_crtc->pipe;
  8313. u32 ctl, stride;
  8314. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8315. ctl &= ~PLANE_CTL_TILED_MASK;
  8316. if (obj->tiling_mode == I915_TILING_X)
  8317. ctl |= PLANE_CTL_TILED_X;
  8318. /*
  8319. * The stride is either expressed as a multiple of 64 bytes chunks for
  8320. * linear buffers or in number of tiles for tiled buffers.
  8321. */
  8322. stride = fb->pitches[0] >> 6;
  8323. if (obj->tiling_mode == I915_TILING_X)
  8324. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  8325. /*
  8326. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8327. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8328. */
  8329. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8330. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8331. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  8332. POSTING_READ(PLANE_SURF(pipe, 0));
  8333. }
  8334. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  8335. {
  8336. struct drm_device *dev = intel_crtc->base.dev;
  8337. struct drm_i915_private *dev_priv = dev->dev_private;
  8338. struct intel_framebuffer *intel_fb =
  8339. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8340. struct drm_i915_gem_object *obj = intel_fb->obj;
  8341. u32 dspcntr;
  8342. u32 reg;
  8343. reg = DSPCNTR(intel_crtc->plane);
  8344. dspcntr = I915_READ(reg);
  8345. if (obj->tiling_mode != I915_TILING_NONE)
  8346. dspcntr |= DISPPLANE_TILED;
  8347. else
  8348. dspcntr &= ~DISPPLANE_TILED;
  8349. I915_WRITE(reg, dspcntr);
  8350. I915_WRITE(DSPSURF(intel_crtc->plane),
  8351. intel_crtc->unpin_work->gtt_offset);
  8352. POSTING_READ(DSPSURF(intel_crtc->plane));
  8353. }
  8354. /*
  8355. * XXX: This is the temporary way to update the plane registers until we get
  8356. * around to using the usual plane update functions for MMIO flips
  8357. */
  8358. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  8359. {
  8360. struct drm_device *dev = intel_crtc->base.dev;
  8361. bool atomic_update;
  8362. u32 start_vbl_count;
  8363. intel_mark_page_flip_active(intel_crtc);
  8364. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  8365. if (INTEL_INFO(dev)->gen >= 9)
  8366. skl_do_mmio_flip(intel_crtc);
  8367. else
  8368. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8369. ilk_do_mmio_flip(intel_crtc);
  8370. if (atomic_update)
  8371. intel_pipe_update_end(intel_crtc, start_vbl_count);
  8372. }
  8373. static void intel_mmio_flip_work_func(struct work_struct *work)
  8374. {
  8375. struct intel_crtc *crtc =
  8376. container_of(work, struct intel_crtc, mmio_flip.work);
  8377. struct intel_mmio_flip *mmio_flip;
  8378. mmio_flip = &crtc->mmio_flip;
  8379. if (mmio_flip->req)
  8380. WARN_ON(__i915_wait_request(mmio_flip->req,
  8381. crtc->reset_counter,
  8382. false, NULL, NULL) != 0);
  8383. intel_do_mmio_flip(crtc);
  8384. if (mmio_flip->req) {
  8385. mutex_lock(&crtc->base.dev->struct_mutex);
  8386. i915_gem_request_assign(&mmio_flip->req, NULL);
  8387. mutex_unlock(&crtc->base.dev->struct_mutex);
  8388. }
  8389. }
  8390. static int intel_queue_mmio_flip(struct drm_device *dev,
  8391. struct drm_crtc *crtc,
  8392. struct drm_framebuffer *fb,
  8393. struct drm_i915_gem_object *obj,
  8394. struct intel_engine_cs *ring,
  8395. uint32_t flags)
  8396. {
  8397. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8398. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  8399. obj->last_write_req);
  8400. schedule_work(&intel_crtc->mmio_flip.work);
  8401. return 0;
  8402. }
  8403. static int intel_default_queue_flip(struct drm_device *dev,
  8404. struct drm_crtc *crtc,
  8405. struct drm_framebuffer *fb,
  8406. struct drm_i915_gem_object *obj,
  8407. struct intel_engine_cs *ring,
  8408. uint32_t flags)
  8409. {
  8410. return -ENODEV;
  8411. }
  8412. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  8413. struct drm_crtc *crtc)
  8414. {
  8415. struct drm_i915_private *dev_priv = dev->dev_private;
  8416. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8417. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8418. u32 addr;
  8419. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8420. return true;
  8421. if (!work->enable_stall_check)
  8422. return false;
  8423. if (work->flip_ready_vblank == 0) {
  8424. if (work->flip_queued_req &&
  8425. !i915_gem_request_completed(work->flip_queued_req, true))
  8426. return false;
  8427. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  8428. }
  8429. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  8430. return false;
  8431. /* Potential stall - if we see that the flip has happened,
  8432. * assume a missed interrupt. */
  8433. if (INTEL_INFO(dev)->gen >= 4)
  8434. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8435. else
  8436. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8437. /* There is a potential issue here with a false positive after a flip
  8438. * to the same address. We could address this by checking for a
  8439. * non-incrementing frame counter.
  8440. */
  8441. return addr == work->gtt_offset;
  8442. }
  8443. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8444. {
  8445. struct drm_i915_private *dev_priv = dev->dev_private;
  8446. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8447. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8448. WARN_ON(!in_interrupt());
  8449. if (crtc == NULL)
  8450. return;
  8451. spin_lock(&dev->event_lock);
  8452. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8453. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8454. intel_crtc->unpin_work->flip_queued_vblank,
  8455. drm_vblank_count(dev, pipe));
  8456. page_flip_completed(intel_crtc);
  8457. }
  8458. spin_unlock(&dev->event_lock);
  8459. }
  8460. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8461. struct drm_framebuffer *fb,
  8462. struct drm_pending_vblank_event *event,
  8463. uint32_t page_flip_flags)
  8464. {
  8465. struct drm_device *dev = crtc->dev;
  8466. struct drm_i915_private *dev_priv = dev->dev_private;
  8467. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8468. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8469. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8470. struct drm_plane *primary = crtc->primary;
  8471. enum pipe pipe = intel_crtc->pipe;
  8472. struct intel_unpin_work *work;
  8473. struct intel_engine_cs *ring;
  8474. int ret;
  8475. /*
  8476. * drm_mode_page_flip_ioctl() should already catch this, but double
  8477. * check to be safe. In the future we may enable pageflipping from
  8478. * a disabled primary plane.
  8479. */
  8480. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8481. return -EBUSY;
  8482. /* Can't change pixel format via MI display flips. */
  8483. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8484. return -EINVAL;
  8485. /*
  8486. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8487. * Note that pitch changes could also affect these register.
  8488. */
  8489. if (INTEL_INFO(dev)->gen > 3 &&
  8490. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8491. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8492. return -EINVAL;
  8493. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8494. goto out_hang;
  8495. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8496. if (work == NULL)
  8497. return -ENOMEM;
  8498. work->event = event;
  8499. work->crtc = crtc;
  8500. work->old_fb = old_fb;
  8501. INIT_WORK(&work->work, intel_unpin_work_fn);
  8502. ret = drm_crtc_vblank_get(crtc);
  8503. if (ret)
  8504. goto free_work;
  8505. /* We borrow the event spin lock for protecting unpin_work */
  8506. spin_lock_irq(&dev->event_lock);
  8507. if (intel_crtc->unpin_work) {
  8508. /* Before declaring the flip queue wedged, check if
  8509. * the hardware completed the operation behind our backs.
  8510. */
  8511. if (__intel_pageflip_stall_check(dev, crtc)) {
  8512. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8513. page_flip_completed(intel_crtc);
  8514. } else {
  8515. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8516. spin_unlock_irq(&dev->event_lock);
  8517. drm_crtc_vblank_put(crtc);
  8518. kfree(work);
  8519. return -EBUSY;
  8520. }
  8521. }
  8522. intel_crtc->unpin_work = work;
  8523. spin_unlock_irq(&dev->event_lock);
  8524. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8525. flush_workqueue(dev_priv->wq);
  8526. /* Reference the objects for the scheduled work. */
  8527. drm_framebuffer_reference(work->old_fb);
  8528. drm_gem_object_reference(&obj->base);
  8529. crtc->primary->fb = fb;
  8530. update_state_fb(crtc->primary);
  8531. work->pending_flip_obj = obj;
  8532. ret = i915_mutex_lock_interruptible(dev);
  8533. if (ret)
  8534. goto cleanup;
  8535. atomic_inc(&intel_crtc->unpin_work_count);
  8536. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8537. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8538. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8539. if (IS_VALLEYVIEW(dev)) {
  8540. ring = &dev_priv->ring[BCS];
  8541. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  8542. /* vlv: DISPLAY_FLIP fails to change tiling */
  8543. ring = NULL;
  8544. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  8545. ring = &dev_priv->ring[BCS];
  8546. } else if (INTEL_INFO(dev)->gen >= 7) {
  8547. ring = i915_gem_request_get_ring(obj->last_read_req);
  8548. if (ring == NULL || ring->id != RCS)
  8549. ring = &dev_priv->ring[BCS];
  8550. } else {
  8551. ring = &dev_priv->ring[RCS];
  8552. }
  8553. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  8554. crtc->primary->state, ring);
  8555. if (ret)
  8556. goto cleanup_pending;
  8557. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  8558. + intel_crtc->dspaddr_offset;
  8559. if (use_mmio_flip(ring, obj)) {
  8560. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8561. page_flip_flags);
  8562. if (ret)
  8563. goto cleanup_unpin;
  8564. i915_gem_request_assign(&work->flip_queued_req,
  8565. obj->last_write_req);
  8566. } else {
  8567. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8568. page_flip_flags);
  8569. if (ret)
  8570. goto cleanup_unpin;
  8571. i915_gem_request_assign(&work->flip_queued_req,
  8572. intel_ring_get_request(ring));
  8573. }
  8574. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  8575. work->enable_stall_check = true;
  8576. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  8577. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8578. intel_fbc_disable(dev);
  8579. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8580. mutex_unlock(&dev->struct_mutex);
  8581. trace_i915_flip_request(intel_crtc->plane, obj);
  8582. return 0;
  8583. cleanup_unpin:
  8584. intel_unpin_fb_obj(fb, crtc->primary->state);
  8585. cleanup_pending:
  8586. atomic_dec(&intel_crtc->unpin_work_count);
  8587. mutex_unlock(&dev->struct_mutex);
  8588. cleanup:
  8589. crtc->primary->fb = old_fb;
  8590. update_state_fb(crtc->primary);
  8591. drm_gem_object_unreference_unlocked(&obj->base);
  8592. drm_framebuffer_unreference(work->old_fb);
  8593. spin_lock_irq(&dev->event_lock);
  8594. intel_crtc->unpin_work = NULL;
  8595. spin_unlock_irq(&dev->event_lock);
  8596. drm_crtc_vblank_put(crtc);
  8597. free_work:
  8598. kfree(work);
  8599. if (ret == -EIO) {
  8600. out_hang:
  8601. ret = intel_plane_restore(primary);
  8602. if (ret == 0 && event) {
  8603. spin_lock_irq(&dev->event_lock);
  8604. drm_send_vblank_event(dev, pipe, event);
  8605. spin_unlock_irq(&dev->event_lock);
  8606. }
  8607. }
  8608. return ret;
  8609. }
  8610. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8611. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8612. .load_lut = intel_crtc_load_lut,
  8613. .atomic_begin = intel_begin_crtc_commit,
  8614. .atomic_flush = intel_finish_crtc_commit,
  8615. };
  8616. /**
  8617. * intel_modeset_update_staged_output_state
  8618. *
  8619. * Updates the staged output configuration state, e.g. after we've read out the
  8620. * current hw state.
  8621. */
  8622. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8623. {
  8624. struct intel_crtc *crtc;
  8625. struct intel_encoder *encoder;
  8626. struct intel_connector *connector;
  8627. for_each_intel_connector(dev, connector) {
  8628. connector->new_encoder =
  8629. to_intel_encoder(connector->base.encoder);
  8630. }
  8631. for_each_intel_encoder(dev, encoder) {
  8632. encoder->new_crtc =
  8633. to_intel_crtc(encoder->base.crtc);
  8634. }
  8635. for_each_intel_crtc(dev, crtc) {
  8636. crtc->new_enabled = crtc->base.state->enable;
  8637. if (crtc->new_enabled)
  8638. crtc->new_config = crtc->config;
  8639. else
  8640. crtc->new_config = NULL;
  8641. }
  8642. }
  8643. /* Transitional helper to copy current connector/encoder state to
  8644. * connector->state. This is needed so that code that is partially
  8645. * converted to atomic does the right thing.
  8646. */
  8647. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8648. {
  8649. struct intel_connector *connector;
  8650. for_each_intel_connector(dev, connector) {
  8651. if (connector->base.encoder) {
  8652. connector->base.state->best_encoder =
  8653. connector->base.encoder;
  8654. connector->base.state->crtc =
  8655. connector->base.encoder->crtc;
  8656. } else {
  8657. connector->base.state->best_encoder = NULL;
  8658. connector->base.state->crtc = NULL;
  8659. }
  8660. }
  8661. }
  8662. /**
  8663. * intel_modeset_commit_output_state
  8664. *
  8665. * This function copies the stage display pipe configuration to the real one.
  8666. */
  8667. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8668. {
  8669. struct intel_crtc *crtc;
  8670. struct intel_encoder *encoder;
  8671. struct intel_connector *connector;
  8672. for_each_intel_connector(dev, connector) {
  8673. connector->base.encoder = &connector->new_encoder->base;
  8674. }
  8675. for_each_intel_encoder(dev, encoder) {
  8676. encoder->base.crtc = &encoder->new_crtc->base;
  8677. }
  8678. for_each_intel_crtc(dev, crtc) {
  8679. crtc->base.state->enable = crtc->new_enabled;
  8680. crtc->base.enabled = crtc->new_enabled;
  8681. }
  8682. intel_modeset_update_connector_atomic_state(dev);
  8683. }
  8684. static void
  8685. connected_sink_compute_bpp(struct intel_connector *connector,
  8686. struct intel_crtc_state *pipe_config)
  8687. {
  8688. int bpp = pipe_config->pipe_bpp;
  8689. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8690. connector->base.base.id,
  8691. connector->base.name);
  8692. /* Don't use an invalid EDID bpc value */
  8693. if (connector->base.display_info.bpc &&
  8694. connector->base.display_info.bpc * 3 < bpp) {
  8695. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8696. bpp, connector->base.display_info.bpc*3);
  8697. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8698. }
  8699. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8700. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8701. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8702. bpp);
  8703. pipe_config->pipe_bpp = 24;
  8704. }
  8705. }
  8706. static int
  8707. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8708. struct drm_framebuffer *fb,
  8709. struct intel_crtc_state *pipe_config)
  8710. {
  8711. struct drm_device *dev = crtc->base.dev;
  8712. struct drm_atomic_state *state;
  8713. struct intel_connector *connector;
  8714. int bpp, i;
  8715. switch (fb->pixel_format) {
  8716. case DRM_FORMAT_C8:
  8717. bpp = 8*3; /* since we go through a colormap */
  8718. break;
  8719. case DRM_FORMAT_XRGB1555:
  8720. case DRM_FORMAT_ARGB1555:
  8721. /* checked in intel_framebuffer_init already */
  8722. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8723. return -EINVAL;
  8724. case DRM_FORMAT_RGB565:
  8725. bpp = 6*3; /* min is 18bpp */
  8726. break;
  8727. case DRM_FORMAT_XBGR8888:
  8728. case DRM_FORMAT_ABGR8888:
  8729. /* checked in intel_framebuffer_init already */
  8730. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8731. return -EINVAL;
  8732. case DRM_FORMAT_XRGB8888:
  8733. case DRM_FORMAT_ARGB8888:
  8734. bpp = 8*3;
  8735. break;
  8736. case DRM_FORMAT_XRGB2101010:
  8737. case DRM_FORMAT_ARGB2101010:
  8738. case DRM_FORMAT_XBGR2101010:
  8739. case DRM_FORMAT_ABGR2101010:
  8740. /* checked in intel_framebuffer_init already */
  8741. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8742. return -EINVAL;
  8743. bpp = 10*3;
  8744. break;
  8745. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8746. default:
  8747. DRM_DEBUG_KMS("unsupported depth\n");
  8748. return -EINVAL;
  8749. }
  8750. pipe_config->pipe_bpp = bpp;
  8751. state = pipe_config->base.state;
  8752. /* Clamp display bpp to EDID value */
  8753. for (i = 0; i < state->num_connector; i++) {
  8754. if (!state->connectors[i])
  8755. continue;
  8756. connector = to_intel_connector(state->connectors[i]);
  8757. if (state->connector_states[i]->crtc != &crtc->base)
  8758. continue;
  8759. connected_sink_compute_bpp(connector, pipe_config);
  8760. }
  8761. return bpp;
  8762. }
  8763. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8764. {
  8765. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8766. "type: 0x%x flags: 0x%x\n",
  8767. mode->crtc_clock,
  8768. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8769. mode->crtc_hsync_end, mode->crtc_htotal,
  8770. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8771. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8772. }
  8773. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8774. struct intel_crtc_state *pipe_config,
  8775. const char *context)
  8776. {
  8777. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8778. context, pipe_name(crtc->pipe));
  8779. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8780. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8781. pipe_config->pipe_bpp, pipe_config->dither);
  8782. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8783. pipe_config->has_pch_encoder,
  8784. pipe_config->fdi_lanes,
  8785. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8786. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8787. pipe_config->fdi_m_n.tu);
  8788. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8789. pipe_config->has_dp_encoder,
  8790. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8791. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8792. pipe_config->dp_m_n.tu);
  8793. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8794. pipe_config->has_dp_encoder,
  8795. pipe_config->dp_m2_n2.gmch_m,
  8796. pipe_config->dp_m2_n2.gmch_n,
  8797. pipe_config->dp_m2_n2.link_m,
  8798. pipe_config->dp_m2_n2.link_n,
  8799. pipe_config->dp_m2_n2.tu);
  8800. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8801. pipe_config->has_audio,
  8802. pipe_config->has_infoframe);
  8803. DRM_DEBUG_KMS("requested mode:\n");
  8804. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8805. DRM_DEBUG_KMS("adjusted mode:\n");
  8806. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8807. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8808. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8809. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8810. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8811. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8812. pipe_config->gmch_pfit.control,
  8813. pipe_config->gmch_pfit.pgm_ratios,
  8814. pipe_config->gmch_pfit.lvds_border_bits);
  8815. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8816. pipe_config->pch_pfit.pos,
  8817. pipe_config->pch_pfit.size,
  8818. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8819. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8820. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8821. }
  8822. static bool encoders_cloneable(const struct intel_encoder *a,
  8823. const struct intel_encoder *b)
  8824. {
  8825. /* masks could be asymmetric, so check both ways */
  8826. return a == b || (a->cloneable & (1 << b->type) &&
  8827. b->cloneable & (1 << a->type));
  8828. }
  8829. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8830. struct intel_encoder *encoder)
  8831. {
  8832. struct drm_device *dev = crtc->base.dev;
  8833. struct intel_encoder *source_encoder;
  8834. for_each_intel_encoder(dev, source_encoder) {
  8835. if (source_encoder->new_crtc != crtc)
  8836. continue;
  8837. if (!encoders_cloneable(encoder, source_encoder))
  8838. return false;
  8839. }
  8840. return true;
  8841. }
  8842. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8843. {
  8844. struct drm_device *dev = crtc->base.dev;
  8845. struct intel_encoder *encoder;
  8846. for_each_intel_encoder(dev, encoder) {
  8847. if (encoder->new_crtc != crtc)
  8848. continue;
  8849. if (!check_single_encoder_cloning(crtc, encoder))
  8850. return false;
  8851. }
  8852. return true;
  8853. }
  8854. static bool check_digital_port_conflicts(struct drm_device *dev)
  8855. {
  8856. struct intel_connector *connector;
  8857. unsigned int used_ports = 0;
  8858. /*
  8859. * Walk the connector list instead of the encoder
  8860. * list to detect the problem on ddi platforms
  8861. * where there's just one encoder per digital port.
  8862. */
  8863. for_each_intel_connector(dev, connector) {
  8864. struct intel_encoder *encoder = connector->new_encoder;
  8865. if (!encoder)
  8866. continue;
  8867. WARN_ON(!encoder->new_crtc);
  8868. switch (encoder->type) {
  8869. unsigned int port_mask;
  8870. case INTEL_OUTPUT_UNKNOWN:
  8871. if (WARN_ON(!HAS_DDI(dev)))
  8872. break;
  8873. case INTEL_OUTPUT_DISPLAYPORT:
  8874. case INTEL_OUTPUT_HDMI:
  8875. case INTEL_OUTPUT_EDP:
  8876. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8877. /* the same port mustn't appear more than once */
  8878. if (used_ports & port_mask)
  8879. return false;
  8880. used_ports |= port_mask;
  8881. default:
  8882. break;
  8883. }
  8884. }
  8885. return true;
  8886. }
  8887. static void
  8888. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  8889. {
  8890. struct drm_crtc_state tmp_state;
  8891. /* Clear only the intel specific part of the crtc state */
  8892. tmp_state = crtc_state->base;
  8893. memset(crtc_state, 0, sizeof *crtc_state);
  8894. crtc_state->base = tmp_state;
  8895. }
  8896. static struct intel_crtc_state *
  8897. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8898. struct drm_framebuffer *fb,
  8899. struct drm_display_mode *mode,
  8900. struct drm_atomic_state *state)
  8901. {
  8902. struct drm_device *dev = crtc->dev;
  8903. struct intel_encoder *encoder;
  8904. struct intel_connector *connector;
  8905. struct drm_connector_state *connector_state;
  8906. struct intel_crtc_state *pipe_config;
  8907. int plane_bpp, ret = -EINVAL;
  8908. int i;
  8909. bool retry = true;
  8910. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8911. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8912. return ERR_PTR(-EINVAL);
  8913. }
  8914. if (!check_digital_port_conflicts(dev)) {
  8915. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8916. return ERR_PTR(-EINVAL);
  8917. }
  8918. pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
  8919. if (IS_ERR(pipe_config))
  8920. return pipe_config;
  8921. clear_intel_crtc_state(pipe_config);
  8922. pipe_config->base.crtc = crtc;
  8923. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  8924. drm_mode_copy(&pipe_config->base.mode, mode);
  8925. pipe_config->cpu_transcoder =
  8926. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8927. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8928. /*
  8929. * Sanitize sync polarity flags based on requested ones. If neither
  8930. * positive or negative polarity is requested, treat this as meaning
  8931. * negative polarity.
  8932. */
  8933. if (!(pipe_config->base.adjusted_mode.flags &
  8934. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8935. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8936. if (!(pipe_config->base.adjusted_mode.flags &
  8937. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8938. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8939. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8940. * plane pixel format and any sink constraints into account. Returns the
  8941. * source plane bpp so that dithering can be selected on mismatches
  8942. * after encoders and crtc also have had their say. */
  8943. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8944. fb, pipe_config);
  8945. if (plane_bpp < 0)
  8946. goto fail;
  8947. /*
  8948. * Determine the real pipe dimensions. Note that stereo modes can
  8949. * increase the actual pipe size due to the frame doubling and
  8950. * insertion of additional space for blanks between the frame. This
  8951. * is stored in the crtc timings. We use the requested mode to do this
  8952. * computation to clearly distinguish it from the adjusted mode, which
  8953. * can be changed by the connectors in the below retry loop.
  8954. */
  8955. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  8956. &pipe_config->pipe_src_w,
  8957. &pipe_config->pipe_src_h);
  8958. encoder_retry:
  8959. /* Ensure the port clock defaults are reset when retrying. */
  8960. pipe_config->port_clock = 0;
  8961. pipe_config->pixel_multiplier = 1;
  8962. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8963. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  8964. CRTC_STEREO_DOUBLE);
  8965. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8966. * adjust it according to limitations or connector properties, and also
  8967. * a chance to reject the mode entirely.
  8968. */
  8969. for (i = 0; i < state->num_connector; i++) {
  8970. connector = to_intel_connector(state->connectors[i]);
  8971. if (!connector)
  8972. continue;
  8973. connector_state = state->connector_states[i];
  8974. if (connector_state->crtc != crtc)
  8975. continue;
  8976. encoder = to_intel_encoder(connector_state->best_encoder);
  8977. if (!(encoder->compute_config(encoder, pipe_config))) {
  8978. DRM_DEBUG_KMS("Encoder config failure\n");
  8979. goto fail;
  8980. }
  8981. }
  8982. /* Set default port clock if not overwritten by the encoder. Needs to be
  8983. * done afterwards in case the encoder adjusts the mode. */
  8984. if (!pipe_config->port_clock)
  8985. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  8986. * pipe_config->pixel_multiplier;
  8987. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8988. if (ret < 0) {
  8989. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8990. goto fail;
  8991. }
  8992. if (ret == RETRY) {
  8993. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8994. ret = -EINVAL;
  8995. goto fail;
  8996. }
  8997. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8998. retry = false;
  8999. goto encoder_retry;
  9000. }
  9001. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  9002. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  9003. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9004. return pipe_config;
  9005. fail:
  9006. return ERR_PTR(ret);
  9007. }
  9008. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  9009. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  9010. static void
  9011. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  9012. unsigned *prepare_pipes, unsigned *disable_pipes)
  9013. {
  9014. struct intel_crtc *intel_crtc;
  9015. struct drm_device *dev = crtc->dev;
  9016. struct intel_encoder *encoder;
  9017. struct intel_connector *connector;
  9018. struct drm_crtc *tmp_crtc;
  9019. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  9020. /* Check which crtcs have changed outputs connected to them, these need
  9021. * to be part of the prepare_pipes mask. We don't (yet) support global
  9022. * modeset across multiple crtcs, so modeset_pipes will only have one
  9023. * bit set at most. */
  9024. for_each_intel_connector(dev, connector) {
  9025. if (connector->base.encoder == &connector->new_encoder->base)
  9026. continue;
  9027. if (connector->base.encoder) {
  9028. tmp_crtc = connector->base.encoder->crtc;
  9029. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  9030. }
  9031. if (connector->new_encoder)
  9032. *prepare_pipes |=
  9033. 1 << connector->new_encoder->new_crtc->pipe;
  9034. }
  9035. for_each_intel_encoder(dev, encoder) {
  9036. if (encoder->base.crtc == &encoder->new_crtc->base)
  9037. continue;
  9038. if (encoder->base.crtc) {
  9039. tmp_crtc = encoder->base.crtc;
  9040. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  9041. }
  9042. if (encoder->new_crtc)
  9043. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  9044. }
  9045. /* Check for pipes that will be enabled/disabled ... */
  9046. for_each_intel_crtc(dev, intel_crtc) {
  9047. if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
  9048. continue;
  9049. if (!intel_crtc->new_enabled)
  9050. *disable_pipes |= 1 << intel_crtc->pipe;
  9051. else
  9052. *prepare_pipes |= 1 << intel_crtc->pipe;
  9053. }
  9054. /* set_mode is also used to update properties on life display pipes. */
  9055. intel_crtc = to_intel_crtc(crtc);
  9056. if (intel_crtc->new_enabled)
  9057. *prepare_pipes |= 1 << intel_crtc->pipe;
  9058. /*
  9059. * For simplicity do a full modeset on any pipe where the output routing
  9060. * changed. We could be more clever, but that would require us to be
  9061. * more careful with calling the relevant encoder->mode_set functions.
  9062. */
  9063. if (*prepare_pipes)
  9064. *modeset_pipes = *prepare_pipes;
  9065. /* ... and mask these out. */
  9066. *modeset_pipes &= ~(*disable_pipes);
  9067. *prepare_pipes &= ~(*disable_pipes);
  9068. /*
  9069. * HACK: We don't (yet) fully support global modesets. intel_set_config
  9070. * obies this rule, but the modeset restore mode of
  9071. * intel_modeset_setup_hw_state does not.
  9072. */
  9073. *modeset_pipes &= 1 << intel_crtc->pipe;
  9074. *prepare_pipes &= 1 << intel_crtc->pipe;
  9075. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  9076. *modeset_pipes, *prepare_pipes, *disable_pipes);
  9077. }
  9078. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  9079. {
  9080. struct drm_encoder *encoder;
  9081. struct drm_device *dev = crtc->dev;
  9082. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  9083. if (encoder->crtc == crtc)
  9084. return true;
  9085. return false;
  9086. }
  9087. static void
  9088. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  9089. {
  9090. struct drm_i915_private *dev_priv = dev->dev_private;
  9091. struct intel_encoder *intel_encoder;
  9092. struct intel_crtc *intel_crtc;
  9093. struct drm_connector *connector;
  9094. intel_shared_dpll_commit(dev_priv);
  9095. for_each_intel_encoder(dev, intel_encoder) {
  9096. if (!intel_encoder->base.crtc)
  9097. continue;
  9098. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  9099. if (prepare_pipes & (1 << intel_crtc->pipe))
  9100. intel_encoder->connectors_active = false;
  9101. }
  9102. intel_modeset_commit_output_state(dev);
  9103. /* Double check state. */
  9104. for_each_intel_crtc(dev, intel_crtc) {
  9105. WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
  9106. WARN_ON(intel_crtc->new_config &&
  9107. intel_crtc->new_config != intel_crtc->config);
  9108. WARN_ON(intel_crtc->base.state->enable != !!intel_crtc->new_config);
  9109. }
  9110. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9111. if (!connector->encoder || !connector->encoder->crtc)
  9112. continue;
  9113. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  9114. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  9115. struct drm_property *dpms_property =
  9116. dev->mode_config.dpms_property;
  9117. connector->dpms = DRM_MODE_DPMS_ON;
  9118. drm_object_property_set_value(&connector->base,
  9119. dpms_property,
  9120. DRM_MODE_DPMS_ON);
  9121. intel_encoder = to_intel_encoder(connector->encoder);
  9122. intel_encoder->connectors_active = true;
  9123. }
  9124. }
  9125. }
  9126. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9127. {
  9128. int diff;
  9129. if (clock1 == clock2)
  9130. return true;
  9131. if (!clock1 || !clock2)
  9132. return false;
  9133. diff = abs(clock1 - clock2);
  9134. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9135. return true;
  9136. return false;
  9137. }
  9138. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  9139. list_for_each_entry((intel_crtc), \
  9140. &(dev)->mode_config.crtc_list, \
  9141. base.head) \
  9142. if (mask & (1 <<(intel_crtc)->pipe))
  9143. static bool
  9144. intel_pipe_config_compare(struct drm_device *dev,
  9145. struct intel_crtc_state *current_config,
  9146. struct intel_crtc_state *pipe_config)
  9147. {
  9148. #define PIPE_CONF_CHECK_X(name) \
  9149. if (current_config->name != pipe_config->name) { \
  9150. DRM_ERROR("mismatch in " #name " " \
  9151. "(expected 0x%08x, found 0x%08x)\n", \
  9152. current_config->name, \
  9153. pipe_config->name); \
  9154. return false; \
  9155. }
  9156. #define PIPE_CONF_CHECK_I(name) \
  9157. if (current_config->name != pipe_config->name) { \
  9158. DRM_ERROR("mismatch in " #name " " \
  9159. "(expected %i, found %i)\n", \
  9160. current_config->name, \
  9161. pipe_config->name); \
  9162. return false; \
  9163. }
  9164. /* This is required for BDW+ where there is only one set of registers for
  9165. * switching between high and low RR.
  9166. * This macro can be used whenever a comparison has to be made between one
  9167. * hw state and multiple sw state variables.
  9168. */
  9169. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  9170. if ((current_config->name != pipe_config->name) && \
  9171. (current_config->alt_name != pipe_config->name)) { \
  9172. DRM_ERROR("mismatch in " #name " " \
  9173. "(expected %i or %i, found %i)\n", \
  9174. current_config->name, \
  9175. current_config->alt_name, \
  9176. pipe_config->name); \
  9177. return false; \
  9178. }
  9179. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9180. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9181. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  9182. "(expected %i, found %i)\n", \
  9183. current_config->name & (mask), \
  9184. pipe_config->name & (mask)); \
  9185. return false; \
  9186. }
  9187. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9188. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9189. DRM_ERROR("mismatch in " #name " " \
  9190. "(expected %i, found %i)\n", \
  9191. current_config->name, \
  9192. pipe_config->name); \
  9193. return false; \
  9194. }
  9195. #define PIPE_CONF_QUIRK(quirk) \
  9196. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9197. PIPE_CONF_CHECK_I(cpu_transcoder);
  9198. PIPE_CONF_CHECK_I(has_pch_encoder);
  9199. PIPE_CONF_CHECK_I(fdi_lanes);
  9200. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  9201. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  9202. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  9203. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  9204. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  9205. PIPE_CONF_CHECK_I(has_dp_encoder);
  9206. if (INTEL_INFO(dev)->gen < 8) {
  9207. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  9208. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  9209. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  9210. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  9211. PIPE_CONF_CHECK_I(dp_m_n.tu);
  9212. if (current_config->has_drrs) {
  9213. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  9214. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  9215. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  9216. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  9217. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  9218. }
  9219. } else {
  9220. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  9221. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  9222. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  9223. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  9224. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  9225. }
  9226. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9227. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9228. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9229. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9230. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9231. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9232. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9233. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9234. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9235. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9236. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9237. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9238. PIPE_CONF_CHECK_I(pixel_multiplier);
  9239. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9240. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  9241. IS_VALLEYVIEW(dev))
  9242. PIPE_CONF_CHECK_I(limited_color_range);
  9243. PIPE_CONF_CHECK_I(has_infoframe);
  9244. PIPE_CONF_CHECK_I(has_audio);
  9245. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9246. DRM_MODE_FLAG_INTERLACE);
  9247. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9248. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9249. DRM_MODE_FLAG_PHSYNC);
  9250. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9251. DRM_MODE_FLAG_NHSYNC);
  9252. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9253. DRM_MODE_FLAG_PVSYNC);
  9254. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9255. DRM_MODE_FLAG_NVSYNC);
  9256. }
  9257. PIPE_CONF_CHECK_I(pipe_src_w);
  9258. PIPE_CONF_CHECK_I(pipe_src_h);
  9259. /*
  9260. * FIXME: BIOS likes to set up a cloned config with lvds+external
  9261. * screen. Since we don't yet re-compute the pipe config when moving
  9262. * just the lvds port away to another pipe the sw tracking won't match.
  9263. *
  9264. * Proper atomic modesets with recomputed global state will fix this.
  9265. * Until then just don't check gmch state for inherited modes.
  9266. */
  9267. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  9268. PIPE_CONF_CHECK_I(gmch_pfit.control);
  9269. /* pfit ratios are autocomputed by the hw on gen4+ */
  9270. if (INTEL_INFO(dev)->gen < 4)
  9271. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  9272. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  9273. }
  9274. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9275. if (current_config->pch_pfit.enabled) {
  9276. PIPE_CONF_CHECK_I(pch_pfit.pos);
  9277. PIPE_CONF_CHECK_I(pch_pfit.size);
  9278. }
  9279. /* BDW+ don't expose a synchronous way to read the state */
  9280. if (IS_HASWELL(dev))
  9281. PIPE_CONF_CHECK_I(ips_enabled);
  9282. PIPE_CONF_CHECK_I(double_wide);
  9283. PIPE_CONF_CHECK_X(ddi_pll_sel);
  9284. PIPE_CONF_CHECK_I(shared_dpll);
  9285. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9286. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9287. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9288. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9289. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9290. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9291. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9292. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9293. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  9294. PIPE_CONF_CHECK_I(pipe_bpp);
  9295. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9296. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9297. #undef PIPE_CONF_CHECK_X
  9298. #undef PIPE_CONF_CHECK_I
  9299. #undef PIPE_CONF_CHECK_I_ALT
  9300. #undef PIPE_CONF_CHECK_FLAGS
  9301. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9302. #undef PIPE_CONF_QUIRK
  9303. return true;
  9304. }
  9305. static void check_wm_state(struct drm_device *dev)
  9306. {
  9307. struct drm_i915_private *dev_priv = dev->dev_private;
  9308. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9309. struct intel_crtc *intel_crtc;
  9310. int plane;
  9311. if (INTEL_INFO(dev)->gen < 9)
  9312. return;
  9313. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9314. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9315. for_each_intel_crtc(dev, intel_crtc) {
  9316. struct skl_ddb_entry *hw_entry, *sw_entry;
  9317. const enum pipe pipe = intel_crtc->pipe;
  9318. if (!intel_crtc->active)
  9319. continue;
  9320. /* planes */
  9321. for_each_plane(dev_priv, pipe, plane) {
  9322. hw_entry = &hw_ddb.plane[pipe][plane];
  9323. sw_entry = &sw_ddb->plane[pipe][plane];
  9324. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  9325. continue;
  9326. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  9327. "(expected (%u,%u), found (%u,%u))\n",
  9328. pipe_name(pipe), plane + 1,
  9329. sw_entry->start, sw_entry->end,
  9330. hw_entry->start, hw_entry->end);
  9331. }
  9332. /* cursor */
  9333. hw_entry = &hw_ddb.cursor[pipe];
  9334. sw_entry = &sw_ddb->cursor[pipe];
  9335. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  9336. continue;
  9337. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  9338. "(expected (%u,%u), found (%u,%u))\n",
  9339. pipe_name(pipe),
  9340. sw_entry->start, sw_entry->end,
  9341. hw_entry->start, hw_entry->end);
  9342. }
  9343. }
  9344. static void
  9345. check_connector_state(struct drm_device *dev)
  9346. {
  9347. struct intel_connector *connector;
  9348. for_each_intel_connector(dev, connector) {
  9349. /* This also checks the encoder/connector hw state with the
  9350. * ->get_hw_state callbacks. */
  9351. intel_connector_check_state(connector);
  9352. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  9353. "connector's staged encoder doesn't match current encoder\n");
  9354. }
  9355. }
  9356. static void
  9357. check_encoder_state(struct drm_device *dev)
  9358. {
  9359. struct intel_encoder *encoder;
  9360. struct intel_connector *connector;
  9361. for_each_intel_encoder(dev, encoder) {
  9362. bool enabled = false;
  9363. bool active = false;
  9364. enum pipe pipe, tracked_pipe;
  9365. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9366. encoder->base.base.id,
  9367. encoder->base.name);
  9368. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  9369. "encoder's stage crtc doesn't match current crtc\n");
  9370. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  9371. "encoder's active_connectors set, but no crtc\n");
  9372. for_each_intel_connector(dev, connector) {
  9373. if (connector->base.encoder != &encoder->base)
  9374. continue;
  9375. enabled = true;
  9376. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  9377. active = true;
  9378. }
  9379. /*
  9380. * for MST connectors if we unplug the connector is gone
  9381. * away but the encoder is still connected to a crtc
  9382. * until a modeset happens in response to the hotplug.
  9383. */
  9384. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  9385. continue;
  9386. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9387. "encoder's enabled state mismatch "
  9388. "(expected %i, found %i)\n",
  9389. !!encoder->base.crtc, enabled);
  9390. I915_STATE_WARN(active && !encoder->base.crtc,
  9391. "active encoder with no crtc\n");
  9392. I915_STATE_WARN(encoder->connectors_active != active,
  9393. "encoder's computed active state doesn't match tracked active state "
  9394. "(expected %i, found %i)\n", active, encoder->connectors_active);
  9395. active = encoder->get_hw_state(encoder, &pipe);
  9396. I915_STATE_WARN(active != encoder->connectors_active,
  9397. "encoder's hw state doesn't match sw tracking "
  9398. "(expected %i, found %i)\n",
  9399. encoder->connectors_active, active);
  9400. if (!encoder->base.crtc)
  9401. continue;
  9402. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  9403. I915_STATE_WARN(active && pipe != tracked_pipe,
  9404. "active encoder's pipe doesn't match"
  9405. "(expected %i, found %i)\n",
  9406. tracked_pipe, pipe);
  9407. }
  9408. }
  9409. static void
  9410. check_crtc_state(struct drm_device *dev)
  9411. {
  9412. struct drm_i915_private *dev_priv = dev->dev_private;
  9413. struct intel_crtc *crtc;
  9414. struct intel_encoder *encoder;
  9415. struct intel_crtc_state pipe_config;
  9416. for_each_intel_crtc(dev, crtc) {
  9417. bool enabled = false;
  9418. bool active = false;
  9419. memset(&pipe_config, 0, sizeof(pipe_config));
  9420. DRM_DEBUG_KMS("[CRTC:%d]\n",
  9421. crtc->base.base.id);
  9422. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  9423. "active crtc, but not enabled in sw tracking\n");
  9424. for_each_intel_encoder(dev, encoder) {
  9425. if (encoder->base.crtc != &crtc->base)
  9426. continue;
  9427. enabled = true;
  9428. if (encoder->connectors_active)
  9429. active = true;
  9430. }
  9431. I915_STATE_WARN(active != crtc->active,
  9432. "crtc's computed active state doesn't match tracked active state "
  9433. "(expected %i, found %i)\n", active, crtc->active);
  9434. I915_STATE_WARN(enabled != crtc->base.state->enable,
  9435. "crtc's computed enabled state doesn't match tracked enabled state "
  9436. "(expected %i, found %i)\n", enabled,
  9437. crtc->base.state->enable);
  9438. active = dev_priv->display.get_pipe_config(crtc,
  9439. &pipe_config);
  9440. /* hw state is inconsistent with the pipe quirk */
  9441. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  9442. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  9443. active = crtc->active;
  9444. for_each_intel_encoder(dev, encoder) {
  9445. enum pipe pipe;
  9446. if (encoder->base.crtc != &crtc->base)
  9447. continue;
  9448. if (encoder->get_hw_state(encoder, &pipe))
  9449. encoder->get_config(encoder, &pipe_config);
  9450. }
  9451. I915_STATE_WARN(crtc->active != active,
  9452. "crtc active state doesn't match with hw state "
  9453. "(expected %i, found %i)\n", crtc->active, active);
  9454. if (active &&
  9455. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  9456. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9457. intel_dump_pipe_config(crtc, &pipe_config,
  9458. "[hw state]");
  9459. intel_dump_pipe_config(crtc, crtc->config,
  9460. "[sw state]");
  9461. }
  9462. }
  9463. }
  9464. static void
  9465. check_shared_dpll_state(struct drm_device *dev)
  9466. {
  9467. struct drm_i915_private *dev_priv = dev->dev_private;
  9468. struct intel_crtc *crtc;
  9469. struct intel_dpll_hw_state dpll_hw_state;
  9470. int i;
  9471. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9472. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9473. int enabled_crtcs = 0, active_crtcs = 0;
  9474. bool active;
  9475. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9476. DRM_DEBUG_KMS("%s\n", pll->name);
  9477. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9478. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  9479. "more active pll users than references: %i vs %i\n",
  9480. pll->active, hweight32(pll->config.crtc_mask));
  9481. I915_STATE_WARN(pll->active && !pll->on,
  9482. "pll in active use but not on in sw tracking\n");
  9483. I915_STATE_WARN(pll->on && !pll->active,
  9484. "pll in on but not on in use in sw tracking\n");
  9485. I915_STATE_WARN(pll->on != active,
  9486. "pll on state mismatch (expected %i, found %i)\n",
  9487. pll->on, active);
  9488. for_each_intel_crtc(dev, crtc) {
  9489. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  9490. enabled_crtcs++;
  9491. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9492. active_crtcs++;
  9493. }
  9494. I915_STATE_WARN(pll->active != active_crtcs,
  9495. "pll active crtcs mismatch (expected %i, found %i)\n",
  9496. pll->active, active_crtcs);
  9497. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9498. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9499. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9500. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9501. sizeof(dpll_hw_state)),
  9502. "pll hw state mismatch\n");
  9503. }
  9504. }
  9505. void
  9506. intel_modeset_check_state(struct drm_device *dev)
  9507. {
  9508. check_wm_state(dev);
  9509. check_connector_state(dev);
  9510. check_encoder_state(dev);
  9511. check_crtc_state(dev);
  9512. check_shared_dpll_state(dev);
  9513. }
  9514. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  9515. int dotclock)
  9516. {
  9517. /*
  9518. * FDI already provided one idea for the dotclock.
  9519. * Yell if the encoder disagrees.
  9520. */
  9521. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  9522. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9523. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  9524. }
  9525. static void update_scanline_offset(struct intel_crtc *crtc)
  9526. {
  9527. struct drm_device *dev = crtc->base.dev;
  9528. /*
  9529. * The scanline counter increments at the leading edge of hsync.
  9530. *
  9531. * On most platforms it starts counting from vtotal-1 on the
  9532. * first active line. That means the scanline counter value is
  9533. * always one less than what we would expect. Ie. just after
  9534. * start of vblank, which also occurs at start of hsync (on the
  9535. * last active line), the scanline counter will read vblank_start-1.
  9536. *
  9537. * On gen2 the scanline counter starts counting from 1 instead
  9538. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9539. * to keep the value positive), instead of adding one.
  9540. *
  9541. * On HSW+ the behaviour of the scanline counter depends on the output
  9542. * type. For DP ports it behaves like most other platforms, but on HDMI
  9543. * there's an extra 1 line difference. So we need to add two instead of
  9544. * one to the value.
  9545. */
  9546. if (IS_GEN2(dev)) {
  9547. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  9548. int vtotal;
  9549. vtotal = mode->crtc_vtotal;
  9550. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9551. vtotal /= 2;
  9552. crtc->scanline_offset = vtotal - 1;
  9553. } else if (HAS_DDI(dev) &&
  9554. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9555. crtc->scanline_offset = 2;
  9556. } else
  9557. crtc->scanline_offset = 1;
  9558. }
  9559. static struct intel_crtc_state *
  9560. intel_modeset_compute_config(struct drm_crtc *crtc,
  9561. struct drm_display_mode *mode,
  9562. struct drm_framebuffer *fb,
  9563. struct drm_atomic_state *state,
  9564. unsigned *modeset_pipes,
  9565. unsigned *prepare_pipes,
  9566. unsigned *disable_pipes)
  9567. {
  9568. struct drm_device *dev = crtc->dev;
  9569. struct intel_crtc_state *pipe_config = NULL;
  9570. struct intel_crtc *intel_crtc;
  9571. int ret = 0;
  9572. ret = drm_atomic_add_affected_connectors(state, crtc);
  9573. if (ret)
  9574. return ERR_PTR(ret);
  9575. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9576. prepare_pipes, disable_pipes);
  9577. for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
  9578. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  9579. if (IS_ERR(pipe_config))
  9580. return pipe_config;
  9581. pipe_config->base.enable = false;
  9582. }
  9583. /*
  9584. * Note this needs changes when we start tracking multiple modes
  9585. * and crtcs. At that point we'll need to compute the whole config
  9586. * (i.e. one pipe_config for each crtc) rather than just the one
  9587. * for this crtc.
  9588. */
  9589. for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
  9590. /* FIXME: For now we still expect modeset_pipes has at most
  9591. * one bit set. */
  9592. if (WARN_ON(&intel_crtc->base != crtc))
  9593. continue;
  9594. pipe_config = intel_modeset_pipe_config(crtc, fb, mode, state);
  9595. if (IS_ERR(pipe_config))
  9596. return pipe_config;
  9597. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9598. "[modeset]");
  9599. }
  9600. return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
  9601. }
  9602. static int __intel_set_mode_setup_plls(struct drm_device *dev,
  9603. unsigned modeset_pipes,
  9604. unsigned disable_pipes)
  9605. {
  9606. struct drm_i915_private *dev_priv = to_i915(dev);
  9607. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9608. struct intel_crtc *intel_crtc;
  9609. int ret = 0;
  9610. if (!dev_priv->display.crtc_compute_clock)
  9611. return 0;
  9612. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9613. if (ret)
  9614. goto done;
  9615. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9616. struct intel_crtc_state *state = intel_crtc->new_config;
  9617. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9618. state);
  9619. if (ret) {
  9620. intel_shared_dpll_abort_config(dev_priv);
  9621. goto done;
  9622. }
  9623. }
  9624. done:
  9625. return ret;
  9626. }
  9627. static int __intel_set_mode(struct drm_crtc *crtc,
  9628. struct drm_display_mode *mode,
  9629. int x, int y, struct drm_framebuffer *fb,
  9630. struct intel_crtc_state *pipe_config,
  9631. unsigned modeset_pipes,
  9632. unsigned prepare_pipes,
  9633. unsigned disable_pipes)
  9634. {
  9635. struct drm_device *dev = crtc->dev;
  9636. struct drm_i915_private *dev_priv = dev->dev_private;
  9637. struct drm_display_mode *saved_mode;
  9638. struct intel_crtc_state *crtc_state_copy = NULL;
  9639. struct intel_crtc *intel_crtc;
  9640. int ret = 0;
  9641. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9642. if (!saved_mode)
  9643. return -ENOMEM;
  9644. crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
  9645. if (!crtc_state_copy) {
  9646. ret = -ENOMEM;
  9647. goto done;
  9648. }
  9649. *saved_mode = crtc->mode;
  9650. if (modeset_pipes)
  9651. to_intel_crtc(crtc)->new_config = pipe_config;
  9652. /*
  9653. * See if the config requires any additional preparation, e.g.
  9654. * to adjust global state with pipes off. We need to do this
  9655. * here so we can get the modeset_pipe updated config for the new
  9656. * mode set on this crtc. For other crtcs we need to use the
  9657. * adjusted_mode bits in the crtc directly.
  9658. */
  9659. if (IS_VALLEYVIEW(dev)) {
  9660. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9661. /* may have added more to prepare_pipes than we should */
  9662. prepare_pipes &= ~disable_pipes;
  9663. }
  9664. ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
  9665. if (ret)
  9666. goto done;
  9667. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9668. intel_crtc_disable(&intel_crtc->base);
  9669. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9670. if (intel_crtc->base.state->enable)
  9671. dev_priv->display.crtc_disable(&intel_crtc->base);
  9672. }
  9673. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9674. * to set it here already despite that we pass it down the callchain.
  9675. *
  9676. * Note we'll need to fix this up when we start tracking multiple
  9677. * pipes; here we assume a single modeset_pipe and only track the
  9678. * single crtc and mode.
  9679. */
  9680. if (modeset_pipes) {
  9681. crtc->mode = *mode;
  9682. /* mode_set/enable/disable functions rely on a correct pipe
  9683. * config. */
  9684. intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
  9685. /*
  9686. * Calculate and store various constants which
  9687. * are later needed by vblank and swap-completion
  9688. * timestamping. They are derived from true hwmode.
  9689. */
  9690. drm_calc_timestamping_constants(crtc,
  9691. &pipe_config->base.adjusted_mode);
  9692. }
  9693. /* Only after disabling all output pipelines that will be changed can we
  9694. * update the the output configuration. */
  9695. intel_modeset_update_state(dev, prepare_pipes);
  9696. modeset_update_crtc_power_domains(pipe_config->base.state);
  9697. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9698. * on the DPLL.
  9699. */
  9700. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9701. struct drm_plane *primary = intel_crtc->base.primary;
  9702. int vdisplay, hdisplay;
  9703. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9704. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9705. fb, 0, 0,
  9706. hdisplay, vdisplay,
  9707. x << 16, y << 16,
  9708. hdisplay << 16, vdisplay << 16);
  9709. }
  9710. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9711. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9712. update_scanline_offset(intel_crtc);
  9713. dev_priv->display.crtc_enable(&intel_crtc->base);
  9714. }
  9715. /* FIXME: add subpixel order */
  9716. done:
  9717. if (ret && crtc->state->enable)
  9718. crtc->mode = *saved_mode;
  9719. if (ret == 0 && pipe_config) {
  9720. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9721. /* The pipe_config will be freed with the atomic state, so
  9722. * make a copy. */
  9723. memcpy(crtc_state_copy, intel_crtc->config,
  9724. sizeof *crtc_state_copy);
  9725. intel_crtc->config = crtc_state_copy;
  9726. intel_crtc->base.state = &crtc_state_copy->base;
  9727. if (modeset_pipes)
  9728. intel_crtc->new_config = intel_crtc->config;
  9729. } else {
  9730. kfree(crtc_state_copy);
  9731. }
  9732. kfree(saved_mode);
  9733. return ret;
  9734. }
  9735. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9736. struct drm_display_mode *mode,
  9737. int x, int y, struct drm_framebuffer *fb,
  9738. struct intel_crtc_state *pipe_config,
  9739. unsigned modeset_pipes,
  9740. unsigned prepare_pipes,
  9741. unsigned disable_pipes)
  9742. {
  9743. int ret;
  9744. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9745. prepare_pipes, disable_pipes);
  9746. if (ret == 0)
  9747. intel_modeset_check_state(crtc->dev);
  9748. return ret;
  9749. }
  9750. static int intel_set_mode(struct drm_crtc *crtc,
  9751. struct drm_display_mode *mode,
  9752. int x, int y, struct drm_framebuffer *fb,
  9753. struct drm_atomic_state *state)
  9754. {
  9755. struct intel_crtc_state *pipe_config;
  9756. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9757. int ret = 0;
  9758. pipe_config = intel_modeset_compute_config(crtc, mode, fb, state,
  9759. &modeset_pipes,
  9760. &prepare_pipes,
  9761. &disable_pipes);
  9762. if (IS_ERR(pipe_config)) {
  9763. ret = PTR_ERR(pipe_config);
  9764. goto out;
  9765. }
  9766. ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9767. modeset_pipes, prepare_pipes,
  9768. disable_pipes);
  9769. if (ret)
  9770. goto out;
  9771. out:
  9772. return ret;
  9773. }
  9774. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9775. {
  9776. struct drm_device *dev = crtc->dev;
  9777. struct drm_atomic_state *state;
  9778. struct intel_encoder *encoder;
  9779. struct intel_connector *connector;
  9780. struct drm_connector_state *connector_state;
  9781. state = drm_atomic_state_alloc(dev);
  9782. if (!state) {
  9783. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  9784. crtc->base.id);
  9785. return;
  9786. }
  9787. state->acquire_ctx = dev->mode_config.acquire_ctx;
  9788. /* The force restore path in the HW readout code relies on the staged
  9789. * config still keeping the user requested config while the actual
  9790. * state has been overwritten by the configuration read from HW. We
  9791. * need to copy the staged config to the atomic state, otherwise the
  9792. * mode set will just reapply the state the HW is already in. */
  9793. for_each_intel_encoder(dev, encoder) {
  9794. if (&encoder->new_crtc->base != crtc)
  9795. continue;
  9796. for_each_intel_connector(dev, connector) {
  9797. if (connector->new_encoder != encoder)
  9798. continue;
  9799. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  9800. if (IS_ERR(connector_state)) {
  9801. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  9802. connector->base.base.id,
  9803. connector->base.name,
  9804. PTR_ERR(connector_state));
  9805. continue;
  9806. }
  9807. connector_state->crtc = crtc;
  9808. connector_state->best_encoder = &encoder->base;
  9809. }
  9810. }
  9811. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
  9812. state);
  9813. drm_atomic_state_free(state);
  9814. }
  9815. #undef for_each_intel_crtc_masked
  9816. static void intel_set_config_free(struct intel_set_config *config)
  9817. {
  9818. if (!config)
  9819. return;
  9820. kfree(config->save_connector_encoders);
  9821. kfree(config->save_encoder_crtcs);
  9822. kfree(config->save_crtc_enabled);
  9823. kfree(config);
  9824. }
  9825. static int intel_set_config_save_state(struct drm_device *dev,
  9826. struct intel_set_config *config)
  9827. {
  9828. struct drm_crtc *crtc;
  9829. struct drm_encoder *encoder;
  9830. struct drm_connector *connector;
  9831. int count;
  9832. config->save_crtc_enabled =
  9833. kcalloc(dev->mode_config.num_crtc,
  9834. sizeof(bool), GFP_KERNEL);
  9835. if (!config->save_crtc_enabled)
  9836. return -ENOMEM;
  9837. config->save_encoder_crtcs =
  9838. kcalloc(dev->mode_config.num_encoder,
  9839. sizeof(struct drm_crtc *), GFP_KERNEL);
  9840. if (!config->save_encoder_crtcs)
  9841. return -ENOMEM;
  9842. config->save_connector_encoders =
  9843. kcalloc(dev->mode_config.num_connector,
  9844. sizeof(struct drm_encoder *), GFP_KERNEL);
  9845. if (!config->save_connector_encoders)
  9846. return -ENOMEM;
  9847. /* Copy data. Note that driver private data is not affected.
  9848. * Should anything bad happen only the expected state is
  9849. * restored, not the drivers personal bookkeeping.
  9850. */
  9851. count = 0;
  9852. for_each_crtc(dev, crtc) {
  9853. config->save_crtc_enabled[count++] = crtc->state->enable;
  9854. }
  9855. count = 0;
  9856. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9857. config->save_encoder_crtcs[count++] = encoder->crtc;
  9858. }
  9859. count = 0;
  9860. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9861. config->save_connector_encoders[count++] = connector->encoder;
  9862. }
  9863. return 0;
  9864. }
  9865. static void intel_set_config_restore_state(struct drm_device *dev,
  9866. struct intel_set_config *config)
  9867. {
  9868. struct intel_crtc *crtc;
  9869. struct intel_encoder *encoder;
  9870. struct intel_connector *connector;
  9871. int count;
  9872. count = 0;
  9873. for_each_intel_crtc(dev, crtc) {
  9874. crtc->new_enabled = config->save_crtc_enabled[count++];
  9875. if (crtc->new_enabled)
  9876. crtc->new_config = crtc->config;
  9877. else
  9878. crtc->new_config = NULL;
  9879. }
  9880. count = 0;
  9881. for_each_intel_encoder(dev, encoder) {
  9882. encoder->new_crtc =
  9883. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9884. }
  9885. count = 0;
  9886. for_each_intel_connector(dev, connector) {
  9887. connector->new_encoder =
  9888. to_intel_encoder(config->save_connector_encoders[count++]);
  9889. }
  9890. }
  9891. static bool
  9892. is_crtc_connector_off(struct drm_mode_set *set)
  9893. {
  9894. int i;
  9895. if (set->num_connectors == 0)
  9896. return false;
  9897. if (WARN_ON(set->connectors == NULL))
  9898. return false;
  9899. for (i = 0; i < set->num_connectors; i++)
  9900. if (set->connectors[i]->encoder &&
  9901. set->connectors[i]->encoder->crtc == set->crtc &&
  9902. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9903. return true;
  9904. return false;
  9905. }
  9906. static void
  9907. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9908. struct intel_set_config *config)
  9909. {
  9910. /* We should be able to check here if the fb has the same properties
  9911. * and then just flip_or_move it */
  9912. if (is_crtc_connector_off(set)) {
  9913. config->mode_changed = true;
  9914. } else if (set->crtc->primary->fb != set->fb) {
  9915. /*
  9916. * If we have no fb, we can only flip as long as the crtc is
  9917. * active, otherwise we need a full mode set. The crtc may
  9918. * be active if we've only disabled the primary plane, or
  9919. * in fastboot situations.
  9920. */
  9921. if (set->crtc->primary->fb == NULL) {
  9922. struct intel_crtc *intel_crtc =
  9923. to_intel_crtc(set->crtc);
  9924. if (intel_crtc->active) {
  9925. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9926. config->fb_changed = true;
  9927. } else {
  9928. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9929. config->mode_changed = true;
  9930. }
  9931. } else if (set->fb == NULL) {
  9932. config->mode_changed = true;
  9933. } else if (set->fb->pixel_format !=
  9934. set->crtc->primary->fb->pixel_format) {
  9935. config->mode_changed = true;
  9936. } else {
  9937. config->fb_changed = true;
  9938. }
  9939. }
  9940. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9941. config->fb_changed = true;
  9942. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9943. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9944. drm_mode_debug_printmodeline(&set->crtc->mode);
  9945. drm_mode_debug_printmodeline(set->mode);
  9946. config->mode_changed = true;
  9947. }
  9948. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9949. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9950. }
  9951. static int
  9952. intel_modeset_stage_output_state(struct drm_device *dev,
  9953. struct drm_mode_set *set,
  9954. struct intel_set_config *config,
  9955. struct drm_atomic_state *state)
  9956. {
  9957. struct intel_connector *connector;
  9958. struct drm_connector_state *connector_state;
  9959. struct intel_encoder *encoder;
  9960. struct intel_crtc *crtc;
  9961. int ro;
  9962. /* The upper layers ensure that we either disable a crtc or have a list
  9963. * of connectors. For paranoia, double-check this. */
  9964. WARN_ON(!set->fb && (set->num_connectors != 0));
  9965. WARN_ON(set->fb && (set->num_connectors == 0));
  9966. for_each_intel_connector(dev, connector) {
  9967. /* Otherwise traverse passed in connector list and get encoders
  9968. * for them. */
  9969. for (ro = 0; ro < set->num_connectors; ro++) {
  9970. if (set->connectors[ro] == &connector->base) {
  9971. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9972. break;
  9973. }
  9974. }
  9975. /* If we disable the crtc, disable all its connectors. Also, if
  9976. * the connector is on the changing crtc but not on the new
  9977. * connector list, disable it. */
  9978. if ((!set->fb || ro == set->num_connectors) &&
  9979. connector->base.encoder &&
  9980. connector->base.encoder->crtc == set->crtc) {
  9981. connector->new_encoder = NULL;
  9982. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9983. connector->base.base.id,
  9984. connector->base.name);
  9985. }
  9986. if (&connector->new_encoder->base != connector->base.encoder) {
  9987. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
  9988. connector->base.base.id,
  9989. connector->base.name);
  9990. config->mode_changed = true;
  9991. }
  9992. }
  9993. /* connector->new_encoder is now updated for all connectors. */
  9994. /* Update crtc of enabled connectors. */
  9995. for_each_intel_connector(dev, connector) {
  9996. struct drm_crtc *new_crtc;
  9997. if (!connector->new_encoder)
  9998. continue;
  9999. new_crtc = connector->new_encoder->base.crtc;
  10000. for (ro = 0; ro < set->num_connectors; ro++) {
  10001. if (set->connectors[ro] == &connector->base)
  10002. new_crtc = set->crtc;
  10003. }
  10004. /* Make sure the new CRTC will work with the encoder */
  10005. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  10006. new_crtc)) {
  10007. return -EINVAL;
  10008. }
  10009. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  10010. connector_state =
  10011. drm_atomic_get_connector_state(state, &connector->base);
  10012. if (IS_ERR(connector_state))
  10013. return PTR_ERR(connector_state);
  10014. connector_state->crtc = new_crtc;
  10015. connector_state->best_encoder = &connector->new_encoder->base;
  10016. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  10017. connector->base.base.id,
  10018. connector->base.name,
  10019. new_crtc->base.id);
  10020. }
  10021. /* Check for any encoders that needs to be disabled. */
  10022. for_each_intel_encoder(dev, encoder) {
  10023. int num_connectors = 0;
  10024. for_each_intel_connector(dev, connector) {
  10025. if (connector->new_encoder == encoder) {
  10026. WARN_ON(!connector->new_encoder->new_crtc);
  10027. num_connectors++;
  10028. }
  10029. }
  10030. if (num_connectors == 0)
  10031. encoder->new_crtc = NULL;
  10032. else if (num_connectors > 1)
  10033. return -EINVAL;
  10034. /* Only now check for crtc changes so we don't miss encoders
  10035. * that will be disabled. */
  10036. if (&encoder->new_crtc->base != encoder->base.crtc) {
  10037. DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
  10038. encoder->base.base.id,
  10039. encoder->base.name);
  10040. config->mode_changed = true;
  10041. }
  10042. }
  10043. /* Now we've also updated encoder->new_crtc for all encoders. */
  10044. for_each_intel_connector(dev, connector) {
  10045. connector_state =
  10046. drm_atomic_get_connector_state(state, &connector->base);
  10047. if (IS_ERR(connector_state))
  10048. return PTR_ERR(connector_state);
  10049. if (connector->new_encoder) {
  10050. if (connector->new_encoder != connector->encoder)
  10051. connector->encoder = connector->new_encoder;
  10052. } else {
  10053. connector_state->crtc = NULL;
  10054. }
  10055. }
  10056. for_each_intel_crtc(dev, crtc) {
  10057. crtc->new_enabled = false;
  10058. for_each_intel_encoder(dev, encoder) {
  10059. if (encoder->new_crtc == crtc) {
  10060. crtc->new_enabled = true;
  10061. break;
  10062. }
  10063. }
  10064. if (crtc->new_enabled != crtc->base.state->enable) {
  10065. DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
  10066. crtc->base.base.id,
  10067. crtc->new_enabled ? "en" : "dis");
  10068. config->mode_changed = true;
  10069. }
  10070. if (crtc->new_enabled)
  10071. crtc->new_config = crtc->config;
  10072. else
  10073. crtc->new_config = NULL;
  10074. }
  10075. return 0;
  10076. }
  10077. static void disable_crtc_nofb(struct intel_crtc *crtc)
  10078. {
  10079. struct drm_device *dev = crtc->base.dev;
  10080. struct intel_encoder *encoder;
  10081. struct intel_connector *connector;
  10082. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  10083. pipe_name(crtc->pipe));
  10084. for_each_intel_connector(dev, connector) {
  10085. if (connector->new_encoder &&
  10086. connector->new_encoder->new_crtc == crtc)
  10087. connector->new_encoder = NULL;
  10088. }
  10089. for_each_intel_encoder(dev, encoder) {
  10090. if (encoder->new_crtc == crtc)
  10091. encoder->new_crtc = NULL;
  10092. }
  10093. crtc->new_enabled = false;
  10094. crtc->new_config = NULL;
  10095. }
  10096. static int intel_crtc_set_config(struct drm_mode_set *set)
  10097. {
  10098. struct drm_device *dev;
  10099. struct drm_mode_set save_set;
  10100. struct drm_atomic_state *state = NULL;
  10101. struct intel_set_config *config;
  10102. struct intel_crtc_state *pipe_config;
  10103. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  10104. int ret;
  10105. BUG_ON(!set);
  10106. BUG_ON(!set->crtc);
  10107. BUG_ON(!set->crtc->helper_private);
  10108. /* Enforce sane interface api - has been abused by the fb helper. */
  10109. BUG_ON(!set->mode && set->fb);
  10110. BUG_ON(set->fb && set->num_connectors == 0);
  10111. if (set->fb) {
  10112. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  10113. set->crtc->base.id, set->fb->base.id,
  10114. (int)set->num_connectors, set->x, set->y);
  10115. } else {
  10116. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  10117. }
  10118. dev = set->crtc->dev;
  10119. ret = -ENOMEM;
  10120. config = kzalloc(sizeof(*config), GFP_KERNEL);
  10121. if (!config)
  10122. goto out_config;
  10123. ret = intel_set_config_save_state(dev, config);
  10124. if (ret)
  10125. goto out_config;
  10126. save_set.crtc = set->crtc;
  10127. save_set.mode = &set->crtc->mode;
  10128. save_set.x = set->crtc->x;
  10129. save_set.y = set->crtc->y;
  10130. save_set.fb = set->crtc->primary->fb;
  10131. /* Compute whether we need a full modeset, only an fb base update or no
  10132. * change at all. In the future we might also check whether only the
  10133. * mode changed, e.g. for LVDS where we only change the panel fitter in
  10134. * such cases. */
  10135. intel_set_config_compute_mode_changes(set, config);
  10136. state = drm_atomic_state_alloc(dev);
  10137. if (!state) {
  10138. ret = -ENOMEM;
  10139. goto out_config;
  10140. }
  10141. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10142. ret = intel_modeset_stage_output_state(dev, set, config, state);
  10143. if (ret)
  10144. goto fail;
  10145. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  10146. set->fb, state,
  10147. &modeset_pipes,
  10148. &prepare_pipes,
  10149. &disable_pipes);
  10150. if (IS_ERR(pipe_config)) {
  10151. ret = PTR_ERR(pipe_config);
  10152. goto fail;
  10153. } else if (pipe_config) {
  10154. if (pipe_config->has_audio !=
  10155. to_intel_crtc(set->crtc)->config->has_audio)
  10156. config->mode_changed = true;
  10157. /*
  10158. * Note we have an issue here with infoframes: current code
  10159. * only updates them on the full mode set path per hw
  10160. * requirements. So here we should be checking for any
  10161. * required changes and forcing a mode set.
  10162. */
  10163. }
  10164. intel_update_pipe_size(to_intel_crtc(set->crtc));
  10165. if (config->mode_changed) {
  10166. ret = intel_set_mode_pipes(set->crtc, set->mode,
  10167. set->x, set->y, set->fb, pipe_config,
  10168. modeset_pipes, prepare_pipes,
  10169. disable_pipes);
  10170. } else if (config->fb_changed) {
  10171. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  10172. struct drm_plane *primary = set->crtc->primary;
  10173. int vdisplay, hdisplay;
  10174. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  10175. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  10176. 0, 0, hdisplay, vdisplay,
  10177. set->x << 16, set->y << 16,
  10178. hdisplay << 16, vdisplay << 16);
  10179. /*
  10180. * We need to make sure the primary plane is re-enabled if it
  10181. * has previously been turned off.
  10182. */
  10183. if (!intel_crtc->primary_enabled && ret == 0) {
  10184. WARN_ON(!intel_crtc->active);
  10185. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  10186. }
  10187. /*
  10188. * In the fastboot case this may be our only check of the
  10189. * state after boot. It would be better to only do it on
  10190. * the first update, but we don't have a nice way of doing that
  10191. * (and really, set_config isn't used much for high freq page
  10192. * flipping, so increasing its cost here shouldn't be a big
  10193. * deal).
  10194. */
  10195. if (i915.fastboot && ret == 0)
  10196. intel_modeset_check_state(set->crtc->dev);
  10197. }
  10198. if (ret) {
  10199. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  10200. set->crtc->base.id, ret);
  10201. fail:
  10202. intel_set_config_restore_state(dev, config);
  10203. drm_atomic_state_clear(state);
  10204. /*
  10205. * HACK: if the pipe was on, but we didn't have a framebuffer,
  10206. * force the pipe off to avoid oopsing in the modeset code
  10207. * due to fb==NULL. This should only happen during boot since
  10208. * we don't yet reconstruct the FB from the hardware state.
  10209. */
  10210. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  10211. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  10212. /* Try to restore the config */
  10213. if (config->mode_changed &&
  10214. intel_set_mode(save_set.crtc, save_set.mode,
  10215. save_set.x, save_set.y, save_set.fb,
  10216. state))
  10217. DRM_ERROR("failed to restore config after modeset failure\n");
  10218. }
  10219. out_config:
  10220. if (state)
  10221. drm_atomic_state_free(state);
  10222. intel_set_config_free(config);
  10223. return ret;
  10224. }
  10225. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10226. .gamma_set = intel_crtc_gamma_set,
  10227. .set_config = intel_crtc_set_config,
  10228. .destroy = intel_crtc_destroy,
  10229. .page_flip = intel_crtc_page_flip,
  10230. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10231. .atomic_destroy_state = intel_crtc_destroy_state,
  10232. };
  10233. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  10234. struct intel_shared_dpll *pll,
  10235. struct intel_dpll_hw_state *hw_state)
  10236. {
  10237. uint32_t val;
  10238. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  10239. return false;
  10240. val = I915_READ(PCH_DPLL(pll->id));
  10241. hw_state->dpll = val;
  10242. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  10243. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  10244. return val & DPLL_VCO_ENABLE;
  10245. }
  10246. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  10247. struct intel_shared_dpll *pll)
  10248. {
  10249. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  10250. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  10251. }
  10252. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  10253. struct intel_shared_dpll *pll)
  10254. {
  10255. /* PCH refclock must be enabled first */
  10256. ibx_assert_pch_refclk_enabled(dev_priv);
  10257. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10258. /* Wait for the clocks to stabilize. */
  10259. POSTING_READ(PCH_DPLL(pll->id));
  10260. udelay(150);
  10261. /* The pixel multiplier can only be updated once the
  10262. * DPLL is enabled and the clocks are stable.
  10263. *
  10264. * So write it again.
  10265. */
  10266. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10267. POSTING_READ(PCH_DPLL(pll->id));
  10268. udelay(200);
  10269. }
  10270. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  10271. struct intel_shared_dpll *pll)
  10272. {
  10273. struct drm_device *dev = dev_priv->dev;
  10274. struct intel_crtc *crtc;
  10275. /* Make sure no transcoder isn't still depending on us. */
  10276. for_each_intel_crtc(dev, crtc) {
  10277. if (intel_crtc_to_shared_dpll(crtc) == pll)
  10278. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  10279. }
  10280. I915_WRITE(PCH_DPLL(pll->id), 0);
  10281. POSTING_READ(PCH_DPLL(pll->id));
  10282. udelay(200);
  10283. }
  10284. static char *ibx_pch_dpll_names[] = {
  10285. "PCH DPLL A",
  10286. "PCH DPLL B",
  10287. };
  10288. static void ibx_pch_dpll_init(struct drm_device *dev)
  10289. {
  10290. struct drm_i915_private *dev_priv = dev->dev_private;
  10291. int i;
  10292. dev_priv->num_shared_dpll = 2;
  10293. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10294. dev_priv->shared_dplls[i].id = i;
  10295. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  10296. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  10297. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  10298. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  10299. dev_priv->shared_dplls[i].get_hw_state =
  10300. ibx_pch_dpll_get_hw_state;
  10301. }
  10302. }
  10303. static void intel_shared_dpll_init(struct drm_device *dev)
  10304. {
  10305. struct drm_i915_private *dev_priv = dev->dev_private;
  10306. if (HAS_DDI(dev))
  10307. intel_ddi_pll_init(dev);
  10308. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  10309. ibx_pch_dpll_init(dev);
  10310. else
  10311. dev_priv->num_shared_dpll = 0;
  10312. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  10313. }
  10314. /**
  10315. * intel_wm_need_update - Check whether watermarks need updating
  10316. * @plane: drm plane
  10317. * @state: new plane state
  10318. *
  10319. * Check current plane state versus the new one to determine whether
  10320. * watermarks need to be recalculated.
  10321. *
  10322. * Returns true or false.
  10323. */
  10324. bool intel_wm_need_update(struct drm_plane *plane,
  10325. struct drm_plane_state *state)
  10326. {
  10327. /* Update watermarks on tiling changes. */
  10328. if (!plane->state->fb || !state->fb ||
  10329. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  10330. plane->state->rotation != state->rotation)
  10331. return true;
  10332. return false;
  10333. }
  10334. /**
  10335. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10336. * @plane: drm plane to prepare for
  10337. * @fb: framebuffer to prepare for presentation
  10338. *
  10339. * Prepares a framebuffer for usage on a display plane. Generally this
  10340. * involves pinning the underlying object and updating the frontbuffer tracking
  10341. * bits. Some older platforms need special physical address handling for
  10342. * cursor planes.
  10343. *
  10344. * Returns 0 on success, negative error code on failure.
  10345. */
  10346. int
  10347. intel_prepare_plane_fb(struct drm_plane *plane,
  10348. struct drm_framebuffer *fb,
  10349. const struct drm_plane_state *new_state)
  10350. {
  10351. struct drm_device *dev = plane->dev;
  10352. struct intel_plane *intel_plane = to_intel_plane(plane);
  10353. enum pipe pipe = intel_plane->pipe;
  10354. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10355. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  10356. unsigned frontbuffer_bits = 0;
  10357. int ret = 0;
  10358. if (!obj)
  10359. return 0;
  10360. switch (plane->type) {
  10361. case DRM_PLANE_TYPE_PRIMARY:
  10362. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  10363. break;
  10364. case DRM_PLANE_TYPE_CURSOR:
  10365. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  10366. break;
  10367. case DRM_PLANE_TYPE_OVERLAY:
  10368. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  10369. break;
  10370. }
  10371. mutex_lock(&dev->struct_mutex);
  10372. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10373. INTEL_INFO(dev)->cursor_needs_physical) {
  10374. int align = IS_I830(dev) ? 16 * 1024 : 256;
  10375. ret = i915_gem_object_attach_phys(obj, align);
  10376. if (ret)
  10377. DRM_DEBUG_KMS("failed to attach phys object\n");
  10378. } else {
  10379. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  10380. }
  10381. if (ret == 0)
  10382. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  10383. mutex_unlock(&dev->struct_mutex);
  10384. return ret;
  10385. }
  10386. /**
  10387. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10388. * @plane: drm plane to clean up for
  10389. * @fb: old framebuffer that was on plane
  10390. *
  10391. * Cleans up a framebuffer that has just been removed from a plane.
  10392. */
  10393. void
  10394. intel_cleanup_plane_fb(struct drm_plane *plane,
  10395. struct drm_framebuffer *fb,
  10396. const struct drm_plane_state *old_state)
  10397. {
  10398. struct drm_device *dev = plane->dev;
  10399. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10400. if (WARN_ON(!obj))
  10401. return;
  10402. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  10403. !INTEL_INFO(dev)->cursor_needs_physical) {
  10404. mutex_lock(&dev->struct_mutex);
  10405. intel_unpin_fb_obj(fb, old_state);
  10406. mutex_unlock(&dev->struct_mutex);
  10407. }
  10408. }
  10409. static int
  10410. intel_check_primary_plane(struct drm_plane *plane,
  10411. struct intel_plane_state *state)
  10412. {
  10413. struct drm_device *dev = plane->dev;
  10414. struct drm_i915_private *dev_priv = dev->dev_private;
  10415. struct drm_crtc *crtc = state->base.crtc;
  10416. struct intel_crtc *intel_crtc;
  10417. struct drm_framebuffer *fb = state->base.fb;
  10418. struct drm_rect *dest = &state->dst;
  10419. struct drm_rect *src = &state->src;
  10420. const struct drm_rect *clip = &state->clip;
  10421. int ret;
  10422. crtc = crtc ? crtc : plane->crtc;
  10423. intel_crtc = to_intel_crtc(crtc);
  10424. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10425. src, dest, clip,
  10426. DRM_PLANE_HELPER_NO_SCALING,
  10427. DRM_PLANE_HELPER_NO_SCALING,
  10428. false, true, &state->visible);
  10429. if (ret)
  10430. return ret;
  10431. if (intel_crtc->active) {
  10432. intel_crtc->atomic.wait_for_flips = true;
  10433. /*
  10434. * FBC does not work on some platforms for rotated
  10435. * planes, so disable it when rotation is not 0 and
  10436. * update it when rotation is set back to 0.
  10437. *
  10438. * FIXME: This is redundant with the fbc update done in
  10439. * the primary plane enable function except that that
  10440. * one is done too late. We eventually need to unify
  10441. * this.
  10442. */
  10443. if (intel_crtc->primary_enabled &&
  10444. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  10445. dev_priv->fbc.crtc == intel_crtc &&
  10446. state->base.rotation != BIT(DRM_ROTATE_0)) {
  10447. intel_crtc->atomic.disable_fbc = true;
  10448. }
  10449. if (state->visible) {
  10450. /*
  10451. * BDW signals flip done immediately if the plane
  10452. * is disabled, even if the plane enable is already
  10453. * armed to occur at the next vblank :(
  10454. */
  10455. if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
  10456. intel_crtc->atomic.wait_vblank = true;
  10457. }
  10458. intel_crtc->atomic.fb_bits |=
  10459. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  10460. intel_crtc->atomic.update_fbc = true;
  10461. if (intel_wm_need_update(plane, &state->base))
  10462. intel_crtc->atomic.update_wm = true;
  10463. }
  10464. return 0;
  10465. }
  10466. static void
  10467. intel_commit_primary_plane(struct drm_plane *plane,
  10468. struct intel_plane_state *state)
  10469. {
  10470. struct drm_crtc *crtc = state->base.crtc;
  10471. struct drm_framebuffer *fb = state->base.fb;
  10472. struct drm_device *dev = plane->dev;
  10473. struct drm_i915_private *dev_priv = dev->dev_private;
  10474. struct intel_crtc *intel_crtc;
  10475. struct drm_rect *src = &state->src;
  10476. crtc = crtc ? crtc : plane->crtc;
  10477. intel_crtc = to_intel_crtc(crtc);
  10478. plane->fb = fb;
  10479. crtc->x = src->x1 >> 16;
  10480. crtc->y = src->y1 >> 16;
  10481. if (intel_crtc->active) {
  10482. if (state->visible) {
  10483. /* FIXME: kill this fastboot hack */
  10484. intel_update_pipe_size(intel_crtc);
  10485. intel_crtc->primary_enabled = true;
  10486. dev_priv->display.update_primary_plane(crtc, plane->fb,
  10487. crtc->x, crtc->y);
  10488. } else {
  10489. /*
  10490. * If clipping results in a non-visible primary plane,
  10491. * we'll disable the primary plane. Note that this is
  10492. * a bit different than what happens if userspace
  10493. * explicitly disables the plane by passing fb=0
  10494. * because plane->fb still gets set and pinned.
  10495. */
  10496. intel_disable_primary_hw_plane(plane, crtc);
  10497. }
  10498. }
  10499. }
  10500. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  10501. {
  10502. struct drm_device *dev = crtc->dev;
  10503. struct drm_i915_private *dev_priv = dev->dev_private;
  10504. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10505. struct intel_plane *intel_plane;
  10506. struct drm_plane *p;
  10507. unsigned fb_bits = 0;
  10508. /* Track fb's for any planes being disabled */
  10509. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  10510. intel_plane = to_intel_plane(p);
  10511. if (intel_crtc->atomic.disabled_planes &
  10512. (1 << drm_plane_index(p))) {
  10513. switch (p->type) {
  10514. case DRM_PLANE_TYPE_PRIMARY:
  10515. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  10516. break;
  10517. case DRM_PLANE_TYPE_CURSOR:
  10518. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  10519. break;
  10520. case DRM_PLANE_TYPE_OVERLAY:
  10521. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  10522. break;
  10523. }
  10524. mutex_lock(&dev->struct_mutex);
  10525. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  10526. mutex_unlock(&dev->struct_mutex);
  10527. }
  10528. }
  10529. if (intel_crtc->atomic.wait_for_flips)
  10530. intel_crtc_wait_for_pending_flips(crtc);
  10531. if (intel_crtc->atomic.disable_fbc)
  10532. intel_fbc_disable(dev);
  10533. if (intel_crtc->atomic.pre_disable_primary)
  10534. intel_pre_disable_primary(crtc);
  10535. if (intel_crtc->atomic.update_wm)
  10536. intel_update_watermarks(crtc);
  10537. intel_runtime_pm_get(dev_priv);
  10538. /* Perform vblank evasion around commit operation */
  10539. if (intel_crtc->active)
  10540. intel_crtc->atomic.evade =
  10541. intel_pipe_update_start(intel_crtc,
  10542. &intel_crtc->atomic.start_vbl_count);
  10543. }
  10544. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  10545. {
  10546. struct drm_device *dev = crtc->dev;
  10547. struct drm_i915_private *dev_priv = dev->dev_private;
  10548. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10549. struct drm_plane *p;
  10550. if (intel_crtc->atomic.evade)
  10551. intel_pipe_update_end(intel_crtc,
  10552. intel_crtc->atomic.start_vbl_count);
  10553. intel_runtime_pm_put(dev_priv);
  10554. if (intel_crtc->atomic.wait_vblank)
  10555. intel_wait_for_vblank(dev, intel_crtc->pipe);
  10556. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  10557. if (intel_crtc->atomic.update_fbc) {
  10558. mutex_lock(&dev->struct_mutex);
  10559. intel_fbc_update(dev);
  10560. mutex_unlock(&dev->struct_mutex);
  10561. }
  10562. if (intel_crtc->atomic.post_enable_primary)
  10563. intel_post_enable_primary(crtc);
  10564. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  10565. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  10566. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  10567. false, false);
  10568. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  10569. }
  10570. /**
  10571. * intel_plane_destroy - destroy a plane
  10572. * @plane: plane to destroy
  10573. *
  10574. * Common destruction function for all types of planes (primary, cursor,
  10575. * sprite).
  10576. */
  10577. void intel_plane_destroy(struct drm_plane *plane)
  10578. {
  10579. struct intel_plane *intel_plane = to_intel_plane(plane);
  10580. drm_plane_cleanup(plane);
  10581. kfree(intel_plane);
  10582. }
  10583. const struct drm_plane_funcs intel_plane_funcs = {
  10584. .update_plane = drm_plane_helper_update,
  10585. .disable_plane = drm_plane_helper_disable,
  10586. .destroy = intel_plane_destroy,
  10587. .set_property = drm_atomic_helper_plane_set_property,
  10588. .atomic_get_property = intel_plane_atomic_get_property,
  10589. .atomic_set_property = intel_plane_atomic_set_property,
  10590. .atomic_duplicate_state = intel_plane_duplicate_state,
  10591. .atomic_destroy_state = intel_plane_destroy_state,
  10592. };
  10593. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10594. int pipe)
  10595. {
  10596. struct intel_plane *primary;
  10597. struct intel_plane_state *state;
  10598. const uint32_t *intel_primary_formats;
  10599. int num_formats;
  10600. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10601. if (primary == NULL)
  10602. return NULL;
  10603. state = intel_create_plane_state(&primary->base);
  10604. if (!state) {
  10605. kfree(primary);
  10606. return NULL;
  10607. }
  10608. primary->base.state = &state->base;
  10609. primary->can_scale = false;
  10610. primary->max_downscale = 1;
  10611. primary->pipe = pipe;
  10612. primary->plane = pipe;
  10613. primary->check_plane = intel_check_primary_plane;
  10614. primary->commit_plane = intel_commit_primary_plane;
  10615. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10616. primary->plane = !pipe;
  10617. if (INTEL_INFO(dev)->gen <= 3) {
  10618. intel_primary_formats = intel_primary_formats_gen2;
  10619. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10620. } else {
  10621. intel_primary_formats = intel_primary_formats_gen4;
  10622. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10623. }
  10624. drm_universal_plane_init(dev, &primary->base, 0,
  10625. &intel_plane_funcs,
  10626. intel_primary_formats, num_formats,
  10627. DRM_PLANE_TYPE_PRIMARY);
  10628. if (INTEL_INFO(dev)->gen >= 4) {
  10629. if (!dev->mode_config.rotation_property)
  10630. dev->mode_config.rotation_property =
  10631. drm_mode_create_rotation_property(dev,
  10632. BIT(DRM_ROTATE_0) |
  10633. BIT(DRM_ROTATE_180));
  10634. if (dev->mode_config.rotation_property)
  10635. drm_object_attach_property(&primary->base.base,
  10636. dev->mode_config.rotation_property,
  10637. state->base.rotation);
  10638. }
  10639. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  10640. return &primary->base;
  10641. }
  10642. static int
  10643. intel_check_cursor_plane(struct drm_plane *plane,
  10644. struct intel_plane_state *state)
  10645. {
  10646. struct drm_crtc *crtc = state->base.crtc;
  10647. struct drm_device *dev = plane->dev;
  10648. struct drm_framebuffer *fb = state->base.fb;
  10649. struct drm_rect *dest = &state->dst;
  10650. struct drm_rect *src = &state->src;
  10651. const struct drm_rect *clip = &state->clip;
  10652. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10653. struct intel_crtc *intel_crtc;
  10654. unsigned stride;
  10655. int ret;
  10656. crtc = crtc ? crtc : plane->crtc;
  10657. intel_crtc = to_intel_crtc(crtc);
  10658. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10659. src, dest, clip,
  10660. DRM_PLANE_HELPER_NO_SCALING,
  10661. DRM_PLANE_HELPER_NO_SCALING,
  10662. true, true, &state->visible);
  10663. if (ret)
  10664. return ret;
  10665. /* if we want to turn off the cursor ignore width and height */
  10666. if (!obj)
  10667. goto finish;
  10668. /* Check for which cursor types we support */
  10669. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  10670. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  10671. state->base.crtc_w, state->base.crtc_h);
  10672. return -EINVAL;
  10673. }
  10674. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  10675. if (obj->base.size < stride * state->base.crtc_h) {
  10676. DRM_DEBUG_KMS("buffer is too small\n");
  10677. return -ENOMEM;
  10678. }
  10679. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  10680. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10681. ret = -EINVAL;
  10682. }
  10683. finish:
  10684. if (intel_crtc->active) {
  10685. if (plane->state->crtc_w != state->base.crtc_w)
  10686. intel_crtc->atomic.update_wm = true;
  10687. intel_crtc->atomic.fb_bits |=
  10688. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  10689. }
  10690. return ret;
  10691. }
  10692. static void
  10693. intel_commit_cursor_plane(struct drm_plane *plane,
  10694. struct intel_plane_state *state)
  10695. {
  10696. struct drm_crtc *crtc = state->base.crtc;
  10697. struct drm_device *dev = plane->dev;
  10698. struct intel_crtc *intel_crtc;
  10699. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10700. uint32_t addr;
  10701. crtc = crtc ? crtc : plane->crtc;
  10702. intel_crtc = to_intel_crtc(crtc);
  10703. plane->fb = state->base.fb;
  10704. crtc->cursor_x = state->base.crtc_x;
  10705. crtc->cursor_y = state->base.crtc_y;
  10706. if (intel_crtc->cursor_bo == obj)
  10707. goto update;
  10708. if (!obj)
  10709. addr = 0;
  10710. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10711. addr = i915_gem_obj_ggtt_offset(obj);
  10712. else
  10713. addr = obj->phys_handle->busaddr;
  10714. intel_crtc->cursor_addr = addr;
  10715. intel_crtc->cursor_bo = obj;
  10716. update:
  10717. if (intel_crtc->active)
  10718. intel_crtc_update_cursor(crtc, state->visible);
  10719. }
  10720. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10721. int pipe)
  10722. {
  10723. struct intel_plane *cursor;
  10724. struct intel_plane_state *state;
  10725. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10726. if (cursor == NULL)
  10727. return NULL;
  10728. state = intel_create_plane_state(&cursor->base);
  10729. if (!state) {
  10730. kfree(cursor);
  10731. return NULL;
  10732. }
  10733. cursor->base.state = &state->base;
  10734. cursor->can_scale = false;
  10735. cursor->max_downscale = 1;
  10736. cursor->pipe = pipe;
  10737. cursor->plane = pipe;
  10738. cursor->check_plane = intel_check_cursor_plane;
  10739. cursor->commit_plane = intel_commit_cursor_plane;
  10740. drm_universal_plane_init(dev, &cursor->base, 0,
  10741. &intel_plane_funcs,
  10742. intel_cursor_formats,
  10743. ARRAY_SIZE(intel_cursor_formats),
  10744. DRM_PLANE_TYPE_CURSOR);
  10745. if (INTEL_INFO(dev)->gen >= 4) {
  10746. if (!dev->mode_config.rotation_property)
  10747. dev->mode_config.rotation_property =
  10748. drm_mode_create_rotation_property(dev,
  10749. BIT(DRM_ROTATE_0) |
  10750. BIT(DRM_ROTATE_180));
  10751. if (dev->mode_config.rotation_property)
  10752. drm_object_attach_property(&cursor->base.base,
  10753. dev->mode_config.rotation_property,
  10754. state->base.rotation);
  10755. }
  10756. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  10757. return &cursor->base;
  10758. }
  10759. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10760. {
  10761. struct drm_i915_private *dev_priv = dev->dev_private;
  10762. struct intel_crtc *intel_crtc;
  10763. struct intel_crtc_state *crtc_state = NULL;
  10764. struct drm_plane *primary = NULL;
  10765. struct drm_plane *cursor = NULL;
  10766. int i, ret;
  10767. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10768. if (intel_crtc == NULL)
  10769. return;
  10770. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  10771. if (!crtc_state)
  10772. goto fail;
  10773. intel_crtc_set_state(intel_crtc, crtc_state);
  10774. crtc_state->base.crtc = &intel_crtc->base;
  10775. primary = intel_primary_plane_create(dev, pipe);
  10776. if (!primary)
  10777. goto fail;
  10778. cursor = intel_cursor_plane_create(dev, pipe);
  10779. if (!cursor)
  10780. goto fail;
  10781. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10782. cursor, &intel_crtc_funcs);
  10783. if (ret)
  10784. goto fail;
  10785. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10786. for (i = 0; i < 256; i++) {
  10787. intel_crtc->lut_r[i] = i;
  10788. intel_crtc->lut_g[i] = i;
  10789. intel_crtc->lut_b[i] = i;
  10790. }
  10791. /*
  10792. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10793. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10794. */
  10795. intel_crtc->pipe = pipe;
  10796. intel_crtc->plane = pipe;
  10797. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10798. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10799. intel_crtc->plane = !pipe;
  10800. }
  10801. intel_crtc->cursor_base = ~0;
  10802. intel_crtc->cursor_cntl = ~0;
  10803. intel_crtc->cursor_size = ~0;
  10804. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10805. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10806. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10807. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10808. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10809. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10810. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10811. return;
  10812. fail:
  10813. if (primary)
  10814. drm_plane_cleanup(primary);
  10815. if (cursor)
  10816. drm_plane_cleanup(cursor);
  10817. kfree(crtc_state);
  10818. kfree(intel_crtc);
  10819. }
  10820. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10821. {
  10822. struct drm_encoder *encoder = connector->base.encoder;
  10823. struct drm_device *dev = connector->base.dev;
  10824. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10825. if (!encoder || WARN_ON(!encoder->crtc))
  10826. return INVALID_PIPE;
  10827. return to_intel_crtc(encoder->crtc)->pipe;
  10828. }
  10829. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10830. struct drm_file *file)
  10831. {
  10832. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10833. struct drm_crtc *drmmode_crtc;
  10834. struct intel_crtc *crtc;
  10835. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10836. if (!drmmode_crtc) {
  10837. DRM_ERROR("no such CRTC id\n");
  10838. return -ENOENT;
  10839. }
  10840. crtc = to_intel_crtc(drmmode_crtc);
  10841. pipe_from_crtc_id->pipe = crtc->pipe;
  10842. return 0;
  10843. }
  10844. static int intel_encoder_clones(struct intel_encoder *encoder)
  10845. {
  10846. struct drm_device *dev = encoder->base.dev;
  10847. struct intel_encoder *source_encoder;
  10848. int index_mask = 0;
  10849. int entry = 0;
  10850. for_each_intel_encoder(dev, source_encoder) {
  10851. if (encoders_cloneable(encoder, source_encoder))
  10852. index_mask |= (1 << entry);
  10853. entry++;
  10854. }
  10855. return index_mask;
  10856. }
  10857. static bool has_edp_a(struct drm_device *dev)
  10858. {
  10859. struct drm_i915_private *dev_priv = dev->dev_private;
  10860. if (!IS_MOBILE(dev))
  10861. return false;
  10862. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10863. return false;
  10864. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10865. return false;
  10866. return true;
  10867. }
  10868. static bool intel_crt_present(struct drm_device *dev)
  10869. {
  10870. struct drm_i915_private *dev_priv = dev->dev_private;
  10871. if (INTEL_INFO(dev)->gen >= 9)
  10872. return false;
  10873. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10874. return false;
  10875. if (IS_CHERRYVIEW(dev))
  10876. return false;
  10877. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10878. return false;
  10879. return true;
  10880. }
  10881. static void intel_setup_outputs(struct drm_device *dev)
  10882. {
  10883. struct drm_i915_private *dev_priv = dev->dev_private;
  10884. struct intel_encoder *encoder;
  10885. bool dpd_is_edp = false;
  10886. intel_lvds_init(dev);
  10887. if (intel_crt_present(dev))
  10888. intel_crt_init(dev);
  10889. if (HAS_DDI(dev)) {
  10890. int found;
  10891. /*
  10892. * Haswell uses DDI functions to detect digital outputs.
  10893. * On SKL pre-D0 the strap isn't connected, so we assume
  10894. * it's there.
  10895. */
  10896. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10897. /* WaIgnoreDDIAStrap: skl */
  10898. if (found ||
  10899. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  10900. intel_ddi_init(dev, PORT_A);
  10901. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10902. * register */
  10903. found = I915_READ(SFUSE_STRAP);
  10904. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10905. intel_ddi_init(dev, PORT_B);
  10906. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10907. intel_ddi_init(dev, PORT_C);
  10908. if (found & SFUSE_STRAP_DDID_DETECTED)
  10909. intel_ddi_init(dev, PORT_D);
  10910. } else if (HAS_PCH_SPLIT(dev)) {
  10911. int found;
  10912. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10913. if (has_edp_a(dev))
  10914. intel_dp_init(dev, DP_A, PORT_A);
  10915. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10916. /* PCH SDVOB multiplex with HDMIB */
  10917. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10918. if (!found)
  10919. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10920. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10921. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10922. }
  10923. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10924. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10925. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10926. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10927. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10928. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10929. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10930. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10931. } else if (IS_VALLEYVIEW(dev)) {
  10932. /*
  10933. * The DP_DETECTED bit is the latched state of the DDC
  10934. * SDA pin at boot. However since eDP doesn't require DDC
  10935. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10936. * eDP ports may have been muxed to an alternate function.
  10937. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10938. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10939. * detect eDP ports.
  10940. */
  10941. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  10942. !intel_dp_is_edp(dev, PORT_B))
  10943. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10944. PORT_B);
  10945. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10946. intel_dp_is_edp(dev, PORT_B))
  10947. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10948. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  10949. !intel_dp_is_edp(dev, PORT_C))
  10950. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10951. PORT_C);
  10952. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10953. intel_dp_is_edp(dev, PORT_C))
  10954. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10955. if (IS_CHERRYVIEW(dev)) {
  10956. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10957. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10958. PORT_D);
  10959. /* eDP not supported on port D, so don't check VBT */
  10960. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10961. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10962. }
  10963. intel_dsi_init(dev);
  10964. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10965. bool found = false;
  10966. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10967. DRM_DEBUG_KMS("probing SDVOB\n");
  10968. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10969. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10970. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10971. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10972. }
  10973. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10974. intel_dp_init(dev, DP_B, PORT_B);
  10975. }
  10976. /* Before G4X SDVOC doesn't have its own detect register */
  10977. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10978. DRM_DEBUG_KMS("probing SDVOC\n");
  10979. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10980. }
  10981. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10982. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10983. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10984. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10985. }
  10986. if (SUPPORTS_INTEGRATED_DP(dev))
  10987. intel_dp_init(dev, DP_C, PORT_C);
  10988. }
  10989. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10990. (I915_READ(DP_D) & DP_DETECTED))
  10991. intel_dp_init(dev, DP_D, PORT_D);
  10992. } else if (IS_GEN2(dev))
  10993. intel_dvo_init(dev);
  10994. if (SUPPORTS_TV(dev))
  10995. intel_tv_init(dev);
  10996. intel_psr_init(dev);
  10997. for_each_intel_encoder(dev, encoder) {
  10998. encoder->base.possible_crtcs = encoder->crtc_mask;
  10999. encoder->base.possible_clones =
  11000. intel_encoder_clones(encoder);
  11001. }
  11002. intel_init_pch_refclk(dev);
  11003. drm_helper_move_panel_connectors_to_head(dev);
  11004. }
  11005. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11006. {
  11007. struct drm_device *dev = fb->dev;
  11008. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11009. drm_framebuffer_cleanup(fb);
  11010. mutex_lock(&dev->struct_mutex);
  11011. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11012. drm_gem_object_unreference(&intel_fb->obj->base);
  11013. mutex_unlock(&dev->struct_mutex);
  11014. kfree(intel_fb);
  11015. }
  11016. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11017. struct drm_file *file,
  11018. unsigned int *handle)
  11019. {
  11020. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11021. struct drm_i915_gem_object *obj = intel_fb->obj;
  11022. return drm_gem_handle_create(file, &obj->base, handle);
  11023. }
  11024. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11025. .destroy = intel_user_framebuffer_destroy,
  11026. .create_handle = intel_user_framebuffer_create_handle,
  11027. };
  11028. static
  11029. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11030. uint32_t pixel_format)
  11031. {
  11032. u32 gen = INTEL_INFO(dev)->gen;
  11033. if (gen >= 9) {
  11034. /* "The stride in bytes must not exceed the of the size of 8K
  11035. * pixels and 32K bytes."
  11036. */
  11037. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11038. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11039. return 32*1024;
  11040. } else if (gen >= 4) {
  11041. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11042. return 16*1024;
  11043. else
  11044. return 32*1024;
  11045. } else if (gen >= 3) {
  11046. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11047. return 8*1024;
  11048. else
  11049. return 16*1024;
  11050. } else {
  11051. /* XXX DSPC is limited to 4k tiled */
  11052. return 8*1024;
  11053. }
  11054. }
  11055. static int intel_framebuffer_init(struct drm_device *dev,
  11056. struct intel_framebuffer *intel_fb,
  11057. struct drm_mode_fb_cmd2 *mode_cmd,
  11058. struct drm_i915_gem_object *obj)
  11059. {
  11060. unsigned int aligned_height;
  11061. int ret;
  11062. u32 pitch_limit, stride_alignment;
  11063. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11064. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11065. /* Enforce that fb modifier and tiling mode match, but only for
  11066. * X-tiled. This is needed for FBC. */
  11067. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11068. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11069. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11070. return -EINVAL;
  11071. }
  11072. } else {
  11073. if (obj->tiling_mode == I915_TILING_X)
  11074. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11075. else if (obj->tiling_mode == I915_TILING_Y) {
  11076. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11077. return -EINVAL;
  11078. }
  11079. }
  11080. /* Passed in modifier sanity checking. */
  11081. switch (mode_cmd->modifier[0]) {
  11082. case I915_FORMAT_MOD_Y_TILED:
  11083. case I915_FORMAT_MOD_Yf_TILED:
  11084. if (INTEL_INFO(dev)->gen < 9) {
  11085. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11086. mode_cmd->modifier[0]);
  11087. return -EINVAL;
  11088. }
  11089. case DRM_FORMAT_MOD_NONE:
  11090. case I915_FORMAT_MOD_X_TILED:
  11091. break;
  11092. default:
  11093. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11094. mode_cmd->modifier[0]);
  11095. return -EINVAL;
  11096. }
  11097. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11098. mode_cmd->pixel_format);
  11099. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11100. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11101. mode_cmd->pitches[0], stride_alignment);
  11102. return -EINVAL;
  11103. }
  11104. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11105. mode_cmd->pixel_format);
  11106. if (mode_cmd->pitches[0] > pitch_limit) {
  11107. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11108. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11109. "tiled" : "linear",
  11110. mode_cmd->pitches[0], pitch_limit);
  11111. return -EINVAL;
  11112. }
  11113. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11114. mode_cmd->pitches[0] != obj->stride) {
  11115. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11116. mode_cmd->pitches[0], obj->stride);
  11117. return -EINVAL;
  11118. }
  11119. /* Reject formats not supported by any plane early. */
  11120. switch (mode_cmd->pixel_format) {
  11121. case DRM_FORMAT_C8:
  11122. case DRM_FORMAT_RGB565:
  11123. case DRM_FORMAT_XRGB8888:
  11124. case DRM_FORMAT_ARGB8888:
  11125. break;
  11126. case DRM_FORMAT_XRGB1555:
  11127. case DRM_FORMAT_ARGB1555:
  11128. if (INTEL_INFO(dev)->gen > 3) {
  11129. DRM_DEBUG("unsupported pixel format: %s\n",
  11130. drm_get_format_name(mode_cmd->pixel_format));
  11131. return -EINVAL;
  11132. }
  11133. break;
  11134. case DRM_FORMAT_XBGR8888:
  11135. case DRM_FORMAT_ABGR8888:
  11136. case DRM_FORMAT_XRGB2101010:
  11137. case DRM_FORMAT_ARGB2101010:
  11138. case DRM_FORMAT_XBGR2101010:
  11139. case DRM_FORMAT_ABGR2101010:
  11140. if (INTEL_INFO(dev)->gen < 4) {
  11141. DRM_DEBUG("unsupported pixel format: %s\n",
  11142. drm_get_format_name(mode_cmd->pixel_format));
  11143. return -EINVAL;
  11144. }
  11145. break;
  11146. case DRM_FORMAT_YUYV:
  11147. case DRM_FORMAT_UYVY:
  11148. case DRM_FORMAT_YVYU:
  11149. case DRM_FORMAT_VYUY:
  11150. if (INTEL_INFO(dev)->gen < 5) {
  11151. DRM_DEBUG("unsupported pixel format: %s\n",
  11152. drm_get_format_name(mode_cmd->pixel_format));
  11153. return -EINVAL;
  11154. }
  11155. break;
  11156. default:
  11157. DRM_DEBUG("unsupported pixel format: %s\n",
  11158. drm_get_format_name(mode_cmd->pixel_format));
  11159. return -EINVAL;
  11160. }
  11161. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11162. if (mode_cmd->offsets[0] != 0)
  11163. return -EINVAL;
  11164. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11165. mode_cmd->pixel_format,
  11166. mode_cmd->modifier[0]);
  11167. /* FIXME drm helper for size checks (especially planar formats)? */
  11168. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11169. return -EINVAL;
  11170. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11171. intel_fb->obj = obj;
  11172. intel_fb->obj->framebuffer_references++;
  11173. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11174. if (ret) {
  11175. DRM_ERROR("framebuffer init failed %d\n", ret);
  11176. return ret;
  11177. }
  11178. return 0;
  11179. }
  11180. static struct drm_framebuffer *
  11181. intel_user_framebuffer_create(struct drm_device *dev,
  11182. struct drm_file *filp,
  11183. struct drm_mode_fb_cmd2 *mode_cmd)
  11184. {
  11185. struct drm_i915_gem_object *obj;
  11186. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11187. mode_cmd->handles[0]));
  11188. if (&obj->base == NULL)
  11189. return ERR_PTR(-ENOENT);
  11190. return intel_framebuffer_create(dev, mode_cmd, obj);
  11191. }
  11192. #ifndef CONFIG_DRM_I915_FBDEV
  11193. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  11194. {
  11195. }
  11196. #endif
  11197. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11198. .fb_create = intel_user_framebuffer_create,
  11199. .output_poll_changed = intel_fbdev_output_poll_changed,
  11200. .atomic_check = intel_atomic_check,
  11201. .atomic_commit = intel_atomic_commit,
  11202. };
  11203. /* Set up chip specific display functions */
  11204. static void intel_init_display(struct drm_device *dev)
  11205. {
  11206. struct drm_i915_private *dev_priv = dev->dev_private;
  11207. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  11208. dev_priv->display.find_dpll = g4x_find_best_dpll;
  11209. else if (IS_CHERRYVIEW(dev))
  11210. dev_priv->display.find_dpll = chv_find_best_dpll;
  11211. else if (IS_VALLEYVIEW(dev))
  11212. dev_priv->display.find_dpll = vlv_find_best_dpll;
  11213. else if (IS_PINEVIEW(dev))
  11214. dev_priv->display.find_dpll = pnv_find_best_dpll;
  11215. else
  11216. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  11217. if (INTEL_INFO(dev)->gen >= 9) {
  11218. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11219. dev_priv->display.get_initial_plane_config =
  11220. skylake_get_initial_plane_config;
  11221. dev_priv->display.crtc_compute_clock =
  11222. haswell_crtc_compute_clock;
  11223. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11224. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11225. dev_priv->display.off = ironlake_crtc_off;
  11226. dev_priv->display.update_primary_plane =
  11227. skylake_update_primary_plane;
  11228. } else if (HAS_DDI(dev)) {
  11229. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11230. dev_priv->display.get_initial_plane_config =
  11231. ironlake_get_initial_plane_config;
  11232. dev_priv->display.crtc_compute_clock =
  11233. haswell_crtc_compute_clock;
  11234. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11235. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11236. dev_priv->display.off = ironlake_crtc_off;
  11237. dev_priv->display.update_primary_plane =
  11238. ironlake_update_primary_plane;
  11239. } else if (HAS_PCH_SPLIT(dev)) {
  11240. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11241. dev_priv->display.get_initial_plane_config =
  11242. ironlake_get_initial_plane_config;
  11243. dev_priv->display.crtc_compute_clock =
  11244. ironlake_crtc_compute_clock;
  11245. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11246. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11247. dev_priv->display.off = ironlake_crtc_off;
  11248. dev_priv->display.update_primary_plane =
  11249. ironlake_update_primary_plane;
  11250. } else if (IS_VALLEYVIEW(dev)) {
  11251. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11252. dev_priv->display.get_initial_plane_config =
  11253. i9xx_get_initial_plane_config;
  11254. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11255. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11256. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11257. dev_priv->display.off = i9xx_crtc_off;
  11258. dev_priv->display.update_primary_plane =
  11259. i9xx_update_primary_plane;
  11260. } else {
  11261. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11262. dev_priv->display.get_initial_plane_config =
  11263. i9xx_get_initial_plane_config;
  11264. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11265. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11266. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11267. dev_priv->display.off = i9xx_crtc_off;
  11268. dev_priv->display.update_primary_plane =
  11269. i9xx_update_primary_plane;
  11270. }
  11271. /* Returns the core display clock speed */
  11272. if (IS_VALLEYVIEW(dev))
  11273. dev_priv->display.get_display_clock_speed =
  11274. valleyview_get_display_clock_speed;
  11275. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  11276. dev_priv->display.get_display_clock_speed =
  11277. i945_get_display_clock_speed;
  11278. else if (IS_I915G(dev))
  11279. dev_priv->display.get_display_clock_speed =
  11280. i915_get_display_clock_speed;
  11281. else if (IS_I945GM(dev) || IS_845G(dev))
  11282. dev_priv->display.get_display_clock_speed =
  11283. i9xx_misc_get_display_clock_speed;
  11284. else if (IS_PINEVIEW(dev))
  11285. dev_priv->display.get_display_clock_speed =
  11286. pnv_get_display_clock_speed;
  11287. else if (IS_I915GM(dev))
  11288. dev_priv->display.get_display_clock_speed =
  11289. i915gm_get_display_clock_speed;
  11290. else if (IS_I865G(dev))
  11291. dev_priv->display.get_display_clock_speed =
  11292. i865_get_display_clock_speed;
  11293. else if (IS_I85X(dev))
  11294. dev_priv->display.get_display_clock_speed =
  11295. i855_get_display_clock_speed;
  11296. else /* 852, 830 */
  11297. dev_priv->display.get_display_clock_speed =
  11298. i830_get_display_clock_speed;
  11299. if (IS_GEN5(dev)) {
  11300. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11301. } else if (IS_GEN6(dev)) {
  11302. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11303. } else if (IS_IVYBRIDGE(dev)) {
  11304. /* FIXME: detect B0+ stepping and use auto training */
  11305. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11306. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  11307. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11308. } else if (IS_VALLEYVIEW(dev)) {
  11309. dev_priv->display.modeset_global_resources =
  11310. valleyview_modeset_global_resources;
  11311. }
  11312. switch (INTEL_INFO(dev)->gen) {
  11313. case 2:
  11314. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  11315. break;
  11316. case 3:
  11317. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  11318. break;
  11319. case 4:
  11320. case 5:
  11321. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  11322. break;
  11323. case 6:
  11324. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  11325. break;
  11326. case 7:
  11327. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  11328. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  11329. break;
  11330. case 9:
  11331. /* Drop through - unsupported since execlist only. */
  11332. default:
  11333. /* Default just returns -ENODEV to indicate unsupported */
  11334. dev_priv->display.queue_flip = intel_default_queue_flip;
  11335. }
  11336. intel_panel_init_backlight_funcs(dev);
  11337. mutex_init(&dev_priv->pps_mutex);
  11338. }
  11339. /*
  11340. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  11341. * resume, or other times. This quirk makes sure that's the case for
  11342. * affected systems.
  11343. */
  11344. static void quirk_pipea_force(struct drm_device *dev)
  11345. {
  11346. struct drm_i915_private *dev_priv = dev->dev_private;
  11347. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  11348. DRM_INFO("applying pipe a force quirk\n");
  11349. }
  11350. static void quirk_pipeb_force(struct drm_device *dev)
  11351. {
  11352. struct drm_i915_private *dev_priv = dev->dev_private;
  11353. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  11354. DRM_INFO("applying pipe b force quirk\n");
  11355. }
  11356. /*
  11357. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11358. */
  11359. static void quirk_ssc_force_disable(struct drm_device *dev)
  11360. {
  11361. struct drm_i915_private *dev_priv = dev->dev_private;
  11362. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11363. DRM_INFO("applying lvds SSC disable quirk\n");
  11364. }
  11365. /*
  11366. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11367. * brightness value
  11368. */
  11369. static void quirk_invert_brightness(struct drm_device *dev)
  11370. {
  11371. struct drm_i915_private *dev_priv = dev->dev_private;
  11372. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11373. DRM_INFO("applying inverted panel brightness quirk\n");
  11374. }
  11375. /* Some VBT's incorrectly indicate no backlight is present */
  11376. static void quirk_backlight_present(struct drm_device *dev)
  11377. {
  11378. struct drm_i915_private *dev_priv = dev->dev_private;
  11379. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11380. DRM_INFO("applying backlight present quirk\n");
  11381. }
  11382. struct intel_quirk {
  11383. int device;
  11384. int subsystem_vendor;
  11385. int subsystem_device;
  11386. void (*hook)(struct drm_device *dev);
  11387. };
  11388. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11389. struct intel_dmi_quirk {
  11390. void (*hook)(struct drm_device *dev);
  11391. const struct dmi_system_id (*dmi_id_list)[];
  11392. };
  11393. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11394. {
  11395. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11396. return 1;
  11397. }
  11398. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11399. {
  11400. .dmi_id_list = &(const struct dmi_system_id[]) {
  11401. {
  11402. .callback = intel_dmi_reverse_brightness,
  11403. .ident = "NCR Corporation",
  11404. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11405. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11406. },
  11407. },
  11408. { } /* terminating entry */
  11409. },
  11410. .hook = quirk_invert_brightness,
  11411. },
  11412. };
  11413. static struct intel_quirk intel_quirks[] = {
  11414. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  11415. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  11416. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  11417. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  11418. /* 830 needs to leave pipe A & dpll A up */
  11419. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  11420. /* 830 needs to leave pipe B & dpll B up */
  11421. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  11422. /* Lenovo U160 cannot use SSC on LVDS */
  11423. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11424. /* Sony Vaio Y cannot use SSC on LVDS */
  11425. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11426. /* Acer Aspire 5734Z must invert backlight brightness */
  11427. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11428. /* Acer/eMachines G725 */
  11429. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11430. /* Acer/eMachines e725 */
  11431. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11432. /* Acer/Packard Bell NCL20 */
  11433. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11434. /* Acer Aspire 4736Z */
  11435. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11436. /* Acer Aspire 5336 */
  11437. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11438. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11439. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11440. /* Acer C720 Chromebook (Core i3 4005U) */
  11441. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11442. /* Apple Macbook 2,1 (Core 2 T7400) */
  11443. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11444. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11445. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11446. /* HP Chromebook 14 (Celeron 2955U) */
  11447. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11448. /* Dell Chromebook 11 */
  11449. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11450. };
  11451. static void intel_init_quirks(struct drm_device *dev)
  11452. {
  11453. struct pci_dev *d = dev->pdev;
  11454. int i;
  11455. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11456. struct intel_quirk *q = &intel_quirks[i];
  11457. if (d->device == q->device &&
  11458. (d->subsystem_vendor == q->subsystem_vendor ||
  11459. q->subsystem_vendor == PCI_ANY_ID) &&
  11460. (d->subsystem_device == q->subsystem_device ||
  11461. q->subsystem_device == PCI_ANY_ID))
  11462. q->hook(dev);
  11463. }
  11464. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11465. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  11466. intel_dmi_quirks[i].hook(dev);
  11467. }
  11468. }
  11469. /* Disable the VGA plane that we never use */
  11470. static void i915_disable_vga(struct drm_device *dev)
  11471. {
  11472. struct drm_i915_private *dev_priv = dev->dev_private;
  11473. u8 sr1;
  11474. u32 vga_reg = i915_vgacntrl_reg(dev);
  11475. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  11476. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  11477. outb(SR01, VGA_SR_INDEX);
  11478. sr1 = inb(VGA_SR_DATA);
  11479. outb(sr1 | 1<<5, VGA_SR_DATA);
  11480. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  11481. udelay(300);
  11482. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  11483. POSTING_READ(vga_reg);
  11484. }
  11485. void intel_modeset_init_hw(struct drm_device *dev)
  11486. {
  11487. intel_prepare_ddi(dev);
  11488. if (IS_VALLEYVIEW(dev))
  11489. vlv_update_cdclk(dev);
  11490. intel_init_clock_gating(dev);
  11491. intel_enable_gt_powersave(dev);
  11492. }
  11493. void intel_modeset_init(struct drm_device *dev)
  11494. {
  11495. struct drm_i915_private *dev_priv = dev->dev_private;
  11496. int sprite, ret;
  11497. enum pipe pipe;
  11498. struct intel_crtc *crtc;
  11499. drm_mode_config_init(dev);
  11500. dev->mode_config.min_width = 0;
  11501. dev->mode_config.min_height = 0;
  11502. dev->mode_config.preferred_depth = 24;
  11503. dev->mode_config.prefer_shadow = 1;
  11504. dev->mode_config.allow_fb_modifiers = true;
  11505. dev->mode_config.funcs = &intel_mode_funcs;
  11506. intel_init_quirks(dev);
  11507. intel_init_pm(dev);
  11508. if (INTEL_INFO(dev)->num_pipes == 0)
  11509. return;
  11510. intel_init_display(dev);
  11511. intel_init_audio(dev);
  11512. if (IS_GEN2(dev)) {
  11513. dev->mode_config.max_width = 2048;
  11514. dev->mode_config.max_height = 2048;
  11515. } else if (IS_GEN3(dev)) {
  11516. dev->mode_config.max_width = 4096;
  11517. dev->mode_config.max_height = 4096;
  11518. } else {
  11519. dev->mode_config.max_width = 8192;
  11520. dev->mode_config.max_height = 8192;
  11521. }
  11522. if (IS_845G(dev) || IS_I865G(dev)) {
  11523. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  11524. dev->mode_config.cursor_height = 1023;
  11525. } else if (IS_GEN2(dev)) {
  11526. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  11527. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  11528. } else {
  11529. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  11530. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  11531. }
  11532. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  11533. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  11534. INTEL_INFO(dev)->num_pipes,
  11535. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  11536. for_each_pipe(dev_priv, pipe) {
  11537. intel_crtc_init(dev, pipe);
  11538. for_each_sprite(dev_priv, pipe, sprite) {
  11539. ret = intel_plane_init(dev, pipe, sprite);
  11540. if (ret)
  11541. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  11542. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  11543. }
  11544. }
  11545. intel_init_dpio(dev);
  11546. intel_shared_dpll_init(dev);
  11547. /* Just disable it once at startup */
  11548. i915_disable_vga(dev);
  11549. intel_setup_outputs(dev);
  11550. /* Just in case the BIOS is doing something questionable. */
  11551. intel_fbc_disable(dev);
  11552. drm_modeset_lock_all(dev);
  11553. intel_modeset_setup_hw_state(dev, false);
  11554. drm_modeset_unlock_all(dev);
  11555. for_each_intel_crtc(dev, crtc) {
  11556. if (!crtc->active)
  11557. continue;
  11558. /*
  11559. * Note that reserving the BIOS fb up front prevents us
  11560. * from stuffing other stolen allocations like the ring
  11561. * on top. This prevents some ugliness at boot time, and
  11562. * can even allow for smooth boot transitions if the BIOS
  11563. * fb is large enough for the active pipe configuration.
  11564. */
  11565. if (dev_priv->display.get_initial_plane_config) {
  11566. dev_priv->display.get_initial_plane_config(crtc,
  11567. &crtc->plane_config);
  11568. /*
  11569. * If the fb is shared between multiple heads, we'll
  11570. * just get the first one.
  11571. */
  11572. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  11573. }
  11574. }
  11575. }
  11576. static void intel_enable_pipe_a(struct drm_device *dev)
  11577. {
  11578. struct intel_connector *connector;
  11579. struct drm_connector *crt = NULL;
  11580. struct intel_load_detect_pipe load_detect_temp;
  11581. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  11582. /* We can't just switch on the pipe A, we need to set things up with a
  11583. * proper mode and output configuration. As a gross hack, enable pipe A
  11584. * by enabling the load detect pipe once. */
  11585. for_each_intel_connector(dev, connector) {
  11586. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  11587. crt = &connector->base;
  11588. break;
  11589. }
  11590. }
  11591. if (!crt)
  11592. return;
  11593. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  11594. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  11595. }
  11596. static bool
  11597. intel_check_plane_mapping(struct intel_crtc *crtc)
  11598. {
  11599. struct drm_device *dev = crtc->base.dev;
  11600. struct drm_i915_private *dev_priv = dev->dev_private;
  11601. u32 reg, val;
  11602. if (INTEL_INFO(dev)->num_pipes == 1)
  11603. return true;
  11604. reg = DSPCNTR(!crtc->plane);
  11605. val = I915_READ(reg);
  11606. if ((val & DISPLAY_PLANE_ENABLE) &&
  11607. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  11608. return false;
  11609. return true;
  11610. }
  11611. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  11612. {
  11613. struct drm_device *dev = crtc->base.dev;
  11614. struct drm_i915_private *dev_priv = dev->dev_private;
  11615. u32 reg;
  11616. /* Clear any frame start delays used for debugging left by the BIOS */
  11617. reg = PIPECONF(crtc->config->cpu_transcoder);
  11618. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  11619. /* restore vblank interrupts to correct state */
  11620. drm_crtc_vblank_reset(&crtc->base);
  11621. if (crtc->active) {
  11622. update_scanline_offset(crtc);
  11623. drm_crtc_vblank_on(&crtc->base);
  11624. }
  11625. /* We need to sanitize the plane -> pipe mapping first because this will
  11626. * disable the crtc (and hence change the state) if it is wrong. Note
  11627. * that gen4+ has a fixed plane -> pipe mapping. */
  11628. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  11629. struct intel_connector *connector;
  11630. bool plane;
  11631. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11632. crtc->base.base.id);
  11633. /* Pipe has the wrong plane attached and the plane is active.
  11634. * Temporarily change the plane mapping and disable everything
  11635. * ... */
  11636. plane = crtc->plane;
  11637. crtc->plane = !plane;
  11638. crtc->primary_enabled = true;
  11639. dev_priv->display.crtc_disable(&crtc->base);
  11640. crtc->plane = plane;
  11641. /* ... and break all links. */
  11642. for_each_intel_connector(dev, connector) {
  11643. if (connector->encoder->base.crtc != &crtc->base)
  11644. continue;
  11645. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11646. connector->base.encoder = NULL;
  11647. }
  11648. /* multiple connectors may have the same encoder:
  11649. * handle them and break crtc link separately */
  11650. for_each_intel_connector(dev, connector)
  11651. if (connector->encoder->base.crtc == &crtc->base) {
  11652. connector->encoder->base.crtc = NULL;
  11653. connector->encoder->connectors_active = false;
  11654. }
  11655. WARN_ON(crtc->active);
  11656. crtc->base.state->enable = false;
  11657. crtc->base.enabled = false;
  11658. }
  11659. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11660. crtc->pipe == PIPE_A && !crtc->active) {
  11661. /* BIOS forgot to enable pipe A, this mostly happens after
  11662. * resume. Force-enable the pipe to fix this, the update_dpms
  11663. * call below we restore the pipe to the right state, but leave
  11664. * the required bits on. */
  11665. intel_enable_pipe_a(dev);
  11666. }
  11667. /* Adjust the state of the output pipe according to whether we
  11668. * have active connectors/encoders. */
  11669. intel_crtc_update_dpms(&crtc->base);
  11670. if (crtc->active != crtc->base.state->enable) {
  11671. struct intel_encoder *encoder;
  11672. /* This can happen either due to bugs in the get_hw_state
  11673. * functions or because the pipe is force-enabled due to the
  11674. * pipe A quirk. */
  11675. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11676. crtc->base.base.id,
  11677. crtc->base.state->enable ? "enabled" : "disabled",
  11678. crtc->active ? "enabled" : "disabled");
  11679. crtc->base.state->enable = crtc->active;
  11680. crtc->base.enabled = crtc->active;
  11681. /* Because we only establish the connector -> encoder ->
  11682. * crtc links if something is active, this means the
  11683. * crtc is now deactivated. Break the links. connector
  11684. * -> encoder links are only establish when things are
  11685. * actually up, hence no need to break them. */
  11686. WARN_ON(crtc->active);
  11687. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11688. WARN_ON(encoder->connectors_active);
  11689. encoder->base.crtc = NULL;
  11690. }
  11691. }
  11692. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11693. /*
  11694. * We start out with underrun reporting disabled to avoid races.
  11695. * For correct bookkeeping mark this on active crtcs.
  11696. *
  11697. * Also on gmch platforms we dont have any hardware bits to
  11698. * disable the underrun reporting. Which means we need to start
  11699. * out with underrun reporting disabled also on inactive pipes,
  11700. * since otherwise we'll complain about the garbage we read when
  11701. * e.g. coming up after runtime pm.
  11702. *
  11703. * No protection against concurrent access is required - at
  11704. * worst a fifo underrun happens which also sets this to false.
  11705. */
  11706. crtc->cpu_fifo_underrun_disabled = true;
  11707. crtc->pch_fifo_underrun_disabled = true;
  11708. }
  11709. }
  11710. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11711. {
  11712. struct intel_connector *connector;
  11713. struct drm_device *dev = encoder->base.dev;
  11714. /* We need to check both for a crtc link (meaning that the
  11715. * encoder is active and trying to read from a pipe) and the
  11716. * pipe itself being active. */
  11717. bool has_active_crtc = encoder->base.crtc &&
  11718. to_intel_crtc(encoder->base.crtc)->active;
  11719. if (encoder->connectors_active && !has_active_crtc) {
  11720. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11721. encoder->base.base.id,
  11722. encoder->base.name);
  11723. /* Connector is active, but has no active pipe. This is
  11724. * fallout from our resume register restoring. Disable
  11725. * the encoder manually again. */
  11726. if (encoder->base.crtc) {
  11727. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11728. encoder->base.base.id,
  11729. encoder->base.name);
  11730. encoder->disable(encoder);
  11731. if (encoder->post_disable)
  11732. encoder->post_disable(encoder);
  11733. }
  11734. encoder->base.crtc = NULL;
  11735. encoder->connectors_active = false;
  11736. /* Inconsistent output/port/pipe state happens presumably due to
  11737. * a bug in one of the get_hw_state functions. Or someplace else
  11738. * in our code, like the register restore mess on resume. Clamp
  11739. * things to off as a safer default. */
  11740. for_each_intel_connector(dev, connector) {
  11741. if (connector->encoder != encoder)
  11742. continue;
  11743. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11744. connector->base.encoder = NULL;
  11745. }
  11746. }
  11747. /* Enabled encoders without active connectors will be fixed in
  11748. * the crtc fixup. */
  11749. }
  11750. void i915_redisable_vga_power_on(struct drm_device *dev)
  11751. {
  11752. struct drm_i915_private *dev_priv = dev->dev_private;
  11753. u32 vga_reg = i915_vgacntrl_reg(dev);
  11754. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11755. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11756. i915_disable_vga(dev);
  11757. }
  11758. }
  11759. void i915_redisable_vga(struct drm_device *dev)
  11760. {
  11761. struct drm_i915_private *dev_priv = dev->dev_private;
  11762. /* This function can be called both from intel_modeset_setup_hw_state or
  11763. * at a very early point in our resume sequence, where the power well
  11764. * structures are not yet restored. Since this function is at a very
  11765. * paranoid "someone might have enabled VGA while we were not looking"
  11766. * level, just check if the power well is enabled instead of trying to
  11767. * follow the "don't touch the power well if we don't need it" policy
  11768. * the rest of the driver uses. */
  11769. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11770. return;
  11771. i915_redisable_vga_power_on(dev);
  11772. }
  11773. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11774. {
  11775. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11776. if (!crtc->active)
  11777. return false;
  11778. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11779. }
  11780. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11781. {
  11782. struct drm_i915_private *dev_priv = dev->dev_private;
  11783. enum pipe pipe;
  11784. struct intel_crtc *crtc;
  11785. struct intel_encoder *encoder;
  11786. struct intel_connector *connector;
  11787. int i;
  11788. for_each_intel_crtc(dev, crtc) {
  11789. memset(crtc->config, 0, sizeof(*crtc->config));
  11790. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11791. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11792. crtc->config);
  11793. crtc->base.state->enable = crtc->active;
  11794. crtc->base.enabled = crtc->active;
  11795. crtc->primary_enabled = primary_get_hw_state(crtc);
  11796. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11797. crtc->base.base.id,
  11798. crtc->active ? "enabled" : "disabled");
  11799. }
  11800. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11801. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11802. pll->on = pll->get_hw_state(dev_priv, pll,
  11803. &pll->config.hw_state);
  11804. pll->active = 0;
  11805. pll->config.crtc_mask = 0;
  11806. for_each_intel_crtc(dev, crtc) {
  11807. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11808. pll->active++;
  11809. pll->config.crtc_mask |= 1 << crtc->pipe;
  11810. }
  11811. }
  11812. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11813. pll->name, pll->config.crtc_mask, pll->on);
  11814. if (pll->config.crtc_mask)
  11815. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11816. }
  11817. for_each_intel_encoder(dev, encoder) {
  11818. pipe = 0;
  11819. if (encoder->get_hw_state(encoder, &pipe)) {
  11820. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11821. encoder->base.crtc = &crtc->base;
  11822. encoder->get_config(encoder, crtc->config);
  11823. } else {
  11824. encoder->base.crtc = NULL;
  11825. }
  11826. encoder->connectors_active = false;
  11827. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11828. encoder->base.base.id,
  11829. encoder->base.name,
  11830. encoder->base.crtc ? "enabled" : "disabled",
  11831. pipe_name(pipe));
  11832. }
  11833. for_each_intel_connector(dev, connector) {
  11834. if (connector->get_hw_state(connector)) {
  11835. connector->base.dpms = DRM_MODE_DPMS_ON;
  11836. connector->encoder->connectors_active = true;
  11837. connector->base.encoder = &connector->encoder->base;
  11838. } else {
  11839. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11840. connector->base.encoder = NULL;
  11841. }
  11842. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11843. connector->base.base.id,
  11844. connector->base.name,
  11845. connector->base.encoder ? "enabled" : "disabled");
  11846. }
  11847. }
  11848. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11849. * and i915 state tracking structures. */
  11850. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11851. bool force_restore)
  11852. {
  11853. struct drm_i915_private *dev_priv = dev->dev_private;
  11854. enum pipe pipe;
  11855. struct intel_crtc *crtc;
  11856. struct intel_encoder *encoder;
  11857. int i;
  11858. intel_modeset_readout_hw_state(dev);
  11859. /*
  11860. * Now that we have the config, copy it to each CRTC struct
  11861. * Note that this could go away if we move to using crtc_config
  11862. * checking everywhere.
  11863. */
  11864. for_each_intel_crtc(dev, crtc) {
  11865. if (crtc->active && i915.fastboot) {
  11866. intel_mode_from_pipe_config(&crtc->base.mode,
  11867. crtc->config);
  11868. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11869. crtc->base.base.id);
  11870. drm_mode_debug_printmodeline(&crtc->base.mode);
  11871. }
  11872. }
  11873. /* HW state is read out, now we need to sanitize this mess. */
  11874. for_each_intel_encoder(dev, encoder) {
  11875. intel_sanitize_encoder(encoder);
  11876. }
  11877. for_each_pipe(dev_priv, pipe) {
  11878. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11879. intel_sanitize_crtc(crtc);
  11880. intel_dump_pipe_config(crtc, crtc->config,
  11881. "[setup_hw_state]");
  11882. }
  11883. intel_modeset_update_connector_atomic_state(dev);
  11884. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11885. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11886. if (!pll->on || pll->active)
  11887. continue;
  11888. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11889. pll->disable(dev_priv, pll);
  11890. pll->on = false;
  11891. }
  11892. if (IS_GEN9(dev))
  11893. skl_wm_get_hw_state(dev);
  11894. else if (HAS_PCH_SPLIT(dev))
  11895. ilk_wm_get_hw_state(dev);
  11896. if (force_restore) {
  11897. i915_redisable_vga(dev);
  11898. /*
  11899. * We need to use raw interfaces for restoring state to avoid
  11900. * checking (bogus) intermediate states.
  11901. */
  11902. for_each_pipe(dev_priv, pipe) {
  11903. struct drm_crtc *crtc =
  11904. dev_priv->pipe_to_crtc_mapping[pipe];
  11905. intel_crtc_restore_mode(crtc);
  11906. }
  11907. } else {
  11908. intel_modeset_update_staged_output_state(dev);
  11909. }
  11910. intel_modeset_check_state(dev);
  11911. }
  11912. void intel_modeset_gem_init(struct drm_device *dev)
  11913. {
  11914. struct drm_i915_private *dev_priv = dev->dev_private;
  11915. struct drm_crtc *c;
  11916. struct drm_i915_gem_object *obj;
  11917. int ret;
  11918. mutex_lock(&dev->struct_mutex);
  11919. intel_init_gt_powersave(dev);
  11920. mutex_unlock(&dev->struct_mutex);
  11921. /*
  11922. * There may be no VBT; and if the BIOS enabled SSC we can
  11923. * just keep using it to avoid unnecessary flicker. Whereas if the
  11924. * BIOS isn't using it, don't assume it will work even if the VBT
  11925. * indicates as much.
  11926. */
  11927. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11928. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11929. DREF_SSC1_ENABLE);
  11930. intel_modeset_init_hw(dev);
  11931. intel_setup_overlay(dev);
  11932. /*
  11933. * Make sure any fbs we allocated at startup are properly
  11934. * pinned & fenced. When we do the allocation it's too early
  11935. * for this.
  11936. */
  11937. for_each_crtc(dev, c) {
  11938. obj = intel_fb_obj(c->primary->fb);
  11939. if (obj == NULL)
  11940. continue;
  11941. mutex_lock(&dev->struct_mutex);
  11942. ret = intel_pin_and_fence_fb_obj(c->primary,
  11943. c->primary->fb,
  11944. c->primary->state,
  11945. NULL);
  11946. mutex_unlock(&dev->struct_mutex);
  11947. if (ret) {
  11948. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11949. to_intel_crtc(c)->pipe);
  11950. drm_framebuffer_unreference(c->primary->fb);
  11951. c->primary->fb = NULL;
  11952. update_state_fb(c->primary);
  11953. }
  11954. }
  11955. intel_backlight_register(dev);
  11956. }
  11957. void intel_connector_unregister(struct intel_connector *intel_connector)
  11958. {
  11959. struct drm_connector *connector = &intel_connector->base;
  11960. intel_panel_destroy_backlight(connector);
  11961. drm_connector_unregister(connector);
  11962. }
  11963. void intel_modeset_cleanup(struct drm_device *dev)
  11964. {
  11965. struct drm_i915_private *dev_priv = dev->dev_private;
  11966. struct drm_connector *connector;
  11967. intel_disable_gt_powersave(dev);
  11968. intel_backlight_unregister(dev);
  11969. /*
  11970. * Interrupts and polling as the first thing to avoid creating havoc.
  11971. * Too much stuff here (turning of connectors, ...) would
  11972. * experience fancy races otherwise.
  11973. */
  11974. intel_irq_uninstall(dev_priv);
  11975. /*
  11976. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11977. * poll handlers. Hence disable polling after hpd handling is shut down.
  11978. */
  11979. drm_kms_helper_poll_fini(dev);
  11980. mutex_lock(&dev->struct_mutex);
  11981. intel_unregister_dsm_handler();
  11982. intel_fbc_disable(dev);
  11983. mutex_unlock(&dev->struct_mutex);
  11984. /* flush any delayed tasks or pending work */
  11985. flush_scheduled_work();
  11986. /* destroy the backlight and sysfs files before encoders/connectors */
  11987. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11988. struct intel_connector *intel_connector;
  11989. intel_connector = to_intel_connector(connector);
  11990. intel_connector->unregister(intel_connector);
  11991. }
  11992. drm_mode_config_cleanup(dev);
  11993. intel_cleanup_overlay(dev);
  11994. mutex_lock(&dev->struct_mutex);
  11995. intel_cleanup_gt_powersave(dev);
  11996. mutex_unlock(&dev->struct_mutex);
  11997. }
  11998. /*
  11999. * Return which encoder is currently attached for connector.
  12000. */
  12001. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12002. {
  12003. return &intel_attached_encoder(connector)->base;
  12004. }
  12005. void intel_connector_attach_encoder(struct intel_connector *connector,
  12006. struct intel_encoder *encoder)
  12007. {
  12008. connector->encoder = encoder;
  12009. drm_mode_connector_attach_encoder(&connector->base,
  12010. &encoder->base);
  12011. }
  12012. /*
  12013. * set vga decode state - true == enable VGA decode
  12014. */
  12015. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12016. {
  12017. struct drm_i915_private *dev_priv = dev->dev_private;
  12018. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12019. u16 gmch_ctrl;
  12020. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12021. DRM_ERROR("failed to read control word\n");
  12022. return -EIO;
  12023. }
  12024. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12025. return 0;
  12026. if (state)
  12027. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12028. else
  12029. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12030. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12031. DRM_ERROR("failed to write control word\n");
  12032. return -EIO;
  12033. }
  12034. return 0;
  12035. }
  12036. struct intel_display_error_state {
  12037. u32 power_well_driver;
  12038. int num_transcoders;
  12039. struct intel_cursor_error_state {
  12040. u32 control;
  12041. u32 position;
  12042. u32 base;
  12043. u32 size;
  12044. } cursor[I915_MAX_PIPES];
  12045. struct intel_pipe_error_state {
  12046. bool power_domain_on;
  12047. u32 source;
  12048. u32 stat;
  12049. } pipe[I915_MAX_PIPES];
  12050. struct intel_plane_error_state {
  12051. u32 control;
  12052. u32 stride;
  12053. u32 size;
  12054. u32 pos;
  12055. u32 addr;
  12056. u32 surface;
  12057. u32 tile_offset;
  12058. } plane[I915_MAX_PIPES];
  12059. struct intel_transcoder_error_state {
  12060. bool power_domain_on;
  12061. enum transcoder cpu_transcoder;
  12062. u32 conf;
  12063. u32 htotal;
  12064. u32 hblank;
  12065. u32 hsync;
  12066. u32 vtotal;
  12067. u32 vblank;
  12068. u32 vsync;
  12069. } transcoder[4];
  12070. };
  12071. struct intel_display_error_state *
  12072. intel_display_capture_error_state(struct drm_device *dev)
  12073. {
  12074. struct drm_i915_private *dev_priv = dev->dev_private;
  12075. struct intel_display_error_state *error;
  12076. int transcoders[] = {
  12077. TRANSCODER_A,
  12078. TRANSCODER_B,
  12079. TRANSCODER_C,
  12080. TRANSCODER_EDP,
  12081. };
  12082. int i;
  12083. if (INTEL_INFO(dev)->num_pipes == 0)
  12084. return NULL;
  12085. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12086. if (error == NULL)
  12087. return NULL;
  12088. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12089. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  12090. for_each_pipe(dev_priv, i) {
  12091. error->pipe[i].power_domain_on =
  12092. __intel_display_power_is_enabled(dev_priv,
  12093. POWER_DOMAIN_PIPE(i));
  12094. if (!error->pipe[i].power_domain_on)
  12095. continue;
  12096. error->cursor[i].control = I915_READ(CURCNTR(i));
  12097. error->cursor[i].position = I915_READ(CURPOS(i));
  12098. error->cursor[i].base = I915_READ(CURBASE(i));
  12099. error->plane[i].control = I915_READ(DSPCNTR(i));
  12100. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12101. if (INTEL_INFO(dev)->gen <= 3) {
  12102. error->plane[i].size = I915_READ(DSPSIZE(i));
  12103. error->plane[i].pos = I915_READ(DSPPOS(i));
  12104. }
  12105. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12106. error->plane[i].addr = I915_READ(DSPADDR(i));
  12107. if (INTEL_INFO(dev)->gen >= 4) {
  12108. error->plane[i].surface = I915_READ(DSPSURF(i));
  12109. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12110. }
  12111. error->pipe[i].source = I915_READ(PIPESRC(i));
  12112. if (HAS_GMCH_DISPLAY(dev))
  12113. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12114. }
  12115. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  12116. if (HAS_DDI(dev_priv->dev))
  12117. error->num_transcoders++; /* Account for eDP. */
  12118. for (i = 0; i < error->num_transcoders; i++) {
  12119. enum transcoder cpu_transcoder = transcoders[i];
  12120. error->transcoder[i].power_domain_on =
  12121. __intel_display_power_is_enabled(dev_priv,
  12122. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12123. if (!error->transcoder[i].power_domain_on)
  12124. continue;
  12125. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12126. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12127. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12128. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12129. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12130. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12131. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12132. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12133. }
  12134. return error;
  12135. }
  12136. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12137. void
  12138. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12139. struct drm_device *dev,
  12140. struct intel_display_error_state *error)
  12141. {
  12142. struct drm_i915_private *dev_priv = dev->dev_private;
  12143. int i;
  12144. if (!error)
  12145. return;
  12146. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  12147. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12148. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12149. error->power_well_driver);
  12150. for_each_pipe(dev_priv, i) {
  12151. err_printf(m, "Pipe [%d]:\n", i);
  12152. err_printf(m, " Power: %s\n",
  12153. error->pipe[i].power_domain_on ? "on" : "off");
  12154. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12155. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12156. err_printf(m, "Plane [%d]:\n", i);
  12157. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12158. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12159. if (INTEL_INFO(dev)->gen <= 3) {
  12160. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12161. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12162. }
  12163. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12164. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12165. if (INTEL_INFO(dev)->gen >= 4) {
  12166. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12167. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12168. }
  12169. err_printf(m, "Cursor [%d]:\n", i);
  12170. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12171. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12172. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12173. }
  12174. for (i = 0; i < error->num_transcoders; i++) {
  12175. err_printf(m, "CPU transcoder: %c\n",
  12176. transcoder_name(error->transcoder[i].cpu_transcoder));
  12177. err_printf(m, " Power: %s\n",
  12178. error->transcoder[i].power_domain_on ? "on" : "off");
  12179. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12180. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12181. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12182. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12183. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12184. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12185. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12186. }
  12187. }
  12188. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  12189. {
  12190. struct intel_crtc *crtc;
  12191. for_each_intel_crtc(dev, crtc) {
  12192. struct intel_unpin_work *work;
  12193. spin_lock_irq(&dev->event_lock);
  12194. work = crtc->unpin_work;
  12195. if (work && work->event &&
  12196. work->event->base.file_priv == file) {
  12197. kfree(work->event);
  12198. work->event = NULL;
  12199. }
  12200. spin_unlock_irq(&dev->event_lock);
  12201. }
  12202. }