intel_display.c 383 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845
  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_dp_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <drm/drm_plane_helper.h>
  42. #include <drm/drm_rect.h>
  43. #include <linux/dma_remapping.h>
  44. /* Primary plane formats supported by all gen */
  45. #define COMMON_PRIMARY_FORMATS \
  46. DRM_FORMAT_C8, \
  47. DRM_FORMAT_RGB565, \
  48. DRM_FORMAT_XRGB8888, \
  49. DRM_FORMAT_ARGB8888
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t intel_primary_formats_gen2[] = {
  52. COMMON_PRIMARY_FORMATS,
  53. DRM_FORMAT_XRGB1555,
  54. DRM_FORMAT_ARGB1555,
  55. };
  56. /* Primary plane formats for gen >= 4 */
  57. static const uint32_t intel_primary_formats_gen4[] = {
  58. COMMON_PRIMARY_FORMATS, \
  59. DRM_FORMAT_XBGR8888,
  60. DRM_FORMAT_ABGR8888,
  61. DRM_FORMAT_XRGB2101010,
  62. DRM_FORMAT_ARGB2101010,
  63. DRM_FORMAT_XBGR2101010,
  64. DRM_FORMAT_ABGR2101010,
  65. };
  66. /* Cursor formats */
  67. static const uint32_t intel_cursor_formats[] = {
  68. DRM_FORMAT_ARGB8888,
  69. };
  70. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  71. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  72. struct intel_crtc_state *pipe_config);
  73. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  74. struct intel_crtc_state *pipe_config);
  75. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  76. int x, int y, struct drm_framebuffer *old_fb);
  77. static int intel_framebuffer_init(struct drm_device *dev,
  78. struct intel_framebuffer *ifb,
  79. struct drm_mode_fb_cmd2 *mode_cmd,
  80. struct drm_i915_gem_object *obj);
  81. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  82. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  83. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  84. struct intel_link_m_n *m_n,
  85. struct intel_link_m_n *m2_n2);
  86. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  87. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  88. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  89. static void vlv_prepare_pll(struct intel_crtc *crtc,
  90. const struct intel_crtc_state *pipe_config);
  91. static void chv_prepare_pll(struct intel_crtc *crtc,
  92. const struct intel_crtc_state *pipe_config);
  93. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  94. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  95. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  96. {
  97. if (!connector->mst_port)
  98. return connector->encoder;
  99. else
  100. return &connector->mst_port->mst_encoders[pipe]->base;
  101. }
  102. typedef struct {
  103. int min, max;
  104. } intel_range_t;
  105. typedef struct {
  106. int dot_limit;
  107. int p2_slow, p2_fast;
  108. } intel_p2_t;
  109. typedef struct intel_limit intel_limit_t;
  110. struct intel_limit {
  111. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  112. intel_p2_t p2;
  113. };
  114. int
  115. intel_pch_rawclk(struct drm_device *dev)
  116. {
  117. struct drm_i915_private *dev_priv = dev->dev_private;
  118. WARN_ON(!HAS_PCH_SPLIT(dev));
  119. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  120. }
  121. static inline u32 /* units of 100MHz */
  122. intel_fdi_link_freq(struct drm_device *dev)
  123. {
  124. if (IS_GEN5(dev)) {
  125. struct drm_i915_private *dev_priv = dev->dev_private;
  126. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  127. } else
  128. return 27;
  129. }
  130. static const intel_limit_t intel_limits_i8xx_dac = {
  131. .dot = { .min = 25000, .max = 350000 },
  132. .vco = { .min = 908000, .max = 1512000 },
  133. .n = { .min = 2, .max = 16 },
  134. .m = { .min = 96, .max = 140 },
  135. .m1 = { .min = 18, .max = 26 },
  136. .m2 = { .min = 6, .max = 16 },
  137. .p = { .min = 4, .max = 128 },
  138. .p1 = { .min = 2, .max = 33 },
  139. .p2 = { .dot_limit = 165000,
  140. .p2_slow = 4, .p2_fast = 2 },
  141. };
  142. static const intel_limit_t intel_limits_i8xx_dvo = {
  143. .dot = { .min = 25000, .max = 350000 },
  144. .vco = { .min = 908000, .max = 1512000 },
  145. .n = { .min = 2, .max = 16 },
  146. .m = { .min = 96, .max = 140 },
  147. .m1 = { .min = 18, .max = 26 },
  148. .m2 = { .min = 6, .max = 16 },
  149. .p = { .min = 4, .max = 128 },
  150. .p1 = { .min = 2, .max = 33 },
  151. .p2 = { .dot_limit = 165000,
  152. .p2_slow = 4, .p2_fast = 4 },
  153. };
  154. static const intel_limit_t intel_limits_i8xx_lvds = {
  155. .dot = { .min = 25000, .max = 350000 },
  156. .vco = { .min = 908000, .max = 1512000 },
  157. .n = { .min = 2, .max = 16 },
  158. .m = { .min = 96, .max = 140 },
  159. .m1 = { .min = 18, .max = 26 },
  160. .m2 = { .min = 6, .max = 16 },
  161. .p = { .min = 4, .max = 128 },
  162. .p1 = { .min = 1, .max = 6 },
  163. .p2 = { .dot_limit = 165000,
  164. .p2_slow = 14, .p2_fast = 7 },
  165. };
  166. static const intel_limit_t intel_limits_i9xx_sdvo = {
  167. .dot = { .min = 20000, .max = 400000 },
  168. .vco = { .min = 1400000, .max = 2800000 },
  169. .n = { .min = 1, .max = 6 },
  170. .m = { .min = 70, .max = 120 },
  171. .m1 = { .min = 8, .max = 18 },
  172. .m2 = { .min = 3, .max = 7 },
  173. .p = { .min = 5, .max = 80 },
  174. .p1 = { .min = 1, .max = 8 },
  175. .p2 = { .dot_limit = 200000,
  176. .p2_slow = 10, .p2_fast = 5 },
  177. };
  178. static const intel_limit_t intel_limits_i9xx_lvds = {
  179. .dot = { .min = 20000, .max = 400000 },
  180. .vco = { .min = 1400000, .max = 2800000 },
  181. .n = { .min = 1, .max = 6 },
  182. .m = { .min = 70, .max = 120 },
  183. .m1 = { .min = 8, .max = 18 },
  184. .m2 = { .min = 3, .max = 7 },
  185. .p = { .min = 7, .max = 98 },
  186. .p1 = { .min = 1, .max = 8 },
  187. .p2 = { .dot_limit = 112000,
  188. .p2_slow = 14, .p2_fast = 7 },
  189. };
  190. static const intel_limit_t intel_limits_g4x_sdvo = {
  191. .dot = { .min = 25000, .max = 270000 },
  192. .vco = { .min = 1750000, .max = 3500000},
  193. .n = { .min = 1, .max = 4 },
  194. .m = { .min = 104, .max = 138 },
  195. .m1 = { .min = 17, .max = 23 },
  196. .m2 = { .min = 5, .max = 11 },
  197. .p = { .min = 10, .max = 30 },
  198. .p1 = { .min = 1, .max = 3},
  199. .p2 = { .dot_limit = 270000,
  200. .p2_slow = 10,
  201. .p2_fast = 10
  202. },
  203. };
  204. static const intel_limit_t intel_limits_g4x_hdmi = {
  205. .dot = { .min = 22000, .max = 400000 },
  206. .vco = { .min = 1750000, .max = 3500000},
  207. .n = { .min = 1, .max = 4 },
  208. .m = { .min = 104, .max = 138 },
  209. .m1 = { .min = 16, .max = 23 },
  210. .m2 = { .min = 5, .max = 11 },
  211. .p = { .min = 5, .max = 80 },
  212. .p1 = { .min = 1, .max = 8},
  213. .p2 = { .dot_limit = 165000,
  214. .p2_slow = 10, .p2_fast = 5 },
  215. };
  216. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  217. .dot = { .min = 20000, .max = 115000 },
  218. .vco = { .min = 1750000, .max = 3500000 },
  219. .n = { .min = 1, .max = 3 },
  220. .m = { .min = 104, .max = 138 },
  221. .m1 = { .min = 17, .max = 23 },
  222. .m2 = { .min = 5, .max = 11 },
  223. .p = { .min = 28, .max = 112 },
  224. .p1 = { .min = 2, .max = 8 },
  225. .p2 = { .dot_limit = 0,
  226. .p2_slow = 14, .p2_fast = 14
  227. },
  228. };
  229. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  230. .dot = { .min = 80000, .max = 224000 },
  231. .vco = { .min = 1750000, .max = 3500000 },
  232. .n = { .min = 1, .max = 3 },
  233. .m = { .min = 104, .max = 138 },
  234. .m1 = { .min = 17, .max = 23 },
  235. .m2 = { .min = 5, .max = 11 },
  236. .p = { .min = 14, .max = 42 },
  237. .p1 = { .min = 2, .max = 6 },
  238. .p2 = { .dot_limit = 0,
  239. .p2_slow = 7, .p2_fast = 7
  240. },
  241. };
  242. static const intel_limit_t intel_limits_pineview_sdvo = {
  243. .dot = { .min = 20000, .max = 400000},
  244. .vco = { .min = 1700000, .max = 3500000 },
  245. /* Pineview's Ncounter is a ring counter */
  246. .n = { .min = 3, .max = 6 },
  247. .m = { .min = 2, .max = 256 },
  248. /* Pineview only has one combined m divider, which we treat as m2. */
  249. .m1 = { .min = 0, .max = 0 },
  250. .m2 = { .min = 0, .max = 254 },
  251. .p = { .min = 5, .max = 80 },
  252. .p1 = { .min = 1, .max = 8 },
  253. .p2 = { .dot_limit = 200000,
  254. .p2_slow = 10, .p2_fast = 5 },
  255. };
  256. static const intel_limit_t intel_limits_pineview_lvds = {
  257. .dot = { .min = 20000, .max = 400000 },
  258. .vco = { .min = 1700000, .max = 3500000 },
  259. .n = { .min = 3, .max = 6 },
  260. .m = { .min = 2, .max = 256 },
  261. .m1 = { .min = 0, .max = 0 },
  262. .m2 = { .min = 0, .max = 254 },
  263. .p = { .min = 7, .max = 112 },
  264. .p1 = { .min = 1, .max = 8 },
  265. .p2 = { .dot_limit = 112000,
  266. .p2_slow = 14, .p2_fast = 14 },
  267. };
  268. /* Ironlake / Sandybridge
  269. *
  270. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  271. * the range value for them is (actual_value - 2).
  272. */
  273. static const intel_limit_t intel_limits_ironlake_dac = {
  274. .dot = { .min = 25000, .max = 350000 },
  275. .vco = { .min = 1760000, .max = 3510000 },
  276. .n = { .min = 1, .max = 5 },
  277. .m = { .min = 79, .max = 127 },
  278. .m1 = { .min = 12, .max = 22 },
  279. .m2 = { .min = 5, .max = 9 },
  280. .p = { .min = 5, .max = 80 },
  281. .p1 = { .min = 1, .max = 8 },
  282. .p2 = { .dot_limit = 225000,
  283. .p2_slow = 10, .p2_fast = 5 },
  284. };
  285. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  286. .dot = { .min = 25000, .max = 350000 },
  287. .vco = { .min = 1760000, .max = 3510000 },
  288. .n = { .min = 1, .max = 3 },
  289. .m = { .min = 79, .max = 118 },
  290. .m1 = { .min = 12, .max = 22 },
  291. .m2 = { .min = 5, .max = 9 },
  292. .p = { .min = 28, .max = 112 },
  293. .p1 = { .min = 2, .max = 8 },
  294. .p2 = { .dot_limit = 225000,
  295. .p2_slow = 14, .p2_fast = 14 },
  296. };
  297. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  298. .dot = { .min = 25000, .max = 350000 },
  299. .vco = { .min = 1760000, .max = 3510000 },
  300. .n = { .min = 1, .max = 3 },
  301. .m = { .min = 79, .max = 127 },
  302. .m1 = { .min = 12, .max = 22 },
  303. .m2 = { .min = 5, .max = 9 },
  304. .p = { .min = 14, .max = 56 },
  305. .p1 = { .min = 2, .max = 8 },
  306. .p2 = { .dot_limit = 225000,
  307. .p2_slow = 7, .p2_fast = 7 },
  308. };
  309. /* LVDS 100mhz refclk limits. */
  310. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  311. .dot = { .min = 25000, .max = 350000 },
  312. .vco = { .min = 1760000, .max = 3510000 },
  313. .n = { .min = 1, .max = 2 },
  314. .m = { .min = 79, .max = 126 },
  315. .m1 = { .min = 12, .max = 22 },
  316. .m2 = { .min = 5, .max = 9 },
  317. .p = { .min = 28, .max = 112 },
  318. .p1 = { .min = 2, .max = 8 },
  319. .p2 = { .dot_limit = 225000,
  320. .p2_slow = 14, .p2_fast = 14 },
  321. };
  322. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  323. .dot = { .min = 25000, .max = 350000 },
  324. .vco = { .min = 1760000, .max = 3510000 },
  325. .n = { .min = 1, .max = 3 },
  326. .m = { .min = 79, .max = 126 },
  327. .m1 = { .min = 12, .max = 22 },
  328. .m2 = { .min = 5, .max = 9 },
  329. .p = { .min = 14, .max = 42 },
  330. .p1 = { .min = 2, .max = 6 },
  331. .p2 = { .dot_limit = 225000,
  332. .p2_slow = 7, .p2_fast = 7 },
  333. };
  334. static const intel_limit_t intel_limits_vlv = {
  335. /*
  336. * These are the data rate limits (measured in fast clocks)
  337. * since those are the strictest limits we have. The fast
  338. * clock and actual rate limits are more relaxed, so checking
  339. * them would make no difference.
  340. */
  341. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  342. .vco = { .min = 4000000, .max = 6000000 },
  343. .n = { .min = 1, .max = 7 },
  344. .m1 = { .min = 2, .max = 3 },
  345. .m2 = { .min = 11, .max = 156 },
  346. .p1 = { .min = 2, .max = 3 },
  347. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  348. };
  349. static const intel_limit_t intel_limits_chv = {
  350. /*
  351. * These are the data rate limits (measured in fast clocks)
  352. * since those are the strictest limits we have. The fast
  353. * clock and actual rate limits are more relaxed, so checking
  354. * them would make no difference.
  355. */
  356. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  357. .vco = { .min = 4860000, .max = 6700000 },
  358. .n = { .min = 1, .max = 1 },
  359. .m1 = { .min = 2, .max = 2 },
  360. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  361. .p1 = { .min = 2, .max = 4 },
  362. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  363. };
  364. static void vlv_clock(int refclk, intel_clock_t *clock)
  365. {
  366. clock->m = clock->m1 * clock->m2;
  367. clock->p = clock->p1 * clock->p2;
  368. if (WARN_ON(clock->n == 0 || clock->p == 0))
  369. return;
  370. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  371. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  372. }
  373. /**
  374. * Returns whether any output on the specified pipe is of the specified type
  375. */
  376. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  377. {
  378. struct drm_device *dev = crtc->base.dev;
  379. struct intel_encoder *encoder;
  380. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  381. if (encoder->type == type)
  382. return true;
  383. return false;
  384. }
  385. /**
  386. * Returns whether any output on the specified pipe will have the specified
  387. * type after a staged modeset is complete, i.e., the same as
  388. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  389. * encoder->crtc.
  390. */
  391. static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
  392. {
  393. struct drm_device *dev = crtc->base.dev;
  394. struct intel_encoder *encoder;
  395. for_each_intel_encoder(dev, encoder)
  396. if (encoder->new_crtc == crtc && encoder->type == type)
  397. return true;
  398. return false;
  399. }
  400. static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
  401. int refclk)
  402. {
  403. struct drm_device *dev = crtc->base.dev;
  404. const intel_limit_t *limit;
  405. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  406. if (intel_is_dual_link_lvds(dev)) {
  407. if (refclk == 100000)
  408. limit = &intel_limits_ironlake_dual_lvds_100m;
  409. else
  410. limit = &intel_limits_ironlake_dual_lvds;
  411. } else {
  412. if (refclk == 100000)
  413. limit = &intel_limits_ironlake_single_lvds_100m;
  414. else
  415. limit = &intel_limits_ironlake_single_lvds;
  416. }
  417. } else
  418. limit = &intel_limits_ironlake_dac;
  419. return limit;
  420. }
  421. static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
  422. {
  423. struct drm_device *dev = crtc->base.dev;
  424. const intel_limit_t *limit;
  425. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  426. if (intel_is_dual_link_lvds(dev))
  427. limit = &intel_limits_g4x_dual_channel_lvds;
  428. else
  429. limit = &intel_limits_g4x_single_channel_lvds;
  430. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
  431. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
  432. limit = &intel_limits_g4x_hdmi;
  433. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
  434. limit = &intel_limits_g4x_sdvo;
  435. } else /* The option is for other outputs */
  436. limit = &intel_limits_i9xx_sdvo;
  437. return limit;
  438. }
  439. static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
  440. {
  441. struct drm_device *dev = crtc->base.dev;
  442. const intel_limit_t *limit;
  443. if (HAS_PCH_SPLIT(dev))
  444. limit = intel_ironlake_limit(crtc, refclk);
  445. else if (IS_G4X(dev)) {
  446. limit = intel_g4x_limit(crtc);
  447. } else if (IS_PINEVIEW(dev)) {
  448. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  449. limit = &intel_limits_pineview_lvds;
  450. else
  451. limit = &intel_limits_pineview_sdvo;
  452. } else if (IS_CHERRYVIEW(dev)) {
  453. limit = &intel_limits_chv;
  454. } else if (IS_VALLEYVIEW(dev)) {
  455. limit = &intel_limits_vlv;
  456. } else if (!IS_GEN2(dev)) {
  457. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  458. limit = &intel_limits_i9xx_lvds;
  459. else
  460. limit = &intel_limits_i9xx_sdvo;
  461. } else {
  462. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  463. limit = &intel_limits_i8xx_lvds;
  464. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  465. limit = &intel_limits_i8xx_dvo;
  466. else
  467. limit = &intel_limits_i8xx_dac;
  468. }
  469. return limit;
  470. }
  471. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  472. static void pineview_clock(int refclk, intel_clock_t *clock)
  473. {
  474. clock->m = clock->m2 + 2;
  475. clock->p = clock->p1 * clock->p2;
  476. if (WARN_ON(clock->n == 0 || clock->p == 0))
  477. return;
  478. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  479. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  480. }
  481. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  482. {
  483. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  484. }
  485. static void i9xx_clock(int refclk, intel_clock_t *clock)
  486. {
  487. clock->m = i9xx_dpll_compute_m(clock);
  488. clock->p = clock->p1 * clock->p2;
  489. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  490. return;
  491. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  492. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  493. }
  494. static void chv_clock(int refclk, intel_clock_t *clock)
  495. {
  496. clock->m = clock->m1 * clock->m2;
  497. clock->p = clock->p1 * clock->p2;
  498. if (WARN_ON(clock->n == 0 || clock->p == 0))
  499. return;
  500. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  501. clock->n << 22);
  502. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  503. }
  504. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  505. /**
  506. * Returns whether the given set of divisors are valid for a given refclk with
  507. * the given connectors.
  508. */
  509. static bool intel_PLL_is_valid(struct drm_device *dev,
  510. const intel_limit_t *limit,
  511. const intel_clock_t *clock)
  512. {
  513. if (clock->n < limit->n.min || limit->n.max < clock->n)
  514. INTELPllInvalid("n out of range\n");
  515. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  516. INTELPllInvalid("p1 out of range\n");
  517. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  518. INTELPllInvalid("m2 out of range\n");
  519. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  520. INTELPllInvalid("m1 out of range\n");
  521. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  522. if (clock->m1 <= clock->m2)
  523. INTELPllInvalid("m1 <= m2\n");
  524. if (!IS_VALLEYVIEW(dev)) {
  525. if (clock->p < limit->p.min || limit->p.max < clock->p)
  526. INTELPllInvalid("p out of range\n");
  527. if (clock->m < limit->m.min || limit->m.max < clock->m)
  528. INTELPllInvalid("m out of range\n");
  529. }
  530. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  531. INTELPllInvalid("vco out of range\n");
  532. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  533. * connector, etc., rather than just a single range.
  534. */
  535. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  536. INTELPllInvalid("dot out of range\n");
  537. return true;
  538. }
  539. static bool
  540. i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  541. int target, int refclk, intel_clock_t *match_clock,
  542. intel_clock_t *best_clock)
  543. {
  544. struct drm_device *dev = crtc->base.dev;
  545. intel_clock_t clock;
  546. int err = target;
  547. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  548. /*
  549. * For LVDS just rely on its current settings for dual-channel.
  550. * We haven't figured out how to reliably set up different
  551. * single/dual channel state, if we even can.
  552. */
  553. if (intel_is_dual_link_lvds(dev))
  554. clock.p2 = limit->p2.p2_fast;
  555. else
  556. clock.p2 = limit->p2.p2_slow;
  557. } else {
  558. if (target < limit->p2.dot_limit)
  559. clock.p2 = limit->p2.p2_slow;
  560. else
  561. clock.p2 = limit->p2.p2_fast;
  562. }
  563. memset(best_clock, 0, sizeof(*best_clock));
  564. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  565. clock.m1++) {
  566. for (clock.m2 = limit->m2.min;
  567. clock.m2 <= limit->m2.max; clock.m2++) {
  568. if (clock.m2 >= clock.m1)
  569. break;
  570. for (clock.n = limit->n.min;
  571. clock.n <= limit->n.max; clock.n++) {
  572. for (clock.p1 = limit->p1.min;
  573. clock.p1 <= limit->p1.max; clock.p1++) {
  574. int this_err;
  575. i9xx_clock(refclk, &clock);
  576. if (!intel_PLL_is_valid(dev, limit,
  577. &clock))
  578. continue;
  579. if (match_clock &&
  580. clock.p != match_clock->p)
  581. continue;
  582. this_err = abs(clock.dot - target);
  583. if (this_err < err) {
  584. *best_clock = clock;
  585. err = this_err;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. return (err != target);
  592. }
  593. static bool
  594. pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  595. int target, int refclk, intel_clock_t *match_clock,
  596. intel_clock_t *best_clock)
  597. {
  598. struct drm_device *dev = crtc->base.dev;
  599. intel_clock_t clock;
  600. int err = target;
  601. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  602. /*
  603. * For LVDS just rely on its current settings for dual-channel.
  604. * We haven't figured out how to reliably set up different
  605. * single/dual channel state, if we even can.
  606. */
  607. if (intel_is_dual_link_lvds(dev))
  608. clock.p2 = limit->p2.p2_fast;
  609. else
  610. clock.p2 = limit->p2.p2_slow;
  611. } else {
  612. if (target < limit->p2.dot_limit)
  613. clock.p2 = limit->p2.p2_slow;
  614. else
  615. clock.p2 = limit->p2.p2_fast;
  616. }
  617. memset(best_clock, 0, sizeof(*best_clock));
  618. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  619. clock.m1++) {
  620. for (clock.m2 = limit->m2.min;
  621. clock.m2 <= limit->m2.max; clock.m2++) {
  622. for (clock.n = limit->n.min;
  623. clock.n <= limit->n.max; clock.n++) {
  624. for (clock.p1 = limit->p1.min;
  625. clock.p1 <= limit->p1.max; clock.p1++) {
  626. int this_err;
  627. pineview_clock(refclk, &clock);
  628. if (!intel_PLL_is_valid(dev, limit,
  629. &clock))
  630. continue;
  631. if (match_clock &&
  632. clock.p != match_clock->p)
  633. continue;
  634. this_err = abs(clock.dot - target);
  635. if (this_err < err) {
  636. *best_clock = clock;
  637. err = this_err;
  638. }
  639. }
  640. }
  641. }
  642. }
  643. return (err != target);
  644. }
  645. static bool
  646. g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  647. int target, int refclk, intel_clock_t *match_clock,
  648. intel_clock_t *best_clock)
  649. {
  650. struct drm_device *dev = crtc->base.dev;
  651. intel_clock_t clock;
  652. int max_n;
  653. bool found;
  654. /* approximately equals target * 0.00585 */
  655. int err_most = (target >> 8) + (target >> 9);
  656. found = false;
  657. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  658. if (intel_is_dual_link_lvds(dev))
  659. clock.p2 = limit->p2.p2_fast;
  660. else
  661. clock.p2 = limit->p2.p2_slow;
  662. } else {
  663. if (target < limit->p2.dot_limit)
  664. clock.p2 = limit->p2.p2_slow;
  665. else
  666. clock.p2 = limit->p2.p2_fast;
  667. }
  668. memset(best_clock, 0, sizeof(*best_clock));
  669. max_n = limit->n.max;
  670. /* based on hardware requirement, prefer smaller n to precision */
  671. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  672. /* based on hardware requirement, prefere larger m1,m2 */
  673. for (clock.m1 = limit->m1.max;
  674. clock.m1 >= limit->m1.min; clock.m1--) {
  675. for (clock.m2 = limit->m2.max;
  676. clock.m2 >= limit->m2.min; clock.m2--) {
  677. for (clock.p1 = limit->p1.max;
  678. clock.p1 >= limit->p1.min; clock.p1--) {
  679. int this_err;
  680. i9xx_clock(refclk, &clock);
  681. if (!intel_PLL_is_valid(dev, limit,
  682. &clock))
  683. continue;
  684. this_err = abs(clock.dot - target);
  685. if (this_err < err_most) {
  686. *best_clock = clock;
  687. err_most = this_err;
  688. max_n = clock.n;
  689. found = true;
  690. }
  691. }
  692. }
  693. }
  694. }
  695. return found;
  696. }
  697. static bool
  698. vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  699. int target, int refclk, intel_clock_t *match_clock,
  700. intel_clock_t *best_clock)
  701. {
  702. struct drm_device *dev = crtc->base.dev;
  703. intel_clock_t clock;
  704. unsigned int bestppm = 1000000;
  705. /* min update 19.2 MHz */
  706. int max_n = min(limit->n.max, refclk / 19200);
  707. bool found = false;
  708. target *= 5; /* fast clock */
  709. memset(best_clock, 0, sizeof(*best_clock));
  710. /* based on hardware requirement, prefer smaller n to precision */
  711. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  712. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  713. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  714. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  715. clock.p = clock.p1 * clock.p2;
  716. /* based on hardware requirement, prefer bigger m1,m2 values */
  717. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  718. unsigned int ppm, diff;
  719. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  720. refclk * clock.m1);
  721. vlv_clock(refclk, &clock);
  722. if (!intel_PLL_is_valid(dev, limit,
  723. &clock))
  724. continue;
  725. diff = abs(clock.dot - target);
  726. ppm = div_u64(1000000ULL * diff, target);
  727. if (ppm < 100 && clock.p > best_clock->p) {
  728. bestppm = 0;
  729. *best_clock = clock;
  730. found = true;
  731. }
  732. if (bestppm >= 10 && ppm < bestppm - 10) {
  733. bestppm = ppm;
  734. *best_clock = clock;
  735. found = true;
  736. }
  737. }
  738. }
  739. }
  740. }
  741. return found;
  742. }
  743. static bool
  744. chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  745. int target, int refclk, intel_clock_t *match_clock,
  746. intel_clock_t *best_clock)
  747. {
  748. struct drm_device *dev = crtc->base.dev;
  749. intel_clock_t clock;
  750. uint64_t m2;
  751. int found = false;
  752. memset(best_clock, 0, sizeof(*best_clock));
  753. /*
  754. * Based on hardware doc, the n always set to 1, and m1 always
  755. * set to 2. If requires to support 200Mhz refclk, we need to
  756. * revisit this because n may not 1 anymore.
  757. */
  758. clock.n = 1, clock.m1 = 2;
  759. target *= 5; /* fast clock */
  760. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  761. for (clock.p2 = limit->p2.p2_fast;
  762. clock.p2 >= limit->p2.p2_slow;
  763. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  764. clock.p = clock.p1 * clock.p2;
  765. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  766. clock.n) << 22, refclk * clock.m1);
  767. if (m2 > INT_MAX/clock.m1)
  768. continue;
  769. clock.m2 = m2;
  770. chv_clock(refclk, &clock);
  771. if (!intel_PLL_is_valid(dev, limit, &clock))
  772. continue;
  773. /* based on hardware requirement, prefer bigger p
  774. */
  775. if (clock.p > best_clock->p) {
  776. *best_clock = clock;
  777. found = true;
  778. }
  779. }
  780. }
  781. return found;
  782. }
  783. bool intel_crtc_active(struct drm_crtc *crtc)
  784. {
  785. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  786. /* Be paranoid as we can arrive here with only partial
  787. * state retrieved from the hardware during setup.
  788. *
  789. * We can ditch the adjusted_mode.crtc_clock check as soon
  790. * as Haswell has gained clock readout/fastboot support.
  791. *
  792. * We can ditch the crtc->primary->fb check as soon as we can
  793. * properly reconstruct framebuffers.
  794. */
  795. return intel_crtc->active && crtc->primary->fb &&
  796. intel_crtc->config.base.adjusted_mode.crtc_clock;
  797. }
  798. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  799. enum pipe pipe)
  800. {
  801. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  802. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  803. return intel_crtc->config.cpu_transcoder;
  804. }
  805. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  806. {
  807. struct drm_i915_private *dev_priv = dev->dev_private;
  808. u32 reg = PIPEDSL(pipe);
  809. u32 line1, line2;
  810. u32 line_mask;
  811. if (IS_GEN2(dev))
  812. line_mask = DSL_LINEMASK_GEN2;
  813. else
  814. line_mask = DSL_LINEMASK_GEN3;
  815. line1 = I915_READ(reg) & line_mask;
  816. mdelay(5);
  817. line2 = I915_READ(reg) & line_mask;
  818. return line1 == line2;
  819. }
  820. /*
  821. * intel_wait_for_pipe_off - wait for pipe to turn off
  822. * @crtc: crtc whose pipe to wait for
  823. *
  824. * After disabling a pipe, we can't wait for vblank in the usual way,
  825. * spinning on the vblank interrupt status bit, since we won't actually
  826. * see an interrupt when the pipe is disabled.
  827. *
  828. * On Gen4 and above:
  829. * wait for the pipe register state bit to turn off
  830. *
  831. * Otherwise:
  832. * wait for the display line value to settle (it usually
  833. * ends up stopping at the start of the next frame).
  834. *
  835. */
  836. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  837. {
  838. struct drm_device *dev = crtc->base.dev;
  839. struct drm_i915_private *dev_priv = dev->dev_private;
  840. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  841. enum pipe pipe = crtc->pipe;
  842. if (INTEL_INFO(dev)->gen >= 4) {
  843. int reg = PIPECONF(cpu_transcoder);
  844. /* Wait for the Pipe State to go off */
  845. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  846. 100))
  847. WARN(1, "pipe_off wait timed out\n");
  848. } else {
  849. /* Wait for the display line to settle */
  850. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  851. WARN(1, "pipe_off wait timed out\n");
  852. }
  853. }
  854. /*
  855. * ibx_digital_port_connected - is the specified port connected?
  856. * @dev_priv: i915 private structure
  857. * @port: the port to test
  858. *
  859. * Returns true if @port is connected, false otherwise.
  860. */
  861. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  862. struct intel_digital_port *port)
  863. {
  864. u32 bit;
  865. if (HAS_PCH_IBX(dev_priv->dev)) {
  866. switch (port->port) {
  867. case PORT_B:
  868. bit = SDE_PORTB_HOTPLUG;
  869. break;
  870. case PORT_C:
  871. bit = SDE_PORTC_HOTPLUG;
  872. break;
  873. case PORT_D:
  874. bit = SDE_PORTD_HOTPLUG;
  875. break;
  876. default:
  877. return true;
  878. }
  879. } else {
  880. switch (port->port) {
  881. case PORT_B:
  882. bit = SDE_PORTB_HOTPLUG_CPT;
  883. break;
  884. case PORT_C:
  885. bit = SDE_PORTC_HOTPLUG_CPT;
  886. break;
  887. case PORT_D:
  888. bit = SDE_PORTD_HOTPLUG_CPT;
  889. break;
  890. default:
  891. return true;
  892. }
  893. }
  894. return I915_READ(SDEISR) & bit;
  895. }
  896. static const char *state_string(bool enabled)
  897. {
  898. return enabled ? "on" : "off";
  899. }
  900. /* Only for pre-ILK configs */
  901. void assert_pll(struct drm_i915_private *dev_priv,
  902. enum pipe pipe, bool state)
  903. {
  904. int reg;
  905. u32 val;
  906. bool cur_state;
  907. reg = DPLL(pipe);
  908. val = I915_READ(reg);
  909. cur_state = !!(val & DPLL_VCO_ENABLE);
  910. I915_STATE_WARN(cur_state != state,
  911. "PLL state assertion failure (expected %s, current %s)\n",
  912. state_string(state), state_string(cur_state));
  913. }
  914. /* XXX: the dsi pll is shared between MIPI DSI ports */
  915. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  916. {
  917. u32 val;
  918. bool cur_state;
  919. mutex_lock(&dev_priv->dpio_lock);
  920. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  921. mutex_unlock(&dev_priv->dpio_lock);
  922. cur_state = val & DSI_PLL_VCO_EN;
  923. I915_STATE_WARN(cur_state != state,
  924. "DSI PLL state assertion failure (expected %s, current %s)\n",
  925. state_string(state), state_string(cur_state));
  926. }
  927. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  928. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  929. struct intel_shared_dpll *
  930. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  931. {
  932. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  933. if (crtc->config.shared_dpll < 0)
  934. return NULL;
  935. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  936. }
  937. /* For ILK+ */
  938. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  939. struct intel_shared_dpll *pll,
  940. bool state)
  941. {
  942. bool cur_state;
  943. struct intel_dpll_hw_state hw_state;
  944. if (WARN (!pll,
  945. "asserting DPLL %s with no DPLL\n", state_string(state)))
  946. return;
  947. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  948. I915_STATE_WARN(cur_state != state,
  949. "%s assertion failure (expected %s, current %s)\n",
  950. pll->name, state_string(state), state_string(cur_state));
  951. }
  952. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  953. enum pipe pipe, bool state)
  954. {
  955. int reg;
  956. u32 val;
  957. bool cur_state;
  958. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  959. pipe);
  960. if (HAS_DDI(dev_priv->dev)) {
  961. /* DDI does not have a specific FDI_TX register */
  962. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  963. val = I915_READ(reg);
  964. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  965. } else {
  966. reg = FDI_TX_CTL(pipe);
  967. val = I915_READ(reg);
  968. cur_state = !!(val & FDI_TX_ENABLE);
  969. }
  970. I915_STATE_WARN(cur_state != state,
  971. "FDI TX state assertion failure (expected %s, current %s)\n",
  972. state_string(state), state_string(cur_state));
  973. }
  974. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  975. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  976. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  977. enum pipe pipe, bool state)
  978. {
  979. int reg;
  980. u32 val;
  981. bool cur_state;
  982. reg = FDI_RX_CTL(pipe);
  983. val = I915_READ(reg);
  984. cur_state = !!(val & FDI_RX_ENABLE);
  985. I915_STATE_WARN(cur_state != state,
  986. "FDI RX state assertion failure (expected %s, current %s)\n",
  987. state_string(state), state_string(cur_state));
  988. }
  989. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  990. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  991. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  992. enum pipe pipe)
  993. {
  994. int reg;
  995. u32 val;
  996. /* ILK FDI PLL is always enabled */
  997. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  998. return;
  999. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1000. if (HAS_DDI(dev_priv->dev))
  1001. return;
  1002. reg = FDI_TX_CTL(pipe);
  1003. val = I915_READ(reg);
  1004. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1005. }
  1006. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1007. enum pipe pipe, bool state)
  1008. {
  1009. int reg;
  1010. u32 val;
  1011. bool cur_state;
  1012. reg = FDI_RX_CTL(pipe);
  1013. val = I915_READ(reg);
  1014. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1015. I915_STATE_WARN(cur_state != state,
  1016. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1017. state_string(state), state_string(cur_state));
  1018. }
  1019. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1020. enum pipe pipe)
  1021. {
  1022. struct drm_device *dev = dev_priv->dev;
  1023. int pp_reg;
  1024. u32 val;
  1025. enum pipe panel_pipe = PIPE_A;
  1026. bool locked = true;
  1027. if (WARN_ON(HAS_DDI(dev)))
  1028. return;
  1029. if (HAS_PCH_SPLIT(dev)) {
  1030. u32 port_sel;
  1031. pp_reg = PCH_PP_CONTROL;
  1032. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1033. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1034. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1035. panel_pipe = PIPE_B;
  1036. /* XXX: else fix for eDP */
  1037. } else if (IS_VALLEYVIEW(dev)) {
  1038. /* presumably write lock depends on pipe, not port select */
  1039. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1040. panel_pipe = pipe;
  1041. } else {
  1042. pp_reg = PP_CONTROL;
  1043. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1044. panel_pipe = PIPE_B;
  1045. }
  1046. val = I915_READ(pp_reg);
  1047. if (!(val & PANEL_POWER_ON) ||
  1048. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1049. locked = false;
  1050. I915_STATE_WARN(panel_pipe == pipe && locked,
  1051. "panel assertion failure, pipe %c regs locked\n",
  1052. pipe_name(pipe));
  1053. }
  1054. static void assert_cursor(struct drm_i915_private *dev_priv,
  1055. enum pipe pipe, bool state)
  1056. {
  1057. struct drm_device *dev = dev_priv->dev;
  1058. bool cur_state;
  1059. if (IS_845G(dev) || IS_I865G(dev))
  1060. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1061. else
  1062. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1063. I915_STATE_WARN(cur_state != state,
  1064. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1065. pipe_name(pipe), state_string(state), state_string(cur_state));
  1066. }
  1067. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1068. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1069. void assert_pipe(struct drm_i915_private *dev_priv,
  1070. enum pipe pipe, bool state)
  1071. {
  1072. int reg;
  1073. u32 val;
  1074. bool cur_state;
  1075. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1076. pipe);
  1077. /* if we need the pipe quirk it must be always on */
  1078. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1079. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1080. state = true;
  1081. if (!intel_display_power_is_enabled(dev_priv,
  1082. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1083. cur_state = false;
  1084. } else {
  1085. reg = PIPECONF(cpu_transcoder);
  1086. val = I915_READ(reg);
  1087. cur_state = !!(val & PIPECONF_ENABLE);
  1088. }
  1089. I915_STATE_WARN(cur_state != state,
  1090. "pipe %c assertion failure (expected %s, current %s)\n",
  1091. pipe_name(pipe), state_string(state), state_string(cur_state));
  1092. }
  1093. static void assert_plane(struct drm_i915_private *dev_priv,
  1094. enum plane plane, bool state)
  1095. {
  1096. int reg;
  1097. u32 val;
  1098. bool cur_state;
  1099. reg = DSPCNTR(plane);
  1100. val = I915_READ(reg);
  1101. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1102. I915_STATE_WARN(cur_state != state,
  1103. "plane %c assertion failure (expected %s, current %s)\n",
  1104. plane_name(plane), state_string(state), state_string(cur_state));
  1105. }
  1106. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1107. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1108. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1109. enum pipe pipe)
  1110. {
  1111. struct drm_device *dev = dev_priv->dev;
  1112. int reg, i;
  1113. u32 val;
  1114. int cur_pipe;
  1115. /* Primary planes are fixed to pipes on gen4+ */
  1116. if (INTEL_INFO(dev)->gen >= 4) {
  1117. reg = DSPCNTR(pipe);
  1118. val = I915_READ(reg);
  1119. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1120. "plane %c assertion failure, should be disabled but not\n",
  1121. plane_name(pipe));
  1122. return;
  1123. }
  1124. /* Need to check both planes against the pipe */
  1125. for_each_pipe(dev_priv, i) {
  1126. reg = DSPCNTR(i);
  1127. val = I915_READ(reg);
  1128. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1129. DISPPLANE_SEL_PIPE_SHIFT;
  1130. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1131. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1132. plane_name(i), pipe_name(pipe));
  1133. }
  1134. }
  1135. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1136. enum pipe pipe)
  1137. {
  1138. struct drm_device *dev = dev_priv->dev;
  1139. int reg, sprite;
  1140. u32 val;
  1141. if (INTEL_INFO(dev)->gen >= 9) {
  1142. for_each_sprite(pipe, sprite) {
  1143. val = I915_READ(PLANE_CTL(pipe, sprite));
  1144. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1145. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1146. sprite, pipe_name(pipe));
  1147. }
  1148. } else if (IS_VALLEYVIEW(dev)) {
  1149. for_each_sprite(pipe, sprite) {
  1150. reg = SPCNTR(pipe, sprite);
  1151. val = I915_READ(reg);
  1152. I915_STATE_WARN(val & SP_ENABLE,
  1153. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1154. sprite_name(pipe, sprite), pipe_name(pipe));
  1155. }
  1156. } else if (INTEL_INFO(dev)->gen >= 7) {
  1157. reg = SPRCTL(pipe);
  1158. val = I915_READ(reg);
  1159. I915_STATE_WARN(val & SPRITE_ENABLE,
  1160. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1161. plane_name(pipe), pipe_name(pipe));
  1162. } else if (INTEL_INFO(dev)->gen >= 5) {
  1163. reg = DVSCNTR(pipe);
  1164. val = I915_READ(reg);
  1165. I915_STATE_WARN(val & DVS_ENABLE,
  1166. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1167. plane_name(pipe), pipe_name(pipe));
  1168. }
  1169. }
  1170. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1171. {
  1172. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1173. drm_crtc_vblank_put(crtc);
  1174. }
  1175. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1176. {
  1177. u32 val;
  1178. bool enabled;
  1179. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1180. val = I915_READ(PCH_DREF_CONTROL);
  1181. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1182. DREF_SUPERSPREAD_SOURCE_MASK));
  1183. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1184. }
  1185. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. int reg;
  1189. u32 val;
  1190. bool enabled;
  1191. reg = PCH_TRANSCONF(pipe);
  1192. val = I915_READ(reg);
  1193. enabled = !!(val & TRANS_ENABLE);
  1194. I915_STATE_WARN(enabled,
  1195. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1196. pipe_name(pipe));
  1197. }
  1198. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe, u32 port_sel, u32 val)
  1200. {
  1201. if ((val & DP_PORT_EN) == 0)
  1202. return false;
  1203. if (HAS_PCH_CPT(dev_priv->dev)) {
  1204. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1205. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1206. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1207. return false;
  1208. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1209. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1210. return false;
  1211. } else {
  1212. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1213. return false;
  1214. }
  1215. return true;
  1216. }
  1217. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1218. enum pipe pipe, u32 val)
  1219. {
  1220. if ((val & SDVO_ENABLE) == 0)
  1221. return false;
  1222. if (HAS_PCH_CPT(dev_priv->dev)) {
  1223. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1224. return false;
  1225. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1226. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1227. return false;
  1228. } else {
  1229. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1230. return false;
  1231. }
  1232. return true;
  1233. }
  1234. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1235. enum pipe pipe, u32 val)
  1236. {
  1237. if ((val & LVDS_PORT_EN) == 0)
  1238. return false;
  1239. if (HAS_PCH_CPT(dev_priv->dev)) {
  1240. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1241. return false;
  1242. } else {
  1243. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1244. return false;
  1245. }
  1246. return true;
  1247. }
  1248. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1249. enum pipe pipe, u32 val)
  1250. {
  1251. if ((val & ADPA_DAC_ENABLE) == 0)
  1252. return false;
  1253. if (HAS_PCH_CPT(dev_priv->dev)) {
  1254. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1255. return false;
  1256. } else {
  1257. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1258. return false;
  1259. }
  1260. return true;
  1261. }
  1262. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1263. enum pipe pipe, int reg, u32 port_sel)
  1264. {
  1265. u32 val = I915_READ(reg);
  1266. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1267. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1268. reg, pipe_name(pipe));
  1269. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1270. && (val & DP_PIPEB_SELECT),
  1271. "IBX PCH dp port still using transcoder B\n");
  1272. }
  1273. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1274. enum pipe pipe, int reg)
  1275. {
  1276. u32 val = I915_READ(reg);
  1277. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1278. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1279. reg, pipe_name(pipe));
  1280. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1281. && (val & SDVO_PIPE_B_SELECT),
  1282. "IBX PCH hdmi port still using transcoder B\n");
  1283. }
  1284. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1285. enum pipe pipe)
  1286. {
  1287. int reg;
  1288. u32 val;
  1289. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1290. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1291. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1292. reg = PCH_ADPA;
  1293. val = I915_READ(reg);
  1294. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1295. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1296. pipe_name(pipe));
  1297. reg = PCH_LVDS;
  1298. val = I915_READ(reg);
  1299. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1300. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1301. pipe_name(pipe));
  1302. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1303. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1304. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1305. }
  1306. static void intel_init_dpio(struct drm_device *dev)
  1307. {
  1308. struct drm_i915_private *dev_priv = dev->dev_private;
  1309. if (!IS_VALLEYVIEW(dev))
  1310. return;
  1311. /*
  1312. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1313. * CHV x1 PHY (DP/HDMI D)
  1314. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1315. */
  1316. if (IS_CHERRYVIEW(dev)) {
  1317. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1318. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1319. } else {
  1320. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1321. }
  1322. }
  1323. static void vlv_enable_pll(struct intel_crtc *crtc,
  1324. const struct intel_crtc_state *pipe_config)
  1325. {
  1326. struct drm_device *dev = crtc->base.dev;
  1327. struct drm_i915_private *dev_priv = dev->dev_private;
  1328. int reg = DPLL(crtc->pipe);
  1329. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1330. assert_pipe_disabled(dev_priv, crtc->pipe);
  1331. /* No really, not for ILK+ */
  1332. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1333. /* PLL is protected by panel, make sure we can write it */
  1334. if (IS_MOBILE(dev_priv->dev))
  1335. assert_panel_unlocked(dev_priv, crtc->pipe);
  1336. I915_WRITE(reg, dpll);
  1337. POSTING_READ(reg);
  1338. udelay(150);
  1339. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1340. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1341. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1342. POSTING_READ(DPLL_MD(crtc->pipe));
  1343. /* We do this three times for luck */
  1344. I915_WRITE(reg, dpll);
  1345. POSTING_READ(reg);
  1346. udelay(150); /* wait for warmup */
  1347. I915_WRITE(reg, dpll);
  1348. POSTING_READ(reg);
  1349. udelay(150); /* wait for warmup */
  1350. I915_WRITE(reg, dpll);
  1351. POSTING_READ(reg);
  1352. udelay(150); /* wait for warmup */
  1353. }
  1354. static void chv_enable_pll(struct intel_crtc *crtc,
  1355. const struct intel_crtc_state *pipe_config)
  1356. {
  1357. struct drm_device *dev = crtc->base.dev;
  1358. struct drm_i915_private *dev_priv = dev->dev_private;
  1359. int pipe = crtc->pipe;
  1360. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1361. u32 tmp;
  1362. assert_pipe_disabled(dev_priv, crtc->pipe);
  1363. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1364. mutex_lock(&dev_priv->dpio_lock);
  1365. /* Enable back the 10bit clock to display controller */
  1366. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1367. tmp |= DPIO_DCLKP_EN;
  1368. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1369. /*
  1370. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1371. */
  1372. udelay(1);
  1373. /* Enable PLL */
  1374. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1375. /* Check PLL is locked */
  1376. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1377. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1378. /* not sure when this should be written */
  1379. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1380. POSTING_READ(DPLL_MD(pipe));
  1381. mutex_unlock(&dev_priv->dpio_lock);
  1382. }
  1383. static int intel_num_dvo_pipes(struct drm_device *dev)
  1384. {
  1385. struct intel_crtc *crtc;
  1386. int count = 0;
  1387. for_each_intel_crtc(dev, crtc)
  1388. count += crtc->active &&
  1389. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1390. return count;
  1391. }
  1392. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1393. {
  1394. struct drm_device *dev = crtc->base.dev;
  1395. struct drm_i915_private *dev_priv = dev->dev_private;
  1396. int reg = DPLL(crtc->pipe);
  1397. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1398. assert_pipe_disabled(dev_priv, crtc->pipe);
  1399. /* No really, not for ILK+ */
  1400. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1401. /* PLL is protected by panel, make sure we can write it */
  1402. if (IS_MOBILE(dev) && !IS_I830(dev))
  1403. assert_panel_unlocked(dev_priv, crtc->pipe);
  1404. /* Enable DVO 2x clock on both PLLs if necessary */
  1405. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1406. /*
  1407. * It appears to be important that we don't enable this
  1408. * for the current pipe before otherwise configuring the
  1409. * PLL. No idea how this should be handled if multiple
  1410. * DVO outputs are enabled simultaneosly.
  1411. */
  1412. dpll |= DPLL_DVO_2X_MODE;
  1413. I915_WRITE(DPLL(!crtc->pipe),
  1414. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1415. }
  1416. /* Wait for the clocks to stabilize. */
  1417. POSTING_READ(reg);
  1418. udelay(150);
  1419. if (INTEL_INFO(dev)->gen >= 4) {
  1420. I915_WRITE(DPLL_MD(crtc->pipe),
  1421. crtc->config.dpll_hw_state.dpll_md);
  1422. } else {
  1423. /* The pixel multiplier can only be updated once the
  1424. * DPLL is enabled and the clocks are stable.
  1425. *
  1426. * So write it again.
  1427. */
  1428. I915_WRITE(reg, dpll);
  1429. }
  1430. /* We do this three times for luck */
  1431. I915_WRITE(reg, dpll);
  1432. POSTING_READ(reg);
  1433. udelay(150); /* wait for warmup */
  1434. I915_WRITE(reg, dpll);
  1435. POSTING_READ(reg);
  1436. udelay(150); /* wait for warmup */
  1437. I915_WRITE(reg, dpll);
  1438. POSTING_READ(reg);
  1439. udelay(150); /* wait for warmup */
  1440. }
  1441. /**
  1442. * i9xx_disable_pll - disable a PLL
  1443. * @dev_priv: i915 private structure
  1444. * @pipe: pipe PLL to disable
  1445. *
  1446. * Disable the PLL for @pipe, making sure the pipe is off first.
  1447. *
  1448. * Note! This is for pre-ILK only.
  1449. */
  1450. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1451. {
  1452. struct drm_device *dev = crtc->base.dev;
  1453. struct drm_i915_private *dev_priv = dev->dev_private;
  1454. enum pipe pipe = crtc->pipe;
  1455. /* Disable DVO 2x clock on both PLLs if necessary */
  1456. if (IS_I830(dev) &&
  1457. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1458. intel_num_dvo_pipes(dev) == 1) {
  1459. I915_WRITE(DPLL(PIPE_B),
  1460. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1461. I915_WRITE(DPLL(PIPE_A),
  1462. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1463. }
  1464. /* Don't disable pipe or pipe PLLs if needed */
  1465. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1466. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1467. return;
  1468. /* Make sure the pipe isn't still relying on us */
  1469. assert_pipe_disabled(dev_priv, pipe);
  1470. I915_WRITE(DPLL(pipe), 0);
  1471. POSTING_READ(DPLL(pipe));
  1472. }
  1473. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1474. {
  1475. u32 val = 0;
  1476. /* Make sure the pipe isn't still relying on us */
  1477. assert_pipe_disabled(dev_priv, pipe);
  1478. /*
  1479. * Leave integrated clock source and reference clock enabled for pipe B.
  1480. * The latter is needed for VGA hotplug / manual detection.
  1481. */
  1482. if (pipe == PIPE_B)
  1483. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1484. I915_WRITE(DPLL(pipe), val);
  1485. POSTING_READ(DPLL(pipe));
  1486. }
  1487. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1488. {
  1489. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1490. u32 val;
  1491. /* Make sure the pipe isn't still relying on us */
  1492. assert_pipe_disabled(dev_priv, pipe);
  1493. /* Set PLL en = 0 */
  1494. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1495. if (pipe != PIPE_A)
  1496. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1497. I915_WRITE(DPLL(pipe), val);
  1498. POSTING_READ(DPLL(pipe));
  1499. mutex_lock(&dev_priv->dpio_lock);
  1500. /* Disable 10bit clock to display controller */
  1501. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1502. val &= ~DPIO_DCLKP_EN;
  1503. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1504. /* disable left/right clock distribution */
  1505. if (pipe != PIPE_B) {
  1506. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1507. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1508. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1509. } else {
  1510. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1511. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1512. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1513. }
  1514. mutex_unlock(&dev_priv->dpio_lock);
  1515. }
  1516. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1517. struct intel_digital_port *dport)
  1518. {
  1519. u32 port_mask;
  1520. int dpll_reg;
  1521. switch (dport->port) {
  1522. case PORT_B:
  1523. port_mask = DPLL_PORTB_READY_MASK;
  1524. dpll_reg = DPLL(0);
  1525. break;
  1526. case PORT_C:
  1527. port_mask = DPLL_PORTC_READY_MASK;
  1528. dpll_reg = DPLL(0);
  1529. break;
  1530. case PORT_D:
  1531. port_mask = DPLL_PORTD_READY_MASK;
  1532. dpll_reg = DPIO_PHY_STATUS;
  1533. break;
  1534. default:
  1535. BUG();
  1536. }
  1537. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1538. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1539. port_name(dport->port), I915_READ(dpll_reg));
  1540. }
  1541. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1542. {
  1543. struct drm_device *dev = crtc->base.dev;
  1544. struct drm_i915_private *dev_priv = dev->dev_private;
  1545. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1546. if (WARN_ON(pll == NULL))
  1547. return;
  1548. WARN_ON(!pll->config.crtc_mask);
  1549. if (pll->active == 0) {
  1550. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1551. WARN_ON(pll->on);
  1552. assert_shared_dpll_disabled(dev_priv, pll);
  1553. pll->mode_set(dev_priv, pll);
  1554. }
  1555. }
  1556. /**
  1557. * intel_enable_shared_dpll - enable PCH PLL
  1558. * @dev_priv: i915 private structure
  1559. * @pipe: pipe PLL to enable
  1560. *
  1561. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1562. * drives the transcoder clock.
  1563. */
  1564. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1565. {
  1566. struct drm_device *dev = crtc->base.dev;
  1567. struct drm_i915_private *dev_priv = dev->dev_private;
  1568. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1569. if (WARN_ON(pll == NULL))
  1570. return;
  1571. if (WARN_ON(pll->config.crtc_mask == 0))
  1572. return;
  1573. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1574. pll->name, pll->active, pll->on,
  1575. crtc->base.base.id);
  1576. if (pll->active++) {
  1577. WARN_ON(!pll->on);
  1578. assert_shared_dpll_enabled(dev_priv, pll);
  1579. return;
  1580. }
  1581. WARN_ON(pll->on);
  1582. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1583. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1584. pll->enable(dev_priv, pll);
  1585. pll->on = true;
  1586. }
  1587. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1588. {
  1589. struct drm_device *dev = crtc->base.dev;
  1590. struct drm_i915_private *dev_priv = dev->dev_private;
  1591. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1592. /* PCH only available on ILK+ */
  1593. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1594. if (WARN_ON(pll == NULL))
  1595. return;
  1596. if (WARN_ON(pll->config.crtc_mask == 0))
  1597. return;
  1598. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1599. pll->name, pll->active, pll->on,
  1600. crtc->base.base.id);
  1601. if (WARN_ON(pll->active == 0)) {
  1602. assert_shared_dpll_disabled(dev_priv, pll);
  1603. return;
  1604. }
  1605. assert_shared_dpll_enabled(dev_priv, pll);
  1606. WARN_ON(!pll->on);
  1607. if (--pll->active)
  1608. return;
  1609. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1610. pll->disable(dev_priv, pll);
  1611. pll->on = false;
  1612. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1613. }
  1614. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1615. enum pipe pipe)
  1616. {
  1617. struct drm_device *dev = dev_priv->dev;
  1618. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1619. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1620. uint32_t reg, val, pipeconf_val;
  1621. /* PCH only available on ILK+ */
  1622. BUG_ON(!HAS_PCH_SPLIT(dev));
  1623. /* Make sure PCH DPLL is enabled */
  1624. assert_shared_dpll_enabled(dev_priv,
  1625. intel_crtc_to_shared_dpll(intel_crtc));
  1626. /* FDI must be feeding us bits for PCH ports */
  1627. assert_fdi_tx_enabled(dev_priv, pipe);
  1628. assert_fdi_rx_enabled(dev_priv, pipe);
  1629. if (HAS_PCH_CPT(dev)) {
  1630. /* Workaround: Set the timing override bit before enabling the
  1631. * pch transcoder. */
  1632. reg = TRANS_CHICKEN2(pipe);
  1633. val = I915_READ(reg);
  1634. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1635. I915_WRITE(reg, val);
  1636. }
  1637. reg = PCH_TRANSCONF(pipe);
  1638. val = I915_READ(reg);
  1639. pipeconf_val = I915_READ(PIPECONF(pipe));
  1640. if (HAS_PCH_IBX(dev_priv->dev)) {
  1641. /*
  1642. * make the BPC in transcoder be consistent with
  1643. * that in pipeconf reg.
  1644. */
  1645. val &= ~PIPECONF_BPC_MASK;
  1646. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1647. }
  1648. val &= ~TRANS_INTERLACE_MASK;
  1649. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1650. if (HAS_PCH_IBX(dev_priv->dev) &&
  1651. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1652. val |= TRANS_LEGACY_INTERLACED_ILK;
  1653. else
  1654. val |= TRANS_INTERLACED;
  1655. else
  1656. val |= TRANS_PROGRESSIVE;
  1657. I915_WRITE(reg, val | TRANS_ENABLE);
  1658. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1659. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1660. }
  1661. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1662. enum transcoder cpu_transcoder)
  1663. {
  1664. u32 val, pipeconf_val;
  1665. /* PCH only available on ILK+ */
  1666. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1667. /* FDI must be feeding us bits for PCH ports */
  1668. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1669. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1670. /* Workaround: set timing override bit. */
  1671. val = I915_READ(_TRANSA_CHICKEN2);
  1672. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1673. I915_WRITE(_TRANSA_CHICKEN2, val);
  1674. val = TRANS_ENABLE;
  1675. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1676. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1677. PIPECONF_INTERLACED_ILK)
  1678. val |= TRANS_INTERLACED;
  1679. else
  1680. val |= TRANS_PROGRESSIVE;
  1681. I915_WRITE(LPT_TRANSCONF, val);
  1682. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1683. DRM_ERROR("Failed to enable PCH transcoder\n");
  1684. }
  1685. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1686. enum pipe pipe)
  1687. {
  1688. struct drm_device *dev = dev_priv->dev;
  1689. uint32_t reg, val;
  1690. /* FDI relies on the transcoder */
  1691. assert_fdi_tx_disabled(dev_priv, pipe);
  1692. assert_fdi_rx_disabled(dev_priv, pipe);
  1693. /* Ports must be off as well */
  1694. assert_pch_ports_disabled(dev_priv, pipe);
  1695. reg = PCH_TRANSCONF(pipe);
  1696. val = I915_READ(reg);
  1697. val &= ~TRANS_ENABLE;
  1698. I915_WRITE(reg, val);
  1699. /* wait for PCH transcoder off, transcoder state */
  1700. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1701. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1702. if (!HAS_PCH_IBX(dev)) {
  1703. /* Workaround: Clear the timing override chicken bit again. */
  1704. reg = TRANS_CHICKEN2(pipe);
  1705. val = I915_READ(reg);
  1706. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1707. I915_WRITE(reg, val);
  1708. }
  1709. }
  1710. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1711. {
  1712. u32 val;
  1713. val = I915_READ(LPT_TRANSCONF);
  1714. val &= ~TRANS_ENABLE;
  1715. I915_WRITE(LPT_TRANSCONF, val);
  1716. /* wait for PCH transcoder off, transcoder state */
  1717. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1718. DRM_ERROR("Failed to disable PCH transcoder\n");
  1719. /* Workaround: clear timing override bit. */
  1720. val = I915_READ(_TRANSA_CHICKEN2);
  1721. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1722. I915_WRITE(_TRANSA_CHICKEN2, val);
  1723. }
  1724. /**
  1725. * intel_enable_pipe - enable a pipe, asserting requirements
  1726. * @crtc: crtc responsible for the pipe
  1727. *
  1728. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1729. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1730. */
  1731. static void intel_enable_pipe(struct intel_crtc *crtc)
  1732. {
  1733. struct drm_device *dev = crtc->base.dev;
  1734. struct drm_i915_private *dev_priv = dev->dev_private;
  1735. enum pipe pipe = crtc->pipe;
  1736. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1737. pipe);
  1738. enum pipe pch_transcoder;
  1739. int reg;
  1740. u32 val;
  1741. assert_planes_disabled(dev_priv, pipe);
  1742. assert_cursor_disabled(dev_priv, pipe);
  1743. assert_sprites_disabled(dev_priv, pipe);
  1744. if (HAS_PCH_LPT(dev_priv->dev))
  1745. pch_transcoder = TRANSCODER_A;
  1746. else
  1747. pch_transcoder = pipe;
  1748. /*
  1749. * A pipe without a PLL won't actually be able to drive bits from
  1750. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1751. * need the check.
  1752. */
  1753. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1754. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1755. assert_dsi_pll_enabled(dev_priv);
  1756. else
  1757. assert_pll_enabled(dev_priv, pipe);
  1758. else {
  1759. if (crtc->config.has_pch_encoder) {
  1760. /* if driving the PCH, we need FDI enabled */
  1761. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1762. assert_fdi_tx_pll_enabled(dev_priv,
  1763. (enum pipe) cpu_transcoder);
  1764. }
  1765. /* FIXME: assert CPU port conditions for SNB+ */
  1766. }
  1767. reg = PIPECONF(cpu_transcoder);
  1768. val = I915_READ(reg);
  1769. if (val & PIPECONF_ENABLE) {
  1770. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1771. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1772. return;
  1773. }
  1774. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1775. POSTING_READ(reg);
  1776. }
  1777. /**
  1778. * intel_disable_pipe - disable a pipe, asserting requirements
  1779. * @crtc: crtc whose pipes is to be disabled
  1780. *
  1781. * Disable the pipe of @crtc, making sure that various hardware
  1782. * specific requirements are met, if applicable, e.g. plane
  1783. * disabled, panel fitter off, etc.
  1784. *
  1785. * Will wait until the pipe has shut down before returning.
  1786. */
  1787. static void intel_disable_pipe(struct intel_crtc *crtc)
  1788. {
  1789. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1790. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  1791. enum pipe pipe = crtc->pipe;
  1792. int reg;
  1793. u32 val;
  1794. /*
  1795. * Make sure planes won't keep trying to pump pixels to us,
  1796. * or we might hang the display.
  1797. */
  1798. assert_planes_disabled(dev_priv, pipe);
  1799. assert_cursor_disabled(dev_priv, pipe);
  1800. assert_sprites_disabled(dev_priv, pipe);
  1801. reg = PIPECONF(cpu_transcoder);
  1802. val = I915_READ(reg);
  1803. if ((val & PIPECONF_ENABLE) == 0)
  1804. return;
  1805. /*
  1806. * Double wide has implications for planes
  1807. * so best keep it disabled when not needed.
  1808. */
  1809. if (crtc->config.double_wide)
  1810. val &= ~PIPECONF_DOUBLE_WIDE;
  1811. /* Don't disable pipe or pipe PLLs if needed */
  1812. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1813. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1814. val &= ~PIPECONF_ENABLE;
  1815. I915_WRITE(reg, val);
  1816. if ((val & PIPECONF_ENABLE) == 0)
  1817. intel_wait_for_pipe_off(crtc);
  1818. }
  1819. /*
  1820. * Plane regs are double buffered, going from enabled->disabled needs a
  1821. * trigger in order to latch. The display address reg provides this.
  1822. */
  1823. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1824. enum plane plane)
  1825. {
  1826. struct drm_device *dev = dev_priv->dev;
  1827. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1828. I915_WRITE(reg, I915_READ(reg));
  1829. POSTING_READ(reg);
  1830. }
  1831. /**
  1832. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1833. * @plane: plane to be enabled
  1834. * @crtc: crtc for the plane
  1835. *
  1836. * Enable @plane on @crtc, making sure that the pipe is running first.
  1837. */
  1838. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1839. struct drm_crtc *crtc)
  1840. {
  1841. struct drm_device *dev = plane->dev;
  1842. struct drm_i915_private *dev_priv = dev->dev_private;
  1843. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1844. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1845. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1846. if (intel_crtc->primary_enabled)
  1847. return;
  1848. intel_crtc->primary_enabled = true;
  1849. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1850. crtc->x, crtc->y);
  1851. /*
  1852. * BDW signals flip done immediately if the plane
  1853. * is disabled, even if the plane enable is already
  1854. * armed to occur at the next vblank :(
  1855. */
  1856. if (IS_BROADWELL(dev))
  1857. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1858. }
  1859. /**
  1860. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1861. * @plane: plane to be disabled
  1862. * @crtc: crtc for the plane
  1863. *
  1864. * Disable @plane on @crtc, making sure that the pipe is running first.
  1865. */
  1866. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1867. struct drm_crtc *crtc)
  1868. {
  1869. struct drm_device *dev = plane->dev;
  1870. struct drm_i915_private *dev_priv = dev->dev_private;
  1871. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1872. if (WARN_ON(!intel_crtc->active))
  1873. return;
  1874. if (!intel_crtc->primary_enabled)
  1875. return;
  1876. intel_crtc->primary_enabled = false;
  1877. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1878. crtc->x, crtc->y);
  1879. }
  1880. static bool need_vtd_wa(struct drm_device *dev)
  1881. {
  1882. #ifdef CONFIG_INTEL_IOMMU
  1883. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1884. return true;
  1885. #endif
  1886. return false;
  1887. }
  1888. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1889. {
  1890. int tile_height;
  1891. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1892. return ALIGN(height, tile_height);
  1893. }
  1894. int
  1895. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1896. struct drm_framebuffer *fb,
  1897. struct intel_engine_cs *pipelined)
  1898. {
  1899. struct drm_device *dev = fb->dev;
  1900. struct drm_i915_private *dev_priv = dev->dev_private;
  1901. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1902. u32 alignment;
  1903. int ret;
  1904. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1905. switch (obj->tiling_mode) {
  1906. case I915_TILING_NONE:
  1907. if (INTEL_INFO(dev)->gen >= 9)
  1908. alignment = 256 * 1024;
  1909. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1910. alignment = 128 * 1024;
  1911. else if (INTEL_INFO(dev)->gen >= 4)
  1912. alignment = 4 * 1024;
  1913. else
  1914. alignment = 64 * 1024;
  1915. break;
  1916. case I915_TILING_X:
  1917. if (INTEL_INFO(dev)->gen >= 9)
  1918. alignment = 256 * 1024;
  1919. else {
  1920. /* pin() will align the object as required by fence */
  1921. alignment = 0;
  1922. }
  1923. break;
  1924. case I915_TILING_Y:
  1925. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1926. return -EINVAL;
  1927. default:
  1928. BUG();
  1929. }
  1930. /* Note that the w/a also requires 64 PTE of padding following the
  1931. * bo. We currently fill all unused PTE with the shadow page and so
  1932. * we should always have valid PTE following the scanout preventing
  1933. * the VT-d warning.
  1934. */
  1935. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1936. alignment = 256 * 1024;
  1937. /*
  1938. * Global gtt pte registers are special registers which actually forward
  1939. * writes to a chunk of system memory. Which means that there is no risk
  1940. * that the register values disappear as soon as we call
  1941. * intel_runtime_pm_put(), so it is correct to wrap only the
  1942. * pin/unpin/fence and not more.
  1943. */
  1944. intel_runtime_pm_get(dev_priv);
  1945. dev_priv->mm.interruptible = false;
  1946. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1947. if (ret)
  1948. goto err_interruptible;
  1949. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1950. * fence, whereas 965+ only requires a fence if using
  1951. * framebuffer compression. For simplicity, we always install
  1952. * a fence as the cost is not that onerous.
  1953. */
  1954. ret = i915_gem_object_get_fence(obj);
  1955. if (ret)
  1956. goto err_unpin;
  1957. i915_gem_object_pin_fence(obj);
  1958. dev_priv->mm.interruptible = true;
  1959. intel_runtime_pm_put(dev_priv);
  1960. return 0;
  1961. err_unpin:
  1962. i915_gem_object_unpin_from_display_plane(obj);
  1963. err_interruptible:
  1964. dev_priv->mm.interruptible = true;
  1965. intel_runtime_pm_put(dev_priv);
  1966. return ret;
  1967. }
  1968. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1969. {
  1970. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1971. i915_gem_object_unpin_fence(obj);
  1972. i915_gem_object_unpin_from_display_plane(obj);
  1973. }
  1974. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1975. * is assumed to be a power-of-two. */
  1976. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1977. unsigned int tiling_mode,
  1978. unsigned int cpp,
  1979. unsigned int pitch)
  1980. {
  1981. if (tiling_mode != I915_TILING_NONE) {
  1982. unsigned int tile_rows, tiles;
  1983. tile_rows = *y / 8;
  1984. *y %= 8;
  1985. tiles = *x / (512/cpp);
  1986. *x %= 512/cpp;
  1987. return tile_rows * pitch * 8 + tiles * 4096;
  1988. } else {
  1989. unsigned int offset;
  1990. offset = *y * pitch + *x * cpp;
  1991. *y = 0;
  1992. *x = (offset & 4095) / cpp;
  1993. return offset & -4096;
  1994. }
  1995. }
  1996. int intel_format_to_fourcc(int format)
  1997. {
  1998. switch (format) {
  1999. case DISPPLANE_8BPP:
  2000. return DRM_FORMAT_C8;
  2001. case DISPPLANE_BGRX555:
  2002. return DRM_FORMAT_XRGB1555;
  2003. case DISPPLANE_BGRX565:
  2004. return DRM_FORMAT_RGB565;
  2005. default:
  2006. case DISPPLANE_BGRX888:
  2007. return DRM_FORMAT_XRGB8888;
  2008. case DISPPLANE_RGBX888:
  2009. return DRM_FORMAT_XBGR8888;
  2010. case DISPPLANE_BGRX101010:
  2011. return DRM_FORMAT_XRGB2101010;
  2012. case DISPPLANE_RGBX101010:
  2013. return DRM_FORMAT_XBGR2101010;
  2014. }
  2015. }
  2016. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  2017. struct intel_plane_config *plane_config)
  2018. {
  2019. struct drm_device *dev = crtc->base.dev;
  2020. struct drm_i915_gem_object *obj = NULL;
  2021. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2022. u32 base = plane_config->base;
  2023. if (plane_config->size == 0)
  2024. return false;
  2025. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  2026. plane_config->size);
  2027. if (!obj)
  2028. return false;
  2029. if (plane_config->tiled) {
  2030. obj->tiling_mode = I915_TILING_X;
  2031. obj->stride = crtc->base.primary->fb->pitches[0];
  2032. }
  2033. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2034. mode_cmd.width = crtc->base.primary->fb->width;
  2035. mode_cmd.height = crtc->base.primary->fb->height;
  2036. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2037. mutex_lock(&dev->struct_mutex);
  2038. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2039. &mode_cmd, obj)) {
  2040. DRM_DEBUG_KMS("intel fb init failed\n");
  2041. goto out_unref_obj;
  2042. }
  2043. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2044. mutex_unlock(&dev->struct_mutex);
  2045. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2046. return true;
  2047. out_unref_obj:
  2048. drm_gem_object_unreference(&obj->base);
  2049. mutex_unlock(&dev->struct_mutex);
  2050. return false;
  2051. }
  2052. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2053. struct intel_plane_config *plane_config)
  2054. {
  2055. struct drm_device *dev = intel_crtc->base.dev;
  2056. struct drm_i915_private *dev_priv = dev->dev_private;
  2057. struct drm_crtc *c;
  2058. struct intel_crtc *i;
  2059. struct drm_i915_gem_object *obj;
  2060. if (!intel_crtc->base.primary->fb)
  2061. return;
  2062. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2063. return;
  2064. kfree(intel_crtc->base.primary->fb);
  2065. intel_crtc->base.primary->fb = NULL;
  2066. /*
  2067. * Failed to alloc the obj, check to see if we should share
  2068. * an fb with another CRTC instead
  2069. */
  2070. for_each_crtc(dev, c) {
  2071. i = to_intel_crtc(c);
  2072. if (c == &intel_crtc->base)
  2073. continue;
  2074. if (!i->active)
  2075. continue;
  2076. obj = intel_fb_obj(c->primary->fb);
  2077. if (obj == NULL)
  2078. continue;
  2079. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2080. if (obj->tiling_mode != I915_TILING_NONE)
  2081. dev_priv->preserve_bios_swizzle = true;
  2082. drm_framebuffer_reference(c->primary->fb);
  2083. intel_crtc->base.primary->fb = c->primary->fb;
  2084. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2085. break;
  2086. }
  2087. }
  2088. }
  2089. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2090. struct drm_framebuffer *fb,
  2091. int x, int y)
  2092. {
  2093. struct drm_device *dev = crtc->dev;
  2094. struct drm_i915_private *dev_priv = dev->dev_private;
  2095. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2096. struct drm_i915_gem_object *obj;
  2097. int plane = intel_crtc->plane;
  2098. unsigned long linear_offset;
  2099. u32 dspcntr;
  2100. u32 reg = DSPCNTR(plane);
  2101. int pixel_size;
  2102. if (!intel_crtc->primary_enabled) {
  2103. I915_WRITE(reg, 0);
  2104. if (INTEL_INFO(dev)->gen >= 4)
  2105. I915_WRITE(DSPSURF(plane), 0);
  2106. else
  2107. I915_WRITE(DSPADDR(plane), 0);
  2108. POSTING_READ(reg);
  2109. return;
  2110. }
  2111. obj = intel_fb_obj(fb);
  2112. if (WARN_ON(obj == NULL))
  2113. return;
  2114. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2115. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2116. dspcntr |= DISPLAY_PLANE_ENABLE;
  2117. if (INTEL_INFO(dev)->gen < 4) {
  2118. if (intel_crtc->pipe == PIPE_B)
  2119. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2120. /* pipesrc and dspsize control the size that is scaled from,
  2121. * which should always be the user's requested size.
  2122. */
  2123. I915_WRITE(DSPSIZE(plane),
  2124. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2125. (intel_crtc->config.pipe_src_w - 1));
  2126. I915_WRITE(DSPPOS(plane), 0);
  2127. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2128. I915_WRITE(PRIMSIZE(plane),
  2129. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2130. (intel_crtc->config.pipe_src_w - 1));
  2131. I915_WRITE(PRIMPOS(plane), 0);
  2132. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2133. }
  2134. switch (fb->pixel_format) {
  2135. case DRM_FORMAT_C8:
  2136. dspcntr |= DISPPLANE_8BPP;
  2137. break;
  2138. case DRM_FORMAT_XRGB1555:
  2139. case DRM_FORMAT_ARGB1555:
  2140. dspcntr |= DISPPLANE_BGRX555;
  2141. break;
  2142. case DRM_FORMAT_RGB565:
  2143. dspcntr |= DISPPLANE_BGRX565;
  2144. break;
  2145. case DRM_FORMAT_XRGB8888:
  2146. case DRM_FORMAT_ARGB8888:
  2147. dspcntr |= DISPPLANE_BGRX888;
  2148. break;
  2149. case DRM_FORMAT_XBGR8888:
  2150. case DRM_FORMAT_ABGR8888:
  2151. dspcntr |= DISPPLANE_RGBX888;
  2152. break;
  2153. case DRM_FORMAT_XRGB2101010:
  2154. case DRM_FORMAT_ARGB2101010:
  2155. dspcntr |= DISPPLANE_BGRX101010;
  2156. break;
  2157. case DRM_FORMAT_XBGR2101010:
  2158. case DRM_FORMAT_ABGR2101010:
  2159. dspcntr |= DISPPLANE_RGBX101010;
  2160. break;
  2161. default:
  2162. BUG();
  2163. }
  2164. if (INTEL_INFO(dev)->gen >= 4 &&
  2165. obj->tiling_mode != I915_TILING_NONE)
  2166. dspcntr |= DISPPLANE_TILED;
  2167. if (IS_G4X(dev))
  2168. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2169. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2170. if (INTEL_INFO(dev)->gen >= 4) {
  2171. intel_crtc->dspaddr_offset =
  2172. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2173. pixel_size,
  2174. fb->pitches[0]);
  2175. linear_offset -= intel_crtc->dspaddr_offset;
  2176. } else {
  2177. intel_crtc->dspaddr_offset = linear_offset;
  2178. }
  2179. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2180. dspcntr |= DISPPLANE_ROTATE_180;
  2181. x += (intel_crtc->config.pipe_src_w - 1);
  2182. y += (intel_crtc->config.pipe_src_h - 1);
  2183. /* Finding the last pixel of the last line of the display
  2184. data and adding to linear_offset*/
  2185. linear_offset +=
  2186. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2187. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2188. }
  2189. I915_WRITE(reg, dspcntr);
  2190. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2191. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2192. fb->pitches[0]);
  2193. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2194. if (INTEL_INFO(dev)->gen >= 4) {
  2195. I915_WRITE(DSPSURF(plane),
  2196. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2197. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2198. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2199. } else
  2200. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2201. POSTING_READ(reg);
  2202. }
  2203. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2204. struct drm_framebuffer *fb,
  2205. int x, int y)
  2206. {
  2207. struct drm_device *dev = crtc->dev;
  2208. struct drm_i915_private *dev_priv = dev->dev_private;
  2209. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2210. struct drm_i915_gem_object *obj;
  2211. int plane = intel_crtc->plane;
  2212. unsigned long linear_offset;
  2213. u32 dspcntr;
  2214. u32 reg = DSPCNTR(plane);
  2215. int pixel_size;
  2216. if (!intel_crtc->primary_enabled) {
  2217. I915_WRITE(reg, 0);
  2218. I915_WRITE(DSPSURF(plane), 0);
  2219. POSTING_READ(reg);
  2220. return;
  2221. }
  2222. obj = intel_fb_obj(fb);
  2223. if (WARN_ON(obj == NULL))
  2224. return;
  2225. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2226. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2227. dspcntr |= DISPLAY_PLANE_ENABLE;
  2228. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2229. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2230. switch (fb->pixel_format) {
  2231. case DRM_FORMAT_C8:
  2232. dspcntr |= DISPPLANE_8BPP;
  2233. break;
  2234. case DRM_FORMAT_RGB565:
  2235. dspcntr |= DISPPLANE_BGRX565;
  2236. break;
  2237. case DRM_FORMAT_XRGB8888:
  2238. case DRM_FORMAT_ARGB8888:
  2239. dspcntr |= DISPPLANE_BGRX888;
  2240. break;
  2241. case DRM_FORMAT_XBGR8888:
  2242. case DRM_FORMAT_ABGR8888:
  2243. dspcntr |= DISPPLANE_RGBX888;
  2244. break;
  2245. case DRM_FORMAT_XRGB2101010:
  2246. case DRM_FORMAT_ARGB2101010:
  2247. dspcntr |= DISPPLANE_BGRX101010;
  2248. break;
  2249. case DRM_FORMAT_XBGR2101010:
  2250. case DRM_FORMAT_ABGR2101010:
  2251. dspcntr |= DISPPLANE_RGBX101010;
  2252. break;
  2253. default:
  2254. BUG();
  2255. }
  2256. if (obj->tiling_mode != I915_TILING_NONE)
  2257. dspcntr |= DISPPLANE_TILED;
  2258. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2259. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2260. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2261. intel_crtc->dspaddr_offset =
  2262. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2263. pixel_size,
  2264. fb->pitches[0]);
  2265. linear_offset -= intel_crtc->dspaddr_offset;
  2266. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2267. dspcntr |= DISPPLANE_ROTATE_180;
  2268. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2269. x += (intel_crtc->config.pipe_src_w - 1);
  2270. y += (intel_crtc->config.pipe_src_h - 1);
  2271. /* Finding the last pixel of the last line of the display
  2272. data and adding to linear_offset*/
  2273. linear_offset +=
  2274. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2275. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2276. }
  2277. }
  2278. I915_WRITE(reg, dspcntr);
  2279. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2280. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2281. fb->pitches[0]);
  2282. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2283. I915_WRITE(DSPSURF(plane),
  2284. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2285. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2286. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2287. } else {
  2288. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2289. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2290. }
  2291. POSTING_READ(reg);
  2292. }
  2293. static void skylake_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 intel_framebuffer *intel_fb;
  2301. struct drm_i915_gem_object *obj;
  2302. int pipe = intel_crtc->pipe;
  2303. u32 plane_ctl, stride;
  2304. if (!intel_crtc->primary_enabled) {
  2305. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2306. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2307. POSTING_READ(PLANE_CTL(pipe, 0));
  2308. return;
  2309. }
  2310. plane_ctl = PLANE_CTL_ENABLE |
  2311. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2312. PLANE_CTL_PIPE_CSC_ENABLE;
  2313. switch (fb->pixel_format) {
  2314. case DRM_FORMAT_RGB565:
  2315. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2316. break;
  2317. case DRM_FORMAT_XRGB8888:
  2318. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2319. break;
  2320. case DRM_FORMAT_XBGR8888:
  2321. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2322. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2323. break;
  2324. case DRM_FORMAT_XRGB2101010:
  2325. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2326. break;
  2327. case DRM_FORMAT_XBGR2101010:
  2328. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2329. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2330. break;
  2331. default:
  2332. BUG();
  2333. }
  2334. intel_fb = to_intel_framebuffer(fb);
  2335. obj = intel_fb->obj;
  2336. /*
  2337. * The stride is either expressed as a multiple of 64 bytes chunks for
  2338. * linear buffers or in number of tiles for tiled buffers.
  2339. */
  2340. switch (obj->tiling_mode) {
  2341. case I915_TILING_NONE:
  2342. stride = fb->pitches[0] >> 6;
  2343. break;
  2344. case I915_TILING_X:
  2345. plane_ctl |= PLANE_CTL_TILED_X;
  2346. stride = fb->pitches[0] >> 9;
  2347. break;
  2348. default:
  2349. BUG();
  2350. }
  2351. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2352. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180))
  2353. plane_ctl |= PLANE_CTL_ROTATE_180;
  2354. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2355. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2356. i915_gem_obj_ggtt_offset(obj),
  2357. x, y, fb->width, fb->height,
  2358. fb->pitches[0]);
  2359. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2360. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2361. I915_WRITE(PLANE_SIZE(pipe, 0),
  2362. (intel_crtc->config.pipe_src_h - 1) << 16 |
  2363. (intel_crtc->config.pipe_src_w - 1));
  2364. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2365. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2366. POSTING_READ(PLANE_SURF(pipe, 0));
  2367. }
  2368. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2369. static int
  2370. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2371. int x, int y, enum mode_set_atomic state)
  2372. {
  2373. struct drm_device *dev = crtc->dev;
  2374. struct drm_i915_private *dev_priv = dev->dev_private;
  2375. if (dev_priv->display.disable_fbc)
  2376. dev_priv->display.disable_fbc(dev);
  2377. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2378. return 0;
  2379. }
  2380. static void intel_complete_page_flips(struct drm_device *dev)
  2381. {
  2382. struct drm_crtc *crtc;
  2383. for_each_crtc(dev, crtc) {
  2384. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2385. enum plane plane = intel_crtc->plane;
  2386. intel_prepare_page_flip(dev, plane);
  2387. intel_finish_page_flip_plane(dev, plane);
  2388. }
  2389. }
  2390. static void intel_update_primary_planes(struct drm_device *dev)
  2391. {
  2392. struct drm_i915_private *dev_priv = dev->dev_private;
  2393. struct drm_crtc *crtc;
  2394. for_each_crtc(dev, crtc) {
  2395. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2396. drm_modeset_lock(&crtc->mutex, NULL);
  2397. /*
  2398. * FIXME: Once we have proper support for primary planes (and
  2399. * disabling them without disabling the entire crtc) allow again
  2400. * a NULL crtc->primary->fb.
  2401. */
  2402. if (intel_crtc->active && crtc->primary->fb)
  2403. dev_priv->display.update_primary_plane(crtc,
  2404. crtc->primary->fb,
  2405. crtc->x,
  2406. crtc->y);
  2407. drm_modeset_unlock(&crtc->mutex);
  2408. }
  2409. }
  2410. void intel_prepare_reset(struct drm_device *dev)
  2411. {
  2412. struct drm_i915_private *dev_priv = to_i915(dev);
  2413. struct intel_crtc *crtc;
  2414. /* no reset support for gen2 */
  2415. if (IS_GEN2(dev))
  2416. return;
  2417. /* reset doesn't touch the display */
  2418. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2419. return;
  2420. drm_modeset_lock_all(dev);
  2421. /*
  2422. * Disabling the crtcs gracefully seems nicer. Also the
  2423. * g33 docs say we should at least disable all the planes.
  2424. */
  2425. for_each_intel_crtc(dev, crtc) {
  2426. if (crtc->active)
  2427. dev_priv->display.crtc_disable(&crtc->base);
  2428. }
  2429. }
  2430. void intel_finish_reset(struct drm_device *dev)
  2431. {
  2432. struct drm_i915_private *dev_priv = to_i915(dev);
  2433. /*
  2434. * Flips in the rings will be nuked by the reset,
  2435. * so complete all pending flips so that user space
  2436. * will get its events and not get stuck.
  2437. */
  2438. intel_complete_page_flips(dev);
  2439. /* no reset support for gen2 */
  2440. if (IS_GEN2(dev))
  2441. return;
  2442. /* reset doesn't touch the display */
  2443. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2444. /*
  2445. * Flips in the rings have been nuked by the reset,
  2446. * so update the base address of all primary
  2447. * planes to the the last fb to make sure we're
  2448. * showing the correct fb after a reset.
  2449. */
  2450. intel_update_primary_planes(dev);
  2451. return;
  2452. }
  2453. /*
  2454. * The display has been reset as well,
  2455. * so need a full re-initialization.
  2456. */
  2457. intel_runtime_pm_disable_interrupts(dev_priv);
  2458. intel_runtime_pm_enable_interrupts(dev_priv);
  2459. intel_modeset_init_hw(dev);
  2460. spin_lock_irq(&dev_priv->irq_lock);
  2461. if (dev_priv->display.hpd_irq_setup)
  2462. dev_priv->display.hpd_irq_setup(dev);
  2463. spin_unlock_irq(&dev_priv->irq_lock);
  2464. intel_modeset_setup_hw_state(dev, true);
  2465. intel_hpd_init(dev_priv);
  2466. drm_modeset_unlock_all(dev);
  2467. }
  2468. static int
  2469. intel_finish_fb(struct drm_framebuffer *old_fb)
  2470. {
  2471. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2472. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2473. bool was_interruptible = dev_priv->mm.interruptible;
  2474. int ret;
  2475. /* Big Hammer, we also need to ensure that any pending
  2476. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2477. * current scanout is retired before unpinning the old
  2478. * framebuffer.
  2479. *
  2480. * This should only fail upon a hung GPU, in which case we
  2481. * can safely continue.
  2482. */
  2483. dev_priv->mm.interruptible = false;
  2484. ret = i915_gem_object_finish_gpu(obj);
  2485. dev_priv->mm.interruptible = was_interruptible;
  2486. return ret;
  2487. }
  2488. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2489. {
  2490. struct drm_device *dev = crtc->dev;
  2491. struct drm_i915_private *dev_priv = dev->dev_private;
  2492. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2493. bool pending;
  2494. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2495. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2496. return false;
  2497. spin_lock_irq(&dev->event_lock);
  2498. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2499. spin_unlock_irq(&dev->event_lock);
  2500. return pending;
  2501. }
  2502. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2503. {
  2504. struct drm_device *dev = crtc->base.dev;
  2505. struct drm_i915_private *dev_priv = dev->dev_private;
  2506. const struct drm_display_mode *adjusted_mode;
  2507. if (!i915.fastboot)
  2508. return;
  2509. /*
  2510. * Update pipe size and adjust fitter if needed: the reason for this is
  2511. * that in compute_mode_changes we check the native mode (not the pfit
  2512. * mode) to see if we can flip rather than do a full mode set. In the
  2513. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2514. * pfit state, we'll end up with a big fb scanned out into the wrong
  2515. * sized surface.
  2516. *
  2517. * To fix this properly, we need to hoist the checks up into
  2518. * compute_mode_changes (or above), check the actual pfit state and
  2519. * whether the platform allows pfit disable with pipe active, and only
  2520. * then update the pipesrc and pfit state, even on the flip path.
  2521. */
  2522. adjusted_mode = &crtc->config.base.adjusted_mode;
  2523. I915_WRITE(PIPESRC(crtc->pipe),
  2524. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2525. (adjusted_mode->crtc_vdisplay - 1));
  2526. if (!crtc->config.pch_pfit.enabled &&
  2527. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2528. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2529. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2530. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2531. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2532. }
  2533. crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2534. crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2535. }
  2536. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2537. {
  2538. struct drm_device *dev = crtc->dev;
  2539. struct drm_i915_private *dev_priv = dev->dev_private;
  2540. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2541. int pipe = intel_crtc->pipe;
  2542. u32 reg, temp;
  2543. /* enable normal train */
  2544. reg = FDI_TX_CTL(pipe);
  2545. temp = I915_READ(reg);
  2546. if (IS_IVYBRIDGE(dev)) {
  2547. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2548. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2549. } else {
  2550. temp &= ~FDI_LINK_TRAIN_NONE;
  2551. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2552. }
  2553. I915_WRITE(reg, temp);
  2554. reg = FDI_RX_CTL(pipe);
  2555. temp = I915_READ(reg);
  2556. if (HAS_PCH_CPT(dev)) {
  2557. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2558. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2559. } else {
  2560. temp &= ~FDI_LINK_TRAIN_NONE;
  2561. temp |= FDI_LINK_TRAIN_NONE;
  2562. }
  2563. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2564. /* wait one idle pattern time */
  2565. POSTING_READ(reg);
  2566. udelay(1000);
  2567. /* IVB wants error correction enabled */
  2568. if (IS_IVYBRIDGE(dev))
  2569. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2570. FDI_FE_ERRC_ENABLE);
  2571. }
  2572. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2573. {
  2574. return crtc->base.enabled && crtc->active &&
  2575. crtc->config.has_pch_encoder;
  2576. }
  2577. static void ivb_modeset_global_resources(struct drm_device *dev)
  2578. {
  2579. struct drm_i915_private *dev_priv = dev->dev_private;
  2580. struct intel_crtc *pipe_B_crtc =
  2581. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2582. struct intel_crtc *pipe_C_crtc =
  2583. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2584. uint32_t temp;
  2585. /*
  2586. * When everything is off disable fdi C so that we could enable fdi B
  2587. * with all lanes. Note that we don't care about enabled pipes without
  2588. * an enabled pch encoder.
  2589. */
  2590. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2591. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2592. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2593. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2594. temp = I915_READ(SOUTH_CHICKEN1);
  2595. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2596. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2597. I915_WRITE(SOUTH_CHICKEN1, temp);
  2598. }
  2599. }
  2600. /* The FDI link training functions for ILK/Ibexpeak. */
  2601. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2602. {
  2603. struct drm_device *dev = crtc->dev;
  2604. struct drm_i915_private *dev_priv = dev->dev_private;
  2605. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2606. int pipe = intel_crtc->pipe;
  2607. u32 reg, temp, tries;
  2608. /* FDI needs bits from pipe first */
  2609. assert_pipe_enabled(dev_priv, pipe);
  2610. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2611. for train result */
  2612. reg = FDI_RX_IMR(pipe);
  2613. temp = I915_READ(reg);
  2614. temp &= ~FDI_RX_SYMBOL_LOCK;
  2615. temp &= ~FDI_RX_BIT_LOCK;
  2616. I915_WRITE(reg, temp);
  2617. I915_READ(reg);
  2618. udelay(150);
  2619. /* enable CPU FDI TX and PCH FDI RX */
  2620. reg = FDI_TX_CTL(pipe);
  2621. temp = I915_READ(reg);
  2622. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2623. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2624. temp &= ~FDI_LINK_TRAIN_NONE;
  2625. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2626. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2627. reg = FDI_RX_CTL(pipe);
  2628. temp = I915_READ(reg);
  2629. temp &= ~FDI_LINK_TRAIN_NONE;
  2630. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2631. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2632. POSTING_READ(reg);
  2633. udelay(150);
  2634. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2635. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2636. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2637. FDI_RX_PHASE_SYNC_POINTER_EN);
  2638. reg = FDI_RX_IIR(pipe);
  2639. for (tries = 0; tries < 5; tries++) {
  2640. temp = I915_READ(reg);
  2641. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2642. if ((temp & FDI_RX_BIT_LOCK)) {
  2643. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2644. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2645. break;
  2646. }
  2647. }
  2648. if (tries == 5)
  2649. DRM_ERROR("FDI train 1 fail!\n");
  2650. /* Train 2 */
  2651. reg = FDI_TX_CTL(pipe);
  2652. temp = I915_READ(reg);
  2653. temp &= ~FDI_LINK_TRAIN_NONE;
  2654. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2655. I915_WRITE(reg, temp);
  2656. reg = FDI_RX_CTL(pipe);
  2657. temp = I915_READ(reg);
  2658. temp &= ~FDI_LINK_TRAIN_NONE;
  2659. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2660. I915_WRITE(reg, temp);
  2661. POSTING_READ(reg);
  2662. udelay(150);
  2663. reg = FDI_RX_IIR(pipe);
  2664. for (tries = 0; tries < 5; tries++) {
  2665. temp = I915_READ(reg);
  2666. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2667. if (temp & FDI_RX_SYMBOL_LOCK) {
  2668. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2669. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2670. break;
  2671. }
  2672. }
  2673. if (tries == 5)
  2674. DRM_ERROR("FDI train 2 fail!\n");
  2675. DRM_DEBUG_KMS("FDI train done\n");
  2676. }
  2677. static const int snb_b_fdi_train_param[] = {
  2678. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2679. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2680. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2681. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2682. };
  2683. /* The FDI link training functions for SNB/Cougarpoint. */
  2684. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2685. {
  2686. struct drm_device *dev = crtc->dev;
  2687. struct drm_i915_private *dev_priv = dev->dev_private;
  2688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2689. int pipe = intel_crtc->pipe;
  2690. u32 reg, temp, i, retry;
  2691. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2692. for train result */
  2693. reg = FDI_RX_IMR(pipe);
  2694. temp = I915_READ(reg);
  2695. temp &= ~FDI_RX_SYMBOL_LOCK;
  2696. temp &= ~FDI_RX_BIT_LOCK;
  2697. I915_WRITE(reg, temp);
  2698. POSTING_READ(reg);
  2699. udelay(150);
  2700. /* enable CPU FDI TX and PCH FDI RX */
  2701. reg = FDI_TX_CTL(pipe);
  2702. temp = I915_READ(reg);
  2703. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2704. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2705. temp &= ~FDI_LINK_TRAIN_NONE;
  2706. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2707. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2708. /* SNB-B */
  2709. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2710. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2711. I915_WRITE(FDI_RX_MISC(pipe),
  2712. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2713. reg = FDI_RX_CTL(pipe);
  2714. temp = I915_READ(reg);
  2715. if (HAS_PCH_CPT(dev)) {
  2716. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2717. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2718. } else {
  2719. temp &= ~FDI_LINK_TRAIN_NONE;
  2720. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2721. }
  2722. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2723. POSTING_READ(reg);
  2724. udelay(150);
  2725. for (i = 0; i < 4; i++) {
  2726. reg = FDI_TX_CTL(pipe);
  2727. temp = I915_READ(reg);
  2728. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2729. temp |= snb_b_fdi_train_param[i];
  2730. I915_WRITE(reg, temp);
  2731. POSTING_READ(reg);
  2732. udelay(500);
  2733. for (retry = 0; retry < 5; retry++) {
  2734. reg = FDI_RX_IIR(pipe);
  2735. temp = I915_READ(reg);
  2736. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2737. if (temp & FDI_RX_BIT_LOCK) {
  2738. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2739. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2740. break;
  2741. }
  2742. udelay(50);
  2743. }
  2744. if (retry < 5)
  2745. break;
  2746. }
  2747. if (i == 4)
  2748. DRM_ERROR("FDI train 1 fail!\n");
  2749. /* Train 2 */
  2750. reg = FDI_TX_CTL(pipe);
  2751. temp = I915_READ(reg);
  2752. temp &= ~FDI_LINK_TRAIN_NONE;
  2753. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2754. if (IS_GEN6(dev)) {
  2755. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2756. /* SNB-B */
  2757. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2758. }
  2759. I915_WRITE(reg, temp);
  2760. reg = FDI_RX_CTL(pipe);
  2761. temp = I915_READ(reg);
  2762. if (HAS_PCH_CPT(dev)) {
  2763. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2764. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2765. } else {
  2766. temp &= ~FDI_LINK_TRAIN_NONE;
  2767. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2768. }
  2769. I915_WRITE(reg, temp);
  2770. POSTING_READ(reg);
  2771. udelay(150);
  2772. for (i = 0; i < 4; i++) {
  2773. reg = FDI_TX_CTL(pipe);
  2774. temp = I915_READ(reg);
  2775. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2776. temp |= snb_b_fdi_train_param[i];
  2777. I915_WRITE(reg, temp);
  2778. POSTING_READ(reg);
  2779. udelay(500);
  2780. for (retry = 0; retry < 5; retry++) {
  2781. reg = FDI_RX_IIR(pipe);
  2782. temp = I915_READ(reg);
  2783. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2784. if (temp & FDI_RX_SYMBOL_LOCK) {
  2785. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2786. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2787. break;
  2788. }
  2789. udelay(50);
  2790. }
  2791. if (retry < 5)
  2792. break;
  2793. }
  2794. if (i == 4)
  2795. DRM_ERROR("FDI train 2 fail!\n");
  2796. DRM_DEBUG_KMS("FDI train done.\n");
  2797. }
  2798. /* Manual link training for Ivy Bridge A0 parts */
  2799. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2800. {
  2801. struct drm_device *dev = crtc->dev;
  2802. struct drm_i915_private *dev_priv = dev->dev_private;
  2803. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2804. int pipe = intel_crtc->pipe;
  2805. u32 reg, temp, i, j;
  2806. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2807. for train result */
  2808. reg = FDI_RX_IMR(pipe);
  2809. temp = I915_READ(reg);
  2810. temp &= ~FDI_RX_SYMBOL_LOCK;
  2811. temp &= ~FDI_RX_BIT_LOCK;
  2812. I915_WRITE(reg, temp);
  2813. POSTING_READ(reg);
  2814. udelay(150);
  2815. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2816. I915_READ(FDI_RX_IIR(pipe)));
  2817. /* Try each vswing and preemphasis setting twice before moving on */
  2818. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2819. /* disable first in case we need to retry */
  2820. reg = FDI_TX_CTL(pipe);
  2821. temp = I915_READ(reg);
  2822. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2823. temp &= ~FDI_TX_ENABLE;
  2824. I915_WRITE(reg, temp);
  2825. reg = FDI_RX_CTL(pipe);
  2826. temp = I915_READ(reg);
  2827. temp &= ~FDI_LINK_TRAIN_AUTO;
  2828. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2829. temp &= ~FDI_RX_ENABLE;
  2830. I915_WRITE(reg, temp);
  2831. /* enable CPU FDI TX and PCH FDI RX */
  2832. reg = FDI_TX_CTL(pipe);
  2833. temp = I915_READ(reg);
  2834. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2835. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2836. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2837. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2838. temp |= snb_b_fdi_train_param[j/2];
  2839. temp |= FDI_COMPOSITE_SYNC;
  2840. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2841. I915_WRITE(FDI_RX_MISC(pipe),
  2842. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2843. reg = FDI_RX_CTL(pipe);
  2844. temp = I915_READ(reg);
  2845. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2846. temp |= FDI_COMPOSITE_SYNC;
  2847. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2848. POSTING_READ(reg);
  2849. udelay(1); /* should be 0.5us */
  2850. for (i = 0; i < 4; i++) {
  2851. reg = FDI_RX_IIR(pipe);
  2852. temp = I915_READ(reg);
  2853. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2854. if (temp & FDI_RX_BIT_LOCK ||
  2855. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2856. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2857. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2858. i);
  2859. break;
  2860. }
  2861. udelay(1); /* should be 0.5us */
  2862. }
  2863. if (i == 4) {
  2864. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2865. continue;
  2866. }
  2867. /* Train 2 */
  2868. reg = FDI_TX_CTL(pipe);
  2869. temp = I915_READ(reg);
  2870. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2871. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2872. I915_WRITE(reg, temp);
  2873. reg = FDI_RX_CTL(pipe);
  2874. temp = I915_READ(reg);
  2875. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2876. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2877. I915_WRITE(reg, temp);
  2878. POSTING_READ(reg);
  2879. udelay(2); /* should be 1.5us */
  2880. for (i = 0; i < 4; i++) {
  2881. reg = FDI_RX_IIR(pipe);
  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_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2886. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2887. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2888. i);
  2889. goto train_done;
  2890. }
  2891. udelay(2); /* should be 1.5us */
  2892. }
  2893. if (i == 4)
  2894. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2895. }
  2896. train_done:
  2897. DRM_DEBUG_KMS("FDI train done.\n");
  2898. }
  2899. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2900. {
  2901. struct drm_device *dev = intel_crtc->base.dev;
  2902. struct drm_i915_private *dev_priv = dev->dev_private;
  2903. int pipe = intel_crtc->pipe;
  2904. u32 reg, temp;
  2905. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2906. reg = FDI_RX_CTL(pipe);
  2907. temp = I915_READ(reg);
  2908. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2909. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2910. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2911. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2912. POSTING_READ(reg);
  2913. udelay(200);
  2914. /* Switch from Rawclk to PCDclk */
  2915. temp = I915_READ(reg);
  2916. I915_WRITE(reg, temp | FDI_PCDCLK);
  2917. POSTING_READ(reg);
  2918. udelay(200);
  2919. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2920. reg = FDI_TX_CTL(pipe);
  2921. temp = I915_READ(reg);
  2922. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2923. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2924. POSTING_READ(reg);
  2925. udelay(100);
  2926. }
  2927. }
  2928. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2929. {
  2930. struct drm_device *dev = intel_crtc->base.dev;
  2931. struct drm_i915_private *dev_priv = dev->dev_private;
  2932. int pipe = intel_crtc->pipe;
  2933. u32 reg, temp;
  2934. /* Switch from PCDclk to Rawclk */
  2935. reg = FDI_RX_CTL(pipe);
  2936. temp = I915_READ(reg);
  2937. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2938. /* Disable CPU FDI TX PLL */
  2939. reg = FDI_TX_CTL(pipe);
  2940. temp = I915_READ(reg);
  2941. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2942. POSTING_READ(reg);
  2943. udelay(100);
  2944. reg = FDI_RX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2947. /* Wait for the clocks to turn off. */
  2948. POSTING_READ(reg);
  2949. udelay(100);
  2950. }
  2951. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2952. {
  2953. struct drm_device *dev = crtc->dev;
  2954. struct drm_i915_private *dev_priv = dev->dev_private;
  2955. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2956. int pipe = intel_crtc->pipe;
  2957. u32 reg, temp;
  2958. /* disable CPU FDI tx and PCH FDI rx */
  2959. reg = FDI_TX_CTL(pipe);
  2960. temp = I915_READ(reg);
  2961. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2962. POSTING_READ(reg);
  2963. reg = FDI_RX_CTL(pipe);
  2964. temp = I915_READ(reg);
  2965. temp &= ~(0x7 << 16);
  2966. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2967. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2968. POSTING_READ(reg);
  2969. udelay(100);
  2970. /* Ironlake workaround, disable clock pointer after downing FDI */
  2971. if (HAS_PCH_IBX(dev))
  2972. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2973. /* still set train pattern 1 */
  2974. reg = FDI_TX_CTL(pipe);
  2975. temp = I915_READ(reg);
  2976. temp &= ~FDI_LINK_TRAIN_NONE;
  2977. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2978. I915_WRITE(reg, temp);
  2979. reg = FDI_RX_CTL(pipe);
  2980. temp = I915_READ(reg);
  2981. if (HAS_PCH_CPT(dev)) {
  2982. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2983. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2984. } else {
  2985. temp &= ~FDI_LINK_TRAIN_NONE;
  2986. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2987. }
  2988. /* BPC in FDI rx is consistent with that in PIPECONF */
  2989. temp &= ~(0x07 << 16);
  2990. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2991. I915_WRITE(reg, temp);
  2992. POSTING_READ(reg);
  2993. udelay(100);
  2994. }
  2995. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2996. {
  2997. struct intel_crtc *crtc;
  2998. /* Note that we don't need to be called with mode_config.lock here
  2999. * as our list of CRTC objects is static for the lifetime of the
  3000. * device and so cannot disappear as we iterate. Similarly, we can
  3001. * happily treat the predicates as racy, atomic checks as userspace
  3002. * cannot claim and pin a new fb without at least acquring the
  3003. * struct_mutex and so serialising with us.
  3004. */
  3005. for_each_intel_crtc(dev, crtc) {
  3006. if (atomic_read(&crtc->unpin_work_count) == 0)
  3007. continue;
  3008. if (crtc->unpin_work)
  3009. intel_wait_for_vblank(dev, crtc->pipe);
  3010. return true;
  3011. }
  3012. return false;
  3013. }
  3014. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3015. {
  3016. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3017. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3018. /* ensure that the unpin work is consistent wrt ->pending. */
  3019. smp_rmb();
  3020. intel_crtc->unpin_work = NULL;
  3021. if (work->event)
  3022. drm_send_vblank_event(intel_crtc->base.dev,
  3023. intel_crtc->pipe,
  3024. work->event);
  3025. drm_crtc_vblank_put(&intel_crtc->base);
  3026. wake_up_all(&dev_priv->pending_flip_queue);
  3027. queue_work(dev_priv->wq, &work->work);
  3028. trace_i915_flip_complete(intel_crtc->plane,
  3029. work->pending_flip_obj);
  3030. }
  3031. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3032. {
  3033. struct drm_device *dev = crtc->dev;
  3034. struct drm_i915_private *dev_priv = dev->dev_private;
  3035. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3036. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3037. !intel_crtc_has_pending_flip(crtc),
  3038. 60*HZ) == 0)) {
  3039. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3040. spin_lock_irq(&dev->event_lock);
  3041. if (intel_crtc->unpin_work) {
  3042. WARN_ONCE(1, "Removing stuck page flip\n");
  3043. page_flip_completed(intel_crtc);
  3044. }
  3045. spin_unlock_irq(&dev->event_lock);
  3046. }
  3047. if (crtc->primary->fb) {
  3048. mutex_lock(&dev->struct_mutex);
  3049. intel_finish_fb(crtc->primary->fb);
  3050. mutex_unlock(&dev->struct_mutex);
  3051. }
  3052. }
  3053. /* Program iCLKIP clock to the desired frequency */
  3054. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3055. {
  3056. struct drm_device *dev = crtc->dev;
  3057. struct drm_i915_private *dev_priv = dev->dev_private;
  3058. int clock = to_intel_crtc(crtc)->config.base.adjusted_mode.crtc_clock;
  3059. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3060. u32 temp;
  3061. mutex_lock(&dev_priv->dpio_lock);
  3062. /* It is necessary to ungate the pixclk gate prior to programming
  3063. * the divisors, and gate it back when it is done.
  3064. */
  3065. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3066. /* Disable SSCCTL */
  3067. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3068. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3069. SBI_SSCCTL_DISABLE,
  3070. SBI_ICLK);
  3071. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3072. if (clock == 20000) {
  3073. auxdiv = 1;
  3074. divsel = 0x41;
  3075. phaseinc = 0x20;
  3076. } else {
  3077. /* The iCLK virtual clock root frequency is in MHz,
  3078. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3079. * divisors, it is necessary to divide one by another, so we
  3080. * convert the virtual clock precision to KHz here for higher
  3081. * precision.
  3082. */
  3083. u32 iclk_virtual_root_freq = 172800 * 1000;
  3084. u32 iclk_pi_range = 64;
  3085. u32 desired_divisor, msb_divisor_value, pi_value;
  3086. desired_divisor = (iclk_virtual_root_freq / clock);
  3087. msb_divisor_value = desired_divisor / iclk_pi_range;
  3088. pi_value = desired_divisor % iclk_pi_range;
  3089. auxdiv = 0;
  3090. divsel = msb_divisor_value - 2;
  3091. phaseinc = pi_value;
  3092. }
  3093. /* This should not happen with any sane values */
  3094. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3095. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3096. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3097. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3098. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3099. clock,
  3100. auxdiv,
  3101. divsel,
  3102. phasedir,
  3103. phaseinc);
  3104. /* Program SSCDIVINTPHASE6 */
  3105. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3106. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3107. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3108. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3109. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3110. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3111. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3112. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3113. /* Program SSCAUXDIV */
  3114. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3115. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3116. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3117. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3118. /* Enable modulator and associated divider */
  3119. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3120. temp &= ~SBI_SSCCTL_DISABLE;
  3121. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3122. /* Wait for initialization time */
  3123. udelay(24);
  3124. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3125. mutex_unlock(&dev_priv->dpio_lock);
  3126. }
  3127. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3128. enum pipe pch_transcoder)
  3129. {
  3130. struct drm_device *dev = crtc->base.dev;
  3131. struct drm_i915_private *dev_priv = dev->dev_private;
  3132. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  3133. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3134. I915_READ(HTOTAL(cpu_transcoder)));
  3135. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3136. I915_READ(HBLANK(cpu_transcoder)));
  3137. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3138. I915_READ(HSYNC(cpu_transcoder)));
  3139. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3140. I915_READ(VTOTAL(cpu_transcoder)));
  3141. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3142. I915_READ(VBLANK(cpu_transcoder)));
  3143. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3144. I915_READ(VSYNC(cpu_transcoder)));
  3145. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3146. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3147. }
  3148. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3149. {
  3150. struct drm_i915_private *dev_priv = dev->dev_private;
  3151. uint32_t temp;
  3152. temp = I915_READ(SOUTH_CHICKEN1);
  3153. if (temp & FDI_BC_BIFURCATION_SELECT)
  3154. return;
  3155. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3156. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3157. temp |= FDI_BC_BIFURCATION_SELECT;
  3158. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3159. I915_WRITE(SOUTH_CHICKEN1, temp);
  3160. POSTING_READ(SOUTH_CHICKEN1);
  3161. }
  3162. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3163. {
  3164. struct drm_device *dev = intel_crtc->base.dev;
  3165. struct drm_i915_private *dev_priv = dev->dev_private;
  3166. switch (intel_crtc->pipe) {
  3167. case PIPE_A:
  3168. break;
  3169. case PIPE_B:
  3170. if (intel_crtc->config.fdi_lanes > 2)
  3171. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3172. else
  3173. cpt_enable_fdi_bc_bifurcation(dev);
  3174. break;
  3175. case PIPE_C:
  3176. cpt_enable_fdi_bc_bifurcation(dev);
  3177. break;
  3178. default:
  3179. BUG();
  3180. }
  3181. }
  3182. /*
  3183. * Enable PCH resources required for PCH ports:
  3184. * - PCH PLLs
  3185. * - FDI training & RX/TX
  3186. * - update transcoder timings
  3187. * - DP transcoding bits
  3188. * - transcoder
  3189. */
  3190. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3191. {
  3192. struct drm_device *dev = crtc->dev;
  3193. struct drm_i915_private *dev_priv = dev->dev_private;
  3194. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3195. int pipe = intel_crtc->pipe;
  3196. u32 reg, temp;
  3197. assert_pch_transcoder_disabled(dev_priv, pipe);
  3198. if (IS_IVYBRIDGE(dev))
  3199. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3200. /* Write the TU size bits before fdi link training, so that error
  3201. * detection works. */
  3202. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3203. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3204. /* For PCH output, training FDI link */
  3205. dev_priv->display.fdi_link_train(crtc);
  3206. /* We need to program the right clock selection before writing the pixel
  3207. * mutliplier into the DPLL. */
  3208. if (HAS_PCH_CPT(dev)) {
  3209. u32 sel;
  3210. temp = I915_READ(PCH_DPLL_SEL);
  3211. temp |= TRANS_DPLL_ENABLE(pipe);
  3212. sel = TRANS_DPLLB_SEL(pipe);
  3213. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3214. temp |= sel;
  3215. else
  3216. temp &= ~sel;
  3217. I915_WRITE(PCH_DPLL_SEL, temp);
  3218. }
  3219. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3220. * transcoder, and we actually should do this to not upset any PCH
  3221. * transcoder that already use the clock when we share it.
  3222. *
  3223. * Note that enable_shared_dpll tries to do the right thing, but
  3224. * get_shared_dpll unconditionally resets the pll - we need that to have
  3225. * the right LVDS enable sequence. */
  3226. intel_enable_shared_dpll(intel_crtc);
  3227. /* set transcoder timing, panel must allow it */
  3228. assert_panel_unlocked(dev_priv, pipe);
  3229. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3230. intel_fdi_normal_train(crtc);
  3231. /* For PCH DP, enable TRANS_DP_CTL */
  3232. if (HAS_PCH_CPT(dev) && intel_crtc->config.has_dp_encoder) {
  3233. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3234. reg = TRANS_DP_CTL(pipe);
  3235. temp = I915_READ(reg);
  3236. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3237. TRANS_DP_SYNC_MASK |
  3238. TRANS_DP_BPC_MASK);
  3239. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3240. TRANS_DP_ENH_FRAMING);
  3241. temp |= bpc << 9; /* same format but at 11:9 */
  3242. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3243. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3244. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3245. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3246. switch (intel_trans_dp_port_sel(crtc)) {
  3247. case PCH_DP_B:
  3248. temp |= TRANS_DP_PORT_SEL_B;
  3249. break;
  3250. case PCH_DP_C:
  3251. temp |= TRANS_DP_PORT_SEL_C;
  3252. break;
  3253. case PCH_DP_D:
  3254. temp |= TRANS_DP_PORT_SEL_D;
  3255. break;
  3256. default:
  3257. BUG();
  3258. }
  3259. I915_WRITE(reg, temp);
  3260. }
  3261. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3262. }
  3263. static void lpt_pch_enable(struct drm_crtc *crtc)
  3264. {
  3265. struct drm_device *dev = crtc->dev;
  3266. struct drm_i915_private *dev_priv = dev->dev_private;
  3267. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3268. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3269. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3270. lpt_program_iclkip(crtc);
  3271. /* Set transcoder timing. */
  3272. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3273. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3274. }
  3275. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3276. {
  3277. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3278. if (pll == NULL)
  3279. return;
  3280. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3281. WARN(1, "bad %s crtc mask\n", pll->name);
  3282. return;
  3283. }
  3284. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3285. if (pll->config.crtc_mask == 0) {
  3286. WARN_ON(pll->on);
  3287. WARN_ON(pll->active);
  3288. }
  3289. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3290. }
  3291. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3292. {
  3293. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3294. struct intel_shared_dpll *pll;
  3295. enum intel_dpll_id i;
  3296. if (HAS_PCH_IBX(dev_priv->dev)) {
  3297. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3298. i = (enum intel_dpll_id) crtc->pipe;
  3299. pll = &dev_priv->shared_dplls[i];
  3300. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3301. crtc->base.base.id, pll->name);
  3302. WARN_ON(pll->new_config->crtc_mask);
  3303. goto found;
  3304. }
  3305. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3306. pll = &dev_priv->shared_dplls[i];
  3307. /* Only want to check enabled timings first */
  3308. if (pll->new_config->crtc_mask == 0)
  3309. continue;
  3310. if (memcmp(&crtc->new_config->dpll_hw_state,
  3311. &pll->new_config->hw_state,
  3312. sizeof(pll->new_config->hw_state)) == 0) {
  3313. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3314. crtc->base.base.id, pll->name,
  3315. pll->new_config->crtc_mask,
  3316. pll->active);
  3317. goto found;
  3318. }
  3319. }
  3320. /* Ok no matching timings, maybe there's a free one? */
  3321. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3322. pll = &dev_priv->shared_dplls[i];
  3323. if (pll->new_config->crtc_mask == 0) {
  3324. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3325. crtc->base.base.id, pll->name);
  3326. goto found;
  3327. }
  3328. }
  3329. return NULL;
  3330. found:
  3331. if (pll->new_config->crtc_mask == 0)
  3332. pll->new_config->hw_state = crtc->new_config->dpll_hw_state;
  3333. crtc->new_config->shared_dpll = i;
  3334. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3335. pipe_name(crtc->pipe));
  3336. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3337. return pll;
  3338. }
  3339. /**
  3340. * intel_shared_dpll_start_config - start a new PLL staged config
  3341. * @dev_priv: DRM device
  3342. * @clear_pipes: mask of pipes that will have their PLLs freed
  3343. *
  3344. * Starts a new PLL staged config, copying the current config but
  3345. * releasing the references of pipes specified in clear_pipes.
  3346. */
  3347. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3348. unsigned clear_pipes)
  3349. {
  3350. struct intel_shared_dpll *pll;
  3351. enum intel_dpll_id i;
  3352. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3353. pll = &dev_priv->shared_dplls[i];
  3354. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3355. GFP_KERNEL);
  3356. if (!pll->new_config)
  3357. goto cleanup;
  3358. pll->new_config->crtc_mask &= ~clear_pipes;
  3359. }
  3360. return 0;
  3361. cleanup:
  3362. while (--i >= 0) {
  3363. pll = &dev_priv->shared_dplls[i];
  3364. kfree(pll->new_config);
  3365. pll->new_config = NULL;
  3366. }
  3367. return -ENOMEM;
  3368. }
  3369. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3370. {
  3371. struct intel_shared_dpll *pll;
  3372. enum intel_dpll_id i;
  3373. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3374. pll = &dev_priv->shared_dplls[i];
  3375. WARN_ON(pll->new_config == &pll->config);
  3376. pll->config = *pll->new_config;
  3377. kfree(pll->new_config);
  3378. pll->new_config = NULL;
  3379. }
  3380. }
  3381. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3382. {
  3383. struct intel_shared_dpll *pll;
  3384. enum intel_dpll_id i;
  3385. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3386. pll = &dev_priv->shared_dplls[i];
  3387. WARN_ON(pll->new_config == &pll->config);
  3388. kfree(pll->new_config);
  3389. pll->new_config = NULL;
  3390. }
  3391. }
  3392. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3393. {
  3394. struct drm_i915_private *dev_priv = dev->dev_private;
  3395. int dslreg = PIPEDSL(pipe);
  3396. u32 temp;
  3397. temp = I915_READ(dslreg);
  3398. udelay(500);
  3399. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3400. if (wait_for(I915_READ(dslreg) != temp, 5))
  3401. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3402. }
  3403. }
  3404. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3405. {
  3406. struct drm_device *dev = crtc->base.dev;
  3407. struct drm_i915_private *dev_priv = dev->dev_private;
  3408. int pipe = crtc->pipe;
  3409. if (crtc->config.pch_pfit.enabled) {
  3410. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3411. I915_WRITE(PS_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3412. I915_WRITE(PS_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3413. }
  3414. }
  3415. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3416. {
  3417. struct drm_device *dev = crtc->base.dev;
  3418. struct drm_i915_private *dev_priv = dev->dev_private;
  3419. int pipe = crtc->pipe;
  3420. if (crtc->config.pch_pfit.enabled) {
  3421. /* Force use of hard-coded filter coefficients
  3422. * as some pre-programmed values are broken,
  3423. * e.g. x201.
  3424. */
  3425. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3426. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3427. PF_PIPE_SEL_IVB(pipe));
  3428. else
  3429. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3430. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3431. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3432. }
  3433. }
  3434. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3435. {
  3436. struct drm_device *dev = crtc->dev;
  3437. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3438. struct drm_plane *plane;
  3439. struct intel_plane *intel_plane;
  3440. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3441. intel_plane = to_intel_plane(plane);
  3442. if (intel_plane->pipe == pipe)
  3443. intel_plane_restore(&intel_plane->base);
  3444. }
  3445. }
  3446. static void intel_disable_sprite_planes(struct drm_crtc *crtc)
  3447. {
  3448. struct drm_device *dev = crtc->dev;
  3449. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3450. struct drm_plane *plane;
  3451. struct intel_plane *intel_plane;
  3452. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3453. intel_plane = to_intel_plane(plane);
  3454. if (intel_plane->pipe == pipe)
  3455. plane->funcs->disable_plane(plane);
  3456. }
  3457. }
  3458. void hsw_enable_ips(struct intel_crtc *crtc)
  3459. {
  3460. struct drm_device *dev = crtc->base.dev;
  3461. struct drm_i915_private *dev_priv = dev->dev_private;
  3462. if (!crtc->config.ips_enabled)
  3463. return;
  3464. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3465. intel_wait_for_vblank(dev, crtc->pipe);
  3466. assert_plane_enabled(dev_priv, crtc->plane);
  3467. if (IS_BROADWELL(dev)) {
  3468. mutex_lock(&dev_priv->rps.hw_lock);
  3469. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3470. mutex_unlock(&dev_priv->rps.hw_lock);
  3471. /* Quoting Art Runyan: "its not safe to expect any particular
  3472. * value in IPS_CTL bit 31 after enabling IPS through the
  3473. * mailbox." Moreover, the mailbox may return a bogus state,
  3474. * so we need to just enable it and continue on.
  3475. */
  3476. } else {
  3477. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3478. /* The bit only becomes 1 in the next vblank, so this wait here
  3479. * is essentially intel_wait_for_vblank. If we don't have this
  3480. * and don't wait for vblanks until the end of crtc_enable, then
  3481. * the HW state readout code will complain that the expected
  3482. * IPS_CTL value is not the one we read. */
  3483. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3484. DRM_ERROR("Timed out waiting for IPS enable\n");
  3485. }
  3486. }
  3487. void hsw_disable_ips(struct intel_crtc *crtc)
  3488. {
  3489. struct drm_device *dev = crtc->base.dev;
  3490. struct drm_i915_private *dev_priv = dev->dev_private;
  3491. if (!crtc->config.ips_enabled)
  3492. return;
  3493. assert_plane_enabled(dev_priv, crtc->plane);
  3494. if (IS_BROADWELL(dev)) {
  3495. mutex_lock(&dev_priv->rps.hw_lock);
  3496. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3497. mutex_unlock(&dev_priv->rps.hw_lock);
  3498. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3499. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3500. DRM_ERROR("Timed out waiting for IPS disable\n");
  3501. } else {
  3502. I915_WRITE(IPS_CTL, 0);
  3503. POSTING_READ(IPS_CTL);
  3504. }
  3505. /* We need to wait for a vblank before we can disable the plane. */
  3506. intel_wait_for_vblank(dev, crtc->pipe);
  3507. }
  3508. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3509. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3510. {
  3511. struct drm_device *dev = crtc->dev;
  3512. struct drm_i915_private *dev_priv = dev->dev_private;
  3513. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3514. enum pipe pipe = intel_crtc->pipe;
  3515. int palreg = PALETTE(pipe);
  3516. int i;
  3517. bool reenable_ips = false;
  3518. /* The clocks have to be on to load the palette. */
  3519. if (!crtc->enabled || !intel_crtc->active)
  3520. return;
  3521. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3522. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3523. assert_dsi_pll_enabled(dev_priv);
  3524. else
  3525. assert_pll_enabled(dev_priv, pipe);
  3526. }
  3527. /* use legacy palette for Ironlake */
  3528. if (!HAS_GMCH_DISPLAY(dev))
  3529. palreg = LGC_PALETTE(pipe);
  3530. /* Workaround : Do not read or write the pipe palette/gamma data while
  3531. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3532. */
  3533. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3534. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3535. GAMMA_MODE_MODE_SPLIT)) {
  3536. hsw_disable_ips(intel_crtc);
  3537. reenable_ips = true;
  3538. }
  3539. for (i = 0; i < 256; i++) {
  3540. I915_WRITE(palreg + 4 * i,
  3541. (intel_crtc->lut_r[i] << 16) |
  3542. (intel_crtc->lut_g[i] << 8) |
  3543. intel_crtc->lut_b[i]);
  3544. }
  3545. if (reenable_ips)
  3546. hsw_enable_ips(intel_crtc);
  3547. }
  3548. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3549. {
  3550. if (!enable && intel_crtc->overlay) {
  3551. struct drm_device *dev = intel_crtc->base.dev;
  3552. struct drm_i915_private *dev_priv = dev->dev_private;
  3553. mutex_lock(&dev->struct_mutex);
  3554. dev_priv->mm.interruptible = false;
  3555. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3556. dev_priv->mm.interruptible = true;
  3557. mutex_unlock(&dev->struct_mutex);
  3558. }
  3559. /* Let userspace switch the overlay on again. In most cases userspace
  3560. * has to recompute where to put it anyway.
  3561. */
  3562. }
  3563. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3564. {
  3565. struct drm_device *dev = crtc->dev;
  3566. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3567. int pipe = intel_crtc->pipe;
  3568. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3569. intel_enable_sprite_planes(crtc);
  3570. intel_crtc_update_cursor(crtc, true);
  3571. intel_crtc_dpms_overlay(intel_crtc, true);
  3572. hsw_enable_ips(intel_crtc);
  3573. mutex_lock(&dev->struct_mutex);
  3574. intel_fbc_update(dev);
  3575. mutex_unlock(&dev->struct_mutex);
  3576. /*
  3577. * FIXME: Once we grow proper nuclear flip support out of this we need
  3578. * to compute the mask of flip planes precisely. For the time being
  3579. * consider this a flip from a NULL plane.
  3580. */
  3581. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3582. }
  3583. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3584. {
  3585. struct drm_device *dev = crtc->dev;
  3586. struct drm_i915_private *dev_priv = dev->dev_private;
  3587. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3588. int pipe = intel_crtc->pipe;
  3589. int plane = intel_crtc->plane;
  3590. intel_crtc_wait_for_pending_flips(crtc);
  3591. if (dev_priv->fbc.plane == plane)
  3592. intel_fbc_disable(dev);
  3593. hsw_disable_ips(intel_crtc);
  3594. intel_crtc_dpms_overlay(intel_crtc, false);
  3595. intel_crtc_update_cursor(crtc, false);
  3596. intel_disable_sprite_planes(crtc);
  3597. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3598. /*
  3599. * FIXME: Once we grow proper nuclear flip support out of this we need
  3600. * to compute the mask of flip planes precisely. For the time being
  3601. * consider this a flip to a NULL plane.
  3602. */
  3603. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3604. }
  3605. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3606. {
  3607. struct drm_device *dev = crtc->dev;
  3608. struct drm_i915_private *dev_priv = dev->dev_private;
  3609. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3610. struct intel_encoder *encoder;
  3611. int pipe = intel_crtc->pipe;
  3612. WARN_ON(!crtc->enabled);
  3613. if (intel_crtc->active)
  3614. return;
  3615. if (intel_crtc->config.has_pch_encoder)
  3616. intel_prepare_shared_dpll(intel_crtc);
  3617. if (intel_crtc->config.has_dp_encoder)
  3618. intel_dp_set_m_n(intel_crtc);
  3619. intel_set_pipe_timings(intel_crtc);
  3620. if (intel_crtc->config.has_pch_encoder) {
  3621. intel_cpu_transcoder_set_m_n(intel_crtc,
  3622. &intel_crtc->config.fdi_m_n, NULL);
  3623. }
  3624. ironlake_set_pipeconf(crtc);
  3625. intel_crtc->active = true;
  3626. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3627. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3628. for_each_encoder_on_crtc(dev, crtc, encoder)
  3629. if (encoder->pre_enable)
  3630. encoder->pre_enable(encoder);
  3631. if (intel_crtc->config.has_pch_encoder) {
  3632. /* Note: FDI PLL enabling _must_ be done before we enable the
  3633. * cpu pipes, hence this is separate from all the other fdi/pch
  3634. * enabling. */
  3635. ironlake_fdi_pll_enable(intel_crtc);
  3636. } else {
  3637. assert_fdi_tx_disabled(dev_priv, pipe);
  3638. assert_fdi_rx_disabled(dev_priv, pipe);
  3639. }
  3640. ironlake_pfit_enable(intel_crtc);
  3641. /*
  3642. * On ILK+ LUT must be loaded before the pipe is running but with
  3643. * clocks enabled
  3644. */
  3645. intel_crtc_load_lut(crtc);
  3646. intel_update_watermarks(crtc);
  3647. intel_enable_pipe(intel_crtc);
  3648. if (intel_crtc->config.has_pch_encoder)
  3649. ironlake_pch_enable(crtc);
  3650. assert_vblank_disabled(crtc);
  3651. drm_crtc_vblank_on(crtc);
  3652. for_each_encoder_on_crtc(dev, crtc, encoder)
  3653. encoder->enable(encoder);
  3654. if (HAS_PCH_CPT(dev))
  3655. cpt_verify_modeset(dev, intel_crtc->pipe);
  3656. intel_crtc_enable_planes(crtc);
  3657. }
  3658. /* IPS only exists on ULT machines and is tied to pipe A. */
  3659. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3660. {
  3661. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3662. }
  3663. /*
  3664. * This implements the workaround described in the "notes" section of the mode
  3665. * set sequence documentation. When going from no pipes or single pipe to
  3666. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3667. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3668. */
  3669. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3670. {
  3671. struct drm_device *dev = crtc->base.dev;
  3672. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3673. /* We want to get the other_active_crtc only if there's only 1 other
  3674. * active crtc. */
  3675. for_each_intel_crtc(dev, crtc_it) {
  3676. if (!crtc_it->active || crtc_it == crtc)
  3677. continue;
  3678. if (other_active_crtc)
  3679. return;
  3680. other_active_crtc = crtc_it;
  3681. }
  3682. if (!other_active_crtc)
  3683. return;
  3684. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3685. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3686. }
  3687. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3688. {
  3689. struct drm_device *dev = crtc->dev;
  3690. struct drm_i915_private *dev_priv = dev->dev_private;
  3691. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3692. struct intel_encoder *encoder;
  3693. int pipe = intel_crtc->pipe;
  3694. WARN_ON(!crtc->enabled);
  3695. if (intel_crtc->active)
  3696. return;
  3697. if (intel_crtc_to_shared_dpll(intel_crtc))
  3698. intel_enable_shared_dpll(intel_crtc);
  3699. if (intel_crtc->config.has_dp_encoder)
  3700. intel_dp_set_m_n(intel_crtc);
  3701. intel_set_pipe_timings(intel_crtc);
  3702. if (intel_crtc->config.cpu_transcoder != TRANSCODER_EDP) {
  3703. I915_WRITE(PIPE_MULT(intel_crtc->config.cpu_transcoder),
  3704. intel_crtc->config.pixel_multiplier - 1);
  3705. }
  3706. if (intel_crtc->config.has_pch_encoder) {
  3707. intel_cpu_transcoder_set_m_n(intel_crtc,
  3708. &intel_crtc->config.fdi_m_n, NULL);
  3709. }
  3710. haswell_set_pipeconf(crtc);
  3711. intel_set_pipe_csc(crtc);
  3712. intel_crtc->active = true;
  3713. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3714. for_each_encoder_on_crtc(dev, crtc, encoder)
  3715. if (encoder->pre_enable)
  3716. encoder->pre_enable(encoder);
  3717. if (intel_crtc->config.has_pch_encoder) {
  3718. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3719. true);
  3720. dev_priv->display.fdi_link_train(crtc);
  3721. }
  3722. intel_ddi_enable_pipe_clock(intel_crtc);
  3723. if (IS_SKYLAKE(dev))
  3724. skylake_pfit_enable(intel_crtc);
  3725. else
  3726. ironlake_pfit_enable(intel_crtc);
  3727. /*
  3728. * On ILK+ LUT must be loaded before the pipe is running but with
  3729. * clocks enabled
  3730. */
  3731. intel_crtc_load_lut(crtc);
  3732. intel_ddi_set_pipe_settings(crtc);
  3733. intel_ddi_enable_transcoder_func(crtc);
  3734. intel_update_watermarks(crtc);
  3735. intel_enable_pipe(intel_crtc);
  3736. if (intel_crtc->config.has_pch_encoder)
  3737. lpt_pch_enable(crtc);
  3738. if (intel_crtc->config.dp_encoder_is_mst)
  3739. intel_ddi_set_vc_payload_alloc(crtc, true);
  3740. assert_vblank_disabled(crtc);
  3741. drm_crtc_vblank_on(crtc);
  3742. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3743. encoder->enable(encoder);
  3744. intel_opregion_notify_encoder(encoder, true);
  3745. }
  3746. /* If we change the relative order between pipe/planes enabling, we need
  3747. * to change the workaround. */
  3748. haswell_mode_set_planes_workaround(intel_crtc);
  3749. intel_crtc_enable_planes(crtc);
  3750. }
  3751. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3752. {
  3753. struct drm_device *dev = crtc->base.dev;
  3754. struct drm_i915_private *dev_priv = dev->dev_private;
  3755. int pipe = crtc->pipe;
  3756. /* To avoid upsetting the power well on haswell only disable the pfit if
  3757. * it's in use. The hw state code will make sure we get this right. */
  3758. if (crtc->config.pch_pfit.enabled) {
  3759. I915_WRITE(PS_CTL(pipe), 0);
  3760. I915_WRITE(PS_WIN_POS(pipe), 0);
  3761. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3762. }
  3763. }
  3764. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3765. {
  3766. struct drm_device *dev = crtc->base.dev;
  3767. struct drm_i915_private *dev_priv = dev->dev_private;
  3768. int pipe = crtc->pipe;
  3769. /* To avoid upsetting the power well on haswell only disable the pfit if
  3770. * it's in use. The hw state code will make sure we get this right. */
  3771. if (crtc->config.pch_pfit.enabled) {
  3772. I915_WRITE(PF_CTL(pipe), 0);
  3773. I915_WRITE(PF_WIN_POS(pipe), 0);
  3774. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3775. }
  3776. }
  3777. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3778. {
  3779. struct drm_device *dev = crtc->dev;
  3780. struct drm_i915_private *dev_priv = dev->dev_private;
  3781. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3782. struct intel_encoder *encoder;
  3783. int pipe = intel_crtc->pipe;
  3784. u32 reg, temp;
  3785. if (!intel_crtc->active)
  3786. return;
  3787. intel_crtc_disable_planes(crtc);
  3788. for_each_encoder_on_crtc(dev, crtc, encoder)
  3789. encoder->disable(encoder);
  3790. drm_crtc_vblank_off(crtc);
  3791. assert_vblank_disabled(crtc);
  3792. if (intel_crtc->config.has_pch_encoder)
  3793. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3794. intel_disable_pipe(intel_crtc);
  3795. ironlake_pfit_disable(intel_crtc);
  3796. for_each_encoder_on_crtc(dev, crtc, encoder)
  3797. if (encoder->post_disable)
  3798. encoder->post_disable(encoder);
  3799. if (intel_crtc->config.has_pch_encoder) {
  3800. ironlake_fdi_disable(crtc);
  3801. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3802. if (HAS_PCH_CPT(dev)) {
  3803. /* disable TRANS_DP_CTL */
  3804. reg = TRANS_DP_CTL(pipe);
  3805. temp = I915_READ(reg);
  3806. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3807. TRANS_DP_PORT_SEL_MASK);
  3808. temp |= TRANS_DP_PORT_SEL_NONE;
  3809. I915_WRITE(reg, temp);
  3810. /* disable DPLL_SEL */
  3811. temp = I915_READ(PCH_DPLL_SEL);
  3812. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3813. I915_WRITE(PCH_DPLL_SEL, temp);
  3814. }
  3815. /* disable PCH DPLL */
  3816. intel_disable_shared_dpll(intel_crtc);
  3817. ironlake_fdi_pll_disable(intel_crtc);
  3818. }
  3819. intel_crtc->active = false;
  3820. intel_update_watermarks(crtc);
  3821. mutex_lock(&dev->struct_mutex);
  3822. intel_fbc_update(dev);
  3823. mutex_unlock(&dev->struct_mutex);
  3824. }
  3825. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3826. {
  3827. struct drm_device *dev = crtc->dev;
  3828. struct drm_i915_private *dev_priv = dev->dev_private;
  3829. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3830. struct intel_encoder *encoder;
  3831. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3832. if (!intel_crtc->active)
  3833. return;
  3834. intel_crtc_disable_planes(crtc);
  3835. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3836. intel_opregion_notify_encoder(encoder, false);
  3837. encoder->disable(encoder);
  3838. }
  3839. drm_crtc_vblank_off(crtc);
  3840. assert_vblank_disabled(crtc);
  3841. if (intel_crtc->config.has_pch_encoder)
  3842. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3843. false);
  3844. intel_disable_pipe(intel_crtc);
  3845. if (intel_crtc->config.dp_encoder_is_mst)
  3846. intel_ddi_set_vc_payload_alloc(crtc, false);
  3847. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3848. if (IS_SKYLAKE(dev))
  3849. skylake_pfit_disable(intel_crtc);
  3850. else
  3851. ironlake_pfit_disable(intel_crtc);
  3852. intel_ddi_disable_pipe_clock(intel_crtc);
  3853. if (intel_crtc->config.has_pch_encoder) {
  3854. lpt_disable_pch_transcoder(dev_priv);
  3855. intel_ddi_fdi_disable(crtc);
  3856. }
  3857. for_each_encoder_on_crtc(dev, crtc, encoder)
  3858. if (encoder->post_disable)
  3859. encoder->post_disable(encoder);
  3860. intel_crtc->active = false;
  3861. intel_update_watermarks(crtc);
  3862. mutex_lock(&dev->struct_mutex);
  3863. intel_fbc_update(dev);
  3864. mutex_unlock(&dev->struct_mutex);
  3865. if (intel_crtc_to_shared_dpll(intel_crtc))
  3866. intel_disable_shared_dpll(intel_crtc);
  3867. }
  3868. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3869. {
  3870. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3871. intel_put_shared_dpll(intel_crtc);
  3872. }
  3873. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3874. {
  3875. struct drm_device *dev = crtc->base.dev;
  3876. struct drm_i915_private *dev_priv = dev->dev_private;
  3877. struct intel_crtc_state *pipe_config = &crtc->config;
  3878. if (!crtc->config.gmch_pfit.control)
  3879. return;
  3880. /*
  3881. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3882. * according to register description and PRM.
  3883. */
  3884. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3885. assert_pipe_disabled(dev_priv, crtc->pipe);
  3886. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3887. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3888. /* Border color in case we don't scale up to the full screen. Black by
  3889. * default, change to something else for debugging. */
  3890. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3891. }
  3892. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3893. {
  3894. switch (port) {
  3895. case PORT_A:
  3896. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3897. case PORT_B:
  3898. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3899. case PORT_C:
  3900. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3901. case PORT_D:
  3902. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3903. default:
  3904. WARN_ON_ONCE(1);
  3905. return POWER_DOMAIN_PORT_OTHER;
  3906. }
  3907. }
  3908. #define for_each_power_domain(domain, mask) \
  3909. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3910. if ((1 << (domain)) & (mask))
  3911. enum intel_display_power_domain
  3912. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3913. {
  3914. struct drm_device *dev = intel_encoder->base.dev;
  3915. struct intel_digital_port *intel_dig_port;
  3916. switch (intel_encoder->type) {
  3917. case INTEL_OUTPUT_UNKNOWN:
  3918. /* Only DDI platforms should ever use this output type */
  3919. WARN_ON_ONCE(!HAS_DDI(dev));
  3920. case INTEL_OUTPUT_DISPLAYPORT:
  3921. case INTEL_OUTPUT_HDMI:
  3922. case INTEL_OUTPUT_EDP:
  3923. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3924. return port_to_power_domain(intel_dig_port->port);
  3925. case INTEL_OUTPUT_DP_MST:
  3926. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3927. return port_to_power_domain(intel_dig_port->port);
  3928. case INTEL_OUTPUT_ANALOG:
  3929. return POWER_DOMAIN_PORT_CRT;
  3930. case INTEL_OUTPUT_DSI:
  3931. return POWER_DOMAIN_PORT_DSI;
  3932. default:
  3933. return POWER_DOMAIN_PORT_OTHER;
  3934. }
  3935. }
  3936. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3937. {
  3938. struct drm_device *dev = crtc->dev;
  3939. struct intel_encoder *intel_encoder;
  3940. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3941. enum pipe pipe = intel_crtc->pipe;
  3942. unsigned long mask;
  3943. enum transcoder transcoder;
  3944. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3945. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3946. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3947. if (intel_crtc->config.pch_pfit.enabled ||
  3948. intel_crtc->config.pch_pfit.force_thru)
  3949. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3950. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3951. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3952. return mask;
  3953. }
  3954. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3955. {
  3956. struct drm_i915_private *dev_priv = dev->dev_private;
  3957. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3958. struct intel_crtc *crtc;
  3959. /*
  3960. * First get all needed power domains, then put all unneeded, to avoid
  3961. * any unnecessary toggling of the power wells.
  3962. */
  3963. for_each_intel_crtc(dev, crtc) {
  3964. enum intel_display_power_domain domain;
  3965. if (!crtc->base.enabled)
  3966. continue;
  3967. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3968. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3969. intel_display_power_get(dev_priv, domain);
  3970. }
  3971. if (dev_priv->display.modeset_global_resources)
  3972. dev_priv->display.modeset_global_resources(dev);
  3973. for_each_intel_crtc(dev, crtc) {
  3974. enum intel_display_power_domain domain;
  3975. for_each_power_domain(domain, crtc->enabled_power_domains)
  3976. intel_display_power_put(dev_priv, domain);
  3977. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3978. }
  3979. intel_display_set_init_power(dev_priv, false);
  3980. }
  3981. /* returns HPLL frequency in kHz */
  3982. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3983. {
  3984. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3985. /* Obtain SKU information */
  3986. mutex_lock(&dev_priv->dpio_lock);
  3987. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3988. CCK_FUSE_HPLL_FREQ_MASK;
  3989. mutex_unlock(&dev_priv->dpio_lock);
  3990. return vco_freq[hpll_freq] * 1000;
  3991. }
  3992. static void vlv_update_cdclk(struct drm_device *dev)
  3993. {
  3994. struct drm_i915_private *dev_priv = dev->dev_private;
  3995. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  3996. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  3997. dev_priv->vlv_cdclk_freq);
  3998. /*
  3999. * Program the gmbus_freq based on the cdclk frequency.
  4000. * BSpec erroneously claims we should aim for 4MHz, but
  4001. * in fact 1MHz is the correct frequency.
  4002. */
  4003. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4004. }
  4005. /* Adjust CDclk dividers to allow high res or save power if possible */
  4006. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4007. {
  4008. struct drm_i915_private *dev_priv = dev->dev_private;
  4009. u32 val, cmd;
  4010. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4011. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4012. cmd = 2;
  4013. else if (cdclk == 266667)
  4014. cmd = 1;
  4015. else
  4016. cmd = 0;
  4017. mutex_lock(&dev_priv->rps.hw_lock);
  4018. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4019. val &= ~DSPFREQGUAR_MASK;
  4020. val |= (cmd << DSPFREQGUAR_SHIFT);
  4021. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4022. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4023. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4024. 50)) {
  4025. DRM_ERROR("timed out waiting for CDclk change\n");
  4026. }
  4027. mutex_unlock(&dev_priv->rps.hw_lock);
  4028. if (cdclk == 400000) {
  4029. u32 divider;
  4030. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4031. mutex_lock(&dev_priv->dpio_lock);
  4032. /* adjust cdclk divider */
  4033. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4034. val &= ~DISPLAY_FREQUENCY_VALUES;
  4035. val |= divider;
  4036. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4037. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4038. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4039. 50))
  4040. DRM_ERROR("timed out waiting for CDclk change\n");
  4041. mutex_unlock(&dev_priv->dpio_lock);
  4042. }
  4043. mutex_lock(&dev_priv->dpio_lock);
  4044. /* adjust self-refresh exit latency value */
  4045. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4046. val &= ~0x7f;
  4047. /*
  4048. * For high bandwidth configs, we set a higher latency in the bunit
  4049. * so that the core display fetch happens in time to avoid underruns.
  4050. */
  4051. if (cdclk == 400000)
  4052. val |= 4500 / 250; /* 4.5 usec */
  4053. else
  4054. val |= 3000 / 250; /* 3.0 usec */
  4055. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4056. mutex_unlock(&dev_priv->dpio_lock);
  4057. vlv_update_cdclk(dev);
  4058. }
  4059. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4060. {
  4061. struct drm_i915_private *dev_priv = dev->dev_private;
  4062. u32 val, cmd;
  4063. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4064. switch (cdclk) {
  4065. case 400000:
  4066. cmd = 3;
  4067. break;
  4068. case 333333:
  4069. case 320000:
  4070. cmd = 2;
  4071. break;
  4072. case 266667:
  4073. cmd = 1;
  4074. break;
  4075. case 200000:
  4076. cmd = 0;
  4077. break;
  4078. default:
  4079. MISSING_CASE(cdclk);
  4080. return;
  4081. }
  4082. mutex_lock(&dev_priv->rps.hw_lock);
  4083. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4084. val &= ~DSPFREQGUAR_MASK_CHV;
  4085. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4086. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4087. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4088. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4089. 50)) {
  4090. DRM_ERROR("timed out waiting for CDclk change\n");
  4091. }
  4092. mutex_unlock(&dev_priv->rps.hw_lock);
  4093. vlv_update_cdclk(dev);
  4094. }
  4095. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4096. int max_pixclk)
  4097. {
  4098. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4099. /* FIXME: Punit isn't quite ready yet */
  4100. if (IS_CHERRYVIEW(dev_priv->dev))
  4101. return 400000;
  4102. /*
  4103. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4104. * 200MHz
  4105. * 267MHz
  4106. * 320/333MHz (depends on HPLL freq)
  4107. * 400MHz
  4108. * So we check to see whether we're above 90% of the lower bin and
  4109. * adjust if needed.
  4110. *
  4111. * We seem to get an unstable or solid color picture at 200MHz.
  4112. * Not sure what's wrong. For now use 200MHz only when all pipes
  4113. * are off.
  4114. */
  4115. if (max_pixclk > freq_320*9/10)
  4116. return 400000;
  4117. else if (max_pixclk > 266667*9/10)
  4118. return freq_320;
  4119. else if (max_pixclk > 0)
  4120. return 266667;
  4121. else
  4122. return 200000;
  4123. }
  4124. /* compute the max pixel clock for new configuration */
  4125. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4126. {
  4127. struct drm_device *dev = dev_priv->dev;
  4128. struct intel_crtc *intel_crtc;
  4129. int max_pixclk = 0;
  4130. for_each_intel_crtc(dev, intel_crtc) {
  4131. if (intel_crtc->new_enabled)
  4132. max_pixclk = max(max_pixclk,
  4133. intel_crtc->new_config->base.adjusted_mode.crtc_clock);
  4134. }
  4135. return max_pixclk;
  4136. }
  4137. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4138. unsigned *prepare_pipes)
  4139. {
  4140. struct drm_i915_private *dev_priv = dev->dev_private;
  4141. struct intel_crtc *intel_crtc;
  4142. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4143. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4144. dev_priv->vlv_cdclk_freq)
  4145. return;
  4146. /* disable/enable all currently active pipes while we change cdclk */
  4147. for_each_intel_crtc(dev, intel_crtc)
  4148. if (intel_crtc->base.enabled)
  4149. *prepare_pipes |= (1 << intel_crtc->pipe);
  4150. }
  4151. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4152. {
  4153. struct drm_i915_private *dev_priv = dev->dev_private;
  4154. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4155. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4156. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4157. /*
  4158. * FIXME: We can end up here with all power domains off, yet
  4159. * with a CDCLK frequency other than the minimum. To account
  4160. * for this take the PIPE-A power domain, which covers the HW
  4161. * blocks needed for the following programming. This can be
  4162. * removed once it's guaranteed that we get here either with
  4163. * the minimum CDCLK set, or the required power domains
  4164. * enabled.
  4165. */
  4166. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4167. if (IS_CHERRYVIEW(dev))
  4168. cherryview_set_cdclk(dev, req_cdclk);
  4169. else
  4170. valleyview_set_cdclk(dev, req_cdclk);
  4171. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4172. }
  4173. }
  4174. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4175. {
  4176. struct drm_device *dev = crtc->dev;
  4177. struct drm_i915_private *dev_priv = to_i915(dev);
  4178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4179. struct intel_encoder *encoder;
  4180. int pipe = intel_crtc->pipe;
  4181. bool is_dsi;
  4182. WARN_ON(!crtc->enabled);
  4183. if (intel_crtc->active)
  4184. return;
  4185. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4186. if (!is_dsi) {
  4187. if (IS_CHERRYVIEW(dev))
  4188. chv_prepare_pll(intel_crtc, &intel_crtc->config);
  4189. else
  4190. vlv_prepare_pll(intel_crtc, &intel_crtc->config);
  4191. }
  4192. if (intel_crtc->config.has_dp_encoder)
  4193. intel_dp_set_m_n(intel_crtc);
  4194. intel_set_pipe_timings(intel_crtc);
  4195. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4196. struct drm_i915_private *dev_priv = dev->dev_private;
  4197. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4198. I915_WRITE(CHV_CANVAS(pipe), 0);
  4199. }
  4200. i9xx_set_pipeconf(intel_crtc);
  4201. intel_crtc->active = true;
  4202. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4203. for_each_encoder_on_crtc(dev, crtc, encoder)
  4204. if (encoder->pre_pll_enable)
  4205. encoder->pre_pll_enable(encoder);
  4206. if (!is_dsi) {
  4207. if (IS_CHERRYVIEW(dev))
  4208. chv_enable_pll(intel_crtc, &intel_crtc->config);
  4209. else
  4210. vlv_enable_pll(intel_crtc, &intel_crtc->config);
  4211. }
  4212. for_each_encoder_on_crtc(dev, crtc, encoder)
  4213. if (encoder->pre_enable)
  4214. encoder->pre_enable(encoder);
  4215. i9xx_pfit_enable(intel_crtc);
  4216. intel_crtc_load_lut(crtc);
  4217. intel_update_watermarks(crtc);
  4218. intel_enable_pipe(intel_crtc);
  4219. assert_vblank_disabled(crtc);
  4220. drm_crtc_vblank_on(crtc);
  4221. for_each_encoder_on_crtc(dev, crtc, encoder)
  4222. encoder->enable(encoder);
  4223. intel_crtc_enable_planes(crtc);
  4224. /* Underruns don't raise interrupts, so check manually. */
  4225. i9xx_check_fifo_underruns(dev_priv);
  4226. }
  4227. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4228. {
  4229. struct drm_device *dev = crtc->base.dev;
  4230. struct drm_i915_private *dev_priv = dev->dev_private;
  4231. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  4232. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  4233. }
  4234. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4235. {
  4236. struct drm_device *dev = crtc->dev;
  4237. struct drm_i915_private *dev_priv = to_i915(dev);
  4238. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4239. struct intel_encoder *encoder;
  4240. int pipe = intel_crtc->pipe;
  4241. WARN_ON(!crtc->enabled);
  4242. if (intel_crtc->active)
  4243. return;
  4244. i9xx_set_pll_dividers(intel_crtc);
  4245. if (intel_crtc->config.has_dp_encoder)
  4246. intel_dp_set_m_n(intel_crtc);
  4247. intel_set_pipe_timings(intel_crtc);
  4248. i9xx_set_pipeconf(intel_crtc);
  4249. intel_crtc->active = true;
  4250. if (!IS_GEN2(dev))
  4251. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4252. for_each_encoder_on_crtc(dev, crtc, encoder)
  4253. if (encoder->pre_enable)
  4254. encoder->pre_enable(encoder);
  4255. i9xx_enable_pll(intel_crtc);
  4256. i9xx_pfit_enable(intel_crtc);
  4257. intel_crtc_load_lut(crtc);
  4258. intel_update_watermarks(crtc);
  4259. intel_enable_pipe(intel_crtc);
  4260. assert_vblank_disabled(crtc);
  4261. drm_crtc_vblank_on(crtc);
  4262. for_each_encoder_on_crtc(dev, crtc, encoder)
  4263. encoder->enable(encoder);
  4264. intel_crtc_enable_planes(crtc);
  4265. /*
  4266. * Gen2 reports pipe underruns whenever all planes are disabled.
  4267. * So don't enable underrun reporting before at least some planes
  4268. * are enabled.
  4269. * FIXME: Need to fix the logic to work when we turn off all planes
  4270. * but leave the pipe running.
  4271. */
  4272. if (IS_GEN2(dev))
  4273. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4274. /* Underruns don't raise interrupts, so check manually. */
  4275. i9xx_check_fifo_underruns(dev_priv);
  4276. }
  4277. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4278. {
  4279. struct drm_device *dev = crtc->base.dev;
  4280. struct drm_i915_private *dev_priv = dev->dev_private;
  4281. if (!crtc->config.gmch_pfit.control)
  4282. return;
  4283. assert_pipe_disabled(dev_priv, crtc->pipe);
  4284. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4285. I915_READ(PFIT_CONTROL));
  4286. I915_WRITE(PFIT_CONTROL, 0);
  4287. }
  4288. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4289. {
  4290. struct drm_device *dev = crtc->dev;
  4291. struct drm_i915_private *dev_priv = dev->dev_private;
  4292. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4293. struct intel_encoder *encoder;
  4294. int pipe = intel_crtc->pipe;
  4295. if (!intel_crtc->active)
  4296. return;
  4297. /*
  4298. * Gen2 reports pipe underruns whenever all planes are disabled.
  4299. * So diasble underrun reporting before all the planes get disabled.
  4300. * FIXME: Need to fix the logic to work when we turn off all planes
  4301. * but leave the pipe running.
  4302. */
  4303. if (IS_GEN2(dev))
  4304. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4305. /*
  4306. * Vblank time updates from the shadow to live plane control register
  4307. * are blocked if the memory self-refresh mode is active at that
  4308. * moment. So to make sure the plane gets truly disabled, disable
  4309. * first the self-refresh mode. The self-refresh enable bit in turn
  4310. * will be checked/applied by the HW only at the next frame start
  4311. * event which is after the vblank start event, so we need to have a
  4312. * wait-for-vblank between disabling the plane and the pipe.
  4313. */
  4314. intel_set_memory_cxsr(dev_priv, false);
  4315. intel_crtc_disable_planes(crtc);
  4316. /*
  4317. * On gen2 planes are double buffered but the pipe isn't, so we must
  4318. * wait for planes to fully turn off before disabling the pipe.
  4319. * We also need to wait on all gmch platforms because of the
  4320. * self-refresh mode constraint explained above.
  4321. */
  4322. intel_wait_for_vblank(dev, pipe);
  4323. for_each_encoder_on_crtc(dev, crtc, encoder)
  4324. encoder->disable(encoder);
  4325. drm_crtc_vblank_off(crtc);
  4326. assert_vblank_disabled(crtc);
  4327. intel_disable_pipe(intel_crtc);
  4328. i9xx_pfit_disable(intel_crtc);
  4329. for_each_encoder_on_crtc(dev, crtc, encoder)
  4330. if (encoder->post_disable)
  4331. encoder->post_disable(encoder);
  4332. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4333. if (IS_CHERRYVIEW(dev))
  4334. chv_disable_pll(dev_priv, pipe);
  4335. else if (IS_VALLEYVIEW(dev))
  4336. vlv_disable_pll(dev_priv, pipe);
  4337. else
  4338. i9xx_disable_pll(intel_crtc);
  4339. }
  4340. if (!IS_GEN2(dev))
  4341. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4342. intel_crtc->active = false;
  4343. intel_update_watermarks(crtc);
  4344. mutex_lock(&dev->struct_mutex);
  4345. intel_fbc_update(dev);
  4346. mutex_unlock(&dev->struct_mutex);
  4347. }
  4348. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4349. {
  4350. }
  4351. /* Master function to enable/disable CRTC and corresponding power wells */
  4352. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4353. {
  4354. struct drm_device *dev = crtc->dev;
  4355. struct drm_i915_private *dev_priv = dev->dev_private;
  4356. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4357. enum intel_display_power_domain domain;
  4358. unsigned long domains;
  4359. if (enable) {
  4360. if (!intel_crtc->active) {
  4361. domains = get_crtc_power_domains(crtc);
  4362. for_each_power_domain(domain, domains)
  4363. intel_display_power_get(dev_priv, domain);
  4364. intel_crtc->enabled_power_domains = domains;
  4365. dev_priv->display.crtc_enable(crtc);
  4366. }
  4367. } else {
  4368. if (intel_crtc->active) {
  4369. dev_priv->display.crtc_disable(crtc);
  4370. domains = intel_crtc->enabled_power_domains;
  4371. for_each_power_domain(domain, domains)
  4372. intel_display_power_put(dev_priv, domain);
  4373. intel_crtc->enabled_power_domains = 0;
  4374. }
  4375. }
  4376. }
  4377. /**
  4378. * Sets the power management mode of the pipe and plane.
  4379. */
  4380. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4381. {
  4382. struct drm_device *dev = crtc->dev;
  4383. struct intel_encoder *intel_encoder;
  4384. bool enable = false;
  4385. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4386. enable |= intel_encoder->connectors_active;
  4387. intel_crtc_control(crtc, enable);
  4388. }
  4389. static void intel_crtc_disable(struct drm_crtc *crtc)
  4390. {
  4391. struct drm_device *dev = crtc->dev;
  4392. struct drm_connector *connector;
  4393. struct drm_i915_private *dev_priv = dev->dev_private;
  4394. /* crtc should still be enabled when we disable it. */
  4395. WARN_ON(!crtc->enabled);
  4396. dev_priv->display.crtc_disable(crtc);
  4397. dev_priv->display.off(crtc);
  4398. crtc->primary->funcs->disable_plane(crtc->primary);
  4399. /* Update computed state. */
  4400. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4401. if (!connector->encoder || !connector->encoder->crtc)
  4402. continue;
  4403. if (connector->encoder->crtc != crtc)
  4404. continue;
  4405. connector->dpms = DRM_MODE_DPMS_OFF;
  4406. to_intel_encoder(connector->encoder)->connectors_active = false;
  4407. }
  4408. }
  4409. void intel_encoder_destroy(struct drm_encoder *encoder)
  4410. {
  4411. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4412. drm_encoder_cleanup(encoder);
  4413. kfree(intel_encoder);
  4414. }
  4415. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4416. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4417. * state of the entire output pipe. */
  4418. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4419. {
  4420. if (mode == DRM_MODE_DPMS_ON) {
  4421. encoder->connectors_active = true;
  4422. intel_crtc_update_dpms(encoder->base.crtc);
  4423. } else {
  4424. encoder->connectors_active = false;
  4425. intel_crtc_update_dpms(encoder->base.crtc);
  4426. }
  4427. }
  4428. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4429. * internal consistency). */
  4430. static void intel_connector_check_state(struct intel_connector *connector)
  4431. {
  4432. if (connector->get_hw_state(connector)) {
  4433. struct intel_encoder *encoder = connector->encoder;
  4434. struct drm_crtc *crtc;
  4435. bool encoder_enabled;
  4436. enum pipe pipe;
  4437. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4438. connector->base.base.id,
  4439. connector->base.name);
  4440. /* there is no real hw state for MST connectors */
  4441. if (connector->mst_port)
  4442. return;
  4443. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4444. "wrong connector dpms state\n");
  4445. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  4446. "active connector not linked to encoder\n");
  4447. if (encoder) {
  4448. I915_STATE_WARN(!encoder->connectors_active,
  4449. "encoder->connectors_active not set\n");
  4450. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4451. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  4452. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  4453. return;
  4454. crtc = encoder->base.crtc;
  4455. I915_STATE_WARN(!crtc->enabled, "crtc not enabled\n");
  4456. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4457. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  4458. "encoder active on the wrong pipe\n");
  4459. }
  4460. }
  4461. }
  4462. /* Even simpler default implementation, if there's really no special case to
  4463. * consider. */
  4464. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4465. {
  4466. /* All the simple cases only support two dpms states. */
  4467. if (mode != DRM_MODE_DPMS_ON)
  4468. mode = DRM_MODE_DPMS_OFF;
  4469. if (mode == connector->dpms)
  4470. return;
  4471. connector->dpms = mode;
  4472. /* Only need to change hw state when actually enabled */
  4473. if (connector->encoder)
  4474. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4475. intel_modeset_check_state(connector->dev);
  4476. }
  4477. /* Simple connector->get_hw_state implementation for encoders that support only
  4478. * one connector and no cloning and hence the encoder state determines the state
  4479. * of the connector. */
  4480. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4481. {
  4482. enum pipe pipe = 0;
  4483. struct intel_encoder *encoder = connector->encoder;
  4484. return encoder->get_hw_state(encoder, &pipe);
  4485. }
  4486. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4487. struct intel_crtc_state *pipe_config)
  4488. {
  4489. struct drm_i915_private *dev_priv = dev->dev_private;
  4490. struct intel_crtc *pipe_B_crtc =
  4491. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4492. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4493. pipe_name(pipe), pipe_config->fdi_lanes);
  4494. if (pipe_config->fdi_lanes > 4) {
  4495. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4496. pipe_name(pipe), pipe_config->fdi_lanes);
  4497. return false;
  4498. }
  4499. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4500. if (pipe_config->fdi_lanes > 2) {
  4501. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4502. pipe_config->fdi_lanes);
  4503. return false;
  4504. } else {
  4505. return true;
  4506. }
  4507. }
  4508. if (INTEL_INFO(dev)->num_pipes == 2)
  4509. return true;
  4510. /* Ivybridge 3 pipe is really complicated */
  4511. switch (pipe) {
  4512. case PIPE_A:
  4513. return true;
  4514. case PIPE_B:
  4515. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4516. pipe_config->fdi_lanes > 2) {
  4517. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4518. pipe_name(pipe), pipe_config->fdi_lanes);
  4519. return false;
  4520. }
  4521. return true;
  4522. case PIPE_C:
  4523. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4524. pipe_B_crtc->config.fdi_lanes <= 2) {
  4525. if (pipe_config->fdi_lanes > 2) {
  4526. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4527. pipe_name(pipe), pipe_config->fdi_lanes);
  4528. return false;
  4529. }
  4530. } else {
  4531. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4532. return false;
  4533. }
  4534. return true;
  4535. default:
  4536. BUG();
  4537. }
  4538. }
  4539. #define RETRY 1
  4540. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4541. struct intel_crtc_state *pipe_config)
  4542. {
  4543. struct drm_device *dev = intel_crtc->base.dev;
  4544. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4545. int lane, link_bw, fdi_dotclock;
  4546. bool setup_ok, needs_recompute = false;
  4547. retry:
  4548. /* FDI is a binary signal running at ~2.7GHz, encoding
  4549. * each output octet as 10 bits. The actual frequency
  4550. * is stored as a divider into a 100MHz clock, and the
  4551. * mode pixel clock is stored in units of 1KHz.
  4552. * Hence the bw of each lane in terms of the mode signal
  4553. * is:
  4554. */
  4555. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4556. fdi_dotclock = adjusted_mode->crtc_clock;
  4557. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4558. pipe_config->pipe_bpp);
  4559. pipe_config->fdi_lanes = lane;
  4560. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4561. link_bw, &pipe_config->fdi_m_n);
  4562. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4563. intel_crtc->pipe, pipe_config);
  4564. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4565. pipe_config->pipe_bpp -= 2*3;
  4566. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4567. pipe_config->pipe_bpp);
  4568. needs_recompute = true;
  4569. pipe_config->bw_constrained = true;
  4570. goto retry;
  4571. }
  4572. if (needs_recompute)
  4573. return RETRY;
  4574. return setup_ok ? 0 : -EINVAL;
  4575. }
  4576. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4577. struct intel_crtc_state *pipe_config)
  4578. {
  4579. pipe_config->ips_enabled = i915.enable_ips &&
  4580. hsw_crtc_supports_ips(crtc) &&
  4581. pipe_config->pipe_bpp <= 24;
  4582. }
  4583. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4584. struct intel_crtc_state *pipe_config)
  4585. {
  4586. struct drm_device *dev = crtc->base.dev;
  4587. struct drm_i915_private *dev_priv = dev->dev_private;
  4588. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4589. /* FIXME should check pixel clock limits on all platforms */
  4590. if (INTEL_INFO(dev)->gen < 4) {
  4591. int clock_limit =
  4592. dev_priv->display.get_display_clock_speed(dev);
  4593. /*
  4594. * Enable pixel doubling when the dot clock
  4595. * is > 90% of the (display) core speed.
  4596. *
  4597. * GDG double wide on either pipe,
  4598. * otherwise pipe A only.
  4599. */
  4600. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4601. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4602. clock_limit *= 2;
  4603. pipe_config->double_wide = true;
  4604. }
  4605. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4606. return -EINVAL;
  4607. }
  4608. /*
  4609. * Pipe horizontal size must be even in:
  4610. * - DVO ganged mode
  4611. * - LVDS dual channel mode
  4612. * - Double wide pipe
  4613. */
  4614. if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4615. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4616. pipe_config->pipe_src_w &= ~1;
  4617. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4618. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4619. */
  4620. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4621. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4622. return -EINVAL;
  4623. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4624. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4625. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4626. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4627. * for lvds. */
  4628. pipe_config->pipe_bpp = 8*3;
  4629. }
  4630. if (HAS_IPS(dev))
  4631. hsw_compute_ips_config(crtc, pipe_config);
  4632. if (pipe_config->has_pch_encoder)
  4633. return ironlake_fdi_compute_config(crtc, pipe_config);
  4634. return 0;
  4635. }
  4636. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4637. {
  4638. struct drm_i915_private *dev_priv = dev->dev_private;
  4639. u32 val;
  4640. int divider;
  4641. /* FIXME: Punit isn't quite ready yet */
  4642. if (IS_CHERRYVIEW(dev))
  4643. return 400000;
  4644. if (dev_priv->hpll_freq == 0)
  4645. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4646. mutex_lock(&dev_priv->dpio_lock);
  4647. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4648. mutex_unlock(&dev_priv->dpio_lock);
  4649. divider = val & DISPLAY_FREQUENCY_VALUES;
  4650. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4651. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4652. "cdclk change in progress\n");
  4653. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4654. }
  4655. static int i945_get_display_clock_speed(struct drm_device *dev)
  4656. {
  4657. return 400000;
  4658. }
  4659. static int i915_get_display_clock_speed(struct drm_device *dev)
  4660. {
  4661. return 333000;
  4662. }
  4663. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4664. {
  4665. return 200000;
  4666. }
  4667. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4668. {
  4669. u16 gcfgc = 0;
  4670. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4671. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4672. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4673. return 267000;
  4674. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4675. return 333000;
  4676. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4677. return 444000;
  4678. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4679. return 200000;
  4680. default:
  4681. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4682. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4683. return 133000;
  4684. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4685. return 167000;
  4686. }
  4687. }
  4688. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4689. {
  4690. u16 gcfgc = 0;
  4691. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4692. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4693. return 133000;
  4694. else {
  4695. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4696. case GC_DISPLAY_CLOCK_333_MHZ:
  4697. return 333000;
  4698. default:
  4699. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4700. return 190000;
  4701. }
  4702. }
  4703. }
  4704. static int i865_get_display_clock_speed(struct drm_device *dev)
  4705. {
  4706. return 266000;
  4707. }
  4708. static int i855_get_display_clock_speed(struct drm_device *dev)
  4709. {
  4710. u16 hpllcc = 0;
  4711. /* Assume that the hardware is in the high speed state. This
  4712. * should be the default.
  4713. */
  4714. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4715. case GC_CLOCK_133_200:
  4716. case GC_CLOCK_100_200:
  4717. return 200000;
  4718. case GC_CLOCK_166_250:
  4719. return 250000;
  4720. case GC_CLOCK_100_133:
  4721. return 133000;
  4722. }
  4723. /* Shouldn't happen */
  4724. return 0;
  4725. }
  4726. static int i830_get_display_clock_speed(struct drm_device *dev)
  4727. {
  4728. return 133000;
  4729. }
  4730. static void
  4731. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4732. {
  4733. while (*num > DATA_LINK_M_N_MASK ||
  4734. *den > DATA_LINK_M_N_MASK) {
  4735. *num >>= 1;
  4736. *den >>= 1;
  4737. }
  4738. }
  4739. static void compute_m_n(unsigned int m, unsigned int n,
  4740. uint32_t *ret_m, uint32_t *ret_n)
  4741. {
  4742. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4743. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4744. intel_reduce_m_n_ratio(ret_m, ret_n);
  4745. }
  4746. void
  4747. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4748. int pixel_clock, int link_clock,
  4749. struct intel_link_m_n *m_n)
  4750. {
  4751. m_n->tu = 64;
  4752. compute_m_n(bits_per_pixel * pixel_clock,
  4753. link_clock * nlanes * 8,
  4754. &m_n->gmch_m, &m_n->gmch_n);
  4755. compute_m_n(pixel_clock, link_clock,
  4756. &m_n->link_m, &m_n->link_n);
  4757. }
  4758. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4759. {
  4760. if (i915.panel_use_ssc >= 0)
  4761. return i915.panel_use_ssc != 0;
  4762. return dev_priv->vbt.lvds_use_ssc
  4763. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4764. }
  4765. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  4766. {
  4767. struct drm_device *dev = crtc->base.dev;
  4768. struct drm_i915_private *dev_priv = dev->dev_private;
  4769. int refclk;
  4770. if (IS_VALLEYVIEW(dev)) {
  4771. refclk = 100000;
  4772. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4773. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4774. refclk = dev_priv->vbt.lvds_ssc_freq;
  4775. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4776. } else if (!IS_GEN2(dev)) {
  4777. refclk = 96000;
  4778. } else {
  4779. refclk = 48000;
  4780. }
  4781. return refclk;
  4782. }
  4783. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4784. {
  4785. return (1 << dpll->n) << 16 | dpll->m2;
  4786. }
  4787. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4788. {
  4789. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4790. }
  4791. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4792. intel_clock_t *reduced_clock)
  4793. {
  4794. struct drm_device *dev = crtc->base.dev;
  4795. u32 fp, fp2 = 0;
  4796. if (IS_PINEVIEW(dev)) {
  4797. fp = pnv_dpll_compute_fp(&crtc->new_config->dpll);
  4798. if (reduced_clock)
  4799. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4800. } else {
  4801. fp = i9xx_dpll_compute_fp(&crtc->new_config->dpll);
  4802. if (reduced_clock)
  4803. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4804. }
  4805. crtc->new_config->dpll_hw_state.fp0 = fp;
  4806. crtc->lowfreq_avail = false;
  4807. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4808. reduced_clock && i915.powersave) {
  4809. crtc->new_config->dpll_hw_state.fp1 = fp2;
  4810. crtc->lowfreq_avail = true;
  4811. } else {
  4812. crtc->new_config->dpll_hw_state.fp1 = fp;
  4813. }
  4814. }
  4815. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4816. pipe)
  4817. {
  4818. u32 reg_val;
  4819. /*
  4820. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4821. * and set it to a reasonable value instead.
  4822. */
  4823. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4824. reg_val &= 0xffffff00;
  4825. reg_val |= 0x00000030;
  4826. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4827. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4828. reg_val &= 0x8cffffff;
  4829. reg_val = 0x8c000000;
  4830. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4831. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4832. reg_val &= 0xffffff00;
  4833. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4834. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4835. reg_val &= 0x00ffffff;
  4836. reg_val |= 0xb0000000;
  4837. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4838. }
  4839. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4840. struct intel_link_m_n *m_n)
  4841. {
  4842. struct drm_device *dev = crtc->base.dev;
  4843. struct drm_i915_private *dev_priv = dev->dev_private;
  4844. int pipe = crtc->pipe;
  4845. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4846. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4847. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4848. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4849. }
  4850. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4851. struct intel_link_m_n *m_n,
  4852. struct intel_link_m_n *m2_n2)
  4853. {
  4854. struct drm_device *dev = crtc->base.dev;
  4855. struct drm_i915_private *dev_priv = dev->dev_private;
  4856. int pipe = crtc->pipe;
  4857. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4858. if (INTEL_INFO(dev)->gen >= 5) {
  4859. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4860. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4861. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4862. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4863. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4864. * for gen < 8) and if DRRS is supported (to make sure the
  4865. * registers are not unnecessarily accessed).
  4866. */
  4867. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4868. crtc->config.has_drrs) {
  4869. I915_WRITE(PIPE_DATA_M2(transcoder),
  4870. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4871. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4872. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4873. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4874. }
  4875. } else {
  4876. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4877. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4878. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4879. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4880. }
  4881. }
  4882. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4883. {
  4884. if (crtc->config.has_pch_encoder)
  4885. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4886. else
  4887. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
  4888. &crtc->config.dp_m2_n2);
  4889. }
  4890. static void vlv_update_pll(struct intel_crtc *crtc,
  4891. struct intel_crtc_state *pipe_config)
  4892. {
  4893. u32 dpll, dpll_md;
  4894. /*
  4895. * Enable DPIO clock input. We should never disable the reference
  4896. * clock for pipe B, since VGA hotplug / manual detection depends
  4897. * on it.
  4898. */
  4899. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4900. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4901. /* We should never disable this, set it here for state tracking */
  4902. if (crtc->pipe == PIPE_B)
  4903. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4904. dpll |= DPLL_VCO_ENABLE;
  4905. pipe_config->dpll_hw_state.dpll = dpll;
  4906. dpll_md = (pipe_config->pixel_multiplier - 1)
  4907. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4908. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  4909. }
  4910. static void vlv_prepare_pll(struct intel_crtc *crtc,
  4911. const struct intel_crtc_state *pipe_config)
  4912. {
  4913. struct drm_device *dev = crtc->base.dev;
  4914. struct drm_i915_private *dev_priv = dev->dev_private;
  4915. int pipe = crtc->pipe;
  4916. u32 mdiv;
  4917. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4918. u32 coreclk, reg_val;
  4919. mutex_lock(&dev_priv->dpio_lock);
  4920. bestn = pipe_config->dpll.n;
  4921. bestm1 = pipe_config->dpll.m1;
  4922. bestm2 = pipe_config->dpll.m2;
  4923. bestp1 = pipe_config->dpll.p1;
  4924. bestp2 = pipe_config->dpll.p2;
  4925. /* See eDP HDMI DPIO driver vbios notes doc */
  4926. /* PLL B needs special handling */
  4927. if (pipe == PIPE_B)
  4928. vlv_pllb_recal_opamp(dev_priv, pipe);
  4929. /* Set up Tx target for periodic Rcomp update */
  4930. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4931. /* Disable target IRef on PLL */
  4932. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4933. reg_val &= 0x00ffffff;
  4934. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4935. /* Disable fast lock */
  4936. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4937. /* Set idtafcrecal before PLL is enabled */
  4938. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4939. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4940. mdiv |= ((bestn << DPIO_N_SHIFT));
  4941. mdiv |= (1 << DPIO_K_SHIFT);
  4942. /*
  4943. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4944. * but we don't support that).
  4945. * Note: don't use the DAC post divider as it seems unstable.
  4946. */
  4947. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4948. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4949. mdiv |= DPIO_ENABLE_CALIBRATION;
  4950. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4951. /* Set HBR and RBR LPF coefficients */
  4952. if (pipe_config->port_clock == 162000 ||
  4953. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  4954. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  4955. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4956. 0x009f0003);
  4957. else
  4958. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4959. 0x00d0000f);
  4960. if (crtc->config.has_dp_encoder) {
  4961. /* Use SSC source */
  4962. if (pipe == PIPE_A)
  4963. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4964. 0x0df40000);
  4965. else
  4966. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4967. 0x0df70000);
  4968. } else { /* HDMI or VGA */
  4969. /* Use bend source */
  4970. if (pipe == PIPE_A)
  4971. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4972. 0x0df70000);
  4973. else
  4974. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4975. 0x0df40000);
  4976. }
  4977. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4978. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4979. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  4980. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  4981. coreclk |= 0x01000000;
  4982. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4983. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4984. mutex_unlock(&dev_priv->dpio_lock);
  4985. }
  4986. static void chv_update_pll(struct intel_crtc *crtc,
  4987. struct intel_crtc_state *pipe_config)
  4988. {
  4989. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4990. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4991. DPLL_VCO_ENABLE;
  4992. if (crtc->pipe != PIPE_A)
  4993. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4994. pipe_config->dpll_hw_state.dpll_md =
  4995. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4996. }
  4997. static void chv_prepare_pll(struct intel_crtc *crtc,
  4998. const struct intel_crtc_state *pipe_config)
  4999. {
  5000. struct drm_device *dev = crtc->base.dev;
  5001. struct drm_i915_private *dev_priv = dev->dev_private;
  5002. int pipe = crtc->pipe;
  5003. int dpll_reg = DPLL(crtc->pipe);
  5004. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5005. u32 loopfilter, intcoeff;
  5006. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5007. int refclk;
  5008. bestn = pipe_config->dpll.n;
  5009. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5010. bestm1 = pipe_config->dpll.m1;
  5011. bestm2 = pipe_config->dpll.m2 >> 22;
  5012. bestp1 = pipe_config->dpll.p1;
  5013. bestp2 = pipe_config->dpll.p2;
  5014. /*
  5015. * Enable Refclk and SSC
  5016. */
  5017. I915_WRITE(dpll_reg,
  5018. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5019. mutex_lock(&dev_priv->dpio_lock);
  5020. /* p1 and p2 divider */
  5021. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5022. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5023. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5024. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5025. 1 << DPIO_CHV_K_DIV_SHIFT);
  5026. /* Feedback post-divider - m2 */
  5027. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5028. /* Feedback refclk divider - n and m1 */
  5029. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5030. DPIO_CHV_M1_DIV_BY_2 |
  5031. 1 << DPIO_CHV_N_DIV_SHIFT);
  5032. /* M2 fraction division */
  5033. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5034. /* M2 fraction division enable */
  5035. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  5036. DPIO_CHV_FRAC_DIV_EN |
  5037. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  5038. /* Loop filter */
  5039. refclk = i9xx_get_refclk(crtc, 0);
  5040. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  5041. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  5042. if (refclk == 100000)
  5043. intcoeff = 11;
  5044. else if (refclk == 38400)
  5045. intcoeff = 10;
  5046. else
  5047. intcoeff = 9;
  5048. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  5049. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5050. /* AFC Recal */
  5051. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5052. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5053. DPIO_AFC_RECAL);
  5054. mutex_unlock(&dev_priv->dpio_lock);
  5055. }
  5056. /**
  5057. * vlv_force_pll_on - forcibly enable just the PLL
  5058. * @dev_priv: i915 private structure
  5059. * @pipe: pipe PLL to enable
  5060. * @dpll: PLL configuration
  5061. *
  5062. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5063. * in cases where we need the PLL enabled even when @pipe is not going to
  5064. * be enabled.
  5065. */
  5066. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5067. const struct dpll *dpll)
  5068. {
  5069. struct intel_crtc *crtc =
  5070. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5071. struct intel_crtc_state pipe_config = {
  5072. .pixel_multiplier = 1,
  5073. .dpll = *dpll,
  5074. };
  5075. if (IS_CHERRYVIEW(dev)) {
  5076. chv_update_pll(crtc, &pipe_config);
  5077. chv_prepare_pll(crtc, &pipe_config);
  5078. chv_enable_pll(crtc, &pipe_config);
  5079. } else {
  5080. vlv_update_pll(crtc, &pipe_config);
  5081. vlv_prepare_pll(crtc, &pipe_config);
  5082. vlv_enable_pll(crtc, &pipe_config);
  5083. }
  5084. }
  5085. /**
  5086. * vlv_force_pll_off - forcibly disable just the PLL
  5087. * @dev_priv: i915 private structure
  5088. * @pipe: pipe PLL to disable
  5089. *
  5090. * Disable the PLL for @pipe. To be used in cases where we need
  5091. * the PLL enabled even when @pipe is not going to be enabled.
  5092. */
  5093. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5094. {
  5095. if (IS_CHERRYVIEW(dev))
  5096. chv_disable_pll(to_i915(dev), pipe);
  5097. else
  5098. vlv_disable_pll(to_i915(dev), pipe);
  5099. }
  5100. static void i9xx_update_pll(struct intel_crtc *crtc,
  5101. intel_clock_t *reduced_clock,
  5102. int num_connectors)
  5103. {
  5104. struct drm_device *dev = crtc->base.dev;
  5105. struct drm_i915_private *dev_priv = dev->dev_private;
  5106. u32 dpll;
  5107. bool is_sdvo;
  5108. struct dpll *clock = &crtc->new_config->dpll;
  5109. i9xx_update_pll_dividers(crtc, reduced_clock);
  5110. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5111. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5112. dpll = DPLL_VGA_MODE_DIS;
  5113. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5114. dpll |= DPLLB_MODE_LVDS;
  5115. else
  5116. dpll |= DPLLB_MODE_DAC_SERIAL;
  5117. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5118. dpll |= (crtc->new_config->pixel_multiplier - 1)
  5119. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5120. }
  5121. if (is_sdvo)
  5122. dpll |= DPLL_SDVO_HIGH_SPEED;
  5123. if (crtc->new_config->has_dp_encoder)
  5124. dpll |= DPLL_SDVO_HIGH_SPEED;
  5125. /* compute bitmask from p1 value */
  5126. if (IS_PINEVIEW(dev))
  5127. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5128. else {
  5129. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5130. if (IS_G4X(dev) && reduced_clock)
  5131. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5132. }
  5133. switch (clock->p2) {
  5134. case 5:
  5135. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5136. break;
  5137. case 7:
  5138. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5139. break;
  5140. case 10:
  5141. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5142. break;
  5143. case 14:
  5144. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5145. break;
  5146. }
  5147. if (INTEL_INFO(dev)->gen >= 4)
  5148. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5149. if (crtc->new_config->sdvo_tv_clock)
  5150. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5151. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5152. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5153. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5154. else
  5155. dpll |= PLL_REF_INPUT_DREFCLK;
  5156. dpll |= DPLL_VCO_ENABLE;
  5157. crtc->new_config->dpll_hw_state.dpll = dpll;
  5158. if (INTEL_INFO(dev)->gen >= 4) {
  5159. u32 dpll_md = (crtc->new_config->pixel_multiplier - 1)
  5160. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5161. crtc->new_config->dpll_hw_state.dpll_md = dpll_md;
  5162. }
  5163. }
  5164. static void i8xx_update_pll(struct intel_crtc *crtc,
  5165. intel_clock_t *reduced_clock,
  5166. int num_connectors)
  5167. {
  5168. struct drm_device *dev = crtc->base.dev;
  5169. struct drm_i915_private *dev_priv = dev->dev_private;
  5170. u32 dpll;
  5171. struct dpll *clock = &crtc->new_config->dpll;
  5172. i9xx_update_pll_dividers(crtc, reduced_clock);
  5173. dpll = DPLL_VGA_MODE_DIS;
  5174. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5175. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5176. } else {
  5177. if (clock->p1 == 2)
  5178. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5179. else
  5180. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5181. if (clock->p2 == 4)
  5182. dpll |= PLL_P2_DIVIDE_BY_4;
  5183. }
  5184. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5185. dpll |= DPLL_DVO_2X_MODE;
  5186. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5187. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5188. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5189. else
  5190. dpll |= PLL_REF_INPUT_DREFCLK;
  5191. dpll |= DPLL_VCO_ENABLE;
  5192. crtc->new_config->dpll_hw_state.dpll = dpll;
  5193. }
  5194. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5195. {
  5196. struct drm_device *dev = intel_crtc->base.dev;
  5197. struct drm_i915_private *dev_priv = dev->dev_private;
  5198. enum pipe pipe = intel_crtc->pipe;
  5199. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5200. struct drm_display_mode *adjusted_mode =
  5201. &intel_crtc->config.base.adjusted_mode;
  5202. uint32_t crtc_vtotal, crtc_vblank_end;
  5203. int vsyncshift = 0;
  5204. /* We need to be careful not to changed the adjusted mode, for otherwise
  5205. * the hw state checker will get angry at the mismatch. */
  5206. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5207. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5208. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5209. /* the chip adds 2 halflines automatically */
  5210. crtc_vtotal -= 1;
  5211. crtc_vblank_end -= 1;
  5212. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5213. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5214. else
  5215. vsyncshift = adjusted_mode->crtc_hsync_start -
  5216. adjusted_mode->crtc_htotal / 2;
  5217. if (vsyncshift < 0)
  5218. vsyncshift += adjusted_mode->crtc_htotal;
  5219. }
  5220. if (INTEL_INFO(dev)->gen > 3)
  5221. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5222. I915_WRITE(HTOTAL(cpu_transcoder),
  5223. (adjusted_mode->crtc_hdisplay - 1) |
  5224. ((adjusted_mode->crtc_htotal - 1) << 16));
  5225. I915_WRITE(HBLANK(cpu_transcoder),
  5226. (adjusted_mode->crtc_hblank_start - 1) |
  5227. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5228. I915_WRITE(HSYNC(cpu_transcoder),
  5229. (adjusted_mode->crtc_hsync_start - 1) |
  5230. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5231. I915_WRITE(VTOTAL(cpu_transcoder),
  5232. (adjusted_mode->crtc_vdisplay - 1) |
  5233. ((crtc_vtotal - 1) << 16));
  5234. I915_WRITE(VBLANK(cpu_transcoder),
  5235. (adjusted_mode->crtc_vblank_start - 1) |
  5236. ((crtc_vblank_end - 1) << 16));
  5237. I915_WRITE(VSYNC(cpu_transcoder),
  5238. (adjusted_mode->crtc_vsync_start - 1) |
  5239. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5240. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5241. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5242. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5243. * bits. */
  5244. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5245. (pipe == PIPE_B || pipe == PIPE_C))
  5246. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5247. /* pipesrc controls the size that is scaled from, which should
  5248. * always be the user's requested size.
  5249. */
  5250. I915_WRITE(PIPESRC(pipe),
  5251. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  5252. (intel_crtc->config.pipe_src_h - 1));
  5253. }
  5254. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5255. struct intel_crtc_state *pipe_config)
  5256. {
  5257. struct drm_device *dev = crtc->base.dev;
  5258. struct drm_i915_private *dev_priv = dev->dev_private;
  5259. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5260. uint32_t tmp;
  5261. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5262. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5263. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5264. tmp = I915_READ(HBLANK(cpu_transcoder));
  5265. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5266. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5267. tmp = I915_READ(HSYNC(cpu_transcoder));
  5268. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5269. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5270. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5271. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5272. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5273. tmp = I915_READ(VBLANK(cpu_transcoder));
  5274. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5275. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5276. tmp = I915_READ(VSYNC(cpu_transcoder));
  5277. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5278. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5279. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5280. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5281. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5282. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5283. }
  5284. tmp = I915_READ(PIPESRC(crtc->pipe));
  5285. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5286. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5287. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5288. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5289. }
  5290. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5291. struct intel_crtc_state *pipe_config)
  5292. {
  5293. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5294. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5295. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5296. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5297. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5298. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5299. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5300. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5301. mode->flags = pipe_config->base.adjusted_mode.flags;
  5302. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5303. mode->flags |= pipe_config->base.adjusted_mode.flags;
  5304. }
  5305. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5306. {
  5307. struct drm_device *dev = intel_crtc->base.dev;
  5308. struct drm_i915_private *dev_priv = dev->dev_private;
  5309. uint32_t pipeconf;
  5310. pipeconf = 0;
  5311. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5312. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5313. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5314. if (intel_crtc->config.double_wide)
  5315. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5316. /* only g4x and later have fancy bpc/dither controls */
  5317. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5318. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5319. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5320. pipeconf |= PIPECONF_DITHER_EN |
  5321. PIPECONF_DITHER_TYPE_SP;
  5322. switch (intel_crtc->config.pipe_bpp) {
  5323. case 18:
  5324. pipeconf |= PIPECONF_6BPC;
  5325. break;
  5326. case 24:
  5327. pipeconf |= PIPECONF_8BPC;
  5328. break;
  5329. case 30:
  5330. pipeconf |= PIPECONF_10BPC;
  5331. break;
  5332. default:
  5333. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5334. BUG();
  5335. }
  5336. }
  5337. if (HAS_PIPE_CXSR(dev)) {
  5338. if (intel_crtc->lowfreq_avail) {
  5339. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5340. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5341. } else {
  5342. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5343. }
  5344. }
  5345. if (intel_crtc->config.base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5346. if (INTEL_INFO(dev)->gen < 4 ||
  5347. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5348. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5349. else
  5350. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5351. } else
  5352. pipeconf |= PIPECONF_PROGRESSIVE;
  5353. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5354. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5355. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5356. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5357. }
  5358. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc)
  5359. {
  5360. struct drm_device *dev = crtc->base.dev;
  5361. struct drm_i915_private *dev_priv = dev->dev_private;
  5362. int refclk, num_connectors = 0;
  5363. intel_clock_t clock, reduced_clock;
  5364. bool ok, has_reduced_clock = false;
  5365. bool is_lvds = false, is_dsi = false;
  5366. struct intel_encoder *encoder;
  5367. const intel_limit_t *limit;
  5368. for_each_intel_encoder(dev, encoder) {
  5369. if (encoder->new_crtc != crtc)
  5370. continue;
  5371. switch (encoder->type) {
  5372. case INTEL_OUTPUT_LVDS:
  5373. is_lvds = true;
  5374. break;
  5375. case INTEL_OUTPUT_DSI:
  5376. is_dsi = true;
  5377. break;
  5378. default:
  5379. break;
  5380. }
  5381. num_connectors++;
  5382. }
  5383. if (is_dsi)
  5384. return 0;
  5385. if (!crtc->new_config->clock_set) {
  5386. refclk = i9xx_get_refclk(crtc, num_connectors);
  5387. /*
  5388. * Returns a set of divisors for the desired target clock with
  5389. * the given refclk, or FALSE. The returned values represent
  5390. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5391. * 2) / p1 / p2.
  5392. */
  5393. limit = intel_limit(crtc, refclk);
  5394. ok = dev_priv->display.find_dpll(limit, crtc,
  5395. crtc->new_config->port_clock,
  5396. refclk, NULL, &clock);
  5397. if (!ok) {
  5398. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5399. return -EINVAL;
  5400. }
  5401. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5402. /*
  5403. * Ensure we match the reduced clock's P to the target
  5404. * clock. If the clocks don't match, we can't switch
  5405. * the display clock by using the FP0/FP1. In such case
  5406. * we will disable the LVDS downclock feature.
  5407. */
  5408. has_reduced_clock =
  5409. dev_priv->display.find_dpll(limit, crtc,
  5410. dev_priv->lvds_downclock,
  5411. refclk, &clock,
  5412. &reduced_clock);
  5413. }
  5414. /* Compat-code for transition, will disappear. */
  5415. crtc->new_config->dpll.n = clock.n;
  5416. crtc->new_config->dpll.m1 = clock.m1;
  5417. crtc->new_config->dpll.m2 = clock.m2;
  5418. crtc->new_config->dpll.p1 = clock.p1;
  5419. crtc->new_config->dpll.p2 = clock.p2;
  5420. }
  5421. if (IS_GEN2(dev)) {
  5422. i8xx_update_pll(crtc,
  5423. has_reduced_clock ? &reduced_clock : NULL,
  5424. num_connectors);
  5425. } else if (IS_CHERRYVIEW(dev)) {
  5426. chv_update_pll(crtc, crtc->new_config);
  5427. } else if (IS_VALLEYVIEW(dev)) {
  5428. vlv_update_pll(crtc, crtc->new_config);
  5429. } else {
  5430. i9xx_update_pll(crtc,
  5431. has_reduced_clock ? &reduced_clock : NULL,
  5432. num_connectors);
  5433. }
  5434. return 0;
  5435. }
  5436. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5437. struct intel_crtc_state *pipe_config)
  5438. {
  5439. struct drm_device *dev = crtc->base.dev;
  5440. struct drm_i915_private *dev_priv = dev->dev_private;
  5441. uint32_t tmp;
  5442. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5443. return;
  5444. tmp = I915_READ(PFIT_CONTROL);
  5445. if (!(tmp & PFIT_ENABLE))
  5446. return;
  5447. /* Check whether the pfit is attached to our pipe. */
  5448. if (INTEL_INFO(dev)->gen < 4) {
  5449. if (crtc->pipe != PIPE_B)
  5450. return;
  5451. } else {
  5452. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5453. return;
  5454. }
  5455. pipe_config->gmch_pfit.control = tmp;
  5456. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5457. if (INTEL_INFO(dev)->gen < 5)
  5458. pipe_config->gmch_pfit.lvds_border_bits =
  5459. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5460. }
  5461. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5462. struct intel_crtc_state *pipe_config)
  5463. {
  5464. struct drm_device *dev = crtc->base.dev;
  5465. struct drm_i915_private *dev_priv = dev->dev_private;
  5466. int pipe = pipe_config->cpu_transcoder;
  5467. intel_clock_t clock;
  5468. u32 mdiv;
  5469. int refclk = 100000;
  5470. /* In case of MIPI DPLL will not even be used */
  5471. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5472. return;
  5473. mutex_lock(&dev_priv->dpio_lock);
  5474. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5475. mutex_unlock(&dev_priv->dpio_lock);
  5476. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5477. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5478. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5479. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5480. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5481. vlv_clock(refclk, &clock);
  5482. /* clock.dot is the fast clock */
  5483. pipe_config->port_clock = clock.dot / 5;
  5484. }
  5485. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5486. struct intel_plane_config *plane_config)
  5487. {
  5488. struct drm_device *dev = crtc->base.dev;
  5489. struct drm_i915_private *dev_priv = dev->dev_private;
  5490. u32 val, base, offset;
  5491. int pipe = crtc->pipe, plane = crtc->plane;
  5492. int fourcc, pixel_format;
  5493. int aligned_height;
  5494. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5495. if (!crtc->base.primary->fb) {
  5496. DRM_DEBUG_KMS("failed to alloc fb\n");
  5497. return;
  5498. }
  5499. val = I915_READ(DSPCNTR(plane));
  5500. if (INTEL_INFO(dev)->gen >= 4)
  5501. if (val & DISPPLANE_TILED)
  5502. plane_config->tiled = true;
  5503. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5504. fourcc = intel_format_to_fourcc(pixel_format);
  5505. crtc->base.primary->fb->pixel_format = fourcc;
  5506. crtc->base.primary->fb->bits_per_pixel =
  5507. drm_format_plane_cpp(fourcc, 0) * 8;
  5508. if (INTEL_INFO(dev)->gen >= 4) {
  5509. if (plane_config->tiled)
  5510. offset = I915_READ(DSPTILEOFF(plane));
  5511. else
  5512. offset = I915_READ(DSPLINOFF(plane));
  5513. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5514. } else {
  5515. base = I915_READ(DSPADDR(plane));
  5516. }
  5517. plane_config->base = base;
  5518. val = I915_READ(PIPESRC(pipe));
  5519. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5520. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5521. val = I915_READ(DSPSTRIDE(pipe));
  5522. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  5523. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5524. plane_config->tiled);
  5525. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5526. aligned_height);
  5527. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5528. pipe, plane, crtc->base.primary->fb->width,
  5529. crtc->base.primary->fb->height,
  5530. crtc->base.primary->fb->bits_per_pixel, base,
  5531. crtc->base.primary->fb->pitches[0],
  5532. plane_config->size);
  5533. }
  5534. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5535. struct intel_crtc_state *pipe_config)
  5536. {
  5537. struct drm_device *dev = crtc->base.dev;
  5538. struct drm_i915_private *dev_priv = dev->dev_private;
  5539. int pipe = pipe_config->cpu_transcoder;
  5540. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5541. intel_clock_t clock;
  5542. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5543. int refclk = 100000;
  5544. mutex_lock(&dev_priv->dpio_lock);
  5545. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5546. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5547. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5548. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5549. mutex_unlock(&dev_priv->dpio_lock);
  5550. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5551. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5552. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5553. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5554. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5555. chv_clock(refclk, &clock);
  5556. /* clock.dot is the fast clock */
  5557. pipe_config->port_clock = clock.dot / 5;
  5558. }
  5559. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5560. struct intel_crtc_state *pipe_config)
  5561. {
  5562. struct drm_device *dev = crtc->base.dev;
  5563. struct drm_i915_private *dev_priv = dev->dev_private;
  5564. uint32_t tmp;
  5565. if (!intel_display_power_is_enabled(dev_priv,
  5566. POWER_DOMAIN_PIPE(crtc->pipe)))
  5567. return false;
  5568. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5569. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5570. tmp = I915_READ(PIPECONF(crtc->pipe));
  5571. if (!(tmp & PIPECONF_ENABLE))
  5572. return false;
  5573. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5574. switch (tmp & PIPECONF_BPC_MASK) {
  5575. case PIPECONF_6BPC:
  5576. pipe_config->pipe_bpp = 18;
  5577. break;
  5578. case PIPECONF_8BPC:
  5579. pipe_config->pipe_bpp = 24;
  5580. break;
  5581. case PIPECONF_10BPC:
  5582. pipe_config->pipe_bpp = 30;
  5583. break;
  5584. default:
  5585. break;
  5586. }
  5587. }
  5588. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5589. pipe_config->limited_color_range = true;
  5590. if (INTEL_INFO(dev)->gen < 4)
  5591. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5592. intel_get_pipe_timings(crtc, pipe_config);
  5593. i9xx_get_pfit_config(crtc, pipe_config);
  5594. if (INTEL_INFO(dev)->gen >= 4) {
  5595. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5596. pipe_config->pixel_multiplier =
  5597. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5598. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5599. pipe_config->dpll_hw_state.dpll_md = tmp;
  5600. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5601. tmp = I915_READ(DPLL(crtc->pipe));
  5602. pipe_config->pixel_multiplier =
  5603. ((tmp & SDVO_MULTIPLIER_MASK)
  5604. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5605. } else {
  5606. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5607. * port and will be fixed up in the encoder->get_config
  5608. * function. */
  5609. pipe_config->pixel_multiplier = 1;
  5610. }
  5611. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5612. if (!IS_VALLEYVIEW(dev)) {
  5613. /*
  5614. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5615. * on 830. Filter it out here so that we don't
  5616. * report errors due to that.
  5617. */
  5618. if (IS_I830(dev))
  5619. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5620. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5621. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5622. } else {
  5623. /* Mask out read-only status bits. */
  5624. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5625. DPLL_PORTC_READY_MASK |
  5626. DPLL_PORTB_READY_MASK);
  5627. }
  5628. if (IS_CHERRYVIEW(dev))
  5629. chv_crtc_clock_get(crtc, pipe_config);
  5630. else if (IS_VALLEYVIEW(dev))
  5631. vlv_crtc_clock_get(crtc, pipe_config);
  5632. else
  5633. i9xx_crtc_clock_get(crtc, pipe_config);
  5634. return true;
  5635. }
  5636. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5637. {
  5638. struct drm_i915_private *dev_priv = dev->dev_private;
  5639. struct intel_encoder *encoder;
  5640. u32 val, final;
  5641. bool has_lvds = false;
  5642. bool has_cpu_edp = false;
  5643. bool has_panel = false;
  5644. bool has_ck505 = false;
  5645. bool can_ssc = false;
  5646. /* We need to take the global config into account */
  5647. for_each_intel_encoder(dev, encoder) {
  5648. switch (encoder->type) {
  5649. case INTEL_OUTPUT_LVDS:
  5650. has_panel = true;
  5651. has_lvds = true;
  5652. break;
  5653. case INTEL_OUTPUT_EDP:
  5654. has_panel = true;
  5655. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5656. has_cpu_edp = true;
  5657. break;
  5658. default:
  5659. break;
  5660. }
  5661. }
  5662. if (HAS_PCH_IBX(dev)) {
  5663. has_ck505 = dev_priv->vbt.display_clock_mode;
  5664. can_ssc = has_ck505;
  5665. } else {
  5666. has_ck505 = false;
  5667. can_ssc = true;
  5668. }
  5669. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5670. has_panel, has_lvds, has_ck505);
  5671. /* Ironlake: try to setup display ref clock before DPLL
  5672. * enabling. This is only under driver's control after
  5673. * PCH B stepping, previous chipset stepping should be
  5674. * ignoring this setting.
  5675. */
  5676. val = I915_READ(PCH_DREF_CONTROL);
  5677. /* As we must carefully and slowly disable/enable each source in turn,
  5678. * compute the final state we want first and check if we need to
  5679. * make any changes at all.
  5680. */
  5681. final = val;
  5682. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5683. if (has_ck505)
  5684. final |= DREF_NONSPREAD_CK505_ENABLE;
  5685. else
  5686. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5687. final &= ~DREF_SSC_SOURCE_MASK;
  5688. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5689. final &= ~DREF_SSC1_ENABLE;
  5690. if (has_panel) {
  5691. final |= DREF_SSC_SOURCE_ENABLE;
  5692. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5693. final |= DREF_SSC1_ENABLE;
  5694. if (has_cpu_edp) {
  5695. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5696. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5697. else
  5698. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5699. } else
  5700. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5701. } else {
  5702. final |= DREF_SSC_SOURCE_DISABLE;
  5703. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5704. }
  5705. if (final == val)
  5706. return;
  5707. /* Always enable nonspread source */
  5708. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5709. if (has_ck505)
  5710. val |= DREF_NONSPREAD_CK505_ENABLE;
  5711. else
  5712. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5713. if (has_panel) {
  5714. val &= ~DREF_SSC_SOURCE_MASK;
  5715. val |= DREF_SSC_SOURCE_ENABLE;
  5716. /* SSC must be turned on before enabling the CPU output */
  5717. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5718. DRM_DEBUG_KMS("Using SSC on panel\n");
  5719. val |= DREF_SSC1_ENABLE;
  5720. } else
  5721. val &= ~DREF_SSC1_ENABLE;
  5722. /* Get SSC going before enabling the outputs */
  5723. I915_WRITE(PCH_DREF_CONTROL, val);
  5724. POSTING_READ(PCH_DREF_CONTROL);
  5725. udelay(200);
  5726. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5727. /* Enable CPU source on CPU attached eDP */
  5728. if (has_cpu_edp) {
  5729. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5730. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5731. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5732. } else
  5733. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5734. } else
  5735. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5736. I915_WRITE(PCH_DREF_CONTROL, val);
  5737. POSTING_READ(PCH_DREF_CONTROL);
  5738. udelay(200);
  5739. } else {
  5740. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5741. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5742. /* Turn off CPU output */
  5743. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5744. I915_WRITE(PCH_DREF_CONTROL, val);
  5745. POSTING_READ(PCH_DREF_CONTROL);
  5746. udelay(200);
  5747. /* Turn off the SSC source */
  5748. val &= ~DREF_SSC_SOURCE_MASK;
  5749. val |= DREF_SSC_SOURCE_DISABLE;
  5750. /* Turn off SSC1 */
  5751. val &= ~DREF_SSC1_ENABLE;
  5752. I915_WRITE(PCH_DREF_CONTROL, val);
  5753. POSTING_READ(PCH_DREF_CONTROL);
  5754. udelay(200);
  5755. }
  5756. BUG_ON(val != final);
  5757. }
  5758. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5759. {
  5760. uint32_t tmp;
  5761. tmp = I915_READ(SOUTH_CHICKEN2);
  5762. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5763. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5764. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5765. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5766. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5767. tmp = I915_READ(SOUTH_CHICKEN2);
  5768. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5769. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5770. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5771. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5772. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5773. }
  5774. /* WaMPhyProgramming:hsw */
  5775. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5776. {
  5777. uint32_t tmp;
  5778. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5779. tmp &= ~(0xFF << 24);
  5780. tmp |= (0x12 << 24);
  5781. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5782. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5783. tmp |= (1 << 11);
  5784. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5785. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5786. tmp |= (1 << 11);
  5787. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5788. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5789. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5790. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5791. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5792. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5793. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5794. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5795. tmp &= ~(7 << 13);
  5796. tmp |= (5 << 13);
  5797. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5798. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5799. tmp &= ~(7 << 13);
  5800. tmp |= (5 << 13);
  5801. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5802. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5803. tmp &= ~0xFF;
  5804. tmp |= 0x1C;
  5805. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5806. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5807. tmp &= ~0xFF;
  5808. tmp |= 0x1C;
  5809. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5810. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5811. tmp &= ~(0xFF << 16);
  5812. tmp |= (0x1C << 16);
  5813. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5814. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5815. tmp &= ~(0xFF << 16);
  5816. tmp |= (0x1C << 16);
  5817. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5818. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5819. tmp |= (1 << 27);
  5820. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5821. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5822. tmp |= (1 << 27);
  5823. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5824. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5825. tmp &= ~(0xF << 28);
  5826. tmp |= (4 << 28);
  5827. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5828. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5829. tmp &= ~(0xF << 28);
  5830. tmp |= (4 << 28);
  5831. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5832. }
  5833. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5834. * Programming" based on the parameters passed:
  5835. * - Sequence to enable CLKOUT_DP
  5836. * - Sequence to enable CLKOUT_DP without spread
  5837. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5838. */
  5839. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5840. bool with_fdi)
  5841. {
  5842. struct drm_i915_private *dev_priv = dev->dev_private;
  5843. uint32_t reg, tmp;
  5844. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5845. with_spread = true;
  5846. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5847. with_fdi, "LP PCH doesn't have FDI\n"))
  5848. with_fdi = false;
  5849. mutex_lock(&dev_priv->dpio_lock);
  5850. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5851. tmp &= ~SBI_SSCCTL_DISABLE;
  5852. tmp |= SBI_SSCCTL_PATHALT;
  5853. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5854. udelay(24);
  5855. if (with_spread) {
  5856. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5857. tmp &= ~SBI_SSCCTL_PATHALT;
  5858. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5859. if (with_fdi) {
  5860. lpt_reset_fdi_mphy(dev_priv);
  5861. lpt_program_fdi_mphy(dev_priv);
  5862. }
  5863. }
  5864. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5865. SBI_GEN0 : SBI_DBUFF0;
  5866. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5867. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5868. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5869. mutex_unlock(&dev_priv->dpio_lock);
  5870. }
  5871. /* Sequence to disable CLKOUT_DP */
  5872. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5873. {
  5874. struct drm_i915_private *dev_priv = dev->dev_private;
  5875. uint32_t reg, tmp;
  5876. mutex_lock(&dev_priv->dpio_lock);
  5877. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5878. SBI_GEN0 : SBI_DBUFF0;
  5879. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5880. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5881. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5882. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5883. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5884. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5885. tmp |= SBI_SSCCTL_PATHALT;
  5886. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5887. udelay(32);
  5888. }
  5889. tmp |= SBI_SSCCTL_DISABLE;
  5890. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5891. }
  5892. mutex_unlock(&dev_priv->dpio_lock);
  5893. }
  5894. static void lpt_init_pch_refclk(struct drm_device *dev)
  5895. {
  5896. struct intel_encoder *encoder;
  5897. bool has_vga = false;
  5898. for_each_intel_encoder(dev, encoder) {
  5899. switch (encoder->type) {
  5900. case INTEL_OUTPUT_ANALOG:
  5901. has_vga = true;
  5902. break;
  5903. default:
  5904. break;
  5905. }
  5906. }
  5907. if (has_vga)
  5908. lpt_enable_clkout_dp(dev, true, true);
  5909. else
  5910. lpt_disable_clkout_dp(dev);
  5911. }
  5912. /*
  5913. * Initialize reference clocks when the driver loads
  5914. */
  5915. void intel_init_pch_refclk(struct drm_device *dev)
  5916. {
  5917. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5918. ironlake_init_pch_refclk(dev);
  5919. else if (HAS_PCH_LPT(dev))
  5920. lpt_init_pch_refclk(dev);
  5921. }
  5922. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5923. {
  5924. struct drm_device *dev = crtc->dev;
  5925. struct drm_i915_private *dev_priv = dev->dev_private;
  5926. struct intel_encoder *encoder;
  5927. int num_connectors = 0;
  5928. bool is_lvds = false;
  5929. for_each_intel_encoder(dev, encoder) {
  5930. if (encoder->new_crtc != to_intel_crtc(crtc))
  5931. continue;
  5932. switch (encoder->type) {
  5933. case INTEL_OUTPUT_LVDS:
  5934. is_lvds = true;
  5935. break;
  5936. default:
  5937. break;
  5938. }
  5939. num_connectors++;
  5940. }
  5941. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5942. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5943. dev_priv->vbt.lvds_ssc_freq);
  5944. return dev_priv->vbt.lvds_ssc_freq;
  5945. }
  5946. return 120000;
  5947. }
  5948. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5949. {
  5950. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5951. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5952. int pipe = intel_crtc->pipe;
  5953. uint32_t val;
  5954. val = 0;
  5955. switch (intel_crtc->config.pipe_bpp) {
  5956. case 18:
  5957. val |= PIPECONF_6BPC;
  5958. break;
  5959. case 24:
  5960. val |= PIPECONF_8BPC;
  5961. break;
  5962. case 30:
  5963. val |= PIPECONF_10BPC;
  5964. break;
  5965. case 36:
  5966. val |= PIPECONF_12BPC;
  5967. break;
  5968. default:
  5969. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5970. BUG();
  5971. }
  5972. if (intel_crtc->config.dither)
  5973. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5974. if (intel_crtc->config.base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5975. val |= PIPECONF_INTERLACED_ILK;
  5976. else
  5977. val |= PIPECONF_PROGRESSIVE;
  5978. if (intel_crtc->config.limited_color_range)
  5979. val |= PIPECONF_COLOR_RANGE_SELECT;
  5980. I915_WRITE(PIPECONF(pipe), val);
  5981. POSTING_READ(PIPECONF(pipe));
  5982. }
  5983. /*
  5984. * Set up the pipe CSC unit.
  5985. *
  5986. * Currently only full range RGB to limited range RGB conversion
  5987. * is supported, but eventually this should handle various
  5988. * RGB<->YCbCr scenarios as well.
  5989. */
  5990. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5991. {
  5992. struct drm_device *dev = crtc->dev;
  5993. struct drm_i915_private *dev_priv = dev->dev_private;
  5994. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5995. int pipe = intel_crtc->pipe;
  5996. uint16_t coeff = 0x7800; /* 1.0 */
  5997. /*
  5998. * TODO: Check what kind of values actually come out of the pipe
  5999. * with these coeff/postoff values and adjust to get the best
  6000. * accuracy. Perhaps we even need to take the bpc value into
  6001. * consideration.
  6002. */
  6003. if (intel_crtc->config.limited_color_range)
  6004. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6005. /*
  6006. * GY/GU and RY/RU should be the other way around according
  6007. * to BSpec, but reality doesn't agree. Just set them up in
  6008. * a way that results in the correct picture.
  6009. */
  6010. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6011. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6012. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6013. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6014. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6015. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6016. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6017. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6018. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6019. if (INTEL_INFO(dev)->gen > 6) {
  6020. uint16_t postoff = 0;
  6021. if (intel_crtc->config.limited_color_range)
  6022. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6023. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6024. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6025. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6026. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6027. } else {
  6028. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6029. if (intel_crtc->config.limited_color_range)
  6030. mode |= CSC_BLACK_SCREEN_OFFSET;
  6031. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6032. }
  6033. }
  6034. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6035. {
  6036. struct drm_device *dev = crtc->dev;
  6037. struct drm_i915_private *dev_priv = dev->dev_private;
  6038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6039. enum pipe pipe = intel_crtc->pipe;
  6040. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  6041. uint32_t val;
  6042. val = 0;
  6043. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  6044. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6045. if (intel_crtc->config.base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6046. val |= PIPECONF_INTERLACED_ILK;
  6047. else
  6048. val |= PIPECONF_PROGRESSIVE;
  6049. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6050. POSTING_READ(PIPECONF(cpu_transcoder));
  6051. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6052. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6053. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6054. val = 0;
  6055. switch (intel_crtc->config.pipe_bpp) {
  6056. case 18:
  6057. val |= PIPEMISC_DITHER_6_BPC;
  6058. break;
  6059. case 24:
  6060. val |= PIPEMISC_DITHER_8_BPC;
  6061. break;
  6062. case 30:
  6063. val |= PIPEMISC_DITHER_10_BPC;
  6064. break;
  6065. case 36:
  6066. val |= PIPEMISC_DITHER_12_BPC;
  6067. break;
  6068. default:
  6069. /* Case prevented by pipe_config_set_bpp. */
  6070. BUG();
  6071. }
  6072. if (intel_crtc->config.dither)
  6073. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6074. I915_WRITE(PIPEMISC(pipe), val);
  6075. }
  6076. }
  6077. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6078. intel_clock_t *clock,
  6079. bool *has_reduced_clock,
  6080. intel_clock_t *reduced_clock)
  6081. {
  6082. struct drm_device *dev = crtc->dev;
  6083. struct drm_i915_private *dev_priv = dev->dev_private;
  6084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6085. int refclk;
  6086. const intel_limit_t *limit;
  6087. bool ret, is_lvds = false;
  6088. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6089. refclk = ironlake_get_refclk(crtc);
  6090. /*
  6091. * Returns a set of divisors for the desired target clock with the given
  6092. * refclk, or FALSE. The returned values represent the clock equation:
  6093. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6094. */
  6095. limit = intel_limit(intel_crtc, refclk);
  6096. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6097. intel_crtc->new_config->port_clock,
  6098. refclk, NULL, clock);
  6099. if (!ret)
  6100. return false;
  6101. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6102. /*
  6103. * Ensure we match the reduced clock's P to the target clock.
  6104. * If the clocks don't match, we can't switch the display clock
  6105. * by using the FP0/FP1. In such case we will disable the LVDS
  6106. * downclock feature.
  6107. */
  6108. *has_reduced_clock =
  6109. dev_priv->display.find_dpll(limit, intel_crtc,
  6110. dev_priv->lvds_downclock,
  6111. refclk, clock,
  6112. reduced_clock);
  6113. }
  6114. return true;
  6115. }
  6116. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6117. {
  6118. /*
  6119. * Account for spread spectrum to avoid
  6120. * oversubscribing the link. Max center spread
  6121. * is 2.5%; use 5% for safety's sake.
  6122. */
  6123. u32 bps = target_clock * bpp * 21 / 20;
  6124. return DIV_ROUND_UP(bps, link_bw * 8);
  6125. }
  6126. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6127. {
  6128. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6129. }
  6130. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6131. u32 *fp,
  6132. intel_clock_t *reduced_clock, u32 *fp2)
  6133. {
  6134. struct drm_crtc *crtc = &intel_crtc->base;
  6135. struct drm_device *dev = crtc->dev;
  6136. struct drm_i915_private *dev_priv = dev->dev_private;
  6137. struct intel_encoder *intel_encoder;
  6138. uint32_t dpll;
  6139. int factor, num_connectors = 0;
  6140. bool is_lvds = false, is_sdvo = false;
  6141. for_each_intel_encoder(dev, intel_encoder) {
  6142. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6143. continue;
  6144. switch (intel_encoder->type) {
  6145. case INTEL_OUTPUT_LVDS:
  6146. is_lvds = true;
  6147. break;
  6148. case INTEL_OUTPUT_SDVO:
  6149. case INTEL_OUTPUT_HDMI:
  6150. is_sdvo = true;
  6151. break;
  6152. default:
  6153. break;
  6154. }
  6155. num_connectors++;
  6156. }
  6157. /* Enable autotuning of the PLL clock (if permissible) */
  6158. factor = 21;
  6159. if (is_lvds) {
  6160. if ((intel_panel_use_ssc(dev_priv) &&
  6161. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6162. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6163. factor = 25;
  6164. } else if (intel_crtc->new_config->sdvo_tv_clock)
  6165. factor = 20;
  6166. if (ironlake_needs_fb_cb_tune(&intel_crtc->new_config->dpll, factor))
  6167. *fp |= FP_CB_TUNE;
  6168. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6169. *fp2 |= FP_CB_TUNE;
  6170. dpll = 0;
  6171. if (is_lvds)
  6172. dpll |= DPLLB_MODE_LVDS;
  6173. else
  6174. dpll |= DPLLB_MODE_DAC_SERIAL;
  6175. dpll |= (intel_crtc->new_config->pixel_multiplier - 1)
  6176. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6177. if (is_sdvo)
  6178. dpll |= DPLL_SDVO_HIGH_SPEED;
  6179. if (intel_crtc->new_config->has_dp_encoder)
  6180. dpll |= DPLL_SDVO_HIGH_SPEED;
  6181. /* compute bitmask from p1 value */
  6182. dpll |= (1 << (intel_crtc->new_config->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6183. /* also FPA1 */
  6184. dpll |= (1 << (intel_crtc->new_config->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6185. switch (intel_crtc->new_config->dpll.p2) {
  6186. case 5:
  6187. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6188. break;
  6189. case 7:
  6190. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6191. break;
  6192. case 10:
  6193. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6194. break;
  6195. case 14:
  6196. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6197. break;
  6198. }
  6199. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6200. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6201. else
  6202. dpll |= PLL_REF_INPUT_DREFCLK;
  6203. return dpll | DPLL_VCO_ENABLE;
  6204. }
  6205. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc)
  6206. {
  6207. struct drm_device *dev = crtc->base.dev;
  6208. intel_clock_t clock, reduced_clock;
  6209. u32 dpll = 0, fp = 0, fp2 = 0;
  6210. bool ok, has_reduced_clock = false;
  6211. bool is_lvds = false;
  6212. struct intel_shared_dpll *pll;
  6213. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6214. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6215. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6216. ok = ironlake_compute_clocks(&crtc->base, &clock,
  6217. &has_reduced_clock, &reduced_clock);
  6218. if (!ok && !crtc->new_config->clock_set) {
  6219. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6220. return -EINVAL;
  6221. }
  6222. /* Compat-code for transition, will disappear. */
  6223. if (!crtc->new_config->clock_set) {
  6224. crtc->new_config->dpll.n = clock.n;
  6225. crtc->new_config->dpll.m1 = clock.m1;
  6226. crtc->new_config->dpll.m2 = clock.m2;
  6227. crtc->new_config->dpll.p1 = clock.p1;
  6228. crtc->new_config->dpll.p2 = clock.p2;
  6229. }
  6230. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6231. if (crtc->new_config->has_pch_encoder) {
  6232. fp = i9xx_dpll_compute_fp(&crtc->new_config->dpll);
  6233. if (has_reduced_clock)
  6234. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6235. dpll = ironlake_compute_dpll(crtc,
  6236. &fp, &reduced_clock,
  6237. has_reduced_clock ? &fp2 : NULL);
  6238. crtc->new_config->dpll_hw_state.dpll = dpll;
  6239. crtc->new_config->dpll_hw_state.fp0 = fp;
  6240. if (has_reduced_clock)
  6241. crtc->new_config->dpll_hw_state.fp1 = fp2;
  6242. else
  6243. crtc->new_config->dpll_hw_state.fp1 = fp;
  6244. pll = intel_get_shared_dpll(crtc);
  6245. if (pll == NULL) {
  6246. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6247. pipe_name(crtc->pipe));
  6248. return -EINVAL;
  6249. }
  6250. }
  6251. if (is_lvds && has_reduced_clock && i915.powersave)
  6252. crtc->lowfreq_avail = true;
  6253. else
  6254. crtc->lowfreq_avail = false;
  6255. return 0;
  6256. }
  6257. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6258. struct intel_link_m_n *m_n)
  6259. {
  6260. struct drm_device *dev = crtc->base.dev;
  6261. struct drm_i915_private *dev_priv = dev->dev_private;
  6262. enum pipe pipe = crtc->pipe;
  6263. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6264. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6265. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6266. & ~TU_SIZE_MASK;
  6267. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6268. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6269. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6270. }
  6271. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6272. enum transcoder transcoder,
  6273. struct intel_link_m_n *m_n,
  6274. struct intel_link_m_n *m2_n2)
  6275. {
  6276. struct drm_device *dev = crtc->base.dev;
  6277. struct drm_i915_private *dev_priv = dev->dev_private;
  6278. enum pipe pipe = crtc->pipe;
  6279. if (INTEL_INFO(dev)->gen >= 5) {
  6280. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6281. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6282. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6283. & ~TU_SIZE_MASK;
  6284. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6285. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6286. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6287. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6288. * gen < 8) and if DRRS is supported (to make sure the
  6289. * registers are not unnecessarily read).
  6290. */
  6291. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6292. crtc->config.has_drrs) {
  6293. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6294. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6295. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6296. & ~TU_SIZE_MASK;
  6297. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6298. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6299. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6300. }
  6301. } else {
  6302. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6303. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6304. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6305. & ~TU_SIZE_MASK;
  6306. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6307. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6308. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6309. }
  6310. }
  6311. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6312. struct intel_crtc_state *pipe_config)
  6313. {
  6314. if (crtc->config.has_pch_encoder)
  6315. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6316. else
  6317. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6318. &pipe_config->dp_m_n,
  6319. &pipe_config->dp_m2_n2);
  6320. }
  6321. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6322. struct intel_crtc_state *pipe_config)
  6323. {
  6324. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6325. &pipe_config->fdi_m_n, NULL);
  6326. }
  6327. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6328. struct intel_crtc_state *pipe_config)
  6329. {
  6330. struct drm_device *dev = crtc->base.dev;
  6331. struct drm_i915_private *dev_priv = dev->dev_private;
  6332. uint32_t tmp;
  6333. tmp = I915_READ(PS_CTL(crtc->pipe));
  6334. if (tmp & PS_ENABLE) {
  6335. pipe_config->pch_pfit.enabled = true;
  6336. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6337. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6338. }
  6339. }
  6340. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6341. struct intel_crtc_state *pipe_config)
  6342. {
  6343. struct drm_device *dev = crtc->base.dev;
  6344. struct drm_i915_private *dev_priv = dev->dev_private;
  6345. uint32_t tmp;
  6346. tmp = I915_READ(PF_CTL(crtc->pipe));
  6347. if (tmp & PF_ENABLE) {
  6348. pipe_config->pch_pfit.enabled = true;
  6349. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6350. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6351. /* We currently do not free assignements of panel fitters on
  6352. * ivb/hsw (since we don't use the higher upscaling modes which
  6353. * differentiates them) so just WARN about this case for now. */
  6354. if (IS_GEN7(dev)) {
  6355. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6356. PF_PIPE_SEL_IVB(crtc->pipe));
  6357. }
  6358. }
  6359. }
  6360. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6361. struct intel_plane_config *plane_config)
  6362. {
  6363. struct drm_device *dev = crtc->base.dev;
  6364. struct drm_i915_private *dev_priv = dev->dev_private;
  6365. u32 val, base, offset;
  6366. int pipe = crtc->pipe, plane = crtc->plane;
  6367. int fourcc, pixel_format;
  6368. int aligned_height;
  6369. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6370. if (!crtc->base.primary->fb) {
  6371. DRM_DEBUG_KMS("failed to alloc fb\n");
  6372. return;
  6373. }
  6374. val = I915_READ(DSPCNTR(plane));
  6375. if (INTEL_INFO(dev)->gen >= 4)
  6376. if (val & DISPPLANE_TILED)
  6377. plane_config->tiled = true;
  6378. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6379. fourcc = intel_format_to_fourcc(pixel_format);
  6380. crtc->base.primary->fb->pixel_format = fourcc;
  6381. crtc->base.primary->fb->bits_per_pixel =
  6382. drm_format_plane_cpp(fourcc, 0) * 8;
  6383. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6384. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6385. offset = I915_READ(DSPOFFSET(plane));
  6386. } else {
  6387. if (plane_config->tiled)
  6388. offset = I915_READ(DSPTILEOFF(plane));
  6389. else
  6390. offset = I915_READ(DSPLINOFF(plane));
  6391. }
  6392. plane_config->base = base;
  6393. val = I915_READ(PIPESRC(pipe));
  6394. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6395. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6396. val = I915_READ(DSPSTRIDE(pipe));
  6397. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  6398. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6399. plane_config->tiled);
  6400. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6401. aligned_height);
  6402. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6403. pipe, plane, crtc->base.primary->fb->width,
  6404. crtc->base.primary->fb->height,
  6405. crtc->base.primary->fb->bits_per_pixel, base,
  6406. crtc->base.primary->fb->pitches[0],
  6407. plane_config->size);
  6408. }
  6409. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6410. struct intel_crtc_state *pipe_config)
  6411. {
  6412. struct drm_device *dev = crtc->base.dev;
  6413. struct drm_i915_private *dev_priv = dev->dev_private;
  6414. uint32_t tmp;
  6415. if (!intel_display_power_is_enabled(dev_priv,
  6416. POWER_DOMAIN_PIPE(crtc->pipe)))
  6417. return false;
  6418. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6419. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6420. tmp = I915_READ(PIPECONF(crtc->pipe));
  6421. if (!(tmp & PIPECONF_ENABLE))
  6422. return false;
  6423. switch (tmp & PIPECONF_BPC_MASK) {
  6424. case PIPECONF_6BPC:
  6425. pipe_config->pipe_bpp = 18;
  6426. break;
  6427. case PIPECONF_8BPC:
  6428. pipe_config->pipe_bpp = 24;
  6429. break;
  6430. case PIPECONF_10BPC:
  6431. pipe_config->pipe_bpp = 30;
  6432. break;
  6433. case PIPECONF_12BPC:
  6434. pipe_config->pipe_bpp = 36;
  6435. break;
  6436. default:
  6437. break;
  6438. }
  6439. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6440. pipe_config->limited_color_range = true;
  6441. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6442. struct intel_shared_dpll *pll;
  6443. pipe_config->has_pch_encoder = true;
  6444. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6445. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6446. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6447. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6448. if (HAS_PCH_IBX(dev_priv->dev)) {
  6449. pipe_config->shared_dpll =
  6450. (enum intel_dpll_id) crtc->pipe;
  6451. } else {
  6452. tmp = I915_READ(PCH_DPLL_SEL);
  6453. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6454. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6455. else
  6456. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6457. }
  6458. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6459. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6460. &pipe_config->dpll_hw_state));
  6461. tmp = pipe_config->dpll_hw_state.dpll;
  6462. pipe_config->pixel_multiplier =
  6463. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6464. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6465. ironlake_pch_clock_get(crtc, pipe_config);
  6466. } else {
  6467. pipe_config->pixel_multiplier = 1;
  6468. }
  6469. intel_get_pipe_timings(crtc, pipe_config);
  6470. ironlake_get_pfit_config(crtc, pipe_config);
  6471. return true;
  6472. }
  6473. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6474. {
  6475. struct drm_device *dev = dev_priv->dev;
  6476. struct intel_crtc *crtc;
  6477. for_each_intel_crtc(dev, crtc)
  6478. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6479. pipe_name(crtc->pipe));
  6480. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6481. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6482. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6483. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6484. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6485. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6486. "CPU PWM1 enabled\n");
  6487. if (IS_HASWELL(dev))
  6488. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6489. "CPU PWM2 enabled\n");
  6490. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6491. "PCH PWM1 enabled\n");
  6492. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6493. "Utility pin enabled\n");
  6494. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6495. /*
  6496. * In theory we can still leave IRQs enabled, as long as only the HPD
  6497. * interrupts remain enabled. We used to check for that, but since it's
  6498. * gen-specific and since we only disable LCPLL after we fully disable
  6499. * the interrupts, the check below should be enough.
  6500. */
  6501. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6502. }
  6503. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6504. {
  6505. struct drm_device *dev = dev_priv->dev;
  6506. if (IS_HASWELL(dev))
  6507. return I915_READ(D_COMP_HSW);
  6508. else
  6509. return I915_READ(D_COMP_BDW);
  6510. }
  6511. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6512. {
  6513. struct drm_device *dev = dev_priv->dev;
  6514. if (IS_HASWELL(dev)) {
  6515. mutex_lock(&dev_priv->rps.hw_lock);
  6516. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6517. val))
  6518. DRM_ERROR("Failed to write to D_COMP\n");
  6519. mutex_unlock(&dev_priv->rps.hw_lock);
  6520. } else {
  6521. I915_WRITE(D_COMP_BDW, val);
  6522. POSTING_READ(D_COMP_BDW);
  6523. }
  6524. }
  6525. /*
  6526. * This function implements pieces of two sequences from BSpec:
  6527. * - Sequence for display software to disable LCPLL
  6528. * - Sequence for display software to allow package C8+
  6529. * The steps implemented here are just the steps that actually touch the LCPLL
  6530. * register. Callers should take care of disabling all the display engine
  6531. * functions, doing the mode unset, fixing interrupts, etc.
  6532. */
  6533. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6534. bool switch_to_fclk, bool allow_power_down)
  6535. {
  6536. uint32_t val;
  6537. assert_can_disable_lcpll(dev_priv);
  6538. val = I915_READ(LCPLL_CTL);
  6539. if (switch_to_fclk) {
  6540. val |= LCPLL_CD_SOURCE_FCLK;
  6541. I915_WRITE(LCPLL_CTL, val);
  6542. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6543. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6544. DRM_ERROR("Switching to FCLK failed\n");
  6545. val = I915_READ(LCPLL_CTL);
  6546. }
  6547. val |= LCPLL_PLL_DISABLE;
  6548. I915_WRITE(LCPLL_CTL, val);
  6549. POSTING_READ(LCPLL_CTL);
  6550. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6551. DRM_ERROR("LCPLL still locked\n");
  6552. val = hsw_read_dcomp(dev_priv);
  6553. val |= D_COMP_COMP_DISABLE;
  6554. hsw_write_dcomp(dev_priv, val);
  6555. ndelay(100);
  6556. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6557. 1))
  6558. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6559. if (allow_power_down) {
  6560. val = I915_READ(LCPLL_CTL);
  6561. val |= LCPLL_POWER_DOWN_ALLOW;
  6562. I915_WRITE(LCPLL_CTL, val);
  6563. POSTING_READ(LCPLL_CTL);
  6564. }
  6565. }
  6566. /*
  6567. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6568. * source.
  6569. */
  6570. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6571. {
  6572. uint32_t val;
  6573. val = I915_READ(LCPLL_CTL);
  6574. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6575. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6576. return;
  6577. /*
  6578. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6579. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6580. *
  6581. * The other problem is that hsw_restore_lcpll() is called as part of
  6582. * the runtime PM resume sequence, so we can't just call
  6583. * gen6_gt_force_wake_get() because that function calls
  6584. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6585. * while we are on the resume sequence. So to solve this problem we have
  6586. * to call special forcewake code that doesn't touch runtime PM and
  6587. * doesn't enable the forcewake delayed work.
  6588. */
  6589. spin_lock_irq(&dev_priv->uncore.lock);
  6590. if (dev_priv->uncore.forcewake_count++ == 0)
  6591. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6592. spin_unlock_irq(&dev_priv->uncore.lock);
  6593. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6594. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6595. I915_WRITE(LCPLL_CTL, val);
  6596. POSTING_READ(LCPLL_CTL);
  6597. }
  6598. val = hsw_read_dcomp(dev_priv);
  6599. val |= D_COMP_COMP_FORCE;
  6600. val &= ~D_COMP_COMP_DISABLE;
  6601. hsw_write_dcomp(dev_priv, val);
  6602. val = I915_READ(LCPLL_CTL);
  6603. val &= ~LCPLL_PLL_DISABLE;
  6604. I915_WRITE(LCPLL_CTL, val);
  6605. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6606. DRM_ERROR("LCPLL not locked yet\n");
  6607. if (val & LCPLL_CD_SOURCE_FCLK) {
  6608. val = I915_READ(LCPLL_CTL);
  6609. val &= ~LCPLL_CD_SOURCE_FCLK;
  6610. I915_WRITE(LCPLL_CTL, val);
  6611. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6612. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6613. DRM_ERROR("Switching back to LCPLL failed\n");
  6614. }
  6615. /* See the big comment above. */
  6616. spin_lock_irq(&dev_priv->uncore.lock);
  6617. if (--dev_priv->uncore.forcewake_count == 0)
  6618. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6619. spin_unlock_irq(&dev_priv->uncore.lock);
  6620. }
  6621. /*
  6622. * Package states C8 and deeper are really deep PC states that can only be
  6623. * reached when all the devices on the system allow it, so even if the graphics
  6624. * device allows PC8+, it doesn't mean the system will actually get to these
  6625. * states. Our driver only allows PC8+ when going into runtime PM.
  6626. *
  6627. * The requirements for PC8+ are that all the outputs are disabled, the power
  6628. * well is disabled and most interrupts are disabled, and these are also
  6629. * requirements for runtime PM. When these conditions are met, we manually do
  6630. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6631. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6632. * hang the machine.
  6633. *
  6634. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6635. * the state of some registers, so when we come back from PC8+ we need to
  6636. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6637. * need to take care of the registers kept by RC6. Notice that this happens even
  6638. * if we don't put the device in PCI D3 state (which is what currently happens
  6639. * because of the runtime PM support).
  6640. *
  6641. * For more, read "Display Sequences for Package C8" on the hardware
  6642. * documentation.
  6643. */
  6644. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6645. {
  6646. struct drm_device *dev = dev_priv->dev;
  6647. uint32_t val;
  6648. DRM_DEBUG_KMS("Enabling package C8+\n");
  6649. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6650. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6651. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6652. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6653. }
  6654. lpt_disable_clkout_dp(dev);
  6655. hsw_disable_lcpll(dev_priv, true, true);
  6656. }
  6657. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6658. {
  6659. struct drm_device *dev = dev_priv->dev;
  6660. uint32_t val;
  6661. DRM_DEBUG_KMS("Disabling package C8+\n");
  6662. hsw_restore_lcpll(dev_priv);
  6663. lpt_init_pch_refclk(dev);
  6664. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6665. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6666. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6667. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6668. }
  6669. intel_prepare_ddi(dev);
  6670. }
  6671. static int haswell_crtc_compute_clock(struct intel_crtc *crtc)
  6672. {
  6673. if (!intel_ddi_pll_select(crtc))
  6674. return -EINVAL;
  6675. crtc->lowfreq_avail = false;
  6676. return 0;
  6677. }
  6678. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  6679. enum port port,
  6680. struct intel_crtc_state *pipe_config)
  6681. {
  6682. u32 temp, dpll_ctl1;
  6683. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  6684. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  6685. switch (pipe_config->ddi_pll_sel) {
  6686. case SKL_DPLL0:
  6687. /*
  6688. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  6689. * of the shared DPLL framework and thus needs to be read out
  6690. * separately
  6691. */
  6692. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  6693. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  6694. break;
  6695. case SKL_DPLL1:
  6696. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  6697. break;
  6698. case SKL_DPLL2:
  6699. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  6700. break;
  6701. case SKL_DPLL3:
  6702. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  6703. break;
  6704. }
  6705. }
  6706. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6707. enum port port,
  6708. struct intel_crtc_state *pipe_config)
  6709. {
  6710. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6711. switch (pipe_config->ddi_pll_sel) {
  6712. case PORT_CLK_SEL_WRPLL1:
  6713. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6714. break;
  6715. case PORT_CLK_SEL_WRPLL2:
  6716. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6717. break;
  6718. }
  6719. }
  6720. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6721. struct intel_crtc_state *pipe_config)
  6722. {
  6723. struct drm_device *dev = crtc->base.dev;
  6724. struct drm_i915_private *dev_priv = dev->dev_private;
  6725. struct intel_shared_dpll *pll;
  6726. enum port port;
  6727. uint32_t tmp;
  6728. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6729. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6730. if (IS_SKYLAKE(dev))
  6731. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  6732. else
  6733. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6734. if (pipe_config->shared_dpll >= 0) {
  6735. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6736. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6737. &pipe_config->dpll_hw_state));
  6738. }
  6739. /*
  6740. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6741. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6742. * the PCH transcoder is on.
  6743. */
  6744. if (INTEL_INFO(dev)->gen < 9 &&
  6745. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6746. pipe_config->has_pch_encoder = true;
  6747. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6748. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6749. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6750. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6751. }
  6752. }
  6753. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6754. struct intel_crtc_state *pipe_config)
  6755. {
  6756. struct drm_device *dev = crtc->base.dev;
  6757. struct drm_i915_private *dev_priv = dev->dev_private;
  6758. enum intel_display_power_domain pfit_domain;
  6759. uint32_t tmp;
  6760. if (!intel_display_power_is_enabled(dev_priv,
  6761. POWER_DOMAIN_PIPE(crtc->pipe)))
  6762. return false;
  6763. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6764. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6765. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6766. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6767. enum pipe trans_edp_pipe;
  6768. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6769. default:
  6770. WARN(1, "unknown pipe linked to edp transcoder\n");
  6771. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6772. case TRANS_DDI_EDP_INPUT_A_ON:
  6773. trans_edp_pipe = PIPE_A;
  6774. break;
  6775. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6776. trans_edp_pipe = PIPE_B;
  6777. break;
  6778. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6779. trans_edp_pipe = PIPE_C;
  6780. break;
  6781. }
  6782. if (trans_edp_pipe == crtc->pipe)
  6783. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6784. }
  6785. if (!intel_display_power_is_enabled(dev_priv,
  6786. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6787. return false;
  6788. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6789. if (!(tmp & PIPECONF_ENABLE))
  6790. return false;
  6791. haswell_get_ddi_port_state(crtc, pipe_config);
  6792. intel_get_pipe_timings(crtc, pipe_config);
  6793. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6794. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  6795. if (IS_SKYLAKE(dev))
  6796. skylake_get_pfit_config(crtc, pipe_config);
  6797. else
  6798. ironlake_get_pfit_config(crtc, pipe_config);
  6799. }
  6800. if (IS_HASWELL(dev))
  6801. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6802. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6803. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  6804. pipe_config->pixel_multiplier =
  6805. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  6806. } else {
  6807. pipe_config->pixel_multiplier = 1;
  6808. }
  6809. return true;
  6810. }
  6811. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6812. {
  6813. struct drm_device *dev = crtc->dev;
  6814. struct drm_i915_private *dev_priv = dev->dev_private;
  6815. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6816. uint32_t cntl = 0, size = 0;
  6817. if (base) {
  6818. unsigned int width = intel_crtc->cursor_width;
  6819. unsigned int height = intel_crtc->cursor_height;
  6820. unsigned int stride = roundup_pow_of_two(width) * 4;
  6821. switch (stride) {
  6822. default:
  6823. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6824. width, stride);
  6825. stride = 256;
  6826. /* fallthrough */
  6827. case 256:
  6828. case 512:
  6829. case 1024:
  6830. case 2048:
  6831. break;
  6832. }
  6833. cntl |= CURSOR_ENABLE |
  6834. CURSOR_GAMMA_ENABLE |
  6835. CURSOR_FORMAT_ARGB |
  6836. CURSOR_STRIDE(stride);
  6837. size = (height << 12) | width;
  6838. }
  6839. if (intel_crtc->cursor_cntl != 0 &&
  6840. (intel_crtc->cursor_base != base ||
  6841. intel_crtc->cursor_size != size ||
  6842. intel_crtc->cursor_cntl != cntl)) {
  6843. /* On these chipsets we can only modify the base/size/stride
  6844. * whilst the cursor is disabled.
  6845. */
  6846. I915_WRITE(_CURACNTR, 0);
  6847. POSTING_READ(_CURACNTR);
  6848. intel_crtc->cursor_cntl = 0;
  6849. }
  6850. if (intel_crtc->cursor_base != base) {
  6851. I915_WRITE(_CURABASE, base);
  6852. intel_crtc->cursor_base = base;
  6853. }
  6854. if (intel_crtc->cursor_size != size) {
  6855. I915_WRITE(CURSIZE, size);
  6856. intel_crtc->cursor_size = size;
  6857. }
  6858. if (intel_crtc->cursor_cntl != cntl) {
  6859. I915_WRITE(_CURACNTR, cntl);
  6860. POSTING_READ(_CURACNTR);
  6861. intel_crtc->cursor_cntl = cntl;
  6862. }
  6863. }
  6864. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6865. {
  6866. struct drm_device *dev = crtc->dev;
  6867. struct drm_i915_private *dev_priv = dev->dev_private;
  6868. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6869. int pipe = intel_crtc->pipe;
  6870. uint32_t cntl;
  6871. cntl = 0;
  6872. if (base) {
  6873. cntl = MCURSOR_GAMMA_ENABLE;
  6874. switch (intel_crtc->cursor_width) {
  6875. case 64:
  6876. cntl |= CURSOR_MODE_64_ARGB_AX;
  6877. break;
  6878. case 128:
  6879. cntl |= CURSOR_MODE_128_ARGB_AX;
  6880. break;
  6881. case 256:
  6882. cntl |= CURSOR_MODE_256_ARGB_AX;
  6883. break;
  6884. default:
  6885. MISSING_CASE(intel_crtc->cursor_width);
  6886. return;
  6887. }
  6888. cntl |= pipe << 28; /* Connect to correct pipe */
  6889. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6890. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6891. }
  6892. if (to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180))
  6893. cntl |= CURSOR_ROTATE_180;
  6894. if (intel_crtc->cursor_cntl != cntl) {
  6895. I915_WRITE(CURCNTR(pipe), cntl);
  6896. POSTING_READ(CURCNTR(pipe));
  6897. intel_crtc->cursor_cntl = cntl;
  6898. }
  6899. /* and commit changes on next vblank */
  6900. I915_WRITE(CURBASE(pipe), base);
  6901. POSTING_READ(CURBASE(pipe));
  6902. intel_crtc->cursor_base = base;
  6903. }
  6904. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6905. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6906. bool on)
  6907. {
  6908. struct drm_device *dev = crtc->dev;
  6909. struct drm_i915_private *dev_priv = dev->dev_private;
  6910. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6911. int pipe = intel_crtc->pipe;
  6912. int x = crtc->cursor_x;
  6913. int y = crtc->cursor_y;
  6914. u32 base = 0, pos = 0;
  6915. if (on)
  6916. base = intel_crtc->cursor_addr;
  6917. if (x >= intel_crtc->config.pipe_src_w)
  6918. base = 0;
  6919. if (y >= intel_crtc->config.pipe_src_h)
  6920. base = 0;
  6921. if (x < 0) {
  6922. if (x + intel_crtc->cursor_width <= 0)
  6923. base = 0;
  6924. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6925. x = -x;
  6926. }
  6927. pos |= x << CURSOR_X_SHIFT;
  6928. if (y < 0) {
  6929. if (y + intel_crtc->cursor_height <= 0)
  6930. base = 0;
  6931. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6932. y = -y;
  6933. }
  6934. pos |= y << CURSOR_Y_SHIFT;
  6935. if (base == 0 && intel_crtc->cursor_base == 0)
  6936. return;
  6937. I915_WRITE(CURPOS(pipe), pos);
  6938. /* ILK+ do this automagically */
  6939. if (HAS_GMCH_DISPLAY(dev) &&
  6940. to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180)) {
  6941. base += (intel_crtc->cursor_height *
  6942. intel_crtc->cursor_width - 1) * 4;
  6943. }
  6944. if (IS_845G(dev) || IS_I865G(dev))
  6945. i845_update_cursor(crtc, base);
  6946. else
  6947. i9xx_update_cursor(crtc, base);
  6948. }
  6949. static bool cursor_size_ok(struct drm_device *dev,
  6950. uint32_t width, uint32_t height)
  6951. {
  6952. if (width == 0 || height == 0)
  6953. return false;
  6954. /*
  6955. * 845g/865g are special in that they are only limited by
  6956. * the width of their cursors, the height is arbitrary up to
  6957. * the precision of the register. Everything else requires
  6958. * square cursors, limited to a few power-of-two sizes.
  6959. */
  6960. if (IS_845G(dev) || IS_I865G(dev)) {
  6961. if ((width & 63) != 0)
  6962. return false;
  6963. if (width > (IS_845G(dev) ? 64 : 512))
  6964. return false;
  6965. if (height > 1023)
  6966. return false;
  6967. } else {
  6968. switch (width | height) {
  6969. case 256:
  6970. case 128:
  6971. if (IS_GEN2(dev))
  6972. return false;
  6973. case 64:
  6974. break;
  6975. default:
  6976. return false;
  6977. }
  6978. }
  6979. return true;
  6980. }
  6981. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6982. u16 *blue, uint32_t start, uint32_t size)
  6983. {
  6984. int end = (start + size > 256) ? 256 : start + size, i;
  6985. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6986. for (i = start; i < end; i++) {
  6987. intel_crtc->lut_r[i] = red[i] >> 8;
  6988. intel_crtc->lut_g[i] = green[i] >> 8;
  6989. intel_crtc->lut_b[i] = blue[i] >> 8;
  6990. }
  6991. intel_crtc_load_lut(crtc);
  6992. }
  6993. /* VESA 640x480x72Hz mode to set on the pipe */
  6994. static struct drm_display_mode load_detect_mode = {
  6995. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6996. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6997. };
  6998. struct drm_framebuffer *
  6999. __intel_framebuffer_create(struct drm_device *dev,
  7000. struct drm_mode_fb_cmd2 *mode_cmd,
  7001. struct drm_i915_gem_object *obj)
  7002. {
  7003. struct intel_framebuffer *intel_fb;
  7004. int ret;
  7005. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7006. if (!intel_fb) {
  7007. drm_gem_object_unreference(&obj->base);
  7008. return ERR_PTR(-ENOMEM);
  7009. }
  7010. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7011. if (ret)
  7012. goto err;
  7013. return &intel_fb->base;
  7014. err:
  7015. drm_gem_object_unreference(&obj->base);
  7016. kfree(intel_fb);
  7017. return ERR_PTR(ret);
  7018. }
  7019. static struct drm_framebuffer *
  7020. intel_framebuffer_create(struct drm_device *dev,
  7021. struct drm_mode_fb_cmd2 *mode_cmd,
  7022. struct drm_i915_gem_object *obj)
  7023. {
  7024. struct drm_framebuffer *fb;
  7025. int ret;
  7026. ret = i915_mutex_lock_interruptible(dev);
  7027. if (ret)
  7028. return ERR_PTR(ret);
  7029. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7030. mutex_unlock(&dev->struct_mutex);
  7031. return fb;
  7032. }
  7033. static u32
  7034. intel_framebuffer_pitch_for_width(int width, int bpp)
  7035. {
  7036. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7037. return ALIGN(pitch, 64);
  7038. }
  7039. static u32
  7040. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7041. {
  7042. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7043. return PAGE_ALIGN(pitch * mode->vdisplay);
  7044. }
  7045. static struct drm_framebuffer *
  7046. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7047. struct drm_display_mode *mode,
  7048. int depth, int bpp)
  7049. {
  7050. struct drm_i915_gem_object *obj;
  7051. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7052. obj = i915_gem_alloc_object(dev,
  7053. intel_framebuffer_size_for_mode(mode, bpp));
  7054. if (obj == NULL)
  7055. return ERR_PTR(-ENOMEM);
  7056. mode_cmd.width = mode->hdisplay;
  7057. mode_cmd.height = mode->vdisplay;
  7058. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7059. bpp);
  7060. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7061. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7062. }
  7063. static struct drm_framebuffer *
  7064. mode_fits_in_fbdev(struct drm_device *dev,
  7065. struct drm_display_mode *mode)
  7066. {
  7067. #ifdef CONFIG_DRM_I915_FBDEV
  7068. struct drm_i915_private *dev_priv = dev->dev_private;
  7069. struct drm_i915_gem_object *obj;
  7070. struct drm_framebuffer *fb;
  7071. if (!dev_priv->fbdev)
  7072. return NULL;
  7073. if (!dev_priv->fbdev->fb)
  7074. return NULL;
  7075. obj = dev_priv->fbdev->fb->obj;
  7076. BUG_ON(!obj);
  7077. fb = &dev_priv->fbdev->fb->base;
  7078. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7079. fb->bits_per_pixel))
  7080. return NULL;
  7081. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7082. return NULL;
  7083. return fb;
  7084. #else
  7085. return NULL;
  7086. #endif
  7087. }
  7088. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7089. struct drm_display_mode *mode,
  7090. struct intel_load_detect_pipe *old,
  7091. struct drm_modeset_acquire_ctx *ctx)
  7092. {
  7093. struct intel_crtc *intel_crtc;
  7094. struct intel_encoder *intel_encoder =
  7095. intel_attached_encoder(connector);
  7096. struct drm_crtc *possible_crtc;
  7097. struct drm_encoder *encoder = &intel_encoder->base;
  7098. struct drm_crtc *crtc = NULL;
  7099. struct drm_device *dev = encoder->dev;
  7100. struct drm_framebuffer *fb;
  7101. struct drm_mode_config *config = &dev->mode_config;
  7102. int ret, i = -1;
  7103. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7104. connector->base.id, connector->name,
  7105. encoder->base.id, encoder->name);
  7106. retry:
  7107. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7108. if (ret)
  7109. goto fail_unlock;
  7110. /*
  7111. * Algorithm gets a little messy:
  7112. *
  7113. * - if the connector already has an assigned crtc, use it (but make
  7114. * sure it's on first)
  7115. *
  7116. * - try to find the first unused crtc that can drive this connector,
  7117. * and use that if we find one
  7118. */
  7119. /* See if we already have a CRTC for this connector */
  7120. if (encoder->crtc) {
  7121. crtc = encoder->crtc;
  7122. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7123. if (ret)
  7124. goto fail_unlock;
  7125. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7126. if (ret)
  7127. goto fail_unlock;
  7128. old->dpms_mode = connector->dpms;
  7129. old->load_detect_temp = false;
  7130. /* Make sure the crtc and connector are running */
  7131. if (connector->dpms != DRM_MODE_DPMS_ON)
  7132. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7133. return true;
  7134. }
  7135. /* Find an unused one (if possible) */
  7136. for_each_crtc(dev, possible_crtc) {
  7137. i++;
  7138. if (!(encoder->possible_crtcs & (1 << i)))
  7139. continue;
  7140. if (possible_crtc->enabled)
  7141. continue;
  7142. /* This can occur when applying the pipe A quirk on resume. */
  7143. if (to_intel_crtc(possible_crtc)->new_enabled)
  7144. continue;
  7145. crtc = possible_crtc;
  7146. break;
  7147. }
  7148. /*
  7149. * If we didn't find an unused CRTC, don't use any.
  7150. */
  7151. if (!crtc) {
  7152. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7153. goto fail_unlock;
  7154. }
  7155. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7156. if (ret)
  7157. goto fail_unlock;
  7158. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7159. if (ret)
  7160. goto fail_unlock;
  7161. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7162. to_intel_connector(connector)->new_encoder = intel_encoder;
  7163. intel_crtc = to_intel_crtc(crtc);
  7164. intel_crtc->new_enabled = true;
  7165. intel_crtc->new_config = &intel_crtc->config;
  7166. old->dpms_mode = connector->dpms;
  7167. old->load_detect_temp = true;
  7168. old->release_fb = NULL;
  7169. if (!mode)
  7170. mode = &load_detect_mode;
  7171. /* We need a framebuffer large enough to accommodate all accesses
  7172. * that the plane may generate whilst we perform load detection.
  7173. * We can not rely on the fbcon either being present (we get called
  7174. * during its initialisation to detect all boot displays, or it may
  7175. * not even exist) or that it is large enough to satisfy the
  7176. * requested mode.
  7177. */
  7178. fb = mode_fits_in_fbdev(dev, mode);
  7179. if (fb == NULL) {
  7180. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7181. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7182. old->release_fb = fb;
  7183. } else
  7184. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7185. if (IS_ERR(fb)) {
  7186. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7187. goto fail;
  7188. }
  7189. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7190. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7191. if (old->release_fb)
  7192. old->release_fb->funcs->destroy(old->release_fb);
  7193. goto fail;
  7194. }
  7195. /* let the connector get through one full cycle before testing */
  7196. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7197. return true;
  7198. fail:
  7199. intel_crtc->new_enabled = crtc->enabled;
  7200. if (intel_crtc->new_enabled)
  7201. intel_crtc->new_config = &intel_crtc->config;
  7202. else
  7203. intel_crtc->new_config = NULL;
  7204. fail_unlock:
  7205. if (ret == -EDEADLK) {
  7206. drm_modeset_backoff(ctx);
  7207. goto retry;
  7208. }
  7209. return false;
  7210. }
  7211. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7212. struct intel_load_detect_pipe *old)
  7213. {
  7214. struct intel_encoder *intel_encoder =
  7215. intel_attached_encoder(connector);
  7216. struct drm_encoder *encoder = &intel_encoder->base;
  7217. struct drm_crtc *crtc = encoder->crtc;
  7218. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7219. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7220. connector->base.id, connector->name,
  7221. encoder->base.id, encoder->name);
  7222. if (old->load_detect_temp) {
  7223. to_intel_connector(connector)->new_encoder = NULL;
  7224. intel_encoder->new_crtc = NULL;
  7225. intel_crtc->new_enabled = false;
  7226. intel_crtc->new_config = NULL;
  7227. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7228. if (old->release_fb) {
  7229. drm_framebuffer_unregister_private(old->release_fb);
  7230. drm_framebuffer_unreference(old->release_fb);
  7231. }
  7232. return;
  7233. }
  7234. /* Switch crtc and encoder back off if necessary */
  7235. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7236. connector->funcs->dpms(connector, old->dpms_mode);
  7237. }
  7238. static int i9xx_pll_refclk(struct drm_device *dev,
  7239. const struct intel_crtc_state *pipe_config)
  7240. {
  7241. struct drm_i915_private *dev_priv = dev->dev_private;
  7242. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7243. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7244. return dev_priv->vbt.lvds_ssc_freq;
  7245. else if (HAS_PCH_SPLIT(dev))
  7246. return 120000;
  7247. else if (!IS_GEN2(dev))
  7248. return 96000;
  7249. else
  7250. return 48000;
  7251. }
  7252. /* Returns the clock of the currently programmed mode of the given pipe. */
  7253. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7254. struct intel_crtc_state *pipe_config)
  7255. {
  7256. struct drm_device *dev = crtc->base.dev;
  7257. struct drm_i915_private *dev_priv = dev->dev_private;
  7258. int pipe = pipe_config->cpu_transcoder;
  7259. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7260. u32 fp;
  7261. intel_clock_t clock;
  7262. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7263. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7264. fp = pipe_config->dpll_hw_state.fp0;
  7265. else
  7266. fp = pipe_config->dpll_hw_state.fp1;
  7267. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7268. if (IS_PINEVIEW(dev)) {
  7269. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7270. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7271. } else {
  7272. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7273. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7274. }
  7275. if (!IS_GEN2(dev)) {
  7276. if (IS_PINEVIEW(dev))
  7277. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7278. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7279. else
  7280. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7281. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7282. switch (dpll & DPLL_MODE_MASK) {
  7283. case DPLLB_MODE_DAC_SERIAL:
  7284. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7285. 5 : 10;
  7286. break;
  7287. case DPLLB_MODE_LVDS:
  7288. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7289. 7 : 14;
  7290. break;
  7291. default:
  7292. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7293. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7294. return;
  7295. }
  7296. if (IS_PINEVIEW(dev))
  7297. pineview_clock(refclk, &clock);
  7298. else
  7299. i9xx_clock(refclk, &clock);
  7300. } else {
  7301. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7302. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7303. if (is_lvds) {
  7304. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7305. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7306. if (lvds & LVDS_CLKB_POWER_UP)
  7307. clock.p2 = 7;
  7308. else
  7309. clock.p2 = 14;
  7310. } else {
  7311. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7312. clock.p1 = 2;
  7313. else {
  7314. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7315. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7316. }
  7317. if (dpll & PLL_P2_DIVIDE_BY_4)
  7318. clock.p2 = 4;
  7319. else
  7320. clock.p2 = 2;
  7321. }
  7322. i9xx_clock(refclk, &clock);
  7323. }
  7324. /*
  7325. * This value includes pixel_multiplier. We will use
  7326. * port_clock to compute adjusted_mode.crtc_clock in the
  7327. * encoder's get_config() function.
  7328. */
  7329. pipe_config->port_clock = clock.dot;
  7330. }
  7331. int intel_dotclock_calculate(int link_freq,
  7332. const struct intel_link_m_n *m_n)
  7333. {
  7334. /*
  7335. * The calculation for the data clock is:
  7336. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7337. * But we want to avoid losing precison if possible, so:
  7338. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7339. *
  7340. * and the link clock is simpler:
  7341. * link_clock = (m * link_clock) / n
  7342. */
  7343. if (!m_n->link_n)
  7344. return 0;
  7345. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7346. }
  7347. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7348. struct intel_crtc_state *pipe_config)
  7349. {
  7350. struct drm_device *dev = crtc->base.dev;
  7351. /* read out port_clock from the DPLL */
  7352. i9xx_crtc_clock_get(crtc, pipe_config);
  7353. /*
  7354. * This value does not include pixel_multiplier.
  7355. * We will check that port_clock and adjusted_mode.crtc_clock
  7356. * agree once we know their relationship in the encoder's
  7357. * get_config() function.
  7358. */
  7359. pipe_config->base.adjusted_mode.crtc_clock =
  7360. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7361. &pipe_config->fdi_m_n);
  7362. }
  7363. /** Returns the currently programmed mode of the given pipe. */
  7364. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7365. struct drm_crtc *crtc)
  7366. {
  7367. struct drm_i915_private *dev_priv = dev->dev_private;
  7368. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7369. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7370. struct drm_display_mode *mode;
  7371. struct intel_crtc_state pipe_config;
  7372. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7373. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7374. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7375. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7376. enum pipe pipe = intel_crtc->pipe;
  7377. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7378. if (!mode)
  7379. return NULL;
  7380. /*
  7381. * Construct a pipe_config sufficient for getting the clock info
  7382. * back out of crtc_clock_get.
  7383. *
  7384. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7385. * to use a real value here instead.
  7386. */
  7387. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7388. pipe_config.pixel_multiplier = 1;
  7389. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7390. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7391. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7392. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7393. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7394. mode->hdisplay = (htot & 0xffff) + 1;
  7395. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7396. mode->hsync_start = (hsync & 0xffff) + 1;
  7397. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7398. mode->vdisplay = (vtot & 0xffff) + 1;
  7399. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7400. mode->vsync_start = (vsync & 0xffff) + 1;
  7401. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7402. drm_mode_set_name(mode);
  7403. return mode;
  7404. }
  7405. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7406. {
  7407. struct drm_device *dev = crtc->dev;
  7408. struct drm_i915_private *dev_priv = dev->dev_private;
  7409. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7410. if (!HAS_GMCH_DISPLAY(dev))
  7411. return;
  7412. if (!dev_priv->lvds_downclock_avail)
  7413. return;
  7414. /*
  7415. * Since this is called by a timer, we should never get here in
  7416. * the manual case.
  7417. */
  7418. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7419. int pipe = intel_crtc->pipe;
  7420. int dpll_reg = DPLL(pipe);
  7421. int dpll;
  7422. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7423. assert_panel_unlocked(dev_priv, pipe);
  7424. dpll = I915_READ(dpll_reg);
  7425. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7426. I915_WRITE(dpll_reg, dpll);
  7427. intel_wait_for_vblank(dev, pipe);
  7428. dpll = I915_READ(dpll_reg);
  7429. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7430. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7431. }
  7432. }
  7433. void intel_mark_busy(struct drm_device *dev)
  7434. {
  7435. struct drm_i915_private *dev_priv = dev->dev_private;
  7436. if (dev_priv->mm.busy)
  7437. return;
  7438. intel_runtime_pm_get(dev_priv);
  7439. i915_update_gfx_val(dev_priv);
  7440. dev_priv->mm.busy = true;
  7441. }
  7442. void intel_mark_idle(struct drm_device *dev)
  7443. {
  7444. struct drm_i915_private *dev_priv = dev->dev_private;
  7445. struct drm_crtc *crtc;
  7446. if (!dev_priv->mm.busy)
  7447. return;
  7448. dev_priv->mm.busy = false;
  7449. if (!i915.powersave)
  7450. goto out;
  7451. for_each_crtc(dev, crtc) {
  7452. if (!crtc->primary->fb)
  7453. continue;
  7454. intel_decrease_pllclock(crtc);
  7455. }
  7456. if (INTEL_INFO(dev)->gen >= 6)
  7457. gen6_rps_idle(dev->dev_private);
  7458. out:
  7459. intel_runtime_pm_put(dev_priv);
  7460. }
  7461. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7462. {
  7463. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7464. struct drm_device *dev = crtc->dev;
  7465. struct intel_unpin_work *work;
  7466. spin_lock_irq(&dev->event_lock);
  7467. work = intel_crtc->unpin_work;
  7468. intel_crtc->unpin_work = NULL;
  7469. spin_unlock_irq(&dev->event_lock);
  7470. if (work) {
  7471. cancel_work_sync(&work->work);
  7472. kfree(work);
  7473. }
  7474. drm_crtc_cleanup(crtc);
  7475. kfree(intel_crtc);
  7476. }
  7477. static void intel_unpin_work_fn(struct work_struct *__work)
  7478. {
  7479. struct intel_unpin_work *work =
  7480. container_of(__work, struct intel_unpin_work, work);
  7481. struct drm_device *dev = work->crtc->dev;
  7482. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7483. mutex_lock(&dev->struct_mutex);
  7484. intel_unpin_fb_obj(work->old_fb_obj);
  7485. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7486. drm_gem_object_unreference(&work->old_fb_obj->base);
  7487. intel_fbc_update(dev);
  7488. if (work->flip_queued_req)
  7489. i915_gem_request_assign(&work->flip_queued_req, NULL);
  7490. mutex_unlock(&dev->struct_mutex);
  7491. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7492. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7493. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7494. kfree(work);
  7495. }
  7496. static void do_intel_finish_page_flip(struct drm_device *dev,
  7497. struct drm_crtc *crtc)
  7498. {
  7499. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7500. struct intel_unpin_work *work;
  7501. unsigned long flags;
  7502. /* Ignore early vblank irqs */
  7503. if (intel_crtc == NULL)
  7504. return;
  7505. /*
  7506. * This is called both by irq handlers and the reset code (to complete
  7507. * lost pageflips) so needs the full irqsave spinlocks.
  7508. */
  7509. spin_lock_irqsave(&dev->event_lock, flags);
  7510. work = intel_crtc->unpin_work;
  7511. /* Ensure we don't miss a work->pending update ... */
  7512. smp_rmb();
  7513. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7514. spin_unlock_irqrestore(&dev->event_lock, flags);
  7515. return;
  7516. }
  7517. page_flip_completed(intel_crtc);
  7518. spin_unlock_irqrestore(&dev->event_lock, flags);
  7519. }
  7520. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7521. {
  7522. struct drm_i915_private *dev_priv = dev->dev_private;
  7523. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7524. do_intel_finish_page_flip(dev, crtc);
  7525. }
  7526. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7527. {
  7528. struct drm_i915_private *dev_priv = dev->dev_private;
  7529. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7530. do_intel_finish_page_flip(dev, crtc);
  7531. }
  7532. /* Is 'a' after or equal to 'b'? */
  7533. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7534. {
  7535. return !((a - b) & 0x80000000);
  7536. }
  7537. static bool page_flip_finished(struct intel_crtc *crtc)
  7538. {
  7539. struct drm_device *dev = crtc->base.dev;
  7540. struct drm_i915_private *dev_priv = dev->dev_private;
  7541. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7542. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7543. return true;
  7544. /*
  7545. * The relevant registers doen't exist on pre-ctg.
  7546. * As the flip done interrupt doesn't trigger for mmio
  7547. * flips on gmch platforms, a flip count check isn't
  7548. * really needed there. But since ctg has the registers,
  7549. * include it in the check anyway.
  7550. */
  7551. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7552. return true;
  7553. /*
  7554. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7555. * used the same base address. In that case the mmio flip might
  7556. * have completed, but the CS hasn't even executed the flip yet.
  7557. *
  7558. * A flip count check isn't enough as the CS might have updated
  7559. * the base address just after start of vblank, but before we
  7560. * managed to process the interrupt. This means we'd complete the
  7561. * CS flip too soon.
  7562. *
  7563. * Combining both checks should get us a good enough result. It may
  7564. * still happen that the CS flip has been executed, but has not
  7565. * yet actually completed. But in case the base address is the same
  7566. * anyway, we don't really care.
  7567. */
  7568. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7569. crtc->unpin_work->gtt_offset &&
  7570. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7571. crtc->unpin_work->flip_count);
  7572. }
  7573. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7574. {
  7575. struct drm_i915_private *dev_priv = dev->dev_private;
  7576. struct intel_crtc *intel_crtc =
  7577. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7578. unsigned long flags;
  7579. /*
  7580. * This is called both by irq handlers and the reset code (to complete
  7581. * lost pageflips) so needs the full irqsave spinlocks.
  7582. *
  7583. * NB: An MMIO update of the plane base pointer will also
  7584. * generate a page-flip completion irq, i.e. every modeset
  7585. * is also accompanied by a spurious intel_prepare_page_flip().
  7586. */
  7587. spin_lock_irqsave(&dev->event_lock, flags);
  7588. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7589. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7590. spin_unlock_irqrestore(&dev->event_lock, flags);
  7591. }
  7592. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7593. {
  7594. /* Ensure that the work item is consistent when activating it ... */
  7595. smp_wmb();
  7596. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7597. /* and that it is marked active as soon as the irq could fire. */
  7598. smp_wmb();
  7599. }
  7600. static int intel_gen2_queue_flip(struct drm_device *dev,
  7601. struct drm_crtc *crtc,
  7602. struct drm_framebuffer *fb,
  7603. struct drm_i915_gem_object *obj,
  7604. struct intel_engine_cs *ring,
  7605. uint32_t flags)
  7606. {
  7607. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7608. u32 flip_mask;
  7609. int ret;
  7610. ret = intel_ring_begin(ring, 6);
  7611. if (ret)
  7612. return ret;
  7613. /* Can't queue multiple flips, so wait for the previous
  7614. * one to finish before executing the next.
  7615. */
  7616. if (intel_crtc->plane)
  7617. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7618. else
  7619. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7620. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7621. intel_ring_emit(ring, MI_NOOP);
  7622. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7623. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7624. intel_ring_emit(ring, fb->pitches[0]);
  7625. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7626. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7627. intel_mark_page_flip_active(intel_crtc);
  7628. __intel_ring_advance(ring);
  7629. return 0;
  7630. }
  7631. static int intel_gen3_queue_flip(struct drm_device *dev,
  7632. struct drm_crtc *crtc,
  7633. struct drm_framebuffer *fb,
  7634. struct drm_i915_gem_object *obj,
  7635. struct intel_engine_cs *ring,
  7636. uint32_t flags)
  7637. {
  7638. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7639. u32 flip_mask;
  7640. int ret;
  7641. ret = intel_ring_begin(ring, 6);
  7642. if (ret)
  7643. return ret;
  7644. if (intel_crtc->plane)
  7645. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7646. else
  7647. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7648. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7649. intel_ring_emit(ring, MI_NOOP);
  7650. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7651. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7652. intel_ring_emit(ring, fb->pitches[0]);
  7653. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7654. intel_ring_emit(ring, MI_NOOP);
  7655. intel_mark_page_flip_active(intel_crtc);
  7656. __intel_ring_advance(ring);
  7657. return 0;
  7658. }
  7659. static int intel_gen4_queue_flip(struct drm_device *dev,
  7660. struct drm_crtc *crtc,
  7661. struct drm_framebuffer *fb,
  7662. struct drm_i915_gem_object *obj,
  7663. struct intel_engine_cs *ring,
  7664. uint32_t flags)
  7665. {
  7666. struct drm_i915_private *dev_priv = dev->dev_private;
  7667. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7668. uint32_t pf, pipesrc;
  7669. int ret;
  7670. ret = intel_ring_begin(ring, 4);
  7671. if (ret)
  7672. return ret;
  7673. /* i965+ uses the linear or tiled offsets from the
  7674. * Display Registers (which do not change across a page-flip)
  7675. * so we need only reprogram the base address.
  7676. */
  7677. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7678. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7679. intel_ring_emit(ring, fb->pitches[0]);
  7680. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7681. obj->tiling_mode);
  7682. /* XXX Enabling the panel-fitter across page-flip is so far
  7683. * untested on non-native modes, so ignore it for now.
  7684. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7685. */
  7686. pf = 0;
  7687. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7688. intel_ring_emit(ring, pf | pipesrc);
  7689. intel_mark_page_flip_active(intel_crtc);
  7690. __intel_ring_advance(ring);
  7691. return 0;
  7692. }
  7693. static int intel_gen6_queue_flip(struct drm_device *dev,
  7694. struct drm_crtc *crtc,
  7695. struct drm_framebuffer *fb,
  7696. struct drm_i915_gem_object *obj,
  7697. struct intel_engine_cs *ring,
  7698. uint32_t flags)
  7699. {
  7700. struct drm_i915_private *dev_priv = dev->dev_private;
  7701. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7702. uint32_t pf, pipesrc;
  7703. int ret;
  7704. ret = intel_ring_begin(ring, 4);
  7705. if (ret)
  7706. return ret;
  7707. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7708. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7709. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7710. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7711. /* Contrary to the suggestions in the documentation,
  7712. * "Enable Panel Fitter" does not seem to be required when page
  7713. * flipping with a non-native mode, and worse causes a normal
  7714. * modeset to fail.
  7715. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7716. */
  7717. pf = 0;
  7718. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7719. intel_ring_emit(ring, pf | pipesrc);
  7720. intel_mark_page_flip_active(intel_crtc);
  7721. __intel_ring_advance(ring);
  7722. return 0;
  7723. }
  7724. static int intel_gen7_queue_flip(struct drm_device *dev,
  7725. struct drm_crtc *crtc,
  7726. struct drm_framebuffer *fb,
  7727. struct drm_i915_gem_object *obj,
  7728. struct intel_engine_cs *ring,
  7729. uint32_t flags)
  7730. {
  7731. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7732. uint32_t plane_bit = 0;
  7733. int len, ret;
  7734. switch (intel_crtc->plane) {
  7735. case PLANE_A:
  7736. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7737. break;
  7738. case PLANE_B:
  7739. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7740. break;
  7741. case PLANE_C:
  7742. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7743. break;
  7744. default:
  7745. WARN_ONCE(1, "unknown plane in flip command\n");
  7746. return -ENODEV;
  7747. }
  7748. len = 4;
  7749. if (ring->id == RCS) {
  7750. len += 6;
  7751. /*
  7752. * On Gen 8, SRM is now taking an extra dword to accommodate
  7753. * 48bits addresses, and we need a NOOP for the batch size to
  7754. * stay even.
  7755. */
  7756. if (IS_GEN8(dev))
  7757. len += 2;
  7758. }
  7759. /*
  7760. * BSpec MI_DISPLAY_FLIP for IVB:
  7761. * "The full packet must be contained within the same cache line."
  7762. *
  7763. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7764. * cacheline, if we ever start emitting more commands before
  7765. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7766. * then do the cacheline alignment, and finally emit the
  7767. * MI_DISPLAY_FLIP.
  7768. */
  7769. ret = intel_ring_cacheline_align(ring);
  7770. if (ret)
  7771. return ret;
  7772. ret = intel_ring_begin(ring, len);
  7773. if (ret)
  7774. return ret;
  7775. /* Unmask the flip-done completion message. Note that the bspec says that
  7776. * we should do this for both the BCS and RCS, and that we must not unmask
  7777. * more than one flip event at any time (or ensure that one flip message
  7778. * can be sent by waiting for flip-done prior to queueing new flips).
  7779. * Experimentation says that BCS works despite DERRMR masking all
  7780. * flip-done completion events and that unmasking all planes at once
  7781. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7782. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7783. */
  7784. if (ring->id == RCS) {
  7785. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7786. intel_ring_emit(ring, DERRMR);
  7787. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7788. DERRMR_PIPEB_PRI_FLIP_DONE |
  7789. DERRMR_PIPEC_PRI_FLIP_DONE));
  7790. if (IS_GEN8(dev))
  7791. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7792. MI_SRM_LRM_GLOBAL_GTT);
  7793. else
  7794. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7795. MI_SRM_LRM_GLOBAL_GTT);
  7796. intel_ring_emit(ring, DERRMR);
  7797. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7798. if (IS_GEN8(dev)) {
  7799. intel_ring_emit(ring, 0);
  7800. intel_ring_emit(ring, MI_NOOP);
  7801. }
  7802. }
  7803. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7804. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7805. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7806. intel_ring_emit(ring, (MI_NOOP));
  7807. intel_mark_page_flip_active(intel_crtc);
  7808. __intel_ring_advance(ring);
  7809. return 0;
  7810. }
  7811. static bool use_mmio_flip(struct intel_engine_cs *ring,
  7812. struct drm_i915_gem_object *obj)
  7813. {
  7814. /*
  7815. * This is not being used for older platforms, because
  7816. * non-availability of flip done interrupt forces us to use
  7817. * CS flips. Older platforms derive flip done using some clever
  7818. * tricks involving the flip_pending status bits and vblank irqs.
  7819. * So using MMIO flips there would disrupt this mechanism.
  7820. */
  7821. if (ring == NULL)
  7822. return true;
  7823. if (INTEL_INFO(ring->dev)->gen < 5)
  7824. return false;
  7825. if (i915.use_mmio_flip < 0)
  7826. return false;
  7827. else if (i915.use_mmio_flip > 0)
  7828. return true;
  7829. else if (i915.enable_execlists)
  7830. return true;
  7831. else
  7832. return ring != i915_gem_request_get_ring(obj->last_read_req);
  7833. }
  7834. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  7835. {
  7836. struct drm_device *dev = intel_crtc->base.dev;
  7837. struct drm_i915_private *dev_priv = dev->dev_private;
  7838. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  7839. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7840. struct drm_i915_gem_object *obj = intel_fb->obj;
  7841. const enum pipe pipe = intel_crtc->pipe;
  7842. u32 ctl, stride;
  7843. ctl = I915_READ(PLANE_CTL(pipe, 0));
  7844. ctl &= ~PLANE_CTL_TILED_MASK;
  7845. if (obj->tiling_mode == I915_TILING_X)
  7846. ctl |= PLANE_CTL_TILED_X;
  7847. /*
  7848. * The stride is either expressed as a multiple of 64 bytes chunks for
  7849. * linear buffers or in number of tiles for tiled buffers.
  7850. */
  7851. stride = fb->pitches[0] >> 6;
  7852. if (obj->tiling_mode == I915_TILING_X)
  7853. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  7854. /*
  7855. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  7856. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  7857. */
  7858. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  7859. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  7860. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  7861. POSTING_READ(PLANE_SURF(pipe, 0));
  7862. }
  7863. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  7864. {
  7865. struct drm_device *dev = intel_crtc->base.dev;
  7866. struct drm_i915_private *dev_priv = dev->dev_private;
  7867. struct intel_framebuffer *intel_fb =
  7868. to_intel_framebuffer(intel_crtc->base.primary->fb);
  7869. struct drm_i915_gem_object *obj = intel_fb->obj;
  7870. u32 dspcntr;
  7871. u32 reg;
  7872. reg = DSPCNTR(intel_crtc->plane);
  7873. dspcntr = I915_READ(reg);
  7874. if (obj->tiling_mode != I915_TILING_NONE)
  7875. dspcntr |= DISPPLANE_TILED;
  7876. else
  7877. dspcntr &= ~DISPPLANE_TILED;
  7878. I915_WRITE(reg, dspcntr);
  7879. I915_WRITE(DSPSURF(intel_crtc->plane),
  7880. intel_crtc->unpin_work->gtt_offset);
  7881. POSTING_READ(DSPSURF(intel_crtc->plane));
  7882. }
  7883. /*
  7884. * XXX: This is the temporary way to update the plane registers until we get
  7885. * around to using the usual plane update functions for MMIO flips
  7886. */
  7887. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  7888. {
  7889. struct drm_device *dev = intel_crtc->base.dev;
  7890. bool atomic_update;
  7891. u32 start_vbl_count;
  7892. intel_mark_page_flip_active(intel_crtc);
  7893. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  7894. if (INTEL_INFO(dev)->gen >= 9)
  7895. skl_do_mmio_flip(intel_crtc);
  7896. else
  7897. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  7898. ilk_do_mmio_flip(intel_crtc);
  7899. if (atomic_update)
  7900. intel_pipe_update_end(intel_crtc, start_vbl_count);
  7901. }
  7902. static void intel_mmio_flip_work_func(struct work_struct *work)
  7903. {
  7904. struct intel_crtc *crtc =
  7905. container_of(work, struct intel_crtc, mmio_flip.work);
  7906. struct intel_mmio_flip *mmio_flip;
  7907. mmio_flip = &crtc->mmio_flip;
  7908. if (mmio_flip->req)
  7909. WARN_ON(__i915_wait_request(mmio_flip->req,
  7910. crtc->reset_counter,
  7911. false, NULL, NULL) != 0);
  7912. intel_do_mmio_flip(crtc);
  7913. if (mmio_flip->req) {
  7914. mutex_lock(&crtc->base.dev->struct_mutex);
  7915. i915_gem_request_assign(&mmio_flip->req, NULL);
  7916. mutex_unlock(&crtc->base.dev->struct_mutex);
  7917. }
  7918. }
  7919. static int intel_queue_mmio_flip(struct drm_device *dev,
  7920. struct drm_crtc *crtc,
  7921. struct drm_framebuffer *fb,
  7922. struct drm_i915_gem_object *obj,
  7923. struct intel_engine_cs *ring,
  7924. uint32_t flags)
  7925. {
  7926. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7927. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  7928. obj->last_write_req);
  7929. schedule_work(&intel_crtc->mmio_flip.work);
  7930. return 0;
  7931. }
  7932. static int intel_gen9_queue_flip(struct drm_device *dev,
  7933. struct drm_crtc *crtc,
  7934. struct drm_framebuffer *fb,
  7935. struct drm_i915_gem_object *obj,
  7936. struct intel_engine_cs *ring,
  7937. uint32_t flags)
  7938. {
  7939. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7940. uint32_t plane = 0, stride;
  7941. int ret;
  7942. switch(intel_crtc->pipe) {
  7943. case PIPE_A:
  7944. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_A;
  7945. break;
  7946. case PIPE_B:
  7947. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_B;
  7948. break;
  7949. case PIPE_C:
  7950. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_C;
  7951. break;
  7952. default:
  7953. WARN_ONCE(1, "unknown plane in flip command\n");
  7954. return -ENODEV;
  7955. }
  7956. switch (obj->tiling_mode) {
  7957. case I915_TILING_NONE:
  7958. stride = fb->pitches[0] >> 6;
  7959. break;
  7960. case I915_TILING_X:
  7961. stride = fb->pitches[0] >> 9;
  7962. break;
  7963. default:
  7964. WARN_ONCE(1, "unknown tiling in flip command\n");
  7965. return -ENODEV;
  7966. }
  7967. ret = intel_ring_begin(ring, 10);
  7968. if (ret)
  7969. return ret;
  7970. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7971. intel_ring_emit(ring, DERRMR);
  7972. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7973. DERRMR_PIPEB_PRI_FLIP_DONE |
  7974. DERRMR_PIPEC_PRI_FLIP_DONE));
  7975. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7976. MI_SRM_LRM_GLOBAL_GTT);
  7977. intel_ring_emit(ring, DERRMR);
  7978. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7979. intel_ring_emit(ring, 0);
  7980. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane);
  7981. intel_ring_emit(ring, stride << 6 | obj->tiling_mode);
  7982. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7983. intel_mark_page_flip_active(intel_crtc);
  7984. __intel_ring_advance(ring);
  7985. return 0;
  7986. }
  7987. static int intel_default_queue_flip(struct drm_device *dev,
  7988. struct drm_crtc *crtc,
  7989. struct drm_framebuffer *fb,
  7990. struct drm_i915_gem_object *obj,
  7991. struct intel_engine_cs *ring,
  7992. uint32_t flags)
  7993. {
  7994. return -ENODEV;
  7995. }
  7996. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  7997. struct drm_crtc *crtc)
  7998. {
  7999. struct drm_i915_private *dev_priv = dev->dev_private;
  8000. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8001. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8002. u32 addr;
  8003. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8004. return true;
  8005. if (!work->enable_stall_check)
  8006. return false;
  8007. if (work->flip_ready_vblank == 0) {
  8008. if (work->flip_queued_req &&
  8009. !i915_gem_request_completed(work->flip_queued_req, true))
  8010. return false;
  8011. work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8012. }
  8013. if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
  8014. return false;
  8015. /* Potential stall - if we see that the flip has happened,
  8016. * assume a missed interrupt. */
  8017. if (INTEL_INFO(dev)->gen >= 4)
  8018. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8019. else
  8020. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8021. /* There is a potential issue here with a false positive after a flip
  8022. * to the same address. We could address this by checking for a
  8023. * non-incrementing frame counter.
  8024. */
  8025. return addr == work->gtt_offset;
  8026. }
  8027. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8028. {
  8029. struct drm_i915_private *dev_priv = dev->dev_private;
  8030. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8031. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8032. WARN_ON(!in_irq());
  8033. if (crtc == NULL)
  8034. return;
  8035. spin_lock(&dev->event_lock);
  8036. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8037. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8038. intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  8039. page_flip_completed(intel_crtc);
  8040. }
  8041. spin_unlock(&dev->event_lock);
  8042. }
  8043. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8044. struct drm_framebuffer *fb,
  8045. struct drm_pending_vblank_event *event,
  8046. uint32_t page_flip_flags)
  8047. {
  8048. struct drm_device *dev = crtc->dev;
  8049. struct drm_i915_private *dev_priv = dev->dev_private;
  8050. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8051. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8052. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8053. struct drm_plane *primary = crtc->primary;
  8054. enum pipe pipe = intel_crtc->pipe;
  8055. struct intel_unpin_work *work;
  8056. struct intel_engine_cs *ring;
  8057. int ret;
  8058. /*
  8059. * drm_mode_page_flip_ioctl() should already catch this, but double
  8060. * check to be safe. In the future we may enable pageflipping from
  8061. * a disabled primary plane.
  8062. */
  8063. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8064. return -EBUSY;
  8065. /* Can't change pixel format via MI display flips. */
  8066. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8067. return -EINVAL;
  8068. /*
  8069. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8070. * Note that pitch changes could also affect these register.
  8071. */
  8072. if (INTEL_INFO(dev)->gen > 3 &&
  8073. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8074. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8075. return -EINVAL;
  8076. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8077. goto out_hang;
  8078. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8079. if (work == NULL)
  8080. return -ENOMEM;
  8081. work->event = event;
  8082. work->crtc = crtc;
  8083. work->old_fb_obj = intel_fb_obj(old_fb);
  8084. INIT_WORK(&work->work, intel_unpin_work_fn);
  8085. ret = drm_crtc_vblank_get(crtc);
  8086. if (ret)
  8087. goto free_work;
  8088. /* We borrow the event spin lock for protecting unpin_work */
  8089. spin_lock_irq(&dev->event_lock);
  8090. if (intel_crtc->unpin_work) {
  8091. /* Before declaring the flip queue wedged, check if
  8092. * the hardware completed the operation behind our backs.
  8093. */
  8094. if (__intel_pageflip_stall_check(dev, crtc)) {
  8095. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8096. page_flip_completed(intel_crtc);
  8097. } else {
  8098. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8099. spin_unlock_irq(&dev->event_lock);
  8100. drm_crtc_vblank_put(crtc);
  8101. kfree(work);
  8102. return -EBUSY;
  8103. }
  8104. }
  8105. intel_crtc->unpin_work = work;
  8106. spin_unlock_irq(&dev->event_lock);
  8107. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8108. flush_workqueue(dev_priv->wq);
  8109. ret = i915_mutex_lock_interruptible(dev);
  8110. if (ret)
  8111. goto cleanup;
  8112. /* Reference the objects for the scheduled work. */
  8113. drm_gem_object_reference(&work->old_fb_obj->base);
  8114. drm_gem_object_reference(&obj->base);
  8115. crtc->primary->fb = fb;
  8116. work->pending_flip_obj = obj;
  8117. atomic_inc(&intel_crtc->unpin_work_count);
  8118. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8119. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8120. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8121. if (IS_VALLEYVIEW(dev)) {
  8122. ring = &dev_priv->ring[BCS];
  8123. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8124. /* vlv: DISPLAY_FLIP fails to change tiling */
  8125. ring = NULL;
  8126. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  8127. ring = &dev_priv->ring[BCS];
  8128. } else if (INTEL_INFO(dev)->gen >= 7) {
  8129. ring = i915_gem_request_get_ring(obj->last_read_req);
  8130. if (ring == NULL || ring->id != RCS)
  8131. ring = &dev_priv->ring[BCS];
  8132. } else {
  8133. ring = &dev_priv->ring[RCS];
  8134. }
  8135. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
  8136. if (ret)
  8137. goto cleanup_pending;
  8138. work->gtt_offset =
  8139. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8140. if (use_mmio_flip(ring, obj)) {
  8141. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8142. page_flip_flags);
  8143. if (ret)
  8144. goto cleanup_unpin;
  8145. i915_gem_request_assign(&work->flip_queued_req,
  8146. obj->last_write_req);
  8147. } else {
  8148. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8149. page_flip_flags);
  8150. if (ret)
  8151. goto cleanup_unpin;
  8152. i915_gem_request_assign(&work->flip_queued_req,
  8153. intel_ring_get_request(ring));
  8154. }
  8155. work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8156. work->enable_stall_check = true;
  8157. i915_gem_track_fb(work->old_fb_obj, obj,
  8158. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8159. intel_fbc_disable(dev);
  8160. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8161. mutex_unlock(&dev->struct_mutex);
  8162. trace_i915_flip_request(intel_crtc->plane, obj);
  8163. return 0;
  8164. cleanup_unpin:
  8165. intel_unpin_fb_obj(obj);
  8166. cleanup_pending:
  8167. atomic_dec(&intel_crtc->unpin_work_count);
  8168. crtc->primary->fb = old_fb;
  8169. drm_gem_object_unreference(&work->old_fb_obj->base);
  8170. drm_gem_object_unreference(&obj->base);
  8171. mutex_unlock(&dev->struct_mutex);
  8172. cleanup:
  8173. spin_lock_irq(&dev->event_lock);
  8174. intel_crtc->unpin_work = NULL;
  8175. spin_unlock_irq(&dev->event_lock);
  8176. drm_crtc_vblank_put(crtc);
  8177. free_work:
  8178. kfree(work);
  8179. if (ret == -EIO) {
  8180. out_hang:
  8181. ret = intel_plane_restore(primary);
  8182. if (ret == 0 && event) {
  8183. spin_lock_irq(&dev->event_lock);
  8184. drm_send_vblank_event(dev, pipe, event);
  8185. spin_unlock_irq(&dev->event_lock);
  8186. }
  8187. }
  8188. return ret;
  8189. }
  8190. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8191. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8192. .load_lut = intel_crtc_load_lut,
  8193. .atomic_begin = intel_begin_crtc_commit,
  8194. .atomic_flush = intel_finish_crtc_commit,
  8195. };
  8196. /**
  8197. * intel_modeset_update_staged_output_state
  8198. *
  8199. * Updates the staged output configuration state, e.g. after we've read out the
  8200. * current hw state.
  8201. */
  8202. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8203. {
  8204. struct intel_crtc *crtc;
  8205. struct intel_encoder *encoder;
  8206. struct intel_connector *connector;
  8207. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8208. base.head) {
  8209. connector->new_encoder =
  8210. to_intel_encoder(connector->base.encoder);
  8211. }
  8212. for_each_intel_encoder(dev, encoder) {
  8213. encoder->new_crtc =
  8214. to_intel_crtc(encoder->base.crtc);
  8215. }
  8216. for_each_intel_crtc(dev, crtc) {
  8217. crtc->new_enabled = crtc->base.enabled;
  8218. if (crtc->new_enabled)
  8219. crtc->new_config = &crtc->config;
  8220. else
  8221. crtc->new_config = NULL;
  8222. }
  8223. }
  8224. /**
  8225. * intel_modeset_commit_output_state
  8226. *
  8227. * This function copies the stage display pipe configuration to the real one.
  8228. */
  8229. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8230. {
  8231. struct intel_crtc *crtc;
  8232. struct intel_encoder *encoder;
  8233. struct intel_connector *connector;
  8234. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8235. base.head) {
  8236. connector->base.encoder = &connector->new_encoder->base;
  8237. }
  8238. for_each_intel_encoder(dev, encoder) {
  8239. encoder->base.crtc = &encoder->new_crtc->base;
  8240. }
  8241. for_each_intel_crtc(dev, crtc) {
  8242. crtc->base.enabled = crtc->new_enabled;
  8243. }
  8244. }
  8245. static void
  8246. connected_sink_compute_bpp(struct intel_connector *connector,
  8247. struct intel_crtc_state *pipe_config)
  8248. {
  8249. int bpp = pipe_config->pipe_bpp;
  8250. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8251. connector->base.base.id,
  8252. connector->base.name);
  8253. /* Don't use an invalid EDID bpc value */
  8254. if (connector->base.display_info.bpc &&
  8255. connector->base.display_info.bpc * 3 < bpp) {
  8256. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8257. bpp, connector->base.display_info.bpc*3);
  8258. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8259. }
  8260. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8261. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8262. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8263. bpp);
  8264. pipe_config->pipe_bpp = 24;
  8265. }
  8266. }
  8267. static int
  8268. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8269. struct drm_framebuffer *fb,
  8270. struct intel_crtc_state *pipe_config)
  8271. {
  8272. struct drm_device *dev = crtc->base.dev;
  8273. struct intel_connector *connector;
  8274. int bpp;
  8275. switch (fb->pixel_format) {
  8276. case DRM_FORMAT_C8:
  8277. bpp = 8*3; /* since we go through a colormap */
  8278. break;
  8279. case DRM_FORMAT_XRGB1555:
  8280. case DRM_FORMAT_ARGB1555:
  8281. /* checked in intel_framebuffer_init already */
  8282. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8283. return -EINVAL;
  8284. case DRM_FORMAT_RGB565:
  8285. bpp = 6*3; /* min is 18bpp */
  8286. break;
  8287. case DRM_FORMAT_XBGR8888:
  8288. case DRM_FORMAT_ABGR8888:
  8289. /* checked in intel_framebuffer_init already */
  8290. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8291. return -EINVAL;
  8292. case DRM_FORMAT_XRGB8888:
  8293. case DRM_FORMAT_ARGB8888:
  8294. bpp = 8*3;
  8295. break;
  8296. case DRM_FORMAT_XRGB2101010:
  8297. case DRM_FORMAT_ARGB2101010:
  8298. case DRM_FORMAT_XBGR2101010:
  8299. case DRM_FORMAT_ABGR2101010:
  8300. /* checked in intel_framebuffer_init already */
  8301. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8302. return -EINVAL;
  8303. bpp = 10*3;
  8304. break;
  8305. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8306. default:
  8307. DRM_DEBUG_KMS("unsupported depth\n");
  8308. return -EINVAL;
  8309. }
  8310. pipe_config->pipe_bpp = bpp;
  8311. /* Clamp display bpp to EDID value */
  8312. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8313. base.head) {
  8314. if (!connector->new_encoder ||
  8315. connector->new_encoder->new_crtc != crtc)
  8316. continue;
  8317. connected_sink_compute_bpp(connector, pipe_config);
  8318. }
  8319. return bpp;
  8320. }
  8321. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8322. {
  8323. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8324. "type: 0x%x flags: 0x%x\n",
  8325. mode->crtc_clock,
  8326. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8327. mode->crtc_hsync_end, mode->crtc_htotal,
  8328. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8329. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8330. }
  8331. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8332. struct intel_crtc_state *pipe_config,
  8333. const char *context)
  8334. {
  8335. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8336. context, pipe_name(crtc->pipe));
  8337. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8338. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8339. pipe_config->pipe_bpp, pipe_config->dither);
  8340. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8341. pipe_config->has_pch_encoder,
  8342. pipe_config->fdi_lanes,
  8343. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8344. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8345. pipe_config->fdi_m_n.tu);
  8346. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8347. pipe_config->has_dp_encoder,
  8348. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8349. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8350. pipe_config->dp_m_n.tu);
  8351. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8352. pipe_config->has_dp_encoder,
  8353. pipe_config->dp_m2_n2.gmch_m,
  8354. pipe_config->dp_m2_n2.gmch_n,
  8355. pipe_config->dp_m2_n2.link_m,
  8356. pipe_config->dp_m2_n2.link_n,
  8357. pipe_config->dp_m2_n2.tu);
  8358. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8359. pipe_config->has_audio,
  8360. pipe_config->has_infoframe);
  8361. DRM_DEBUG_KMS("requested mode:\n");
  8362. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8363. DRM_DEBUG_KMS("adjusted mode:\n");
  8364. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8365. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8366. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8367. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8368. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8369. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8370. pipe_config->gmch_pfit.control,
  8371. pipe_config->gmch_pfit.pgm_ratios,
  8372. pipe_config->gmch_pfit.lvds_border_bits);
  8373. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8374. pipe_config->pch_pfit.pos,
  8375. pipe_config->pch_pfit.size,
  8376. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8377. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8378. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8379. }
  8380. static bool encoders_cloneable(const struct intel_encoder *a,
  8381. const struct intel_encoder *b)
  8382. {
  8383. /* masks could be asymmetric, so check both ways */
  8384. return a == b || (a->cloneable & (1 << b->type) &&
  8385. b->cloneable & (1 << a->type));
  8386. }
  8387. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8388. struct intel_encoder *encoder)
  8389. {
  8390. struct drm_device *dev = crtc->base.dev;
  8391. struct intel_encoder *source_encoder;
  8392. for_each_intel_encoder(dev, source_encoder) {
  8393. if (source_encoder->new_crtc != crtc)
  8394. continue;
  8395. if (!encoders_cloneable(encoder, source_encoder))
  8396. return false;
  8397. }
  8398. return true;
  8399. }
  8400. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8401. {
  8402. struct drm_device *dev = crtc->base.dev;
  8403. struct intel_encoder *encoder;
  8404. for_each_intel_encoder(dev, encoder) {
  8405. if (encoder->new_crtc != crtc)
  8406. continue;
  8407. if (!check_single_encoder_cloning(crtc, encoder))
  8408. return false;
  8409. }
  8410. return true;
  8411. }
  8412. static bool check_digital_port_conflicts(struct drm_device *dev)
  8413. {
  8414. struct intel_connector *connector;
  8415. unsigned int used_ports = 0;
  8416. /*
  8417. * Walk the connector list instead of the encoder
  8418. * list to detect the problem on ddi platforms
  8419. * where there's just one encoder per digital port.
  8420. */
  8421. list_for_each_entry(connector,
  8422. &dev->mode_config.connector_list, base.head) {
  8423. struct intel_encoder *encoder = connector->new_encoder;
  8424. if (!encoder)
  8425. continue;
  8426. WARN_ON(!encoder->new_crtc);
  8427. switch (encoder->type) {
  8428. unsigned int port_mask;
  8429. case INTEL_OUTPUT_UNKNOWN:
  8430. if (WARN_ON(!HAS_DDI(dev)))
  8431. break;
  8432. case INTEL_OUTPUT_DISPLAYPORT:
  8433. case INTEL_OUTPUT_HDMI:
  8434. case INTEL_OUTPUT_EDP:
  8435. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8436. /* the same port mustn't appear more than once */
  8437. if (used_ports & port_mask)
  8438. return false;
  8439. used_ports |= port_mask;
  8440. default:
  8441. break;
  8442. }
  8443. }
  8444. return true;
  8445. }
  8446. static struct intel_crtc_state *
  8447. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8448. struct drm_framebuffer *fb,
  8449. struct drm_display_mode *mode)
  8450. {
  8451. struct drm_device *dev = crtc->dev;
  8452. struct intel_encoder *encoder;
  8453. struct intel_crtc_state *pipe_config;
  8454. int plane_bpp, ret = -EINVAL;
  8455. bool retry = true;
  8456. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8457. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8458. return ERR_PTR(-EINVAL);
  8459. }
  8460. if (!check_digital_port_conflicts(dev)) {
  8461. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8462. return ERR_PTR(-EINVAL);
  8463. }
  8464. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8465. if (!pipe_config)
  8466. return ERR_PTR(-ENOMEM);
  8467. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  8468. drm_mode_copy(&pipe_config->base.mode, mode);
  8469. pipe_config->cpu_transcoder =
  8470. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8471. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8472. /*
  8473. * Sanitize sync polarity flags based on requested ones. If neither
  8474. * positive or negative polarity is requested, treat this as meaning
  8475. * negative polarity.
  8476. */
  8477. if (!(pipe_config->base.adjusted_mode.flags &
  8478. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8479. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8480. if (!(pipe_config->base.adjusted_mode.flags &
  8481. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8482. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8483. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8484. * plane pixel format and any sink constraints into account. Returns the
  8485. * source plane bpp so that dithering can be selected on mismatches
  8486. * after encoders and crtc also have had their say. */
  8487. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8488. fb, pipe_config);
  8489. if (plane_bpp < 0)
  8490. goto fail;
  8491. /*
  8492. * Determine the real pipe dimensions. Note that stereo modes can
  8493. * increase the actual pipe size due to the frame doubling and
  8494. * insertion of additional space for blanks between the frame. This
  8495. * is stored in the crtc timings. We use the requested mode to do this
  8496. * computation to clearly distinguish it from the adjusted mode, which
  8497. * can be changed by the connectors in the below retry loop.
  8498. */
  8499. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  8500. &pipe_config->pipe_src_w,
  8501. &pipe_config->pipe_src_h);
  8502. encoder_retry:
  8503. /* Ensure the port clock defaults are reset when retrying. */
  8504. pipe_config->port_clock = 0;
  8505. pipe_config->pixel_multiplier = 1;
  8506. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8507. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  8508. CRTC_STEREO_DOUBLE);
  8509. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8510. * adjust it according to limitations or connector properties, and also
  8511. * a chance to reject the mode entirely.
  8512. */
  8513. for_each_intel_encoder(dev, encoder) {
  8514. if (&encoder->new_crtc->base != crtc)
  8515. continue;
  8516. if (!(encoder->compute_config(encoder, pipe_config))) {
  8517. DRM_DEBUG_KMS("Encoder config failure\n");
  8518. goto fail;
  8519. }
  8520. }
  8521. /* Set default port clock if not overwritten by the encoder. Needs to be
  8522. * done afterwards in case the encoder adjusts the mode. */
  8523. if (!pipe_config->port_clock)
  8524. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  8525. * pipe_config->pixel_multiplier;
  8526. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8527. if (ret < 0) {
  8528. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8529. goto fail;
  8530. }
  8531. if (ret == RETRY) {
  8532. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8533. ret = -EINVAL;
  8534. goto fail;
  8535. }
  8536. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8537. retry = false;
  8538. goto encoder_retry;
  8539. }
  8540. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8541. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8542. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8543. return pipe_config;
  8544. fail:
  8545. kfree(pipe_config);
  8546. return ERR_PTR(ret);
  8547. }
  8548. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8549. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8550. static void
  8551. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8552. unsigned *prepare_pipes, unsigned *disable_pipes)
  8553. {
  8554. struct intel_crtc *intel_crtc;
  8555. struct drm_device *dev = crtc->dev;
  8556. struct intel_encoder *encoder;
  8557. struct intel_connector *connector;
  8558. struct drm_crtc *tmp_crtc;
  8559. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8560. /* Check which crtcs have changed outputs connected to them, these need
  8561. * to be part of the prepare_pipes mask. We don't (yet) support global
  8562. * modeset across multiple crtcs, so modeset_pipes will only have one
  8563. * bit set at most. */
  8564. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8565. base.head) {
  8566. if (connector->base.encoder == &connector->new_encoder->base)
  8567. continue;
  8568. if (connector->base.encoder) {
  8569. tmp_crtc = connector->base.encoder->crtc;
  8570. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8571. }
  8572. if (connector->new_encoder)
  8573. *prepare_pipes |=
  8574. 1 << connector->new_encoder->new_crtc->pipe;
  8575. }
  8576. for_each_intel_encoder(dev, encoder) {
  8577. if (encoder->base.crtc == &encoder->new_crtc->base)
  8578. continue;
  8579. if (encoder->base.crtc) {
  8580. tmp_crtc = encoder->base.crtc;
  8581. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8582. }
  8583. if (encoder->new_crtc)
  8584. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8585. }
  8586. /* Check for pipes that will be enabled/disabled ... */
  8587. for_each_intel_crtc(dev, intel_crtc) {
  8588. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8589. continue;
  8590. if (!intel_crtc->new_enabled)
  8591. *disable_pipes |= 1 << intel_crtc->pipe;
  8592. else
  8593. *prepare_pipes |= 1 << intel_crtc->pipe;
  8594. }
  8595. /* set_mode is also used to update properties on life display pipes. */
  8596. intel_crtc = to_intel_crtc(crtc);
  8597. if (intel_crtc->new_enabled)
  8598. *prepare_pipes |= 1 << intel_crtc->pipe;
  8599. /*
  8600. * For simplicity do a full modeset on any pipe where the output routing
  8601. * changed. We could be more clever, but that would require us to be
  8602. * more careful with calling the relevant encoder->mode_set functions.
  8603. */
  8604. if (*prepare_pipes)
  8605. *modeset_pipes = *prepare_pipes;
  8606. /* ... and mask these out. */
  8607. *modeset_pipes &= ~(*disable_pipes);
  8608. *prepare_pipes &= ~(*disable_pipes);
  8609. /*
  8610. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8611. * obies this rule, but the modeset restore mode of
  8612. * intel_modeset_setup_hw_state does not.
  8613. */
  8614. *modeset_pipes &= 1 << intel_crtc->pipe;
  8615. *prepare_pipes &= 1 << intel_crtc->pipe;
  8616. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8617. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8618. }
  8619. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8620. {
  8621. struct drm_encoder *encoder;
  8622. struct drm_device *dev = crtc->dev;
  8623. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8624. if (encoder->crtc == crtc)
  8625. return true;
  8626. return false;
  8627. }
  8628. static void
  8629. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8630. {
  8631. struct drm_i915_private *dev_priv = dev->dev_private;
  8632. struct intel_encoder *intel_encoder;
  8633. struct intel_crtc *intel_crtc;
  8634. struct drm_connector *connector;
  8635. intel_shared_dpll_commit(dev_priv);
  8636. for_each_intel_encoder(dev, intel_encoder) {
  8637. if (!intel_encoder->base.crtc)
  8638. continue;
  8639. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8640. if (prepare_pipes & (1 << intel_crtc->pipe))
  8641. intel_encoder->connectors_active = false;
  8642. }
  8643. intel_modeset_commit_output_state(dev);
  8644. /* Double check state. */
  8645. for_each_intel_crtc(dev, intel_crtc) {
  8646. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8647. WARN_ON(intel_crtc->new_config &&
  8648. intel_crtc->new_config != &intel_crtc->config);
  8649. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8650. }
  8651. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8652. if (!connector->encoder || !connector->encoder->crtc)
  8653. continue;
  8654. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8655. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8656. struct drm_property *dpms_property =
  8657. dev->mode_config.dpms_property;
  8658. connector->dpms = DRM_MODE_DPMS_ON;
  8659. drm_object_property_set_value(&connector->base,
  8660. dpms_property,
  8661. DRM_MODE_DPMS_ON);
  8662. intel_encoder = to_intel_encoder(connector->encoder);
  8663. intel_encoder->connectors_active = true;
  8664. }
  8665. }
  8666. }
  8667. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8668. {
  8669. int diff;
  8670. if (clock1 == clock2)
  8671. return true;
  8672. if (!clock1 || !clock2)
  8673. return false;
  8674. diff = abs(clock1 - clock2);
  8675. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8676. return true;
  8677. return false;
  8678. }
  8679. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8680. list_for_each_entry((intel_crtc), \
  8681. &(dev)->mode_config.crtc_list, \
  8682. base.head) \
  8683. if (mask & (1 <<(intel_crtc)->pipe))
  8684. static bool
  8685. intel_pipe_config_compare(struct drm_device *dev,
  8686. struct intel_crtc_state *current_config,
  8687. struct intel_crtc_state *pipe_config)
  8688. {
  8689. #define PIPE_CONF_CHECK_X(name) \
  8690. if (current_config->name != pipe_config->name) { \
  8691. DRM_ERROR("mismatch in " #name " " \
  8692. "(expected 0x%08x, found 0x%08x)\n", \
  8693. current_config->name, \
  8694. pipe_config->name); \
  8695. return false; \
  8696. }
  8697. #define PIPE_CONF_CHECK_I(name) \
  8698. if (current_config->name != pipe_config->name) { \
  8699. DRM_ERROR("mismatch in " #name " " \
  8700. "(expected %i, found %i)\n", \
  8701. current_config->name, \
  8702. pipe_config->name); \
  8703. return false; \
  8704. }
  8705. /* This is required for BDW+ where there is only one set of registers for
  8706. * switching between high and low RR.
  8707. * This macro can be used whenever a comparison has to be made between one
  8708. * hw state and multiple sw state variables.
  8709. */
  8710. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8711. if ((current_config->name != pipe_config->name) && \
  8712. (current_config->alt_name != pipe_config->name)) { \
  8713. DRM_ERROR("mismatch in " #name " " \
  8714. "(expected %i or %i, found %i)\n", \
  8715. current_config->name, \
  8716. current_config->alt_name, \
  8717. pipe_config->name); \
  8718. return false; \
  8719. }
  8720. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8721. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8722. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8723. "(expected %i, found %i)\n", \
  8724. current_config->name & (mask), \
  8725. pipe_config->name & (mask)); \
  8726. return false; \
  8727. }
  8728. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8729. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8730. DRM_ERROR("mismatch in " #name " " \
  8731. "(expected %i, found %i)\n", \
  8732. current_config->name, \
  8733. pipe_config->name); \
  8734. return false; \
  8735. }
  8736. #define PIPE_CONF_QUIRK(quirk) \
  8737. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8738. PIPE_CONF_CHECK_I(cpu_transcoder);
  8739. PIPE_CONF_CHECK_I(has_pch_encoder);
  8740. PIPE_CONF_CHECK_I(fdi_lanes);
  8741. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8742. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8743. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8744. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8745. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8746. PIPE_CONF_CHECK_I(has_dp_encoder);
  8747. if (INTEL_INFO(dev)->gen < 8) {
  8748. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8749. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8750. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8751. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8752. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8753. if (current_config->has_drrs) {
  8754. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8755. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8756. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8757. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8758. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8759. }
  8760. } else {
  8761. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8762. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8763. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8764. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8765. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8766. }
  8767. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  8768. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  8769. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  8770. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  8771. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  8772. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  8773. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  8774. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  8775. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  8776. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  8777. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  8778. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  8779. PIPE_CONF_CHECK_I(pixel_multiplier);
  8780. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8781. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8782. IS_VALLEYVIEW(dev))
  8783. PIPE_CONF_CHECK_I(limited_color_range);
  8784. PIPE_CONF_CHECK_I(has_infoframe);
  8785. PIPE_CONF_CHECK_I(has_audio);
  8786. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8787. DRM_MODE_FLAG_INTERLACE);
  8788. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8789. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8790. DRM_MODE_FLAG_PHSYNC);
  8791. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8792. DRM_MODE_FLAG_NHSYNC);
  8793. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8794. DRM_MODE_FLAG_PVSYNC);
  8795. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8796. DRM_MODE_FLAG_NVSYNC);
  8797. }
  8798. PIPE_CONF_CHECK_I(pipe_src_w);
  8799. PIPE_CONF_CHECK_I(pipe_src_h);
  8800. /*
  8801. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8802. * screen. Since we don't yet re-compute the pipe config when moving
  8803. * just the lvds port away to another pipe the sw tracking won't match.
  8804. *
  8805. * Proper atomic modesets with recomputed global state will fix this.
  8806. * Until then just don't check gmch state for inherited modes.
  8807. */
  8808. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8809. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8810. /* pfit ratios are autocomputed by the hw on gen4+ */
  8811. if (INTEL_INFO(dev)->gen < 4)
  8812. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8813. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8814. }
  8815. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8816. if (current_config->pch_pfit.enabled) {
  8817. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8818. PIPE_CONF_CHECK_I(pch_pfit.size);
  8819. }
  8820. /* BDW+ don't expose a synchronous way to read the state */
  8821. if (IS_HASWELL(dev))
  8822. PIPE_CONF_CHECK_I(ips_enabled);
  8823. PIPE_CONF_CHECK_I(double_wide);
  8824. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8825. PIPE_CONF_CHECK_I(shared_dpll);
  8826. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8827. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8828. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8829. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8830. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8831. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  8832. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  8833. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  8834. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8835. PIPE_CONF_CHECK_I(pipe_bpp);
  8836. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  8837. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8838. #undef PIPE_CONF_CHECK_X
  8839. #undef PIPE_CONF_CHECK_I
  8840. #undef PIPE_CONF_CHECK_I_ALT
  8841. #undef PIPE_CONF_CHECK_FLAGS
  8842. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8843. #undef PIPE_CONF_QUIRK
  8844. return true;
  8845. }
  8846. static void check_wm_state(struct drm_device *dev)
  8847. {
  8848. struct drm_i915_private *dev_priv = dev->dev_private;
  8849. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  8850. struct intel_crtc *intel_crtc;
  8851. int plane;
  8852. if (INTEL_INFO(dev)->gen < 9)
  8853. return;
  8854. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  8855. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  8856. for_each_intel_crtc(dev, intel_crtc) {
  8857. struct skl_ddb_entry *hw_entry, *sw_entry;
  8858. const enum pipe pipe = intel_crtc->pipe;
  8859. if (!intel_crtc->active)
  8860. continue;
  8861. /* planes */
  8862. for_each_plane(pipe, plane) {
  8863. hw_entry = &hw_ddb.plane[pipe][plane];
  8864. sw_entry = &sw_ddb->plane[pipe][plane];
  8865. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8866. continue;
  8867. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  8868. "(expected (%u,%u), found (%u,%u))\n",
  8869. pipe_name(pipe), plane + 1,
  8870. sw_entry->start, sw_entry->end,
  8871. hw_entry->start, hw_entry->end);
  8872. }
  8873. /* cursor */
  8874. hw_entry = &hw_ddb.cursor[pipe];
  8875. sw_entry = &sw_ddb->cursor[pipe];
  8876. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8877. continue;
  8878. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  8879. "(expected (%u,%u), found (%u,%u))\n",
  8880. pipe_name(pipe),
  8881. sw_entry->start, sw_entry->end,
  8882. hw_entry->start, hw_entry->end);
  8883. }
  8884. }
  8885. static void
  8886. check_connector_state(struct drm_device *dev)
  8887. {
  8888. struct intel_connector *connector;
  8889. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8890. base.head) {
  8891. /* This also checks the encoder/connector hw state with the
  8892. * ->get_hw_state callbacks. */
  8893. intel_connector_check_state(connector);
  8894. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  8895. "connector's staged encoder doesn't match current encoder\n");
  8896. }
  8897. }
  8898. static void
  8899. check_encoder_state(struct drm_device *dev)
  8900. {
  8901. struct intel_encoder *encoder;
  8902. struct intel_connector *connector;
  8903. for_each_intel_encoder(dev, encoder) {
  8904. bool enabled = false;
  8905. bool active = false;
  8906. enum pipe pipe, tracked_pipe;
  8907. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8908. encoder->base.base.id,
  8909. encoder->base.name);
  8910. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8911. "encoder's stage crtc doesn't match current crtc\n");
  8912. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  8913. "encoder's active_connectors set, but no crtc\n");
  8914. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8915. base.head) {
  8916. if (connector->base.encoder != &encoder->base)
  8917. continue;
  8918. enabled = true;
  8919. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8920. active = true;
  8921. }
  8922. /*
  8923. * for MST connectors if we unplug the connector is gone
  8924. * away but the encoder is still connected to a crtc
  8925. * until a modeset happens in response to the hotplug.
  8926. */
  8927. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  8928. continue;
  8929. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  8930. "encoder's enabled state mismatch "
  8931. "(expected %i, found %i)\n",
  8932. !!encoder->base.crtc, enabled);
  8933. I915_STATE_WARN(active && !encoder->base.crtc,
  8934. "active encoder with no crtc\n");
  8935. I915_STATE_WARN(encoder->connectors_active != active,
  8936. "encoder's computed active state doesn't match tracked active state "
  8937. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8938. active = encoder->get_hw_state(encoder, &pipe);
  8939. I915_STATE_WARN(active != encoder->connectors_active,
  8940. "encoder's hw state doesn't match sw tracking "
  8941. "(expected %i, found %i)\n",
  8942. encoder->connectors_active, active);
  8943. if (!encoder->base.crtc)
  8944. continue;
  8945. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8946. I915_STATE_WARN(active && pipe != tracked_pipe,
  8947. "active encoder's pipe doesn't match"
  8948. "(expected %i, found %i)\n",
  8949. tracked_pipe, pipe);
  8950. }
  8951. }
  8952. static void
  8953. check_crtc_state(struct drm_device *dev)
  8954. {
  8955. struct drm_i915_private *dev_priv = dev->dev_private;
  8956. struct intel_crtc *crtc;
  8957. struct intel_encoder *encoder;
  8958. struct intel_crtc_state pipe_config;
  8959. for_each_intel_crtc(dev, crtc) {
  8960. bool enabled = false;
  8961. bool active = false;
  8962. memset(&pipe_config, 0, sizeof(pipe_config));
  8963. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8964. crtc->base.base.id);
  8965. I915_STATE_WARN(crtc->active && !crtc->base.enabled,
  8966. "active crtc, but not enabled in sw tracking\n");
  8967. for_each_intel_encoder(dev, encoder) {
  8968. if (encoder->base.crtc != &crtc->base)
  8969. continue;
  8970. enabled = true;
  8971. if (encoder->connectors_active)
  8972. active = true;
  8973. }
  8974. I915_STATE_WARN(active != crtc->active,
  8975. "crtc's computed active state doesn't match tracked active state "
  8976. "(expected %i, found %i)\n", active, crtc->active);
  8977. I915_STATE_WARN(enabled != crtc->base.enabled,
  8978. "crtc's computed enabled state doesn't match tracked enabled state "
  8979. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8980. active = dev_priv->display.get_pipe_config(crtc,
  8981. &pipe_config);
  8982. /* hw state is inconsistent with the pipe quirk */
  8983. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  8984. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  8985. active = crtc->active;
  8986. for_each_intel_encoder(dev, encoder) {
  8987. enum pipe pipe;
  8988. if (encoder->base.crtc != &crtc->base)
  8989. continue;
  8990. if (encoder->get_hw_state(encoder, &pipe))
  8991. encoder->get_config(encoder, &pipe_config);
  8992. }
  8993. I915_STATE_WARN(crtc->active != active,
  8994. "crtc active state doesn't match with hw state "
  8995. "(expected %i, found %i)\n", crtc->active, active);
  8996. if (active &&
  8997. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  8998. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  8999. intel_dump_pipe_config(crtc, &pipe_config,
  9000. "[hw state]");
  9001. intel_dump_pipe_config(crtc, &crtc->config,
  9002. "[sw state]");
  9003. }
  9004. }
  9005. }
  9006. static void
  9007. check_shared_dpll_state(struct drm_device *dev)
  9008. {
  9009. struct drm_i915_private *dev_priv = dev->dev_private;
  9010. struct intel_crtc *crtc;
  9011. struct intel_dpll_hw_state dpll_hw_state;
  9012. int i;
  9013. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9014. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9015. int enabled_crtcs = 0, active_crtcs = 0;
  9016. bool active;
  9017. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9018. DRM_DEBUG_KMS("%s\n", pll->name);
  9019. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9020. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  9021. "more active pll users than references: %i vs %i\n",
  9022. pll->active, hweight32(pll->config.crtc_mask));
  9023. I915_STATE_WARN(pll->active && !pll->on,
  9024. "pll in active use but not on in sw tracking\n");
  9025. I915_STATE_WARN(pll->on && !pll->active,
  9026. "pll in on but not on in use in sw tracking\n");
  9027. I915_STATE_WARN(pll->on != active,
  9028. "pll on state mismatch (expected %i, found %i)\n",
  9029. pll->on, active);
  9030. for_each_intel_crtc(dev, crtc) {
  9031. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9032. enabled_crtcs++;
  9033. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9034. active_crtcs++;
  9035. }
  9036. I915_STATE_WARN(pll->active != active_crtcs,
  9037. "pll active crtcs mismatch (expected %i, found %i)\n",
  9038. pll->active, active_crtcs);
  9039. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9040. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9041. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9042. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9043. sizeof(dpll_hw_state)),
  9044. "pll hw state mismatch\n");
  9045. }
  9046. }
  9047. void
  9048. intel_modeset_check_state(struct drm_device *dev)
  9049. {
  9050. check_wm_state(dev);
  9051. check_connector_state(dev);
  9052. check_encoder_state(dev);
  9053. check_crtc_state(dev);
  9054. check_shared_dpll_state(dev);
  9055. }
  9056. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  9057. int dotclock)
  9058. {
  9059. /*
  9060. * FDI already provided one idea for the dotclock.
  9061. * Yell if the encoder disagrees.
  9062. */
  9063. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  9064. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9065. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  9066. }
  9067. static void update_scanline_offset(struct intel_crtc *crtc)
  9068. {
  9069. struct drm_device *dev = crtc->base.dev;
  9070. /*
  9071. * The scanline counter increments at the leading edge of hsync.
  9072. *
  9073. * On most platforms it starts counting from vtotal-1 on the
  9074. * first active line. That means the scanline counter value is
  9075. * always one less than what we would expect. Ie. just after
  9076. * start of vblank, which also occurs at start of hsync (on the
  9077. * last active line), the scanline counter will read vblank_start-1.
  9078. *
  9079. * On gen2 the scanline counter starts counting from 1 instead
  9080. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9081. * to keep the value positive), instead of adding one.
  9082. *
  9083. * On HSW+ the behaviour of the scanline counter depends on the output
  9084. * type. For DP ports it behaves like most other platforms, but on HDMI
  9085. * there's an extra 1 line difference. So we need to add two instead of
  9086. * one to the value.
  9087. */
  9088. if (IS_GEN2(dev)) {
  9089. const struct drm_display_mode *mode = &crtc->config.base.adjusted_mode;
  9090. int vtotal;
  9091. vtotal = mode->crtc_vtotal;
  9092. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9093. vtotal /= 2;
  9094. crtc->scanline_offset = vtotal - 1;
  9095. } else if (HAS_DDI(dev) &&
  9096. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9097. crtc->scanline_offset = 2;
  9098. } else
  9099. crtc->scanline_offset = 1;
  9100. }
  9101. static struct intel_crtc_state *
  9102. intel_modeset_compute_config(struct drm_crtc *crtc,
  9103. struct drm_display_mode *mode,
  9104. struct drm_framebuffer *fb,
  9105. unsigned *modeset_pipes,
  9106. unsigned *prepare_pipes,
  9107. unsigned *disable_pipes)
  9108. {
  9109. struct intel_crtc_state *pipe_config = NULL;
  9110. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9111. prepare_pipes, disable_pipes);
  9112. if ((*modeset_pipes) == 0)
  9113. goto out;
  9114. /*
  9115. * Note this needs changes when we start tracking multiple modes
  9116. * and crtcs. At that point we'll need to compute the whole config
  9117. * (i.e. one pipe_config for each crtc) rather than just the one
  9118. * for this crtc.
  9119. */
  9120. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9121. if (IS_ERR(pipe_config)) {
  9122. goto out;
  9123. }
  9124. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9125. "[modeset]");
  9126. out:
  9127. return pipe_config;
  9128. }
  9129. static int __intel_set_mode(struct drm_crtc *crtc,
  9130. struct drm_display_mode *mode,
  9131. int x, int y, struct drm_framebuffer *fb,
  9132. struct intel_crtc_state *pipe_config,
  9133. unsigned modeset_pipes,
  9134. unsigned prepare_pipes,
  9135. unsigned disable_pipes)
  9136. {
  9137. struct drm_device *dev = crtc->dev;
  9138. struct drm_i915_private *dev_priv = dev->dev_private;
  9139. struct drm_display_mode *saved_mode;
  9140. struct intel_crtc *intel_crtc;
  9141. int ret = 0;
  9142. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9143. if (!saved_mode)
  9144. return -ENOMEM;
  9145. *saved_mode = crtc->mode;
  9146. if (modeset_pipes)
  9147. to_intel_crtc(crtc)->new_config = pipe_config;
  9148. /*
  9149. * See if the config requires any additional preparation, e.g.
  9150. * to adjust global state with pipes off. We need to do this
  9151. * here so we can get the modeset_pipe updated config for the new
  9152. * mode set on this crtc. For other crtcs we need to use the
  9153. * adjusted_mode bits in the crtc directly.
  9154. */
  9155. if (IS_VALLEYVIEW(dev)) {
  9156. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9157. /* may have added more to prepare_pipes than we should */
  9158. prepare_pipes &= ~disable_pipes;
  9159. }
  9160. if (dev_priv->display.crtc_compute_clock) {
  9161. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9162. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9163. if (ret)
  9164. goto done;
  9165. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9166. ret = dev_priv->display.crtc_compute_clock(intel_crtc);
  9167. if (ret) {
  9168. intel_shared_dpll_abort_config(dev_priv);
  9169. goto done;
  9170. }
  9171. }
  9172. }
  9173. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9174. intel_crtc_disable(&intel_crtc->base);
  9175. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9176. if (intel_crtc->base.enabled)
  9177. dev_priv->display.crtc_disable(&intel_crtc->base);
  9178. }
  9179. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9180. * to set it here already despite that we pass it down the callchain.
  9181. *
  9182. * Note we'll need to fix this up when we start tracking multiple
  9183. * pipes; here we assume a single modeset_pipe and only track the
  9184. * single crtc and mode.
  9185. */
  9186. if (modeset_pipes) {
  9187. crtc->mode = *mode;
  9188. /* mode_set/enable/disable functions rely on a correct pipe
  9189. * config. */
  9190. to_intel_crtc(crtc)->config = *pipe_config;
  9191. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  9192. /*
  9193. * Calculate and store various constants which
  9194. * are later needed by vblank and swap-completion
  9195. * timestamping. They are derived from true hwmode.
  9196. */
  9197. drm_calc_timestamping_constants(crtc,
  9198. &pipe_config->base.adjusted_mode);
  9199. }
  9200. /* Only after disabling all output pipelines that will be changed can we
  9201. * update the the output configuration. */
  9202. intel_modeset_update_state(dev, prepare_pipes);
  9203. modeset_update_crtc_power_domains(dev);
  9204. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9205. * on the DPLL.
  9206. */
  9207. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9208. struct drm_plane *primary = intel_crtc->base.primary;
  9209. int vdisplay, hdisplay;
  9210. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9211. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9212. fb, 0, 0,
  9213. hdisplay, vdisplay,
  9214. x << 16, y << 16,
  9215. hdisplay << 16, vdisplay << 16);
  9216. }
  9217. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9218. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9219. update_scanline_offset(intel_crtc);
  9220. dev_priv->display.crtc_enable(&intel_crtc->base);
  9221. }
  9222. /* FIXME: add subpixel order */
  9223. done:
  9224. if (ret && crtc->enabled)
  9225. crtc->mode = *saved_mode;
  9226. kfree(pipe_config);
  9227. kfree(saved_mode);
  9228. return ret;
  9229. }
  9230. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9231. struct drm_display_mode *mode,
  9232. int x, int y, struct drm_framebuffer *fb,
  9233. struct intel_crtc_state *pipe_config,
  9234. unsigned modeset_pipes,
  9235. unsigned prepare_pipes,
  9236. unsigned disable_pipes)
  9237. {
  9238. int ret;
  9239. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9240. prepare_pipes, disable_pipes);
  9241. if (ret == 0)
  9242. intel_modeset_check_state(crtc->dev);
  9243. return ret;
  9244. }
  9245. static int intel_set_mode(struct drm_crtc *crtc,
  9246. struct drm_display_mode *mode,
  9247. int x, int y, struct drm_framebuffer *fb)
  9248. {
  9249. struct intel_crtc_state *pipe_config;
  9250. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9251. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9252. &modeset_pipes,
  9253. &prepare_pipes,
  9254. &disable_pipes);
  9255. if (IS_ERR(pipe_config))
  9256. return PTR_ERR(pipe_config);
  9257. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9258. modeset_pipes, prepare_pipes,
  9259. disable_pipes);
  9260. }
  9261. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9262. {
  9263. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9264. }
  9265. #undef for_each_intel_crtc_masked
  9266. static void intel_set_config_free(struct intel_set_config *config)
  9267. {
  9268. if (!config)
  9269. return;
  9270. kfree(config->save_connector_encoders);
  9271. kfree(config->save_encoder_crtcs);
  9272. kfree(config->save_crtc_enabled);
  9273. kfree(config);
  9274. }
  9275. static int intel_set_config_save_state(struct drm_device *dev,
  9276. struct intel_set_config *config)
  9277. {
  9278. struct drm_crtc *crtc;
  9279. struct drm_encoder *encoder;
  9280. struct drm_connector *connector;
  9281. int count;
  9282. config->save_crtc_enabled =
  9283. kcalloc(dev->mode_config.num_crtc,
  9284. sizeof(bool), GFP_KERNEL);
  9285. if (!config->save_crtc_enabled)
  9286. return -ENOMEM;
  9287. config->save_encoder_crtcs =
  9288. kcalloc(dev->mode_config.num_encoder,
  9289. sizeof(struct drm_crtc *), GFP_KERNEL);
  9290. if (!config->save_encoder_crtcs)
  9291. return -ENOMEM;
  9292. config->save_connector_encoders =
  9293. kcalloc(dev->mode_config.num_connector,
  9294. sizeof(struct drm_encoder *), GFP_KERNEL);
  9295. if (!config->save_connector_encoders)
  9296. return -ENOMEM;
  9297. /* Copy data. Note that driver private data is not affected.
  9298. * Should anything bad happen only the expected state is
  9299. * restored, not the drivers personal bookkeeping.
  9300. */
  9301. count = 0;
  9302. for_each_crtc(dev, crtc) {
  9303. config->save_crtc_enabled[count++] = crtc->enabled;
  9304. }
  9305. count = 0;
  9306. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9307. config->save_encoder_crtcs[count++] = encoder->crtc;
  9308. }
  9309. count = 0;
  9310. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9311. config->save_connector_encoders[count++] = connector->encoder;
  9312. }
  9313. return 0;
  9314. }
  9315. static void intel_set_config_restore_state(struct drm_device *dev,
  9316. struct intel_set_config *config)
  9317. {
  9318. struct intel_crtc *crtc;
  9319. struct intel_encoder *encoder;
  9320. struct intel_connector *connector;
  9321. int count;
  9322. count = 0;
  9323. for_each_intel_crtc(dev, crtc) {
  9324. crtc->new_enabled = config->save_crtc_enabled[count++];
  9325. if (crtc->new_enabled)
  9326. crtc->new_config = &crtc->config;
  9327. else
  9328. crtc->new_config = NULL;
  9329. }
  9330. count = 0;
  9331. for_each_intel_encoder(dev, encoder) {
  9332. encoder->new_crtc =
  9333. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9334. }
  9335. count = 0;
  9336. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9337. connector->new_encoder =
  9338. to_intel_encoder(config->save_connector_encoders[count++]);
  9339. }
  9340. }
  9341. static bool
  9342. is_crtc_connector_off(struct drm_mode_set *set)
  9343. {
  9344. int i;
  9345. if (set->num_connectors == 0)
  9346. return false;
  9347. if (WARN_ON(set->connectors == NULL))
  9348. return false;
  9349. for (i = 0; i < set->num_connectors; i++)
  9350. if (set->connectors[i]->encoder &&
  9351. set->connectors[i]->encoder->crtc == set->crtc &&
  9352. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9353. return true;
  9354. return false;
  9355. }
  9356. static void
  9357. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9358. struct intel_set_config *config)
  9359. {
  9360. /* We should be able to check here if the fb has the same properties
  9361. * and then just flip_or_move it */
  9362. if (is_crtc_connector_off(set)) {
  9363. config->mode_changed = true;
  9364. } else if (set->crtc->primary->fb != set->fb) {
  9365. /*
  9366. * If we have no fb, we can only flip as long as the crtc is
  9367. * active, otherwise we need a full mode set. The crtc may
  9368. * be active if we've only disabled the primary plane, or
  9369. * in fastboot situations.
  9370. */
  9371. if (set->crtc->primary->fb == NULL) {
  9372. struct intel_crtc *intel_crtc =
  9373. to_intel_crtc(set->crtc);
  9374. if (intel_crtc->active) {
  9375. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9376. config->fb_changed = true;
  9377. } else {
  9378. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9379. config->mode_changed = true;
  9380. }
  9381. } else if (set->fb == NULL) {
  9382. config->mode_changed = true;
  9383. } else if (set->fb->pixel_format !=
  9384. set->crtc->primary->fb->pixel_format) {
  9385. config->mode_changed = true;
  9386. } else {
  9387. config->fb_changed = true;
  9388. }
  9389. }
  9390. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9391. config->fb_changed = true;
  9392. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9393. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9394. drm_mode_debug_printmodeline(&set->crtc->mode);
  9395. drm_mode_debug_printmodeline(set->mode);
  9396. config->mode_changed = true;
  9397. }
  9398. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9399. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9400. }
  9401. static int
  9402. intel_modeset_stage_output_state(struct drm_device *dev,
  9403. struct drm_mode_set *set,
  9404. struct intel_set_config *config)
  9405. {
  9406. struct intel_connector *connector;
  9407. struct intel_encoder *encoder;
  9408. struct intel_crtc *crtc;
  9409. int ro;
  9410. /* The upper layers ensure that we either disable a crtc or have a list
  9411. * of connectors. For paranoia, double-check this. */
  9412. WARN_ON(!set->fb && (set->num_connectors != 0));
  9413. WARN_ON(set->fb && (set->num_connectors == 0));
  9414. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9415. base.head) {
  9416. /* Otherwise traverse passed in connector list and get encoders
  9417. * for them. */
  9418. for (ro = 0; ro < set->num_connectors; ro++) {
  9419. if (set->connectors[ro] == &connector->base) {
  9420. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9421. break;
  9422. }
  9423. }
  9424. /* If we disable the crtc, disable all its connectors. Also, if
  9425. * the connector is on the changing crtc but not on the new
  9426. * connector list, disable it. */
  9427. if ((!set->fb || ro == set->num_connectors) &&
  9428. connector->base.encoder &&
  9429. connector->base.encoder->crtc == set->crtc) {
  9430. connector->new_encoder = NULL;
  9431. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9432. connector->base.base.id,
  9433. connector->base.name);
  9434. }
  9435. if (&connector->new_encoder->base != connector->base.encoder) {
  9436. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9437. config->mode_changed = true;
  9438. }
  9439. }
  9440. /* connector->new_encoder is now updated for all connectors. */
  9441. /* Update crtc of enabled connectors. */
  9442. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9443. base.head) {
  9444. struct drm_crtc *new_crtc;
  9445. if (!connector->new_encoder)
  9446. continue;
  9447. new_crtc = connector->new_encoder->base.crtc;
  9448. for (ro = 0; ro < set->num_connectors; ro++) {
  9449. if (set->connectors[ro] == &connector->base)
  9450. new_crtc = set->crtc;
  9451. }
  9452. /* Make sure the new CRTC will work with the encoder */
  9453. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9454. new_crtc)) {
  9455. return -EINVAL;
  9456. }
  9457. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9458. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9459. connector->base.base.id,
  9460. connector->base.name,
  9461. new_crtc->base.id);
  9462. }
  9463. /* Check for any encoders that needs to be disabled. */
  9464. for_each_intel_encoder(dev, encoder) {
  9465. int num_connectors = 0;
  9466. list_for_each_entry(connector,
  9467. &dev->mode_config.connector_list,
  9468. base.head) {
  9469. if (connector->new_encoder == encoder) {
  9470. WARN_ON(!connector->new_encoder->new_crtc);
  9471. num_connectors++;
  9472. }
  9473. }
  9474. if (num_connectors == 0)
  9475. encoder->new_crtc = NULL;
  9476. else if (num_connectors > 1)
  9477. return -EINVAL;
  9478. /* Only now check for crtc changes so we don't miss encoders
  9479. * that will be disabled. */
  9480. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9481. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9482. config->mode_changed = true;
  9483. }
  9484. }
  9485. /* Now we've also updated encoder->new_crtc for all encoders. */
  9486. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9487. base.head) {
  9488. if (connector->new_encoder)
  9489. if (connector->new_encoder != connector->encoder)
  9490. connector->encoder = connector->new_encoder;
  9491. }
  9492. for_each_intel_crtc(dev, crtc) {
  9493. crtc->new_enabled = false;
  9494. for_each_intel_encoder(dev, encoder) {
  9495. if (encoder->new_crtc == crtc) {
  9496. crtc->new_enabled = true;
  9497. break;
  9498. }
  9499. }
  9500. if (crtc->new_enabled != crtc->base.enabled) {
  9501. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9502. crtc->new_enabled ? "en" : "dis");
  9503. config->mode_changed = true;
  9504. }
  9505. if (crtc->new_enabled)
  9506. crtc->new_config = &crtc->config;
  9507. else
  9508. crtc->new_config = NULL;
  9509. }
  9510. return 0;
  9511. }
  9512. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9513. {
  9514. struct drm_device *dev = crtc->base.dev;
  9515. struct intel_encoder *encoder;
  9516. struct intel_connector *connector;
  9517. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9518. pipe_name(crtc->pipe));
  9519. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9520. if (connector->new_encoder &&
  9521. connector->new_encoder->new_crtc == crtc)
  9522. connector->new_encoder = NULL;
  9523. }
  9524. for_each_intel_encoder(dev, encoder) {
  9525. if (encoder->new_crtc == crtc)
  9526. encoder->new_crtc = NULL;
  9527. }
  9528. crtc->new_enabled = false;
  9529. crtc->new_config = NULL;
  9530. }
  9531. static int intel_crtc_set_config(struct drm_mode_set *set)
  9532. {
  9533. struct drm_device *dev;
  9534. struct drm_mode_set save_set;
  9535. struct intel_set_config *config;
  9536. struct intel_crtc_state *pipe_config;
  9537. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9538. int ret;
  9539. BUG_ON(!set);
  9540. BUG_ON(!set->crtc);
  9541. BUG_ON(!set->crtc->helper_private);
  9542. /* Enforce sane interface api - has been abused by the fb helper. */
  9543. BUG_ON(!set->mode && set->fb);
  9544. BUG_ON(set->fb && set->num_connectors == 0);
  9545. if (set->fb) {
  9546. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9547. set->crtc->base.id, set->fb->base.id,
  9548. (int)set->num_connectors, set->x, set->y);
  9549. } else {
  9550. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9551. }
  9552. dev = set->crtc->dev;
  9553. ret = -ENOMEM;
  9554. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9555. if (!config)
  9556. goto out_config;
  9557. ret = intel_set_config_save_state(dev, config);
  9558. if (ret)
  9559. goto out_config;
  9560. save_set.crtc = set->crtc;
  9561. save_set.mode = &set->crtc->mode;
  9562. save_set.x = set->crtc->x;
  9563. save_set.y = set->crtc->y;
  9564. save_set.fb = set->crtc->primary->fb;
  9565. /* Compute whether we need a full modeset, only an fb base update or no
  9566. * change at all. In the future we might also check whether only the
  9567. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9568. * such cases. */
  9569. intel_set_config_compute_mode_changes(set, config);
  9570. ret = intel_modeset_stage_output_state(dev, set, config);
  9571. if (ret)
  9572. goto fail;
  9573. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9574. set->fb,
  9575. &modeset_pipes,
  9576. &prepare_pipes,
  9577. &disable_pipes);
  9578. if (IS_ERR(pipe_config)) {
  9579. ret = PTR_ERR(pipe_config);
  9580. goto fail;
  9581. } else if (pipe_config) {
  9582. if (pipe_config->has_audio !=
  9583. to_intel_crtc(set->crtc)->config.has_audio)
  9584. config->mode_changed = true;
  9585. /*
  9586. * Note we have an issue here with infoframes: current code
  9587. * only updates them on the full mode set path per hw
  9588. * requirements. So here we should be checking for any
  9589. * required changes and forcing a mode set.
  9590. */
  9591. }
  9592. /* set_mode will free it in the mode_changed case */
  9593. if (!config->mode_changed)
  9594. kfree(pipe_config);
  9595. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9596. if (config->mode_changed) {
  9597. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9598. set->x, set->y, set->fb, pipe_config,
  9599. modeset_pipes, prepare_pipes,
  9600. disable_pipes);
  9601. } else if (config->fb_changed) {
  9602. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9603. struct drm_plane *primary = set->crtc->primary;
  9604. int vdisplay, hdisplay;
  9605. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  9606. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  9607. 0, 0, hdisplay, vdisplay,
  9608. set->x << 16, set->y << 16,
  9609. hdisplay << 16, vdisplay << 16);
  9610. /*
  9611. * We need to make sure the primary plane is re-enabled if it
  9612. * has previously been turned off.
  9613. */
  9614. if (!intel_crtc->primary_enabled && ret == 0) {
  9615. WARN_ON(!intel_crtc->active);
  9616. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9617. }
  9618. /*
  9619. * In the fastboot case this may be our only check of the
  9620. * state after boot. It would be better to only do it on
  9621. * the first update, but we don't have a nice way of doing that
  9622. * (and really, set_config isn't used much for high freq page
  9623. * flipping, so increasing its cost here shouldn't be a big
  9624. * deal).
  9625. */
  9626. if (i915.fastboot && ret == 0)
  9627. intel_modeset_check_state(set->crtc->dev);
  9628. }
  9629. if (ret) {
  9630. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9631. set->crtc->base.id, ret);
  9632. fail:
  9633. intel_set_config_restore_state(dev, config);
  9634. /*
  9635. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9636. * force the pipe off to avoid oopsing in the modeset code
  9637. * due to fb==NULL. This should only happen during boot since
  9638. * we don't yet reconstruct the FB from the hardware state.
  9639. */
  9640. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9641. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9642. /* Try to restore the config */
  9643. if (config->mode_changed &&
  9644. intel_set_mode(save_set.crtc, save_set.mode,
  9645. save_set.x, save_set.y, save_set.fb))
  9646. DRM_ERROR("failed to restore config after modeset failure\n");
  9647. }
  9648. out_config:
  9649. intel_set_config_free(config);
  9650. return ret;
  9651. }
  9652. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9653. .gamma_set = intel_crtc_gamma_set,
  9654. .set_config = intel_crtc_set_config,
  9655. .destroy = intel_crtc_destroy,
  9656. .page_flip = intel_crtc_page_flip,
  9657. };
  9658. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9659. struct intel_shared_dpll *pll,
  9660. struct intel_dpll_hw_state *hw_state)
  9661. {
  9662. uint32_t val;
  9663. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9664. return false;
  9665. val = I915_READ(PCH_DPLL(pll->id));
  9666. hw_state->dpll = val;
  9667. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9668. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9669. return val & DPLL_VCO_ENABLE;
  9670. }
  9671. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9672. struct intel_shared_dpll *pll)
  9673. {
  9674. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  9675. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  9676. }
  9677. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9678. struct intel_shared_dpll *pll)
  9679. {
  9680. /* PCH refclock must be enabled first */
  9681. ibx_assert_pch_refclk_enabled(dev_priv);
  9682. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9683. /* Wait for the clocks to stabilize. */
  9684. POSTING_READ(PCH_DPLL(pll->id));
  9685. udelay(150);
  9686. /* The pixel multiplier can only be updated once the
  9687. * DPLL is enabled and the clocks are stable.
  9688. *
  9689. * So write it again.
  9690. */
  9691. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9692. POSTING_READ(PCH_DPLL(pll->id));
  9693. udelay(200);
  9694. }
  9695. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9696. struct intel_shared_dpll *pll)
  9697. {
  9698. struct drm_device *dev = dev_priv->dev;
  9699. struct intel_crtc *crtc;
  9700. /* Make sure no transcoder isn't still depending on us. */
  9701. for_each_intel_crtc(dev, crtc) {
  9702. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9703. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9704. }
  9705. I915_WRITE(PCH_DPLL(pll->id), 0);
  9706. POSTING_READ(PCH_DPLL(pll->id));
  9707. udelay(200);
  9708. }
  9709. static char *ibx_pch_dpll_names[] = {
  9710. "PCH DPLL A",
  9711. "PCH DPLL B",
  9712. };
  9713. static void ibx_pch_dpll_init(struct drm_device *dev)
  9714. {
  9715. struct drm_i915_private *dev_priv = dev->dev_private;
  9716. int i;
  9717. dev_priv->num_shared_dpll = 2;
  9718. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9719. dev_priv->shared_dplls[i].id = i;
  9720. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9721. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9722. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9723. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9724. dev_priv->shared_dplls[i].get_hw_state =
  9725. ibx_pch_dpll_get_hw_state;
  9726. }
  9727. }
  9728. static void intel_shared_dpll_init(struct drm_device *dev)
  9729. {
  9730. struct drm_i915_private *dev_priv = dev->dev_private;
  9731. if (HAS_DDI(dev))
  9732. intel_ddi_pll_init(dev);
  9733. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9734. ibx_pch_dpll_init(dev);
  9735. else
  9736. dev_priv->num_shared_dpll = 0;
  9737. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9738. }
  9739. /**
  9740. * intel_prepare_plane_fb - Prepare fb for usage on plane
  9741. * @plane: drm plane to prepare for
  9742. * @fb: framebuffer to prepare for presentation
  9743. *
  9744. * Prepares a framebuffer for usage on a display plane. Generally this
  9745. * involves pinning the underlying object and updating the frontbuffer tracking
  9746. * bits. Some older platforms need special physical address handling for
  9747. * cursor planes.
  9748. *
  9749. * Returns 0 on success, negative error code on failure.
  9750. */
  9751. int
  9752. intel_prepare_plane_fb(struct drm_plane *plane,
  9753. struct drm_framebuffer *fb)
  9754. {
  9755. struct drm_device *dev = plane->dev;
  9756. struct intel_plane *intel_plane = to_intel_plane(plane);
  9757. enum pipe pipe = intel_plane->pipe;
  9758. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9759. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9760. unsigned frontbuffer_bits = 0;
  9761. int ret = 0;
  9762. if (!obj)
  9763. return 0;
  9764. switch (plane->type) {
  9765. case DRM_PLANE_TYPE_PRIMARY:
  9766. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  9767. break;
  9768. case DRM_PLANE_TYPE_CURSOR:
  9769. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  9770. break;
  9771. case DRM_PLANE_TYPE_OVERLAY:
  9772. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  9773. break;
  9774. }
  9775. mutex_lock(&dev->struct_mutex);
  9776. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  9777. INTEL_INFO(dev)->cursor_needs_physical) {
  9778. int align = IS_I830(dev) ? 16 * 1024 : 256;
  9779. ret = i915_gem_object_attach_phys(obj, align);
  9780. if (ret)
  9781. DRM_DEBUG_KMS("failed to attach phys object\n");
  9782. } else {
  9783. ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
  9784. }
  9785. if (ret == 0)
  9786. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  9787. mutex_unlock(&dev->struct_mutex);
  9788. return ret;
  9789. }
  9790. /**
  9791. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  9792. * @plane: drm plane to clean up for
  9793. * @fb: old framebuffer that was on plane
  9794. *
  9795. * Cleans up a framebuffer that has just been removed from a plane.
  9796. */
  9797. void
  9798. intel_cleanup_plane_fb(struct drm_plane *plane,
  9799. struct drm_framebuffer *fb)
  9800. {
  9801. struct drm_device *dev = plane->dev;
  9802. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9803. if (WARN_ON(!obj))
  9804. return;
  9805. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  9806. !INTEL_INFO(dev)->cursor_needs_physical) {
  9807. mutex_lock(&dev->struct_mutex);
  9808. intel_unpin_fb_obj(obj);
  9809. mutex_unlock(&dev->struct_mutex);
  9810. }
  9811. }
  9812. static int
  9813. intel_check_primary_plane(struct drm_plane *plane,
  9814. struct intel_plane_state *state)
  9815. {
  9816. struct drm_device *dev = plane->dev;
  9817. struct drm_i915_private *dev_priv = dev->dev_private;
  9818. struct drm_crtc *crtc = state->base.crtc;
  9819. struct intel_crtc *intel_crtc;
  9820. struct intel_plane *intel_plane = to_intel_plane(plane);
  9821. struct drm_framebuffer *fb = state->base.fb;
  9822. struct drm_rect *dest = &state->dst;
  9823. struct drm_rect *src = &state->src;
  9824. const struct drm_rect *clip = &state->clip;
  9825. int ret;
  9826. crtc = crtc ? crtc : plane->crtc;
  9827. intel_crtc = to_intel_crtc(crtc);
  9828. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9829. src, dest, clip,
  9830. DRM_PLANE_HELPER_NO_SCALING,
  9831. DRM_PLANE_HELPER_NO_SCALING,
  9832. false, true, &state->visible);
  9833. if (ret)
  9834. return ret;
  9835. if (intel_crtc->active) {
  9836. intel_crtc->atomic.wait_for_flips = true;
  9837. /*
  9838. * FBC does not work on some platforms for rotated
  9839. * planes, so disable it when rotation is not 0 and
  9840. * update it when rotation is set back to 0.
  9841. *
  9842. * FIXME: This is redundant with the fbc update done in
  9843. * the primary plane enable function except that that
  9844. * one is done too late. We eventually need to unify
  9845. * this.
  9846. */
  9847. if (intel_crtc->primary_enabled &&
  9848. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9849. dev_priv->fbc.plane == intel_crtc->plane &&
  9850. intel_plane->rotation != BIT(DRM_ROTATE_0)) {
  9851. intel_crtc->atomic.disable_fbc = true;
  9852. }
  9853. if (state->visible) {
  9854. /*
  9855. * BDW signals flip done immediately if the plane
  9856. * is disabled, even if the plane enable is already
  9857. * armed to occur at the next vblank :(
  9858. */
  9859. if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
  9860. intel_crtc->atomic.wait_vblank = true;
  9861. }
  9862. intel_crtc->atomic.fb_bits |=
  9863. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  9864. intel_crtc->atomic.update_fbc = true;
  9865. }
  9866. return 0;
  9867. }
  9868. static void
  9869. intel_commit_primary_plane(struct drm_plane *plane,
  9870. struct intel_plane_state *state)
  9871. {
  9872. struct drm_crtc *crtc = state->base.crtc;
  9873. struct drm_framebuffer *fb = state->base.fb;
  9874. struct drm_device *dev = plane->dev;
  9875. struct drm_i915_private *dev_priv = dev->dev_private;
  9876. struct intel_crtc *intel_crtc;
  9877. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9878. struct intel_plane *intel_plane = to_intel_plane(plane);
  9879. struct drm_rect *src = &state->src;
  9880. crtc = crtc ? crtc : plane->crtc;
  9881. intel_crtc = to_intel_crtc(crtc);
  9882. plane->fb = fb;
  9883. crtc->x = src->x1 >> 16;
  9884. crtc->y = src->y1 >> 16;
  9885. intel_plane->obj = obj;
  9886. if (intel_crtc->active) {
  9887. if (state->visible) {
  9888. /* FIXME: kill this fastboot hack */
  9889. intel_update_pipe_size(intel_crtc);
  9890. intel_crtc->primary_enabled = true;
  9891. dev_priv->display.update_primary_plane(crtc, plane->fb,
  9892. crtc->x, crtc->y);
  9893. } else {
  9894. /*
  9895. * If clipping results in a non-visible primary plane,
  9896. * we'll disable the primary plane. Note that this is
  9897. * a bit different than what happens if userspace
  9898. * explicitly disables the plane by passing fb=0
  9899. * because plane->fb still gets set and pinned.
  9900. */
  9901. intel_disable_primary_hw_plane(plane, crtc);
  9902. }
  9903. }
  9904. }
  9905. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  9906. {
  9907. struct drm_device *dev = crtc->dev;
  9908. struct drm_i915_private *dev_priv = dev->dev_private;
  9909. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9910. struct intel_plane *intel_plane;
  9911. struct drm_plane *p;
  9912. unsigned fb_bits = 0;
  9913. /* Track fb's for any planes being disabled */
  9914. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  9915. intel_plane = to_intel_plane(p);
  9916. if (intel_crtc->atomic.disabled_planes &
  9917. (1 << drm_plane_index(p))) {
  9918. switch (p->type) {
  9919. case DRM_PLANE_TYPE_PRIMARY:
  9920. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  9921. break;
  9922. case DRM_PLANE_TYPE_CURSOR:
  9923. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  9924. break;
  9925. case DRM_PLANE_TYPE_OVERLAY:
  9926. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  9927. break;
  9928. }
  9929. mutex_lock(&dev->struct_mutex);
  9930. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  9931. mutex_unlock(&dev->struct_mutex);
  9932. }
  9933. }
  9934. if (intel_crtc->atomic.wait_for_flips)
  9935. intel_crtc_wait_for_pending_flips(crtc);
  9936. if (intel_crtc->atomic.disable_fbc)
  9937. intel_fbc_disable(dev);
  9938. if (intel_crtc->atomic.pre_disable_primary)
  9939. intel_pre_disable_primary(crtc);
  9940. if (intel_crtc->atomic.update_wm)
  9941. intel_update_watermarks(crtc);
  9942. intel_runtime_pm_get(dev_priv);
  9943. /* Perform vblank evasion around commit operation */
  9944. if (intel_crtc->active)
  9945. intel_crtc->atomic.evade =
  9946. intel_pipe_update_start(intel_crtc,
  9947. &intel_crtc->atomic.start_vbl_count);
  9948. }
  9949. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  9950. {
  9951. struct drm_device *dev = crtc->dev;
  9952. struct drm_i915_private *dev_priv = dev->dev_private;
  9953. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9954. struct drm_plane *p;
  9955. if (intel_crtc->atomic.evade)
  9956. intel_pipe_update_end(intel_crtc,
  9957. intel_crtc->atomic.start_vbl_count);
  9958. intel_runtime_pm_put(dev_priv);
  9959. if (intel_crtc->atomic.wait_vblank)
  9960. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9961. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  9962. if (intel_crtc->atomic.update_fbc) {
  9963. mutex_lock(&dev->struct_mutex);
  9964. intel_fbc_update(dev);
  9965. mutex_unlock(&dev->struct_mutex);
  9966. }
  9967. if (intel_crtc->atomic.post_enable_primary)
  9968. intel_post_enable_primary(crtc);
  9969. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  9970. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  9971. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  9972. false, false);
  9973. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  9974. }
  9975. /**
  9976. * intel_plane_destroy - destroy a plane
  9977. * @plane: plane to destroy
  9978. *
  9979. * Common destruction function for all types of planes (primary, cursor,
  9980. * sprite).
  9981. */
  9982. void intel_plane_destroy(struct drm_plane *plane)
  9983. {
  9984. struct intel_plane *intel_plane = to_intel_plane(plane);
  9985. drm_plane_cleanup(plane);
  9986. kfree(intel_plane);
  9987. }
  9988. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  9989. .update_plane = drm_plane_helper_update,
  9990. .disable_plane = drm_plane_helper_disable,
  9991. .destroy = intel_plane_destroy,
  9992. .set_property = intel_plane_set_property,
  9993. .atomic_duplicate_state = intel_plane_duplicate_state,
  9994. .atomic_destroy_state = intel_plane_destroy_state,
  9995. };
  9996. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  9997. int pipe)
  9998. {
  9999. struct intel_plane *primary;
  10000. const uint32_t *intel_primary_formats;
  10001. int num_formats;
  10002. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10003. if (primary == NULL)
  10004. return NULL;
  10005. primary->base.state = intel_plane_duplicate_state(&primary->base);
  10006. if (primary->base.state == NULL) {
  10007. kfree(primary);
  10008. return NULL;
  10009. }
  10010. primary->can_scale = false;
  10011. primary->max_downscale = 1;
  10012. primary->pipe = pipe;
  10013. primary->plane = pipe;
  10014. primary->rotation = BIT(DRM_ROTATE_0);
  10015. primary->check_plane = intel_check_primary_plane;
  10016. primary->commit_plane = intel_commit_primary_plane;
  10017. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10018. primary->plane = !pipe;
  10019. if (INTEL_INFO(dev)->gen <= 3) {
  10020. intel_primary_formats = intel_primary_formats_gen2;
  10021. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10022. } else {
  10023. intel_primary_formats = intel_primary_formats_gen4;
  10024. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10025. }
  10026. drm_universal_plane_init(dev, &primary->base, 0,
  10027. &intel_primary_plane_funcs,
  10028. intel_primary_formats, num_formats,
  10029. DRM_PLANE_TYPE_PRIMARY);
  10030. if (INTEL_INFO(dev)->gen >= 4) {
  10031. if (!dev->mode_config.rotation_property)
  10032. dev->mode_config.rotation_property =
  10033. drm_mode_create_rotation_property(dev,
  10034. BIT(DRM_ROTATE_0) |
  10035. BIT(DRM_ROTATE_180));
  10036. if (dev->mode_config.rotation_property)
  10037. drm_object_attach_property(&primary->base.base,
  10038. dev->mode_config.rotation_property,
  10039. primary->rotation);
  10040. }
  10041. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  10042. return &primary->base;
  10043. }
  10044. static int
  10045. intel_check_cursor_plane(struct drm_plane *plane,
  10046. struct intel_plane_state *state)
  10047. {
  10048. struct drm_crtc *crtc = state->base.crtc;
  10049. struct drm_device *dev = plane->dev;
  10050. struct drm_framebuffer *fb = state->base.fb;
  10051. struct drm_rect *dest = &state->dst;
  10052. struct drm_rect *src = &state->src;
  10053. const struct drm_rect *clip = &state->clip;
  10054. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10055. struct intel_crtc *intel_crtc;
  10056. unsigned stride;
  10057. int ret;
  10058. crtc = crtc ? crtc : plane->crtc;
  10059. intel_crtc = to_intel_crtc(crtc);
  10060. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10061. src, dest, clip,
  10062. DRM_PLANE_HELPER_NO_SCALING,
  10063. DRM_PLANE_HELPER_NO_SCALING,
  10064. true, true, &state->visible);
  10065. if (ret)
  10066. return ret;
  10067. /* if we want to turn off the cursor ignore width and height */
  10068. if (!obj)
  10069. goto finish;
  10070. /* Check for which cursor types we support */
  10071. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  10072. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  10073. state->base.crtc_w, state->base.crtc_h);
  10074. return -EINVAL;
  10075. }
  10076. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  10077. if (obj->base.size < stride * state->base.crtc_h) {
  10078. DRM_DEBUG_KMS("buffer is too small\n");
  10079. return -ENOMEM;
  10080. }
  10081. if (fb == crtc->cursor->fb)
  10082. return 0;
  10083. /* we only need to pin inside GTT if cursor is non-phy */
  10084. mutex_lock(&dev->struct_mutex);
  10085. if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
  10086. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10087. ret = -EINVAL;
  10088. }
  10089. mutex_unlock(&dev->struct_mutex);
  10090. finish:
  10091. if (intel_crtc->active) {
  10092. if (intel_crtc->cursor_width != state->base.crtc_w)
  10093. intel_crtc->atomic.update_wm = true;
  10094. intel_crtc->atomic.fb_bits |=
  10095. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  10096. }
  10097. return ret;
  10098. }
  10099. static void
  10100. intel_commit_cursor_plane(struct drm_plane *plane,
  10101. struct intel_plane_state *state)
  10102. {
  10103. struct drm_crtc *crtc = state->base.crtc;
  10104. struct drm_device *dev = plane->dev;
  10105. struct intel_crtc *intel_crtc;
  10106. struct intel_plane *intel_plane = to_intel_plane(plane);
  10107. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10108. uint32_t addr;
  10109. crtc = crtc ? crtc : plane->crtc;
  10110. intel_crtc = to_intel_crtc(crtc);
  10111. plane->fb = state->base.fb;
  10112. crtc->cursor_x = state->base.crtc_x;
  10113. crtc->cursor_y = state->base.crtc_y;
  10114. intel_plane->obj = obj;
  10115. if (intel_crtc->cursor_bo == obj)
  10116. goto update;
  10117. if (!obj)
  10118. addr = 0;
  10119. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10120. addr = i915_gem_obj_ggtt_offset(obj);
  10121. else
  10122. addr = obj->phys_handle->busaddr;
  10123. intel_crtc->cursor_addr = addr;
  10124. intel_crtc->cursor_bo = obj;
  10125. update:
  10126. intel_crtc->cursor_width = state->base.crtc_w;
  10127. intel_crtc->cursor_height = state->base.crtc_h;
  10128. if (intel_crtc->active)
  10129. intel_crtc_update_cursor(crtc, state->visible);
  10130. }
  10131. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10132. .update_plane = drm_plane_helper_update,
  10133. .disable_plane = drm_plane_helper_disable,
  10134. .destroy = intel_plane_destroy,
  10135. .set_property = intel_plane_set_property,
  10136. .atomic_duplicate_state = intel_plane_duplicate_state,
  10137. .atomic_destroy_state = intel_plane_destroy_state,
  10138. };
  10139. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10140. int pipe)
  10141. {
  10142. struct intel_plane *cursor;
  10143. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10144. if (cursor == NULL)
  10145. return NULL;
  10146. cursor->base.state = intel_plane_duplicate_state(&cursor->base);
  10147. if (cursor->base.state == NULL) {
  10148. kfree(cursor);
  10149. return NULL;
  10150. }
  10151. cursor->can_scale = false;
  10152. cursor->max_downscale = 1;
  10153. cursor->pipe = pipe;
  10154. cursor->plane = pipe;
  10155. cursor->rotation = BIT(DRM_ROTATE_0);
  10156. cursor->check_plane = intel_check_cursor_plane;
  10157. cursor->commit_plane = intel_commit_cursor_plane;
  10158. drm_universal_plane_init(dev, &cursor->base, 0,
  10159. &intel_cursor_plane_funcs,
  10160. intel_cursor_formats,
  10161. ARRAY_SIZE(intel_cursor_formats),
  10162. DRM_PLANE_TYPE_CURSOR);
  10163. if (INTEL_INFO(dev)->gen >= 4) {
  10164. if (!dev->mode_config.rotation_property)
  10165. dev->mode_config.rotation_property =
  10166. drm_mode_create_rotation_property(dev,
  10167. BIT(DRM_ROTATE_0) |
  10168. BIT(DRM_ROTATE_180));
  10169. if (dev->mode_config.rotation_property)
  10170. drm_object_attach_property(&cursor->base.base,
  10171. dev->mode_config.rotation_property,
  10172. cursor->rotation);
  10173. }
  10174. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  10175. return &cursor->base;
  10176. }
  10177. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10178. {
  10179. struct drm_i915_private *dev_priv = dev->dev_private;
  10180. struct intel_crtc *intel_crtc;
  10181. struct drm_plane *primary = NULL;
  10182. struct drm_plane *cursor = NULL;
  10183. int i, ret;
  10184. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10185. if (intel_crtc == NULL)
  10186. return;
  10187. primary = intel_primary_plane_create(dev, pipe);
  10188. if (!primary)
  10189. goto fail;
  10190. cursor = intel_cursor_plane_create(dev, pipe);
  10191. if (!cursor)
  10192. goto fail;
  10193. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10194. cursor, &intel_crtc_funcs);
  10195. if (ret)
  10196. goto fail;
  10197. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10198. for (i = 0; i < 256; i++) {
  10199. intel_crtc->lut_r[i] = i;
  10200. intel_crtc->lut_g[i] = i;
  10201. intel_crtc->lut_b[i] = i;
  10202. }
  10203. /*
  10204. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10205. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10206. */
  10207. intel_crtc->pipe = pipe;
  10208. intel_crtc->plane = pipe;
  10209. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10210. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10211. intel_crtc->plane = !pipe;
  10212. }
  10213. intel_crtc->cursor_base = ~0;
  10214. intel_crtc->cursor_cntl = ~0;
  10215. intel_crtc->cursor_size = ~0;
  10216. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10217. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10218. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10219. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10220. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10221. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10222. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10223. return;
  10224. fail:
  10225. if (primary)
  10226. drm_plane_cleanup(primary);
  10227. if (cursor)
  10228. drm_plane_cleanup(cursor);
  10229. kfree(intel_crtc);
  10230. }
  10231. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10232. {
  10233. struct drm_encoder *encoder = connector->base.encoder;
  10234. struct drm_device *dev = connector->base.dev;
  10235. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10236. if (!encoder || WARN_ON(!encoder->crtc))
  10237. return INVALID_PIPE;
  10238. return to_intel_crtc(encoder->crtc)->pipe;
  10239. }
  10240. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10241. struct drm_file *file)
  10242. {
  10243. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10244. struct drm_crtc *drmmode_crtc;
  10245. struct intel_crtc *crtc;
  10246. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  10247. return -ENODEV;
  10248. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10249. if (!drmmode_crtc) {
  10250. DRM_ERROR("no such CRTC id\n");
  10251. return -ENOENT;
  10252. }
  10253. crtc = to_intel_crtc(drmmode_crtc);
  10254. pipe_from_crtc_id->pipe = crtc->pipe;
  10255. return 0;
  10256. }
  10257. static int intel_encoder_clones(struct intel_encoder *encoder)
  10258. {
  10259. struct drm_device *dev = encoder->base.dev;
  10260. struct intel_encoder *source_encoder;
  10261. int index_mask = 0;
  10262. int entry = 0;
  10263. for_each_intel_encoder(dev, source_encoder) {
  10264. if (encoders_cloneable(encoder, source_encoder))
  10265. index_mask |= (1 << entry);
  10266. entry++;
  10267. }
  10268. return index_mask;
  10269. }
  10270. static bool has_edp_a(struct drm_device *dev)
  10271. {
  10272. struct drm_i915_private *dev_priv = dev->dev_private;
  10273. if (!IS_MOBILE(dev))
  10274. return false;
  10275. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10276. return false;
  10277. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10278. return false;
  10279. return true;
  10280. }
  10281. static bool intel_crt_present(struct drm_device *dev)
  10282. {
  10283. struct drm_i915_private *dev_priv = dev->dev_private;
  10284. if (INTEL_INFO(dev)->gen >= 9)
  10285. return false;
  10286. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10287. return false;
  10288. if (IS_CHERRYVIEW(dev))
  10289. return false;
  10290. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10291. return false;
  10292. return true;
  10293. }
  10294. static void intel_setup_outputs(struct drm_device *dev)
  10295. {
  10296. struct drm_i915_private *dev_priv = dev->dev_private;
  10297. struct intel_encoder *encoder;
  10298. bool dpd_is_edp = false;
  10299. intel_lvds_init(dev);
  10300. if (intel_crt_present(dev))
  10301. intel_crt_init(dev);
  10302. if (HAS_DDI(dev)) {
  10303. int found;
  10304. /* Haswell uses DDI functions to detect digital outputs */
  10305. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10306. /* DDI A only supports eDP */
  10307. if (found)
  10308. intel_ddi_init(dev, PORT_A);
  10309. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10310. * register */
  10311. found = I915_READ(SFUSE_STRAP);
  10312. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10313. intel_ddi_init(dev, PORT_B);
  10314. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10315. intel_ddi_init(dev, PORT_C);
  10316. if (found & SFUSE_STRAP_DDID_DETECTED)
  10317. intel_ddi_init(dev, PORT_D);
  10318. } else if (HAS_PCH_SPLIT(dev)) {
  10319. int found;
  10320. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10321. if (has_edp_a(dev))
  10322. intel_dp_init(dev, DP_A, PORT_A);
  10323. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10324. /* PCH SDVOB multiplex with HDMIB */
  10325. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10326. if (!found)
  10327. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10328. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10329. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10330. }
  10331. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10332. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10333. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10334. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10335. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10336. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10337. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10338. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10339. } else if (IS_VALLEYVIEW(dev)) {
  10340. /*
  10341. * The DP_DETECTED bit is the latched state of the DDC
  10342. * SDA pin at boot. However since eDP doesn't require DDC
  10343. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10344. * eDP ports may have been muxed to an alternate function.
  10345. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10346. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10347. * detect eDP ports.
  10348. */
  10349. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  10350. !intel_dp_is_edp(dev, PORT_B))
  10351. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10352. PORT_B);
  10353. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10354. intel_dp_is_edp(dev, PORT_B))
  10355. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10356. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  10357. !intel_dp_is_edp(dev, PORT_C))
  10358. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10359. PORT_C);
  10360. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10361. intel_dp_is_edp(dev, PORT_C))
  10362. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10363. if (IS_CHERRYVIEW(dev)) {
  10364. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10365. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10366. PORT_D);
  10367. /* eDP not supported on port D, so don't check VBT */
  10368. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10369. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10370. }
  10371. intel_dsi_init(dev);
  10372. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10373. bool found = false;
  10374. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10375. DRM_DEBUG_KMS("probing SDVOB\n");
  10376. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10377. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10378. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10379. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10380. }
  10381. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10382. intel_dp_init(dev, DP_B, PORT_B);
  10383. }
  10384. /* Before G4X SDVOC doesn't have its own detect register */
  10385. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10386. DRM_DEBUG_KMS("probing SDVOC\n");
  10387. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10388. }
  10389. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10390. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10391. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10392. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10393. }
  10394. if (SUPPORTS_INTEGRATED_DP(dev))
  10395. intel_dp_init(dev, DP_C, PORT_C);
  10396. }
  10397. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10398. (I915_READ(DP_D) & DP_DETECTED))
  10399. intel_dp_init(dev, DP_D, PORT_D);
  10400. } else if (IS_GEN2(dev))
  10401. intel_dvo_init(dev);
  10402. if (SUPPORTS_TV(dev))
  10403. intel_tv_init(dev);
  10404. intel_psr_init(dev);
  10405. for_each_intel_encoder(dev, encoder) {
  10406. encoder->base.possible_crtcs = encoder->crtc_mask;
  10407. encoder->base.possible_clones =
  10408. intel_encoder_clones(encoder);
  10409. }
  10410. intel_init_pch_refclk(dev);
  10411. drm_helper_move_panel_connectors_to_head(dev);
  10412. }
  10413. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10414. {
  10415. struct drm_device *dev = fb->dev;
  10416. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10417. drm_framebuffer_cleanup(fb);
  10418. mutex_lock(&dev->struct_mutex);
  10419. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10420. drm_gem_object_unreference(&intel_fb->obj->base);
  10421. mutex_unlock(&dev->struct_mutex);
  10422. kfree(intel_fb);
  10423. }
  10424. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10425. struct drm_file *file,
  10426. unsigned int *handle)
  10427. {
  10428. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10429. struct drm_i915_gem_object *obj = intel_fb->obj;
  10430. return drm_gem_handle_create(file, &obj->base, handle);
  10431. }
  10432. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10433. .destroy = intel_user_framebuffer_destroy,
  10434. .create_handle = intel_user_framebuffer_create_handle,
  10435. };
  10436. static int intel_framebuffer_init(struct drm_device *dev,
  10437. struct intel_framebuffer *intel_fb,
  10438. struct drm_mode_fb_cmd2 *mode_cmd,
  10439. struct drm_i915_gem_object *obj)
  10440. {
  10441. int aligned_height;
  10442. int pitch_limit;
  10443. int ret;
  10444. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10445. if (obj->tiling_mode == I915_TILING_Y) {
  10446. DRM_DEBUG("hardware does not support tiling Y\n");
  10447. return -EINVAL;
  10448. }
  10449. if (mode_cmd->pitches[0] & 63) {
  10450. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10451. mode_cmd->pitches[0]);
  10452. return -EINVAL;
  10453. }
  10454. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10455. pitch_limit = 32*1024;
  10456. } else if (INTEL_INFO(dev)->gen >= 4) {
  10457. if (obj->tiling_mode)
  10458. pitch_limit = 16*1024;
  10459. else
  10460. pitch_limit = 32*1024;
  10461. } else if (INTEL_INFO(dev)->gen >= 3) {
  10462. if (obj->tiling_mode)
  10463. pitch_limit = 8*1024;
  10464. else
  10465. pitch_limit = 16*1024;
  10466. } else
  10467. /* XXX DSPC is limited to 4k tiled */
  10468. pitch_limit = 8*1024;
  10469. if (mode_cmd->pitches[0] > pitch_limit) {
  10470. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10471. obj->tiling_mode ? "tiled" : "linear",
  10472. mode_cmd->pitches[0], pitch_limit);
  10473. return -EINVAL;
  10474. }
  10475. if (obj->tiling_mode != I915_TILING_NONE &&
  10476. mode_cmd->pitches[0] != obj->stride) {
  10477. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10478. mode_cmd->pitches[0], obj->stride);
  10479. return -EINVAL;
  10480. }
  10481. /* Reject formats not supported by any plane early. */
  10482. switch (mode_cmd->pixel_format) {
  10483. case DRM_FORMAT_C8:
  10484. case DRM_FORMAT_RGB565:
  10485. case DRM_FORMAT_XRGB8888:
  10486. case DRM_FORMAT_ARGB8888:
  10487. break;
  10488. case DRM_FORMAT_XRGB1555:
  10489. case DRM_FORMAT_ARGB1555:
  10490. if (INTEL_INFO(dev)->gen > 3) {
  10491. DRM_DEBUG("unsupported pixel format: %s\n",
  10492. drm_get_format_name(mode_cmd->pixel_format));
  10493. return -EINVAL;
  10494. }
  10495. break;
  10496. case DRM_FORMAT_XBGR8888:
  10497. case DRM_FORMAT_ABGR8888:
  10498. case DRM_FORMAT_XRGB2101010:
  10499. case DRM_FORMAT_ARGB2101010:
  10500. case DRM_FORMAT_XBGR2101010:
  10501. case DRM_FORMAT_ABGR2101010:
  10502. if (INTEL_INFO(dev)->gen < 4) {
  10503. DRM_DEBUG("unsupported pixel format: %s\n",
  10504. drm_get_format_name(mode_cmd->pixel_format));
  10505. return -EINVAL;
  10506. }
  10507. break;
  10508. case DRM_FORMAT_YUYV:
  10509. case DRM_FORMAT_UYVY:
  10510. case DRM_FORMAT_YVYU:
  10511. case DRM_FORMAT_VYUY:
  10512. if (INTEL_INFO(dev)->gen < 5) {
  10513. DRM_DEBUG("unsupported pixel format: %s\n",
  10514. drm_get_format_name(mode_cmd->pixel_format));
  10515. return -EINVAL;
  10516. }
  10517. break;
  10518. default:
  10519. DRM_DEBUG("unsupported pixel format: %s\n",
  10520. drm_get_format_name(mode_cmd->pixel_format));
  10521. return -EINVAL;
  10522. }
  10523. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10524. if (mode_cmd->offsets[0] != 0)
  10525. return -EINVAL;
  10526. aligned_height = intel_align_height(dev, mode_cmd->height,
  10527. obj->tiling_mode);
  10528. /* FIXME drm helper for size checks (especially planar formats)? */
  10529. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10530. return -EINVAL;
  10531. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10532. intel_fb->obj = obj;
  10533. intel_fb->obj->framebuffer_references++;
  10534. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10535. if (ret) {
  10536. DRM_ERROR("framebuffer init failed %d\n", ret);
  10537. return ret;
  10538. }
  10539. return 0;
  10540. }
  10541. static struct drm_framebuffer *
  10542. intel_user_framebuffer_create(struct drm_device *dev,
  10543. struct drm_file *filp,
  10544. struct drm_mode_fb_cmd2 *mode_cmd)
  10545. {
  10546. struct drm_i915_gem_object *obj;
  10547. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10548. mode_cmd->handles[0]));
  10549. if (&obj->base == NULL)
  10550. return ERR_PTR(-ENOENT);
  10551. return intel_framebuffer_create(dev, mode_cmd, obj);
  10552. }
  10553. #ifndef CONFIG_DRM_I915_FBDEV
  10554. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10555. {
  10556. }
  10557. #endif
  10558. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10559. .fb_create = intel_user_framebuffer_create,
  10560. .output_poll_changed = intel_fbdev_output_poll_changed,
  10561. };
  10562. /* Set up chip specific display functions */
  10563. static void intel_init_display(struct drm_device *dev)
  10564. {
  10565. struct drm_i915_private *dev_priv = dev->dev_private;
  10566. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10567. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10568. else if (IS_CHERRYVIEW(dev))
  10569. dev_priv->display.find_dpll = chv_find_best_dpll;
  10570. else if (IS_VALLEYVIEW(dev))
  10571. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10572. else if (IS_PINEVIEW(dev))
  10573. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10574. else
  10575. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10576. if (HAS_DDI(dev)) {
  10577. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10578. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10579. dev_priv->display.crtc_compute_clock =
  10580. haswell_crtc_compute_clock;
  10581. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10582. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10583. dev_priv->display.off = ironlake_crtc_off;
  10584. if (INTEL_INFO(dev)->gen >= 9)
  10585. dev_priv->display.update_primary_plane =
  10586. skylake_update_primary_plane;
  10587. else
  10588. dev_priv->display.update_primary_plane =
  10589. ironlake_update_primary_plane;
  10590. } else if (HAS_PCH_SPLIT(dev)) {
  10591. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10592. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10593. dev_priv->display.crtc_compute_clock =
  10594. ironlake_crtc_compute_clock;
  10595. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10596. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10597. dev_priv->display.off = ironlake_crtc_off;
  10598. dev_priv->display.update_primary_plane =
  10599. ironlake_update_primary_plane;
  10600. } else if (IS_VALLEYVIEW(dev)) {
  10601. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10602. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10603. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10604. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10605. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10606. dev_priv->display.off = i9xx_crtc_off;
  10607. dev_priv->display.update_primary_plane =
  10608. i9xx_update_primary_plane;
  10609. } else {
  10610. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10611. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10612. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10613. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10614. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10615. dev_priv->display.off = i9xx_crtc_off;
  10616. dev_priv->display.update_primary_plane =
  10617. i9xx_update_primary_plane;
  10618. }
  10619. /* Returns the core display clock speed */
  10620. if (IS_VALLEYVIEW(dev))
  10621. dev_priv->display.get_display_clock_speed =
  10622. valleyview_get_display_clock_speed;
  10623. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10624. dev_priv->display.get_display_clock_speed =
  10625. i945_get_display_clock_speed;
  10626. else if (IS_I915G(dev))
  10627. dev_priv->display.get_display_clock_speed =
  10628. i915_get_display_clock_speed;
  10629. else if (IS_I945GM(dev) || IS_845G(dev))
  10630. dev_priv->display.get_display_clock_speed =
  10631. i9xx_misc_get_display_clock_speed;
  10632. else if (IS_PINEVIEW(dev))
  10633. dev_priv->display.get_display_clock_speed =
  10634. pnv_get_display_clock_speed;
  10635. else if (IS_I915GM(dev))
  10636. dev_priv->display.get_display_clock_speed =
  10637. i915gm_get_display_clock_speed;
  10638. else if (IS_I865G(dev))
  10639. dev_priv->display.get_display_clock_speed =
  10640. i865_get_display_clock_speed;
  10641. else if (IS_I85X(dev))
  10642. dev_priv->display.get_display_clock_speed =
  10643. i855_get_display_clock_speed;
  10644. else /* 852, 830 */
  10645. dev_priv->display.get_display_clock_speed =
  10646. i830_get_display_clock_speed;
  10647. if (IS_GEN5(dev)) {
  10648. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10649. } else if (IS_GEN6(dev)) {
  10650. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10651. } else if (IS_IVYBRIDGE(dev)) {
  10652. /* FIXME: detect B0+ stepping and use auto training */
  10653. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10654. dev_priv->display.modeset_global_resources =
  10655. ivb_modeset_global_resources;
  10656. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10657. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10658. } else if (IS_VALLEYVIEW(dev)) {
  10659. dev_priv->display.modeset_global_resources =
  10660. valleyview_modeset_global_resources;
  10661. }
  10662. /* Default just returns -ENODEV to indicate unsupported */
  10663. dev_priv->display.queue_flip = intel_default_queue_flip;
  10664. switch (INTEL_INFO(dev)->gen) {
  10665. case 2:
  10666. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10667. break;
  10668. case 3:
  10669. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10670. break;
  10671. case 4:
  10672. case 5:
  10673. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10674. break;
  10675. case 6:
  10676. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10677. break;
  10678. case 7:
  10679. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10680. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10681. break;
  10682. case 9:
  10683. dev_priv->display.queue_flip = intel_gen9_queue_flip;
  10684. break;
  10685. }
  10686. intel_panel_init_backlight_funcs(dev);
  10687. mutex_init(&dev_priv->pps_mutex);
  10688. }
  10689. /*
  10690. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10691. * resume, or other times. This quirk makes sure that's the case for
  10692. * affected systems.
  10693. */
  10694. static void quirk_pipea_force(struct drm_device *dev)
  10695. {
  10696. struct drm_i915_private *dev_priv = dev->dev_private;
  10697. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10698. DRM_INFO("applying pipe a force quirk\n");
  10699. }
  10700. static void quirk_pipeb_force(struct drm_device *dev)
  10701. {
  10702. struct drm_i915_private *dev_priv = dev->dev_private;
  10703. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10704. DRM_INFO("applying pipe b force quirk\n");
  10705. }
  10706. /*
  10707. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10708. */
  10709. static void quirk_ssc_force_disable(struct drm_device *dev)
  10710. {
  10711. struct drm_i915_private *dev_priv = dev->dev_private;
  10712. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10713. DRM_INFO("applying lvds SSC disable quirk\n");
  10714. }
  10715. /*
  10716. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10717. * brightness value
  10718. */
  10719. static void quirk_invert_brightness(struct drm_device *dev)
  10720. {
  10721. struct drm_i915_private *dev_priv = dev->dev_private;
  10722. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10723. DRM_INFO("applying inverted panel brightness quirk\n");
  10724. }
  10725. /* Some VBT's incorrectly indicate no backlight is present */
  10726. static void quirk_backlight_present(struct drm_device *dev)
  10727. {
  10728. struct drm_i915_private *dev_priv = dev->dev_private;
  10729. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10730. DRM_INFO("applying backlight present quirk\n");
  10731. }
  10732. struct intel_quirk {
  10733. int device;
  10734. int subsystem_vendor;
  10735. int subsystem_device;
  10736. void (*hook)(struct drm_device *dev);
  10737. };
  10738. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10739. struct intel_dmi_quirk {
  10740. void (*hook)(struct drm_device *dev);
  10741. const struct dmi_system_id (*dmi_id_list)[];
  10742. };
  10743. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10744. {
  10745. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10746. return 1;
  10747. }
  10748. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10749. {
  10750. .dmi_id_list = &(const struct dmi_system_id[]) {
  10751. {
  10752. .callback = intel_dmi_reverse_brightness,
  10753. .ident = "NCR Corporation",
  10754. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10755. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10756. },
  10757. },
  10758. { } /* terminating entry */
  10759. },
  10760. .hook = quirk_invert_brightness,
  10761. },
  10762. };
  10763. static struct intel_quirk intel_quirks[] = {
  10764. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10765. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10766. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10767. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10768. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10769. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10770. /* 830 needs to leave pipe A & dpll A up */
  10771. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10772. /* 830 needs to leave pipe B & dpll B up */
  10773. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10774. /* Lenovo U160 cannot use SSC on LVDS */
  10775. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10776. /* Sony Vaio Y cannot use SSC on LVDS */
  10777. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10778. /* Acer Aspire 5734Z must invert backlight brightness */
  10779. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10780. /* Acer/eMachines G725 */
  10781. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10782. /* Acer/eMachines e725 */
  10783. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10784. /* Acer/Packard Bell NCL20 */
  10785. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10786. /* Acer Aspire 4736Z */
  10787. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10788. /* Acer Aspire 5336 */
  10789. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10790. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10791. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10792. /* Acer C720 Chromebook (Core i3 4005U) */
  10793. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  10794. /* Apple Macbook 2,1 (Core 2 T7400) */
  10795. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  10796. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10797. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10798. /* HP Chromebook 14 (Celeron 2955U) */
  10799. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10800. };
  10801. static void intel_init_quirks(struct drm_device *dev)
  10802. {
  10803. struct pci_dev *d = dev->pdev;
  10804. int i;
  10805. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10806. struct intel_quirk *q = &intel_quirks[i];
  10807. if (d->device == q->device &&
  10808. (d->subsystem_vendor == q->subsystem_vendor ||
  10809. q->subsystem_vendor == PCI_ANY_ID) &&
  10810. (d->subsystem_device == q->subsystem_device ||
  10811. q->subsystem_device == PCI_ANY_ID))
  10812. q->hook(dev);
  10813. }
  10814. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10815. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10816. intel_dmi_quirks[i].hook(dev);
  10817. }
  10818. }
  10819. /* Disable the VGA plane that we never use */
  10820. static void i915_disable_vga(struct drm_device *dev)
  10821. {
  10822. struct drm_i915_private *dev_priv = dev->dev_private;
  10823. u8 sr1;
  10824. u32 vga_reg = i915_vgacntrl_reg(dev);
  10825. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10826. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10827. outb(SR01, VGA_SR_INDEX);
  10828. sr1 = inb(VGA_SR_DATA);
  10829. outb(sr1 | 1<<5, VGA_SR_DATA);
  10830. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10831. udelay(300);
  10832. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  10833. POSTING_READ(vga_reg);
  10834. }
  10835. void intel_modeset_init_hw(struct drm_device *dev)
  10836. {
  10837. intel_prepare_ddi(dev);
  10838. if (IS_VALLEYVIEW(dev))
  10839. vlv_update_cdclk(dev);
  10840. intel_init_clock_gating(dev);
  10841. intel_enable_gt_powersave(dev);
  10842. }
  10843. void intel_modeset_init(struct drm_device *dev)
  10844. {
  10845. struct drm_i915_private *dev_priv = dev->dev_private;
  10846. int sprite, ret;
  10847. enum pipe pipe;
  10848. struct intel_crtc *crtc;
  10849. drm_mode_config_init(dev);
  10850. dev->mode_config.min_width = 0;
  10851. dev->mode_config.min_height = 0;
  10852. dev->mode_config.preferred_depth = 24;
  10853. dev->mode_config.prefer_shadow = 1;
  10854. dev->mode_config.funcs = &intel_mode_funcs;
  10855. intel_init_quirks(dev);
  10856. intel_init_pm(dev);
  10857. if (INTEL_INFO(dev)->num_pipes == 0)
  10858. return;
  10859. intel_init_display(dev);
  10860. intel_init_audio(dev);
  10861. if (IS_GEN2(dev)) {
  10862. dev->mode_config.max_width = 2048;
  10863. dev->mode_config.max_height = 2048;
  10864. } else if (IS_GEN3(dev)) {
  10865. dev->mode_config.max_width = 4096;
  10866. dev->mode_config.max_height = 4096;
  10867. } else {
  10868. dev->mode_config.max_width = 8192;
  10869. dev->mode_config.max_height = 8192;
  10870. }
  10871. if (IS_845G(dev) || IS_I865G(dev)) {
  10872. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  10873. dev->mode_config.cursor_height = 1023;
  10874. } else if (IS_GEN2(dev)) {
  10875. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10876. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10877. } else {
  10878. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10879. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10880. }
  10881. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10882. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10883. INTEL_INFO(dev)->num_pipes,
  10884. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10885. for_each_pipe(dev_priv, pipe) {
  10886. intel_crtc_init(dev, pipe);
  10887. for_each_sprite(pipe, sprite) {
  10888. ret = intel_plane_init(dev, pipe, sprite);
  10889. if (ret)
  10890. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10891. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10892. }
  10893. }
  10894. intel_init_dpio(dev);
  10895. intel_shared_dpll_init(dev);
  10896. /* Just disable it once at startup */
  10897. i915_disable_vga(dev);
  10898. intel_setup_outputs(dev);
  10899. /* Just in case the BIOS is doing something questionable. */
  10900. intel_fbc_disable(dev);
  10901. drm_modeset_lock_all(dev);
  10902. intel_modeset_setup_hw_state(dev, false);
  10903. drm_modeset_unlock_all(dev);
  10904. for_each_intel_crtc(dev, crtc) {
  10905. if (!crtc->active)
  10906. continue;
  10907. /*
  10908. * Note that reserving the BIOS fb up front prevents us
  10909. * from stuffing other stolen allocations like the ring
  10910. * on top. This prevents some ugliness at boot time, and
  10911. * can even allow for smooth boot transitions if the BIOS
  10912. * fb is large enough for the active pipe configuration.
  10913. */
  10914. if (dev_priv->display.get_plane_config) {
  10915. dev_priv->display.get_plane_config(crtc,
  10916. &crtc->plane_config);
  10917. /*
  10918. * If the fb is shared between multiple heads, we'll
  10919. * just get the first one.
  10920. */
  10921. intel_find_plane_obj(crtc, &crtc->plane_config);
  10922. }
  10923. }
  10924. }
  10925. static void intel_enable_pipe_a(struct drm_device *dev)
  10926. {
  10927. struct intel_connector *connector;
  10928. struct drm_connector *crt = NULL;
  10929. struct intel_load_detect_pipe load_detect_temp;
  10930. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  10931. /* We can't just switch on the pipe A, we need to set things up with a
  10932. * proper mode and output configuration. As a gross hack, enable pipe A
  10933. * by enabling the load detect pipe once. */
  10934. list_for_each_entry(connector,
  10935. &dev->mode_config.connector_list,
  10936. base.head) {
  10937. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  10938. crt = &connector->base;
  10939. break;
  10940. }
  10941. }
  10942. if (!crt)
  10943. return;
  10944. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  10945. intel_release_load_detect_pipe(crt, &load_detect_temp);
  10946. }
  10947. static bool
  10948. intel_check_plane_mapping(struct intel_crtc *crtc)
  10949. {
  10950. struct drm_device *dev = crtc->base.dev;
  10951. struct drm_i915_private *dev_priv = dev->dev_private;
  10952. u32 reg, val;
  10953. if (INTEL_INFO(dev)->num_pipes == 1)
  10954. return true;
  10955. reg = DSPCNTR(!crtc->plane);
  10956. val = I915_READ(reg);
  10957. if ((val & DISPLAY_PLANE_ENABLE) &&
  10958. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  10959. return false;
  10960. return true;
  10961. }
  10962. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  10963. {
  10964. struct drm_device *dev = crtc->base.dev;
  10965. struct drm_i915_private *dev_priv = dev->dev_private;
  10966. u32 reg;
  10967. /* Clear any frame start delays used for debugging left by the BIOS */
  10968. reg = PIPECONF(crtc->config.cpu_transcoder);
  10969. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  10970. /* restore vblank interrupts to correct state */
  10971. if (crtc->active) {
  10972. update_scanline_offset(crtc);
  10973. drm_vblank_on(dev, crtc->pipe);
  10974. } else
  10975. drm_vblank_off(dev, crtc->pipe);
  10976. /* We need to sanitize the plane -> pipe mapping first because this will
  10977. * disable the crtc (and hence change the state) if it is wrong. Note
  10978. * that gen4+ has a fixed plane -> pipe mapping. */
  10979. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  10980. struct intel_connector *connector;
  10981. bool plane;
  10982. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  10983. crtc->base.base.id);
  10984. /* Pipe has the wrong plane attached and the plane is active.
  10985. * Temporarily change the plane mapping and disable everything
  10986. * ... */
  10987. plane = crtc->plane;
  10988. crtc->plane = !plane;
  10989. crtc->primary_enabled = true;
  10990. dev_priv->display.crtc_disable(&crtc->base);
  10991. crtc->plane = plane;
  10992. /* ... and break all links. */
  10993. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10994. base.head) {
  10995. if (connector->encoder->base.crtc != &crtc->base)
  10996. continue;
  10997. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10998. connector->base.encoder = NULL;
  10999. }
  11000. /* multiple connectors may have the same encoder:
  11001. * handle them and break crtc link separately */
  11002. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11003. base.head)
  11004. if (connector->encoder->base.crtc == &crtc->base) {
  11005. connector->encoder->base.crtc = NULL;
  11006. connector->encoder->connectors_active = false;
  11007. }
  11008. WARN_ON(crtc->active);
  11009. crtc->base.enabled = false;
  11010. }
  11011. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11012. crtc->pipe == PIPE_A && !crtc->active) {
  11013. /* BIOS forgot to enable pipe A, this mostly happens after
  11014. * resume. Force-enable the pipe to fix this, the update_dpms
  11015. * call below we restore the pipe to the right state, but leave
  11016. * the required bits on. */
  11017. intel_enable_pipe_a(dev);
  11018. }
  11019. /* Adjust the state of the output pipe according to whether we
  11020. * have active connectors/encoders. */
  11021. intel_crtc_update_dpms(&crtc->base);
  11022. if (crtc->active != crtc->base.enabled) {
  11023. struct intel_encoder *encoder;
  11024. /* This can happen either due to bugs in the get_hw_state
  11025. * functions or because the pipe is force-enabled due to the
  11026. * pipe A quirk. */
  11027. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11028. crtc->base.base.id,
  11029. crtc->base.enabled ? "enabled" : "disabled",
  11030. crtc->active ? "enabled" : "disabled");
  11031. crtc->base.enabled = crtc->active;
  11032. /* Because we only establish the connector -> encoder ->
  11033. * crtc links if something is active, this means the
  11034. * crtc is now deactivated. Break the links. connector
  11035. * -> encoder links are only establish when things are
  11036. * actually up, hence no need to break them. */
  11037. WARN_ON(crtc->active);
  11038. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11039. WARN_ON(encoder->connectors_active);
  11040. encoder->base.crtc = NULL;
  11041. }
  11042. }
  11043. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11044. /*
  11045. * We start out with underrun reporting disabled to avoid races.
  11046. * For correct bookkeeping mark this on active crtcs.
  11047. *
  11048. * Also on gmch platforms we dont have any hardware bits to
  11049. * disable the underrun reporting. Which means we need to start
  11050. * out with underrun reporting disabled also on inactive pipes,
  11051. * since otherwise we'll complain about the garbage we read when
  11052. * e.g. coming up after runtime pm.
  11053. *
  11054. * No protection against concurrent access is required - at
  11055. * worst a fifo underrun happens which also sets this to false.
  11056. */
  11057. crtc->cpu_fifo_underrun_disabled = true;
  11058. crtc->pch_fifo_underrun_disabled = true;
  11059. }
  11060. }
  11061. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11062. {
  11063. struct intel_connector *connector;
  11064. struct drm_device *dev = encoder->base.dev;
  11065. /* We need to check both for a crtc link (meaning that the
  11066. * encoder is active and trying to read from a pipe) and the
  11067. * pipe itself being active. */
  11068. bool has_active_crtc = encoder->base.crtc &&
  11069. to_intel_crtc(encoder->base.crtc)->active;
  11070. if (encoder->connectors_active && !has_active_crtc) {
  11071. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11072. encoder->base.base.id,
  11073. encoder->base.name);
  11074. /* Connector is active, but has no active pipe. This is
  11075. * fallout from our resume register restoring. Disable
  11076. * the encoder manually again. */
  11077. if (encoder->base.crtc) {
  11078. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11079. encoder->base.base.id,
  11080. encoder->base.name);
  11081. encoder->disable(encoder);
  11082. if (encoder->post_disable)
  11083. encoder->post_disable(encoder);
  11084. }
  11085. encoder->base.crtc = NULL;
  11086. encoder->connectors_active = false;
  11087. /* Inconsistent output/port/pipe state happens presumably due to
  11088. * a bug in one of the get_hw_state functions. Or someplace else
  11089. * in our code, like the register restore mess on resume. Clamp
  11090. * things to off as a safer default. */
  11091. list_for_each_entry(connector,
  11092. &dev->mode_config.connector_list,
  11093. base.head) {
  11094. if (connector->encoder != encoder)
  11095. continue;
  11096. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11097. connector->base.encoder = NULL;
  11098. }
  11099. }
  11100. /* Enabled encoders without active connectors will be fixed in
  11101. * the crtc fixup. */
  11102. }
  11103. void i915_redisable_vga_power_on(struct drm_device *dev)
  11104. {
  11105. struct drm_i915_private *dev_priv = dev->dev_private;
  11106. u32 vga_reg = i915_vgacntrl_reg(dev);
  11107. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11108. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11109. i915_disable_vga(dev);
  11110. }
  11111. }
  11112. void i915_redisable_vga(struct drm_device *dev)
  11113. {
  11114. struct drm_i915_private *dev_priv = dev->dev_private;
  11115. /* This function can be called both from intel_modeset_setup_hw_state or
  11116. * at a very early point in our resume sequence, where the power well
  11117. * structures are not yet restored. Since this function is at a very
  11118. * paranoid "someone might have enabled VGA while we were not looking"
  11119. * level, just check if the power well is enabled instead of trying to
  11120. * follow the "don't touch the power well if we don't need it" policy
  11121. * the rest of the driver uses. */
  11122. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11123. return;
  11124. i915_redisable_vga_power_on(dev);
  11125. }
  11126. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11127. {
  11128. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11129. if (!crtc->active)
  11130. return false;
  11131. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11132. }
  11133. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11134. {
  11135. struct drm_i915_private *dev_priv = dev->dev_private;
  11136. enum pipe pipe;
  11137. struct intel_crtc *crtc;
  11138. struct intel_encoder *encoder;
  11139. struct intel_connector *connector;
  11140. int i;
  11141. for_each_intel_crtc(dev, crtc) {
  11142. memset(&crtc->config, 0, sizeof(crtc->config));
  11143. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11144. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11145. &crtc->config);
  11146. crtc->base.enabled = crtc->active;
  11147. crtc->primary_enabled = primary_get_hw_state(crtc);
  11148. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11149. crtc->base.base.id,
  11150. crtc->active ? "enabled" : "disabled");
  11151. }
  11152. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11153. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11154. pll->on = pll->get_hw_state(dev_priv, pll,
  11155. &pll->config.hw_state);
  11156. pll->active = 0;
  11157. pll->config.crtc_mask = 0;
  11158. for_each_intel_crtc(dev, crtc) {
  11159. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11160. pll->active++;
  11161. pll->config.crtc_mask |= 1 << crtc->pipe;
  11162. }
  11163. }
  11164. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11165. pll->name, pll->config.crtc_mask, pll->on);
  11166. if (pll->config.crtc_mask)
  11167. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11168. }
  11169. for_each_intel_encoder(dev, encoder) {
  11170. pipe = 0;
  11171. if (encoder->get_hw_state(encoder, &pipe)) {
  11172. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11173. encoder->base.crtc = &crtc->base;
  11174. encoder->get_config(encoder, &crtc->config);
  11175. } else {
  11176. encoder->base.crtc = NULL;
  11177. }
  11178. encoder->connectors_active = false;
  11179. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11180. encoder->base.base.id,
  11181. encoder->base.name,
  11182. encoder->base.crtc ? "enabled" : "disabled",
  11183. pipe_name(pipe));
  11184. }
  11185. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11186. base.head) {
  11187. if (connector->get_hw_state(connector)) {
  11188. connector->base.dpms = DRM_MODE_DPMS_ON;
  11189. connector->encoder->connectors_active = true;
  11190. connector->base.encoder = &connector->encoder->base;
  11191. } else {
  11192. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11193. connector->base.encoder = NULL;
  11194. }
  11195. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11196. connector->base.base.id,
  11197. connector->base.name,
  11198. connector->base.encoder ? "enabled" : "disabled");
  11199. }
  11200. }
  11201. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11202. * and i915 state tracking structures. */
  11203. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11204. bool force_restore)
  11205. {
  11206. struct drm_i915_private *dev_priv = dev->dev_private;
  11207. enum pipe pipe;
  11208. struct intel_crtc *crtc;
  11209. struct intel_encoder *encoder;
  11210. int i;
  11211. intel_modeset_readout_hw_state(dev);
  11212. /*
  11213. * Now that we have the config, copy it to each CRTC struct
  11214. * Note that this could go away if we move to using crtc_config
  11215. * checking everywhere.
  11216. */
  11217. for_each_intel_crtc(dev, crtc) {
  11218. if (crtc->active && i915.fastboot) {
  11219. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  11220. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11221. crtc->base.base.id);
  11222. drm_mode_debug_printmodeline(&crtc->base.mode);
  11223. }
  11224. }
  11225. /* HW state is read out, now we need to sanitize this mess. */
  11226. for_each_intel_encoder(dev, encoder) {
  11227. intel_sanitize_encoder(encoder);
  11228. }
  11229. for_each_pipe(dev_priv, pipe) {
  11230. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11231. intel_sanitize_crtc(crtc);
  11232. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  11233. }
  11234. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11235. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11236. if (!pll->on || pll->active)
  11237. continue;
  11238. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11239. pll->disable(dev_priv, pll);
  11240. pll->on = false;
  11241. }
  11242. if (IS_GEN9(dev))
  11243. skl_wm_get_hw_state(dev);
  11244. else if (HAS_PCH_SPLIT(dev))
  11245. ilk_wm_get_hw_state(dev);
  11246. if (force_restore) {
  11247. i915_redisable_vga(dev);
  11248. /*
  11249. * We need to use raw interfaces for restoring state to avoid
  11250. * checking (bogus) intermediate states.
  11251. */
  11252. for_each_pipe(dev_priv, pipe) {
  11253. struct drm_crtc *crtc =
  11254. dev_priv->pipe_to_crtc_mapping[pipe];
  11255. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11256. crtc->primary->fb);
  11257. }
  11258. } else {
  11259. intel_modeset_update_staged_output_state(dev);
  11260. }
  11261. intel_modeset_check_state(dev);
  11262. }
  11263. void intel_modeset_gem_init(struct drm_device *dev)
  11264. {
  11265. struct drm_i915_private *dev_priv = dev->dev_private;
  11266. struct drm_crtc *c;
  11267. struct drm_i915_gem_object *obj;
  11268. mutex_lock(&dev->struct_mutex);
  11269. intel_init_gt_powersave(dev);
  11270. mutex_unlock(&dev->struct_mutex);
  11271. /*
  11272. * There may be no VBT; and if the BIOS enabled SSC we can
  11273. * just keep using it to avoid unnecessary flicker. Whereas if the
  11274. * BIOS isn't using it, don't assume it will work even if the VBT
  11275. * indicates as much.
  11276. */
  11277. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11278. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11279. DREF_SSC1_ENABLE);
  11280. intel_modeset_init_hw(dev);
  11281. intel_setup_overlay(dev);
  11282. /*
  11283. * Make sure any fbs we allocated at startup are properly
  11284. * pinned & fenced. When we do the allocation it's too early
  11285. * for this.
  11286. */
  11287. mutex_lock(&dev->struct_mutex);
  11288. for_each_crtc(dev, c) {
  11289. obj = intel_fb_obj(c->primary->fb);
  11290. if (obj == NULL)
  11291. continue;
  11292. if (intel_pin_and_fence_fb_obj(c->primary,
  11293. c->primary->fb,
  11294. NULL)) {
  11295. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11296. to_intel_crtc(c)->pipe);
  11297. drm_framebuffer_unreference(c->primary->fb);
  11298. c->primary->fb = NULL;
  11299. }
  11300. }
  11301. mutex_unlock(&dev->struct_mutex);
  11302. intel_backlight_register(dev);
  11303. }
  11304. void intel_connector_unregister(struct intel_connector *intel_connector)
  11305. {
  11306. struct drm_connector *connector = &intel_connector->base;
  11307. intel_panel_destroy_backlight(connector);
  11308. drm_connector_unregister(connector);
  11309. }
  11310. void intel_modeset_cleanup(struct drm_device *dev)
  11311. {
  11312. struct drm_i915_private *dev_priv = dev->dev_private;
  11313. struct drm_connector *connector;
  11314. intel_disable_gt_powersave(dev);
  11315. intel_backlight_unregister(dev);
  11316. /*
  11317. * Interrupts and polling as the first thing to avoid creating havoc.
  11318. * Too much stuff here (turning of connectors, ...) would
  11319. * experience fancy races otherwise.
  11320. */
  11321. intel_irq_uninstall(dev_priv);
  11322. /*
  11323. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11324. * poll handlers. Hence disable polling after hpd handling is shut down.
  11325. */
  11326. drm_kms_helper_poll_fini(dev);
  11327. mutex_lock(&dev->struct_mutex);
  11328. intel_unregister_dsm_handler();
  11329. intel_fbc_disable(dev);
  11330. ironlake_teardown_rc6(dev);
  11331. mutex_unlock(&dev->struct_mutex);
  11332. /* flush any delayed tasks or pending work */
  11333. flush_scheduled_work();
  11334. /* destroy the backlight and sysfs files before encoders/connectors */
  11335. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11336. struct intel_connector *intel_connector;
  11337. intel_connector = to_intel_connector(connector);
  11338. intel_connector->unregister(intel_connector);
  11339. }
  11340. drm_mode_config_cleanup(dev);
  11341. intel_cleanup_overlay(dev);
  11342. mutex_lock(&dev->struct_mutex);
  11343. intel_cleanup_gt_powersave(dev);
  11344. mutex_unlock(&dev->struct_mutex);
  11345. }
  11346. /*
  11347. * Return which encoder is currently attached for connector.
  11348. */
  11349. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11350. {
  11351. return &intel_attached_encoder(connector)->base;
  11352. }
  11353. void intel_connector_attach_encoder(struct intel_connector *connector,
  11354. struct intel_encoder *encoder)
  11355. {
  11356. connector->encoder = encoder;
  11357. drm_mode_connector_attach_encoder(&connector->base,
  11358. &encoder->base);
  11359. }
  11360. /*
  11361. * set vga decode state - true == enable VGA decode
  11362. */
  11363. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11364. {
  11365. struct drm_i915_private *dev_priv = dev->dev_private;
  11366. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11367. u16 gmch_ctrl;
  11368. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11369. DRM_ERROR("failed to read control word\n");
  11370. return -EIO;
  11371. }
  11372. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11373. return 0;
  11374. if (state)
  11375. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11376. else
  11377. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11378. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11379. DRM_ERROR("failed to write control word\n");
  11380. return -EIO;
  11381. }
  11382. return 0;
  11383. }
  11384. struct intel_display_error_state {
  11385. u32 power_well_driver;
  11386. int num_transcoders;
  11387. struct intel_cursor_error_state {
  11388. u32 control;
  11389. u32 position;
  11390. u32 base;
  11391. u32 size;
  11392. } cursor[I915_MAX_PIPES];
  11393. struct intel_pipe_error_state {
  11394. bool power_domain_on;
  11395. u32 source;
  11396. u32 stat;
  11397. } pipe[I915_MAX_PIPES];
  11398. struct intel_plane_error_state {
  11399. u32 control;
  11400. u32 stride;
  11401. u32 size;
  11402. u32 pos;
  11403. u32 addr;
  11404. u32 surface;
  11405. u32 tile_offset;
  11406. } plane[I915_MAX_PIPES];
  11407. struct intel_transcoder_error_state {
  11408. bool power_domain_on;
  11409. enum transcoder cpu_transcoder;
  11410. u32 conf;
  11411. u32 htotal;
  11412. u32 hblank;
  11413. u32 hsync;
  11414. u32 vtotal;
  11415. u32 vblank;
  11416. u32 vsync;
  11417. } transcoder[4];
  11418. };
  11419. struct intel_display_error_state *
  11420. intel_display_capture_error_state(struct drm_device *dev)
  11421. {
  11422. struct drm_i915_private *dev_priv = dev->dev_private;
  11423. struct intel_display_error_state *error;
  11424. int transcoders[] = {
  11425. TRANSCODER_A,
  11426. TRANSCODER_B,
  11427. TRANSCODER_C,
  11428. TRANSCODER_EDP,
  11429. };
  11430. int i;
  11431. if (INTEL_INFO(dev)->num_pipes == 0)
  11432. return NULL;
  11433. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11434. if (error == NULL)
  11435. return NULL;
  11436. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11437. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11438. for_each_pipe(dev_priv, i) {
  11439. error->pipe[i].power_domain_on =
  11440. __intel_display_power_is_enabled(dev_priv,
  11441. POWER_DOMAIN_PIPE(i));
  11442. if (!error->pipe[i].power_domain_on)
  11443. continue;
  11444. error->cursor[i].control = I915_READ(CURCNTR(i));
  11445. error->cursor[i].position = I915_READ(CURPOS(i));
  11446. error->cursor[i].base = I915_READ(CURBASE(i));
  11447. error->plane[i].control = I915_READ(DSPCNTR(i));
  11448. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11449. if (INTEL_INFO(dev)->gen <= 3) {
  11450. error->plane[i].size = I915_READ(DSPSIZE(i));
  11451. error->plane[i].pos = I915_READ(DSPPOS(i));
  11452. }
  11453. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11454. error->plane[i].addr = I915_READ(DSPADDR(i));
  11455. if (INTEL_INFO(dev)->gen >= 4) {
  11456. error->plane[i].surface = I915_READ(DSPSURF(i));
  11457. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11458. }
  11459. error->pipe[i].source = I915_READ(PIPESRC(i));
  11460. if (HAS_GMCH_DISPLAY(dev))
  11461. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11462. }
  11463. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11464. if (HAS_DDI(dev_priv->dev))
  11465. error->num_transcoders++; /* Account for eDP. */
  11466. for (i = 0; i < error->num_transcoders; i++) {
  11467. enum transcoder cpu_transcoder = transcoders[i];
  11468. error->transcoder[i].power_domain_on =
  11469. __intel_display_power_is_enabled(dev_priv,
  11470. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11471. if (!error->transcoder[i].power_domain_on)
  11472. continue;
  11473. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11474. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11475. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11476. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11477. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11478. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11479. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11480. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11481. }
  11482. return error;
  11483. }
  11484. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11485. void
  11486. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11487. struct drm_device *dev,
  11488. struct intel_display_error_state *error)
  11489. {
  11490. struct drm_i915_private *dev_priv = dev->dev_private;
  11491. int i;
  11492. if (!error)
  11493. return;
  11494. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11495. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11496. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11497. error->power_well_driver);
  11498. for_each_pipe(dev_priv, i) {
  11499. err_printf(m, "Pipe [%d]:\n", i);
  11500. err_printf(m, " Power: %s\n",
  11501. error->pipe[i].power_domain_on ? "on" : "off");
  11502. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11503. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11504. err_printf(m, "Plane [%d]:\n", i);
  11505. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11506. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11507. if (INTEL_INFO(dev)->gen <= 3) {
  11508. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11509. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11510. }
  11511. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11512. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11513. if (INTEL_INFO(dev)->gen >= 4) {
  11514. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11515. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11516. }
  11517. err_printf(m, "Cursor [%d]:\n", i);
  11518. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11519. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11520. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11521. }
  11522. for (i = 0; i < error->num_transcoders; i++) {
  11523. err_printf(m, "CPU transcoder: %c\n",
  11524. transcoder_name(error->transcoder[i].cpu_transcoder));
  11525. err_printf(m, " Power: %s\n",
  11526. error->transcoder[i].power_domain_on ? "on" : "off");
  11527. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11528. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11529. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11530. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11531. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11532. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11533. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11534. }
  11535. }
  11536. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11537. {
  11538. struct intel_crtc *crtc;
  11539. for_each_intel_crtc(dev, crtc) {
  11540. struct intel_unpin_work *work;
  11541. spin_lock_irq(&dev->event_lock);
  11542. work = crtc->unpin_work;
  11543. if (work && work->event &&
  11544. work->event->base.file_priv == file) {
  11545. kfree(work->event);
  11546. work->event = NULL;
  11547. }
  11548. spin_unlock_irq(&dev->event_lock);
  11549. }
  11550. }