intel_display.c 396 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. /* Primary plane formats supported by all gen */
  47. #define COMMON_PRIMARY_FORMATS \
  48. DRM_FORMAT_C8, \
  49. DRM_FORMAT_RGB565, \
  50. DRM_FORMAT_XRGB8888, \
  51. DRM_FORMAT_ARGB8888
  52. /* Primary plane formats for gen <= 3 */
  53. static const uint32_t intel_primary_formats_gen2[] = {
  54. COMMON_PRIMARY_FORMATS,
  55. DRM_FORMAT_XRGB1555,
  56. DRM_FORMAT_ARGB1555,
  57. };
  58. /* Primary plane formats for gen >= 4 */
  59. static const uint32_t intel_primary_formats_gen4[] = {
  60. COMMON_PRIMARY_FORMATS, \
  61. DRM_FORMAT_XBGR8888,
  62. DRM_FORMAT_ABGR8888,
  63. DRM_FORMAT_XRGB2101010,
  64. DRM_FORMAT_ARGB2101010,
  65. DRM_FORMAT_XBGR2101010,
  66. DRM_FORMAT_ABGR2101010,
  67. };
  68. /* Cursor formats */
  69. static const uint32_t intel_cursor_formats[] = {
  70. DRM_FORMAT_ARGB8888,
  71. };
  72. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  73. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  74. struct intel_crtc_state *pipe_config);
  75. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  76. struct intel_crtc_state *pipe_config);
  77. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  78. int x, int y, struct drm_framebuffer *old_fb);
  79. static int intel_framebuffer_init(struct drm_device *dev,
  80. struct intel_framebuffer *ifb,
  81. struct drm_mode_fb_cmd2 *mode_cmd,
  82. struct drm_i915_gem_object *obj);
  83. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  84. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  85. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  86. struct intel_link_m_n *m_n,
  87. struct intel_link_m_n *m2_n2);
  88. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  89. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  90. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  91. static void vlv_prepare_pll(struct intel_crtc *crtc,
  92. const struct intel_crtc_state *pipe_config);
  93. static void chv_prepare_pll(struct intel_crtc *crtc,
  94. const struct intel_crtc_state *pipe_config);
  95. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  96. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  97. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  98. {
  99. if (!connector->mst_port)
  100. return connector->encoder;
  101. else
  102. return &connector->mst_port->mst_encoders[pipe]->base;
  103. }
  104. typedef struct {
  105. int min, max;
  106. } intel_range_t;
  107. typedef struct {
  108. int dot_limit;
  109. int p2_slow, p2_fast;
  110. } intel_p2_t;
  111. typedef struct intel_limit intel_limit_t;
  112. struct intel_limit {
  113. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  114. intel_p2_t p2;
  115. };
  116. int
  117. intel_pch_rawclk(struct drm_device *dev)
  118. {
  119. struct drm_i915_private *dev_priv = dev->dev_private;
  120. WARN_ON(!HAS_PCH_SPLIT(dev));
  121. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  122. }
  123. static inline u32 /* units of 100MHz */
  124. intel_fdi_link_freq(struct drm_device *dev)
  125. {
  126. if (IS_GEN5(dev)) {
  127. struct drm_i915_private *dev_priv = dev->dev_private;
  128. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  129. } else
  130. return 27;
  131. }
  132. static const intel_limit_t intel_limits_i8xx_dac = {
  133. .dot = { .min = 25000, .max = 350000 },
  134. .vco = { .min = 908000, .max = 1512000 },
  135. .n = { .min = 2, .max = 16 },
  136. .m = { .min = 96, .max = 140 },
  137. .m1 = { .min = 18, .max = 26 },
  138. .m2 = { .min = 6, .max = 16 },
  139. .p = { .min = 4, .max = 128 },
  140. .p1 = { .min = 2, .max = 33 },
  141. .p2 = { .dot_limit = 165000,
  142. .p2_slow = 4, .p2_fast = 2 },
  143. };
  144. static const intel_limit_t intel_limits_i8xx_dvo = {
  145. .dot = { .min = 25000, .max = 350000 },
  146. .vco = { .min = 908000, .max = 1512000 },
  147. .n = { .min = 2, .max = 16 },
  148. .m = { .min = 96, .max = 140 },
  149. .m1 = { .min = 18, .max = 26 },
  150. .m2 = { .min = 6, .max = 16 },
  151. .p = { .min = 4, .max = 128 },
  152. .p1 = { .min = 2, .max = 33 },
  153. .p2 = { .dot_limit = 165000,
  154. .p2_slow = 4, .p2_fast = 4 },
  155. };
  156. static const intel_limit_t intel_limits_i8xx_lvds = {
  157. .dot = { .min = 25000, .max = 350000 },
  158. .vco = { .min = 908000, .max = 1512000 },
  159. .n = { .min = 2, .max = 16 },
  160. .m = { .min = 96, .max = 140 },
  161. .m1 = { .min = 18, .max = 26 },
  162. .m2 = { .min = 6, .max = 16 },
  163. .p = { .min = 4, .max = 128 },
  164. .p1 = { .min = 1, .max = 6 },
  165. .p2 = { .dot_limit = 165000,
  166. .p2_slow = 14, .p2_fast = 7 },
  167. };
  168. static const intel_limit_t intel_limits_i9xx_sdvo = {
  169. .dot = { .min = 20000, .max = 400000 },
  170. .vco = { .min = 1400000, .max = 2800000 },
  171. .n = { .min = 1, .max = 6 },
  172. .m = { .min = 70, .max = 120 },
  173. .m1 = { .min = 8, .max = 18 },
  174. .m2 = { .min = 3, .max = 7 },
  175. .p = { .min = 5, .max = 80 },
  176. .p1 = { .min = 1, .max = 8 },
  177. .p2 = { .dot_limit = 200000,
  178. .p2_slow = 10, .p2_fast = 5 },
  179. };
  180. static const intel_limit_t intel_limits_i9xx_lvds = {
  181. .dot = { .min = 20000, .max = 400000 },
  182. .vco = { .min = 1400000, .max = 2800000 },
  183. .n = { .min = 1, .max = 6 },
  184. .m = { .min = 70, .max = 120 },
  185. .m1 = { .min = 8, .max = 18 },
  186. .m2 = { .min = 3, .max = 7 },
  187. .p = { .min = 7, .max = 98 },
  188. .p1 = { .min = 1, .max = 8 },
  189. .p2 = { .dot_limit = 112000,
  190. .p2_slow = 14, .p2_fast = 7 },
  191. };
  192. static const intel_limit_t intel_limits_g4x_sdvo = {
  193. .dot = { .min = 25000, .max = 270000 },
  194. .vco = { .min = 1750000, .max = 3500000},
  195. .n = { .min = 1, .max = 4 },
  196. .m = { .min = 104, .max = 138 },
  197. .m1 = { .min = 17, .max = 23 },
  198. .m2 = { .min = 5, .max = 11 },
  199. .p = { .min = 10, .max = 30 },
  200. .p1 = { .min = 1, .max = 3},
  201. .p2 = { .dot_limit = 270000,
  202. .p2_slow = 10,
  203. .p2_fast = 10
  204. },
  205. };
  206. static const intel_limit_t intel_limits_g4x_hdmi = {
  207. .dot = { .min = 22000, .max = 400000 },
  208. .vco = { .min = 1750000, .max = 3500000},
  209. .n = { .min = 1, .max = 4 },
  210. .m = { .min = 104, .max = 138 },
  211. .m1 = { .min = 16, .max = 23 },
  212. .m2 = { .min = 5, .max = 11 },
  213. .p = { .min = 5, .max = 80 },
  214. .p1 = { .min = 1, .max = 8},
  215. .p2 = { .dot_limit = 165000,
  216. .p2_slow = 10, .p2_fast = 5 },
  217. };
  218. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  219. .dot = { .min = 20000, .max = 115000 },
  220. .vco = { .min = 1750000, .max = 3500000 },
  221. .n = { .min = 1, .max = 3 },
  222. .m = { .min = 104, .max = 138 },
  223. .m1 = { .min = 17, .max = 23 },
  224. .m2 = { .min = 5, .max = 11 },
  225. .p = { .min = 28, .max = 112 },
  226. .p1 = { .min = 2, .max = 8 },
  227. .p2 = { .dot_limit = 0,
  228. .p2_slow = 14, .p2_fast = 14
  229. },
  230. };
  231. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  232. .dot = { .min = 80000, .max = 224000 },
  233. .vco = { .min = 1750000, .max = 3500000 },
  234. .n = { .min = 1, .max = 3 },
  235. .m = { .min = 104, .max = 138 },
  236. .m1 = { .min = 17, .max = 23 },
  237. .m2 = { .min = 5, .max = 11 },
  238. .p = { .min = 14, .max = 42 },
  239. .p1 = { .min = 2, .max = 6 },
  240. .p2 = { .dot_limit = 0,
  241. .p2_slow = 7, .p2_fast = 7
  242. },
  243. };
  244. static const intel_limit_t intel_limits_pineview_sdvo = {
  245. .dot = { .min = 20000, .max = 400000},
  246. .vco = { .min = 1700000, .max = 3500000 },
  247. /* Pineview's Ncounter is a ring counter */
  248. .n = { .min = 3, .max = 6 },
  249. .m = { .min = 2, .max = 256 },
  250. /* Pineview only has one combined m divider, which we treat as m2. */
  251. .m1 = { .min = 0, .max = 0 },
  252. .m2 = { .min = 0, .max = 254 },
  253. .p = { .min = 5, .max = 80 },
  254. .p1 = { .min = 1, .max = 8 },
  255. .p2 = { .dot_limit = 200000,
  256. .p2_slow = 10, .p2_fast = 5 },
  257. };
  258. static const intel_limit_t intel_limits_pineview_lvds = {
  259. .dot = { .min = 20000, .max = 400000 },
  260. .vco = { .min = 1700000, .max = 3500000 },
  261. .n = { .min = 3, .max = 6 },
  262. .m = { .min = 2, .max = 256 },
  263. .m1 = { .min = 0, .max = 0 },
  264. .m2 = { .min = 0, .max = 254 },
  265. .p = { .min = 7, .max = 112 },
  266. .p1 = { .min = 1, .max = 8 },
  267. .p2 = { .dot_limit = 112000,
  268. .p2_slow = 14, .p2_fast = 14 },
  269. };
  270. /* Ironlake / Sandybridge
  271. *
  272. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  273. * the range value for them is (actual_value - 2).
  274. */
  275. static const intel_limit_t intel_limits_ironlake_dac = {
  276. .dot = { .min = 25000, .max = 350000 },
  277. .vco = { .min = 1760000, .max = 3510000 },
  278. .n = { .min = 1, .max = 5 },
  279. .m = { .min = 79, .max = 127 },
  280. .m1 = { .min = 12, .max = 22 },
  281. .m2 = { .min = 5, .max = 9 },
  282. .p = { .min = 5, .max = 80 },
  283. .p1 = { .min = 1, .max = 8 },
  284. .p2 = { .dot_limit = 225000,
  285. .p2_slow = 10, .p2_fast = 5 },
  286. };
  287. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  288. .dot = { .min = 25000, .max = 350000 },
  289. .vco = { .min = 1760000, .max = 3510000 },
  290. .n = { .min = 1, .max = 3 },
  291. .m = { .min = 79, .max = 118 },
  292. .m1 = { .min = 12, .max = 22 },
  293. .m2 = { .min = 5, .max = 9 },
  294. .p = { .min = 28, .max = 112 },
  295. .p1 = { .min = 2, .max = 8 },
  296. .p2 = { .dot_limit = 225000,
  297. .p2_slow = 14, .p2_fast = 14 },
  298. };
  299. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  300. .dot = { .min = 25000, .max = 350000 },
  301. .vco = { .min = 1760000, .max = 3510000 },
  302. .n = { .min = 1, .max = 3 },
  303. .m = { .min = 79, .max = 127 },
  304. .m1 = { .min = 12, .max = 22 },
  305. .m2 = { .min = 5, .max = 9 },
  306. .p = { .min = 14, .max = 56 },
  307. .p1 = { .min = 2, .max = 8 },
  308. .p2 = { .dot_limit = 225000,
  309. .p2_slow = 7, .p2_fast = 7 },
  310. };
  311. /* LVDS 100mhz refclk limits. */
  312. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  313. .dot = { .min = 25000, .max = 350000 },
  314. .vco = { .min = 1760000, .max = 3510000 },
  315. .n = { .min = 1, .max = 2 },
  316. .m = { .min = 79, .max = 126 },
  317. .m1 = { .min = 12, .max = 22 },
  318. .m2 = { .min = 5, .max = 9 },
  319. .p = { .min = 28, .max = 112 },
  320. .p1 = { .min = 2, .max = 8 },
  321. .p2 = { .dot_limit = 225000,
  322. .p2_slow = 14, .p2_fast = 14 },
  323. };
  324. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  325. .dot = { .min = 25000, .max = 350000 },
  326. .vco = { .min = 1760000, .max = 3510000 },
  327. .n = { .min = 1, .max = 3 },
  328. .m = { .min = 79, .max = 126 },
  329. .m1 = { .min = 12, .max = 22 },
  330. .m2 = { .min = 5, .max = 9 },
  331. .p = { .min = 14, .max = 42 },
  332. .p1 = { .min = 2, .max = 6 },
  333. .p2 = { .dot_limit = 225000,
  334. .p2_slow = 7, .p2_fast = 7 },
  335. };
  336. static const intel_limit_t intel_limits_vlv = {
  337. /*
  338. * These are the data rate limits (measured in fast clocks)
  339. * since those are the strictest limits we have. The fast
  340. * clock and actual rate limits are more relaxed, so checking
  341. * them would make no difference.
  342. */
  343. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  344. .vco = { .min = 4000000, .max = 6000000 },
  345. .n = { .min = 1, .max = 7 },
  346. .m1 = { .min = 2, .max = 3 },
  347. .m2 = { .min = 11, .max = 156 },
  348. .p1 = { .min = 2, .max = 3 },
  349. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  350. };
  351. static const intel_limit_t intel_limits_chv = {
  352. /*
  353. * These are the data rate limits (measured in fast clocks)
  354. * since those are the strictest limits we have. The fast
  355. * clock and actual rate limits are more relaxed, so checking
  356. * them would make no difference.
  357. */
  358. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  359. .vco = { .min = 4800000, .max = 6480000 },
  360. .n = { .min = 1, .max = 1 },
  361. .m1 = { .min = 2, .max = 2 },
  362. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  363. .p1 = { .min = 2, .max = 4 },
  364. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  365. };
  366. static void vlv_clock(int refclk, intel_clock_t *clock)
  367. {
  368. clock->m = clock->m1 * clock->m2;
  369. clock->p = clock->p1 * clock->p2;
  370. if (WARN_ON(clock->n == 0 || clock->p == 0))
  371. return;
  372. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  373. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  374. }
  375. /**
  376. * Returns whether any output on the specified pipe is of the specified type
  377. */
  378. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  379. {
  380. struct drm_device *dev = crtc->base.dev;
  381. struct intel_encoder *encoder;
  382. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  383. if (encoder->type == type)
  384. return true;
  385. return false;
  386. }
  387. /**
  388. * Returns whether any output on the specified pipe will have the specified
  389. * type after a staged modeset is complete, i.e., the same as
  390. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  391. * encoder->crtc.
  392. */
  393. static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
  394. {
  395. struct drm_device *dev = crtc->base.dev;
  396. struct intel_encoder *encoder;
  397. for_each_intel_encoder(dev, encoder)
  398. if (encoder->new_crtc == crtc && encoder->type == type)
  399. return true;
  400. return false;
  401. }
  402. static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
  403. int refclk)
  404. {
  405. struct drm_device *dev = crtc->base.dev;
  406. const intel_limit_t *limit;
  407. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  408. if (intel_is_dual_link_lvds(dev)) {
  409. if (refclk == 100000)
  410. limit = &intel_limits_ironlake_dual_lvds_100m;
  411. else
  412. limit = &intel_limits_ironlake_dual_lvds;
  413. } else {
  414. if (refclk == 100000)
  415. limit = &intel_limits_ironlake_single_lvds_100m;
  416. else
  417. limit = &intel_limits_ironlake_single_lvds;
  418. }
  419. } else
  420. limit = &intel_limits_ironlake_dac;
  421. return limit;
  422. }
  423. static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
  424. {
  425. struct drm_device *dev = crtc->base.dev;
  426. const intel_limit_t *limit;
  427. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  428. if (intel_is_dual_link_lvds(dev))
  429. limit = &intel_limits_g4x_dual_channel_lvds;
  430. else
  431. limit = &intel_limits_g4x_single_channel_lvds;
  432. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
  433. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
  434. limit = &intel_limits_g4x_hdmi;
  435. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
  436. limit = &intel_limits_g4x_sdvo;
  437. } else /* The option is for other outputs */
  438. limit = &intel_limits_i9xx_sdvo;
  439. return limit;
  440. }
  441. static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
  442. {
  443. struct drm_device *dev = crtc->base.dev;
  444. const intel_limit_t *limit;
  445. if (HAS_PCH_SPLIT(dev))
  446. limit = intel_ironlake_limit(crtc, refclk);
  447. else if (IS_G4X(dev)) {
  448. limit = intel_g4x_limit(crtc);
  449. } else if (IS_PINEVIEW(dev)) {
  450. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  451. limit = &intel_limits_pineview_lvds;
  452. else
  453. limit = &intel_limits_pineview_sdvo;
  454. } else if (IS_CHERRYVIEW(dev)) {
  455. limit = &intel_limits_chv;
  456. } else if (IS_VALLEYVIEW(dev)) {
  457. limit = &intel_limits_vlv;
  458. } else if (!IS_GEN2(dev)) {
  459. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  460. limit = &intel_limits_i9xx_lvds;
  461. else
  462. limit = &intel_limits_i9xx_sdvo;
  463. } else {
  464. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  465. limit = &intel_limits_i8xx_lvds;
  466. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  467. limit = &intel_limits_i8xx_dvo;
  468. else
  469. limit = &intel_limits_i8xx_dac;
  470. }
  471. return limit;
  472. }
  473. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  474. static void pineview_clock(int refclk, intel_clock_t *clock)
  475. {
  476. clock->m = clock->m2 + 2;
  477. clock->p = clock->p1 * clock->p2;
  478. if (WARN_ON(clock->n == 0 || clock->p == 0))
  479. return;
  480. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  481. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  482. }
  483. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  484. {
  485. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  486. }
  487. static void i9xx_clock(int refclk, intel_clock_t *clock)
  488. {
  489. clock->m = i9xx_dpll_compute_m(clock);
  490. clock->p = clock->p1 * clock->p2;
  491. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  492. return;
  493. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  494. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  495. }
  496. static void chv_clock(int refclk, intel_clock_t *clock)
  497. {
  498. clock->m = clock->m1 * clock->m2;
  499. clock->p = clock->p1 * clock->p2;
  500. if (WARN_ON(clock->n == 0 || clock->p == 0))
  501. return;
  502. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  503. clock->n << 22);
  504. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  505. }
  506. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  507. /**
  508. * Returns whether the given set of divisors are valid for a given refclk with
  509. * the given connectors.
  510. */
  511. static bool intel_PLL_is_valid(struct drm_device *dev,
  512. const intel_limit_t *limit,
  513. const intel_clock_t *clock)
  514. {
  515. if (clock->n < limit->n.min || limit->n.max < clock->n)
  516. INTELPllInvalid("n out of range\n");
  517. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  518. INTELPllInvalid("p1 out of range\n");
  519. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  520. INTELPllInvalid("m2 out of range\n");
  521. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  522. INTELPllInvalid("m1 out of range\n");
  523. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  524. if (clock->m1 <= clock->m2)
  525. INTELPllInvalid("m1 <= m2\n");
  526. if (!IS_VALLEYVIEW(dev)) {
  527. if (clock->p < limit->p.min || limit->p.max < clock->p)
  528. INTELPllInvalid("p out of range\n");
  529. if (clock->m < limit->m.min || limit->m.max < clock->m)
  530. INTELPllInvalid("m out of range\n");
  531. }
  532. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  533. INTELPllInvalid("vco out of range\n");
  534. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  535. * connector, etc., rather than just a single range.
  536. */
  537. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  538. INTELPllInvalid("dot out of range\n");
  539. return true;
  540. }
  541. static bool
  542. i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  543. int target, int refclk, intel_clock_t *match_clock,
  544. intel_clock_t *best_clock)
  545. {
  546. struct drm_device *dev = crtc->base.dev;
  547. intel_clock_t clock;
  548. int err = target;
  549. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  550. /*
  551. * For LVDS just rely on its current settings for dual-channel.
  552. * We haven't figured out how to reliably set up different
  553. * single/dual channel state, if we even can.
  554. */
  555. if (intel_is_dual_link_lvds(dev))
  556. clock.p2 = limit->p2.p2_fast;
  557. else
  558. clock.p2 = limit->p2.p2_slow;
  559. } else {
  560. if (target < limit->p2.dot_limit)
  561. clock.p2 = limit->p2.p2_slow;
  562. else
  563. clock.p2 = limit->p2.p2_fast;
  564. }
  565. memset(best_clock, 0, sizeof(*best_clock));
  566. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  567. clock.m1++) {
  568. for (clock.m2 = limit->m2.min;
  569. clock.m2 <= limit->m2.max; clock.m2++) {
  570. if (clock.m2 >= clock.m1)
  571. break;
  572. for (clock.n = limit->n.min;
  573. clock.n <= limit->n.max; clock.n++) {
  574. for (clock.p1 = limit->p1.min;
  575. clock.p1 <= limit->p1.max; clock.p1++) {
  576. int this_err;
  577. i9xx_clock(refclk, &clock);
  578. if (!intel_PLL_is_valid(dev, limit,
  579. &clock))
  580. continue;
  581. if (match_clock &&
  582. clock.p != match_clock->p)
  583. continue;
  584. this_err = abs(clock.dot - target);
  585. if (this_err < err) {
  586. *best_clock = clock;
  587. err = this_err;
  588. }
  589. }
  590. }
  591. }
  592. }
  593. return (err != target);
  594. }
  595. static bool
  596. pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  597. int target, int refclk, intel_clock_t *match_clock,
  598. intel_clock_t *best_clock)
  599. {
  600. struct drm_device *dev = crtc->base.dev;
  601. intel_clock_t clock;
  602. int err = target;
  603. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  604. /*
  605. * For LVDS just rely on its current settings for dual-channel.
  606. * We haven't figured out how to reliably set up different
  607. * single/dual channel state, if we even can.
  608. */
  609. if (intel_is_dual_link_lvds(dev))
  610. clock.p2 = limit->p2.p2_fast;
  611. else
  612. clock.p2 = limit->p2.p2_slow;
  613. } else {
  614. if (target < limit->p2.dot_limit)
  615. clock.p2 = limit->p2.p2_slow;
  616. else
  617. clock.p2 = limit->p2.p2_fast;
  618. }
  619. memset(best_clock, 0, sizeof(*best_clock));
  620. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  621. clock.m1++) {
  622. for (clock.m2 = limit->m2.min;
  623. clock.m2 <= limit->m2.max; clock.m2++) {
  624. for (clock.n = limit->n.min;
  625. clock.n <= limit->n.max; clock.n++) {
  626. for (clock.p1 = limit->p1.min;
  627. clock.p1 <= limit->p1.max; clock.p1++) {
  628. int this_err;
  629. pineview_clock(refclk, &clock);
  630. if (!intel_PLL_is_valid(dev, limit,
  631. &clock))
  632. continue;
  633. if (match_clock &&
  634. clock.p != match_clock->p)
  635. continue;
  636. this_err = abs(clock.dot - target);
  637. if (this_err < err) {
  638. *best_clock = clock;
  639. err = this_err;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. return (err != target);
  646. }
  647. static bool
  648. g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  649. int target, int refclk, intel_clock_t *match_clock,
  650. intel_clock_t *best_clock)
  651. {
  652. struct drm_device *dev = crtc->base.dev;
  653. intel_clock_t clock;
  654. int max_n;
  655. bool found;
  656. /* approximately equals target * 0.00585 */
  657. int err_most = (target >> 8) + (target >> 9);
  658. found = false;
  659. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  660. if (intel_is_dual_link_lvds(dev))
  661. clock.p2 = limit->p2.p2_fast;
  662. else
  663. clock.p2 = limit->p2.p2_slow;
  664. } else {
  665. if (target < limit->p2.dot_limit)
  666. clock.p2 = limit->p2.p2_slow;
  667. else
  668. clock.p2 = limit->p2.p2_fast;
  669. }
  670. memset(best_clock, 0, sizeof(*best_clock));
  671. max_n = limit->n.max;
  672. /* based on hardware requirement, prefer smaller n to precision */
  673. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  674. /* based on hardware requirement, prefere larger m1,m2 */
  675. for (clock.m1 = limit->m1.max;
  676. clock.m1 >= limit->m1.min; clock.m1--) {
  677. for (clock.m2 = limit->m2.max;
  678. clock.m2 >= limit->m2.min; clock.m2--) {
  679. for (clock.p1 = limit->p1.max;
  680. clock.p1 >= limit->p1.min; clock.p1--) {
  681. int this_err;
  682. i9xx_clock(refclk, &clock);
  683. if (!intel_PLL_is_valid(dev, limit,
  684. &clock))
  685. continue;
  686. this_err = abs(clock.dot - target);
  687. if (this_err < err_most) {
  688. *best_clock = clock;
  689. err_most = this_err;
  690. max_n = clock.n;
  691. found = true;
  692. }
  693. }
  694. }
  695. }
  696. }
  697. return found;
  698. }
  699. /*
  700. * Check if the calculated PLL configuration is more optimal compared to the
  701. * best configuration and error found so far. Return the calculated error.
  702. */
  703. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  704. const intel_clock_t *calculated_clock,
  705. const intel_clock_t *best_clock,
  706. unsigned int best_error_ppm,
  707. unsigned int *error_ppm)
  708. {
  709. /*
  710. * For CHV ignore the error and consider only the P value.
  711. * Prefer a bigger P value based on HW requirements.
  712. */
  713. if (IS_CHERRYVIEW(dev)) {
  714. *error_ppm = 0;
  715. return calculated_clock->p > best_clock->p;
  716. }
  717. if (WARN_ON_ONCE(!target_freq))
  718. return false;
  719. *error_ppm = div_u64(1000000ULL *
  720. abs(target_freq - calculated_clock->dot),
  721. target_freq);
  722. /*
  723. * Prefer a better P value over a better (smaller) error if the error
  724. * is small. Ensure this preference for future configurations too by
  725. * setting the error to 0.
  726. */
  727. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  728. *error_ppm = 0;
  729. return true;
  730. }
  731. return *error_ppm + 10 < best_error_ppm;
  732. }
  733. static bool
  734. vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  735. int target, int refclk, intel_clock_t *match_clock,
  736. intel_clock_t *best_clock)
  737. {
  738. struct drm_device *dev = crtc->base.dev;
  739. intel_clock_t clock;
  740. unsigned int bestppm = 1000000;
  741. /* min update 19.2 MHz */
  742. int max_n = min(limit->n.max, refclk / 19200);
  743. bool found = false;
  744. target *= 5; /* fast clock */
  745. memset(best_clock, 0, sizeof(*best_clock));
  746. /* based on hardware requirement, prefer smaller n to precision */
  747. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  748. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  749. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  750. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  751. clock.p = clock.p1 * clock.p2;
  752. /* based on hardware requirement, prefer bigger m1,m2 values */
  753. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  754. unsigned int ppm;
  755. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  756. refclk * clock.m1);
  757. vlv_clock(refclk, &clock);
  758. if (!intel_PLL_is_valid(dev, limit,
  759. &clock))
  760. continue;
  761. if (!vlv_PLL_is_optimal(dev, target,
  762. &clock,
  763. best_clock,
  764. bestppm, &ppm))
  765. continue;
  766. *best_clock = clock;
  767. bestppm = ppm;
  768. found = true;
  769. }
  770. }
  771. }
  772. }
  773. return found;
  774. }
  775. static bool
  776. chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  777. int target, int refclk, intel_clock_t *match_clock,
  778. intel_clock_t *best_clock)
  779. {
  780. struct drm_device *dev = crtc->base.dev;
  781. unsigned int best_error_ppm;
  782. intel_clock_t clock;
  783. uint64_t m2;
  784. int found = false;
  785. memset(best_clock, 0, sizeof(*best_clock));
  786. best_error_ppm = 1000000;
  787. /*
  788. * Based on hardware doc, the n always set to 1, and m1 always
  789. * set to 2. If requires to support 200Mhz refclk, we need to
  790. * revisit this because n may not 1 anymore.
  791. */
  792. clock.n = 1, clock.m1 = 2;
  793. target *= 5; /* fast clock */
  794. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  795. for (clock.p2 = limit->p2.p2_fast;
  796. clock.p2 >= limit->p2.p2_slow;
  797. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  798. unsigned int error_ppm;
  799. clock.p = clock.p1 * clock.p2;
  800. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  801. clock.n) << 22, refclk * clock.m1);
  802. if (m2 > INT_MAX/clock.m1)
  803. continue;
  804. clock.m2 = m2;
  805. chv_clock(refclk, &clock);
  806. if (!intel_PLL_is_valid(dev, limit, &clock))
  807. continue;
  808. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  809. best_error_ppm, &error_ppm))
  810. continue;
  811. *best_clock = clock;
  812. best_error_ppm = error_ppm;
  813. found = true;
  814. }
  815. }
  816. return found;
  817. }
  818. bool intel_crtc_active(struct drm_crtc *crtc)
  819. {
  820. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  821. /* Be paranoid as we can arrive here with only partial
  822. * state retrieved from the hardware during setup.
  823. *
  824. * We can ditch the adjusted_mode.crtc_clock check as soon
  825. * as Haswell has gained clock readout/fastboot support.
  826. *
  827. * We can ditch the crtc->primary->fb check as soon as we can
  828. * properly reconstruct framebuffers.
  829. *
  830. * FIXME: The intel_crtc->active here should be switched to
  831. * crtc->state->active once we have proper CRTC states wired up
  832. * for atomic.
  833. */
  834. return intel_crtc->active && crtc->primary->state->fb &&
  835. intel_crtc->config->base.adjusted_mode.crtc_clock;
  836. }
  837. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  838. enum pipe pipe)
  839. {
  840. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  841. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  842. return intel_crtc->config->cpu_transcoder;
  843. }
  844. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  845. {
  846. struct drm_i915_private *dev_priv = dev->dev_private;
  847. u32 reg = PIPEDSL(pipe);
  848. u32 line1, line2;
  849. u32 line_mask;
  850. if (IS_GEN2(dev))
  851. line_mask = DSL_LINEMASK_GEN2;
  852. else
  853. line_mask = DSL_LINEMASK_GEN3;
  854. line1 = I915_READ(reg) & line_mask;
  855. mdelay(5);
  856. line2 = I915_READ(reg) & line_mask;
  857. return line1 == line2;
  858. }
  859. /*
  860. * intel_wait_for_pipe_off - wait for pipe to turn off
  861. * @crtc: crtc whose pipe to wait for
  862. *
  863. * After disabling a pipe, we can't wait for vblank in the usual way,
  864. * spinning on the vblank interrupt status bit, since we won't actually
  865. * see an interrupt when the pipe is disabled.
  866. *
  867. * On Gen4 and above:
  868. * wait for the pipe register state bit to turn off
  869. *
  870. * Otherwise:
  871. * wait for the display line value to settle (it usually
  872. * ends up stopping at the start of the next frame).
  873. *
  874. */
  875. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  876. {
  877. struct drm_device *dev = crtc->base.dev;
  878. struct drm_i915_private *dev_priv = dev->dev_private;
  879. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  880. enum pipe pipe = crtc->pipe;
  881. if (INTEL_INFO(dev)->gen >= 4) {
  882. int reg = PIPECONF(cpu_transcoder);
  883. /* Wait for the Pipe State to go off */
  884. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  885. 100))
  886. WARN(1, "pipe_off wait timed out\n");
  887. } else {
  888. /* Wait for the display line to settle */
  889. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  890. WARN(1, "pipe_off wait timed out\n");
  891. }
  892. }
  893. /*
  894. * ibx_digital_port_connected - is the specified port connected?
  895. * @dev_priv: i915 private structure
  896. * @port: the port to test
  897. *
  898. * Returns true if @port is connected, false otherwise.
  899. */
  900. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  901. struct intel_digital_port *port)
  902. {
  903. u32 bit;
  904. if (HAS_PCH_IBX(dev_priv->dev)) {
  905. switch (port->port) {
  906. case PORT_B:
  907. bit = SDE_PORTB_HOTPLUG;
  908. break;
  909. case PORT_C:
  910. bit = SDE_PORTC_HOTPLUG;
  911. break;
  912. case PORT_D:
  913. bit = SDE_PORTD_HOTPLUG;
  914. break;
  915. default:
  916. return true;
  917. }
  918. } else {
  919. switch (port->port) {
  920. case PORT_B:
  921. bit = SDE_PORTB_HOTPLUG_CPT;
  922. break;
  923. case PORT_C:
  924. bit = SDE_PORTC_HOTPLUG_CPT;
  925. break;
  926. case PORT_D:
  927. bit = SDE_PORTD_HOTPLUG_CPT;
  928. break;
  929. default:
  930. return true;
  931. }
  932. }
  933. return I915_READ(SDEISR) & bit;
  934. }
  935. static const char *state_string(bool enabled)
  936. {
  937. return enabled ? "on" : "off";
  938. }
  939. /* Only for pre-ILK configs */
  940. void assert_pll(struct drm_i915_private *dev_priv,
  941. enum pipe pipe, bool state)
  942. {
  943. int reg;
  944. u32 val;
  945. bool cur_state;
  946. reg = DPLL(pipe);
  947. val = I915_READ(reg);
  948. cur_state = !!(val & DPLL_VCO_ENABLE);
  949. I915_STATE_WARN(cur_state != state,
  950. "PLL state assertion failure (expected %s, current %s)\n",
  951. state_string(state), state_string(cur_state));
  952. }
  953. /* XXX: the dsi pll is shared between MIPI DSI ports */
  954. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  955. {
  956. u32 val;
  957. bool cur_state;
  958. mutex_lock(&dev_priv->dpio_lock);
  959. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  960. mutex_unlock(&dev_priv->dpio_lock);
  961. cur_state = val & DSI_PLL_VCO_EN;
  962. I915_STATE_WARN(cur_state != state,
  963. "DSI PLL state assertion failure (expected %s, current %s)\n",
  964. state_string(state), state_string(cur_state));
  965. }
  966. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  967. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  968. struct intel_shared_dpll *
  969. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  970. {
  971. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  972. if (crtc->config->shared_dpll < 0)
  973. return NULL;
  974. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  975. }
  976. /* For ILK+ */
  977. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  978. struct intel_shared_dpll *pll,
  979. bool state)
  980. {
  981. bool cur_state;
  982. struct intel_dpll_hw_state hw_state;
  983. if (WARN (!pll,
  984. "asserting DPLL %s with no DPLL\n", state_string(state)))
  985. return;
  986. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  987. I915_STATE_WARN(cur_state != state,
  988. "%s assertion failure (expected %s, current %s)\n",
  989. pll->name, state_string(state), state_string(cur_state));
  990. }
  991. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  992. enum pipe pipe, bool state)
  993. {
  994. int reg;
  995. u32 val;
  996. bool cur_state;
  997. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  998. pipe);
  999. if (HAS_DDI(dev_priv->dev)) {
  1000. /* DDI does not have a specific FDI_TX register */
  1001. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1002. val = I915_READ(reg);
  1003. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1004. } else {
  1005. reg = FDI_TX_CTL(pipe);
  1006. val = I915_READ(reg);
  1007. cur_state = !!(val & FDI_TX_ENABLE);
  1008. }
  1009. I915_STATE_WARN(cur_state != state,
  1010. "FDI TX state assertion failure (expected %s, current %s)\n",
  1011. state_string(state), state_string(cur_state));
  1012. }
  1013. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1014. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1015. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1016. enum pipe pipe, bool state)
  1017. {
  1018. int reg;
  1019. u32 val;
  1020. bool cur_state;
  1021. reg = FDI_RX_CTL(pipe);
  1022. val = I915_READ(reg);
  1023. cur_state = !!(val & FDI_RX_ENABLE);
  1024. I915_STATE_WARN(cur_state != state,
  1025. "FDI RX state assertion failure (expected %s, current %s)\n",
  1026. state_string(state), state_string(cur_state));
  1027. }
  1028. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1029. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1030. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1031. enum pipe pipe)
  1032. {
  1033. int reg;
  1034. u32 val;
  1035. /* ILK FDI PLL is always enabled */
  1036. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1037. return;
  1038. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1039. if (HAS_DDI(dev_priv->dev))
  1040. return;
  1041. reg = FDI_TX_CTL(pipe);
  1042. val = I915_READ(reg);
  1043. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1044. }
  1045. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1046. enum pipe pipe, bool state)
  1047. {
  1048. int reg;
  1049. u32 val;
  1050. bool cur_state;
  1051. reg = FDI_RX_CTL(pipe);
  1052. val = I915_READ(reg);
  1053. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1054. I915_STATE_WARN(cur_state != state,
  1055. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1056. state_string(state), state_string(cur_state));
  1057. }
  1058. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1059. enum pipe pipe)
  1060. {
  1061. struct drm_device *dev = dev_priv->dev;
  1062. int pp_reg;
  1063. u32 val;
  1064. enum pipe panel_pipe = PIPE_A;
  1065. bool locked = true;
  1066. if (WARN_ON(HAS_DDI(dev)))
  1067. return;
  1068. if (HAS_PCH_SPLIT(dev)) {
  1069. u32 port_sel;
  1070. pp_reg = PCH_PP_CONTROL;
  1071. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1072. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1073. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1074. panel_pipe = PIPE_B;
  1075. /* XXX: else fix for eDP */
  1076. } else if (IS_VALLEYVIEW(dev)) {
  1077. /* presumably write lock depends on pipe, not port select */
  1078. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1079. panel_pipe = pipe;
  1080. } else {
  1081. pp_reg = PP_CONTROL;
  1082. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1083. panel_pipe = PIPE_B;
  1084. }
  1085. val = I915_READ(pp_reg);
  1086. if (!(val & PANEL_POWER_ON) ||
  1087. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1088. locked = false;
  1089. I915_STATE_WARN(panel_pipe == pipe && locked,
  1090. "panel assertion failure, pipe %c regs locked\n",
  1091. pipe_name(pipe));
  1092. }
  1093. static void assert_cursor(struct drm_i915_private *dev_priv,
  1094. enum pipe pipe, bool state)
  1095. {
  1096. struct drm_device *dev = dev_priv->dev;
  1097. bool cur_state;
  1098. if (IS_845G(dev) || IS_I865G(dev))
  1099. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1100. else
  1101. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1102. I915_STATE_WARN(cur_state != state,
  1103. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1104. pipe_name(pipe), state_string(state), state_string(cur_state));
  1105. }
  1106. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1107. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1108. void assert_pipe(struct drm_i915_private *dev_priv,
  1109. enum pipe pipe, bool state)
  1110. {
  1111. int reg;
  1112. u32 val;
  1113. bool cur_state;
  1114. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1115. pipe);
  1116. /* if we need the pipe quirk it must be always on */
  1117. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1118. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1119. state = true;
  1120. if (!intel_display_power_is_enabled(dev_priv,
  1121. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1122. cur_state = false;
  1123. } else {
  1124. reg = PIPECONF(cpu_transcoder);
  1125. val = I915_READ(reg);
  1126. cur_state = !!(val & PIPECONF_ENABLE);
  1127. }
  1128. I915_STATE_WARN(cur_state != state,
  1129. "pipe %c assertion failure (expected %s, current %s)\n",
  1130. pipe_name(pipe), state_string(state), state_string(cur_state));
  1131. }
  1132. static void assert_plane(struct drm_i915_private *dev_priv,
  1133. enum plane plane, bool state)
  1134. {
  1135. int reg;
  1136. u32 val;
  1137. bool cur_state;
  1138. reg = DSPCNTR(plane);
  1139. val = I915_READ(reg);
  1140. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1141. I915_STATE_WARN(cur_state != state,
  1142. "plane %c assertion failure (expected %s, current %s)\n",
  1143. plane_name(plane), state_string(state), state_string(cur_state));
  1144. }
  1145. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1146. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1147. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1148. enum pipe pipe)
  1149. {
  1150. struct drm_device *dev = dev_priv->dev;
  1151. int reg, i;
  1152. u32 val;
  1153. int cur_pipe;
  1154. /* Primary planes are fixed to pipes on gen4+ */
  1155. if (INTEL_INFO(dev)->gen >= 4) {
  1156. reg = DSPCNTR(pipe);
  1157. val = I915_READ(reg);
  1158. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1159. "plane %c assertion failure, should be disabled but not\n",
  1160. plane_name(pipe));
  1161. return;
  1162. }
  1163. /* Need to check both planes against the pipe */
  1164. for_each_pipe(dev_priv, i) {
  1165. reg = DSPCNTR(i);
  1166. val = I915_READ(reg);
  1167. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1168. DISPPLANE_SEL_PIPE_SHIFT;
  1169. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1170. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1171. plane_name(i), pipe_name(pipe));
  1172. }
  1173. }
  1174. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1175. enum pipe pipe)
  1176. {
  1177. struct drm_device *dev = dev_priv->dev;
  1178. int reg, sprite;
  1179. u32 val;
  1180. if (INTEL_INFO(dev)->gen >= 9) {
  1181. for_each_sprite(dev_priv, pipe, sprite) {
  1182. val = I915_READ(PLANE_CTL(pipe, sprite));
  1183. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1184. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1185. sprite, pipe_name(pipe));
  1186. }
  1187. } else if (IS_VALLEYVIEW(dev)) {
  1188. for_each_sprite(dev_priv, pipe, sprite) {
  1189. reg = SPCNTR(pipe, sprite);
  1190. val = I915_READ(reg);
  1191. I915_STATE_WARN(val & SP_ENABLE,
  1192. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1193. sprite_name(pipe, sprite), pipe_name(pipe));
  1194. }
  1195. } else if (INTEL_INFO(dev)->gen >= 7) {
  1196. reg = SPRCTL(pipe);
  1197. val = I915_READ(reg);
  1198. I915_STATE_WARN(val & SPRITE_ENABLE,
  1199. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1200. plane_name(pipe), pipe_name(pipe));
  1201. } else if (INTEL_INFO(dev)->gen >= 5) {
  1202. reg = DVSCNTR(pipe);
  1203. val = I915_READ(reg);
  1204. I915_STATE_WARN(val & DVS_ENABLE,
  1205. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1206. plane_name(pipe), pipe_name(pipe));
  1207. }
  1208. }
  1209. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1210. {
  1211. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1212. drm_crtc_vblank_put(crtc);
  1213. }
  1214. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1215. {
  1216. u32 val;
  1217. bool enabled;
  1218. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1219. val = I915_READ(PCH_DREF_CONTROL);
  1220. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1221. DREF_SUPERSPREAD_SOURCE_MASK));
  1222. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1223. }
  1224. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1225. enum pipe pipe)
  1226. {
  1227. int reg;
  1228. u32 val;
  1229. bool enabled;
  1230. reg = PCH_TRANSCONF(pipe);
  1231. val = I915_READ(reg);
  1232. enabled = !!(val & TRANS_ENABLE);
  1233. I915_STATE_WARN(enabled,
  1234. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1235. pipe_name(pipe));
  1236. }
  1237. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1238. enum pipe pipe, u32 port_sel, u32 val)
  1239. {
  1240. if ((val & DP_PORT_EN) == 0)
  1241. return false;
  1242. if (HAS_PCH_CPT(dev_priv->dev)) {
  1243. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1244. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1245. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1246. return false;
  1247. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1248. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1249. return false;
  1250. } else {
  1251. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1252. return false;
  1253. }
  1254. return true;
  1255. }
  1256. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1257. enum pipe pipe, u32 val)
  1258. {
  1259. if ((val & SDVO_ENABLE) == 0)
  1260. return false;
  1261. if (HAS_PCH_CPT(dev_priv->dev)) {
  1262. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1263. return false;
  1264. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1265. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1266. return false;
  1267. } else {
  1268. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1269. return false;
  1270. }
  1271. return true;
  1272. }
  1273. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1274. enum pipe pipe, u32 val)
  1275. {
  1276. if ((val & LVDS_PORT_EN) == 0)
  1277. return false;
  1278. if (HAS_PCH_CPT(dev_priv->dev)) {
  1279. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1280. return false;
  1281. } else {
  1282. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1283. return false;
  1284. }
  1285. return true;
  1286. }
  1287. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1288. enum pipe pipe, u32 val)
  1289. {
  1290. if ((val & ADPA_DAC_ENABLE) == 0)
  1291. return false;
  1292. if (HAS_PCH_CPT(dev_priv->dev)) {
  1293. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1294. return false;
  1295. } else {
  1296. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1297. return false;
  1298. }
  1299. return true;
  1300. }
  1301. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1302. enum pipe pipe, int reg, u32 port_sel)
  1303. {
  1304. u32 val = I915_READ(reg);
  1305. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1306. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1307. reg, pipe_name(pipe));
  1308. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1309. && (val & DP_PIPEB_SELECT),
  1310. "IBX PCH dp port still using transcoder B\n");
  1311. }
  1312. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1313. enum pipe pipe, int reg)
  1314. {
  1315. u32 val = I915_READ(reg);
  1316. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1317. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1318. reg, pipe_name(pipe));
  1319. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1320. && (val & SDVO_PIPE_B_SELECT),
  1321. "IBX PCH hdmi port still using transcoder B\n");
  1322. }
  1323. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1324. enum pipe pipe)
  1325. {
  1326. int reg;
  1327. u32 val;
  1328. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1329. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1330. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1331. reg = PCH_ADPA;
  1332. val = I915_READ(reg);
  1333. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1334. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1335. pipe_name(pipe));
  1336. reg = PCH_LVDS;
  1337. val = I915_READ(reg);
  1338. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1339. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1340. pipe_name(pipe));
  1341. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1342. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1343. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1344. }
  1345. static void intel_init_dpio(struct drm_device *dev)
  1346. {
  1347. struct drm_i915_private *dev_priv = dev->dev_private;
  1348. if (!IS_VALLEYVIEW(dev))
  1349. return;
  1350. /*
  1351. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1352. * CHV x1 PHY (DP/HDMI D)
  1353. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1354. */
  1355. if (IS_CHERRYVIEW(dev)) {
  1356. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1357. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1358. } else {
  1359. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1360. }
  1361. }
  1362. static void vlv_enable_pll(struct intel_crtc *crtc,
  1363. const struct intel_crtc_state *pipe_config)
  1364. {
  1365. struct drm_device *dev = crtc->base.dev;
  1366. struct drm_i915_private *dev_priv = dev->dev_private;
  1367. int reg = DPLL(crtc->pipe);
  1368. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1369. assert_pipe_disabled(dev_priv, crtc->pipe);
  1370. /* No really, not for ILK+ */
  1371. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1372. /* PLL is protected by panel, make sure we can write it */
  1373. if (IS_MOBILE(dev_priv->dev))
  1374. assert_panel_unlocked(dev_priv, crtc->pipe);
  1375. I915_WRITE(reg, dpll);
  1376. POSTING_READ(reg);
  1377. udelay(150);
  1378. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1379. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1380. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1381. POSTING_READ(DPLL_MD(crtc->pipe));
  1382. /* We do this three times for luck */
  1383. I915_WRITE(reg, dpll);
  1384. POSTING_READ(reg);
  1385. udelay(150); /* wait for warmup */
  1386. I915_WRITE(reg, dpll);
  1387. POSTING_READ(reg);
  1388. udelay(150); /* wait for warmup */
  1389. I915_WRITE(reg, dpll);
  1390. POSTING_READ(reg);
  1391. udelay(150); /* wait for warmup */
  1392. }
  1393. static void chv_enable_pll(struct intel_crtc *crtc,
  1394. const struct intel_crtc_state *pipe_config)
  1395. {
  1396. struct drm_device *dev = crtc->base.dev;
  1397. struct drm_i915_private *dev_priv = dev->dev_private;
  1398. int pipe = crtc->pipe;
  1399. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1400. u32 tmp;
  1401. assert_pipe_disabled(dev_priv, crtc->pipe);
  1402. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1403. mutex_lock(&dev_priv->dpio_lock);
  1404. /* Enable back the 10bit clock to display controller */
  1405. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1406. tmp |= DPIO_DCLKP_EN;
  1407. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1408. /*
  1409. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1410. */
  1411. udelay(1);
  1412. /* Enable PLL */
  1413. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1414. /* Check PLL is locked */
  1415. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1416. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1417. /* not sure when this should be written */
  1418. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1419. POSTING_READ(DPLL_MD(pipe));
  1420. mutex_unlock(&dev_priv->dpio_lock);
  1421. }
  1422. static int intel_num_dvo_pipes(struct drm_device *dev)
  1423. {
  1424. struct intel_crtc *crtc;
  1425. int count = 0;
  1426. for_each_intel_crtc(dev, crtc)
  1427. count += crtc->active &&
  1428. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1429. return count;
  1430. }
  1431. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1432. {
  1433. struct drm_device *dev = crtc->base.dev;
  1434. struct drm_i915_private *dev_priv = dev->dev_private;
  1435. int reg = DPLL(crtc->pipe);
  1436. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1437. assert_pipe_disabled(dev_priv, crtc->pipe);
  1438. /* No really, not for ILK+ */
  1439. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1440. /* PLL is protected by panel, make sure we can write it */
  1441. if (IS_MOBILE(dev) && !IS_I830(dev))
  1442. assert_panel_unlocked(dev_priv, crtc->pipe);
  1443. /* Enable DVO 2x clock on both PLLs if necessary */
  1444. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1445. /*
  1446. * It appears to be important that we don't enable this
  1447. * for the current pipe before otherwise configuring the
  1448. * PLL. No idea how this should be handled if multiple
  1449. * DVO outputs are enabled simultaneosly.
  1450. */
  1451. dpll |= DPLL_DVO_2X_MODE;
  1452. I915_WRITE(DPLL(!crtc->pipe),
  1453. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1454. }
  1455. /* Wait for the clocks to stabilize. */
  1456. POSTING_READ(reg);
  1457. udelay(150);
  1458. if (INTEL_INFO(dev)->gen >= 4) {
  1459. I915_WRITE(DPLL_MD(crtc->pipe),
  1460. crtc->config->dpll_hw_state.dpll_md);
  1461. } else {
  1462. /* The pixel multiplier can only be updated once the
  1463. * DPLL is enabled and the clocks are stable.
  1464. *
  1465. * So write it again.
  1466. */
  1467. I915_WRITE(reg, dpll);
  1468. }
  1469. /* We do this three times for luck */
  1470. I915_WRITE(reg, dpll);
  1471. POSTING_READ(reg);
  1472. udelay(150); /* wait for warmup */
  1473. I915_WRITE(reg, dpll);
  1474. POSTING_READ(reg);
  1475. udelay(150); /* wait for warmup */
  1476. I915_WRITE(reg, dpll);
  1477. POSTING_READ(reg);
  1478. udelay(150); /* wait for warmup */
  1479. }
  1480. /**
  1481. * i9xx_disable_pll - disable a PLL
  1482. * @dev_priv: i915 private structure
  1483. * @pipe: pipe PLL to disable
  1484. *
  1485. * Disable the PLL for @pipe, making sure the pipe is off first.
  1486. *
  1487. * Note! This is for pre-ILK only.
  1488. */
  1489. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1490. {
  1491. struct drm_device *dev = crtc->base.dev;
  1492. struct drm_i915_private *dev_priv = dev->dev_private;
  1493. enum pipe pipe = crtc->pipe;
  1494. /* Disable DVO 2x clock on both PLLs if necessary */
  1495. if (IS_I830(dev) &&
  1496. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1497. intel_num_dvo_pipes(dev) == 1) {
  1498. I915_WRITE(DPLL(PIPE_B),
  1499. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1500. I915_WRITE(DPLL(PIPE_A),
  1501. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1502. }
  1503. /* Don't disable pipe or pipe PLLs if needed */
  1504. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1505. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1506. return;
  1507. /* Make sure the pipe isn't still relying on us */
  1508. assert_pipe_disabled(dev_priv, pipe);
  1509. I915_WRITE(DPLL(pipe), 0);
  1510. POSTING_READ(DPLL(pipe));
  1511. }
  1512. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1513. {
  1514. u32 val = 0;
  1515. /* Make sure the pipe isn't still relying on us */
  1516. assert_pipe_disabled(dev_priv, pipe);
  1517. /*
  1518. * Leave integrated clock source and reference clock enabled for pipe B.
  1519. * The latter is needed for VGA hotplug / manual detection.
  1520. */
  1521. if (pipe == PIPE_B)
  1522. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1523. I915_WRITE(DPLL(pipe), val);
  1524. POSTING_READ(DPLL(pipe));
  1525. }
  1526. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1527. {
  1528. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1529. u32 val;
  1530. /* Make sure the pipe isn't still relying on us */
  1531. assert_pipe_disabled(dev_priv, pipe);
  1532. /* Set PLL en = 0 */
  1533. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1534. if (pipe != PIPE_A)
  1535. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1536. I915_WRITE(DPLL(pipe), val);
  1537. POSTING_READ(DPLL(pipe));
  1538. mutex_lock(&dev_priv->dpio_lock);
  1539. /* Disable 10bit clock to display controller */
  1540. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1541. val &= ~DPIO_DCLKP_EN;
  1542. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1543. /* disable left/right clock distribution */
  1544. if (pipe != PIPE_B) {
  1545. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1546. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1547. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1548. } else {
  1549. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1550. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1551. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1552. }
  1553. mutex_unlock(&dev_priv->dpio_lock);
  1554. }
  1555. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1556. struct intel_digital_port *dport)
  1557. {
  1558. u32 port_mask;
  1559. int dpll_reg;
  1560. switch (dport->port) {
  1561. case PORT_B:
  1562. port_mask = DPLL_PORTB_READY_MASK;
  1563. dpll_reg = DPLL(0);
  1564. break;
  1565. case PORT_C:
  1566. port_mask = DPLL_PORTC_READY_MASK;
  1567. dpll_reg = DPLL(0);
  1568. break;
  1569. case PORT_D:
  1570. port_mask = DPLL_PORTD_READY_MASK;
  1571. dpll_reg = DPIO_PHY_STATUS;
  1572. break;
  1573. default:
  1574. BUG();
  1575. }
  1576. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1577. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1578. port_name(dport->port), I915_READ(dpll_reg));
  1579. }
  1580. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1581. {
  1582. struct drm_device *dev = crtc->base.dev;
  1583. struct drm_i915_private *dev_priv = dev->dev_private;
  1584. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1585. if (WARN_ON(pll == NULL))
  1586. return;
  1587. WARN_ON(!pll->config.crtc_mask);
  1588. if (pll->active == 0) {
  1589. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1590. WARN_ON(pll->on);
  1591. assert_shared_dpll_disabled(dev_priv, pll);
  1592. pll->mode_set(dev_priv, pll);
  1593. }
  1594. }
  1595. /**
  1596. * intel_enable_shared_dpll - enable PCH PLL
  1597. * @dev_priv: i915 private structure
  1598. * @pipe: pipe PLL to enable
  1599. *
  1600. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1601. * drives the transcoder clock.
  1602. */
  1603. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1604. {
  1605. struct drm_device *dev = crtc->base.dev;
  1606. struct drm_i915_private *dev_priv = dev->dev_private;
  1607. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1608. if (WARN_ON(pll == NULL))
  1609. return;
  1610. if (WARN_ON(pll->config.crtc_mask == 0))
  1611. return;
  1612. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1613. pll->name, pll->active, pll->on,
  1614. crtc->base.base.id);
  1615. if (pll->active++) {
  1616. WARN_ON(!pll->on);
  1617. assert_shared_dpll_enabled(dev_priv, pll);
  1618. return;
  1619. }
  1620. WARN_ON(pll->on);
  1621. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1622. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1623. pll->enable(dev_priv, pll);
  1624. pll->on = true;
  1625. }
  1626. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1627. {
  1628. struct drm_device *dev = crtc->base.dev;
  1629. struct drm_i915_private *dev_priv = dev->dev_private;
  1630. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1631. /* PCH only available on ILK+ */
  1632. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1633. if (WARN_ON(pll == NULL))
  1634. return;
  1635. if (WARN_ON(pll->config.crtc_mask == 0))
  1636. return;
  1637. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1638. pll->name, pll->active, pll->on,
  1639. crtc->base.base.id);
  1640. if (WARN_ON(pll->active == 0)) {
  1641. assert_shared_dpll_disabled(dev_priv, pll);
  1642. return;
  1643. }
  1644. assert_shared_dpll_enabled(dev_priv, pll);
  1645. WARN_ON(!pll->on);
  1646. if (--pll->active)
  1647. return;
  1648. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1649. pll->disable(dev_priv, pll);
  1650. pll->on = false;
  1651. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1652. }
  1653. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1654. enum pipe pipe)
  1655. {
  1656. struct drm_device *dev = dev_priv->dev;
  1657. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1658. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1659. uint32_t reg, val, pipeconf_val;
  1660. /* PCH only available on ILK+ */
  1661. BUG_ON(!HAS_PCH_SPLIT(dev));
  1662. /* Make sure PCH DPLL is enabled */
  1663. assert_shared_dpll_enabled(dev_priv,
  1664. intel_crtc_to_shared_dpll(intel_crtc));
  1665. /* FDI must be feeding us bits for PCH ports */
  1666. assert_fdi_tx_enabled(dev_priv, pipe);
  1667. assert_fdi_rx_enabled(dev_priv, pipe);
  1668. if (HAS_PCH_CPT(dev)) {
  1669. /* Workaround: Set the timing override bit before enabling the
  1670. * pch transcoder. */
  1671. reg = TRANS_CHICKEN2(pipe);
  1672. val = I915_READ(reg);
  1673. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1674. I915_WRITE(reg, val);
  1675. }
  1676. reg = PCH_TRANSCONF(pipe);
  1677. val = I915_READ(reg);
  1678. pipeconf_val = I915_READ(PIPECONF(pipe));
  1679. if (HAS_PCH_IBX(dev_priv->dev)) {
  1680. /*
  1681. * make the BPC in transcoder be consistent with
  1682. * that in pipeconf reg.
  1683. */
  1684. val &= ~PIPECONF_BPC_MASK;
  1685. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1686. }
  1687. val &= ~TRANS_INTERLACE_MASK;
  1688. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1689. if (HAS_PCH_IBX(dev_priv->dev) &&
  1690. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1691. val |= TRANS_LEGACY_INTERLACED_ILK;
  1692. else
  1693. val |= TRANS_INTERLACED;
  1694. else
  1695. val |= TRANS_PROGRESSIVE;
  1696. I915_WRITE(reg, val | TRANS_ENABLE);
  1697. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1698. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1699. }
  1700. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1701. enum transcoder cpu_transcoder)
  1702. {
  1703. u32 val, pipeconf_val;
  1704. /* PCH only available on ILK+ */
  1705. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1706. /* FDI must be feeding us bits for PCH ports */
  1707. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1708. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1709. /* Workaround: set timing override bit. */
  1710. val = I915_READ(_TRANSA_CHICKEN2);
  1711. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1712. I915_WRITE(_TRANSA_CHICKEN2, val);
  1713. val = TRANS_ENABLE;
  1714. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1715. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1716. PIPECONF_INTERLACED_ILK)
  1717. val |= TRANS_INTERLACED;
  1718. else
  1719. val |= TRANS_PROGRESSIVE;
  1720. I915_WRITE(LPT_TRANSCONF, val);
  1721. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1722. DRM_ERROR("Failed to enable PCH transcoder\n");
  1723. }
  1724. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1725. enum pipe pipe)
  1726. {
  1727. struct drm_device *dev = dev_priv->dev;
  1728. uint32_t reg, val;
  1729. /* FDI relies on the transcoder */
  1730. assert_fdi_tx_disabled(dev_priv, pipe);
  1731. assert_fdi_rx_disabled(dev_priv, pipe);
  1732. /* Ports must be off as well */
  1733. assert_pch_ports_disabled(dev_priv, pipe);
  1734. reg = PCH_TRANSCONF(pipe);
  1735. val = I915_READ(reg);
  1736. val &= ~TRANS_ENABLE;
  1737. I915_WRITE(reg, val);
  1738. /* wait for PCH transcoder off, transcoder state */
  1739. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1740. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1741. if (!HAS_PCH_IBX(dev)) {
  1742. /* Workaround: Clear the timing override chicken bit again. */
  1743. reg = TRANS_CHICKEN2(pipe);
  1744. val = I915_READ(reg);
  1745. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1746. I915_WRITE(reg, val);
  1747. }
  1748. }
  1749. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1750. {
  1751. u32 val;
  1752. val = I915_READ(LPT_TRANSCONF);
  1753. val &= ~TRANS_ENABLE;
  1754. I915_WRITE(LPT_TRANSCONF, val);
  1755. /* wait for PCH transcoder off, transcoder state */
  1756. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1757. DRM_ERROR("Failed to disable PCH transcoder\n");
  1758. /* Workaround: clear timing override bit. */
  1759. val = I915_READ(_TRANSA_CHICKEN2);
  1760. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1761. I915_WRITE(_TRANSA_CHICKEN2, val);
  1762. }
  1763. /**
  1764. * intel_enable_pipe - enable a pipe, asserting requirements
  1765. * @crtc: crtc responsible for the pipe
  1766. *
  1767. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1768. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1769. */
  1770. static void intel_enable_pipe(struct intel_crtc *crtc)
  1771. {
  1772. struct drm_device *dev = crtc->base.dev;
  1773. struct drm_i915_private *dev_priv = dev->dev_private;
  1774. enum pipe pipe = crtc->pipe;
  1775. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1776. pipe);
  1777. enum pipe pch_transcoder;
  1778. int reg;
  1779. u32 val;
  1780. assert_planes_disabled(dev_priv, pipe);
  1781. assert_cursor_disabled(dev_priv, pipe);
  1782. assert_sprites_disabled(dev_priv, pipe);
  1783. if (HAS_PCH_LPT(dev_priv->dev))
  1784. pch_transcoder = TRANSCODER_A;
  1785. else
  1786. pch_transcoder = pipe;
  1787. /*
  1788. * A pipe without a PLL won't actually be able to drive bits from
  1789. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1790. * need the check.
  1791. */
  1792. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1793. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1794. assert_dsi_pll_enabled(dev_priv);
  1795. else
  1796. assert_pll_enabled(dev_priv, pipe);
  1797. else {
  1798. if (crtc->config->has_pch_encoder) {
  1799. /* if driving the PCH, we need FDI enabled */
  1800. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1801. assert_fdi_tx_pll_enabled(dev_priv,
  1802. (enum pipe) cpu_transcoder);
  1803. }
  1804. /* FIXME: assert CPU port conditions for SNB+ */
  1805. }
  1806. reg = PIPECONF(cpu_transcoder);
  1807. val = I915_READ(reg);
  1808. if (val & PIPECONF_ENABLE) {
  1809. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1810. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1811. return;
  1812. }
  1813. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1814. POSTING_READ(reg);
  1815. }
  1816. /**
  1817. * intel_disable_pipe - disable a pipe, asserting requirements
  1818. * @crtc: crtc whose pipes is to be disabled
  1819. *
  1820. * Disable the pipe of @crtc, making sure that various hardware
  1821. * specific requirements are met, if applicable, e.g. plane
  1822. * disabled, panel fitter off, etc.
  1823. *
  1824. * Will wait until the pipe has shut down before returning.
  1825. */
  1826. static void intel_disable_pipe(struct intel_crtc *crtc)
  1827. {
  1828. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1829. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1830. enum pipe pipe = crtc->pipe;
  1831. int reg;
  1832. u32 val;
  1833. /*
  1834. * Make sure planes won't keep trying to pump pixels to us,
  1835. * or we might hang the display.
  1836. */
  1837. assert_planes_disabled(dev_priv, pipe);
  1838. assert_cursor_disabled(dev_priv, pipe);
  1839. assert_sprites_disabled(dev_priv, pipe);
  1840. reg = PIPECONF(cpu_transcoder);
  1841. val = I915_READ(reg);
  1842. if ((val & PIPECONF_ENABLE) == 0)
  1843. return;
  1844. /*
  1845. * Double wide has implications for planes
  1846. * so best keep it disabled when not needed.
  1847. */
  1848. if (crtc->config->double_wide)
  1849. val &= ~PIPECONF_DOUBLE_WIDE;
  1850. /* Don't disable pipe or pipe PLLs if needed */
  1851. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1852. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1853. val &= ~PIPECONF_ENABLE;
  1854. I915_WRITE(reg, val);
  1855. if ((val & PIPECONF_ENABLE) == 0)
  1856. intel_wait_for_pipe_off(crtc);
  1857. }
  1858. /*
  1859. * Plane regs are double buffered, going from enabled->disabled needs a
  1860. * trigger in order to latch. The display address reg provides this.
  1861. */
  1862. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1863. enum plane plane)
  1864. {
  1865. struct drm_device *dev = dev_priv->dev;
  1866. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1867. I915_WRITE(reg, I915_READ(reg));
  1868. POSTING_READ(reg);
  1869. }
  1870. /**
  1871. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1872. * @plane: plane to be enabled
  1873. * @crtc: crtc for the plane
  1874. *
  1875. * Enable @plane on @crtc, making sure that the pipe is running first.
  1876. */
  1877. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1878. struct drm_crtc *crtc)
  1879. {
  1880. struct drm_device *dev = plane->dev;
  1881. struct drm_i915_private *dev_priv = dev->dev_private;
  1882. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1883. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1884. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1885. if (intel_crtc->primary_enabled)
  1886. return;
  1887. intel_crtc->primary_enabled = true;
  1888. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1889. crtc->x, crtc->y);
  1890. /*
  1891. * BDW signals flip done immediately if the plane
  1892. * is disabled, even if the plane enable is already
  1893. * armed to occur at the next vblank :(
  1894. */
  1895. if (IS_BROADWELL(dev))
  1896. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1897. }
  1898. /**
  1899. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1900. * @plane: plane to be disabled
  1901. * @crtc: crtc for the plane
  1902. *
  1903. * Disable @plane on @crtc, making sure that the pipe is running first.
  1904. */
  1905. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1906. struct drm_crtc *crtc)
  1907. {
  1908. struct drm_device *dev = plane->dev;
  1909. struct drm_i915_private *dev_priv = dev->dev_private;
  1910. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1911. if (WARN_ON(!intel_crtc->active))
  1912. return;
  1913. if (!intel_crtc->primary_enabled)
  1914. return;
  1915. intel_crtc->primary_enabled = false;
  1916. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1917. crtc->x, crtc->y);
  1918. }
  1919. static bool need_vtd_wa(struct drm_device *dev)
  1920. {
  1921. #ifdef CONFIG_INTEL_IOMMU
  1922. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1923. return true;
  1924. #endif
  1925. return false;
  1926. }
  1927. unsigned int
  1928. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1929. uint64_t fb_format_modifier)
  1930. {
  1931. unsigned int tile_height;
  1932. uint32_t pixel_bytes;
  1933. switch (fb_format_modifier) {
  1934. case DRM_FORMAT_MOD_NONE:
  1935. tile_height = 1;
  1936. break;
  1937. case I915_FORMAT_MOD_X_TILED:
  1938. tile_height = IS_GEN2(dev) ? 16 : 8;
  1939. break;
  1940. case I915_FORMAT_MOD_Y_TILED:
  1941. tile_height = 32;
  1942. break;
  1943. case I915_FORMAT_MOD_Yf_TILED:
  1944. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1945. switch (pixel_bytes) {
  1946. default:
  1947. case 1:
  1948. tile_height = 64;
  1949. break;
  1950. case 2:
  1951. case 4:
  1952. tile_height = 32;
  1953. break;
  1954. case 8:
  1955. tile_height = 16;
  1956. break;
  1957. case 16:
  1958. WARN_ONCE(1,
  1959. "128-bit pixels are not supported for display!");
  1960. tile_height = 16;
  1961. break;
  1962. }
  1963. break;
  1964. default:
  1965. MISSING_CASE(fb_format_modifier);
  1966. tile_height = 1;
  1967. break;
  1968. }
  1969. return tile_height;
  1970. }
  1971. unsigned int
  1972. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1973. uint32_t pixel_format, uint64_t fb_format_modifier)
  1974. {
  1975. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1976. fb_format_modifier));
  1977. }
  1978. static int
  1979. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1980. const struct drm_plane_state *plane_state)
  1981. {
  1982. struct intel_rotation_info *info = &view->rotation_info;
  1983. static const struct i915_ggtt_view rotated_view =
  1984. { .type = I915_GGTT_VIEW_ROTATED };
  1985. *view = i915_ggtt_view_normal;
  1986. if (!plane_state)
  1987. return 0;
  1988. if (!intel_rotation_90_or_270(plane_state->rotation))
  1989. return 0;
  1990. *view = rotated_view;
  1991. info->height = fb->height;
  1992. info->pixel_format = fb->pixel_format;
  1993. info->pitch = fb->pitches[0];
  1994. info->fb_modifier = fb->modifier[0];
  1995. if (!(info->fb_modifier == I915_FORMAT_MOD_Y_TILED ||
  1996. info->fb_modifier == I915_FORMAT_MOD_Yf_TILED)) {
  1997. DRM_DEBUG_KMS(
  1998. "Y or Yf tiling is needed for 90/270 rotation!\n");
  1999. return -EINVAL;
  2000. }
  2001. return 0;
  2002. }
  2003. int
  2004. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2005. struct drm_framebuffer *fb,
  2006. const struct drm_plane_state *plane_state,
  2007. struct intel_engine_cs *pipelined)
  2008. {
  2009. struct drm_device *dev = fb->dev;
  2010. struct drm_i915_private *dev_priv = dev->dev_private;
  2011. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2012. struct i915_ggtt_view view;
  2013. u32 alignment;
  2014. int ret;
  2015. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2016. switch (fb->modifier[0]) {
  2017. case DRM_FORMAT_MOD_NONE:
  2018. if (INTEL_INFO(dev)->gen >= 9)
  2019. alignment = 256 * 1024;
  2020. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  2021. alignment = 128 * 1024;
  2022. else if (INTEL_INFO(dev)->gen >= 4)
  2023. alignment = 4 * 1024;
  2024. else
  2025. alignment = 64 * 1024;
  2026. break;
  2027. case I915_FORMAT_MOD_X_TILED:
  2028. if (INTEL_INFO(dev)->gen >= 9)
  2029. alignment = 256 * 1024;
  2030. else {
  2031. /* pin() will align the object as required by fence */
  2032. alignment = 0;
  2033. }
  2034. break;
  2035. case I915_FORMAT_MOD_Y_TILED:
  2036. case I915_FORMAT_MOD_Yf_TILED:
  2037. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2038. "Y tiling bo slipped through, driver bug!\n"))
  2039. return -EINVAL;
  2040. alignment = 1 * 1024 * 1024;
  2041. break;
  2042. default:
  2043. MISSING_CASE(fb->modifier[0]);
  2044. return -EINVAL;
  2045. }
  2046. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2047. if (ret)
  2048. return ret;
  2049. /* Note that the w/a also requires 64 PTE of padding following the
  2050. * bo. We currently fill all unused PTE with the shadow page and so
  2051. * we should always have valid PTE following the scanout preventing
  2052. * the VT-d warning.
  2053. */
  2054. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2055. alignment = 256 * 1024;
  2056. /*
  2057. * Global gtt pte registers are special registers which actually forward
  2058. * writes to a chunk of system memory. Which means that there is no risk
  2059. * that the register values disappear as soon as we call
  2060. * intel_runtime_pm_put(), so it is correct to wrap only the
  2061. * pin/unpin/fence and not more.
  2062. */
  2063. intel_runtime_pm_get(dev_priv);
  2064. dev_priv->mm.interruptible = false;
  2065. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2066. &view);
  2067. if (ret)
  2068. goto err_interruptible;
  2069. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2070. * fence, whereas 965+ only requires a fence if using
  2071. * framebuffer compression. For simplicity, we always install
  2072. * a fence as the cost is not that onerous.
  2073. */
  2074. ret = i915_gem_object_get_fence(obj);
  2075. if (ret)
  2076. goto err_unpin;
  2077. i915_gem_object_pin_fence(obj);
  2078. dev_priv->mm.interruptible = true;
  2079. intel_runtime_pm_put(dev_priv);
  2080. return 0;
  2081. err_unpin:
  2082. i915_gem_object_unpin_from_display_plane(obj, &view);
  2083. err_interruptible:
  2084. dev_priv->mm.interruptible = true;
  2085. intel_runtime_pm_put(dev_priv);
  2086. return ret;
  2087. }
  2088. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2089. const struct drm_plane_state *plane_state)
  2090. {
  2091. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2092. struct i915_ggtt_view view;
  2093. int ret;
  2094. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2095. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2096. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2097. i915_gem_object_unpin_fence(obj);
  2098. i915_gem_object_unpin_from_display_plane(obj, &view);
  2099. }
  2100. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2101. * is assumed to be a power-of-two. */
  2102. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2103. unsigned int tiling_mode,
  2104. unsigned int cpp,
  2105. unsigned int pitch)
  2106. {
  2107. if (tiling_mode != I915_TILING_NONE) {
  2108. unsigned int tile_rows, tiles;
  2109. tile_rows = *y / 8;
  2110. *y %= 8;
  2111. tiles = *x / (512/cpp);
  2112. *x %= 512/cpp;
  2113. return tile_rows * pitch * 8 + tiles * 4096;
  2114. } else {
  2115. unsigned int offset;
  2116. offset = *y * pitch + *x * cpp;
  2117. *y = 0;
  2118. *x = (offset & 4095) / cpp;
  2119. return offset & -4096;
  2120. }
  2121. }
  2122. static int i9xx_format_to_fourcc(int format)
  2123. {
  2124. switch (format) {
  2125. case DISPPLANE_8BPP:
  2126. return DRM_FORMAT_C8;
  2127. case DISPPLANE_BGRX555:
  2128. return DRM_FORMAT_XRGB1555;
  2129. case DISPPLANE_BGRX565:
  2130. return DRM_FORMAT_RGB565;
  2131. default:
  2132. case DISPPLANE_BGRX888:
  2133. return DRM_FORMAT_XRGB8888;
  2134. case DISPPLANE_RGBX888:
  2135. return DRM_FORMAT_XBGR8888;
  2136. case DISPPLANE_BGRX101010:
  2137. return DRM_FORMAT_XRGB2101010;
  2138. case DISPPLANE_RGBX101010:
  2139. return DRM_FORMAT_XBGR2101010;
  2140. }
  2141. }
  2142. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2143. {
  2144. switch (format) {
  2145. case PLANE_CTL_FORMAT_RGB_565:
  2146. return DRM_FORMAT_RGB565;
  2147. default:
  2148. case PLANE_CTL_FORMAT_XRGB_8888:
  2149. if (rgb_order) {
  2150. if (alpha)
  2151. return DRM_FORMAT_ABGR8888;
  2152. else
  2153. return DRM_FORMAT_XBGR8888;
  2154. } else {
  2155. if (alpha)
  2156. return DRM_FORMAT_ARGB8888;
  2157. else
  2158. return DRM_FORMAT_XRGB8888;
  2159. }
  2160. case PLANE_CTL_FORMAT_XRGB_2101010:
  2161. if (rgb_order)
  2162. return DRM_FORMAT_XBGR2101010;
  2163. else
  2164. return DRM_FORMAT_XRGB2101010;
  2165. }
  2166. }
  2167. static bool
  2168. intel_alloc_plane_obj(struct intel_crtc *crtc,
  2169. struct intel_initial_plane_config *plane_config)
  2170. {
  2171. struct drm_device *dev = crtc->base.dev;
  2172. struct drm_i915_gem_object *obj = NULL;
  2173. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2174. struct drm_framebuffer *fb = &plane_config->fb->base;
  2175. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2176. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2177. PAGE_SIZE);
  2178. size_aligned -= base_aligned;
  2179. if (plane_config->size == 0)
  2180. return false;
  2181. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2182. base_aligned,
  2183. base_aligned,
  2184. size_aligned);
  2185. if (!obj)
  2186. return false;
  2187. obj->tiling_mode = plane_config->tiling;
  2188. if (obj->tiling_mode == I915_TILING_X)
  2189. obj->stride = fb->pitches[0];
  2190. mode_cmd.pixel_format = fb->pixel_format;
  2191. mode_cmd.width = fb->width;
  2192. mode_cmd.height = fb->height;
  2193. mode_cmd.pitches[0] = fb->pitches[0];
  2194. mode_cmd.modifier[0] = fb->modifier[0];
  2195. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2196. mutex_lock(&dev->struct_mutex);
  2197. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2198. &mode_cmd, obj)) {
  2199. DRM_DEBUG_KMS("intel fb init failed\n");
  2200. goto out_unref_obj;
  2201. }
  2202. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2203. mutex_unlock(&dev->struct_mutex);
  2204. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2205. return true;
  2206. out_unref_obj:
  2207. drm_gem_object_unreference(&obj->base);
  2208. mutex_unlock(&dev->struct_mutex);
  2209. return false;
  2210. }
  2211. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2212. static void
  2213. update_state_fb(struct drm_plane *plane)
  2214. {
  2215. if (plane->fb == plane->state->fb)
  2216. return;
  2217. if (plane->state->fb)
  2218. drm_framebuffer_unreference(plane->state->fb);
  2219. plane->state->fb = plane->fb;
  2220. if (plane->state->fb)
  2221. drm_framebuffer_reference(plane->state->fb);
  2222. }
  2223. static void
  2224. intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2225. struct intel_initial_plane_config *plane_config)
  2226. {
  2227. struct drm_device *dev = intel_crtc->base.dev;
  2228. struct drm_i915_private *dev_priv = dev->dev_private;
  2229. struct drm_crtc *c;
  2230. struct intel_crtc *i;
  2231. struct drm_i915_gem_object *obj;
  2232. if (!plane_config->fb)
  2233. return;
  2234. if (intel_alloc_plane_obj(intel_crtc, plane_config)) {
  2235. struct drm_plane *primary = intel_crtc->base.primary;
  2236. primary->fb = &plane_config->fb->base;
  2237. primary->state->crtc = &intel_crtc->base;
  2238. primary->crtc = &intel_crtc->base;
  2239. update_state_fb(primary);
  2240. return;
  2241. }
  2242. kfree(plane_config->fb);
  2243. /*
  2244. * Failed to alloc the obj, check to see if we should share
  2245. * an fb with another CRTC instead
  2246. */
  2247. for_each_crtc(dev, c) {
  2248. i = to_intel_crtc(c);
  2249. if (c == &intel_crtc->base)
  2250. continue;
  2251. if (!i->active)
  2252. continue;
  2253. obj = intel_fb_obj(c->primary->fb);
  2254. if (obj == NULL)
  2255. continue;
  2256. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2257. struct drm_plane *primary = intel_crtc->base.primary;
  2258. if (obj->tiling_mode != I915_TILING_NONE)
  2259. dev_priv->preserve_bios_swizzle = true;
  2260. drm_framebuffer_reference(c->primary->fb);
  2261. primary->fb = c->primary->fb;
  2262. primary->state->crtc = &intel_crtc->base;
  2263. primary->crtc = &intel_crtc->base;
  2264. update_state_fb(intel_crtc->base.primary);
  2265. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2266. break;
  2267. }
  2268. }
  2269. }
  2270. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2271. struct drm_framebuffer *fb,
  2272. int x, int y)
  2273. {
  2274. struct drm_device *dev = crtc->dev;
  2275. struct drm_i915_private *dev_priv = dev->dev_private;
  2276. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2277. struct drm_i915_gem_object *obj;
  2278. int plane = intel_crtc->plane;
  2279. unsigned long linear_offset;
  2280. u32 dspcntr;
  2281. u32 reg = DSPCNTR(plane);
  2282. int pixel_size;
  2283. if (!intel_crtc->primary_enabled) {
  2284. I915_WRITE(reg, 0);
  2285. if (INTEL_INFO(dev)->gen >= 4)
  2286. I915_WRITE(DSPSURF(plane), 0);
  2287. else
  2288. I915_WRITE(DSPADDR(plane), 0);
  2289. POSTING_READ(reg);
  2290. return;
  2291. }
  2292. obj = intel_fb_obj(fb);
  2293. if (WARN_ON(obj == NULL))
  2294. return;
  2295. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2296. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2297. dspcntr |= DISPLAY_PLANE_ENABLE;
  2298. if (INTEL_INFO(dev)->gen < 4) {
  2299. if (intel_crtc->pipe == PIPE_B)
  2300. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2301. /* pipesrc and dspsize control the size that is scaled from,
  2302. * which should always be the user's requested size.
  2303. */
  2304. I915_WRITE(DSPSIZE(plane),
  2305. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2306. (intel_crtc->config->pipe_src_w - 1));
  2307. I915_WRITE(DSPPOS(plane), 0);
  2308. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2309. I915_WRITE(PRIMSIZE(plane),
  2310. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2311. (intel_crtc->config->pipe_src_w - 1));
  2312. I915_WRITE(PRIMPOS(plane), 0);
  2313. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2314. }
  2315. switch (fb->pixel_format) {
  2316. case DRM_FORMAT_C8:
  2317. dspcntr |= DISPPLANE_8BPP;
  2318. break;
  2319. case DRM_FORMAT_XRGB1555:
  2320. case DRM_FORMAT_ARGB1555:
  2321. dspcntr |= DISPPLANE_BGRX555;
  2322. break;
  2323. case DRM_FORMAT_RGB565:
  2324. dspcntr |= DISPPLANE_BGRX565;
  2325. break;
  2326. case DRM_FORMAT_XRGB8888:
  2327. case DRM_FORMAT_ARGB8888:
  2328. dspcntr |= DISPPLANE_BGRX888;
  2329. break;
  2330. case DRM_FORMAT_XBGR8888:
  2331. case DRM_FORMAT_ABGR8888:
  2332. dspcntr |= DISPPLANE_RGBX888;
  2333. break;
  2334. case DRM_FORMAT_XRGB2101010:
  2335. case DRM_FORMAT_ARGB2101010:
  2336. dspcntr |= DISPPLANE_BGRX101010;
  2337. break;
  2338. case DRM_FORMAT_XBGR2101010:
  2339. case DRM_FORMAT_ABGR2101010:
  2340. dspcntr |= DISPPLANE_RGBX101010;
  2341. break;
  2342. default:
  2343. BUG();
  2344. }
  2345. if (INTEL_INFO(dev)->gen >= 4 &&
  2346. obj->tiling_mode != I915_TILING_NONE)
  2347. dspcntr |= DISPPLANE_TILED;
  2348. if (IS_G4X(dev))
  2349. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2350. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2351. if (INTEL_INFO(dev)->gen >= 4) {
  2352. intel_crtc->dspaddr_offset =
  2353. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2354. pixel_size,
  2355. fb->pitches[0]);
  2356. linear_offset -= intel_crtc->dspaddr_offset;
  2357. } else {
  2358. intel_crtc->dspaddr_offset = linear_offset;
  2359. }
  2360. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2361. dspcntr |= DISPPLANE_ROTATE_180;
  2362. x += (intel_crtc->config->pipe_src_w - 1);
  2363. y += (intel_crtc->config->pipe_src_h - 1);
  2364. /* Finding the last pixel of the last line of the display
  2365. data and adding to linear_offset*/
  2366. linear_offset +=
  2367. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2368. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2369. }
  2370. I915_WRITE(reg, dspcntr);
  2371. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2372. if (INTEL_INFO(dev)->gen >= 4) {
  2373. I915_WRITE(DSPSURF(plane),
  2374. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2375. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2376. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2377. } else
  2378. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2379. POSTING_READ(reg);
  2380. }
  2381. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2382. struct drm_framebuffer *fb,
  2383. int x, int y)
  2384. {
  2385. struct drm_device *dev = crtc->dev;
  2386. struct drm_i915_private *dev_priv = dev->dev_private;
  2387. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2388. struct drm_i915_gem_object *obj;
  2389. int plane = intel_crtc->plane;
  2390. unsigned long linear_offset;
  2391. u32 dspcntr;
  2392. u32 reg = DSPCNTR(plane);
  2393. int pixel_size;
  2394. if (!intel_crtc->primary_enabled) {
  2395. I915_WRITE(reg, 0);
  2396. I915_WRITE(DSPSURF(plane), 0);
  2397. POSTING_READ(reg);
  2398. return;
  2399. }
  2400. obj = intel_fb_obj(fb);
  2401. if (WARN_ON(obj == NULL))
  2402. return;
  2403. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2404. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2405. dspcntr |= DISPLAY_PLANE_ENABLE;
  2406. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2407. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2408. switch (fb->pixel_format) {
  2409. case DRM_FORMAT_C8:
  2410. dspcntr |= DISPPLANE_8BPP;
  2411. break;
  2412. case DRM_FORMAT_RGB565:
  2413. dspcntr |= DISPPLANE_BGRX565;
  2414. break;
  2415. case DRM_FORMAT_XRGB8888:
  2416. case DRM_FORMAT_ARGB8888:
  2417. dspcntr |= DISPPLANE_BGRX888;
  2418. break;
  2419. case DRM_FORMAT_XBGR8888:
  2420. case DRM_FORMAT_ABGR8888:
  2421. dspcntr |= DISPPLANE_RGBX888;
  2422. break;
  2423. case DRM_FORMAT_XRGB2101010:
  2424. case DRM_FORMAT_ARGB2101010:
  2425. dspcntr |= DISPPLANE_BGRX101010;
  2426. break;
  2427. case DRM_FORMAT_XBGR2101010:
  2428. case DRM_FORMAT_ABGR2101010:
  2429. dspcntr |= DISPPLANE_RGBX101010;
  2430. break;
  2431. default:
  2432. BUG();
  2433. }
  2434. if (obj->tiling_mode != I915_TILING_NONE)
  2435. dspcntr |= DISPPLANE_TILED;
  2436. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2437. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2438. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2439. intel_crtc->dspaddr_offset =
  2440. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2441. pixel_size,
  2442. fb->pitches[0]);
  2443. linear_offset -= intel_crtc->dspaddr_offset;
  2444. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2445. dspcntr |= DISPPLANE_ROTATE_180;
  2446. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2447. x += (intel_crtc->config->pipe_src_w - 1);
  2448. y += (intel_crtc->config->pipe_src_h - 1);
  2449. /* Finding the last pixel of the last line of the display
  2450. data and adding to linear_offset*/
  2451. linear_offset +=
  2452. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2453. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2454. }
  2455. }
  2456. I915_WRITE(reg, dspcntr);
  2457. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2458. I915_WRITE(DSPSURF(plane),
  2459. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2460. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2461. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2462. } else {
  2463. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2464. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2465. }
  2466. POSTING_READ(reg);
  2467. }
  2468. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2469. uint32_t pixel_format)
  2470. {
  2471. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2472. /*
  2473. * The stride is either expressed as a multiple of 64 bytes
  2474. * chunks for linear buffers or in number of tiles for tiled
  2475. * buffers.
  2476. */
  2477. switch (fb_modifier) {
  2478. case DRM_FORMAT_MOD_NONE:
  2479. return 64;
  2480. case I915_FORMAT_MOD_X_TILED:
  2481. if (INTEL_INFO(dev)->gen == 2)
  2482. return 128;
  2483. return 512;
  2484. case I915_FORMAT_MOD_Y_TILED:
  2485. /* No need to check for old gens and Y tiling since this is
  2486. * about the display engine and those will be blocked before
  2487. * we get here.
  2488. */
  2489. return 128;
  2490. case I915_FORMAT_MOD_Yf_TILED:
  2491. if (bits_per_pixel == 8)
  2492. return 64;
  2493. else
  2494. return 128;
  2495. default:
  2496. MISSING_CASE(fb_modifier);
  2497. return 64;
  2498. }
  2499. }
  2500. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2501. struct drm_i915_gem_object *obj)
  2502. {
  2503. enum i915_ggtt_view_type view = I915_GGTT_VIEW_NORMAL;
  2504. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2505. view = I915_GGTT_VIEW_ROTATED;
  2506. return i915_gem_obj_ggtt_offset_view(obj, view);
  2507. }
  2508. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2509. struct drm_framebuffer *fb,
  2510. int x, int y)
  2511. {
  2512. struct drm_device *dev = crtc->dev;
  2513. struct drm_i915_private *dev_priv = dev->dev_private;
  2514. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2515. struct drm_i915_gem_object *obj;
  2516. int pipe = intel_crtc->pipe;
  2517. u32 plane_ctl, stride_div;
  2518. unsigned long surf_addr;
  2519. if (!intel_crtc->primary_enabled) {
  2520. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2521. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2522. POSTING_READ(PLANE_CTL(pipe, 0));
  2523. return;
  2524. }
  2525. plane_ctl = PLANE_CTL_ENABLE |
  2526. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2527. PLANE_CTL_PIPE_CSC_ENABLE;
  2528. switch (fb->pixel_format) {
  2529. case DRM_FORMAT_RGB565:
  2530. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2531. break;
  2532. case DRM_FORMAT_XRGB8888:
  2533. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2534. break;
  2535. case DRM_FORMAT_ARGB8888:
  2536. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2537. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2538. break;
  2539. case DRM_FORMAT_XBGR8888:
  2540. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2541. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2542. break;
  2543. case DRM_FORMAT_ABGR8888:
  2544. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2545. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2546. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2547. break;
  2548. case DRM_FORMAT_XRGB2101010:
  2549. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2550. break;
  2551. case DRM_FORMAT_XBGR2101010:
  2552. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2553. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2554. break;
  2555. default:
  2556. BUG();
  2557. }
  2558. switch (fb->modifier[0]) {
  2559. case DRM_FORMAT_MOD_NONE:
  2560. break;
  2561. case I915_FORMAT_MOD_X_TILED:
  2562. plane_ctl |= PLANE_CTL_TILED_X;
  2563. break;
  2564. case I915_FORMAT_MOD_Y_TILED:
  2565. plane_ctl |= PLANE_CTL_TILED_Y;
  2566. break;
  2567. case I915_FORMAT_MOD_Yf_TILED:
  2568. plane_ctl |= PLANE_CTL_TILED_YF;
  2569. break;
  2570. default:
  2571. MISSING_CASE(fb->modifier[0]);
  2572. }
  2573. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2574. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
  2575. plane_ctl |= PLANE_CTL_ROTATE_180;
  2576. obj = intel_fb_obj(fb);
  2577. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2578. fb->pixel_format);
  2579. surf_addr = intel_plane_obj_offset(to_intel_plane(crtc->primary), obj);
  2580. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2581. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2582. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2583. I915_WRITE(PLANE_SIZE(pipe, 0),
  2584. (intel_crtc->config->pipe_src_h - 1) << 16 |
  2585. (intel_crtc->config->pipe_src_w - 1));
  2586. I915_WRITE(PLANE_STRIDE(pipe, 0), fb->pitches[0] / stride_div);
  2587. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2588. POSTING_READ(PLANE_SURF(pipe, 0));
  2589. }
  2590. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2591. static int
  2592. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2593. int x, int y, enum mode_set_atomic state)
  2594. {
  2595. struct drm_device *dev = crtc->dev;
  2596. struct drm_i915_private *dev_priv = dev->dev_private;
  2597. if (dev_priv->display.disable_fbc)
  2598. dev_priv->display.disable_fbc(dev);
  2599. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2600. return 0;
  2601. }
  2602. static void intel_complete_page_flips(struct drm_device *dev)
  2603. {
  2604. struct drm_crtc *crtc;
  2605. for_each_crtc(dev, crtc) {
  2606. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2607. enum plane plane = intel_crtc->plane;
  2608. intel_prepare_page_flip(dev, plane);
  2609. intel_finish_page_flip_plane(dev, plane);
  2610. }
  2611. }
  2612. static void intel_update_primary_planes(struct drm_device *dev)
  2613. {
  2614. struct drm_i915_private *dev_priv = dev->dev_private;
  2615. struct drm_crtc *crtc;
  2616. for_each_crtc(dev, crtc) {
  2617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2618. drm_modeset_lock(&crtc->mutex, NULL);
  2619. /*
  2620. * FIXME: Once we have proper support for primary planes (and
  2621. * disabling them without disabling the entire crtc) allow again
  2622. * a NULL crtc->primary->fb.
  2623. */
  2624. if (intel_crtc->active && crtc->primary->fb)
  2625. dev_priv->display.update_primary_plane(crtc,
  2626. crtc->primary->fb,
  2627. crtc->x,
  2628. crtc->y);
  2629. drm_modeset_unlock(&crtc->mutex);
  2630. }
  2631. }
  2632. void intel_prepare_reset(struct drm_device *dev)
  2633. {
  2634. struct drm_i915_private *dev_priv = to_i915(dev);
  2635. struct intel_crtc *crtc;
  2636. /* no reset support for gen2 */
  2637. if (IS_GEN2(dev))
  2638. return;
  2639. /* reset doesn't touch the display */
  2640. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2641. return;
  2642. drm_modeset_lock_all(dev);
  2643. /*
  2644. * Disabling the crtcs gracefully seems nicer. Also the
  2645. * g33 docs say we should at least disable all the planes.
  2646. */
  2647. for_each_intel_crtc(dev, crtc) {
  2648. if (crtc->active)
  2649. dev_priv->display.crtc_disable(&crtc->base);
  2650. }
  2651. }
  2652. void intel_finish_reset(struct drm_device *dev)
  2653. {
  2654. struct drm_i915_private *dev_priv = to_i915(dev);
  2655. /*
  2656. * Flips in the rings will be nuked by the reset,
  2657. * so complete all pending flips so that user space
  2658. * will get its events and not get stuck.
  2659. */
  2660. intel_complete_page_flips(dev);
  2661. /* no reset support for gen2 */
  2662. if (IS_GEN2(dev))
  2663. return;
  2664. /* reset doesn't touch the display */
  2665. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2666. /*
  2667. * Flips in the rings have been nuked by the reset,
  2668. * so update the base address of all primary
  2669. * planes to the the last fb to make sure we're
  2670. * showing the correct fb after a reset.
  2671. */
  2672. intel_update_primary_planes(dev);
  2673. return;
  2674. }
  2675. /*
  2676. * The display has been reset as well,
  2677. * so need a full re-initialization.
  2678. */
  2679. intel_runtime_pm_disable_interrupts(dev_priv);
  2680. intel_runtime_pm_enable_interrupts(dev_priv);
  2681. intel_modeset_init_hw(dev);
  2682. spin_lock_irq(&dev_priv->irq_lock);
  2683. if (dev_priv->display.hpd_irq_setup)
  2684. dev_priv->display.hpd_irq_setup(dev);
  2685. spin_unlock_irq(&dev_priv->irq_lock);
  2686. intel_modeset_setup_hw_state(dev, true);
  2687. intel_hpd_init(dev_priv);
  2688. drm_modeset_unlock_all(dev);
  2689. }
  2690. static int
  2691. intel_finish_fb(struct drm_framebuffer *old_fb)
  2692. {
  2693. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2694. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2695. bool was_interruptible = dev_priv->mm.interruptible;
  2696. int ret;
  2697. /* Big Hammer, we also need to ensure that any pending
  2698. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2699. * current scanout is retired before unpinning the old
  2700. * framebuffer.
  2701. *
  2702. * This should only fail upon a hung GPU, in which case we
  2703. * can safely continue.
  2704. */
  2705. dev_priv->mm.interruptible = false;
  2706. ret = i915_gem_object_finish_gpu(obj);
  2707. dev_priv->mm.interruptible = was_interruptible;
  2708. return ret;
  2709. }
  2710. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2711. {
  2712. struct drm_device *dev = crtc->dev;
  2713. struct drm_i915_private *dev_priv = dev->dev_private;
  2714. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2715. bool pending;
  2716. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2717. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2718. return false;
  2719. spin_lock_irq(&dev->event_lock);
  2720. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2721. spin_unlock_irq(&dev->event_lock);
  2722. return pending;
  2723. }
  2724. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2725. {
  2726. struct drm_device *dev = crtc->base.dev;
  2727. struct drm_i915_private *dev_priv = dev->dev_private;
  2728. const struct drm_display_mode *adjusted_mode;
  2729. if (!i915.fastboot)
  2730. return;
  2731. /*
  2732. * Update pipe size and adjust fitter if needed: the reason for this is
  2733. * that in compute_mode_changes we check the native mode (not the pfit
  2734. * mode) to see if we can flip rather than do a full mode set. In the
  2735. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2736. * pfit state, we'll end up with a big fb scanned out into the wrong
  2737. * sized surface.
  2738. *
  2739. * To fix this properly, we need to hoist the checks up into
  2740. * compute_mode_changes (or above), check the actual pfit state and
  2741. * whether the platform allows pfit disable with pipe active, and only
  2742. * then update the pipesrc and pfit state, even on the flip path.
  2743. */
  2744. adjusted_mode = &crtc->config->base.adjusted_mode;
  2745. I915_WRITE(PIPESRC(crtc->pipe),
  2746. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2747. (adjusted_mode->crtc_vdisplay - 1));
  2748. if (!crtc->config->pch_pfit.enabled &&
  2749. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2750. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2751. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2752. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2753. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2754. }
  2755. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2756. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2757. }
  2758. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2759. {
  2760. struct drm_device *dev = crtc->dev;
  2761. struct drm_i915_private *dev_priv = dev->dev_private;
  2762. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2763. int pipe = intel_crtc->pipe;
  2764. u32 reg, temp;
  2765. /* enable normal train */
  2766. reg = FDI_TX_CTL(pipe);
  2767. temp = I915_READ(reg);
  2768. if (IS_IVYBRIDGE(dev)) {
  2769. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2770. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2771. } else {
  2772. temp &= ~FDI_LINK_TRAIN_NONE;
  2773. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2774. }
  2775. I915_WRITE(reg, temp);
  2776. reg = FDI_RX_CTL(pipe);
  2777. temp = I915_READ(reg);
  2778. if (HAS_PCH_CPT(dev)) {
  2779. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2780. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2781. } else {
  2782. temp &= ~FDI_LINK_TRAIN_NONE;
  2783. temp |= FDI_LINK_TRAIN_NONE;
  2784. }
  2785. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2786. /* wait one idle pattern time */
  2787. POSTING_READ(reg);
  2788. udelay(1000);
  2789. /* IVB wants error correction enabled */
  2790. if (IS_IVYBRIDGE(dev))
  2791. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2792. FDI_FE_ERRC_ENABLE);
  2793. }
  2794. /* The FDI link training functions for ILK/Ibexpeak. */
  2795. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2796. {
  2797. struct drm_device *dev = crtc->dev;
  2798. struct drm_i915_private *dev_priv = dev->dev_private;
  2799. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2800. int pipe = intel_crtc->pipe;
  2801. u32 reg, temp, tries;
  2802. /* FDI needs bits from pipe first */
  2803. assert_pipe_enabled(dev_priv, pipe);
  2804. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2805. for train result */
  2806. reg = FDI_RX_IMR(pipe);
  2807. temp = I915_READ(reg);
  2808. temp &= ~FDI_RX_SYMBOL_LOCK;
  2809. temp &= ~FDI_RX_BIT_LOCK;
  2810. I915_WRITE(reg, temp);
  2811. I915_READ(reg);
  2812. udelay(150);
  2813. /* enable CPU FDI TX and PCH FDI RX */
  2814. reg = FDI_TX_CTL(pipe);
  2815. temp = I915_READ(reg);
  2816. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2817. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2818. temp &= ~FDI_LINK_TRAIN_NONE;
  2819. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2820. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2821. reg = FDI_RX_CTL(pipe);
  2822. temp = I915_READ(reg);
  2823. temp &= ~FDI_LINK_TRAIN_NONE;
  2824. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2825. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2826. POSTING_READ(reg);
  2827. udelay(150);
  2828. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2829. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2830. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2831. FDI_RX_PHASE_SYNC_POINTER_EN);
  2832. reg = FDI_RX_IIR(pipe);
  2833. for (tries = 0; tries < 5; tries++) {
  2834. temp = I915_READ(reg);
  2835. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2836. if ((temp & FDI_RX_BIT_LOCK)) {
  2837. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2838. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2839. break;
  2840. }
  2841. }
  2842. if (tries == 5)
  2843. DRM_ERROR("FDI train 1 fail!\n");
  2844. /* Train 2 */
  2845. reg = FDI_TX_CTL(pipe);
  2846. temp = I915_READ(reg);
  2847. temp &= ~FDI_LINK_TRAIN_NONE;
  2848. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2849. I915_WRITE(reg, temp);
  2850. reg = FDI_RX_CTL(pipe);
  2851. temp = I915_READ(reg);
  2852. temp &= ~FDI_LINK_TRAIN_NONE;
  2853. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2854. I915_WRITE(reg, temp);
  2855. POSTING_READ(reg);
  2856. udelay(150);
  2857. reg = FDI_RX_IIR(pipe);
  2858. for (tries = 0; tries < 5; tries++) {
  2859. temp = I915_READ(reg);
  2860. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2861. if (temp & FDI_RX_SYMBOL_LOCK) {
  2862. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2863. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2864. break;
  2865. }
  2866. }
  2867. if (tries == 5)
  2868. DRM_ERROR("FDI train 2 fail!\n");
  2869. DRM_DEBUG_KMS("FDI train done\n");
  2870. }
  2871. static const int snb_b_fdi_train_param[] = {
  2872. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2873. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2874. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2875. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2876. };
  2877. /* The FDI link training functions for SNB/Cougarpoint. */
  2878. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2879. {
  2880. struct drm_device *dev = crtc->dev;
  2881. struct drm_i915_private *dev_priv = dev->dev_private;
  2882. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2883. int pipe = intel_crtc->pipe;
  2884. u32 reg, temp, i, retry;
  2885. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2886. for train result */
  2887. reg = FDI_RX_IMR(pipe);
  2888. temp = I915_READ(reg);
  2889. temp &= ~FDI_RX_SYMBOL_LOCK;
  2890. temp &= ~FDI_RX_BIT_LOCK;
  2891. I915_WRITE(reg, temp);
  2892. POSTING_READ(reg);
  2893. udelay(150);
  2894. /* enable CPU FDI TX and PCH FDI RX */
  2895. reg = FDI_TX_CTL(pipe);
  2896. temp = I915_READ(reg);
  2897. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2898. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2899. temp &= ~FDI_LINK_TRAIN_NONE;
  2900. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2901. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2902. /* SNB-B */
  2903. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2904. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2905. I915_WRITE(FDI_RX_MISC(pipe),
  2906. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2907. reg = FDI_RX_CTL(pipe);
  2908. temp = I915_READ(reg);
  2909. if (HAS_PCH_CPT(dev)) {
  2910. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2911. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2912. } else {
  2913. temp &= ~FDI_LINK_TRAIN_NONE;
  2914. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2915. }
  2916. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2917. POSTING_READ(reg);
  2918. udelay(150);
  2919. for (i = 0; i < 4; i++) {
  2920. reg = FDI_TX_CTL(pipe);
  2921. temp = I915_READ(reg);
  2922. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2923. temp |= snb_b_fdi_train_param[i];
  2924. I915_WRITE(reg, temp);
  2925. POSTING_READ(reg);
  2926. udelay(500);
  2927. for (retry = 0; retry < 5; retry++) {
  2928. reg = FDI_RX_IIR(pipe);
  2929. temp = I915_READ(reg);
  2930. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2931. if (temp & FDI_RX_BIT_LOCK) {
  2932. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2933. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2934. break;
  2935. }
  2936. udelay(50);
  2937. }
  2938. if (retry < 5)
  2939. break;
  2940. }
  2941. if (i == 4)
  2942. DRM_ERROR("FDI train 1 fail!\n");
  2943. /* Train 2 */
  2944. reg = FDI_TX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. temp &= ~FDI_LINK_TRAIN_NONE;
  2947. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2948. if (IS_GEN6(dev)) {
  2949. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2950. /* SNB-B */
  2951. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2952. }
  2953. I915_WRITE(reg, temp);
  2954. reg = FDI_RX_CTL(pipe);
  2955. temp = I915_READ(reg);
  2956. if (HAS_PCH_CPT(dev)) {
  2957. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2958. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2959. } else {
  2960. temp &= ~FDI_LINK_TRAIN_NONE;
  2961. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2962. }
  2963. I915_WRITE(reg, temp);
  2964. POSTING_READ(reg);
  2965. udelay(150);
  2966. for (i = 0; i < 4; i++) {
  2967. reg = FDI_TX_CTL(pipe);
  2968. temp = I915_READ(reg);
  2969. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2970. temp |= snb_b_fdi_train_param[i];
  2971. I915_WRITE(reg, temp);
  2972. POSTING_READ(reg);
  2973. udelay(500);
  2974. for (retry = 0; retry < 5; retry++) {
  2975. reg = FDI_RX_IIR(pipe);
  2976. temp = I915_READ(reg);
  2977. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2978. if (temp & FDI_RX_SYMBOL_LOCK) {
  2979. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2980. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2981. break;
  2982. }
  2983. udelay(50);
  2984. }
  2985. if (retry < 5)
  2986. break;
  2987. }
  2988. if (i == 4)
  2989. DRM_ERROR("FDI train 2 fail!\n");
  2990. DRM_DEBUG_KMS("FDI train done.\n");
  2991. }
  2992. /* Manual link training for Ivy Bridge A0 parts */
  2993. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2994. {
  2995. struct drm_device *dev = crtc->dev;
  2996. struct drm_i915_private *dev_priv = dev->dev_private;
  2997. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2998. int pipe = intel_crtc->pipe;
  2999. u32 reg, temp, i, j;
  3000. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3001. for train result */
  3002. reg = FDI_RX_IMR(pipe);
  3003. temp = I915_READ(reg);
  3004. temp &= ~FDI_RX_SYMBOL_LOCK;
  3005. temp &= ~FDI_RX_BIT_LOCK;
  3006. I915_WRITE(reg, temp);
  3007. POSTING_READ(reg);
  3008. udelay(150);
  3009. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3010. I915_READ(FDI_RX_IIR(pipe)));
  3011. /* Try each vswing and preemphasis setting twice before moving on */
  3012. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3013. /* disable first in case we need to retry */
  3014. reg = FDI_TX_CTL(pipe);
  3015. temp = I915_READ(reg);
  3016. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3017. temp &= ~FDI_TX_ENABLE;
  3018. I915_WRITE(reg, temp);
  3019. reg = FDI_RX_CTL(pipe);
  3020. temp = I915_READ(reg);
  3021. temp &= ~FDI_LINK_TRAIN_AUTO;
  3022. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3023. temp &= ~FDI_RX_ENABLE;
  3024. I915_WRITE(reg, temp);
  3025. /* enable CPU FDI TX and PCH FDI RX */
  3026. reg = FDI_TX_CTL(pipe);
  3027. temp = I915_READ(reg);
  3028. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3029. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3030. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3031. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3032. temp |= snb_b_fdi_train_param[j/2];
  3033. temp |= FDI_COMPOSITE_SYNC;
  3034. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3035. I915_WRITE(FDI_RX_MISC(pipe),
  3036. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3037. reg = FDI_RX_CTL(pipe);
  3038. temp = I915_READ(reg);
  3039. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3040. temp |= FDI_COMPOSITE_SYNC;
  3041. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3042. POSTING_READ(reg);
  3043. udelay(1); /* should be 0.5us */
  3044. for (i = 0; i < 4; i++) {
  3045. reg = FDI_RX_IIR(pipe);
  3046. temp = I915_READ(reg);
  3047. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3048. if (temp & FDI_RX_BIT_LOCK ||
  3049. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3050. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3051. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3052. i);
  3053. break;
  3054. }
  3055. udelay(1); /* should be 0.5us */
  3056. }
  3057. if (i == 4) {
  3058. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3059. continue;
  3060. }
  3061. /* Train 2 */
  3062. reg = FDI_TX_CTL(pipe);
  3063. temp = I915_READ(reg);
  3064. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3065. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3066. I915_WRITE(reg, temp);
  3067. reg = FDI_RX_CTL(pipe);
  3068. temp = I915_READ(reg);
  3069. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3070. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3071. I915_WRITE(reg, temp);
  3072. POSTING_READ(reg);
  3073. udelay(2); /* should be 1.5us */
  3074. for (i = 0; i < 4; i++) {
  3075. reg = FDI_RX_IIR(pipe);
  3076. temp = I915_READ(reg);
  3077. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3078. if (temp & FDI_RX_SYMBOL_LOCK ||
  3079. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3080. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3081. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3082. i);
  3083. goto train_done;
  3084. }
  3085. udelay(2); /* should be 1.5us */
  3086. }
  3087. if (i == 4)
  3088. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3089. }
  3090. train_done:
  3091. DRM_DEBUG_KMS("FDI train done.\n");
  3092. }
  3093. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3094. {
  3095. struct drm_device *dev = intel_crtc->base.dev;
  3096. struct drm_i915_private *dev_priv = dev->dev_private;
  3097. int pipe = intel_crtc->pipe;
  3098. u32 reg, temp;
  3099. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3100. reg = FDI_RX_CTL(pipe);
  3101. temp = I915_READ(reg);
  3102. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3103. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3104. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3105. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3106. POSTING_READ(reg);
  3107. udelay(200);
  3108. /* Switch from Rawclk to PCDclk */
  3109. temp = I915_READ(reg);
  3110. I915_WRITE(reg, temp | FDI_PCDCLK);
  3111. POSTING_READ(reg);
  3112. udelay(200);
  3113. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3114. reg = FDI_TX_CTL(pipe);
  3115. temp = I915_READ(reg);
  3116. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3117. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3118. POSTING_READ(reg);
  3119. udelay(100);
  3120. }
  3121. }
  3122. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3123. {
  3124. struct drm_device *dev = intel_crtc->base.dev;
  3125. struct drm_i915_private *dev_priv = dev->dev_private;
  3126. int pipe = intel_crtc->pipe;
  3127. u32 reg, temp;
  3128. /* Switch from PCDclk to Rawclk */
  3129. reg = FDI_RX_CTL(pipe);
  3130. temp = I915_READ(reg);
  3131. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3132. /* Disable CPU FDI TX PLL */
  3133. reg = FDI_TX_CTL(pipe);
  3134. temp = I915_READ(reg);
  3135. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3136. POSTING_READ(reg);
  3137. udelay(100);
  3138. reg = FDI_RX_CTL(pipe);
  3139. temp = I915_READ(reg);
  3140. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3141. /* Wait for the clocks to turn off. */
  3142. POSTING_READ(reg);
  3143. udelay(100);
  3144. }
  3145. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3146. {
  3147. struct drm_device *dev = crtc->dev;
  3148. struct drm_i915_private *dev_priv = dev->dev_private;
  3149. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3150. int pipe = intel_crtc->pipe;
  3151. u32 reg, temp;
  3152. /* disable CPU FDI tx and PCH FDI rx */
  3153. reg = FDI_TX_CTL(pipe);
  3154. temp = I915_READ(reg);
  3155. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3156. POSTING_READ(reg);
  3157. reg = FDI_RX_CTL(pipe);
  3158. temp = I915_READ(reg);
  3159. temp &= ~(0x7 << 16);
  3160. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3161. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3162. POSTING_READ(reg);
  3163. udelay(100);
  3164. /* Ironlake workaround, disable clock pointer after downing FDI */
  3165. if (HAS_PCH_IBX(dev))
  3166. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3167. /* still set train pattern 1 */
  3168. reg = FDI_TX_CTL(pipe);
  3169. temp = I915_READ(reg);
  3170. temp &= ~FDI_LINK_TRAIN_NONE;
  3171. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3172. I915_WRITE(reg, temp);
  3173. reg = FDI_RX_CTL(pipe);
  3174. temp = I915_READ(reg);
  3175. if (HAS_PCH_CPT(dev)) {
  3176. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3177. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3178. } else {
  3179. temp &= ~FDI_LINK_TRAIN_NONE;
  3180. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3181. }
  3182. /* BPC in FDI rx is consistent with that in PIPECONF */
  3183. temp &= ~(0x07 << 16);
  3184. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3185. I915_WRITE(reg, temp);
  3186. POSTING_READ(reg);
  3187. udelay(100);
  3188. }
  3189. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3190. {
  3191. struct intel_crtc *crtc;
  3192. /* Note that we don't need to be called with mode_config.lock here
  3193. * as our list of CRTC objects is static for the lifetime of the
  3194. * device and so cannot disappear as we iterate. Similarly, we can
  3195. * happily treat the predicates as racy, atomic checks as userspace
  3196. * cannot claim and pin a new fb without at least acquring the
  3197. * struct_mutex and so serialising with us.
  3198. */
  3199. for_each_intel_crtc(dev, crtc) {
  3200. if (atomic_read(&crtc->unpin_work_count) == 0)
  3201. continue;
  3202. if (crtc->unpin_work)
  3203. intel_wait_for_vblank(dev, crtc->pipe);
  3204. return true;
  3205. }
  3206. return false;
  3207. }
  3208. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3209. {
  3210. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3211. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3212. /* ensure that the unpin work is consistent wrt ->pending. */
  3213. smp_rmb();
  3214. intel_crtc->unpin_work = NULL;
  3215. if (work->event)
  3216. drm_send_vblank_event(intel_crtc->base.dev,
  3217. intel_crtc->pipe,
  3218. work->event);
  3219. drm_crtc_vblank_put(&intel_crtc->base);
  3220. wake_up_all(&dev_priv->pending_flip_queue);
  3221. queue_work(dev_priv->wq, &work->work);
  3222. trace_i915_flip_complete(intel_crtc->plane,
  3223. work->pending_flip_obj);
  3224. }
  3225. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3226. {
  3227. struct drm_device *dev = crtc->dev;
  3228. struct drm_i915_private *dev_priv = dev->dev_private;
  3229. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3230. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3231. !intel_crtc_has_pending_flip(crtc),
  3232. 60*HZ) == 0)) {
  3233. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3234. spin_lock_irq(&dev->event_lock);
  3235. if (intel_crtc->unpin_work) {
  3236. WARN_ONCE(1, "Removing stuck page flip\n");
  3237. page_flip_completed(intel_crtc);
  3238. }
  3239. spin_unlock_irq(&dev->event_lock);
  3240. }
  3241. if (crtc->primary->fb) {
  3242. mutex_lock(&dev->struct_mutex);
  3243. intel_finish_fb(crtc->primary->fb);
  3244. mutex_unlock(&dev->struct_mutex);
  3245. }
  3246. }
  3247. /* Program iCLKIP clock to the desired frequency */
  3248. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3249. {
  3250. struct drm_device *dev = crtc->dev;
  3251. struct drm_i915_private *dev_priv = dev->dev_private;
  3252. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3253. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3254. u32 temp;
  3255. mutex_lock(&dev_priv->dpio_lock);
  3256. /* It is necessary to ungate the pixclk gate prior to programming
  3257. * the divisors, and gate it back when it is done.
  3258. */
  3259. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3260. /* Disable SSCCTL */
  3261. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3262. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3263. SBI_SSCCTL_DISABLE,
  3264. SBI_ICLK);
  3265. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3266. if (clock == 20000) {
  3267. auxdiv = 1;
  3268. divsel = 0x41;
  3269. phaseinc = 0x20;
  3270. } else {
  3271. /* The iCLK virtual clock root frequency is in MHz,
  3272. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3273. * divisors, it is necessary to divide one by another, so we
  3274. * convert the virtual clock precision to KHz here for higher
  3275. * precision.
  3276. */
  3277. u32 iclk_virtual_root_freq = 172800 * 1000;
  3278. u32 iclk_pi_range = 64;
  3279. u32 desired_divisor, msb_divisor_value, pi_value;
  3280. desired_divisor = (iclk_virtual_root_freq / clock);
  3281. msb_divisor_value = desired_divisor / iclk_pi_range;
  3282. pi_value = desired_divisor % iclk_pi_range;
  3283. auxdiv = 0;
  3284. divsel = msb_divisor_value - 2;
  3285. phaseinc = pi_value;
  3286. }
  3287. /* This should not happen with any sane values */
  3288. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3289. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3290. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3291. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3292. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3293. clock,
  3294. auxdiv,
  3295. divsel,
  3296. phasedir,
  3297. phaseinc);
  3298. /* Program SSCDIVINTPHASE6 */
  3299. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3300. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3301. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3302. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3303. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3304. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3305. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3306. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3307. /* Program SSCAUXDIV */
  3308. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3309. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3310. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3311. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3312. /* Enable modulator and associated divider */
  3313. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3314. temp &= ~SBI_SSCCTL_DISABLE;
  3315. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3316. /* Wait for initialization time */
  3317. udelay(24);
  3318. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3319. mutex_unlock(&dev_priv->dpio_lock);
  3320. }
  3321. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3322. enum pipe pch_transcoder)
  3323. {
  3324. struct drm_device *dev = crtc->base.dev;
  3325. struct drm_i915_private *dev_priv = dev->dev_private;
  3326. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3327. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3328. I915_READ(HTOTAL(cpu_transcoder)));
  3329. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3330. I915_READ(HBLANK(cpu_transcoder)));
  3331. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3332. I915_READ(HSYNC(cpu_transcoder)));
  3333. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3334. I915_READ(VTOTAL(cpu_transcoder)));
  3335. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3336. I915_READ(VBLANK(cpu_transcoder)));
  3337. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3338. I915_READ(VSYNC(cpu_transcoder)));
  3339. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3340. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3341. }
  3342. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3343. {
  3344. struct drm_i915_private *dev_priv = dev->dev_private;
  3345. uint32_t temp;
  3346. temp = I915_READ(SOUTH_CHICKEN1);
  3347. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3348. return;
  3349. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3350. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3351. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3352. if (enable)
  3353. temp |= FDI_BC_BIFURCATION_SELECT;
  3354. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3355. I915_WRITE(SOUTH_CHICKEN1, temp);
  3356. POSTING_READ(SOUTH_CHICKEN1);
  3357. }
  3358. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3359. {
  3360. struct drm_device *dev = intel_crtc->base.dev;
  3361. switch (intel_crtc->pipe) {
  3362. case PIPE_A:
  3363. break;
  3364. case PIPE_B:
  3365. if (intel_crtc->config->fdi_lanes > 2)
  3366. cpt_set_fdi_bc_bifurcation(dev, false);
  3367. else
  3368. cpt_set_fdi_bc_bifurcation(dev, true);
  3369. break;
  3370. case PIPE_C:
  3371. cpt_set_fdi_bc_bifurcation(dev, true);
  3372. break;
  3373. default:
  3374. BUG();
  3375. }
  3376. }
  3377. /*
  3378. * Enable PCH resources required for PCH ports:
  3379. * - PCH PLLs
  3380. * - FDI training & RX/TX
  3381. * - update transcoder timings
  3382. * - DP transcoding bits
  3383. * - transcoder
  3384. */
  3385. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3386. {
  3387. struct drm_device *dev = crtc->dev;
  3388. struct drm_i915_private *dev_priv = dev->dev_private;
  3389. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3390. int pipe = intel_crtc->pipe;
  3391. u32 reg, temp;
  3392. assert_pch_transcoder_disabled(dev_priv, pipe);
  3393. if (IS_IVYBRIDGE(dev))
  3394. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3395. /* Write the TU size bits before fdi link training, so that error
  3396. * detection works. */
  3397. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3398. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3399. /* For PCH output, training FDI link */
  3400. dev_priv->display.fdi_link_train(crtc);
  3401. /* We need to program the right clock selection before writing the pixel
  3402. * mutliplier into the DPLL. */
  3403. if (HAS_PCH_CPT(dev)) {
  3404. u32 sel;
  3405. temp = I915_READ(PCH_DPLL_SEL);
  3406. temp |= TRANS_DPLL_ENABLE(pipe);
  3407. sel = TRANS_DPLLB_SEL(pipe);
  3408. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3409. temp |= sel;
  3410. else
  3411. temp &= ~sel;
  3412. I915_WRITE(PCH_DPLL_SEL, temp);
  3413. }
  3414. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3415. * transcoder, and we actually should do this to not upset any PCH
  3416. * transcoder that already use the clock when we share it.
  3417. *
  3418. * Note that enable_shared_dpll tries to do the right thing, but
  3419. * get_shared_dpll unconditionally resets the pll - we need that to have
  3420. * the right LVDS enable sequence. */
  3421. intel_enable_shared_dpll(intel_crtc);
  3422. /* set transcoder timing, panel must allow it */
  3423. assert_panel_unlocked(dev_priv, pipe);
  3424. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3425. intel_fdi_normal_train(crtc);
  3426. /* For PCH DP, enable TRANS_DP_CTL */
  3427. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3428. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3429. reg = TRANS_DP_CTL(pipe);
  3430. temp = I915_READ(reg);
  3431. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3432. TRANS_DP_SYNC_MASK |
  3433. TRANS_DP_BPC_MASK);
  3434. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3435. TRANS_DP_ENH_FRAMING);
  3436. temp |= bpc << 9; /* same format but at 11:9 */
  3437. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3438. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3439. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3440. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3441. switch (intel_trans_dp_port_sel(crtc)) {
  3442. case PCH_DP_B:
  3443. temp |= TRANS_DP_PORT_SEL_B;
  3444. break;
  3445. case PCH_DP_C:
  3446. temp |= TRANS_DP_PORT_SEL_C;
  3447. break;
  3448. case PCH_DP_D:
  3449. temp |= TRANS_DP_PORT_SEL_D;
  3450. break;
  3451. default:
  3452. BUG();
  3453. }
  3454. I915_WRITE(reg, temp);
  3455. }
  3456. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3457. }
  3458. static void lpt_pch_enable(struct drm_crtc *crtc)
  3459. {
  3460. struct drm_device *dev = crtc->dev;
  3461. struct drm_i915_private *dev_priv = dev->dev_private;
  3462. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3463. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3464. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3465. lpt_program_iclkip(crtc);
  3466. /* Set transcoder timing. */
  3467. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3468. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3469. }
  3470. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3471. {
  3472. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3473. if (pll == NULL)
  3474. return;
  3475. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3476. WARN(1, "bad %s crtc mask\n", pll->name);
  3477. return;
  3478. }
  3479. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3480. if (pll->config.crtc_mask == 0) {
  3481. WARN_ON(pll->on);
  3482. WARN_ON(pll->active);
  3483. }
  3484. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3485. }
  3486. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3487. struct intel_crtc_state *crtc_state)
  3488. {
  3489. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3490. struct intel_shared_dpll *pll;
  3491. enum intel_dpll_id i;
  3492. if (HAS_PCH_IBX(dev_priv->dev)) {
  3493. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3494. i = (enum intel_dpll_id) crtc->pipe;
  3495. pll = &dev_priv->shared_dplls[i];
  3496. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3497. crtc->base.base.id, pll->name);
  3498. WARN_ON(pll->new_config->crtc_mask);
  3499. goto found;
  3500. }
  3501. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3502. pll = &dev_priv->shared_dplls[i];
  3503. /* Only want to check enabled timings first */
  3504. if (pll->new_config->crtc_mask == 0)
  3505. continue;
  3506. if (memcmp(&crtc_state->dpll_hw_state,
  3507. &pll->new_config->hw_state,
  3508. sizeof(pll->new_config->hw_state)) == 0) {
  3509. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3510. crtc->base.base.id, pll->name,
  3511. pll->new_config->crtc_mask,
  3512. pll->active);
  3513. goto found;
  3514. }
  3515. }
  3516. /* Ok no matching timings, maybe there's a free one? */
  3517. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3518. pll = &dev_priv->shared_dplls[i];
  3519. if (pll->new_config->crtc_mask == 0) {
  3520. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3521. crtc->base.base.id, pll->name);
  3522. goto found;
  3523. }
  3524. }
  3525. return NULL;
  3526. found:
  3527. if (pll->new_config->crtc_mask == 0)
  3528. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3529. crtc_state->shared_dpll = i;
  3530. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3531. pipe_name(crtc->pipe));
  3532. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3533. return pll;
  3534. }
  3535. /**
  3536. * intel_shared_dpll_start_config - start a new PLL staged config
  3537. * @dev_priv: DRM device
  3538. * @clear_pipes: mask of pipes that will have their PLLs freed
  3539. *
  3540. * Starts a new PLL staged config, copying the current config but
  3541. * releasing the references of pipes specified in clear_pipes.
  3542. */
  3543. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3544. unsigned clear_pipes)
  3545. {
  3546. struct intel_shared_dpll *pll;
  3547. enum intel_dpll_id i;
  3548. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3549. pll = &dev_priv->shared_dplls[i];
  3550. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3551. GFP_KERNEL);
  3552. if (!pll->new_config)
  3553. goto cleanup;
  3554. pll->new_config->crtc_mask &= ~clear_pipes;
  3555. }
  3556. return 0;
  3557. cleanup:
  3558. while (--i >= 0) {
  3559. pll = &dev_priv->shared_dplls[i];
  3560. kfree(pll->new_config);
  3561. pll->new_config = NULL;
  3562. }
  3563. return -ENOMEM;
  3564. }
  3565. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3566. {
  3567. struct intel_shared_dpll *pll;
  3568. enum intel_dpll_id i;
  3569. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3570. pll = &dev_priv->shared_dplls[i];
  3571. WARN_ON(pll->new_config == &pll->config);
  3572. pll->config = *pll->new_config;
  3573. kfree(pll->new_config);
  3574. pll->new_config = NULL;
  3575. }
  3576. }
  3577. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3578. {
  3579. struct intel_shared_dpll *pll;
  3580. enum intel_dpll_id i;
  3581. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3582. pll = &dev_priv->shared_dplls[i];
  3583. WARN_ON(pll->new_config == &pll->config);
  3584. kfree(pll->new_config);
  3585. pll->new_config = NULL;
  3586. }
  3587. }
  3588. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3589. {
  3590. struct drm_i915_private *dev_priv = dev->dev_private;
  3591. int dslreg = PIPEDSL(pipe);
  3592. u32 temp;
  3593. temp = I915_READ(dslreg);
  3594. udelay(500);
  3595. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3596. if (wait_for(I915_READ(dslreg) != temp, 5))
  3597. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3598. }
  3599. }
  3600. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3601. {
  3602. struct drm_device *dev = crtc->base.dev;
  3603. struct drm_i915_private *dev_priv = dev->dev_private;
  3604. int pipe = crtc->pipe;
  3605. if (crtc->config->pch_pfit.enabled) {
  3606. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3607. I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3608. I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3609. }
  3610. }
  3611. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3612. {
  3613. struct drm_device *dev = crtc->base.dev;
  3614. struct drm_i915_private *dev_priv = dev->dev_private;
  3615. int pipe = crtc->pipe;
  3616. if (crtc->config->pch_pfit.enabled) {
  3617. /* Force use of hard-coded filter coefficients
  3618. * as some pre-programmed values are broken,
  3619. * e.g. x201.
  3620. */
  3621. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3622. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3623. PF_PIPE_SEL_IVB(pipe));
  3624. else
  3625. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3626. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3627. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3628. }
  3629. }
  3630. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3631. {
  3632. struct drm_device *dev = crtc->dev;
  3633. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3634. struct drm_plane *plane;
  3635. struct intel_plane *intel_plane;
  3636. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3637. intel_plane = to_intel_plane(plane);
  3638. if (intel_plane->pipe == pipe)
  3639. intel_plane_restore(&intel_plane->base);
  3640. }
  3641. }
  3642. /*
  3643. * Disable a plane internally without actually modifying the plane's state.
  3644. * This will allow us to easily restore the plane later by just reprogramming
  3645. * its state.
  3646. */
  3647. static void disable_plane_internal(struct drm_plane *plane)
  3648. {
  3649. struct intel_plane *intel_plane = to_intel_plane(plane);
  3650. struct drm_plane_state *state =
  3651. plane->funcs->atomic_duplicate_state(plane);
  3652. struct intel_plane_state *intel_state = to_intel_plane_state(state);
  3653. intel_state->visible = false;
  3654. intel_plane->commit_plane(plane, intel_state);
  3655. intel_plane_destroy_state(plane, state);
  3656. }
  3657. static void intel_disable_sprite_planes(struct drm_crtc *crtc)
  3658. {
  3659. struct drm_device *dev = crtc->dev;
  3660. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3661. struct drm_plane *plane;
  3662. struct intel_plane *intel_plane;
  3663. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3664. intel_plane = to_intel_plane(plane);
  3665. if (plane->fb && intel_plane->pipe == pipe)
  3666. disable_plane_internal(plane);
  3667. }
  3668. }
  3669. void hsw_enable_ips(struct intel_crtc *crtc)
  3670. {
  3671. struct drm_device *dev = crtc->base.dev;
  3672. struct drm_i915_private *dev_priv = dev->dev_private;
  3673. if (!crtc->config->ips_enabled)
  3674. return;
  3675. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3676. intel_wait_for_vblank(dev, crtc->pipe);
  3677. assert_plane_enabled(dev_priv, crtc->plane);
  3678. if (IS_BROADWELL(dev)) {
  3679. mutex_lock(&dev_priv->rps.hw_lock);
  3680. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3681. mutex_unlock(&dev_priv->rps.hw_lock);
  3682. /* Quoting Art Runyan: "its not safe to expect any particular
  3683. * value in IPS_CTL bit 31 after enabling IPS through the
  3684. * mailbox." Moreover, the mailbox may return a bogus state,
  3685. * so we need to just enable it and continue on.
  3686. */
  3687. } else {
  3688. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3689. /* The bit only becomes 1 in the next vblank, so this wait here
  3690. * is essentially intel_wait_for_vblank. If we don't have this
  3691. * and don't wait for vblanks until the end of crtc_enable, then
  3692. * the HW state readout code will complain that the expected
  3693. * IPS_CTL value is not the one we read. */
  3694. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3695. DRM_ERROR("Timed out waiting for IPS enable\n");
  3696. }
  3697. }
  3698. void hsw_disable_ips(struct intel_crtc *crtc)
  3699. {
  3700. struct drm_device *dev = crtc->base.dev;
  3701. struct drm_i915_private *dev_priv = dev->dev_private;
  3702. if (!crtc->config->ips_enabled)
  3703. return;
  3704. assert_plane_enabled(dev_priv, crtc->plane);
  3705. if (IS_BROADWELL(dev)) {
  3706. mutex_lock(&dev_priv->rps.hw_lock);
  3707. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3708. mutex_unlock(&dev_priv->rps.hw_lock);
  3709. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3710. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3711. DRM_ERROR("Timed out waiting for IPS disable\n");
  3712. } else {
  3713. I915_WRITE(IPS_CTL, 0);
  3714. POSTING_READ(IPS_CTL);
  3715. }
  3716. /* We need to wait for a vblank before we can disable the plane. */
  3717. intel_wait_for_vblank(dev, crtc->pipe);
  3718. }
  3719. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3720. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3721. {
  3722. struct drm_device *dev = crtc->dev;
  3723. struct drm_i915_private *dev_priv = dev->dev_private;
  3724. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3725. enum pipe pipe = intel_crtc->pipe;
  3726. int palreg = PALETTE(pipe);
  3727. int i;
  3728. bool reenable_ips = false;
  3729. /* The clocks have to be on to load the palette. */
  3730. if (!crtc->state->enable || !intel_crtc->active)
  3731. return;
  3732. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3733. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3734. assert_dsi_pll_enabled(dev_priv);
  3735. else
  3736. assert_pll_enabled(dev_priv, pipe);
  3737. }
  3738. /* use legacy palette for Ironlake */
  3739. if (!HAS_GMCH_DISPLAY(dev))
  3740. palreg = LGC_PALETTE(pipe);
  3741. /* Workaround : Do not read or write the pipe palette/gamma data while
  3742. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3743. */
  3744. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3745. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3746. GAMMA_MODE_MODE_SPLIT)) {
  3747. hsw_disable_ips(intel_crtc);
  3748. reenable_ips = true;
  3749. }
  3750. for (i = 0; i < 256; i++) {
  3751. I915_WRITE(palreg + 4 * i,
  3752. (intel_crtc->lut_r[i] << 16) |
  3753. (intel_crtc->lut_g[i] << 8) |
  3754. intel_crtc->lut_b[i]);
  3755. }
  3756. if (reenable_ips)
  3757. hsw_enable_ips(intel_crtc);
  3758. }
  3759. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3760. {
  3761. if (!enable && intel_crtc->overlay) {
  3762. struct drm_device *dev = intel_crtc->base.dev;
  3763. struct drm_i915_private *dev_priv = dev->dev_private;
  3764. mutex_lock(&dev->struct_mutex);
  3765. dev_priv->mm.interruptible = false;
  3766. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3767. dev_priv->mm.interruptible = true;
  3768. mutex_unlock(&dev->struct_mutex);
  3769. }
  3770. /* Let userspace switch the overlay on again. In most cases userspace
  3771. * has to recompute where to put it anyway.
  3772. */
  3773. }
  3774. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3775. {
  3776. struct drm_device *dev = crtc->dev;
  3777. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3778. int pipe = intel_crtc->pipe;
  3779. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3780. intel_enable_sprite_planes(crtc);
  3781. intel_crtc_update_cursor(crtc, true);
  3782. intel_crtc_dpms_overlay(intel_crtc, true);
  3783. hsw_enable_ips(intel_crtc);
  3784. mutex_lock(&dev->struct_mutex);
  3785. intel_fbc_update(dev);
  3786. mutex_unlock(&dev->struct_mutex);
  3787. /*
  3788. * FIXME: Once we grow proper nuclear flip support out of this we need
  3789. * to compute the mask of flip planes precisely. For the time being
  3790. * consider this a flip from a NULL plane.
  3791. */
  3792. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3793. }
  3794. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3795. {
  3796. struct drm_device *dev = crtc->dev;
  3797. struct drm_i915_private *dev_priv = dev->dev_private;
  3798. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3799. int pipe = intel_crtc->pipe;
  3800. intel_crtc_wait_for_pending_flips(crtc);
  3801. if (dev_priv->fbc.crtc == intel_crtc)
  3802. intel_fbc_disable(dev);
  3803. hsw_disable_ips(intel_crtc);
  3804. intel_crtc_dpms_overlay(intel_crtc, false);
  3805. intel_crtc_update_cursor(crtc, false);
  3806. intel_disable_sprite_planes(crtc);
  3807. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3808. /*
  3809. * FIXME: Once we grow proper nuclear flip support out of this we need
  3810. * to compute the mask of flip planes precisely. For the time being
  3811. * consider this a flip to a NULL plane.
  3812. */
  3813. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3814. }
  3815. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3816. {
  3817. struct drm_device *dev = crtc->dev;
  3818. struct drm_i915_private *dev_priv = dev->dev_private;
  3819. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3820. struct intel_encoder *encoder;
  3821. int pipe = intel_crtc->pipe;
  3822. WARN_ON(!crtc->state->enable);
  3823. if (intel_crtc->active)
  3824. return;
  3825. if (intel_crtc->config->has_pch_encoder)
  3826. intel_prepare_shared_dpll(intel_crtc);
  3827. if (intel_crtc->config->has_dp_encoder)
  3828. intel_dp_set_m_n(intel_crtc, M1_N1);
  3829. intel_set_pipe_timings(intel_crtc);
  3830. if (intel_crtc->config->has_pch_encoder) {
  3831. intel_cpu_transcoder_set_m_n(intel_crtc,
  3832. &intel_crtc->config->fdi_m_n, NULL);
  3833. }
  3834. ironlake_set_pipeconf(crtc);
  3835. intel_crtc->active = true;
  3836. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3837. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3838. for_each_encoder_on_crtc(dev, crtc, encoder)
  3839. if (encoder->pre_enable)
  3840. encoder->pre_enable(encoder);
  3841. if (intel_crtc->config->has_pch_encoder) {
  3842. /* Note: FDI PLL enabling _must_ be done before we enable the
  3843. * cpu pipes, hence this is separate from all the other fdi/pch
  3844. * enabling. */
  3845. ironlake_fdi_pll_enable(intel_crtc);
  3846. } else {
  3847. assert_fdi_tx_disabled(dev_priv, pipe);
  3848. assert_fdi_rx_disabled(dev_priv, pipe);
  3849. }
  3850. ironlake_pfit_enable(intel_crtc);
  3851. /*
  3852. * On ILK+ LUT must be loaded before the pipe is running but with
  3853. * clocks enabled
  3854. */
  3855. intel_crtc_load_lut(crtc);
  3856. intel_update_watermarks(crtc);
  3857. intel_enable_pipe(intel_crtc);
  3858. if (intel_crtc->config->has_pch_encoder)
  3859. ironlake_pch_enable(crtc);
  3860. assert_vblank_disabled(crtc);
  3861. drm_crtc_vblank_on(crtc);
  3862. for_each_encoder_on_crtc(dev, crtc, encoder)
  3863. encoder->enable(encoder);
  3864. if (HAS_PCH_CPT(dev))
  3865. cpt_verify_modeset(dev, intel_crtc->pipe);
  3866. intel_crtc_enable_planes(crtc);
  3867. }
  3868. /* IPS only exists on ULT machines and is tied to pipe A. */
  3869. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3870. {
  3871. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3872. }
  3873. /*
  3874. * This implements the workaround described in the "notes" section of the mode
  3875. * set sequence documentation. When going from no pipes or single pipe to
  3876. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3877. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3878. */
  3879. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3880. {
  3881. struct drm_device *dev = crtc->base.dev;
  3882. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3883. /* We want to get the other_active_crtc only if there's only 1 other
  3884. * active crtc. */
  3885. for_each_intel_crtc(dev, crtc_it) {
  3886. if (!crtc_it->active || crtc_it == crtc)
  3887. continue;
  3888. if (other_active_crtc)
  3889. return;
  3890. other_active_crtc = crtc_it;
  3891. }
  3892. if (!other_active_crtc)
  3893. return;
  3894. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3895. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3896. }
  3897. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3898. {
  3899. struct drm_device *dev = crtc->dev;
  3900. struct drm_i915_private *dev_priv = dev->dev_private;
  3901. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3902. struct intel_encoder *encoder;
  3903. int pipe = intel_crtc->pipe;
  3904. WARN_ON(!crtc->state->enable);
  3905. if (intel_crtc->active)
  3906. return;
  3907. if (intel_crtc_to_shared_dpll(intel_crtc))
  3908. intel_enable_shared_dpll(intel_crtc);
  3909. if (intel_crtc->config->has_dp_encoder)
  3910. intel_dp_set_m_n(intel_crtc, M1_N1);
  3911. intel_set_pipe_timings(intel_crtc);
  3912. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  3913. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  3914. intel_crtc->config->pixel_multiplier - 1);
  3915. }
  3916. if (intel_crtc->config->has_pch_encoder) {
  3917. intel_cpu_transcoder_set_m_n(intel_crtc,
  3918. &intel_crtc->config->fdi_m_n, NULL);
  3919. }
  3920. haswell_set_pipeconf(crtc);
  3921. intel_set_pipe_csc(crtc);
  3922. intel_crtc->active = true;
  3923. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3924. for_each_encoder_on_crtc(dev, crtc, encoder)
  3925. if (encoder->pre_enable)
  3926. encoder->pre_enable(encoder);
  3927. if (intel_crtc->config->has_pch_encoder) {
  3928. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3929. true);
  3930. dev_priv->display.fdi_link_train(crtc);
  3931. }
  3932. intel_ddi_enable_pipe_clock(intel_crtc);
  3933. if (IS_SKYLAKE(dev))
  3934. skylake_pfit_enable(intel_crtc);
  3935. else
  3936. ironlake_pfit_enable(intel_crtc);
  3937. /*
  3938. * On ILK+ LUT must be loaded before the pipe is running but with
  3939. * clocks enabled
  3940. */
  3941. intel_crtc_load_lut(crtc);
  3942. intel_ddi_set_pipe_settings(crtc);
  3943. intel_ddi_enable_transcoder_func(crtc);
  3944. intel_update_watermarks(crtc);
  3945. intel_enable_pipe(intel_crtc);
  3946. if (intel_crtc->config->has_pch_encoder)
  3947. lpt_pch_enable(crtc);
  3948. if (intel_crtc->config->dp_encoder_is_mst)
  3949. intel_ddi_set_vc_payload_alloc(crtc, true);
  3950. assert_vblank_disabled(crtc);
  3951. drm_crtc_vblank_on(crtc);
  3952. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3953. encoder->enable(encoder);
  3954. intel_opregion_notify_encoder(encoder, true);
  3955. }
  3956. /* If we change the relative order between pipe/planes enabling, we need
  3957. * to change the workaround. */
  3958. haswell_mode_set_planes_workaround(intel_crtc);
  3959. intel_crtc_enable_planes(crtc);
  3960. }
  3961. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3962. {
  3963. struct drm_device *dev = crtc->base.dev;
  3964. struct drm_i915_private *dev_priv = dev->dev_private;
  3965. int pipe = crtc->pipe;
  3966. /* To avoid upsetting the power well on haswell only disable the pfit if
  3967. * it's in use. The hw state code will make sure we get this right. */
  3968. if (crtc->config->pch_pfit.enabled) {
  3969. I915_WRITE(PS_CTL(pipe), 0);
  3970. I915_WRITE(PS_WIN_POS(pipe), 0);
  3971. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3972. }
  3973. }
  3974. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3975. {
  3976. struct drm_device *dev = crtc->base.dev;
  3977. struct drm_i915_private *dev_priv = dev->dev_private;
  3978. int pipe = crtc->pipe;
  3979. /* To avoid upsetting the power well on haswell only disable the pfit if
  3980. * it's in use. The hw state code will make sure we get this right. */
  3981. if (crtc->config->pch_pfit.enabled) {
  3982. I915_WRITE(PF_CTL(pipe), 0);
  3983. I915_WRITE(PF_WIN_POS(pipe), 0);
  3984. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3985. }
  3986. }
  3987. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3988. {
  3989. struct drm_device *dev = crtc->dev;
  3990. struct drm_i915_private *dev_priv = dev->dev_private;
  3991. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3992. struct intel_encoder *encoder;
  3993. int pipe = intel_crtc->pipe;
  3994. u32 reg, temp;
  3995. if (!intel_crtc->active)
  3996. return;
  3997. intel_crtc_disable_planes(crtc);
  3998. for_each_encoder_on_crtc(dev, crtc, encoder)
  3999. encoder->disable(encoder);
  4000. drm_crtc_vblank_off(crtc);
  4001. assert_vblank_disabled(crtc);
  4002. if (intel_crtc->config->has_pch_encoder)
  4003. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4004. intel_disable_pipe(intel_crtc);
  4005. ironlake_pfit_disable(intel_crtc);
  4006. for_each_encoder_on_crtc(dev, crtc, encoder)
  4007. if (encoder->post_disable)
  4008. encoder->post_disable(encoder);
  4009. if (intel_crtc->config->has_pch_encoder) {
  4010. ironlake_fdi_disable(crtc);
  4011. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4012. if (HAS_PCH_CPT(dev)) {
  4013. /* disable TRANS_DP_CTL */
  4014. reg = TRANS_DP_CTL(pipe);
  4015. temp = I915_READ(reg);
  4016. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4017. TRANS_DP_PORT_SEL_MASK);
  4018. temp |= TRANS_DP_PORT_SEL_NONE;
  4019. I915_WRITE(reg, temp);
  4020. /* disable DPLL_SEL */
  4021. temp = I915_READ(PCH_DPLL_SEL);
  4022. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4023. I915_WRITE(PCH_DPLL_SEL, temp);
  4024. }
  4025. /* disable PCH DPLL */
  4026. intel_disable_shared_dpll(intel_crtc);
  4027. ironlake_fdi_pll_disable(intel_crtc);
  4028. }
  4029. intel_crtc->active = false;
  4030. intel_update_watermarks(crtc);
  4031. mutex_lock(&dev->struct_mutex);
  4032. intel_fbc_update(dev);
  4033. mutex_unlock(&dev->struct_mutex);
  4034. }
  4035. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4036. {
  4037. struct drm_device *dev = crtc->dev;
  4038. struct drm_i915_private *dev_priv = dev->dev_private;
  4039. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4040. struct intel_encoder *encoder;
  4041. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4042. if (!intel_crtc->active)
  4043. return;
  4044. intel_crtc_disable_planes(crtc);
  4045. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4046. intel_opregion_notify_encoder(encoder, false);
  4047. encoder->disable(encoder);
  4048. }
  4049. drm_crtc_vblank_off(crtc);
  4050. assert_vblank_disabled(crtc);
  4051. if (intel_crtc->config->has_pch_encoder)
  4052. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4053. false);
  4054. intel_disable_pipe(intel_crtc);
  4055. if (intel_crtc->config->dp_encoder_is_mst)
  4056. intel_ddi_set_vc_payload_alloc(crtc, false);
  4057. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4058. if (IS_SKYLAKE(dev))
  4059. skylake_pfit_disable(intel_crtc);
  4060. else
  4061. ironlake_pfit_disable(intel_crtc);
  4062. intel_ddi_disable_pipe_clock(intel_crtc);
  4063. if (intel_crtc->config->has_pch_encoder) {
  4064. lpt_disable_pch_transcoder(dev_priv);
  4065. intel_ddi_fdi_disable(crtc);
  4066. }
  4067. for_each_encoder_on_crtc(dev, crtc, encoder)
  4068. if (encoder->post_disable)
  4069. encoder->post_disable(encoder);
  4070. intel_crtc->active = false;
  4071. intel_update_watermarks(crtc);
  4072. mutex_lock(&dev->struct_mutex);
  4073. intel_fbc_update(dev);
  4074. mutex_unlock(&dev->struct_mutex);
  4075. if (intel_crtc_to_shared_dpll(intel_crtc))
  4076. intel_disable_shared_dpll(intel_crtc);
  4077. }
  4078. static void ironlake_crtc_off(struct drm_crtc *crtc)
  4079. {
  4080. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4081. intel_put_shared_dpll(intel_crtc);
  4082. }
  4083. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4084. {
  4085. struct drm_device *dev = crtc->base.dev;
  4086. struct drm_i915_private *dev_priv = dev->dev_private;
  4087. struct intel_crtc_state *pipe_config = crtc->config;
  4088. if (!pipe_config->gmch_pfit.control)
  4089. return;
  4090. /*
  4091. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4092. * according to register description and PRM.
  4093. */
  4094. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4095. assert_pipe_disabled(dev_priv, crtc->pipe);
  4096. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4097. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4098. /* Border color in case we don't scale up to the full screen. Black by
  4099. * default, change to something else for debugging. */
  4100. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4101. }
  4102. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4103. {
  4104. switch (port) {
  4105. case PORT_A:
  4106. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4107. case PORT_B:
  4108. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4109. case PORT_C:
  4110. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4111. case PORT_D:
  4112. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4113. default:
  4114. WARN_ON_ONCE(1);
  4115. return POWER_DOMAIN_PORT_OTHER;
  4116. }
  4117. }
  4118. #define for_each_power_domain(domain, mask) \
  4119. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4120. if ((1 << (domain)) & (mask))
  4121. enum intel_display_power_domain
  4122. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4123. {
  4124. struct drm_device *dev = intel_encoder->base.dev;
  4125. struct intel_digital_port *intel_dig_port;
  4126. switch (intel_encoder->type) {
  4127. case INTEL_OUTPUT_UNKNOWN:
  4128. /* Only DDI platforms should ever use this output type */
  4129. WARN_ON_ONCE(!HAS_DDI(dev));
  4130. case INTEL_OUTPUT_DISPLAYPORT:
  4131. case INTEL_OUTPUT_HDMI:
  4132. case INTEL_OUTPUT_EDP:
  4133. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4134. return port_to_power_domain(intel_dig_port->port);
  4135. case INTEL_OUTPUT_DP_MST:
  4136. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4137. return port_to_power_domain(intel_dig_port->port);
  4138. case INTEL_OUTPUT_ANALOG:
  4139. return POWER_DOMAIN_PORT_CRT;
  4140. case INTEL_OUTPUT_DSI:
  4141. return POWER_DOMAIN_PORT_DSI;
  4142. default:
  4143. return POWER_DOMAIN_PORT_OTHER;
  4144. }
  4145. }
  4146. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4147. {
  4148. struct drm_device *dev = crtc->dev;
  4149. struct intel_encoder *intel_encoder;
  4150. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4151. enum pipe pipe = intel_crtc->pipe;
  4152. unsigned long mask;
  4153. enum transcoder transcoder;
  4154. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4155. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4156. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4157. if (intel_crtc->config->pch_pfit.enabled ||
  4158. intel_crtc->config->pch_pfit.force_thru)
  4159. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4160. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4161. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4162. return mask;
  4163. }
  4164. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  4165. {
  4166. struct drm_i915_private *dev_priv = dev->dev_private;
  4167. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4168. struct intel_crtc *crtc;
  4169. /*
  4170. * First get all needed power domains, then put all unneeded, to avoid
  4171. * any unnecessary toggling of the power wells.
  4172. */
  4173. for_each_intel_crtc(dev, crtc) {
  4174. enum intel_display_power_domain domain;
  4175. if (!crtc->base.state->enable)
  4176. continue;
  4177. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4178. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4179. intel_display_power_get(dev_priv, domain);
  4180. }
  4181. if (dev_priv->display.modeset_global_resources)
  4182. dev_priv->display.modeset_global_resources(dev);
  4183. for_each_intel_crtc(dev, crtc) {
  4184. enum intel_display_power_domain domain;
  4185. for_each_power_domain(domain, crtc->enabled_power_domains)
  4186. intel_display_power_put(dev_priv, domain);
  4187. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4188. }
  4189. intel_display_set_init_power(dev_priv, false);
  4190. }
  4191. /* returns HPLL frequency in kHz */
  4192. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4193. {
  4194. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4195. /* Obtain SKU information */
  4196. mutex_lock(&dev_priv->dpio_lock);
  4197. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4198. CCK_FUSE_HPLL_FREQ_MASK;
  4199. mutex_unlock(&dev_priv->dpio_lock);
  4200. return vco_freq[hpll_freq] * 1000;
  4201. }
  4202. static void vlv_update_cdclk(struct drm_device *dev)
  4203. {
  4204. struct drm_i915_private *dev_priv = dev->dev_private;
  4205. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4206. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4207. dev_priv->vlv_cdclk_freq);
  4208. /*
  4209. * Program the gmbus_freq based on the cdclk frequency.
  4210. * BSpec erroneously claims we should aim for 4MHz, but
  4211. * in fact 1MHz is the correct frequency.
  4212. */
  4213. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4214. }
  4215. /* Adjust CDclk dividers to allow high res or save power if possible */
  4216. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4217. {
  4218. struct drm_i915_private *dev_priv = dev->dev_private;
  4219. u32 val, cmd;
  4220. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4221. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4222. cmd = 2;
  4223. else if (cdclk == 266667)
  4224. cmd = 1;
  4225. else
  4226. cmd = 0;
  4227. mutex_lock(&dev_priv->rps.hw_lock);
  4228. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4229. val &= ~DSPFREQGUAR_MASK;
  4230. val |= (cmd << DSPFREQGUAR_SHIFT);
  4231. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4232. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4233. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4234. 50)) {
  4235. DRM_ERROR("timed out waiting for CDclk change\n");
  4236. }
  4237. mutex_unlock(&dev_priv->rps.hw_lock);
  4238. if (cdclk == 400000) {
  4239. u32 divider;
  4240. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4241. mutex_lock(&dev_priv->dpio_lock);
  4242. /* adjust cdclk divider */
  4243. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4244. val &= ~DISPLAY_FREQUENCY_VALUES;
  4245. val |= divider;
  4246. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4247. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4248. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4249. 50))
  4250. DRM_ERROR("timed out waiting for CDclk change\n");
  4251. mutex_unlock(&dev_priv->dpio_lock);
  4252. }
  4253. mutex_lock(&dev_priv->dpio_lock);
  4254. /* adjust self-refresh exit latency value */
  4255. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4256. val &= ~0x7f;
  4257. /*
  4258. * For high bandwidth configs, we set a higher latency in the bunit
  4259. * so that the core display fetch happens in time to avoid underruns.
  4260. */
  4261. if (cdclk == 400000)
  4262. val |= 4500 / 250; /* 4.5 usec */
  4263. else
  4264. val |= 3000 / 250; /* 3.0 usec */
  4265. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4266. mutex_unlock(&dev_priv->dpio_lock);
  4267. vlv_update_cdclk(dev);
  4268. }
  4269. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4270. {
  4271. struct drm_i915_private *dev_priv = dev->dev_private;
  4272. u32 val, cmd;
  4273. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4274. switch (cdclk) {
  4275. case 333333:
  4276. case 320000:
  4277. case 266667:
  4278. case 200000:
  4279. break;
  4280. default:
  4281. MISSING_CASE(cdclk);
  4282. return;
  4283. }
  4284. /*
  4285. * Specs are full of misinformation, but testing on actual
  4286. * hardware has shown that we just need to write the desired
  4287. * CCK divider into the Punit register.
  4288. */
  4289. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4290. mutex_lock(&dev_priv->rps.hw_lock);
  4291. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4292. val &= ~DSPFREQGUAR_MASK_CHV;
  4293. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4294. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4295. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4296. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4297. 50)) {
  4298. DRM_ERROR("timed out waiting for CDclk change\n");
  4299. }
  4300. mutex_unlock(&dev_priv->rps.hw_lock);
  4301. vlv_update_cdclk(dev);
  4302. }
  4303. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4304. int max_pixclk)
  4305. {
  4306. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4307. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4308. /*
  4309. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4310. * 200MHz
  4311. * 267MHz
  4312. * 320/333MHz (depends on HPLL freq)
  4313. * 400MHz (VLV only)
  4314. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4315. * of the lower bin and adjust if needed.
  4316. *
  4317. * We seem to get an unstable or solid color picture at 200MHz.
  4318. * Not sure what's wrong. For now use 200MHz only when all pipes
  4319. * are off.
  4320. */
  4321. if (!IS_CHERRYVIEW(dev_priv) &&
  4322. max_pixclk > freq_320*limit/100)
  4323. return 400000;
  4324. else if (max_pixclk > 266667*limit/100)
  4325. return freq_320;
  4326. else if (max_pixclk > 0)
  4327. return 266667;
  4328. else
  4329. return 200000;
  4330. }
  4331. /* compute the max pixel clock for new configuration */
  4332. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4333. {
  4334. struct drm_device *dev = dev_priv->dev;
  4335. struct intel_crtc *intel_crtc;
  4336. int max_pixclk = 0;
  4337. for_each_intel_crtc(dev, intel_crtc) {
  4338. if (intel_crtc->new_enabled)
  4339. max_pixclk = max(max_pixclk,
  4340. intel_crtc->new_config->base.adjusted_mode.crtc_clock);
  4341. }
  4342. return max_pixclk;
  4343. }
  4344. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4345. unsigned *prepare_pipes)
  4346. {
  4347. struct drm_i915_private *dev_priv = dev->dev_private;
  4348. struct intel_crtc *intel_crtc;
  4349. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4350. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4351. dev_priv->vlv_cdclk_freq)
  4352. return;
  4353. /* disable/enable all currently active pipes while we change cdclk */
  4354. for_each_intel_crtc(dev, intel_crtc)
  4355. if (intel_crtc->base.state->enable)
  4356. *prepare_pipes |= (1 << intel_crtc->pipe);
  4357. }
  4358. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  4359. {
  4360. unsigned int credits, default_credits;
  4361. if (IS_CHERRYVIEW(dev_priv))
  4362. default_credits = PFI_CREDIT(12);
  4363. else
  4364. default_credits = PFI_CREDIT(8);
  4365. if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  4366. /* CHV suggested value is 31 or 63 */
  4367. if (IS_CHERRYVIEW(dev_priv))
  4368. credits = PFI_CREDIT_31;
  4369. else
  4370. credits = PFI_CREDIT(15);
  4371. } else {
  4372. credits = default_credits;
  4373. }
  4374. /*
  4375. * WA - write default credits before re-programming
  4376. * FIXME: should we also set the resend bit here?
  4377. */
  4378. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4379. default_credits);
  4380. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4381. credits | PFI_CREDIT_RESEND);
  4382. /*
  4383. * FIXME is this guaranteed to clear
  4384. * immediately or should we poll for it?
  4385. */
  4386. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  4387. }
  4388. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4389. {
  4390. struct drm_i915_private *dev_priv = dev->dev_private;
  4391. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4392. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4393. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4394. /*
  4395. * FIXME: We can end up here with all power domains off, yet
  4396. * with a CDCLK frequency other than the minimum. To account
  4397. * for this take the PIPE-A power domain, which covers the HW
  4398. * blocks needed for the following programming. This can be
  4399. * removed once it's guaranteed that we get here either with
  4400. * the minimum CDCLK set, or the required power domains
  4401. * enabled.
  4402. */
  4403. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4404. if (IS_CHERRYVIEW(dev))
  4405. cherryview_set_cdclk(dev, req_cdclk);
  4406. else
  4407. valleyview_set_cdclk(dev, req_cdclk);
  4408. vlv_program_pfi_credits(dev_priv);
  4409. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4410. }
  4411. }
  4412. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4413. {
  4414. struct drm_device *dev = crtc->dev;
  4415. struct drm_i915_private *dev_priv = to_i915(dev);
  4416. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4417. struct intel_encoder *encoder;
  4418. int pipe = intel_crtc->pipe;
  4419. bool is_dsi;
  4420. WARN_ON(!crtc->state->enable);
  4421. if (intel_crtc->active)
  4422. return;
  4423. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4424. if (!is_dsi) {
  4425. if (IS_CHERRYVIEW(dev))
  4426. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4427. else
  4428. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4429. }
  4430. if (intel_crtc->config->has_dp_encoder)
  4431. intel_dp_set_m_n(intel_crtc, M1_N1);
  4432. intel_set_pipe_timings(intel_crtc);
  4433. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4434. struct drm_i915_private *dev_priv = dev->dev_private;
  4435. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4436. I915_WRITE(CHV_CANVAS(pipe), 0);
  4437. }
  4438. i9xx_set_pipeconf(intel_crtc);
  4439. intel_crtc->active = true;
  4440. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4441. for_each_encoder_on_crtc(dev, crtc, encoder)
  4442. if (encoder->pre_pll_enable)
  4443. encoder->pre_pll_enable(encoder);
  4444. if (!is_dsi) {
  4445. if (IS_CHERRYVIEW(dev))
  4446. chv_enable_pll(intel_crtc, intel_crtc->config);
  4447. else
  4448. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4449. }
  4450. for_each_encoder_on_crtc(dev, crtc, encoder)
  4451. if (encoder->pre_enable)
  4452. encoder->pre_enable(encoder);
  4453. i9xx_pfit_enable(intel_crtc);
  4454. intel_crtc_load_lut(crtc);
  4455. intel_update_watermarks(crtc);
  4456. intel_enable_pipe(intel_crtc);
  4457. assert_vblank_disabled(crtc);
  4458. drm_crtc_vblank_on(crtc);
  4459. for_each_encoder_on_crtc(dev, crtc, encoder)
  4460. encoder->enable(encoder);
  4461. intel_crtc_enable_planes(crtc);
  4462. /* Underruns don't raise interrupts, so check manually. */
  4463. i9xx_check_fifo_underruns(dev_priv);
  4464. }
  4465. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4466. {
  4467. struct drm_device *dev = crtc->base.dev;
  4468. struct drm_i915_private *dev_priv = dev->dev_private;
  4469. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4470. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4471. }
  4472. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4473. {
  4474. struct drm_device *dev = crtc->dev;
  4475. struct drm_i915_private *dev_priv = to_i915(dev);
  4476. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4477. struct intel_encoder *encoder;
  4478. int pipe = intel_crtc->pipe;
  4479. WARN_ON(!crtc->state->enable);
  4480. if (intel_crtc->active)
  4481. return;
  4482. i9xx_set_pll_dividers(intel_crtc);
  4483. if (intel_crtc->config->has_dp_encoder)
  4484. intel_dp_set_m_n(intel_crtc, M1_N1);
  4485. intel_set_pipe_timings(intel_crtc);
  4486. i9xx_set_pipeconf(intel_crtc);
  4487. intel_crtc->active = true;
  4488. if (!IS_GEN2(dev))
  4489. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4490. for_each_encoder_on_crtc(dev, crtc, encoder)
  4491. if (encoder->pre_enable)
  4492. encoder->pre_enable(encoder);
  4493. i9xx_enable_pll(intel_crtc);
  4494. i9xx_pfit_enable(intel_crtc);
  4495. intel_crtc_load_lut(crtc);
  4496. intel_update_watermarks(crtc);
  4497. intel_enable_pipe(intel_crtc);
  4498. assert_vblank_disabled(crtc);
  4499. drm_crtc_vblank_on(crtc);
  4500. for_each_encoder_on_crtc(dev, crtc, encoder)
  4501. encoder->enable(encoder);
  4502. intel_crtc_enable_planes(crtc);
  4503. /*
  4504. * Gen2 reports pipe underruns whenever all planes are disabled.
  4505. * So don't enable underrun reporting before at least some planes
  4506. * are enabled.
  4507. * FIXME: Need to fix the logic to work when we turn off all planes
  4508. * but leave the pipe running.
  4509. */
  4510. if (IS_GEN2(dev))
  4511. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4512. /* Underruns don't raise interrupts, so check manually. */
  4513. i9xx_check_fifo_underruns(dev_priv);
  4514. }
  4515. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4516. {
  4517. struct drm_device *dev = crtc->base.dev;
  4518. struct drm_i915_private *dev_priv = dev->dev_private;
  4519. if (!crtc->config->gmch_pfit.control)
  4520. return;
  4521. assert_pipe_disabled(dev_priv, crtc->pipe);
  4522. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4523. I915_READ(PFIT_CONTROL));
  4524. I915_WRITE(PFIT_CONTROL, 0);
  4525. }
  4526. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4527. {
  4528. struct drm_device *dev = crtc->dev;
  4529. struct drm_i915_private *dev_priv = dev->dev_private;
  4530. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4531. struct intel_encoder *encoder;
  4532. int pipe = intel_crtc->pipe;
  4533. if (!intel_crtc->active)
  4534. return;
  4535. /*
  4536. * Gen2 reports pipe underruns whenever all planes are disabled.
  4537. * So diasble underrun reporting before all the planes get disabled.
  4538. * FIXME: Need to fix the logic to work when we turn off all planes
  4539. * but leave the pipe running.
  4540. */
  4541. if (IS_GEN2(dev))
  4542. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4543. /*
  4544. * Vblank time updates from the shadow to live plane control register
  4545. * are blocked if the memory self-refresh mode is active at that
  4546. * moment. So to make sure the plane gets truly disabled, disable
  4547. * first the self-refresh mode. The self-refresh enable bit in turn
  4548. * will be checked/applied by the HW only at the next frame start
  4549. * event which is after the vblank start event, so we need to have a
  4550. * wait-for-vblank between disabling the plane and the pipe.
  4551. */
  4552. intel_set_memory_cxsr(dev_priv, false);
  4553. intel_crtc_disable_planes(crtc);
  4554. /*
  4555. * On gen2 planes are double buffered but the pipe isn't, so we must
  4556. * wait for planes to fully turn off before disabling the pipe.
  4557. * We also need to wait on all gmch platforms because of the
  4558. * self-refresh mode constraint explained above.
  4559. */
  4560. intel_wait_for_vblank(dev, pipe);
  4561. for_each_encoder_on_crtc(dev, crtc, encoder)
  4562. encoder->disable(encoder);
  4563. drm_crtc_vblank_off(crtc);
  4564. assert_vblank_disabled(crtc);
  4565. intel_disable_pipe(intel_crtc);
  4566. i9xx_pfit_disable(intel_crtc);
  4567. for_each_encoder_on_crtc(dev, crtc, encoder)
  4568. if (encoder->post_disable)
  4569. encoder->post_disable(encoder);
  4570. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4571. if (IS_CHERRYVIEW(dev))
  4572. chv_disable_pll(dev_priv, pipe);
  4573. else if (IS_VALLEYVIEW(dev))
  4574. vlv_disable_pll(dev_priv, pipe);
  4575. else
  4576. i9xx_disable_pll(intel_crtc);
  4577. }
  4578. if (!IS_GEN2(dev))
  4579. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4580. intel_crtc->active = false;
  4581. intel_update_watermarks(crtc);
  4582. mutex_lock(&dev->struct_mutex);
  4583. intel_fbc_update(dev);
  4584. mutex_unlock(&dev->struct_mutex);
  4585. }
  4586. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4587. {
  4588. }
  4589. /* Master function to enable/disable CRTC and corresponding power wells */
  4590. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4591. {
  4592. struct drm_device *dev = crtc->dev;
  4593. struct drm_i915_private *dev_priv = dev->dev_private;
  4594. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4595. enum intel_display_power_domain domain;
  4596. unsigned long domains;
  4597. if (enable) {
  4598. if (!intel_crtc->active) {
  4599. domains = get_crtc_power_domains(crtc);
  4600. for_each_power_domain(domain, domains)
  4601. intel_display_power_get(dev_priv, domain);
  4602. intel_crtc->enabled_power_domains = domains;
  4603. dev_priv->display.crtc_enable(crtc);
  4604. }
  4605. } else {
  4606. if (intel_crtc->active) {
  4607. dev_priv->display.crtc_disable(crtc);
  4608. domains = intel_crtc->enabled_power_domains;
  4609. for_each_power_domain(domain, domains)
  4610. intel_display_power_put(dev_priv, domain);
  4611. intel_crtc->enabled_power_domains = 0;
  4612. }
  4613. }
  4614. }
  4615. /**
  4616. * Sets the power management mode of the pipe and plane.
  4617. */
  4618. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4619. {
  4620. struct drm_device *dev = crtc->dev;
  4621. struct intel_encoder *intel_encoder;
  4622. bool enable = false;
  4623. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4624. enable |= intel_encoder->connectors_active;
  4625. intel_crtc_control(crtc, enable);
  4626. }
  4627. static void intel_crtc_disable(struct drm_crtc *crtc)
  4628. {
  4629. struct drm_device *dev = crtc->dev;
  4630. struct drm_connector *connector;
  4631. struct drm_i915_private *dev_priv = dev->dev_private;
  4632. /* crtc should still be enabled when we disable it. */
  4633. WARN_ON(!crtc->state->enable);
  4634. dev_priv->display.crtc_disable(crtc);
  4635. dev_priv->display.off(crtc);
  4636. crtc->primary->funcs->disable_plane(crtc->primary);
  4637. /* Update computed state. */
  4638. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4639. if (!connector->encoder || !connector->encoder->crtc)
  4640. continue;
  4641. if (connector->encoder->crtc != crtc)
  4642. continue;
  4643. connector->dpms = DRM_MODE_DPMS_OFF;
  4644. to_intel_encoder(connector->encoder)->connectors_active = false;
  4645. }
  4646. }
  4647. void intel_encoder_destroy(struct drm_encoder *encoder)
  4648. {
  4649. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4650. drm_encoder_cleanup(encoder);
  4651. kfree(intel_encoder);
  4652. }
  4653. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4654. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4655. * state of the entire output pipe. */
  4656. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4657. {
  4658. if (mode == DRM_MODE_DPMS_ON) {
  4659. encoder->connectors_active = true;
  4660. intel_crtc_update_dpms(encoder->base.crtc);
  4661. } else {
  4662. encoder->connectors_active = false;
  4663. intel_crtc_update_dpms(encoder->base.crtc);
  4664. }
  4665. }
  4666. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4667. * internal consistency). */
  4668. static void intel_connector_check_state(struct intel_connector *connector)
  4669. {
  4670. if (connector->get_hw_state(connector)) {
  4671. struct intel_encoder *encoder = connector->encoder;
  4672. struct drm_crtc *crtc;
  4673. bool encoder_enabled;
  4674. enum pipe pipe;
  4675. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4676. connector->base.base.id,
  4677. connector->base.name);
  4678. /* there is no real hw state for MST connectors */
  4679. if (connector->mst_port)
  4680. return;
  4681. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4682. "wrong connector dpms state\n");
  4683. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  4684. "active connector not linked to encoder\n");
  4685. if (encoder) {
  4686. I915_STATE_WARN(!encoder->connectors_active,
  4687. "encoder->connectors_active not set\n");
  4688. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4689. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  4690. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  4691. return;
  4692. crtc = encoder->base.crtc;
  4693. I915_STATE_WARN(!crtc->state->enable,
  4694. "crtc not enabled\n");
  4695. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4696. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  4697. "encoder active on the wrong pipe\n");
  4698. }
  4699. }
  4700. }
  4701. /* Even simpler default implementation, if there's really no special case to
  4702. * consider. */
  4703. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4704. {
  4705. /* All the simple cases only support two dpms states. */
  4706. if (mode != DRM_MODE_DPMS_ON)
  4707. mode = DRM_MODE_DPMS_OFF;
  4708. if (mode == connector->dpms)
  4709. return;
  4710. connector->dpms = mode;
  4711. /* Only need to change hw state when actually enabled */
  4712. if (connector->encoder)
  4713. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4714. intel_modeset_check_state(connector->dev);
  4715. }
  4716. /* Simple connector->get_hw_state implementation for encoders that support only
  4717. * one connector and no cloning and hence the encoder state determines the state
  4718. * of the connector. */
  4719. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4720. {
  4721. enum pipe pipe = 0;
  4722. struct intel_encoder *encoder = connector->encoder;
  4723. return encoder->get_hw_state(encoder, &pipe);
  4724. }
  4725. static int pipe_required_fdi_lanes(struct drm_device *dev, enum pipe pipe)
  4726. {
  4727. struct intel_crtc *crtc =
  4728. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  4729. if (crtc->base.state->enable &&
  4730. crtc->config->has_pch_encoder)
  4731. return crtc->config->fdi_lanes;
  4732. return 0;
  4733. }
  4734. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4735. struct intel_crtc_state *pipe_config)
  4736. {
  4737. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4738. pipe_name(pipe), pipe_config->fdi_lanes);
  4739. if (pipe_config->fdi_lanes > 4) {
  4740. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4741. pipe_name(pipe), pipe_config->fdi_lanes);
  4742. return false;
  4743. }
  4744. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4745. if (pipe_config->fdi_lanes > 2) {
  4746. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4747. pipe_config->fdi_lanes);
  4748. return false;
  4749. } else {
  4750. return true;
  4751. }
  4752. }
  4753. if (INTEL_INFO(dev)->num_pipes == 2)
  4754. return true;
  4755. /* Ivybridge 3 pipe is really complicated */
  4756. switch (pipe) {
  4757. case PIPE_A:
  4758. return true;
  4759. case PIPE_B:
  4760. if (pipe_config->fdi_lanes > 2 &&
  4761. pipe_required_fdi_lanes(dev, PIPE_C) > 0) {
  4762. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4763. pipe_name(pipe), pipe_config->fdi_lanes);
  4764. return false;
  4765. }
  4766. return true;
  4767. case PIPE_C:
  4768. if (pipe_config->fdi_lanes > 2) {
  4769. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  4770. pipe_name(pipe), pipe_config->fdi_lanes);
  4771. return false;
  4772. }
  4773. if (pipe_required_fdi_lanes(dev, PIPE_B) > 2) {
  4774. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4775. return false;
  4776. }
  4777. return true;
  4778. default:
  4779. BUG();
  4780. }
  4781. }
  4782. #define RETRY 1
  4783. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4784. struct intel_crtc_state *pipe_config)
  4785. {
  4786. struct drm_device *dev = intel_crtc->base.dev;
  4787. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4788. int lane, link_bw, fdi_dotclock;
  4789. bool setup_ok, needs_recompute = false;
  4790. retry:
  4791. /* FDI is a binary signal running at ~2.7GHz, encoding
  4792. * each output octet as 10 bits. The actual frequency
  4793. * is stored as a divider into a 100MHz clock, and the
  4794. * mode pixel clock is stored in units of 1KHz.
  4795. * Hence the bw of each lane in terms of the mode signal
  4796. * is:
  4797. */
  4798. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4799. fdi_dotclock = adjusted_mode->crtc_clock;
  4800. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4801. pipe_config->pipe_bpp);
  4802. pipe_config->fdi_lanes = lane;
  4803. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4804. link_bw, &pipe_config->fdi_m_n);
  4805. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4806. intel_crtc->pipe, pipe_config);
  4807. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4808. pipe_config->pipe_bpp -= 2*3;
  4809. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4810. pipe_config->pipe_bpp);
  4811. needs_recompute = true;
  4812. pipe_config->bw_constrained = true;
  4813. goto retry;
  4814. }
  4815. if (needs_recompute)
  4816. return RETRY;
  4817. return setup_ok ? 0 : -EINVAL;
  4818. }
  4819. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4820. struct intel_crtc_state *pipe_config)
  4821. {
  4822. pipe_config->ips_enabled = i915.enable_ips &&
  4823. hsw_crtc_supports_ips(crtc) &&
  4824. pipe_config->pipe_bpp <= 24;
  4825. }
  4826. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4827. struct intel_crtc_state *pipe_config)
  4828. {
  4829. struct drm_device *dev = crtc->base.dev;
  4830. struct drm_i915_private *dev_priv = dev->dev_private;
  4831. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4832. /* FIXME should check pixel clock limits on all platforms */
  4833. if (INTEL_INFO(dev)->gen < 4) {
  4834. int clock_limit =
  4835. dev_priv->display.get_display_clock_speed(dev);
  4836. /*
  4837. * Enable pixel doubling when the dot clock
  4838. * is > 90% of the (display) core speed.
  4839. *
  4840. * GDG double wide on either pipe,
  4841. * otherwise pipe A only.
  4842. */
  4843. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4844. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4845. clock_limit *= 2;
  4846. pipe_config->double_wide = true;
  4847. }
  4848. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4849. return -EINVAL;
  4850. }
  4851. /*
  4852. * Pipe horizontal size must be even in:
  4853. * - DVO ganged mode
  4854. * - LVDS dual channel mode
  4855. * - Double wide pipe
  4856. */
  4857. if ((intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4858. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4859. pipe_config->pipe_src_w &= ~1;
  4860. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4861. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4862. */
  4863. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4864. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4865. return -EINVAL;
  4866. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4867. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4868. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4869. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4870. * for lvds. */
  4871. pipe_config->pipe_bpp = 8*3;
  4872. }
  4873. if (HAS_IPS(dev))
  4874. hsw_compute_ips_config(crtc, pipe_config);
  4875. if (pipe_config->has_pch_encoder)
  4876. return ironlake_fdi_compute_config(crtc, pipe_config);
  4877. return 0;
  4878. }
  4879. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4880. {
  4881. struct drm_i915_private *dev_priv = dev->dev_private;
  4882. u32 val;
  4883. int divider;
  4884. if (dev_priv->hpll_freq == 0)
  4885. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4886. mutex_lock(&dev_priv->dpio_lock);
  4887. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4888. mutex_unlock(&dev_priv->dpio_lock);
  4889. divider = val & DISPLAY_FREQUENCY_VALUES;
  4890. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4891. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4892. "cdclk change in progress\n");
  4893. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4894. }
  4895. static int i945_get_display_clock_speed(struct drm_device *dev)
  4896. {
  4897. return 400000;
  4898. }
  4899. static int i915_get_display_clock_speed(struct drm_device *dev)
  4900. {
  4901. return 333000;
  4902. }
  4903. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4904. {
  4905. return 200000;
  4906. }
  4907. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4908. {
  4909. u16 gcfgc = 0;
  4910. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4911. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4912. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4913. return 267000;
  4914. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4915. return 333000;
  4916. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4917. return 444000;
  4918. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4919. return 200000;
  4920. default:
  4921. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4922. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4923. return 133000;
  4924. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4925. return 167000;
  4926. }
  4927. }
  4928. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4929. {
  4930. u16 gcfgc = 0;
  4931. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4932. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4933. return 133000;
  4934. else {
  4935. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4936. case GC_DISPLAY_CLOCK_333_MHZ:
  4937. return 333000;
  4938. default:
  4939. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4940. return 190000;
  4941. }
  4942. }
  4943. }
  4944. static int i865_get_display_clock_speed(struct drm_device *dev)
  4945. {
  4946. return 266000;
  4947. }
  4948. static int i855_get_display_clock_speed(struct drm_device *dev)
  4949. {
  4950. u16 hpllcc = 0;
  4951. /* Assume that the hardware is in the high speed state. This
  4952. * should be the default.
  4953. */
  4954. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4955. case GC_CLOCK_133_200:
  4956. case GC_CLOCK_100_200:
  4957. return 200000;
  4958. case GC_CLOCK_166_250:
  4959. return 250000;
  4960. case GC_CLOCK_100_133:
  4961. return 133000;
  4962. }
  4963. /* Shouldn't happen */
  4964. return 0;
  4965. }
  4966. static int i830_get_display_clock_speed(struct drm_device *dev)
  4967. {
  4968. return 133000;
  4969. }
  4970. static void
  4971. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4972. {
  4973. while (*num > DATA_LINK_M_N_MASK ||
  4974. *den > DATA_LINK_M_N_MASK) {
  4975. *num >>= 1;
  4976. *den >>= 1;
  4977. }
  4978. }
  4979. static void compute_m_n(unsigned int m, unsigned int n,
  4980. uint32_t *ret_m, uint32_t *ret_n)
  4981. {
  4982. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4983. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4984. intel_reduce_m_n_ratio(ret_m, ret_n);
  4985. }
  4986. void
  4987. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4988. int pixel_clock, int link_clock,
  4989. struct intel_link_m_n *m_n)
  4990. {
  4991. m_n->tu = 64;
  4992. compute_m_n(bits_per_pixel * pixel_clock,
  4993. link_clock * nlanes * 8,
  4994. &m_n->gmch_m, &m_n->gmch_n);
  4995. compute_m_n(pixel_clock, link_clock,
  4996. &m_n->link_m, &m_n->link_n);
  4997. }
  4998. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4999. {
  5000. if (i915.panel_use_ssc >= 0)
  5001. return i915.panel_use_ssc != 0;
  5002. return dev_priv->vbt.lvds_use_ssc
  5003. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5004. }
  5005. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  5006. {
  5007. struct drm_device *dev = crtc->base.dev;
  5008. struct drm_i915_private *dev_priv = dev->dev_private;
  5009. int refclk;
  5010. if (IS_VALLEYVIEW(dev)) {
  5011. refclk = 100000;
  5012. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5013. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5014. refclk = dev_priv->vbt.lvds_ssc_freq;
  5015. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5016. } else if (!IS_GEN2(dev)) {
  5017. refclk = 96000;
  5018. } else {
  5019. refclk = 48000;
  5020. }
  5021. return refclk;
  5022. }
  5023. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5024. {
  5025. return (1 << dpll->n) << 16 | dpll->m2;
  5026. }
  5027. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5028. {
  5029. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5030. }
  5031. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5032. struct intel_crtc_state *crtc_state,
  5033. intel_clock_t *reduced_clock)
  5034. {
  5035. struct drm_device *dev = crtc->base.dev;
  5036. u32 fp, fp2 = 0;
  5037. if (IS_PINEVIEW(dev)) {
  5038. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5039. if (reduced_clock)
  5040. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5041. } else {
  5042. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5043. if (reduced_clock)
  5044. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5045. }
  5046. crtc_state->dpll_hw_state.fp0 = fp;
  5047. crtc->lowfreq_avail = false;
  5048. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5049. reduced_clock) {
  5050. crtc_state->dpll_hw_state.fp1 = fp2;
  5051. crtc->lowfreq_avail = true;
  5052. } else {
  5053. crtc_state->dpll_hw_state.fp1 = fp;
  5054. }
  5055. }
  5056. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5057. pipe)
  5058. {
  5059. u32 reg_val;
  5060. /*
  5061. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5062. * and set it to a reasonable value instead.
  5063. */
  5064. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5065. reg_val &= 0xffffff00;
  5066. reg_val |= 0x00000030;
  5067. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5068. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5069. reg_val &= 0x8cffffff;
  5070. reg_val = 0x8c000000;
  5071. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5072. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5073. reg_val &= 0xffffff00;
  5074. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5075. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5076. reg_val &= 0x00ffffff;
  5077. reg_val |= 0xb0000000;
  5078. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5079. }
  5080. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5081. struct intel_link_m_n *m_n)
  5082. {
  5083. struct drm_device *dev = crtc->base.dev;
  5084. struct drm_i915_private *dev_priv = dev->dev_private;
  5085. int pipe = crtc->pipe;
  5086. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5087. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5088. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5089. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5090. }
  5091. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5092. struct intel_link_m_n *m_n,
  5093. struct intel_link_m_n *m2_n2)
  5094. {
  5095. struct drm_device *dev = crtc->base.dev;
  5096. struct drm_i915_private *dev_priv = dev->dev_private;
  5097. int pipe = crtc->pipe;
  5098. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5099. if (INTEL_INFO(dev)->gen >= 5) {
  5100. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5101. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5102. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5103. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5104. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5105. * for gen < 8) and if DRRS is supported (to make sure the
  5106. * registers are not unnecessarily accessed).
  5107. */
  5108. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  5109. crtc->config->has_drrs) {
  5110. I915_WRITE(PIPE_DATA_M2(transcoder),
  5111. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5112. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5113. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5114. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5115. }
  5116. } else {
  5117. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5118. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5119. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5120. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5121. }
  5122. }
  5123. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5124. {
  5125. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5126. if (m_n == M1_N1) {
  5127. dp_m_n = &crtc->config->dp_m_n;
  5128. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5129. } else if (m_n == M2_N2) {
  5130. /*
  5131. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5132. * needs to be programmed into M1_N1.
  5133. */
  5134. dp_m_n = &crtc->config->dp_m2_n2;
  5135. } else {
  5136. DRM_ERROR("Unsupported divider value\n");
  5137. return;
  5138. }
  5139. if (crtc->config->has_pch_encoder)
  5140. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5141. else
  5142. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5143. }
  5144. static void vlv_update_pll(struct intel_crtc *crtc,
  5145. struct intel_crtc_state *pipe_config)
  5146. {
  5147. u32 dpll, dpll_md;
  5148. /*
  5149. * Enable DPIO clock input. We should never disable the reference
  5150. * clock for pipe B, since VGA hotplug / manual detection depends
  5151. * on it.
  5152. */
  5153. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  5154. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  5155. /* We should never disable this, set it here for state tracking */
  5156. if (crtc->pipe == PIPE_B)
  5157. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5158. dpll |= DPLL_VCO_ENABLE;
  5159. pipe_config->dpll_hw_state.dpll = dpll;
  5160. dpll_md = (pipe_config->pixel_multiplier - 1)
  5161. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5162. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  5163. }
  5164. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5165. const struct intel_crtc_state *pipe_config)
  5166. {
  5167. struct drm_device *dev = crtc->base.dev;
  5168. struct drm_i915_private *dev_priv = dev->dev_private;
  5169. int pipe = crtc->pipe;
  5170. u32 mdiv;
  5171. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5172. u32 coreclk, reg_val;
  5173. mutex_lock(&dev_priv->dpio_lock);
  5174. bestn = pipe_config->dpll.n;
  5175. bestm1 = pipe_config->dpll.m1;
  5176. bestm2 = pipe_config->dpll.m2;
  5177. bestp1 = pipe_config->dpll.p1;
  5178. bestp2 = pipe_config->dpll.p2;
  5179. /* See eDP HDMI DPIO driver vbios notes doc */
  5180. /* PLL B needs special handling */
  5181. if (pipe == PIPE_B)
  5182. vlv_pllb_recal_opamp(dev_priv, pipe);
  5183. /* Set up Tx target for periodic Rcomp update */
  5184. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5185. /* Disable target IRef on PLL */
  5186. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5187. reg_val &= 0x00ffffff;
  5188. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5189. /* Disable fast lock */
  5190. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5191. /* Set idtafcrecal before PLL is enabled */
  5192. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5193. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5194. mdiv |= ((bestn << DPIO_N_SHIFT));
  5195. mdiv |= (1 << DPIO_K_SHIFT);
  5196. /*
  5197. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5198. * but we don't support that).
  5199. * Note: don't use the DAC post divider as it seems unstable.
  5200. */
  5201. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5202. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5203. mdiv |= DPIO_ENABLE_CALIBRATION;
  5204. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5205. /* Set HBR and RBR LPF coefficients */
  5206. if (pipe_config->port_clock == 162000 ||
  5207. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  5208. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  5209. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5210. 0x009f0003);
  5211. else
  5212. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5213. 0x00d0000f);
  5214. if (pipe_config->has_dp_encoder) {
  5215. /* Use SSC source */
  5216. if (pipe == PIPE_A)
  5217. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5218. 0x0df40000);
  5219. else
  5220. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5221. 0x0df70000);
  5222. } else { /* HDMI or VGA */
  5223. /* Use bend source */
  5224. if (pipe == PIPE_A)
  5225. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5226. 0x0df70000);
  5227. else
  5228. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5229. 0x0df40000);
  5230. }
  5231. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5232. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5233. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5234. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5235. coreclk |= 0x01000000;
  5236. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5237. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5238. mutex_unlock(&dev_priv->dpio_lock);
  5239. }
  5240. static void chv_update_pll(struct intel_crtc *crtc,
  5241. struct intel_crtc_state *pipe_config)
  5242. {
  5243. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5244. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5245. DPLL_VCO_ENABLE;
  5246. if (crtc->pipe != PIPE_A)
  5247. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5248. pipe_config->dpll_hw_state.dpll_md =
  5249. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5250. }
  5251. static void chv_prepare_pll(struct intel_crtc *crtc,
  5252. const struct intel_crtc_state *pipe_config)
  5253. {
  5254. struct drm_device *dev = crtc->base.dev;
  5255. struct drm_i915_private *dev_priv = dev->dev_private;
  5256. int pipe = crtc->pipe;
  5257. int dpll_reg = DPLL(crtc->pipe);
  5258. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5259. u32 loopfilter, tribuf_calcntr;
  5260. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5261. u32 dpio_val;
  5262. int vco;
  5263. bestn = pipe_config->dpll.n;
  5264. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5265. bestm1 = pipe_config->dpll.m1;
  5266. bestm2 = pipe_config->dpll.m2 >> 22;
  5267. bestp1 = pipe_config->dpll.p1;
  5268. bestp2 = pipe_config->dpll.p2;
  5269. vco = pipe_config->dpll.vco;
  5270. dpio_val = 0;
  5271. loopfilter = 0;
  5272. /*
  5273. * Enable Refclk and SSC
  5274. */
  5275. I915_WRITE(dpll_reg,
  5276. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5277. mutex_lock(&dev_priv->dpio_lock);
  5278. /* p1 and p2 divider */
  5279. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5280. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5281. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5282. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5283. 1 << DPIO_CHV_K_DIV_SHIFT);
  5284. /* Feedback post-divider - m2 */
  5285. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5286. /* Feedback refclk divider - n and m1 */
  5287. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5288. DPIO_CHV_M1_DIV_BY_2 |
  5289. 1 << DPIO_CHV_N_DIV_SHIFT);
  5290. /* M2 fraction division */
  5291. if (bestm2_frac)
  5292. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5293. /* M2 fraction division enable */
  5294. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5295. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5296. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5297. if (bestm2_frac)
  5298. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5299. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5300. /* Program digital lock detect threshold */
  5301. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5302. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5303. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5304. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5305. if (!bestm2_frac)
  5306. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5307. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5308. /* Loop filter */
  5309. if (vco == 5400000) {
  5310. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5311. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5312. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5313. tribuf_calcntr = 0x9;
  5314. } else if (vco <= 6200000) {
  5315. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5316. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5317. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5318. tribuf_calcntr = 0x9;
  5319. } else if (vco <= 6480000) {
  5320. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5321. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5322. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5323. tribuf_calcntr = 0x8;
  5324. } else {
  5325. /* Not supported. Apply the same limits as in the max case */
  5326. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5327. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5328. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5329. tribuf_calcntr = 0;
  5330. }
  5331. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5332. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5333. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5334. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5335. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5336. /* AFC Recal */
  5337. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5338. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5339. DPIO_AFC_RECAL);
  5340. mutex_unlock(&dev_priv->dpio_lock);
  5341. }
  5342. /**
  5343. * vlv_force_pll_on - forcibly enable just the PLL
  5344. * @dev_priv: i915 private structure
  5345. * @pipe: pipe PLL to enable
  5346. * @dpll: PLL configuration
  5347. *
  5348. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5349. * in cases where we need the PLL enabled even when @pipe is not going to
  5350. * be enabled.
  5351. */
  5352. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5353. const struct dpll *dpll)
  5354. {
  5355. struct intel_crtc *crtc =
  5356. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5357. struct intel_crtc_state pipe_config = {
  5358. .pixel_multiplier = 1,
  5359. .dpll = *dpll,
  5360. };
  5361. if (IS_CHERRYVIEW(dev)) {
  5362. chv_update_pll(crtc, &pipe_config);
  5363. chv_prepare_pll(crtc, &pipe_config);
  5364. chv_enable_pll(crtc, &pipe_config);
  5365. } else {
  5366. vlv_update_pll(crtc, &pipe_config);
  5367. vlv_prepare_pll(crtc, &pipe_config);
  5368. vlv_enable_pll(crtc, &pipe_config);
  5369. }
  5370. }
  5371. /**
  5372. * vlv_force_pll_off - forcibly disable just the PLL
  5373. * @dev_priv: i915 private structure
  5374. * @pipe: pipe PLL to disable
  5375. *
  5376. * Disable the PLL for @pipe. To be used in cases where we need
  5377. * the PLL enabled even when @pipe is not going to be enabled.
  5378. */
  5379. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5380. {
  5381. if (IS_CHERRYVIEW(dev))
  5382. chv_disable_pll(to_i915(dev), pipe);
  5383. else
  5384. vlv_disable_pll(to_i915(dev), pipe);
  5385. }
  5386. static void i9xx_update_pll(struct intel_crtc *crtc,
  5387. struct intel_crtc_state *crtc_state,
  5388. intel_clock_t *reduced_clock,
  5389. int num_connectors)
  5390. {
  5391. struct drm_device *dev = crtc->base.dev;
  5392. struct drm_i915_private *dev_priv = dev->dev_private;
  5393. u32 dpll;
  5394. bool is_sdvo;
  5395. struct dpll *clock = &crtc_state->dpll;
  5396. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5397. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5398. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5399. dpll = DPLL_VGA_MODE_DIS;
  5400. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5401. dpll |= DPLLB_MODE_LVDS;
  5402. else
  5403. dpll |= DPLLB_MODE_DAC_SERIAL;
  5404. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5405. dpll |= (crtc_state->pixel_multiplier - 1)
  5406. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5407. }
  5408. if (is_sdvo)
  5409. dpll |= DPLL_SDVO_HIGH_SPEED;
  5410. if (crtc_state->has_dp_encoder)
  5411. dpll |= DPLL_SDVO_HIGH_SPEED;
  5412. /* compute bitmask from p1 value */
  5413. if (IS_PINEVIEW(dev))
  5414. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5415. else {
  5416. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5417. if (IS_G4X(dev) && reduced_clock)
  5418. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5419. }
  5420. switch (clock->p2) {
  5421. case 5:
  5422. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5423. break;
  5424. case 7:
  5425. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5426. break;
  5427. case 10:
  5428. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5429. break;
  5430. case 14:
  5431. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5432. break;
  5433. }
  5434. if (INTEL_INFO(dev)->gen >= 4)
  5435. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5436. if (crtc_state->sdvo_tv_clock)
  5437. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5438. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5439. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5440. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5441. else
  5442. dpll |= PLL_REF_INPUT_DREFCLK;
  5443. dpll |= DPLL_VCO_ENABLE;
  5444. crtc_state->dpll_hw_state.dpll = dpll;
  5445. if (INTEL_INFO(dev)->gen >= 4) {
  5446. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5447. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5448. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5449. }
  5450. }
  5451. static void i8xx_update_pll(struct intel_crtc *crtc,
  5452. struct intel_crtc_state *crtc_state,
  5453. intel_clock_t *reduced_clock,
  5454. int num_connectors)
  5455. {
  5456. struct drm_device *dev = crtc->base.dev;
  5457. struct drm_i915_private *dev_priv = dev->dev_private;
  5458. u32 dpll;
  5459. struct dpll *clock = &crtc_state->dpll;
  5460. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5461. dpll = DPLL_VGA_MODE_DIS;
  5462. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5463. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5464. } else {
  5465. if (clock->p1 == 2)
  5466. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5467. else
  5468. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5469. if (clock->p2 == 4)
  5470. dpll |= PLL_P2_DIVIDE_BY_4;
  5471. }
  5472. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5473. dpll |= DPLL_DVO_2X_MODE;
  5474. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5475. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5476. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5477. else
  5478. dpll |= PLL_REF_INPUT_DREFCLK;
  5479. dpll |= DPLL_VCO_ENABLE;
  5480. crtc_state->dpll_hw_state.dpll = dpll;
  5481. }
  5482. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5483. {
  5484. struct drm_device *dev = intel_crtc->base.dev;
  5485. struct drm_i915_private *dev_priv = dev->dev_private;
  5486. enum pipe pipe = intel_crtc->pipe;
  5487. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5488. struct drm_display_mode *adjusted_mode =
  5489. &intel_crtc->config->base.adjusted_mode;
  5490. uint32_t crtc_vtotal, crtc_vblank_end;
  5491. int vsyncshift = 0;
  5492. /* We need to be careful not to changed the adjusted mode, for otherwise
  5493. * the hw state checker will get angry at the mismatch. */
  5494. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5495. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5496. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5497. /* the chip adds 2 halflines automatically */
  5498. crtc_vtotal -= 1;
  5499. crtc_vblank_end -= 1;
  5500. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5501. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5502. else
  5503. vsyncshift = adjusted_mode->crtc_hsync_start -
  5504. adjusted_mode->crtc_htotal / 2;
  5505. if (vsyncshift < 0)
  5506. vsyncshift += adjusted_mode->crtc_htotal;
  5507. }
  5508. if (INTEL_INFO(dev)->gen > 3)
  5509. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5510. I915_WRITE(HTOTAL(cpu_transcoder),
  5511. (adjusted_mode->crtc_hdisplay - 1) |
  5512. ((adjusted_mode->crtc_htotal - 1) << 16));
  5513. I915_WRITE(HBLANK(cpu_transcoder),
  5514. (adjusted_mode->crtc_hblank_start - 1) |
  5515. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5516. I915_WRITE(HSYNC(cpu_transcoder),
  5517. (adjusted_mode->crtc_hsync_start - 1) |
  5518. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5519. I915_WRITE(VTOTAL(cpu_transcoder),
  5520. (adjusted_mode->crtc_vdisplay - 1) |
  5521. ((crtc_vtotal - 1) << 16));
  5522. I915_WRITE(VBLANK(cpu_transcoder),
  5523. (adjusted_mode->crtc_vblank_start - 1) |
  5524. ((crtc_vblank_end - 1) << 16));
  5525. I915_WRITE(VSYNC(cpu_transcoder),
  5526. (adjusted_mode->crtc_vsync_start - 1) |
  5527. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5528. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5529. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5530. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5531. * bits. */
  5532. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5533. (pipe == PIPE_B || pipe == PIPE_C))
  5534. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5535. /* pipesrc controls the size that is scaled from, which should
  5536. * always be the user's requested size.
  5537. */
  5538. I915_WRITE(PIPESRC(pipe),
  5539. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5540. (intel_crtc->config->pipe_src_h - 1));
  5541. }
  5542. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5543. struct intel_crtc_state *pipe_config)
  5544. {
  5545. struct drm_device *dev = crtc->base.dev;
  5546. struct drm_i915_private *dev_priv = dev->dev_private;
  5547. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5548. uint32_t tmp;
  5549. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5550. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5551. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5552. tmp = I915_READ(HBLANK(cpu_transcoder));
  5553. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5554. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5555. tmp = I915_READ(HSYNC(cpu_transcoder));
  5556. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5557. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5558. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5559. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5560. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5561. tmp = I915_READ(VBLANK(cpu_transcoder));
  5562. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5563. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5564. tmp = I915_READ(VSYNC(cpu_transcoder));
  5565. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5566. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5567. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5568. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5569. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5570. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5571. }
  5572. tmp = I915_READ(PIPESRC(crtc->pipe));
  5573. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5574. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5575. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5576. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5577. }
  5578. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5579. struct intel_crtc_state *pipe_config)
  5580. {
  5581. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5582. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5583. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5584. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5585. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5586. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5587. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5588. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5589. mode->flags = pipe_config->base.adjusted_mode.flags;
  5590. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5591. mode->flags |= pipe_config->base.adjusted_mode.flags;
  5592. }
  5593. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5594. {
  5595. struct drm_device *dev = intel_crtc->base.dev;
  5596. struct drm_i915_private *dev_priv = dev->dev_private;
  5597. uint32_t pipeconf;
  5598. pipeconf = 0;
  5599. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5600. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5601. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5602. if (intel_crtc->config->double_wide)
  5603. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5604. /* only g4x and later have fancy bpc/dither controls */
  5605. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5606. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5607. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5608. pipeconf |= PIPECONF_DITHER_EN |
  5609. PIPECONF_DITHER_TYPE_SP;
  5610. switch (intel_crtc->config->pipe_bpp) {
  5611. case 18:
  5612. pipeconf |= PIPECONF_6BPC;
  5613. break;
  5614. case 24:
  5615. pipeconf |= PIPECONF_8BPC;
  5616. break;
  5617. case 30:
  5618. pipeconf |= PIPECONF_10BPC;
  5619. break;
  5620. default:
  5621. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5622. BUG();
  5623. }
  5624. }
  5625. if (HAS_PIPE_CXSR(dev)) {
  5626. if (intel_crtc->lowfreq_avail) {
  5627. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5628. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5629. } else {
  5630. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5631. }
  5632. }
  5633. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5634. if (INTEL_INFO(dev)->gen < 4 ||
  5635. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5636. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5637. else
  5638. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5639. } else
  5640. pipeconf |= PIPECONF_PROGRESSIVE;
  5641. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  5642. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5643. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5644. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5645. }
  5646. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  5647. struct intel_crtc_state *crtc_state)
  5648. {
  5649. struct drm_device *dev = crtc->base.dev;
  5650. struct drm_i915_private *dev_priv = dev->dev_private;
  5651. int refclk, num_connectors = 0;
  5652. intel_clock_t clock, reduced_clock;
  5653. bool ok, has_reduced_clock = false;
  5654. bool is_lvds = false, is_dsi = false;
  5655. struct intel_encoder *encoder;
  5656. const intel_limit_t *limit;
  5657. for_each_intel_encoder(dev, encoder) {
  5658. if (encoder->new_crtc != crtc)
  5659. continue;
  5660. switch (encoder->type) {
  5661. case INTEL_OUTPUT_LVDS:
  5662. is_lvds = true;
  5663. break;
  5664. case INTEL_OUTPUT_DSI:
  5665. is_dsi = true;
  5666. break;
  5667. default:
  5668. break;
  5669. }
  5670. num_connectors++;
  5671. }
  5672. if (is_dsi)
  5673. return 0;
  5674. if (!crtc_state->clock_set) {
  5675. refclk = i9xx_get_refclk(crtc, num_connectors);
  5676. /*
  5677. * Returns a set of divisors for the desired target clock with
  5678. * the given refclk, or FALSE. The returned values represent
  5679. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5680. * 2) / p1 / p2.
  5681. */
  5682. limit = intel_limit(crtc, refclk);
  5683. ok = dev_priv->display.find_dpll(limit, crtc,
  5684. crtc_state->port_clock,
  5685. refclk, NULL, &clock);
  5686. if (!ok) {
  5687. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5688. return -EINVAL;
  5689. }
  5690. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5691. /*
  5692. * Ensure we match the reduced clock's P to the target
  5693. * clock. If the clocks don't match, we can't switch
  5694. * the display clock by using the FP0/FP1. In such case
  5695. * we will disable the LVDS downclock feature.
  5696. */
  5697. has_reduced_clock =
  5698. dev_priv->display.find_dpll(limit, crtc,
  5699. dev_priv->lvds_downclock,
  5700. refclk, &clock,
  5701. &reduced_clock);
  5702. }
  5703. /* Compat-code for transition, will disappear. */
  5704. crtc_state->dpll.n = clock.n;
  5705. crtc_state->dpll.m1 = clock.m1;
  5706. crtc_state->dpll.m2 = clock.m2;
  5707. crtc_state->dpll.p1 = clock.p1;
  5708. crtc_state->dpll.p2 = clock.p2;
  5709. }
  5710. if (IS_GEN2(dev)) {
  5711. i8xx_update_pll(crtc, crtc_state,
  5712. has_reduced_clock ? &reduced_clock : NULL,
  5713. num_connectors);
  5714. } else if (IS_CHERRYVIEW(dev)) {
  5715. chv_update_pll(crtc, crtc_state);
  5716. } else if (IS_VALLEYVIEW(dev)) {
  5717. vlv_update_pll(crtc, crtc_state);
  5718. } else {
  5719. i9xx_update_pll(crtc, crtc_state,
  5720. has_reduced_clock ? &reduced_clock : NULL,
  5721. num_connectors);
  5722. }
  5723. return 0;
  5724. }
  5725. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5726. struct intel_crtc_state *pipe_config)
  5727. {
  5728. struct drm_device *dev = crtc->base.dev;
  5729. struct drm_i915_private *dev_priv = dev->dev_private;
  5730. uint32_t tmp;
  5731. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5732. return;
  5733. tmp = I915_READ(PFIT_CONTROL);
  5734. if (!(tmp & PFIT_ENABLE))
  5735. return;
  5736. /* Check whether the pfit is attached to our pipe. */
  5737. if (INTEL_INFO(dev)->gen < 4) {
  5738. if (crtc->pipe != PIPE_B)
  5739. return;
  5740. } else {
  5741. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5742. return;
  5743. }
  5744. pipe_config->gmch_pfit.control = tmp;
  5745. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5746. if (INTEL_INFO(dev)->gen < 5)
  5747. pipe_config->gmch_pfit.lvds_border_bits =
  5748. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5749. }
  5750. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5751. struct intel_crtc_state *pipe_config)
  5752. {
  5753. struct drm_device *dev = crtc->base.dev;
  5754. struct drm_i915_private *dev_priv = dev->dev_private;
  5755. int pipe = pipe_config->cpu_transcoder;
  5756. intel_clock_t clock;
  5757. u32 mdiv;
  5758. int refclk = 100000;
  5759. /* In case of MIPI DPLL will not even be used */
  5760. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5761. return;
  5762. mutex_lock(&dev_priv->dpio_lock);
  5763. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5764. mutex_unlock(&dev_priv->dpio_lock);
  5765. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5766. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5767. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5768. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5769. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5770. vlv_clock(refclk, &clock);
  5771. /* clock.dot is the fast clock */
  5772. pipe_config->port_clock = clock.dot / 5;
  5773. }
  5774. static void
  5775. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  5776. struct intel_initial_plane_config *plane_config)
  5777. {
  5778. struct drm_device *dev = crtc->base.dev;
  5779. struct drm_i915_private *dev_priv = dev->dev_private;
  5780. u32 val, base, offset;
  5781. int pipe = crtc->pipe, plane = crtc->plane;
  5782. int fourcc, pixel_format;
  5783. unsigned int aligned_height;
  5784. struct drm_framebuffer *fb;
  5785. struct intel_framebuffer *intel_fb;
  5786. val = I915_READ(DSPCNTR(plane));
  5787. if (!(val & DISPLAY_PLANE_ENABLE))
  5788. return;
  5789. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5790. if (!intel_fb) {
  5791. DRM_DEBUG_KMS("failed to alloc fb\n");
  5792. return;
  5793. }
  5794. fb = &intel_fb->base;
  5795. if (INTEL_INFO(dev)->gen >= 4) {
  5796. if (val & DISPPLANE_TILED) {
  5797. plane_config->tiling = I915_TILING_X;
  5798. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  5799. }
  5800. }
  5801. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5802. fourcc = i9xx_format_to_fourcc(pixel_format);
  5803. fb->pixel_format = fourcc;
  5804. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  5805. if (INTEL_INFO(dev)->gen >= 4) {
  5806. if (plane_config->tiling)
  5807. offset = I915_READ(DSPTILEOFF(plane));
  5808. else
  5809. offset = I915_READ(DSPLINOFF(plane));
  5810. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5811. } else {
  5812. base = I915_READ(DSPADDR(plane));
  5813. }
  5814. plane_config->base = base;
  5815. val = I915_READ(PIPESRC(pipe));
  5816. fb->width = ((val >> 16) & 0xfff) + 1;
  5817. fb->height = ((val >> 0) & 0xfff) + 1;
  5818. val = I915_READ(DSPSTRIDE(pipe));
  5819. fb->pitches[0] = val & 0xffffffc0;
  5820. aligned_height = intel_fb_align_height(dev, fb->height,
  5821. fb->pixel_format,
  5822. fb->modifier[0]);
  5823. plane_config->size = fb->pitches[0] * aligned_height;
  5824. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5825. pipe_name(pipe), plane, fb->width, fb->height,
  5826. fb->bits_per_pixel, base, fb->pitches[0],
  5827. plane_config->size);
  5828. plane_config->fb = intel_fb;
  5829. }
  5830. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5831. struct intel_crtc_state *pipe_config)
  5832. {
  5833. struct drm_device *dev = crtc->base.dev;
  5834. struct drm_i915_private *dev_priv = dev->dev_private;
  5835. int pipe = pipe_config->cpu_transcoder;
  5836. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5837. intel_clock_t clock;
  5838. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5839. int refclk = 100000;
  5840. mutex_lock(&dev_priv->dpio_lock);
  5841. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5842. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5843. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5844. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5845. mutex_unlock(&dev_priv->dpio_lock);
  5846. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5847. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5848. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5849. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5850. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5851. chv_clock(refclk, &clock);
  5852. /* clock.dot is the fast clock */
  5853. pipe_config->port_clock = clock.dot / 5;
  5854. }
  5855. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5856. struct intel_crtc_state *pipe_config)
  5857. {
  5858. struct drm_device *dev = crtc->base.dev;
  5859. struct drm_i915_private *dev_priv = dev->dev_private;
  5860. uint32_t tmp;
  5861. if (!intel_display_power_is_enabled(dev_priv,
  5862. POWER_DOMAIN_PIPE(crtc->pipe)))
  5863. return false;
  5864. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5865. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5866. tmp = I915_READ(PIPECONF(crtc->pipe));
  5867. if (!(tmp & PIPECONF_ENABLE))
  5868. return false;
  5869. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5870. switch (tmp & PIPECONF_BPC_MASK) {
  5871. case PIPECONF_6BPC:
  5872. pipe_config->pipe_bpp = 18;
  5873. break;
  5874. case PIPECONF_8BPC:
  5875. pipe_config->pipe_bpp = 24;
  5876. break;
  5877. case PIPECONF_10BPC:
  5878. pipe_config->pipe_bpp = 30;
  5879. break;
  5880. default:
  5881. break;
  5882. }
  5883. }
  5884. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5885. pipe_config->limited_color_range = true;
  5886. if (INTEL_INFO(dev)->gen < 4)
  5887. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5888. intel_get_pipe_timings(crtc, pipe_config);
  5889. i9xx_get_pfit_config(crtc, pipe_config);
  5890. if (INTEL_INFO(dev)->gen >= 4) {
  5891. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5892. pipe_config->pixel_multiplier =
  5893. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5894. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5895. pipe_config->dpll_hw_state.dpll_md = tmp;
  5896. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5897. tmp = I915_READ(DPLL(crtc->pipe));
  5898. pipe_config->pixel_multiplier =
  5899. ((tmp & SDVO_MULTIPLIER_MASK)
  5900. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5901. } else {
  5902. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5903. * port and will be fixed up in the encoder->get_config
  5904. * function. */
  5905. pipe_config->pixel_multiplier = 1;
  5906. }
  5907. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5908. if (!IS_VALLEYVIEW(dev)) {
  5909. /*
  5910. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5911. * on 830. Filter it out here so that we don't
  5912. * report errors due to that.
  5913. */
  5914. if (IS_I830(dev))
  5915. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5916. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5917. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5918. } else {
  5919. /* Mask out read-only status bits. */
  5920. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5921. DPLL_PORTC_READY_MASK |
  5922. DPLL_PORTB_READY_MASK);
  5923. }
  5924. if (IS_CHERRYVIEW(dev))
  5925. chv_crtc_clock_get(crtc, pipe_config);
  5926. else if (IS_VALLEYVIEW(dev))
  5927. vlv_crtc_clock_get(crtc, pipe_config);
  5928. else
  5929. i9xx_crtc_clock_get(crtc, pipe_config);
  5930. return true;
  5931. }
  5932. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5933. {
  5934. struct drm_i915_private *dev_priv = dev->dev_private;
  5935. struct intel_encoder *encoder;
  5936. u32 val, final;
  5937. bool has_lvds = false;
  5938. bool has_cpu_edp = false;
  5939. bool has_panel = false;
  5940. bool has_ck505 = false;
  5941. bool can_ssc = false;
  5942. /* We need to take the global config into account */
  5943. for_each_intel_encoder(dev, encoder) {
  5944. switch (encoder->type) {
  5945. case INTEL_OUTPUT_LVDS:
  5946. has_panel = true;
  5947. has_lvds = true;
  5948. break;
  5949. case INTEL_OUTPUT_EDP:
  5950. has_panel = true;
  5951. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5952. has_cpu_edp = true;
  5953. break;
  5954. default:
  5955. break;
  5956. }
  5957. }
  5958. if (HAS_PCH_IBX(dev)) {
  5959. has_ck505 = dev_priv->vbt.display_clock_mode;
  5960. can_ssc = has_ck505;
  5961. } else {
  5962. has_ck505 = false;
  5963. can_ssc = true;
  5964. }
  5965. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5966. has_panel, has_lvds, has_ck505);
  5967. /* Ironlake: try to setup display ref clock before DPLL
  5968. * enabling. This is only under driver's control after
  5969. * PCH B stepping, previous chipset stepping should be
  5970. * ignoring this setting.
  5971. */
  5972. val = I915_READ(PCH_DREF_CONTROL);
  5973. /* As we must carefully and slowly disable/enable each source in turn,
  5974. * compute the final state we want first and check if we need to
  5975. * make any changes at all.
  5976. */
  5977. final = val;
  5978. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5979. if (has_ck505)
  5980. final |= DREF_NONSPREAD_CK505_ENABLE;
  5981. else
  5982. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5983. final &= ~DREF_SSC_SOURCE_MASK;
  5984. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5985. final &= ~DREF_SSC1_ENABLE;
  5986. if (has_panel) {
  5987. final |= DREF_SSC_SOURCE_ENABLE;
  5988. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5989. final |= DREF_SSC1_ENABLE;
  5990. if (has_cpu_edp) {
  5991. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5992. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5993. else
  5994. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5995. } else
  5996. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5997. } else {
  5998. final |= DREF_SSC_SOURCE_DISABLE;
  5999. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6000. }
  6001. if (final == val)
  6002. return;
  6003. /* Always enable nonspread source */
  6004. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6005. if (has_ck505)
  6006. val |= DREF_NONSPREAD_CK505_ENABLE;
  6007. else
  6008. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6009. if (has_panel) {
  6010. val &= ~DREF_SSC_SOURCE_MASK;
  6011. val |= DREF_SSC_SOURCE_ENABLE;
  6012. /* SSC must be turned on before enabling the CPU output */
  6013. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6014. DRM_DEBUG_KMS("Using SSC on panel\n");
  6015. val |= DREF_SSC1_ENABLE;
  6016. } else
  6017. val &= ~DREF_SSC1_ENABLE;
  6018. /* Get SSC going before enabling the outputs */
  6019. I915_WRITE(PCH_DREF_CONTROL, val);
  6020. POSTING_READ(PCH_DREF_CONTROL);
  6021. udelay(200);
  6022. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6023. /* Enable CPU source on CPU attached eDP */
  6024. if (has_cpu_edp) {
  6025. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6026. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6027. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6028. } else
  6029. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6030. } else
  6031. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6032. I915_WRITE(PCH_DREF_CONTROL, val);
  6033. POSTING_READ(PCH_DREF_CONTROL);
  6034. udelay(200);
  6035. } else {
  6036. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6037. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6038. /* Turn off CPU output */
  6039. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6040. I915_WRITE(PCH_DREF_CONTROL, val);
  6041. POSTING_READ(PCH_DREF_CONTROL);
  6042. udelay(200);
  6043. /* Turn off the SSC source */
  6044. val &= ~DREF_SSC_SOURCE_MASK;
  6045. val |= DREF_SSC_SOURCE_DISABLE;
  6046. /* Turn off SSC1 */
  6047. val &= ~DREF_SSC1_ENABLE;
  6048. I915_WRITE(PCH_DREF_CONTROL, val);
  6049. POSTING_READ(PCH_DREF_CONTROL);
  6050. udelay(200);
  6051. }
  6052. BUG_ON(val != final);
  6053. }
  6054. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6055. {
  6056. uint32_t tmp;
  6057. tmp = I915_READ(SOUTH_CHICKEN2);
  6058. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6059. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6060. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6061. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6062. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6063. tmp = I915_READ(SOUTH_CHICKEN2);
  6064. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6065. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6066. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6067. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6068. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6069. }
  6070. /* WaMPhyProgramming:hsw */
  6071. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6072. {
  6073. uint32_t tmp;
  6074. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6075. tmp &= ~(0xFF << 24);
  6076. tmp |= (0x12 << 24);
  6077. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6078. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6079. tmp |= (1 << 11);
  6080. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6081. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6082. tmp |= (1 << 11);
  6083. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6084. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6085. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6086. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6087. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6088. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6089. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6090. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6091. tmp &= ~(7 << 13);
  6092. tmp |= (5 << 13);
  6093. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6094. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6095. tmp &= ~(7 << 13);
  6096. tmp |= (5 << 13);
  6097. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6098. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6099. tmp &= ~0xFF;
  6100. tmp |= 0x1C;
  6101. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6102. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6103. tmp &= ~0xFF;
  6104. tmp |= 0x1C;
  6105. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6106. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6107. tmp &= ~(0xFF << 16);
  6108. tmp |= (0x1C << 16);
  6109. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6110. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6111. tmp &= ~(0xFF << 16);
  6112. tmp |= (0x1C << 16);
  6113. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6114. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6115. tmp |= (1 << 27);
  6116. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6117. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6118. tmp |= (1 << 27);
  6119. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6120. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6121. tmp &= ~(0xF << 28);
  6122. tmp |= (4 << 28);
  6123. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6124. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6125. tmp &= ~(0xF << 28);
  6126. tmp |= (4 << 28);
  6127. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6128. }
  6129. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6130. * Programming" based on the parameters passed:
  6131. * - Sequence to enable CLKOUT_DP
  6132. * - Sequence to enable CLKOUT_DP without spread
  6133. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6134. */
  6135. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  6136. bool with_fdi)
  6137. {
  6138. struct drm_i915_private *dev_priv = dev->dev_private;
  6139. uint32_t reg, tmp;
  6140. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6141. with_spread = true;
  6142. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  6143. with_fdi, "LP PCH doesn't have FDI\n"))
  6144. with_fdi = false;
  6145. mutex_lock(&dev_priv->dpio_lock);
  6146. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6147. tmp &= ~SBI_SSCCTL_DISABLE;
  6148. tmp |= SBI_SSCCTL_PATHALT;
  6149. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6150. udelay(24);
  6151. if (with_spread) {
  6152. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6153. tmp &= ~SBI_SSCCTL_PATHALT;
  6154. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6155. if (with_fdi) {
  6156. lpt_reset_fdi_mphy(dev_priv);
  6157. lpt_program_fdi_mphy(dev_priv);
  6158. }
  6159. }
  6160. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6161. SBI_GEN0 : SBI_DBUFF0;
  6162. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6163. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6164. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6165. mutex_unlock(&dev_priv->dpio_lock);
  6166. }
  6167. /* Sequence to disable CLKOUT_DP */
  6168. static void lpt_disable_clkout_dp(struct drm_device *dev)
  6169. {
  6170. struct drm_i915_private *dev_priv = dev->dev_private;
  6171. uint32_t reg, tmp;
  6172. mutex_lock(&dev_priv->dpio_lock);
  6173. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6174. SBI_GEN0 : SBI_DBUFF0;
  6175. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6176. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6177. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6178. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6179. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6180. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6181. tmp |= SBI_SSCCTL_PATHALT;
  6182. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6183. udelay(32);
  6184. }
  6185. tmp |= SBI_SSCCTL_DISABLE;
  6186. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6187. }
  6188. mutex_unlock(&dev_priv->dpio_lock);
  6189. }
  6190. static void lpt_init_pch_refclk(struct drm_device *dev)
  6191. {
  6192. struct intel_encoder *encoder;
  6193. bool has_vga = false;
  6194. for_each_intel_encoder(dev, encoder) {
  6195. switch (encoder->type) {
  6196. case INTEL_OUTPUT_ANALOG:
  6197. has_vga = true;
  6198. break;
  6199. default:
  6200. break;
  6201. }
  6202. }
  6203. if (has_vga)
  6204. lpt_enable_clkout_dp(dev, true, true);
  6205. else
  6206. lpt_disable_clkout_dp(dev);
  6207. }
  6208. /*
  6209. * Initialize reference clocks when the driver loads
  6210. */
  6211. void intel_init_pch_refclk(struct drm_device *dev)
  6212. {
  6213. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  6214. ironlake_init_pch_refclk(dev);
  6215. else if (HAS_PCH_LPT(dev))
  6216. lpt_init_pch_refclk(dev);
  6217. }
  6218. static int ironlake_get_refclk(struct drm_crtc *crtc)
  6219. {
  6220. struct drm_device *dev = crtc->dev;
  6221. struct drm_i915_private *dev_priv = dev->dev_private;
  6222. struct intel_encoder *encoder;
  6223. int num_connectors = 0;
  6224. bool is_lvds = false;
  6225. for_each_intel_encoder(dev, encoder) {
  6226. if (encoder->new_crtc != to_intel_crtc(crtc))
  6227. continue;
  6228. switch (encoder->type) {
  6229. case INTEL_OUTPUT_LVDS:
  6230. is_lvds = true;
  6231. break;
  6232. default:
  6233. break;
  6234. }
  6235. num_connectors++;
  6236. }
  6237. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6238. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6239. dev_priv->vbt.lvds_ssc_freq);
  6240. return dev_priv->vbt.lvds_ssc_freq;
  6241. }
  6242. return 120000;
  6243. }
  6244. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6245. {
  6246. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  6247. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6248. int pipe = intel_crtc->pipe;
  6249. uint32_t val;
  6250. val = 0;
  6251. switch (intel_crtc->config->pipe_bpp) {
  6252. case 18:
  6253. val |= PIPECONF_6BPC;
  6254. break;
  6255. case 24:
  6256. val |= PIPECONF_8BPC;
  6257. break;
  6258. case 30:
  6259. val |= PIPECONF_10BPC;
  6260. break;
  6261. case 36:
  6262. val |= PIPECONF_12BPC;
  6263. break;
  6264. default:
  6265. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6266. BUG();
  6267. }
  6268. if (intel_crtc->config->dither)
  6269. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6270. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6271. val |= PIPECONF_INTERLACED_ILK;
  6272. else
  6273. val |= PIPECONF_PROGRESSIVE;
  6274. if (intel_crtc->config->limited_color_range)
  6275. val |= PIPECONF_COLOR_RANGE_SELECT;
  6276. I915_WRITE(PIPECONF(pipe), val);
  6277. POSTING_READ(PIPECONF(pipe));
  6278. }
  6279. /*
  6280. * Set up the pipe CSC unit.
  6281. *
  6282. * Currently only full range RGB to limited range RGB conversion
  6283. * is supported, but eventually this should handle various
  6284. * RGB<->YCbCr scenarios as well.
  6285. */
  6286. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6287. {
  6288. struct drm_device *dev = crtc->dev;
  6289. struct drm_i915_private *dev_priv = dev->dev_private;
  6290. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6291. int pipe = intel_crtc->pipe;
  6292. uint16_t coeff = 0x7800; /* 1.0 */
  6293. /*
  6294. * TODO: Check what kind of values actually come out of the pipe
  6295. * with these coeff/postoff values and adjust to get the best
  6296. * accuracy. Perhaps we even need to take the bpc value into
  6297. * consideration.
  6298. */
  6299. if (intel_crtc->config->limited_color_range)
  6300. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6301. /*
  6302. * GY/GU and RY/RU should be the other way around according
  6303. * to BSpec, but reality doesn't agree. Just set them up in
  6304. * a way that results in the correct picture.
  6305. */
  6306. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6307. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6308. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6309. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6310. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6311. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6312. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6313. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6314. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6315. if (INTEL_INFO(dev)->gen > 6) {
  6316. uint16_t postoff = 0;
  6317. if (intel_crtc->config->limited_color_range)
  6318. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6319. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6320. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6321. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6322. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6323. } else {
  6324. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6325. if (intel_crtc->config->limited_color_range)
  6326. mode |= CSC_BLACK_SCREEN_OFFSET;
  6327. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6328. }
  6329. }
  6330. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6331. {
  6332. struct drm_device *dev = crtc->dev;
  6333. struct drm_i915_private *dev_priv = dev->dev_private;
  6334. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6335. enum pipe pipe = intel_crtc->pipe;
  6336. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6337. uint32_t val;
  6338. val = 0;
  6339. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  6340. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6341. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6342. val |= PIPECONF_INTERLACED_ILK;
  6343. else
  6344. val |= PIPECONF_PROGRESSIVE;
  6345. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6346. POSTING_READ(PIPECONF(cpu_transcoder));
  6347. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6348. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6349. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6350. val = 0;
  6351. switch (intel_crtc->config->pipe_bpp) {
  6352. case 18:
  6353. val |= PIPEMISC_DITHER_6_BPC;
  6354. break;
  6355. case 24:
  6356. val |= PIPEMISC_DITHER_8_BPC;
  6357. break;
  6358. case 30:
  6359. val |= PIPEMISC_DITHER_10_BPC;
  6360. break;
  6361. case 36:
  6362. val |= PIPEMISC_DITHER_12_BPC;
  6363. break;
  6364. default:
  6365. /* Case prevented by pipe_config_set_bpp. */
  6366. BUG();
  6367. }
  6368. if (intel_crtc->config->dither)
  6369. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6370. I915_WRITE(PIPEMISC(pipe), val);
  6371. }
  6372. }
  6373. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6374. struct intel_crtc_state *crtc_state,
  6375. intel_clock_t *clock,
  6376. bool *has_reduced_clock,
  6377. intel_clock_t *reduced_clock)
  6378. {
  6379. struct drm_device *dev = crtc->dev;
  6380. struct drm_i915_private *dev_priv = dev->dev_private;
  6381. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6382. int refclk;
  6383. const intel_limit_t *limit;
  6384. bool ret, is_lvds = false;
  6385. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6386. refclk = ironlake_get_refclk(crtc);
  6387. /*
  6388. * Returns a set of divisors for the desired target clock with the given
  6389. * refclk, or FALSE. The returned values represent the clock equation:
  6390. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6391. */
  6392. limit = intel_limit(intel_crtc, refclk);
  6393. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6394. crtc_state->port_clock,
  6395. refclk, NULL, clock);
  6396. if (!ret)
  6397. return false;
  6398. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6399. /*
  6400. * Ensure we match the reduced clock's P to the target clock.
  6401. * If the clocks don't match, we can't switch the display clock
  6402. * by using the FP0/FP1. In such case we will disable the LVDS
  6403. * downclock feature.
  6404. */
  6405. *has_reduced_clock =
  6406. dev_priv->display.find_dpll(limit, intel_crtc,
  6407. dev_priv->lvds_downclock,
  6408. refclk, clock,
  6409. reduced_clock);
  6410. }
  6411. return true;
  6412. }
  6413. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6414. {
  6415. /*
  6416. * Account for spread spectrum to avoid
  6417. * oversubscribing the link. Max center spread
  6418. * is 2.5%; use 5% for safety's sake.
  6419. */
  6420. u32 bps = target_clock * bpp * 21 / 20;
  6421. return DIV_ROUND_UP(bps, link_bw * 8);
  6422. }
  6423. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6424. {
  6425. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6426. }
  6427. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6428. struct intel_crtc_state *crtc_state,
  6429. u32 *fp,
  6430. intel_clock_t *reduced_clock, u32 *fp2)
  6431. {
  6432. struct drm_crtc *crtc = &intel_crtc->base;
  6433. struct drm_device *dev = crtc->dev;
  6434. struct drm_i915_private *dev_priv = dev->dev_private;
  6435. struct intel_encoder *intel_encoder;
  6436. uint32_t dpll;
  6437. int factor, num_connectors = 0;
  6438. bool is_lvds = false, is_sdvo = false;
  6439. for_each_intel_encoder(dev, intel_encoder) {
  6440. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6441. continue;
  6442. switch (intel_encoder->type) {
  6443. case INTEL_OUTPUT_LVDS:
  6444. is_lvds = true;
  6445. break;
  6446. case INTEL_OUTPUT_SDVO:
  6447. case INTEL_OUTPUT_HDMI:
  6448. is_sdvo = true;
  6449. break;
  6450. default:
  6451. break;
  6452. }
  6453. num_connectors++;
  6454. }
  6455. /* Enable autotuning of the PLL clock (if permissible) */
  6456. factor = 21;
  6457. if (is_lvds) {
  6458. if ((intel_panel_use_ssc(dev_priv) &&
  6459. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6460. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6461. factor = 25;
  6462. } else if (crtc_state->sdvo_tv_clock)
  6463. factor = 20;
  6464. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6465. *fp |= FP_CB_TUNE;
  6466. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6467. *fp2 |= FP_CB_TUNE;
  6468. dpll = 0;
  6469. if (is_lvds)
  6470. dpll |= DPLLB_MODE_LVDS;
  6471. else
  6472. dpll |= DPLLB_MODE_DAC_SERIAL;
  6473. dpll |= (crtc_state->pixel_multiplier - 1)
  6474. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6475. if (is_sdvo)
  6476. dpll |= DPLL_SDVO_HIGH_SPEED;
  6477. if (crtc_state->has_dp_encoder)
  6478. dpll |= DPLL_SDVO_HIGH_SPEED;
  6479. /* compute bitmask from p1 value */
  6480. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6481. /* also FPA1 */
  6482. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6483. switch (crtc_state->dpll.p2) {
  6484. case 5:
  6485. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6486. break;
  6487. case 7:
  6488. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6489. break;
  6490. case 10:
  6491. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6492. break;
  6493. case 14:
  6494. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6495. break;
  6496. }
  6497. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6498. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6499. else
  6500. dpll |= PLL_REF_INPUT_DREFCLK;
  6501. return dpll | DPLL_VCO_ENABLE;
  6502. }
  6503. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6504. struct intel_crtc_state *crtc_state)
  6505. {
  6506. struct drm_device *dev = crtc->base.dev;
  6507. intel_clock_t clock, reduced_clock;
  6508. u32 dpll = 0, fp = 0, fp2 = 0;
  6509. bool ok, has_reduced_clock = false;
  6510. bool is_lvds = false;
  6511. struct intel_shared_dpll *pll;
  6512. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6513. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6514. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6515. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  6516. &has_reduced_clock, &reduced_clock);
  6517. if (!ok && !crtc_state->clock_set) {
  6518. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6519. return -EINVAL;
  6520. }
  6521. /* Compat-code for transition, will disappear. */
  6522. if (!crtc_state->clock_set) {
  6523. crtc_state->dpll.n = clock.n;
  6524. crtc_state->dpll.m1 = clock.m1;
  6525. crtc_state->dpll.m2 = clock.m2;
  6526. crtc_state->dpll.p1 = clock.p1;
  6527. crtc_state->dpll.p2 = clock.p2;
  6528. }
  6529. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6530. if (crtc_state->has_pch_encoder) {
  6531. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6532. if (has_reduced_clock)
  6533. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6534. dpll = ironlake_compute_dpll(crtc, crtc_state,
  6535. &fp, &reduced_clock,
  6536. has_reduced_clock ? &fp2 : NULL);
  6537. crtc_state->dpll_hw_state.dpll = dpll;
  6538. crtc_state->dpll_hw_state.fp0 = fp;
  6539. if (has_reduced_clock)
  6540. crtc_state->dpll_hw_state.fp1 = fp2;
  6541. else
  6542. crtc_state->dpll_hw_state.fp1 = fp;
  6543. pll = intel_get_shared_dpll(crtc, crtc_state);
  6544. if (pll == NULL) {
  6545. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6546. pipe_name(crtc->pipe));
  6547. return -EINVAL;
  6548. }
  6549. }
  6550. if (is_lvds && has_reduced_clock)
  6551. crtc->lowfreq_avail = true;
  6552. else
  6553. crtc->lowfreq_avail = false;
  6554. return 0;
  6555. }
  6556. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6557. struct intel_link_m_n *m_n)
  6558. {
  6559. struct drm_device *dev = crtc->base.dev;
  6560. struct drm_i915_private *dev_priv = dev->dev_private;
  6561. enum pipe pipe = crtc->pipe;
  6562. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6563. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6564. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6565. & ~TU_SIZE_MASK;
  6566. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6567. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6568. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6569. }
  6570. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6571. enum transcoder transcoder,
  6572. struct intel_link_m_n *m_n,
  6573. struct intel_link_m_n *m2_n2)
  6574. {
  6575. struct drm_device *dev = crtc->base.dev;
  6576. struct drm_i915_private *dev_priv = dev->dev_private;
  6577. enum pipe pipe = crtc->pipe;
  6578. if (INTEL_INFO(dev)->gen >= 5) {
  6579. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6580. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6581. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6582. & ~TU_SIZE_MASK;
  6583. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6584. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6585. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6586. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6587. * gen < 8) and if DRRS is supported (to make sure the
  6588. * registers are not unnecessarily read).
  6589. */
  6590. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6591. crtc->config->has_drrs) {
  6592. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6593. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6594. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6595. & ~TU_SIZE_MASK;
  6596. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6597. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6598. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6599. }
  6600. } else {
  6601. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6602. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6603. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6604. & ~TU_SIZE_MASK;
  6605. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6606. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6607. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6608. }
  6609. }
  6610. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6611. struct intel_crtc_state *pipe_config)
  6612. {
  6613. if (pipe_config->has_pch_encoder)
  6614. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6615. else
  6616. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6617. &pipe_config->dp_m_n,
  6618. &pipe_config->dp_m2_n2);
  6619. }
  6620. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6621. struct intel_crtc_state *pipe_config)
  6622. {
  6623. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6624. &pipe_config->fdi_m_n, NULL);
  6625. }
  6626. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6627. struct intel_crtc_state *pipe_config)
  6628. {
  6629. struct drm_device *dev = crtc->base.dev;
  6630. struct drm_i915_private *dev_priv = dev->dev_private;
  6631. uint32_t tmp;
  6632. tmp = I915_READ(PS_CTL(crtc->pipe));
  6633. if (tmp & PS_ENABLE) {
  6634. pipe_config->pch_pfit.enabled = true;
  6635. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6636. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6637. }
  6638. }
  6639. static void
  6640. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6641. struct intel_initial_plane_config *plane_config)
  6642. {
  6643. struct drm_device *dev = crtc->base.dev;
  6644. struct drm_i915_private *dev_priv = dev->dev_private;
  6645. u32 val, base, offset, stride_mult, tiling;
  6646. int pipe = crtc->pipe;
  6647. int fourcc, pixel_format;
  6648. unsigned int aligned_height;
  6649. struct drm_framebuffer *fb;
  6650. struct intel_framebuffer *intel_fb;
  6651. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6652. if (!intel_fb) {
  6653. DRM_DEBUG_KMS("failed to alloc fb\n");
  6654. return;
  6655. }
  6656. fb = &intel_fb->base;
  6657. val = I915_READ(PLANE_CTL(pipe, 0));
  6658. if (!(val & PLANE_CTL_ENABLE))
  6659. goto error;
  6660. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  6661. fourcc = skl_format_to_fourcc(pixel_format,
  6662. val & PLANE_CTL_ORDER_RGBX,
  6663. val & PLANE_CTL_ALPHA_MASK);
  6664. fb->pixel_format = fourcc;
  6665. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6666. tiling = val & PLANE_CTL_TILED_MASK;
  6667. switch (tiling) {
  6668. case PLANE_CTL_TILED_LINEAR:
  6669. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  6670. break;
  6671. case PLANE_CTL_TILED_X:
  6672. plane_config->tiling = I915_TILING_X;
  6673. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6674. break;
  6675. case PLANE_CTL_TILED_Y:
  6676. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  6677. break;
  6678. case PLANE_CTL_TILED_YF:
  6679. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  6680. break;
  6681. default:
  6682. MISSING_CASE(tiling);
  6683. goto error;
  6684. }
  6685. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  6686. plane_config->base = base;
  6687. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  6688. val = I915_READ(PLANE_SIZE(pipe, 0));
  6689. fb->height = ((val >> 16) & 0xfff) + 1;
  6690. fb->width = ((val >> 0) & 0x1fff) + 1;
  6691. val = I915_READ(PLANE_STRIDE(pipe, 0));
  6692. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  6693. fb->pixel_format);
  6694. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  6695. aligned_height = intel_fb_align_height(dev, fb->height,
  6696. fb->pixel_format,
  6697. fb->modifier[0]);
  6698. plane_config->size = fb->pitches[0] * aligned_height;
  6699. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6700. pipe_name(pipe), fb->width, fb->height,
  6701. fb->bits_per_pixel, base, fb->pitches[0],
  6702. plane_config->size);
  6703. plane_config->fb = intel_fb;
  6704. return;
  6705. error:
  6706. kfree(fb);
  6707. }
  6708. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6709. struct intel_crtc_state *pipe_config)
  6710. {
  6711. struct drm_device *dev = crtc->base.dev;
  6712. struct drm_i915_private *dev_priv = dev->dev_private;
  6713. uint32_t tmp;
  6714. tmp = I915_READ(PF_CTL(crtc->pipe));
  6715. if (tmp & PF_ENABLE) {
  6716. pipe_config->pch_pfit.enabled = true;
  6717. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6718. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6719. /* We currently do not free assignements of panel fitters on
  6720. * ivb/hsw (since we don't use the higher upscaling modes which
  6721. * differentiates them) so just WARN about this case for now. */
  6722. if (IS_GEN7(dev)) {
  6723. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6724. PF_PIPE_SEL_IVB(crtc->pipe));
  6725. }
  6726. }
  6727. }
  6728. static void
  6729. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  6730. struct intel_initial_plane_config *plane_config)
  6731. {
  6732. struct drm_device *dev = crtc->base.dev;
  6733. struct drm_i915_private *dev_priv = dev->dev_private;
  6734. u32 val, base, offset;
  6735. int pipe = crtc->pipe;
  6736. int fourcc, pixel_format;
  6737. unsigned int aligned_height;
  6738. struct drm_framebuffer *fb;
  6739. struct intel_framebuffer *intel_fb;
  6740. val = I915_READ(DSPCNTR(pipe));
  6741. if (!(val & DISPLAY_PLANE_ENABLE))
  6742. return;
  6743. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6744. if (!intel_fb) {
  6745. DRM_DEBUG_KMS("failed to alloc fb\n");
  6746. return;
  6747. }
  6748. fb = &intel_fb->base;
  6749. if (INTEL_INFO(dev)->gen >= 4) {
  6750. if (val & DISPPLANE_TILED) {
  6751. plane_config->tiling = I915_TILING_X;
  6752. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6753. }
  6754. }
  6755. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6756. fourcc = i9xx_format_to_fourcc(pixel_format);
  6757. fb->pixel_format = fourcc;
  6758. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6759. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  6760. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6761. offset = I915_READ(DSPOFFSET(pipe));
  6762. } else {
  6763. if (plane_config->tiling)
  6764. offset = I915_READ(DSPTILEOFF(pipe));
  6765. else
  6766. offset = I915_READ(DSPLINOFF(pipe));
  6767. }
  6768. plane_config->base = base;
  6769. val = I915_READ(PIPESRC(pipe));
  6770. fb->width = ((val >> 16) & 0xfff) + 1;
  6771. fb->height = ((val >> 0) & 0xfff) + 1;
  6772. val = I915_READ(DSPSTRIDE(pipe));
  6773. fb->pitches[0] = val & 0xffffffc0;
  6774. aligned_height = intel_fb_align_height(dev, fb->height,
  6775. fb->pixel_format,
  6776. fb->modifier[0]);
  6777. plane_config->size = fb->pitches[0] * aligned_height;
  6778. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6779. pipe_name(pipe), fb->width, fb->height,
  6780. fb->bits_per_pixel, base, fb->pitches[0],
  6781. plane_config->size);
  6782. plane_config->fb = intel_fb;
  6783. }
  6784. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6785. struct intel_crtc_state *pipe_config)
  6786. {
  6787. struct drm_device *dev = crtc->base.dev;
  6788. struct drm_i915_private *dev_priv = dev->dev_private;
  6789. uint32_t tmp;
  6790. if (!intel_display_power_is_enabled(dev_priv,
  6791. POWER_DOMAIN_PIPE(crtc->pipe)))
  6792. return false;
  6793. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6794. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6795. tmp = I915_READ(PIPECONF(crtc->pipe));
  6796. if (!(tmp & PIPECONF_ENABLE))
  6797. return false;
  6798. switch (tmp & PIPECONF_BPC_MASK) {
  6799. case PIPECONF_6BPC:
  6800. pipe_config->pipe_bpp = 18;
  6801. break;
  6802. case PIPECONF_8BPC:
  6803. pipe_config->pipe_bpp = 24;
  6804. break;
  6805. case PIPECONF_10BPC:
  6806. pipe_config->pipe_bpp = 30;
  6807. break;
  6808. case PIPECONF_12BPC:
  6809. pipe_config->pipe_bpp = 36;
  6810. break;
  6811. default:
  6812. break;
  6813. }
  6814. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6815. pipe_config->limited_color_range = true;
  6816. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6817. struct intel_shared_dpll *pll;
  6818. pipe_config->has_pch_encoder = true;
  6819. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6820. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6821. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6822. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6823. if (HAS_PCH_IBX(dev_priv->dev)) {
  6824. pipe_config->shared_dpll =
  6825. (enum intel_dpll_id) crtc->pipe;
  6826. } else {
  6827. tmp = I915_READ(PCH_DPLL_SEL);
  6828. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6829. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6830. else
  6831. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6832. }
  6833. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6834. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6835. &pipe_config->dpll_hw_state));
  6836. tmp = pipe_config->dpll_hw_state.dpll;
  6837. pipe_config->pixel_multiplier =
  6838. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6839. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6840. ironlake_pch_clock_get(crtc, pipe_config);
  6841. } else {
  6842. pipe_config->pixel_multiplier = 1;
  6843. }
  6844. intel_get_pipe_timings(crtc, pipe_config);
  6845. ironlake_get_pfit_config(crtc, pipe_config);
  6846. return true;
  6847. }
  6848. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6849. {
  6850. struct drm_device *dev = dev_priv->dev;
  6851. struct intel_crtc *crtc;
  6852. for_each_intel_crtc(dev, crtc)
  6853. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6854. pipe_name(crtc->pipe));
  6855. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6856. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6857. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6858. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6859. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6860. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6861. "CPU PWM1 enabled\n");
  6862. if (IS_HASWELL(dev))
  6863. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6864. "CPU PWM2 enabled\n");
  6865. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6866. "PCH PWM1 enabled\n");
  6867. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6868. "Utility pin enabled\n");
  6869. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6870. /*
  6871. * In theory we can still leave IRQs enabled, as long as only the HPD
  6872. * interrupts remain enabled. We used to check for that, but since it's
  6873. * gen-specific and since we only disable LCPLL after we fully disable
  6874. * the interrupts, the check below should be enough.
  6875. */
  6876. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6877. }
  6878. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6879. {
  6880. struct drm_device *dev = dev_priv->dev;
  6881. if (IS_HASWELL(dev))
  6882. return I915_READ(D_COMP_HSW);
  6883. else
  6884. return I915_READ(D_COMP_BDW);
  6885. }
  6886. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6887. {
  6888. struct drm_device *dev = dev_priv->dev;
  6889. if (IS_HASWELL(dev)) {
  6890. mutex_lock(&dev_priv->rps.hw_lock);
  6891. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6892. val))
  6893. DRM_ERROR("Failed to write to D_COMP\n");
  6894. mutex_unlock(&dev_priv->rps.hw_lock);
  6895. } else {
  6896. I915_WRITE(D_COMP_BDW, val);
  6897. POSTING_READ(D_COMP_BDW);
  6898. }
  6899. }
  6900. /*
  6901. * This function implements pieces of two sequences from BSpec:
  6902. * - Sequence for display software to disable LCPLL
  6903. * - Sequence for display software to allow package C8+
  6904. * The steps implemented here are just the steps that actually touch the LCPLL
  6905. * register. Callers should take care of disabling all the display engine
  6906. * functions, doing the mode unset, fixing interrupts, etc.
  6907. */
  6908. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6909. bool switch_to_fclk, bool allow_power_down)
  6910. {
  6911. uint32_t val;
  6912. assert_can_disable_lcpll(dev_priv);
  6913. val = I915_READ(LCPLL_CTL);
  6914. if (switch_to_fclk) {
  6915. val |= LCPLL_CD_SOURCE_FCLK;
  6916. I915_WRITE(LCPLL_CTL, val);
  6917. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6918. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6919. DRM_ERROR("Switching to FCLK failed\n");
  6920. val = I915_READ(LCPLL_CTL);
  6921. }
  6922. val |= LCPLL_PLL_DISABLE;
  6923. I915_WRITE(LCPLL_CTL, val);
  6924. POSTING_READ(LCPLL_CTL);
  6925. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6926. DRM_ERROR("LCPLL still locked\n");
  6927. val = hsw_read_dcomp(dev_priv);
  6928. val |= D_COMP_COMP_DISABLE;
  6929. hsw_write_dcomp(dev_priv, val);
  6930. ndelay(100);
  6931. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6932. 1))
  6933. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6934. if (allow_power_down) {
  6935. val = I915_READ(LCPLL_CTL);
  6936. val |= LCPLL_POWER_DOWN_ALLOW;
  6937. I915_WRITE(LCPLL_CTL, val);
  6938. POSTING_READ(LCPLL_CTL);
  6939. }
  6940. }
  6941. /*
  6942. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6943. * source.
  6944. */
  6945. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6946. {
  6947. uint32_t val;
  6948. val = I915_READ(LCPLL_CTL);
  6949. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6950. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6951. return;
  6952. /*
  6953. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6954. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6955. */
  6956. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6957. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6958. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6959. I915_WRITE(LCPLL_CTL, val);
  6960. POSTING_READ(LCPLL_CTL);
  6961. }
  6962. val = hsw_read_dcomp(dev_priv);
  6963. val |= D_COMP_COMP_FORCE;
  6964. val &= ~D_COMP_COMP_DISABLE;
  6965. hsw_write_dcomp(dev_priv, val);
  6966. val = I915_READ(LCPLL_CTL);
  6967. val &= ~LCPLL_PLL_DISABLE;
  6968. I915_WRITE(LCPLL_CTL, val);
  6969. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6970. DRM_ERROR("LCPLL not locked yet\n");
  6971. if (val & LCPLL_CD_SOURCE_FCLK) {
  6972. val = I915_READ(LCPLL_CTL);
  6973. val &= ~LCPLL_CD_SOURCE_FCLK;
  6974. I915_WRITE(LCPLL_CTL, val);
  6975. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6976. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6977. DRM_ERROR("Switching back to LCPLL failed\n");
  6978. }
  6979. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6980. }
  6981. /*
  6982. * Package states C8 and deeper are really deep PC states that can only be
  6983. * reached when all the devices on the system allow it, so even if the graphics
  6984. * device allows PC8+, it doesn't mean the system will actually get to these
  6985. * states. Our driver only allows PC8+ when going into runtime PM.
  6986. *
  6987. * The requirements for PC8+ are that all the outputs are disabled, the power
  6988. * well is disabled and most interrupts are disabled, and these are also
  6989. * requirements for runtime PM. When these conditions are met, we manually do
  6990. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6991. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6992. * hang the machine.
  6993. *
  6994. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6995. * the state of some registers, so when we come back from PC8+ we need to
  6996. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6997. * need to take care of the registers kept by RC6. Notice that this happens even
  6998. * if we don't put the device in PCI D3 state (which is what currently happens
  6999. * because of the runtime PM support).
  7000. *
  7001. * For more, read "Display Sequences for Package C8" on the hardware
  7002. * documentation.
  7003. */
  7004. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7005. {
  7006. struct drm_device *dev = dev_priv->dev;
  7007. uint32_t val;
  7008. DRM_DEBUG_KMS("Enabling package C8+\n");
  7009. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7010. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7011. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7012. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7013. }
  7014. lpt_disable_clkout_dp(dev);
  7015. hsw_disable_lcpll(dev_priv, true, true);
  7016. }
  7017. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7018. {
  7019. struct drm_device *dev = dev_priv->dev;
  7020. uint32_t val;
  7021. DRM_DEBUG_KMS("Disabling package C8+\n");
  7022. hsw_restore_lcpll(dev_priv);
  7023. lpt_init_pch_refclk(dev);
  7024. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7025. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7026. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7027. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7028. }
  7029. intel_prepare_ddi(dev);
  7030. }
  7031. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7032. struct intel_crtc_state *crtc_state)
  7033. {
  7034. if (!intel_ddi_pll_select(crtc, crtc_state))
  7035. return -EINVAL;
  7036. crtc->lowfreq_avail = false;
  7037. return 0;
  7038. }
  7039. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7040. enum port port,
  7041. struct intel_crtc_state *pipe_config)
  7042. {
  7043. u32 temp, dpll_ctl1;
  7044. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7045. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  7046. switch (pipe_config->ddi_pll_sel) {
  7047. case SKL_DPLL0:
  7048. /*
  7049. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  7050. * of the shared DPLL framework and thus needs to be read out
  7051. * separately
  7052. */
  7053. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  7054. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  7055. break;
  7056. case SKL_DPLL1:
  7057. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  7058. break;
  7059. case SKL_DPLL2:
  7060. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  7061. break;
  7062. case SKL_DPLL3:
  7063. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  7064. break;
  7065. }
  7066. }
  7067. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7068. enum port port,
  7069. struct intel_crtc_state *pipe_config)
  7070. {
  7071. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7072. switch (pipe_config->ddi_pll_sel) {
  7073. case PORT_CLK_SEL_WRPLL1:
  7074. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  7075. break;
  7076. case PORT_CLK_SEL_WRPLL2:
  7077. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  7078. break;
  7079. }
  7080. }
  7081. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7082. struct intel_crtc_state *pipe_config)
  7083. {
  7084. struct drm_device *dev = crtc->base.dev;
  7085. struct drm_i915_private *dev_priv = dev->dev_private;
  7086. struct intel_shared_dpll *pll;
  7087. enum port port;
  7088. uint32_t tmp;
  7089. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7090. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7091. if (IS_SKYLAKE(dev))
  7092. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7093. else
  7094. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7095. if (pipe_config->shared_dpll >= 0) {
  7096. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7097. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7098. &pipe_config->dpll_hw_state));
  7099. }
  7100. /*
  7101. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7102. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7103. * the PCH transcoder is on.
  7104. */
  7105. if (INTEL_INFO(dev)->gen < 9 &&
  7106. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7107. pipe_config->has_pch_encoder = true;
  7108. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7109. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7110. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7111. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7112. }
  7113. }
  7114. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7115. struct intel_crtc_state *pipe_config)
  7116. {
  7117. struct drm_device *dev = crtc->base.dev;
  7118. struct drm_i915_private *dev_priv = dev->dev_private;
  7119. enum intel_display_power_domain pfit_domain;
  7120. uint32_t tmp;
  7121. if (!intel_display_power_is_enabled(dev_priv,
  7122. POWER_DOMAIN_PIPE(crtc->pipe)))
  7123. return false;
  7124. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7125. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7126. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7127. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7128. enum pipe trans_edp_pipe;
  7129. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7130. default:
  7131. WARN(1, "unknown pipe linked to edp transcoder\n");
  7132. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7133. case TRANS_DDI_EDP_INPUT_A_ON:
  7134. trans_edp_pipe = PIPE_A;
  7135. break;
  7136. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7137. trans_edp_pipe = PIPE_B;
  7138. break;
  7139. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7140. trans_edp_pipe = PIPE_C;
  7141. break;
  7142. }
  7143. if (trans_edp_pipe == crtc->pipe)
  7144. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7145. }
  7146. if (!intel_display_power_is_enabled(dev_priv,
  7147. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  7148. return false;
  7149. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7150. if (!(tmp & PIPECONF_ENABLE))
  7151. return false;
  7152. haswell_get_ddi_port_state(crtc, pipe_config);
  7153. intel_get_pipe_timings(crtc, pipe_config);
  7154. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7155. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  7156. if (IS_SKYLAKE(dev))
  7157. skylake_get_pfit_config(crtc, pipe_config);
  7158. else
  7159. ironlake_get_pfit_config(crtc, pipe_config);
  7160. }
  7161. if (IS_HASWELL(dev))
  7162. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7163. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7164. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  7165. pipe_config->pixel_multiplier =
  7166. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7167. } else {
  7168. pipe_config->pixel_multiplier = 1;
  7169. }
  7170. return true;
  7171. }
  7172. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  7173. {
  7174. struct drm_device *dev = crtc->dev;
  7175. struct drm_i915_private *dev_priv = dev->dev_private;
  7176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7177. uint32_t cntl = 0, size = 0;
  7178. if (base) {
  7179. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  7180. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  7181. unsigned int stride = roundup_pow_of_two(width) * 4;
  7182. switch (stride) {
  7183. default:
  7184. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7185. width, stride);
  7186. stride = 256;
  7187. /* fallthrough */
  7188. case 256:
  7189. case 512:
  7190. case 1024:
  7191. case 2048:
  7192. break;
  7193. }
  7194. cntl |= CURSOR_ENABLE |
  7195. CURSOR_GAMMA_ENABLE |
  7196. CURSOR_FORMAT_ARGB |
  7197. CURSOR_STRIDE(stride);
  7198. size = (height << 12) | width;
  7199. }
  7200. if (intel_crtc->cursor_cntl != 0 &&
  7201. (intel_crtc->cursor_base != base ||
  7202. intel_crtc->cursor_size != size ||
  7203. intel_crtc->cursor_cntl != cntl)) {
  7204. /* On these chipsets we can only modify the base/size/stride
  7205. * whilst the cursor is disabled.
  7206. */
  7207. I915_WRITE(_CURACNTR, 0);
  7208. POSTING_READ(_CURACNTR);
  7209. intel_crtc->cursor_cntl = 0;
  7210. }
  7211. if (intel_crtc->cursor_base != base) {
  7212. I915_WRITE(_CURABASE, base);
  7213. intel_crtc->cursor_base = base;
  7214. }
  7215. if (intel_crtc->cursor_size != size) {
  7216. I915_WRITE(CURSIZE, size);
  7217. intel_crtc->cursor_size = size;
  7218. }
  7219. if (intel_crtc->cursor_cntl != cntl) {
  7220. I915_WRITE(_CURACNTR, cntl);
  7221. POSTING_READ(_CURACNTR);
  7222. intel_crtc->cursor_cntl = cntl;
  7223. }
  7224. }
  7225. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  7226. {
  7227. struct drm_device *dev = crtc->dev;
  7228. struct drm_i915_private *dev_priv = dev->dev_private;
  7229. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7230. int pipe = intel_crtc->pipe;
  7231. uint32_t cntl;
  7232. cntl = 0;
  7233. if (base) {
  7234. cntl = MCURSOR_GAMMA_ENABLE;
  7235. switch (intel_crtc->base.cursor->state->crtc_w) {
  7236. case 64:
  7237. cntl |= CURSOR_MODE_64_ARGB_AX;
  7238. break;
  7239. case 128:
  7240. cntl |= CURSOR_MODE_128_ARGB_AX;
  7241. break;
  7242. case 256:
  7243. cntl |= CURSOR_MODE_256_ARGB_AX;
  7244. break;
  7245. default:
  7246. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  7247. return;
  7248. }
  7249. cntl |= pipe << 28; /* Connect to correct pipe */
  7250. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  7251. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7252. }
  7253. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  7254. cntl |= CURSOR_ROTATE_180;
  7255. if (intel_crtc->cursor_cntl != cntl) {
  7256. I915_WRITE(CURCNTR(pipe), cntl);
  7257. POSTING_READ(CURCNTR(pipe));
  7258. intel_crtc->cursor_cntl = cntl;
  7259. }
  7260. /* and commit changes on next vblank */
  7261. I915_WRITE(CURBASE(pipe), base);
  7262. POSTING_READ(CURBASE(pipe));
  7263. intel_crtc->cursor_base = base;
  7264. }
  7265. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7266. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7267. bool on)
  7268. {
  7269. struct drm_device *dev = crtc->dev;
  7270. struct drm_i915_private *dev_priv = dev->dev_private;
  7271. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7272. int pipe = intel_crtc->pipe;
  7273. int x = crtc->cursor_x;
  7274. int y = crtc->cursor_y;
  7275. u32 base = 0, pos = 0;
  7276. if (on)
  7277. base = intel_crtc->cursor_addr;
  7278. if (x >= intel_crtc->config->pipe_src_w)
  7279. base = 0;
  7280. if (y >= intel_crtc->config->pipe_src_h)
  7281. base = 0;
  7282. if (x < 0) {
  7283. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  7284. base = 0;
  7285. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7286. x = -x;
  7287. }
  7288. pos |= x << CURSOR_X_SHIFT;
  7289. if (y < 0) {
  7290. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  7291. base = 0;
  7292. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7293. y = -y;
  7294. }
  7295. pos |= y << CURSOR_Y_SHIFT;
  7296. if (base == 0 && intel_crtc->cursor_base == 0)
  7297. return;
  7298. I915_WRITE(CURPOS(pipe), pos);
  7299. /* ILK+ do this automagically */
  7300. if (HAS_GMCH_DISPLAY(dev) &&
  7301. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  7302. base += (intel_crtc->base.cursor->state->crtc_h *
  7303. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  7304. }
  7305. if (IS_845G(dev) || IS_I865G(dev))
  7306. i845_update_cursor(crtc, base);
  7307. else
  7308. i9xx_update_cursor(crtc, base);
  7309. }
  7310. static bool cursor_size_ok(struct drm_device *dev,
  7311. uint32_t width, uint32_t height)
  7312. {
  7313. if (width == 0 || height == 0)
  7314. return false;
  7315. /*
  7316. * 845g/865g are special in that they are only limited by
  7317. * the width of their cursors, the height is arbitrary up to
  7318. * the precision of the register. Everything else requires
  7319. * square cursors, limited to a few power-of-two sizes.
  7320. */
  7321. if (IS_845G(dev) || IS_I865G(dev)) {
  7322. if ((width & 63) != 0)
  7323. return false;
  7324. if (width > (IS_845G(dev) ? 64 : 512))
  7325. return false;
  7326. if (height > 1023)
  7327. return false;
  7328. } else {
  7329. switch (width | height) {
  7330. case 256:
  7331. case 128:
  7332. if (IS_GEN2(dev))
  7333. return false;
  7334. case 64:
  7335. break;
  7336. default:
  7337. return false;
  7338. }
  7339. }
  7340. return true;
  7341. }
  7342. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7343. u16 *blue, uint32_t start, uint32_t size)
  7344. {
  7345. int end = (start + size > 256) ? 256 : start + size, i;
  7346. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7347. for (i = start; i < end; i++) {
  7348. intel_crtc->lut_r[i] = red[i] >> 8;
  7349. intel_crtc->lut_g[i] = green[i] >> 8;
  7350. intel_crtc->lut_b[i] = blue[i] >> 8;
  7351. }
  7352. intel_crtc_load_lut(crtc);
  7353. }
  7354. /* VESA 640x480x72Hz mode to set on the pipe */
  7355. static struct drm_display_mode load_detect_mode = {
  7356. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7357. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7358. };
  7359. struct drm_framebuffer *
  7360. __intel_framebuffer_create(struct drm_device *dev,
  7361. struct drm_mode_fb_cmd2 *mode_cmd,
  7362. struct drm_i915_gem_object *obj)
  7363. {
  7364. struct intel_framebuffer *intel_fb;
  7365. int ret;
  7366. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7367. if (!intel_fb) {
  7368. drm_gem_object_unreference(&obj->base);
  7369. return ERR_PTR(-ENOMEM);
  7370. }
  7371. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7372. if (ret)
  7373. goto err;
  7374. return &intel_fb->base;
  7375. err:
  7376. drm_gem_object_unreference(&obj->base);
  7377. kfree(intel_fb);
  7378. return ERR_PTR(ret);
  7379. }
  7380. static struct drm_framebuffer *
  7381. intel_framebuffer_create(struct drm_device *dev,
  7382. struct drm_mode_fb_cmd2 *mode_cmd,
  7383. struct drm_i915_gem_object *obj)
  7384. {
  7385. struct drm_framebuffer *fb;
  7386. int ret;
  7387. ret = i915_mutex_lock_interruptible(dev);
  7388. if (ret)
  7389. return ERR_PTR(ret);
  7390. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7391. mutex_unlock(&dev->struct_mutex);
  7392. return fb;
  7393. }
  7394. static u32
  7395. intel_framebuffer_pitch_for_width(int width, int bpp)
  7396. {
  7397. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7398. return ALIGN(pitch, 64);
  7399. }
  7400. static u32
  7401. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7402. {
  7403. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7404. return PAGE_ALIGN(pitch * mode->vdisplay);
  7405. }
  7406. static struct drm_framebuffer *
  7407. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7408. struct drm_display_mode *mode,
  7409. int depth, int bpp)
  7410. {
  7411. struct drm_i915_gem_object *obj;
  7412. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7413. obj = i915_gem_alloc_object(dev,
  7414. intel_framebuffer_size_for_mode(mode, bpp));
  7415. if (obj == NULL)
  7416. return ERR_PTR(-ENOMEM);
  7417. mode_cmd.width = mode->hdisplay;
  7418. mode_cmd.height = mode->vdisplay;
  7419. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7420. bpp);
  7421. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7422. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7423. }
  7424. static struct drm_framebuffer *
  7425. mode_fits_in_fbdev(struct drm_device *dev,
  7426. struct drm_display_mode *mode)
  7427. {
  7428. #ifdef CONFIG_DRM_I915_FBDEV
  7429. struct drm_i915_private *dev_priv = dev->dev_private;
  7430. struct drm_i915_gem_object *obj;
  7431. struct drm_framebuffer *fb;
  7432. if (!dev_priv->fbdev)
  7433. return NULL;
  7434. if (!dev_priv->fbdev->fb)
  7435. return NULL;
  7436. obj = dev_priv->fbdev->fb->obj;
  7437. BUG_ON(!obj);
  7438. fb = &dev_priv->fbdev->fb->base;
  7439. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7440. fb->bits_per_pixel))
  7441. return NULL;
  7442. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7443. return NULL;
  7444. return fb;
  7445. #else
  7446. return NULL;
  7447. #endif
  7448. }
  7449. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7450. struct drm_display_mode *mode,
  7451. struct intel_load_detect_pipe *old,
  7452. struct drm_modeset_acquire_ctx *ctx)
  7453. {
  7454. struct intel_crtc *intel_crtc;
  7455. struct intel_encoder *intel_encoder =
  7456. intel_attached_encoder(connector);
  7457. struct drm_crtc *possible_crtc;
  7458. struct drm_encoder *encoder = &intel_encoder->base;
  7459. struct drm_crtc *crtc = NULL;
  7460. struct drm_device *dev = encoder->dev;
  7461. struct drm_framebuffer *fb;
  7462. struct drm_mode_config *config = &dev->mode_config;
  7463. int ret, i = -1;
  7464. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7465. connector->base.id, connector->name,
  7466. encoder->base.id, encoder->name);
  7467. retry:
  7468. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7469. if (ret)
  7470. goto fail_unlock;
  7471. /*
  7472. * Algorithm gets a little messy:
  7473. *
  7474. * - if the connector already has an assigned crtc, use it (but make
  7475. * sure it's on first)
  7476. *
  7477. * - try to find the first unused crtc that can drive this connector,
  7478. * and use that if we find one
  7479. */
  7480. /* See if we already have a CRTC for this connector */
  7481. if (encoder->crtc) {
  7482. crtc = encoder->crtc;
  7483. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7484. if (ret)
  7485. goto fail_unlock;
  7486. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7487. if (ret)
  7488. goto fail_unlock;
  7489. old->dpms_mode = connector->dpms;
  7490. old->load_detect_temp = false;
  7491. /* Make sure the crtc and connector are running */
  7492. if (connector->dpms != DRM_MODE_DPMS_ON)
  7493. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7494. return true;
  7495. }
  7496. /* Find an unused one (if possible) */
  7497. for_each_crtc(dev, possible_crtc) {
  7498. i++;
  7499. if (!(encoder->possible_crtcs & (1 << i)))
  7500. continue;
  7501. if (possible_crtc->state->enable)
  7502. continue;
  7503. /* This can occur when applying the pipe A quirk on resume. */
  7504. if (to_intel_crtc(possible_crtc)->new_enabled)
  7505. continue;
  7506. crtc = possible_crtc;
  7507. break;
  7508. }
  7509. /*
  7510. * If we didn't find an unused CRTC, don't use any.
  7511. */
  7512. if (!crtc) {
  7513. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7514. goto fail_unlock;
  7515. }
  7516. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7517. if (ret)
  7518. goto fail_unlock;
  7519. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7520. if (ret)
  7521. goto fail_unlock;
  7522. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7523. to_intel_connector(connector)->new_encoder = intel_encoder;
  7524. intel_crtc = to_intel_crtc(crtc);
  7525. intel_crtc->new_enabled = true;
  7526. intel_crtc->new_config = intel_crtc->config;
  7527. old->dpms_mode = connector->dpms;
  7528. old->load_detect_temp = true;
  7529. old->release_fb = NULL;
  7530. if (!mode)
  7531. mode = &load_detect_mode;
  7532. /* We need a framebuffer large enough to accommodate all accesses
  7533. * that the plane may generate whilst we perform load detection.
  7534. * We can not rely on the fbcon either being present (we get called
  7535. * during its initialisation to detect all boot displays, or it may
  7536. * not even exist) or that it is large enough to satisfy the
  7537. * requested mode.
  7538. */
  7539. fb = mode_fits_in_fbdev(dev, mode);
  7540. if (fb == NULL) {
  7541. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7542. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7543. old->release_fb = fb;
  7544. } else
  7545. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7546. if (IS_ERR(fb)) {
  7547. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7548. goto fail;
  7549. }
  7550. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7551. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7552. if (old->release_fb)
  7553. old->release_fb->funcs->destroy(old->release_fb);
  7554. goto fail;
  7555. }
  7556. crtc->primary->crtc = crtc;
  7557. /* let the connector get through one full cycle before testing */
  7558. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7559. return true;
  7560. fail:
  7561. intel_crtc->new_enabled = crtc->state->enable;
  7562. if (intel_crtc->new_enabled)
  7563. intel_crtc->new_config = intel_crtc->config;
  7564. else
  7565. intel_crtc->new_config = NULL;
  7566. fail_unlock:
  7567. if (ret == -EDEADLK) {
  7568. drm_modeset_backoff(ctx);
  7569. goto retry;
  7570. }
  7571. return false;
  7572. }
  7573. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7574. struct intel_load_detect_pipe *old)
  7575. {
  7576. struct intel_encoder *intel_encoder =
  7577. intel_attached_encoder(connector);
  7578. struct drm_encoder *encoder = &intel_encoder->base;
  7579. struct drm_crtc *crtc = encoder->crtc;
  7580. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7581. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7582. connector->base.id, connector->name,
  7583. encoder->base.id, encoder->name);
  7584. if (old->load_detect_temp) {
  7585. to_intel_connector(connector)->new_encoder = NULL;
  7586. intel_encoder->new_crtc = NULL;
  7587. intel_crtc->new_enabled = false;
  7588. intel_crtc->new_config = NULL;
  7589. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7590. if (old->release_fb) {
  7591. drm_framebuffer_unregister_private(old->release_fb);
  7592. drm_framebuffer_unreference(old->release_fb);
  7593. }
  7594. return;
  7595. }
  7596. /* Switch crtc and encoder back off if necessary */
  7597. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7598. connector->funcs->dpms(connector, old->dpms_mode);
  7599. }
  7600. static int i9xx_pll_refclk(struct drm_device *dev,
  7601. const struct intel_crtc_state *pipe_config)
  7602. {
  7603. struct drm_i915_private *dev_priv = dev->dev_private;
  7604. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7605. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7606. return dev_priv->vbt.lvds_ssc_freq;
  7607. else if (HAS_PCH_SPLIT(dev))
  7608. return 120000;
  7609. else if (!IS_GEN2(dev))
  7610. return 96000;
  7611. else
  7612. return 48000;
  7613. }
  7614. /* Returns the clock of the currently programmed mode of the given pipe. */
  7615. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7616. struct intel_crtc_state *pipe_config)
  7617. {
  7618. struct drm_device *dev = crtc->base.dev;
  7619. struct drm_i915_private *dev_priv = dev->dev_private;
  7620. int pipe = pipe_config->cpu_transcoder;
  7621. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7622. u32 fp;
  7623. intel_clock_t clock;
  7624. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7625. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7626. fp = pipe_config->dpll_hw_state.fp0;
  7627. else
  7628. fp = pipe_config->dpll_hw_state.fp1;
  7629. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7630. if (IS_PINEVIEW(dev)) {
  7631. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7632. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7633. } else {
  7634. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7635. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7636. }
  7637. if (!IS_GEN2(dev)) {
  7638. if (IS_PINEVIEW(dev))
  7639. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7640. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7641. else
  7642. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7643. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7644. switch (dpll & DPLL_MODE_MASK) {
  7645. case DPLLB_MODE_DAC_SERIAL:
  7646. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7647. 5 : 10;
  7648. break;
  7649. case DPLLB_MODE_LVDS:
  7650. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7651. 7 : 14;
  7652. break;
  7653. default:
  7654. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7655. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7656. return;
  7657. }
  7658. if (IS_PINEVIEW(dev))
  7659. pineview_clock(refclk, &clock);
  7660. else
  7661. i9xx_clock(refclk, &clock);
  7662. } else {
  7663. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7664. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7665. if (is_lvds) {
  7666. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7667. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7668. if (lvds & LVDS_CLKB_POWER_UP)
  7669. clock.p2 = 7;
  7670. else
  7671. clock.p2 = 14;
  7672. } else {
  7673. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7674. clock.p1 = 2;
  7675. else {
  7676. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7677. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7678. }
  7679. if (dpll & PLL_P2_DIVIDE_BY_4)
  7680. clock.p2 = 4;
  7681. else
  7682. clock.p2 = 2;
  7683. }
  7684. i9xx_clock(refclk, &clock);
  7685. }
  7686. /*
  7687. * This value includes pixel_multiplier. We will use
  7688. * port_clock to compute adjusted_mode.crtc_clock in the
  7689. * encoder's get_config() function.
  7690. */
  7691. pipe_config->port_clock = clock.dot;
  7692. }
  7693. int intel_dotclock_calculate(int link_freq,
  7694. const struct intel_link_m_n *m_n)
  7695. {
  7696. /*
  7697. * The calculation for the data clock is:
  7698. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7699. * But we want to avoid losing precison if possible, so:
  7700. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7701. *
  7702. * and the link clock is simpler:
  7703. * link_clock = (m * link_clock) / n
  7704. */
  7705. if (!m_n->link_n)
  7706. return 0;
  7707. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7708. }
  7709. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7710. struct intel_crtc_state *pipe_config)
  7711. {
  7712. struct drm_device *dev = crtc->base.dev;
  7713. /* read out port_clock from the DPLL */
  7714. i9xx_crtc_clock_get(crtc, pipe_config);
  7715. /*
  7716. * This value does not include pixel_multiplier.
  7717. * We will check that port_clock and adjusted_mode.crtc_clock
  7718. * agree once we know their relationship in the encoder's
  7719. * get_config() function.
  7720. */
  7721. pipe_config->base.adjusted_mode.crtc_clock =
  7722. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7723. &pipe_config->fdi_m_n);
  7724. }
  7725. /** Returns the currently programmed mode of the given pipe. */
  7726. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7727. struct drm_crtc *crtc)
  7728. {
  7729. struct drm_i915_private *dev_priv = dev->dev_private;
  7730. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7731. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7732. struct drm_display_mode *mode;
  7733. struct intel_crtc_state pipe_config;
  7734. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7735. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7736. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7737. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7738. enum pipe pipe = intel_crtc->pipe;
  7739. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7740. if (!mode)
  7741. return NULL;
  7742. /*
  7743. * Construct a pipe_config sufficient for getting the clock info
  7744. * back out of crtc_clock_get.
  7745. *
  7746. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7747. * to use a real value here instead.
  7748. */
  7749. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7750. pipe_config.pixel_multiplier = 1;
  7751. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7752. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7753. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7754. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7755. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7756. mode->hdisplay = (htot & 0xffff) + 1;
  7757. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7758. mode->hsync_start = (hsync & 0xffff) + 1;
  7759. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7760. mode->vdisplay = (vtot & 0xffff) + 1;
  7761. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7762. mode->vsync_start = (vsync & 0xffff) + 1;
  7763. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7764. drm_mode_set_name(mode);
  7765. return mode;
  7766. }
  7767. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7768. {
  7769. struct drm_device *dev = crtc->dev;
  7770. struct drm_i915_private *dev_priv = dev->dev_private;
  7771. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7772. if (!HAS_GMCH_DISPLAY(dev))
  7773. return;
  7774. if (!dev_priv->lvds_downclock_avail)
  7775. return;
  7776. /*
  7777. * Since this is called by a timer, we should never get here in
  7778. * the manual case.
  7779. */
  7780. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7781. int pipe = intel_crtc->pipe;
  7782. int dpll_reg = DPLL(pipe);
  7783. int dpll;
  7784. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7785. assert_panel_unlocked(dev_priv, pipe);
  7786. dpll = I915_READ(dpll_reg);
  7787. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7788. I915_WRITE(dpll_reg, dpll);
  7789. intel_wait_for_vblank(dev, pipe);
  7790. dpll = I915_READ(dpll_reg);
  7791. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7792. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7793. }
  7794. }
  7795. void intel_mark_busy(struct drm_device *dev)
  7796. {
  7797. struct drm_i915_private *dev_priv = dev->dev_private;
  7798. if (dev_priv->mm.busy)
  7799. return;
  7800. intel_runtime_pm_get(dev_priv);
  7801. i915_update_gfx_val(dev_priv);
  7802. if (INTEL_INFO(dev)->gen >= 6)
  7803. gen6_rps_busy(dev_priv);
  7804. dev_priv->mm.busy = true;
  7805. }
  7806. void intel_mark_idle(struct drm_device *dev)
  7807. {
  7808. struct drm_i915_private *dev_priv = dev->dev_private;
  7809. struct drm_crtc *crtc;
  7810. if (!dev_priv->mm.busy)
  7811. return;
  7812. dev_priv->mm.busy = false;
  7813. for_each_crtc(dev, crtc) {
  7814. if (!crtc->primary->fb)
  7815. continue;
  7816. intel_decrease_pllclock(crtc);
  7817. }
  7818. if (INTEL_INFO(dev)->gen >= 6)
  7819. gen6_rps_idle(dev->dev_private);
  7820. intel_runtime_pm_put(dev_priv);
  7821. }
  7822. static void intel_crtc_set_state(struct intel_crtc *crtc,
  7823. struct intel_crtc_state *crtc_state)
  7824. {
  7825. kfree(crtc->config);
  7826. crtc->config = crtc_state;
  7827. crtc->base.state = &crtc_state->base;
  7828. }
  7829. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7830. {
  7831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7832. struct drm_device *dev = crtc->dev;
  7833. struct intel_unpin_work *work;
  7834. spin_lock_irq(&dev->event_lock);
  7835. work = intel_crtc->unpin_work;
  7836. intel_crtc->unpin_work = NULL;
  7837. spin_unlock_irq(&dev->event_lock);
  7838. if (work) {
  7839. cancel_work_sync(&work->work);
  7840. kfree(work);
  7841. }
  7842. intel_crtc_set_state(intel_crtc, NULL);
  7843. drm_crtc_cleanup(crtc);
  7844. kfree(intel_crtc);
  7845. }
  7846. static void intel_unpin_work_fn(struct work_struct *__work)
  7847. {
  7848. struct intel_unpin_work *work =
  7849. container_of(__work, struct intel_unpin_work, work);
  7850. struct drm_device *dev = work->crtc->dev;
  7851. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7852. mutex_lock(&dev->struct_mutex);
  7853. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  7854. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7855. intel_fbc_update(dev);
  7856. if (work->flip_queued_req)
  7857. i915_gem_request_assign(&work->flip_queued_req, NULL);
  7858. mutex_unlock(&dev->struct_mutex);
  7859. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7860. drm_framebuffer_unreference(work->old_fb);
  7861. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7862. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7863. kfree(work);
  7864. }
  7865. static void do_intel_finish_page_flip(struct drm_device *dev,
  7866. struct drm_crtc *crtc)
  7867. {
  7868. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7869. struct intel_unpin_work *work;
  7870. unsigned long flags;
  7871. /* Ignore early vblank irqs */
  7872. if (intel_crtc == NULL)
  7873. return;
  7874. /*
  7875. * This is called both by irq handlers and the reset code (to complete
  7876. * lost pageflips) so needs the full irqsave spinlocks.
  7877. */
  7878. spin_lock_irqsave(&dev->event_lock, flags);
  7879. work = intel_crtc->unpin_work;
  7880. /* Ensure we don't miss a work->pending update ... */
  7881. smp_rmb();
  7882. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7883. spin_unlock_irqrestore(&dev->event_lock, flags);
  7884. return;
  7885. }
  7886. page_flip_completed(intel_crtc);
  7887. spin_unlock_irqrestore(&dev->event_lock, flags);
  7888. }
  7889. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7890. {
  7891. struct drm_i915_private *dev_priv = dev->dev_private;
  7892. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7893. do_intel_finish_page_flip(dev, crtc);
  7894. }
  7895. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7896. {
  7897. struct drm_i915_private *dev_priv = dev->dev_private;
  7898. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7899. do_intel_finish_page_flip(dev, crtc);
  7900. }
  7901. /* Is 'a' after or equal to 'b'? */
  7902. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7903. {
  7904. return !((a - b) & 0x80000000);
  7905. }
  7906. static bool page_flip_finished(struct intel_crtc *crtc)
  7907. {
  7908. struct drm_device *dev = crtc->base.dev;
  7909. struct drm_i915_private *dev_priv = dev->dev_private;
  7910. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7911. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7912. return true;
  7913. /*
  7914. * The relevant registers doen't exist on pre-ctg.
  7915. * As the flip done interrupt doesn't trigger for mmio
  7916. * flips on gmch platforms, a flip count check isn't
  7917. * really needed there. But since ctg has the registers,
  7918. * include it in the check anyway.
  7919. */
  7920. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7921. return true;
  7922. /*
  7923. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7924. * used the same base address. In that case the mmio flip might
  7925. * have completed, but the CS hasn't even executed the flip yet.
  7926. *
  7927. * A flip count check isn't enough as the CS might have updated
  7928. * the base address just after start of vblank, but before we
  7929. * managed to process the interrupt. This means we'd complete the
  7930. * CS flip too soon.
  7931. *
  7932. * Combining both checks should get us a good enough result. It may
  7933. * still happen that the CS flip has been executed, but has not
  7934. * yet actually completed. But in case the base address is the same
  7935. * anyway, we don't really care.
  7936. */
  7937. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7938. crtc->unpin_work->gtt_offset &&
  7939. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7940. crtc->unpin_work->flip_count);
  7941. }
  7942. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7943. {
  7944. struct drm_i915_private *dev_priv = dev->dev_private;
  7945. struct intel_crtc *intel_crtc =
  7946. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7947. unsigned long flags;
  7948. /*
  7949. * This is called both by irq handlers and the reset code (to complete
  7950. * lost pageflips) so needs the full irqsave spinlocks.
  7951. *
  7952. * NB: An MMIO update of the plane base pointer will also
  7953. * generate a page-flip completion irq, i.e. every modeset
  7954. * is also accompanied by a spurious intel_prepare_page_flip().
  7955. */
  7956. spin_lock_irqsave(&dev->event_lock, flags);
  7957. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7958. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7959. spin_unlock_irqrestore(&dev->event_lock, flags);
  7960. }
  7961. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7962. {
  7963. /* Ensure that the work item is consistent when activating it ... */
  7964. smp_wmb();
  7965. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7966. /* and that it is marked active as soon as the irq could fire. */
  7967. smp_wmb();
  7968. }
  7969. static int intel_gen2_queue_flip(struct drm_device *dev,
  7970. struct drm_crtc *crtc,
  7971. struct drm_framebuffer *fb,
  7972. struct drm_i915_gem_object *obj,
  7973. struct intel_engine_cs *ring,
  7974. uint32_t flags)
  7975. {
  7976. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7977. u32 flip_mask;
  7978. int ret;
  7979. ret = intel_ring_begin(ring, 6);
  7980. if (ret)
  7981. return ret;
  7982. /* Can't queue multiple flips, so wait for the previous
  7983. * one to finish before executing the next.
  7984. */
  7985. if (intel_crtc->plane)
  7986. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7987. else
  7988. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7989. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7990. intel_ring_emit(ring, MI_NOOP);
  7991. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7992. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7993. intel_ring_emit(ring, fb->pitches[0]);
  7994. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7995. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7996. intel_mark_page_flip_active(intel_crtc);
  7997. __intel_ring_advance(ring);
  7998. return 0;
  7999. }
  8000. static int intel_gen3_queue_flip(struct drm_device *dev,
  8001. struct drm_crtc *crtc,
  8002. struct drm_framebuffer *fb,
  8003. struct drm_i915_gem_object *obj,
  8004. struct intel_engine_cs *ring,
  8005. uint32_t flags)
  8006. {
  8007. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8008. u32 flip_mask;
  8009. int ret;
  8010. ret = intel_ring_begin(ring, 6);
  8011. if (ret)
  8012. return ret;
  8013. if (intel_crtc->plane)
  8014. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8015. else
  8016. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8017. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8018. intel_ring_emit(ring, MI_NOOP);
  8019. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  8020. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8021. intel_ring_emit(ring, fb->pitches[0]);
  8022. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8023. intel_ring_emit(ring, MI_NOOP);
  8024. intel_mark_page_flip_active(intel_crtc);
  8025. __intel_ring_advance(ring);
  8026. return 0;
  8027. }
  8028. static int intel_gen4_queue_flip(struct drm_device *dev,
  8029. struct drm_crtc *crtc,
  8030. struct drm_framebuffer *fb,
  8031. struct drm_i915_gem_object *obj,
  8032. struct intel_engine_cs *ring,
  8033. uint32_t flags)
  8034. {
  8035. struct drm_i915_private *dev_priv = dev->dev_private;
  8036. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8037. uint32_t pf, pipesrc;
  8038. int ret;
  8039. ret = intel_ring_begin(ring, 4);
  8040. if (ret)
  8041. return ret;
  8042. /* i965+ uses the linear or tiled offsets from the
  8043. * Display Registers (which do not change across a page-flip)
  8044. * so we need only reprogram the base address.
  8045. */
  8046. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8047. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8048. intel_ring_emit(ring, fb->pitches[0]);
  8049. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  8050. obj->tiling_mode);
  8051. /* XXX Enabling the panel-fitter across page-flip is so far
  8052. * untested on non-native modes, so ignore it for now.
  8053. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8054. */
  8055. pf = 0;
  8056. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8057. intel_ring_emit(ring, pf | pipesrc);
  8058. intel_mark_page_flip_active(intel_crtc);
  8059. __intel_ring_advance(ring);
  8060. return 0;
  8061. }
  8062. static int intel_gen6_queue_flip(struct drm_device *dev,
  8063. struct drm_crtc *crtc,
  8064. struct drm_framebuffer *fb,
  8065. struct drm_i915_gem_object *obj,
  8066. struct intel_engine_cs *ring,
  8067. uint32_t flags)
  8068. {
  8069. struct drm_i915_private *dev_priv = dev->dev_private;
  8070. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8071. uint32_t pf, pipesrc;
  8072. int ret;
  8073. ret = intel_ring_begin(ring, 4);
  8074. if (ret)
  8075. return ret;
  8076. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8077. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8078. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  8079. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8080. /* Contrary to the suggestions in the documentation,
  8081. * "Enable Panel Fitter" does not seem to be required when page
  8082. * flipping with a non-native mode, and worse causes a normal
  8083. * modeset to fail.
  8084. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8085. */
  8086. pf = 0;
  8087. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8088. intel_ring_emit(ring, pf | pipesrc);
  8089. intel_mark_page_flip_active(intel_crtc);
  8090. __intel_ring_advance(ring);
  8091. return 0;
  8092. }
  8093. static int intel_gen7_queue_flip(struct drm_device *dev,
  8094. struct drm_crtc *crtc,
  8095. struct drm_framebuffer *fb,
  8096. struct drm_i915_gem_object *obj,
  8097. struct intel_engine_cs *ring,
  8098. uint32_t flags)
  8099. {
  8100. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8101. uint32_t plane_bit = 0;
  8102. int len, ret;
  8103. switch (intel_crtc->plane) {
  8104. case PLANE_A:
  8105. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8106. break;
  8107. case PLANE_B:
  8108. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8109. break;
  8110. case PLANE_C:
  8111. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8112. break;
  8113. default:
  8114. WARN_ONCE(1, "unknown plane in flip command\n");
  8115. return -ENODEV;
  8116. }
  8117. len = 4;
  8118. if (ring->id == RCS) {
  8119. len += 6;
  8120. /*
  8121. * On Gen 8, SRM is now taking an extra dword to accommodate
  8122. * 48bits addresses, and we need a NOOP for the batch size to
  8123. * stay even.
  8124. */
  8125. if (IS_GEN8(dev))
  8126. len += 2;
  8127. }
  8128. /*
  8129. * BSpec MI_DISPLAY_FLIP for IVB:
  8130. * "The full packet must be contained within the same cache line."
  8131. *
  8132. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8133. * cacheline, if we ever start emitting more commands before
  8134. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8135. * then do the cacheline alignment, and finally emit the
  8136. * MI_DISPLAY_FLIP.
  8137. */
  8138. ret = intel_ring_cacheline_align(ring);
  8139. if (ret)
  8140. return ret;
  8141. ret = intel_ring_begin(ring, len);
  8142. if (ret)
  8143. return ret;
  8144. /* Unmask the flip-done completion message. Note that the bspec says that
  8145. * we should do this for both the BCS and RCS, and that we must not unmask
  8146. * more than one flip event at any time (or ensure that one flip message
  8147. * can be sent by waiting for flip-done prior to queueing new flips).
  8148. * Experimentation says that BCS works despite DERRMR masking all
  8149. * flip-done completion events and that unmasking all planes at once
  8150. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8151. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8152. */
  8153. if (ring->id == RCS) {
  8154. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8155. intel_ring_emit(ring, DERRMR);
  8156. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8157. DERRMR_PIPEB_PRI_FLIP_DONE |
  8158. DERRMR_PIPEC_PRI_FLIP_DONE));
  8159. if (IS_GEN8(dev))
  8160. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8161. MI_SRM_LRM_GLOBAL_GTT);
  8162. else
  8163. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  8164. MI_SRM_LRM_GLOBAL_GTT);
  8165. intel_ring_emit(ring, DERRMR);
  8166. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8167. if (IS_GEN8(dev)) {
  8168. intel_ring_emit(ring, 0);
  8169. intel_ring_emit(ring, MI_NOOP);
  8170. }
  8171. }
  8172. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  8173. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  8174. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8175. intel_ring_emit(ring, (MI_NOOP));
  8176. intel_mark_page_flip_active(intel_crtc);
  8177. __intel_ring_advance(ring);
  8178. return 0;
  8179. }
  8180. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8181. struct drm_i915_gem_object *obj)
  8182. {
  8183. /*
  8184. * This is not being used for older platforms, because
  8185. * non-availability of flip done interrupt forces us to use
  8186. * CS flips. Older platforms derive flip done using some clever
  8187. * tricks involving the flip_pending status bits and vblank irqs.
  8188. * So using MMIO flips there would disrupt this mechanism.
  8189. */
  8190. if (ring == NULL)
  8191. return true;
  8192. if (INTEL_INFO(ring->dev)->gen < 5)
  8193. return false;
  8194. if (i915.use_mmio_flip < 0)
  8195. return false;
  8196. else if (i915.use_mmio_flip > 0)
  8197. return true;
  8198. else if (i915.enable_execlists)
  8199. return true;
  8200. else
  8201. return ring != i915_gem_request_get_ring(obj->last_read_req);
  8202. }
  8203. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  8204. {
  8205. struct drm_device *dev = intel_crtc->base.dev;
  8206. struct drm_i915_private *dev_priv = dev->dev_private;
  8207. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8208. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  8209. struct drm_i915_gem_object *obj = intel_fb->obj;
  8210. const enum pipe pipe = intel_crtc->pipe;
  8211. u32 ctl, stride;
  8212. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8213. ctl &= ~PLANE_CTL_TILED_MASK;
  8214. if (obj->tiling_mode == I915_TILING_X)
  8215. ctl |= PLANE_CTL_TILED_X;
  8216. /*
  8217. * The stride is either expressed as a multiple of 64 bytes chunks for
  8218. * linear buffers or in number of tiles for tiled buffers.
  8219. */
  8220. stride = fb->pitches[0] >> 6;
  8221. if (obj->tiling_mode == I915_TILING_X)
  8222. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  8223. /*
  8224. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8225. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8226. */
  8227. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8228. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8229. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  8230. POSTING_READ(PLANE_SURF(pipe, 0));
  8231. }
  8232. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  8233. {
  8234. struct drm_device *dev = intel_crtc->base.dev;
  8235. struct drm_i915_private *dev_priv = dev->dev_private;
  8236. struct intel_framebuffer *intel_fb =
  8237. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8238. struct drm_i915_gem_object *obj = intel_fb->obj;
  8239. u32 dspcntr;
  8240. u32 reg;
  8241. reg = DSPCNTR(intel_crtc->plane);
  8242. dspcntr = I915_READ(reg);
  8243. if (obj->tiling_mode != I915_TILING_NONE)
  8244. dspcntr |= DISPPLANE_TILED;
  8245. else
  8246. dspcntr &= ~DISPPLANE_TILED;
  8247. I915_WRITE(reg, dspcntr);
  8248. I915_WRITE(DSPSURF(intel_crtc->plane),
  8249. intel_crtc->unpin_work->gtt_offset);
  8250. POSTING_READ(DSPSURF(intel_crtc->plane));
  8251. }
  8252. /*
  8253. * XXX: This is the temporary way to update the plane registers until we get
  8254. * around to using the usual plane update functions for MMIO flips
  8255. */
  8256. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  8257. {
  8258. struct drm_device *dev = intel_crtc->base.dev;
  8259. bool atomic_update;
  8260. u32 start_vbl_count;
  8261. intel_mark_page_flip_active(intel_crtc);
  8262. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  8263. if (INTEL_INFO(dev)->gen >= 9)
  8264. skl_do_mmio_flip(intel_crtc);
  8265. else
  8266. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8267. ilk_do_mmio_flip(intel_crtc);
  8268. if (atomic_update)
  8269. intel_pipe_update_end(intel_crtc, start_vbl_count);
  8270. }
  8271. static void intel_mmio_flip_work_func(struct work_struct *work)
  8272. {
  8273. struct intel_crtc *crtc =
  8274. container_of(work, struct intel_crtc, mmio_flip.work);
  8275. struct intel_mmio_flip *mmio_flip;
  8276. mmio_flip = &crtc->mmio_flip;
  8277. if (mmio_flip->req)
  8278. WARN_ON(__i915_wait_request(mmio_flip->req,
  8279. crtc->reset_counter,
  8280. false, NULL, NULL) != 0);
  8281. intel_do_mmio_flip(crtc);
  8282. if (mmio_flip->req) {
  8283. mutex_lock(&crtc->base.dev->struct_mutex);
  8284. i915_gem_request_assign(&mmio_flip->req, NULL);
  8285. mutex_unlock(&crtc->base.dev->struct_mutex);
  8286. }
  8287. }
  8288. static int intel_queue_mmio_flip(struct drm_device *dev,
  8289. struct drm_crtc *crtc,
  8290. struct drm_framebuffer *fb,
  8291. struct drm_i915_gem_object *obj,
  8292. struct intel_engine_cs *ring,
  8293. uint32_t flags)
  8294. {
  8295. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8296. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  8297. obj->last_write_req);
  8298. schedule_work(&intel_crtc->mmio_flip.work);
  8299. return 0;
  8300. }
  8301. static int intel_default_queue_flip(struct drm_device *dev,
  8302. struct drm_crtc *crtc,
  8303. struct drm_framebuffer *fb,
  8304. struct drm_i915_gem_object *obj,
  8305. struct intel_engine_cs *ring,
  8306. uint32_t flags)
  8307. {
  8308. return -ENODEV;
  8309. }
  8310. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  8311. struct drm_crtc *crtc)
  8312. {
  8313. struct drm_i915_private *dev_priv = dev->dev_private;
  8314. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8315. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8316. u32 addr;
  8317. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8318. return true;
  8319. if (!work->enable_stall_check)
  8320. return false;
  8321. if (work->flip_ready_vblank == 0) {
  8322. if (work->flip_queued_req &&
  8323. !i915_gem_request_completed(work->flip_queued_req, true))
  8324. return false;
  8325. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  8326. }
  8327. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  8328. return false;
  8329. /* Potential stall - if we see that the flip has happened,
  8330. * assume a missed interrupt. */
  8331. if (INTEL_INFO(dev)->gen >= 4)
  8332. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8333. else
  8334. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8335. /* There is a potential issue here with a false positive after a flip
  8336. * to the same address. We could address this by checking for a
  8337. * non-incrementing frame counter.
  8338. */
  8339. return addr == work->gtt_offset;
  8340. }
  8341. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8342. {
  8343. struct drm_i915_private *dev_priv = dev->dev_private;
  8344. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8345. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8346. WARN_ON(!in_interrupt());
  8347. if (crtc == NULL)
  8348. return;
  8349. spin_lock(&dev->event_lock);
  8350. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8351. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8352. intel_crtc->unpin_work->flip_queued_vblank,
  8353. drm_vblank_count(dev, pipe));
  8354. page_flip_completed(intel_crtc);
  8355. }
  8356. spin_unlock(&dev->event_lock);
  8357. }
  8358. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8359. struct drm_framebuffer *fb,
  8360. struct drm_pending_vblank_event *event,
  8361. uint32_t page_flip_flags)
  8362. {
  8363. struct drm_device *dev = crtc->dev;
  8364. struct drm_i915_private *dev_priv = dev->dev_private;
  8365. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8366. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8367. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8368. struct drm_plane *primary = crtc->primary;
  8369. enum pipe pipe = intel_crtc->pipe;
  8370. struct intel_unpin_work *work;
  8371. struct intel_engine_cs *ring;
  8372. int ret;
  8373. /*
  8374. * drm_mode_page_flip_ioctl() should already catch this, but double
  8375. * check to be safe. In the future we may enable pageflipping from
  8376. * a disabled primary plane.
  8377. */
  8378. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8379. return -EBUSY;
  8380. /* Can't change pixel format via MI display flips. */
  8381. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8382. return -EINVAL;
  8383. /*
  8384. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8385. * Note that pitch changes could also affect these register.
  8386. */
  8387. if (INTEL_INFO(dev)->gen > 3 &&
  8388. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8389. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8390. return -EINVAL;
  8391. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8392. goto out_hang;
  8393. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8394. if (work == NULL)
  8395. return -ENOMEM;
  8396. work->event = event;
  8397. work->crtc = crtc;
  8398. work->old_fb = old_fb;
  8399. INIT_WORK(&work->work, intel_unpin_work_fn);
  8400. ret = drm_crtc_vblank_get(crtc);
  8401. if (ret)
  8402. goto free_work;
  8403. /* We borrow the event spin lock for protecting unpin_work */
  8404. spin_lock_irq(&dev->event_lock);
  8405. if (intel_crtc->unpin_work) {
  8406. /* Before declaring the flip queue wedged, check if
  8407. * the hardware completed the operation behind our backs.
  8408. */
  8409. if (__intel_pageflip_stall_check(dev, crtc)) {
  8410. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8411. page_flip_completed(intel_crtc);
  8412. } else {
  8413. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8414. spin_unlock_irq(&dev->event_lock);
  8415. drm_crtc_vblank_put(crtc);
  8416. kfree(work);
  8417. return -EBUSY;
  8418. }
  8419. }
  8420. intel_crtc->unpin_work = work;
  8421. spin_unlock_irq(&dev->event_lock);
  8422. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8423. flush_workqueue(dev_priv->wq);
  8424. /* Reference the objects for the scheduled work. */
  8425. drm_framebuffer_reference(work->old_fb);
  8426. drm_gem_object_reference(&obj->base);
  8427. crtc->primary->fb = fb;
  8428. update_state_fb(crtc->primary);
  8429. work->pending_flip_obj = obj;
  8430. ret = i915_mutex_lock_interruptible(dev);
  8431. if (ret)
  8432. goto cleanup;
  8433. atomic_inc(&intel_crtc->unpin_work_count);
  8434. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8435. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8436. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8437. if (IS_VALLEYVIEW(dev)) {
  8438. ring = &dev_priv->ring[BCS];
  8439. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  8440. /* vlv: DISPLAY_FLIP fails to change tiling */
  8441. ring = NULL;
  8442. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  8443. ring = &dev_priv->ring[BCS];
  8444. } else if (INTEL_INFO(dev)->gen >= 7) {
  8445. ring = i915_gem_request_get_ring(obj->last_read_req);
  8446. if (ring == NULL || ring->id != RCS)
  8447. ring = &dev_priv->ring[BCS];
  8448. } else {
  8449. ring = &dev_priv->ring[RCS];
  8450. }
  8451. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  8452. crtc->primary->state, ring);
  8453. if (ret)
  8454. goto cleanup_pending;
  8455. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  8456. + intel_crtc->dspaddr_offset;
  8457. if (use_mmio_flip(ring, obj)) {
  8458. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8459. page_flip_flags);
  8460. if (ret)
  8461. goto cleanup_unpin;
  8462. i915_gem_request_assign(&work->flip_queued_req,
  8463. obj->last_write_req);
  8464. } else {
  8465. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8466. page_flip_flags);
  8467. if (ret)
  8468. goto cleanup_unpin;
  8469. i915_gem_request_assign(&work->flip_queued_req,
  8470. intel_ring_get_request(ring));
  8471. }
  8472. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  8473. work->enable_stall_check = true;
  8474. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  8475. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8476. intel_fbc_disable(dev);
  8477. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8478. mutex_unlock(&dev->struct_mutex);
  8479. trace_i915_flip_request(intel_crtc->plane, obj);
  8480. return 0;
  8481. cleanup_unpin:
  8482. intel_unpin_fb_obj(fb, crtc->primary->state);
  8483. cleanup_pending:
  8484. atomic_dec(&intel_crtc->unpin_work_count);
  8485. mutex_unlock(&dev->struct_mutex);
  8486. cleanup:
  8487. crtc->primary->fb = old_fb;
  8488. update_state_fb(crtc->primary);
  8489. drm_gem_object_unreference_unlocked(&obj->base);
  8490. drm_framebuffer_unreference(work->old_fb);
  8491. spin_lock_irq(&dev->event_lock);
  8492. intel_crtc->unpin_work = NULL;
  8493. spin_unlock_irq(&dev->event_lock);
  8494. drm_crtc_vblank_put(crtc);
  8495. free_work:
  8496. kfree(work);
  8497. if (ret == -EIO) {
  8498. out_hang:
  8499. ret = intel_plane_restore(primary);
  8500. if (ret == 0 && event) {
  8501. spin_lock_irq(&dev->event_lock);
  8502. drm_send_vblank_event(dev, pipe, event);
  8503. spin_unlock_irq(&dev->event_lock);
  8504. }
  8505. }
  8506. return ret;
  8507. }
  8508. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8509. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8510. .load_lut = intel_crtc_load_lut,
  8511. .atomic_begin = intel_begin_crtc_commit,
  8512. .atomic_flush = intel_finish_crtc_commit,
  8513. };
  8514. /**
  8515. * intel_modeset_update_staged_output_state
  8516. *
  8517. * Updates the staged output configuration state, e.g. after we've read out the
  8518. * current hw state.
  8519. */
  8520. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8521. {
  8522. struct intel_crtc *crtc;
  8523. struct intel_encoder *encoder;
  8524. struct intel_connector *connector;
  8525. for_each_intel_connector(dev, connector) {
  8526. connector->new_encoder =
  8527. to_intel_encoder(connector->base.encoder);
  8528. }
  8529. for_each_intel_encoder(dev, encoder) {
  8530. encoder->new_crtc =
  8531. to_intel_crtc(encoder->base.crtc);
  8532. }
  8533. for_each_intel_crtc(dev, crtc) {
  8534. crtc->new_enabled = crtc->base.state->enable;
  8535. if (crtc->new_enabled)
  8536. crtc->new_config = crtc->config;
  8537. else
  8538. crtc->new_config = NULL;
  8539. }
  8540. }
  8541. /**
  8542. * intel_modeset_commit_output_state
  8543. *
  8544. * This function copies the stage display pipe configuration to the real one.
  8545. */
  8546. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8547. {
  8548. struct intel_crtc *crtc;
  8549. struct intel_encoder *encoder;
  8550. struct intel_connector *connector;
  8551. for_each_intel_connector(dev, connector) {
  8552. connector->base.encoder = &connector->new_encoder->base;
  8553. }
  8554. for_each_intel_encoder(dev, encoder) {
  8555. encoder->base.crtc = &encoder->new_crtc->base;
  8556. }
  8557. for_each_intel_crtc(dev, crtc) {
  8558. crtc->base.state->enable = crtc->new_enabled;
  8559. crtc->base.enabled = crtc->new_enabled;
  8560. }
  8561. }
  8562. static void
  8563. connected_sink_compute_bpp(struct intel_connector *connector,
  8564. struct intel_crtc_state *pipe_config)
  8565. {
  8566. int bpp = pipe_config->pipe_bpp;
  8567. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8568. connector->base.base.id,
  8569. connector->base.name);
  8570. /* Don't use an invalid EDID bpc value */
  8571. if (connector->base.display_info.bpc &&
  8572. connector->base.display_info.bpc * 3 < bpp) {
  8573. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8574. bpp, connector->base.display_info.bpc*3);
  8575. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8576. }
  8577. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8578. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8579. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8580. bpp);
  8581. pipe_config->pipe_bpp = 24;
  8582. }
  8583. }
  8584. static int
  8585. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8586. struct drm_framebuffer *fb,
  8587. struct intel_crtc_state *pipe_config)
  8588. {
  8589. struct drm_device *dev = crtc->base.dev;
  8590. struct intel_connector *connector;
  8591. int bpp;
  8592. switch (fb->pixel_format) {
  8593. case DRM_FORMAT_C8:
  8594. bpp = 8*3; /* since we go through a colormap */
  8595. break;
  8596. case DRM_FORMAT_XRGB1555:
  8597. case DRM_FORMAT_ARGB1555:
  8598. /* checked in intel_framebuffer_init already */
  8599. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8600. return -EINVAL;
  8601. case DRM_FORMAT_RGB565:
  8602. bpp = 6*3; /* min is 18bpp */
  8603. break;
  8604. case DRM_FORMAT_XBGR8888:
  8605. case DRM_FORMAT_ABGR8888:
  8606. /* checked in intel_framebuffer_init already */
  8607. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8608. return -EINVAL;
  8609. case DRM_FORMAT_XRGB8888:
  8610. case DRM_FORMAT_ARGB8888:
  8611. bpp = 8*3;
  8612. break;
  8613. case DRM_FORMAT_XRGB2101010:
  8614. case DRM_FORMAT_ARGB2101010:
  8615. case DRM_FORMAT_XBGR2101010:
  8616. case DRM_FORMAT_ABGR2101010:
  8617. /* checked in intel_framebuffer_init already */
  8618. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8619. return -EINVAL;
  8620. bpp = 10*3;
  8621. break;
  8622. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8623. default:
  8624. DRM_DEBUG_KMS("unsupported depth\n");
  8625. return -EINVAL;
  8626. }
  8627. pipe_config->pipe_bpp = bpp;
  8628. /* Clamp display bpp to EDID value */
  8629. for_each_intel_connector(dev, connector) {
  8630. if (!connector->new_encoder ||
  8631. connector->new_encoder->new_crtc != crtc)
  8632. continue;
  8633. connected_sink_compute_bpp(connector, pipe_config);
  8634. }
  8635. return bpp;
  8636. }
  8637. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8638. {
  8639. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8640. "type: 0x%x flags: 0x%x\n",
  8641. mode->crtc_clock,
  8642. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8643. mode->crtc_hsync_end, mode->crtc_htotal,
  8644. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8645. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8646. }
  8647. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8648. struct intel_crtc_state *pipe_config,
  8649. const char *context)
  8650. {
  8651. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8652. context, pipe_name(crtc->pipe));
  8653. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8654. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8655. pipe_config->pipe_bpp, pipe_config->dither);
  8656. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8657. pipe_config->has_pch_encoder,
  8658. pipe_config->fdi_lanes,
  8659. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8660. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8661. pipe_config->fdi_m_n.tu);
  8662. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8663. pipe_config->has_dp_encoder,
  8664. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8665. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8666. pipe_config->dp_m_n.tu);
  8667. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8668. pipe_config->has_dp_encoder,
  8669. pipe_config->dp_m2_n2.gmch_m,
  8670. pipe_config->dp_m2_n2.gmch_n,
  8671. pipe_config->dp_m2_n2.link_m,
  8672. pipe_config->dp_m2_n2.link_n,
  8673. pipe_config->dp_m2_n2.tu);
  8674. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8675. pipe_config->has_audio,
  8676. pipe_config->has_infoframe);
  8677. DRM_DEBUG_KMS("requested mode:\n");
  8678. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8679. DRM_DEBUG_KMS("adjusted mode:\n");
  8680. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8681. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8682. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8683. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8684. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8685. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8686. pipe_config->gmch_pfit.control,
  8687. pipe_config->gmch_pfit.pgm_ratios,
  8688. pipe_config->gmch_pfit.lvds_border_bits);
  8689. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8690. pipe_config->pch_pfit.pos,
  8691. pipe_config->pch_pfit.size,
  8692. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8693. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8694. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8695. }
  8696. static bool encoders_cloneable(const struct intel_encoder *a,
  8697. const struct intel_encoder *b)
  8698. {
  8699. /* masks could be asymmetric, so check both ways */
  8700. return a == b || (a->cloneable & (1 << b->type) &&
  8701. b->cloneable & (1 << a->type));
  8702. }
  8703. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8704. struct intel_encoder *encoder)
  8705. {
  8706. struct drm_device *dev = crtc->base.dev;
  8707. struct intel_encoder *source_encoder;
  8708. for_each_intel_encoder(dev, source_encoder) {
  8709. if (source_encoder->new_crtc != crtc)
  8710. continue;
  8711. if (!encoders_cloneable(encoder, source_encoder))
  8712. return false;
  8713. }
  8714. return true;
  8715. }
  8716. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8717. {
  8718. struct drm_device *dev = crtc->base.dev;
  8719. struct intel_encoder *encoder;
  8720. for_each_intel_encoder(dev, encoder) {
  8721. if (encoder->new_crtc != crtc)
  8722. continue;
  8723. if (!check_single_encoder_cloning(crtc, encoder))
  8724. return false;
  8725. }
  8726. return true;
  8727. }
  8728. static bool check_digital_port_conflicts(struct drm_device *dev)
  8729. {
  8730. struct intel_connector *connector;
  8731. unsigned int used_ports = 0;
  8732. /*
  8733. * Walk the connector list instead of the encoder
  8734. * list to detect the problem on ddi platforms
  8735. * where there's just one encoder per digital port.
  8736. */
  8737. for_each_intel_connector(dev, connector) {
  8738. struct intel_encoder *encoder = connector->new_encoder;
  8739. if (!encoder)
  8740. continue;
  8741. WARN_ON(!encoder->new_crtc);
  8742. switch (encoder->type) {
  8743. unsigned int port_mask;
  8744. case INTEL_OUTPUT_UNKNOWN:
  8745. if (WARN_ON(!HAS_DDI(dev)))
  8746. break;
  8747. case INTEL_OUTPUT_DISPLAYPORT:
  8748. case INTEL_OUTPUT_HDMI:
  8749. case INTEL_OUTPUT_EDP:
  8750. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8751. /* the same port mustn't appear more than once */
  8752. if (used_ports & port_mask)
  8753. return false;
  8754. used_ports |= port_mask;
  8755. default:
  8756. break;
  8757. }
  8758. }
  8759. return true;
  8760. }
  8761. static struct intel_crtc_state *
  8762. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8763. struct drm_framebuffer *fb,
  8764. struct drm_display_mode *mode)
  8765. {
  8766. struct drm_device *dev = crtc->dev;
  8767. struct intel_encoder *encoder;
  8768. struct intel_crtc_state *pipe_config;
  8769. int plane_bpp, ret = -EINVAL;
  8770. bool retry = true;
  8771. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8772. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8773. return ERR_PTR(-EINVAL);
  8774. }
  8775. if (!check_digital_port_conflicts(dev)) {
  8776. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8777. return ERR_PTR(-EINVAL);
  8778. }
  8779. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8780. if (!pipe_config)
  8781. return ERR_PTR(-ENOMEM);
  8782. pipe_config->base.crtc = crtc;
  8783. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  8784. drm_mode_copy(&pipe_config->base.mode, mode);
  8785. pipe_config->cpu_transcoder =
  8786. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8787. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8788. /*
  8789. * Sanitize sync polarity flags based on requested ones. If neither
  8790. * positive or negative polarity is requested, treat this as meaning
  8791. * negative polarity.
  8792. */
  8793. if (!(pipe_config->base.adjusted_mode.flags &
  8794. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8795. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8796. if (!(pipe_config->base.adjusted_mode.flags &
  8797. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8798. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8799. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8800. * plane pixel format and any sink constraints into account. Returns the
  8801. * source plane bpp so that dithering can be selected on mismatches
  8802. * after encoders and crtc also have had their say. */
  8803. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8804. fb, pipe_config);
  8805. if (plane_bpp < 0)
  8806. goto fail;
  8807. /*
  8808. * Determine the real pipe dimensions. Note that stereo modes can
  8809. * increase the actual pipe size due to the frame doubling and
  8810. * insertion of additional space for blanks between the frame. This
  8811. * is stored in the crtc timings. We use the requested mode to do this
  8812. * computation to clearly distinguish it from the adjusted mode, which
  8813. * can be changed by the connectors in the below retry loop.
  8814. */
  8815. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  8816. &pipe_config->pipe_src_w,
  8817. &pipe_config->pipe_src_h);
  8818. encoder_retry:
  8819. /* Ensure the port clock defaults are reset when retrying. */
  8820. pipe_config->port_clock = 0;
  8821. pipe_config->pixel_multiplier = 1;
  8822. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8823. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  8824. CRTC_STEREO_DOUBLE);
  8825. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8826. * adjust it according to limitations or connector properties, and also
  8827. * a chance to reject the mode entirely.
  8828. */
  8829. for_each_intel_encoder(dev, encoder) {
  8830. if (&encoder->new_crtc->base != crtc)
  8831. continue;
  8832. if (!(encoder->compute_config(encoder, pipe_config))) {
  8833. DRM_DEBUG_KMS("Encoder config failure\n");
  8834. goto fail;
  8835. }
  8836. }
  8837. /* Set default port clock if not overwritten by the encoder. Needs to be
  8838. * done afterwards in case the encoder adjusts the mode. */
  8839. if (!pipe_config->port_clock)
  8840. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  8841. * pipe_config->pixel_multiplier;
  8842. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8843. if (ret < 0) {
  8844. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8845. goto fail;
  8846. }
  8847. if (ret == RETRY) {
  8848. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8849. ret = -EINVAL;
  8850. goto fail;
  8851. }
  8852. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8853. retry = false;
  8854. goto encoder_retry;
  8855. }
  8856. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8857. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8858. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8859. return pipe_config;
  8860. fail:
  8861. kfree(pipe_config);
  8862. return ERR_PTR(ret);
  8863. }
  8864. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8865. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8866. static void
  8867. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8868. unsigned *prepare_pipes, unsigned *disable_pipes)
  8869. {
  8870. struct intel_crtc *intel_crtc;
  8871. struct drm_device *dev = crtc->dev;
  8872. struct intel_encoder *encoder;
  8873. struct intel_connector *connector;
  8874. struct drm_crtc *tmp_crtc;
  8875. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8876. /* Check which crtcs have changed outputs connected to them, these need
  8877. * to be part of the prepare_pipes mask. We don't (yet) support global
  8878. * modeset across multiple crtcs, so modeset_pipes will only have one
  8879. * bit set at most. */
  8880. for_each_intel_connector(dev, connector) {
  8881. if (connector->base.encoder == &connector->new_encoder->base)
  8882. continue;
  8883. if (connector->base.encoder) {
  8884. tmp_crtc = connector->base.encoder->crtc;
  8885. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8886. }
  8887. if (connector->new_encoder)
  8888. *prepare_pipes |=
  8889. 1 << connector->new_encoder->new_crtc->pipe;
  8890. }
  8891. for_each_intel_encoder(dev, encoder) {
  8892. if (encoder->base.crtc == &encoder->new_crtc->base)
  8893. continue;
  8894. if (encoder->base.crtc) {
  8895. tmp_crtc = encoder->base.crtc;
  8896. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8897. }
  8898. if (encoder->new_crtc)
  8899. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8900. }
  8901. /* Check for pipes that will be enabled/disabled ... */
  8902. for_each_intel_crtc(dev, intel_crtc) {
  8903. if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
  8904. continue;
  8905. if (!intel_crtc->new_enabled)
  8906. *disable_pipes |= 1 << intel_crtc->pipe;
  8907. else
  8908. *prepare_pipes |= 1 << intel_crtc->pipe;
  8909. }
  8910. /* set_mode is also used to update properties on life display pipes. */
  8911. intel_crtc = to_intel_crtc(crtc);
  8912. if (intel_crtc->new_enabled)
  8913. *prepare_pipes |= 1 << intel_crtc->pipe;
  8914. /*
  8915. * For simplicity do a full modeset on any pipe where the output routing
  8916. * changed. We could be more clever, but that would require us to be
  8917. * more careful with calling the relevant encoder->mode_set functions.
  8918. */
  8919. if (*prepare_pipes)
  8920. *modeset_pipes = *prepare_pipes;
  8921. /* ... and mask these out. */
  8922. *modeset_pipes &= ~(*disable_pipes);
  8923. *prepare_pipes &= ~(*disable_pipes);
  8924. /*
  8925. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8926. * obies this rule, but the modeset restore mode of
  8927. * intel_modeset_setup_hw_state does not.
  8928. */
  8929. *modeset_pipes &= 1 << intel_crtc->pipe;
  8930. *prepare_pipes &= 1 << intel_crtc->pipe;
  8931. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8932. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8933. }
  8934. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8935. {
  8936. struct drm_encoder *encoder;
  8937. struct drm_device *dev = crtc->dev;
  8938. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8939. if (encoder->crtc == crtc)
  8940. return true;
  8941. return false;
  8942. }
  8943. static void
  8944. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8945. {
  8946. struct drm_i915_private *dev_priv = dev->dev_private;
  8947. struct intel_encoder *intel_encoder;
  8948. struct intel_crtc *intel_crtc;
  8949. struct drm_connector *connector;
  8950. intel_shared_dpll_commit(dev_priv);
  8951. for_each_intel_encoder(dev, intel_encoder) {
  8952. if (!intel_encoder->base.crtc)
  8953. continue;
  8954. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8955. if (prepare_pipes & (1 << intel_crtc->pipe))
  8956. intel_encoder->connectors_active = false;
  8957. }
  8958. intel_modeset_commit_output_state(dev);
  8959. /* Double check state. */
  8960. for_each_intel_crtc(dev, intel_crtc) {
  8961. WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
  8962. WARN_ON(intel_crtc->new_config &&
  8963. intel_crtc->new_config != intel_crtc->config);
  8964. WARN_ON(intel_crtc->base.state->enable != !!intel_crtc->new_config);
  8965. }
  8966. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8967. if (!connector->encoder || !connector->encoder->crtc)
  8968. continue;
  8969. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8970. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8971. struct drm_property *dpms_property =
  8972. dev->mode_config.dpms_property;
  8973. connector->dpms = DRM_MODE_DPMS_ON;
  8974. drm_object_property_set_value(&connector->base,
  8975. dpms_property,
  8976. DRM_MODE_DPMS_ON);
  8977. intel_encoder = to_intel_encoder(connector->encoder);
  8978. intel_encoder->connectors_active = true;
  8979. }
  8980. }
  8981. }
  8982. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8983. {
  8984. int diff;
  8985. if (clock1 == clock2)
  8986. return true;
  8987. if (!clock1 || !clock2)
  8988. return false;
  8989. diff = abs(clock1 - clock2);
  8990. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8991. return true;
  8992. return false;
  8993. }
  8994. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8995. list_for_each_entry((intel_crtc), \
  8996. &(dev)->mode_config.crtc_list, \
  8997. base.head) \
  8998. if (mask & (1 <<(intel_crtc)->pipe))
  8999. static bool
  9000. intel_pipe_config_compare(struct drm_device *dev,
  9001. struct intel_crtc_state *current_config,
  9002. struct intel_crtc_state *pipe_config)
  9003. {
  9004. #define PIPE_CONF_CHECK_X(name) \
  9005. if (current_config->name != pipe_config->name) { \
  9006. DRM_ERROR("mismatch in " #name " " \
  9007. "(expected 0x%08x, found 0x%08x)\n", \
  9008. current_config->name, \
  9009. pipe_config->name); \
  9010. return false; \
  9011. }
  9012. #define PIPE_CONF_CHECK_I(name) \
  9013. if (current_config->name != pipe_config->name) { \
  9014. DRM_ERROR("mismatch in " #name " " \
  9015. "(expected %i, found %i)\n", \
  9016. current_config->name, \
  9017. pipe_config->name); \
  9018. return false; \
  9019. }
  9020. /* This is required for BDW+ where there is only one set of registers for
  9021. * switching between high and low RR.
  9022. * This macro can be used whenever a comparison has to be made between one
  9023. * hw state and multiple sw state variables.
  9024. */
  9025. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  9026. if ((current_config->name != pipe_config->name) && \
  9027. (current_config->alt_name != pipe_config->name)) { \
  9028. DRM_ERROR("mismatch in " #name " " \
  9029. "(expected %i or %i, found %i)\n", \
  9030. current_config->name, \
  9031. current_config->alt_name, \
  9032. pipe_config->name); \
  9033. return false; \
  9034. }
  9035. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9036. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9037. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  9038. "(expected %i, found %i)\n", \
  9039. current_config->name & (mask), \
  9040. pipe_config->name & (mask)); \
  9041. return false; \
  9042. }
  9043. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9044. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9045. DRM_ERROR("mismatch in " #name " " \
  9046. "(expected %i, found %i)\n", \
  9047. current_config->name, \
  9048. pipe_config->name); \
  9049. return false; \
  9050. }
  9051. #define PIPE_CONF_QUIRK(quirk) \
  9052. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9053. PIPE_CONF_CHECK_I(cpu_transcoder);
  9054. PIPE_CONF_CHECK_I(has_pch_encoder);
  9055. PIPE_CONF_CHECK_I(fdi_lanes);
  9056. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  9057. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  9058. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  9059. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  9060. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  9061. PIPE_CONF_CHECK_I(has_dp_encoder);
  9062. if (INTEL_INFO(dev)->gen < 8) {
  9063. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  9064. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  9065. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  9066. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  9067. PIPE_CONF_CHECK_I(dp_m_n.tu);
  9068. if (current_config->has_drrs) {
  9069. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  9070. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  9071. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  9072. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  9073. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  9074. }
  9075. } else {
  9076. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  9077. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  9078. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  9079. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  9080. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  9081. }
  9082. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9083. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9084. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9085. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9086. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9087. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9088. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9089. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9090. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9091. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9092. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9093. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9094. PIPE_CONF_CHECK_I(pixel_multiplier);
  9095. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9096. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  9097. IS_VALLEYVIEW(dev))
  9098. PIPE_CONF_CHECK_I(limited_color_range);
  9099. PIPE_CONF_CHECK_I(has_infoframe);
  9100. PIPE_CONF_CHECK_I(has_audio);
  9101. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9102. DRM_MODE_FLAG_INTERLACE);
  9103. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9104. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9105. DRM_MODE_FLAG_PHSYNC);
  9106. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9107. DRM_MODE_FLAG_NHSYNC);
  9108. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9109. DRM_MODE_FLAG_PVSYNC);
  9110. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9111. DRM_MODE_FLAG_NVSYNC);
  9112. }
  9113. PIPE_CONF_CHECK_I(pipe_src_w);
  9114. PIPE_CONF_CHECK_I(pipe_src_h);
  9115. /*
  9116. * FIXME: BIOS likes to set up a cloned config with lvds+external
  9117. * screen. Since we don't yet re-compute the pipe config when moving
  9118. * just the lvds port away to another pipe the sw tracking won't match.
  9119. *
  9120. * Proper atomic modesets with recomputed global state will fix this.
  9121. * Until then just don't check gmch state for inherited modes.
  9122. */
  9123. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  9124. PIPE_CONF_CHECK_I(gmch_pfit.control);
  9125. /* pfit ratios are autocomputed by the hw on gen4+ */
  9126. if (INTEL_INFO(dev)->gen < 4)
  9127. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  9128. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  9129. }
  9130. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9131. if (current_config->pch_pfit.enabled) {
  9132. PIPE_CONF_CHECK_I(pch_pfit.pos);
  9133. PIPE_CONF_CHECK_I(pch_pfit.size);
  9134. }
  9135. /* BDW+ don't expose a synchronous way to read the state */
  9136. if (IS_HASWELL(dev))
  9137. PIPE_CONF_CHECK_I(ips_enabled);
  9138. PIPE_CONF_CHECK_I(double_wide);
  9139. PIPE_CONF_CHECK_X(ddi_pll_sel);
  9140. PIPE_CONF_CHECK_I(shared_dpll);
  9141. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9142. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9143. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9144. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9145. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9146. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9147. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9148. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9149. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  9150. PIPE_CONF_CHECK_I(pipe_bpp);
  9151. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9152. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9153. #undef PIPE_CONF_CHECK_X
  9154. #undef PIPE_CONF_CHECK_I
  9155. #undef PIPE_CONF_CHECK_I_ALT
  9156. #undef PIPE_CONF_CHECK_FLAGS
  9157. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9158. #undef PIPE_CONF_QUIRK
  9159. return true;
  9160. }
  9161. static void check_wm_state(struct drm_device *dev)
  9162. {
  9163. struct drm_i915_private *dev_priv = dev->dev_private;
  9164. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9165. struct intel_crtc *intel_crtc;
  9166. int plane;
  9167. if (INTEL_INFO(dev)->gen < 9)
  9168. return;
  9169. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9170. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9171. for_each_intel_crtc(dev, intel_crtc) {
  9172. struct skl_ddb_entry *hw_entry, *sw_entry;
  9173. const enum pipe pipe = intel_crtc->pipe;
  9174. if (!intel_crtc->active)
  9175. continue;
  9176. /* planes */
  9177. for_each_plane(dev_priv, pipe, plane) {
  9178. hw_entry = &hw_ddb.plane[pipe][plane];
  9179. sw_entry = &sw_ddb->plane[pipe][plane];
  9180. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  9181. continue;
  9182. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  9183. "(expected (%u,%u), found (%u,%u))\n",
  9184. pipe_name(pipe), plane + 1,
  9185. sw_entry->start, sw_entry->end,
  9186. hw_entry->start, hw_entry->end);
  9187. }
  9188. /* cursor */
  9189. hw_entry = &hw_ddb.cursor[pipe];
  9190. sw_entry = &sw_ddb->cursor[pipe];
  9191. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  9192. continue;
  9193. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  9194. "(expected (%u,%u), found (%u,%u))\n",
  9195. pipe_name(pipe),
  9196. sw_entry->start, sw_entry->end,
  9197. hw_entry->start, hw_entry->end);
  9198. }
  9199. }
  9200. static void
  9201. check_connector_state(struct drm_device *dev)
  9202. {
  9203. struct intel_connector *connector;
  9204. for_each_intel_connector(dev, connector) {
  9205. /* This also checks the encoder/connector hw state with the
  9206. * ->get_hw_state callbacks. */
  9207. intel_connector_check_state(connector);
  9208. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  9209. "connector's staged encoder doesn't match current encoder\n");
  9210. }
  9211. }
  9212. static void
  9213. check_encoder_state(struct drm_device *dev)
  9214. {
  9215. struct intel_encoder *encoder;
  9216. struct intel_connector *connector;
  9217. for_each_intel_encoder(dev, encoder) {
  9218. bool enabled = false;
  9219. bool active = false;
  9220. enum pipe pipe, tracked_pipe;
  9221. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9222. encoder->base.base.id,
  9223. encoder->base.name);
  9224. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  9225. "encoder's stage crtc doesn't match current crtc\n");
  9226. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  9227. "encoder's active_connectors set, but no crtc\n");
  9228. for_each_intel_connector(dev, connector) {
  9229. if (connector->base.encoder != &encoder->base)
  9230. continue;
  9231. enabled = true;
  9232. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  9233. active = true;
  9234. }
  9235. /*
  9236. * for MST connectors if we unplug the connector is gone
  9237. * away but the encoder is still connected to a crtc
  9238. * until a modeset happens in response to the hotplug.
  9239. */
  9240. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  9241. continue;
  9242. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9243. "encoder's enabled state mismatch "
  9244. "(expected %i, found %i)\n",
  9245. !!encoder->base.crtc, enabled);
  9246. I915_STATE_WARN(active && !encoder->base.crtc,
  9247. "active encoder with no crtc\n");
  9248. I915_STATE_WARN(encoder->connectors_active != active,
  9249. "encoder's computed active state doesn't match tracked active state "
  9250. "(expected %i, found %i)\n", active, encoder->connectors_active);
  9251. active = encoder->get_hw_state(encoder, &pipe);
  9252. I915_STATE_WARN(active != encoder->connectors_active,
  9253. "encoder's hw state doesn't match sw tracking "
  9254. "(expected %i, found %i)\n",
  9255. encoder->connectors_active, active);
  9256. if (!encoder->base.crtc)
  9257. continue;
  9258. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  9259. I915_STATE_WARN(active && pipe != tracked_pipe,
  9260. "active encoder's pipe doesn't match"
  9261. "(expected %i, found %i)\n",
  9262. tracked_pipe, pipe);
  9263. }
  9264. }
  9265. static void
  9266. check_crtc_state(struct drm_device *dev)
  9267. {
  9268. struct drm_i915_private *dev_priv = dev->dev_private;
  9269. struct intel_crtc *crtc;
  9270. struct intel_encoder *encoder;
  9271. struct intel_crtc_state pipe_config;
  9272. for_each_intel_crtc(dev, crtc) {
  9273. bool enabled = false;
  9274. bool active = false;
  9275. memset(&pipe_config, 0, sizeof(pipe_config));
  9276. DRM_DEBUG_KMS("[CRTC:%d]\n",
  9277. crtc->base.base.id);
  9278. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  9279. "active crtc, but not enabled in sw tracking\n");
  9280. for_each_intel_encoder(dev, encoder) {
  9281. if (encoder->base.crtc != &crtc->base)
  9282. continue;
  9283. enabled = true;
  9284. if (encoder->connectors_active)
  9285. active = true;
  9286. }
  9287. I915_STATE_WARN(active != crtc->active,
  9288. "crtc's computed active state doesn't match tracked active state "
  9289. "(expected %i, found %i)\n", active, crtc->active);
  9290. I915_STATE_WARN(enabled != crtc->base.state->enable,
  9291. "crtc's computed enabled state doesn't match tracked enabled state "
  9292. "(expected %i, found %i)\n", enabled,
  9293. crtc->base.state->enable);
  9294. active = dev_priv->display.get_pipe_config(crtc,
  9295. &pipe_config);
  9296. /* hw state is inconsistent with the pipe quirk */
  9297. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  9298. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  9299. active = crtc->active;
  9300. for_each_intel_encoder(dev, encoder) {
  9301. enum pipe pipe;
  9302. if (encoder->base.crtc != &crtc->base)
  9303. continue;
  9304. if (encoder->get_hw_state(encoder, &pipe))
  9305. encoder->get_config(encoder, &pipe_config);
  9306. }
  9307. I915_STATE_WARN(crtc->active != active,
  9308. "crtc active state doesn't match with hw state "
  9309. "(expected %i, found %i)\n", crtc->active, active);
  9310. if (active &&
  9311. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  9312. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9313. intel_dump_pipe_config(crtc, &pipe_config,
  9314. "[hw state]");
  9315. intel_dump_pipe_config(crtc, crtc->config,
  9316. "[sw state]");
  9317. }
  9318. }
  9319. }
  9320. static void
  9321. check_shared_dpll_state(struct drm_device *dev)
  9322. {
  9323. struct drm_i915_private *dev_priv = dev->dev_private;
  9324. struct intel_crtc *crtc;
  9325. struct intel_dpll_hw_state dpll_hw_state;
  9326. int i;
  9327. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9328. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9329. int enabled_crtcs = 0, active_crtcs = 0;
  9330. bool active;
  9331. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9332. DRM_DEBUG_KMS("%s\n", pll->name);
  9333. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9334. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  9335. "more active pll users than references: %i vs %i\n",
  9336. pll->active, hweight32(pll->config.crtc_mask));
  9337. I915_STATE_WARN(pll->active && !pll->on,
  9338. "pll in active use but not on in sw tracking\n");
  9339. I915_STATE_WARN(pll->on && !pll->active,
  9340. "pll in on but not on in use in sw tracking\n");
  9341. I915_STATE_WARN(pll->on != active,
  9342. "pll on state mismatch (expected %i, found %i)\n",
  9343. pll->on, active);
  9344. for_each_intel_crtc(dev, crtc) {
  9345. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  9346. enabled_crtcs++;
  9347. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9348. active_crtcs++;
  9349. }
  9350. I915_STATE_WARN(pll->active != active_crtcs,
  9351. "pll active crtcs mismatch (expected %i, found %i)\n",
  9352. pll->active, active_crtcs);
  9353. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9354. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9355. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9356. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9357. sizeof(dpll_hw_state)),
  9358. "pll hw state mismatch\n");
  9359. }
  9360. }
  9361. void
  9362. intel_modeset_check_state(struct drm_device *dev)
  9363. {
  9364. check_wm_state(dev);
  9365. check_connector_state(dev);
  9366. check_encoder_state(dev);
  9367. check_crtc_state(dev);
  9368. check_shared_dpll_state(dev);
  9369. }
  9370. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  9371. int dotclock)
  9372. {
  9373. /*
  9374. * FDI already provided one idea for the dotclock.
  9375. * Yell if the encoder disagrees.
  9376. */
  9377. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  9378. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9379. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  9380. }
  9381. static void update_scanline_offset(struct intel_crtc *crtc)
  9382. {
  9383. struct drm_device *dev = crtc->base.dev;
  9384. /*
  9385. * The scanline counter increments at the leading edge of hsync.
  9386. *
  9387. * On most platforms it starts counting from vtotal-1 on the
  9388. * first active line. That means the scanline counter value is
  9389. * always one less than what we would expect. Ie. just after
  9390. * start of vblank, which also occurs at start of hsync (on the
  9391. * last active line), the scanline counter will read vblank_start-1.
  9392. *
  9393. * On gen2 the scanline counter starts counting from 1 instead
  9394. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9395. * to keep the value positive), instead of adding one.
  9396. *
  9397. * On HSW+ the behaviour of the scanline counter depends on the output
  9398. * type. For DP ports it behaves like most other platforms, but on HDMI
  9399. * there's an extra 1 line difference. So we need to add two instead of
  9400. * one to the value.
  9401. */
  9402. if (IS_GEN2(dev)) {
  9403. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  9404. int vtotal;
  9405. vtotal = mode->crtc_vtotal;
  9406. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9407. vtotal /= 2;
  9408. crtc->scanline_offset = vtotal - 1;
  9409. } else if (HAS_DDI(dev) &&
  9410. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9411. crtc->scanline_offset = 2;
  9412. } else
  9413. crtc->scanline_offset = 1;
  9414. }
  9415. static struct intel_crtc_state *
  9416. intel_modeset_compute_config(struct drm_crtc *crtc,
  9417. struct drm_display_mode *mode,
  9418. struct drm_framebuffer *fb,
  9419. unsigned *modeset_pipes,
  9420. unsigned *prepare_pipes,
  9421. unsigned *disable_pipes)
  9422. {
  9423. struct intel_crtc_state *pipe_config = NULL;
  9424. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9425. prepare_pipes, disable_pipes);
  9426. if ((*modeset_pipes) == 0)
  9427. goto out;
  9428. /*
  9429. * Note this needs changes when we start tracking multiple modes
  9430. * and crtcs. At that point we'll need to compute the whole config
  9431. * (i.e. one pipe_config for each crtc) rather than just the one
  9432. * for this crtc.
  9433. */
  9434. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9435. if (IS_ERR(pipe_config)) {
  9436. goto out;
  9437. }
  9438. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9439. "[modeset]");
  9440. out:
  9441. return pipe_config;
  9442. }
  9443. static int __intel_set_mode_setup_plls(struct drm_device *dev,
  9444. unsigned modeset_pipes,
  9445. unsigned disable_pipes)
  9446. {
  9447. struct drm_i915_private *dev_priv = to_i915(dev);
  9448. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9449. struct intel_crtc *intel_crtc;
  9450. int ret = 0;
  9451. if (!dev_priv->display.crtc_compute_clock)
  9452. return 0;
  9453. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9454. if (ret)
  9455. goto done;
  9456. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9457. struct intel_crtc_state *state = intel_crtc->new_config;
  9458. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9459. state);
  9460. if (ret) {
  9461. intel_shared_dpll_abort_config(dev_priv);
  9462. goto done;
  9463. }
  9464. }
  9465. done:
  9466. return ret;
  9467. }
  9468. static int __intel_set_mode(struct drm_crtc *crtc,
  9469. struct drm_display_mode *mode,
  9470. int x, int y, struct drm_framebuffer *fb,
  9471. struct intel_crtc_state *pipe_config,
  9472. unsigned modeset_pipes,
  9473. unsigned prepare_pipes,
  9474. unsigned disable_pipes)
  9475. {
  9476. struct drm_device *dev = crtc->dev;
  9477. struct drm_i915_private *dev_priv = dev->dev_private;
  9478. struct drm_display_mode *saved_mode;
  9479. struct intel_crtc *intel_crtc;
  9480. int ret = 0;
  9481. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9482. if (!saved_mode)
  9483. return -ENOMEM;
  9484. *saved_mode = crtc->mode;
  9485. if (modeset_pipes)
  9486. to_intel_crtc(crtc)->new_config = pipe_config;
  9487. /*
  9488. * See if the config requires any additional preparation, e.g.
  9489. * to adjust global state with pipes off. We need to do this
  9490. * here so we can get the modeset_pipe updated config for the new
  9491. * mode set on this crtc. For other crtcs we need to use the
  9492. * adjusted_mode bits in the crtc directly.
  9493. */
  9494. if (IS_VALLEYVIEW(dev)) {
  9495. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9496. /* may have added more to prepare_pipes than we should */
  9497. prepare_pipes &= ~disable_pipes;
  9498. }
  9499. ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
  9500. if (ret)
  9501. goto done;
  9502. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9503. intel_crtc_disable(&intel_crtc->base);
  9504. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9505. if (intel_crtc->base.state->enable)
  9506. dev_priv->display.crtc_disable(&intel_crtc->base);
  9507. }
  9508. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9509. * to set it here already despite that we pass it down the callchain.
  9510. *
  9511. * Note we'll need to fix this up when we start tracking multiple
  9512. * pipes; here we assume a single modeset_pipe and only track the
  9513. * single crtc and mode.
  9514. */
  9515. if (modeset_pipes) {
  9516. crtc->mode = *mode;
  9517. /* mode_set/enable/disable functions rely on a correct pipe
  9518. * config. */
  9519. intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
  9520. /*
  9521. * Calculate and store various constants which
  9522. * are later needed by vblank and swap-completion
  9523. * timestamping. They are derived from true hwmode.
  9524. */
  9525. drm_calc_timestamping_constants(crtc,
  9526. &pipe_config->base.adjusted_mode);
  9527. }
  9528. /* Only after disabling all output pipelines that will be changed can we
  9529. * update the the output configuration. */
  9530. intel_modeset_update_state(dev, prepare_pipes);
  9531. modeset_update_crtc_power_domains(dev);
  9532. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9533. * on the DPLL.
  9534. */
  9535. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9536. struct drm_plane *primary = intel_crtc->base.primary;
  9537. int vdisplay, hdisplay;
  9538. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9539. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9540. fb, 0, 0,
  9541. hdisplay, vdisplay,
  9542. x << 16, y << 16,
  9543. hdisplay << 16, vdisplay << 16);
  9544. }
  9545. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9546. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9547. update_scanline_offset(intel_crtc);
  9548. dev_priv->display.crtc_enable(&intel_crtc->base);
  9549. }
  9550. /* FIXME: add subpixel order */
  9551. done:
  9552. if (ret && crtc->state->enable)
  9553. crtc->mode = *saved_mode;
  9554. kfree(saved_mode);
  9555. return ret;
  9556. }
  9557. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9558. struct drm_display_mode *mode,
  9559. int x, int y, struct drm_framebuffer *fb,
  9560. struct intel_crtc_state *pipe_config,
  9561. unsigned modeset_pipes,
  9562. unsigned prepare_pipes,
  9563. unsigned disable_pipes)
  9564. {
  9565. int ret;
  9566. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9567. prepare_pipes, disable_pipes);
  9568. if (ret == 0)
  9569. intel_modeset_check_state(crtc->dev);
  9570. return ret;
  9571. }
  9572. static int intel_set_mode(struct drm_crtc *crtc,
  9573. struct drm_display_mode *mode,
  9574. int x, int y, struct drm_framebuffer *fb)
  9575. {
  9576. struct intel_crtc_state *pipe_config;
  9577. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9578. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9579. &modeset_pipes,
  9580. &prepare_pipes,
  9581. &disable_pipes);
  9582. if (IS_ERR(pipe_config))
  9583. return PTR_ERR(pipe_config);
  9584. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9585. modeset_pipes, prepare_pipes,
  9586. disable_pipes);
  9587. }
  9588. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9589. {
  9590. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9591. }
  9592. #undef for_each_intel_crtc_masked
  9593. static void intel_set_config_free(struct intel_set_config *config)
  9594. {
  9595. if (!config)
  9596. return;
  9597. kfree(config->save_connector_encoders);
  9598. kfree(config->save_encoder_crtcs);
  9599. kfree(config->save_crtc_enabled);
  9600. kfree(config);
  9601. }
  9602. static int intel_set_config_save_state(struct drm_device *dev,
  9603. struct intel_set_config *config)
  9604. {
  9605. struct drm_crtc *crtc;
  9606. struct drm_encoder *encoder;
  9607. struct drm_connector *connector;
  9608. int count;
  9609. config->save_crtc_enabled =
  9610. kcalloc(dev->mode_config.num_crtc,
  9611. sizeof(bool), GFP_KERNEL);
  9612. if (!config->save_crtc_enabled)
  9613. return -ENOMEM;
  9614. config->save_encoder_crtcs =
  9615. kcalloc(dev->mode_config.num_encoder,
  9616. sizeof(struct drm_crtc *), GFP_KERNEL);
  9617. if (!config->save_encoder_crtcs)
  9618. return -ENOMEM;
  9619. config->save_connector_encoders =
  9620. kcalloc(dev->mode_config.num_connector,
  9621. sizeof(struct drm_encoder *), GFP_KERNEL);
  9622. if (!config->save_connector_encoders)
  9623. return -ENOMEM;
  9624. /* Copy data. Note that driver private data is not affected.
  9625. * Should anything bad happen only the expected state is
  9626. * restored, not the drivers personal bookkeeping.
  9627. */
  9628. count = 0;
  9629. for_each_crtc(dev, crtc) {
  9630. config->save_crtc_enabled[count++] = crtc->state->enable;
  9631. }
  9632. count = 0;
  9633. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9634. config->save_encoder_crtcs[count++] = encoder->crtc;
  9635. }
  9636. count = 0;
  9637. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9638. config->save_connector_encoders[count++] = connector->encoder;
  9639. }
  9640. return 0;
  9641. }
  9642. static void intel_set_config_restore_state(struct drm_device *dev,
  9643. struct intel_set_config *config)
  9644. {
  9645. struct intel_crtc *crtc;
  9646. struct intel_encoder *encoder;
  9647. struct intel_connector *connector;
  9648. int count;
  9649. count = 0;
  9650. for_each_intel_crtc(dev, crtc) {
  9651. crtc->new_enabled = config->save_crtc_enabled[count++];
  9652. if (crtc->new_enabled)
  9653. crtc->new_config = crtc->config;
  9654. else
  9655. crtc->new_config = NULL;
  9656. }
  9657. count = 0;
  9658. for_each_intel_encoder(dev, encoder) {
  9659. encoder->new_crtc =
  9660. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9661. }
  9662. count = 0;
  9663. for_each_intel_connector(dev, connector) {
  9664. connector->new_encoder =
  9665. to_intel_encoder(config->save_connector_encoders[count++]);
  9666. }
  9667. }
  9668. static bool
  9669. is_crtc_connector_off(struct drm_mode_set *set)
  9670. {
  9671. int i;
  9672. if (set->num_connectors == 0)
  9673. return false;
  9674. if (WARN_ON(set->connectors == NULL))
  9675. return false;
  9676. for (i = 0; i < set->num_connectors; i++)
  9677. if (set->connectors[i]->encoder &&
  9678. set->connectors[i]->encoder->crtc == set->crtc &&
  9679. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9680. return true;
  9681. return false;
  9682. }
  9683. static void
  9684. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9685. struct intel_set_config *config)
  9686. {
  9687. /* We should be able to check here if the fb has the same properties
  9688. * and then just flip_or_move it */
  9689. if (is_crtc_connector_off(set)) {
  9690. config->mode_changed = true;
  9691. } else if (set->crtc->primary->fb != set->fb) {
  9692. /*
  9693. * If we have no fb, we can only flip as long as the crtc is
  9694. * active, otherwise we need a full mode set. The crtc may
  9695. * be active if we've only disabled the primary plane, or
  9696. * in fastboot situations.
  9697. */
  9698. if (set->crtc->primary->fb == NULL) {
  9699. struct intel_crtc *intel_crtc =
  9700. to_intel_crtc(set->crtc);
  9701. if (intel_crtc->active) {
  9702. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9703. config->fb_changed = true;
  9704. } else {
  9705. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9706. config->mode_changed = true;
  9707. }
  9708. } else if (set->fb == NULL) {
  9709. config->mode_changed = true;
  9710. } else if (set->fb->pixel_format !=
  9711. set->crtc->primary->fb->pixel_format) {
  9712. config->mode_changed = true;
  9713. } else {
  9714. config->fb_changed = true;
  9715. }
  9716. }
  9717. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9718. config->fb_changed = true;
  9719. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9720. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9721. drm_mode_debug_printmodeline(&set->crtc->mode);
  9722. drm_mode_debug_printmodeline(set->mode);
  9723. config->mode_changed = true;
  9724. }
  9725. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9726. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9727. }
  9728. static int
  9729. intel_modeset_stage_output_state(struct drm_device *dev,
  9730. struct drm_mode_set *set,
  9731. struct intel_set_config *config)
  9732. {
  9733. struct intel_connector *connector;
  9734. struct intel_encoder *encoder;
  9735. struct intel_crtc *crtc;
  9736. int ro;
  9737. /* The upper layers ensure that we either disable a crtc or have a list
  9738. * of connectors. For paranoia, double-check this. */
  9739. WARN_ON(!set->fb && (set->num_connectors != 0));
  9740. WARN_ON(set->fb && (set->num_connectors == 0));
  9741. for_each_intel_connector(dev, connector) {
  9742. /* Otherwise traverse passed in connector list and get encoders
  9743. * for them. */
  9744. for (ro = 0; ro < set->num_connectors; ro++) {
  9745. if (set->connectors[ro] == &connector->base) {
  9746. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9747. break;
  9748. }
  9749. }
  9750. /* If we disable the crtc, disable all its connectors. Also, if
  9751. * the connector is on the changing crtc but not on the new
  9752. * connector list, disable it. */
  9753. if ((!set->fb || ro == set->num_connectors) &&
  9754. connector->base.encoder &&
  9755. connector->base.encoder->crtc == set->crtc) {
  9756. connector->new_encoder = NULL;
  9757. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9758. connector->base.base.id,
  9759. connector->base.name);
  9760. }
  9761. if (&connector->new_encoder->base != connector->base.encoder) {
  9762. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
  9763. connector->base.base.id,
  9764. connector->base.name);
  9765. config->mode_changed = true;
  9766. }
  9767. }
  9768. /* connector->new_encoder is now updated for all connectors. */
  9769. /* Update crtc of enabled connectors. */
  9770. for_each_intel_connector(dev, connector) {
  9771. struct drm_crtc *new_crtc;
  9772. if (!connector->new_encoder)
  9773. continue;
  9774. new_crtc = connector->new_encoder->base.crtc;
  9775. for (ro = 0; ro < set->num_connectors; ro++) {
  9776. if (set->connectors[ro] == &connector->base)
  9777. new_crtc = set->crtc;
  9778. }
  9779. /* Make sure the new CRTC will work with the encoder */
  9780. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9781. new_crtc)) {
  9782. return -EINVAL;
  9783. }
  9784. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9785. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9786. connector->base.base.id,
  9787. connector->base.name,
  9788. new_crtc->base.id);
  9789. }
  9790. /* Check for any encoders that needs to be disabled. */
  9791. for_each_intel_encoder(dev, encoder) {
  9792. int num_connectors = 0;
  9793. for_each_intel_connector(dev, connector) {
  9794. if (connector->new_encoder == encoder) {
  9795. WARN_ON(!connector->new_encoder->new_crtc);
  9796. num_connectors++;
  9797. }
  9798. }
  9799. if (num_connectors == 0)
  9800. encoder->new_crtc = NULL;
  9801. else if (num_connectors > 1)
  9802. return -EINVAL;
  9803. /* Only now check for crtc changes so we don't miss encoders
  9804. * that will be disabled. */
  9805. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9806. DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
  9807. encoder->base.base.id,
  9808. encoder->base.name);
  9809. config->mode_changed = true;
  9810. }
  9811. }
  9812. /* Now we've also updated encoder->new_crtc for all encoders. */
  9813. for_each_intel_connector(dev, connector) {
  9814. if (connector->new_encoder)
  9815. if (connector->new_encoder != connector->encoder)
  9816. connector->encoder = connector->new_encoder;
  9817. }
  9818. for_each_intel_crtc(dev, crtc) {
  9819. crtc->new_enabled = false;
  9820. for_each_intel_encoder(dev, encoder) {
  9821. if (encoder->new_crtc == crtc) {
  9822. crtc->new_enabled = true;
  9823. break;
  9824. }
  9825. }
  9826. if (crtc->new_enabled != crtc->base.state->enable) {
  9827. DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
  9828. crtc->base.base.id,
  9829. crtc->new_enabled ? "en" : "dis");
  9830. config->mode_changed = true;
  9831. }
  9832. if (crtc->new_enabled)
  9833. crtc->new_config = crtc->config;
  9834. else
  9835. crtc->new_config = NULL;
  9836. }
  9837. return 0;
  9838. }
  9839. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9840. {
  9841. struct drm_device *dev = crtc->base.dev;
  9842. struct intel_encoder *encoder;
  9843. struct intel_connector *connector;
  9844. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9845. pipe_name(crtc->pipe));
  9846. for_each_intel_connector(dev, connector) {
  9847. if (connector->new_encoder &&
  9848. connector->new_encoder->new_crtc == crtc)
  9849. connector->new_encoder = NULL;
  9850. }
  9851. for_each_intel_encoder(dev, encoder) {
  9852. if (encoder->new_crtc == crtc)
  9853. encoder->new_crtc = NULL;
  9854. }
  9855. crtc->new_enabled = false;
  9856. crtc->new_config = NULL;
  9857. }
  9858. static int intel_crtc_set_config(struct drm_mode_set *set)
  9859. {
  9860. struct drm_device *dev;
  9861. struct drm_mode_set save_set;
  9862. struct intel_set_config *config;
  9863. struct intel_crtc_state *pipe_config;
  9864. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9865. int ret;
  9866. BUG_ON(!set);
  9867. BUG_ON(!set->crtc);
  9868. BUG_ON(!set->crtc->helper_private);
  9869. /* Enforce sane interface api - has been abused by the fb helper. */
  9870. BUG_ON(!set->mode && set->fb);
  9871. BUG_ON(set->fb && set->num_connectors == 0);
  9872. if (set->fb) {
  9873. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9874. set->crtc->base.id, set->fb->base.id,
  9875. (int)set->num_connectors, set->x, set->y);
  9876. } else {
  9877. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9878. }
  9879. dev = set->crtc->dev;
  9880. ret = -ENOMEM;
  9881. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9882. if (!config)
  9883. goto out_config;
  9884. ret = intel_set_config_save_state(dev, config);
  9885. if (ret)
  9886. goto out_config;
  9887. save_set.crtc = set->crtc;
  9888. save_set.mode = &set->crtc->mode;
  9889. save_set.x = set->crtc->x;
  9890. save_set.y = set->crtc->y;
  9891. save_set.fb = set->crtc->primary->fb;
  9892. /* Compute whether we need a full modeset, only an fb base update or no
  9893. * change at all. In the future we might also check whether only the
  9894. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9895. * such cases. */
  9896. intel_set_config_compute_mode_changes(set, config);
  9897. ret = intel_modeset_stage_output_state(dev, set, config);
  9898. if (ret)
  9899. goto fail;
  9900. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9901. set->fb,
  9902. &modeset_pipes,
  9903. &prepare_pipes,
  9904. &disable_pipes);
  9905. if (IS_ERR(pipe_config)) {
  9906. ret = PTR_ERR(pipe_config);
  9907. goto fail;
  9908. } else if (pipe_config) {
  9909. if (pipe_config->has_audio !=
  9910. to_intel_crtc(set->crtc)->config->has_audio)
  9911. config->mode_changed = true;
  9912. /*
  9913. * Note we have an issue here with infoframes: current code
  9914. * only updates them on the full mode set path per hw
  9915. * requirements. So here we should be checking for any
  9916. * required changes and forcing a mode set.
  9917. */
  9918. }
  9919. /* set_mode will free it in the mode_changed case */
  9920. if (!config->mode_changed)
  9921. kfree(pipe_config);
  9922. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9923. if (config->mode_changed) {
  9924. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9925. set->x, set->y, set->fb, pipe_config,
  9926. modeset_pipes, prepare_pipes,
  9927. disable_pipes);
  9928. } else if (config->fb_changed) {
  9929. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9930. struct drm_plane *primary = set->crtc->primary;
  9931. int vdisplay, hdisplay;
  9932. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  9933. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  9934. 0, 0, hdisplay, vdisplay,
  9935. set->x << 16, set->y << 16,
  9936. hdisplay << 16, vdisplay << 16);
  9937. /*
  9938. * We need to make sure the primary plane is re-enabled if it
  9939. * has previously been turned off.
  9940. */
  9941. if (!intel_crtc->primary_enabled && ret == 0) {
  9942. WARN_ON(!intel_crtc->active);
  9943. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9944. }
  9945. /*
  9946. * In the fastboot case this may be our only check of the
  9947. * state after boot. It would be better to only do it on
  9948. * the first update, but we don't have a nice way of doing that
  9949. * (and really, set_config isn't used much for high freq page
  9950. * flipping, so increasing its cost here shouldn't be a big
  9951. * deal).
  9952. */
  9953. if (i915.fastboot && ret == 0)
  9954. intel_modeset_check_state(set->crtc->dev);
  9955. }
  9956. if (ret) {
  9957. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9958. set->crtc->base.id, ret);
  9959. fail:
  9960. intel_set_config_restore_state(dev, config);
  9961. /*
  9962. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9963. * force the pipe off to avoid oopsing in the modeset code
  9964. * due to fb==NULL. This should only happen during boot since
  9965. * we don't yet reconstruct the FB from the hardware state.
  9966. */
  9967. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9968. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9969. /* Try to restore the config */
  9970. if (config->mode_changed &&
  9971. intel_set_mode(save_set.crtc, save_set.mode,
  9972. save_set.x, save_set.y, save_set.fb))
  9973. DRM_ERROR("failed to restore config after modeset failure\n");
  9974. }
  9975. out_config:
  9976. intel_set_config_free(config);
  9977. return ret;
  9978. }
  9979. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9980. .gamma_set = intel_crtc_gamma_set,
  9981. .set_config = intel_crtc_set_config,
  9982. .destroy = intel_crtc_destroy,
  9983. .page_flip = intel_crtc_page_flip,
  9984. .atomic_duplicate_state = intel_crtc_duplicate_state,
  9985. .atomic_destroy_state = intel_crtc_destroy_state,
  9986. };
  9987. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9988. struct intel_shared_dpll *pll,
  9989. struct intel_dpll_hw_state *hw_state)
  9990. {
  9991. uint32_t val;
  9992. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9993. return false;
  9994. val = I915_READ(PCH_DPLL(pll->id));
  9995. hw_state->dpll = val;
  9996. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9997. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9998. return val & DPLL_VCO_ENABLE;
  9999. }
  10000. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  10001. struct intel_shared_dpll *pll)
  10002. {
  10003. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  10004. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  10005. }
  10006. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  10007. struct intel_shared_dpll *pll)
  10008. {
  10009. /* PCH refclock must be enabled first */
  10010. ibx_assert_pch_refclk_enabled(dev_priv);
  10011. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10012. /* Wait for the clocks to stabilize. */
  10013. POSTING_READ(PCH_DPLL(pll->id));
  10014. udelay(150);
  10015. /* The pixel multiplier can only be updated once the
  10016. * DPLL is enabled and the clocks are stable.
  10017. *
  10018. * So write it again.
  10019. */
  10020. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10021. POSTING_READ(PCH_DPLL(pll->id));
  10022. udelay(200);
  10023. }
  10024. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  10025. struct intel_shared_dpll *pll)
  10026. {
  10027. struct drm_device *dev = dev_priv->dev;
  10028. struct intel_crtc *crtc;
  10029. /* Make sure no transcoder isn't still depending on us. */
  10030. for_each_intel_crtc(dev, crtc) {
  10031. if (intel_crtc_to_shared_dpll(crtc) == pll)
  10032. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  10033. }
  10034. I915_WRITE(PCH_DPLL(pll->id), 0);
  10035. POSTING_READ(PCH_DPLL(pll->id));
  10036. udelay(200);
  10037. }
  10038. static char *ibx_pch_dpll_names[] = {
  10039. "PCH DPLL A",
  10040. "PCH DPLL B",
  10041. };
  10042. static void ibx_pch_dpll_init(struct drm_device *dev)
  10043. {
  10044. struct drm_i915_private *dev_priv = dev->dev_private;
  10045. int i;
  10046. dev_priv->num_shared_dpll = 2;
  10047. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10048. dev_priv->shared_dplls[i].id = i;
  10049. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  10050. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  10051. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  10052. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  10053. dev_priv->shared_dplls[i].get_hw_state =
  10054. ibx_pch_dpll_get_hw_state;
  10055. }
  10056. }
  10057. static void intel_shared_dpll_init(struct drm_device *dev)
  10058. {
  10059. struct drm_i915_private *dev_priv = dev->dev_private;
  10060. if (HAS_DDI(dev))
  10061. intel_ddi_pll_init(dev);
  10062. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  10063. ibx_pch_dpll_init(dev);
  10064. else
  10065. dev_priv->num_shared_dpll = 0;
  10066. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  10067. }
  10068. /**
  10069. * intel_wm_need_update - Check whether watermarks need updating
  10070. * @plane: drm plane
  10071. * @state: new plane state
  10072. *
  10073. * Check current plane state versus the new one to determine whether
  10074. * watermarks need to be recalculated.
  10075. *
  10076. * Returns true or false.
  10077. */
  10078. bool intel_wm_need_update(struct drm_plane *plane,
  10079. struct drm_plane_state *state)
  10080. {
  10081. /* Update watermarks on tiling changes. */
  10082. if (!plane->state->fb || !state->fb ||
  10083. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  10084. plane->state->rotation != state->rotation)
  10085. return true;
  10086. return false;
  10087. }
  10088. /**
  10089. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10090. * @plane: drm plane to prepare for
  10091. * @fb: framebuffer to prepare for presentation
  10092. *
  10093. * Prepares a framebuffer for usage on a display plane. Generally this
  10094. * involves pinning the underlying object and updating the frontbuffer tracking
  10095. * bits. Some older platforms need special physical address handling for
  10096. * cursor planes.
  10097. *
  10098. * Returns 0 on success, negative error code on failure.
  10099. */
  10100. int
  10101. intel_prepare_plane_fb(struct drm_plane *plane,
  10102. struct drm_framebuffer *fb,
  10103. const struct drm_plane_state *new_state)
  10104. {
  10105. struct drm_device *dev = plane->dev;
  10106. struct intel_plane *intel_plane = to_intel_plane(plane);
  10107. enum pipe pipe = intel_plane->pipe;
  10108. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10109. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  10110. unsigned frontbuffer_bits = 0;
  10111. int ret = 0;
  10112. if (!obj)
  10113. return 0;
  10114. switch (plane->type) {
  10115. case DRM_PLANE_TYPE_PRIMARY:
  10116. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  10117. break;
  10118. case DRM_PLANE_TYPE_CURSOR:
  10119. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  10120. break;
  10121. case DRM_PLANE_TYPE_OVERLAY:
  10122. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  10123. break;
  10124. }
  10125. mutex_lock(&dev->struct_mutex);
  10126. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10127. INTEL_INFO(dev)->cursor_needs_physical) {
  10128. int align = IS_I830(dev) ? 16 * 1024 : 256;
  10129. ret = i915_gem_object_attach_phys(obj, align);
  10130. if (ret)
  10131. DRM_DEBUG_KMS("failed to attach phys object\n");
  10132. } else {
  10133. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  10134. }
  10135. if (ret == 0)
  10136. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  10137. mutex_unlock(&dev->struct_mutex);
  10138. return ret;
  10139. }
  10140. /**
  10141. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10142. * @plane: drm plane to clean up for
  10143. * @fb: old framebuffer that was on plane
  10144. *
  10145. * Cleans up a framebuffer that has just been removed from a plane.
  10146. */
  10147. void
  10148. intel_cleanup_plane_fb(struct drm_plane *plane,
  10149. struct drm_framebuffer *fb,
  10150. const struct drm_plane_state *old_state)
  10151. {
  10152. struct drm_device *dev = plane->dev;
  10153. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10154. if (WARN_ON(!obj))
  10155. return;
  10156. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  10157. !INTEL_INFO(dev)->cursor_needs_physical) {
  10158. mutex_lock(&dev->struct_mutex);
  10159. intel_unpin_fb_obj(fb, old_state);
  10160. mutex_unlock(&dev->struct_mutex);
  10161. }
  10162. }
  10163. static int
  10164. intel_check_primary_plane(struct drm_plane *plane,
  10165. struct intel_plane_state *state)
  10166. {
  10167. struct drm_device *dev = plane->dev;
  10168. struct drm_i915_private *dev_priv = dev->dev_private;
  10169. struct drm_crtc *crtc = state->base.crtc;
  10170. struct intel_crtc *intel_crtc;
  10171. struct drm_framebuffer *fb = state->base.fb;
  10172. struct drm_rect *dest = &state->dst;
  10173. struct drm_rect *src = &state->src;
  10174. const struct drm_rect *clip = &state->clip;
  10175. int ret;
  10176. crtc = crtc ? crtc : plane->crtc;
  10177. intel_crtc = to_intel_crtc(crtc);
  10178. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10179. src, dest, clip,
  10180. DRM_PLANE_HELPER_NO_SCALING,
  10181. DRM_PLANE_HELPER_NO_SCALING,
  10182. false, true, &state->visible);
  10183. if (ret)
  10184. return ret;
  10185. if (intel_crtc->active) {
  10186. intel_crtc->atomic.wait_for_flips = true;
  10187. /*
  10188. * FBC does not work on some platforms for rotated
  10189. * planes, so disable it when rotation is not 0 and
  10190. * update it when rotation is set back to 0.
  10191. *
  10192. * FIXME: This is redundant with the fbc update done in
  10193. * the primary plane enable function except that that
  10194. * one is done too late. We eventually need to unify
  10195. * this.
  10196. */
  10197. if (intel_crtc->primary_enabled &&
  10198. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  10199. dev_priv->fbc.crtc == intel_crtc &&
  10200. state->base.rotation != BIT(DRM_ROTATE_0)) {
  10201. intel_crtc->atomic.disable_fbc = true;
  10202. }
  10203. if (state->visible) {
  10204. /*
  10205. * BDW signals flip done immediately if the plane
  10206. * is disabled, even if the plane enable is already
  10207. * armed to occur at the next vblank :(
  10208. */
  10209. if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
  10210. intel_crtc->atomic.wait_vblank = true;
  10211. }
  10212. intel_crtc->atomic.fb_bits |=
  10213. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  10214. intel_crtc->atomic.update_fbc = true;
  10215. if (intel_wm_need_update(plane, &state->base))
  10216. intel_crtc->atomic.update_wm = true;
  10217. }
  10218. return 0;
  10219. }
  10220. static void
  10221. intel_commit_primary_plane(struct drm_plane *plane,
  10222. struct intel_plane_state *state)
  10223. {
  10224. struct drm_crtc *crtc = state->base.crtc;
  10225. struct drm_framebuffer *fb = state->base.fb;
  10226. struct drm_device *dev = plane->dev;
  10227. struct drm_i915_private *dev_priv = dev->dev_private;
  10228. struct intel_crtc *intel_crtc;
  10229. struct drm_rect *src = &state->src;
  10230. crtc = crtc ? crtc : plane->crtc;
  10231. intel_crtc = to_intel_crtc(crtc);
  10232. plane->fb = fb;
  10233. crtc->x = src->x1 >> 16;
  10234. crtc->y = src->y1 >> 16;
  10235. if (intel_crtc->active) {
  10236. if (state->visible) {
  10237. /* FIXME: kill this fastboot hack */
  10238. intel_update_pipe_size(intel_crtc);
  10239. intel_crtc->primary_enabled = true;
  10240. dev_priv->display.update_primary_plane(crtc, plane->fb,
  10241. crtc->x, crtc->y);
  10242. } else {
  10243. /*
  10244. * If clipping results in a non-visible primary plane,
  10245. * we'll disable the primary plane. Note that this is
  10246. * a bit different than what happens if userspace
  10247. * explicitly disables the plane by passing fb=0
  10248. * because plane->fb still gets set and pinned.
  10249. */
  10250. intel_disable_primary_hw_plane(plane, crtc);
  10251. }
  10252. }
  10253. }
  10254. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  10255. {
  10256. struct drm_device *dev = crtc->dev;
  10257. struct drm_i915_private *dev_priv = dev->dev_private;
  10258. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10259. struct intel_plane *intel_plane;
  10260. struct drm_plane *p;
  10261. unsigned fb_bits = 0;
  10262. /* Track fb's for any planes being disabled */
  10263. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  10264. intel_plane = to_intel_plane(p);
  10265. if (intel_crtc->atomic.disabled_planes &
  10266. (1 << drm_plane_index(p))) {
  10267. switch (p->type) {
  10268. case DRM_PLANE_TYPE_PRIMARY:
  10269. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  10270. break;
  10271. case DRM_PLANE_TYPE_CURSOR:
  10272. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  10273. break;
  10274. case DRM_PLANE_TYPE_OVERLAY:
  10275. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  10276. break;
  10277. }
  10278. mutex_lock(&dev->struct_mutex);
  10279. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  10280. mutex_unlock(&dev->struct_mutex);
  10281. }
  10282. }
  10283. if (intel_crtc->atomic.wait_for_flips)
  10284. intel_crtc_wait_for_pending_flips(crtc);
  10285. if (intel_crtc->atomic.disable_fbc)
  10286. intel_fbc_disable(dev);
  10287. if (intel_crtc->atomic.pre_disable_primary)
  10288. intel_pre_disable_primary(crtc);
  10289. if (intel_crtc->atomic.update_wm)
  10290. intel_update_watermarks(crtc);
  10291. intel_runtime_pm_get(dev_priv);
  10292. /* Perform vblank evasion around commit operation */
  10293. if (intel_crtc->active)
  10294. intel_crtc->atomic.evade =
  10295. intel_pipe_update_start(intel_crtc,
  10296. &intel_crtc->atomic.start_vbl_count);
  10297. }
  10298. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  10299. {
  10300. struct drm_device *dev = crtc->dev;
  10301. struct drm_i915_private *dev_priv = dev->dev_private;
  10302. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10303. struct drm_plane *p;
  10304. if (intel_crtc->atomic.evade)
  10305. intel_pipe_update_end(intel_crtc,
  10306. intel_crtc->atomic.start_vbl_count);
  10307. intel_runtime_pm_put(dev_priv);
  10308. if (intel_crtc->atomic.wait_vblank)
  10309. intel_wait_for_vblank(dev, intel_crtc->pipe);
  10310. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  10311. if (intel_crtc->atomic.update_fbc) {
  10312. mutex_lock(&dev->struct_mutex);
  10313. intel_fbc_update(dev);
  10314. mutex_unlock(&dev->struct_mutex);
  10315. }
  10316. if (intel_crtc->atomic.post_enable_primary)
  10317. intel_post_enable_primary(crtc);
  10318. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  10319. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  10320. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  10321. false, false);
  10322. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  10323. }
  10324. /**
  10325. * intel_plane_destroy - destroy a plane
  10326. * @plane: plane to destroy
  10327. *
  10328. * Common destruction function for all types of planes (primary, cursor,
  10329. * sprite).
  10330. */
  10331. void intel_plane_destroy(struct drm_plane *plane)
  10332. {
  10333. struct intel_plane *intel_plane = to_intel_plane(plane);
  10334. drm_plane_cleanup(plane);
  10335. kfree(intel_plane);
  10336. }
  10337. const struct drm_plane_funcs intel_plane_funcs = {
  10338. .update_plane = drm_plane_helper_update,
  10339. .disable_plane = drm_plane_helper_disable,
  10340. .destroy = intel_plane_destroy,
  10341. .set_property = drm_atomic_helper_plane_set_property,
  10342. .atomic_get_property = intel_plane_atomic_get_property,
  10343. .atomic_set_property = intel_plane_atomic_set_property,
  10344. .atomic_duplicate_state = intel_plane_duplicate_state,
  10345. .atomic_destroy_state = intel_plane_destroy_state,
  10346. };
  10347. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10348. int pipe)
  10349. {
  10350. struct intel_plane *primary;
  10351. struct intel_plane_state *state;
  10352. const uint32_t *intel_primary_formats;
  10353. int num_formats;
  10354. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10355. if (primary == NULL)
  10356. return NULL;
  10357. state = intel_create_plane_state(&primary->base);
  10358. if (!state) {
  10359. kfree(primary);
  10360. return NULL;
  10361. }
  10362. primary->base.state = &state->base;
  10363. primary->can_scale = false;
  10364. primary->max_downscale = 1;
  10365. primary->pipe = pipe;
  10366. primary->plane = pipe;
  10367. primary->check_plane = intel_check_primary_plane;
  10368. primary->commit_plane = intel_commit_primary_plane;
  10369. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10370. primary->plane = !pipe;
  10371. if (INTEL_INFO(dev)->gen <= 3) {
  10372. intel_primary_formats = intel_primary_formats_gen2;
  10373. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10374. } else {
  10375. intel_primary_formats = intel_primary_formats_gen4;
  10376. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10377. }
  10378. drm_universal_plane_init(dev, &primary->base, 0,
  10379. &intel_plane_funcs,
  10380. intel_primary_formats, num_formats,
  10381. DRM_PLANE_TYPE_PRIMARY);
  10382. if (INTEL_INFO(dev)->gen >= 4) {
  10383. if (!dev->mode_config.rotation_property)
  10384. dev->mode_config.rotation_property =
  10385. drm_mode_create_rotation_property(dev,
  10386. BIT(DRM_ROTATE_0) |
  10387. BIT(DRM_ROTATE_180));
  10388. if (dev->mode_config.rotation_property)
  10389. drm_object_attach_property(&primary->base.base,
  10390. dev->mode_config.rotation_property,
  10391. state->base.rotation);
  10392. }
  10393. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  10394. return &primary->base;
  10395. }
  10396. static int
  10397. intel_check_cursor_plane(struct drm_plane *plane,
  10398. struct intel_plane_state *state)
  10399. {
  10400. struct drm_crtc *crtc = state->base.crtc;
  10401. struct drm_device *dev = plane->dev;
  10402. struct drm_framebuffer *fb = state->base.fb;
  10403. struct drm_rect *dest = &state->dst;
  10404. struct drm_rect *src = &state->src;
  10405. const struct drm_rect *clip = &state->clip;
  10406. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10407. struct intel_crtc *intel_crtc;
  10408. unsigned stride;
  10409. int ret;
  10410. crtc = crtc ? crtc : plane->crtc;
  10411. intel_crtc = to_intel_crtc(crtc);
  10412. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10413. src, dest, clip,
  10414. DRM_PLANE_HELPER_NO_SCALING,
  10415. DRM_PLANE_HELPER_NO_SCALING,
  10416. true, true, &state->visible);
  10417. if (ret)
  10418. return ret;
  10419. /* if we want to turn off the cursor ignore width and height */
  10420. if (!obj)
  10421. goto finish;
  10422. /* Check for which cursor types we support */
  10423. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  10424. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  10425. state->base.crtc_w, state->base.crtc_h);
  10426. return -EINVAL;
  10427. }
  10428. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  10429. if (obj->base.size < stride * state->base.crtc_h) {
  10430. DRM_DEBUG_KMS("buffer is too small\n");
  10431. return -ENOMEM;
  10432. }
  10433. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  10434. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10435. ret = -EINVAL;
  10436. }
  10437. finish:
  10438. if (intel_crtc->active) {
  10439. if (plane->state->crtc_w != state->base.crtc_w)
  10440. intel_crtc->atomic.update_wm = true;
  10441. intel_crtc->atomic.fb_bits |=
  10442. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  10443. }
  10444. return ret;
  10445. }
  10446. static void
  10447. intel_commit_cursor_plane(struct drm_plane *plane,
  10448. struct intel_plane_state *state)
  10449. {
  10450. struct drm_crtc *crtc = state->base.crtc;
  10451. struct drm_device *dev = plane->dev;
  10452. struct intel_crtc *intel_crtc;
  10453. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10454. uint32_t addr;
  10455. crtc = crtc ? crtc : plane->crtc;
  10456. intel_crtc = to_intel_crtc(crtc);
  10457. plane->fb = state->base.fb;
  10458. crtc->cursor_x = state->base.crtc_x;
  10459. crtc->cursor_y = state->base.crtc_y;
  10460. if (intel_crtc->cursor_bo == obj)
  10461. goto update;
  10462. if (!obj)
  10463. addr = 0;
  10464. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10465. addr = i915_gem_obj_ggtt_offset(obj);
  10466. else
  10467. addr = obj->phys_handle->busaddr;
  10468. intel_crtc->cursor_addr = addr;
  10469. intel_crtc->cursor_bo = obj;
  10470. update:
  10471. if (intel_crtc->active)
  10472. intel_crtc_update_cursor(crtc, state->visible);
  10473. }
  10474. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10475. int pipe)
  10476. {
  10477. struct intel_plane *cursor;
  10478. struct intel_plane_state *state;
  10479. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10480. if (cursor == NULL)
  10481. return NULL;
  10482. state = intel_create_plane_state(&cursor->base);
  10483. if (!state) {
  10484. kfree(cursor);
  10485. return NULL;
  10486. }
  10487. cursor->base.state = &state->base;
  10488. cursor->can_scale = false;
  10489. cursor->max_downscale = 1;
  10490. cursor->pipe = pipe;
  10491. cursor->plane = pipe;
  10492. cursor->check_plane = intel_check_cursor_plane;
  10493. cursor->commit_plane = intel_commit_cursor_plane;
  10494. drm_universal_plane_init(dev, &cursor->base, 0,
  10495. &intel_plane_funcs,
  10496. intel_cursor_formats,
  10497. ARRAY_SIZE(intel_cursor_formats),
  10498. DRM_PLANE_TYPE_CURSOR);
  10499. if (INTEL_INFO(dev)->gen >= 4) {
  10500. if (!dev->mode_config.rotation_property)
  10501. dev->mode_config.rotation_property =
  10502. drm_mode_create_rotation_property(dev,
  10503. BIT(DRM_ROTATE_0) |
  10504. BIT(DRM_ROTATE_180));
  10505. if (dev->mode_config.rotation_property)
  10506. drm_object_attach_property(&cursor->base.base,
  10507. dev->mode_config.rotation_property,
  10508. state->base.rotation);
  10509. }
  10510. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  10511. return &cursor->base;
  10512. }
  10513. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10514. {
  10515. struct drm_i915_private *dev_priv = dev->dev_private;
  10516. struct intel_crtc *intel_crtc;
  10517. struct intel_crtc_state *crtc_state = NULL;
  10518. struct drm_plane *primary = NULL;
  10519. struct drm_plane *cursor = NULL;
  10520. int i, ret;
  10521. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10522. if (intel_crtc == NULL)
  10523. return;
  10524. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  10525. if (!crtc_state)
  10526. goto fail;
  10527. intel_crtc_set_state(intel_crtc, crtc_state);
  10528. crtc_state->base.crtc = &intel_crtc->base;
  10529. primary = intel_primary_plane_create(dev, pipe);
  10530. if (!primary)
  10531. goto fail;
  10532. cursor = intel_cursor_plane_create(dev, pipe);
  10533. if (!cursor)
  10534. goto fail;
  10535. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10536. cursor, &intel_crtc_funcs);
  10537. if (ret)
  10538. goto fail;
  10539. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10540. for (i = 0; i < 256; i++) {
  10541. intel_crtc->lut_r[i] = i;
  10542. intel_crtc->lut_g[i] = i;
  10543. intel_crtc->lut_b[i] = i;
  10544. }
  10545. /*
  10546. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10547. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10548. */
  10549. intel_crtc->pipe = pipe;
  10550. intel_crtc->plane = pipe;
  10551. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10552. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10553. intel_crtc->plane = !pipe;
  10554. }
  10555. intel_crtc->cursor_base = ~0;
  10556. intel_crtc->cursor_cntl = ~0;
  10557. intel_crtc->cursor_size = ~0;
  10558. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10559. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10560. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10561. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10562. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10563. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10564. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10565. return;
  10566. fail:
  10567. if (primary)
  10568. drm_plane_cleanup(primary);
  10569. if (cursor)
  10570. drm_plane_cleanup(cursor);
  10571. kfree(crtc_state);
  10572. kfree(intel_crtc);
  10573. }
  10574. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10575. {
  10576. struct drm_encoder *encoder = connector->base.encoder;
  10577. struct drm_device *dev = connector->base.dev;
  10578. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10579. if (!encoder || WARN_ON(!encoder->crtc))
  10580. return INVALID_PIPE;
  10581. return to_intel_crtc(encoder->crtc)->pipe;
  10582. }
  10583. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10584. struct drm_file *file)
  10585. {
  10586. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10587. struct drm_crtc *drmmode_crtc;
  10588. struct intel_crtc *crtc;
  10589. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10590. if (!drmmode_crtc) {
  10591. DRM_ERROR("no such CRTC id\n");
  10592. return -ENOENT;
  10593. }
  10594. crtc = to_intel_crtc(drmmode_crtc);
  10595. pipe_from_crtc_id->pipe = crtc->pipe;
  10596. return 0;
  10597. }
  10598. static int intel_encoder_clones(struct intel_encoder *encoder)
  10599. {
  10600. struct drm_device *dev = encoder->base.dev;
  10601. struct intel_encoder *source_encoder;
  10602. int index_mask = 0;
  10603. int entry = 0;
  10604. for_each_intel_encoder(dev, source_encoder) {
  10605. if (encoders_cloneable(encoder, source_encoder))
  10606. index_mask |= (1 << entry);
  10607. entry++;
  10608. }
  10609. return index_mask;
  10610. }
  10611. static bool has_edp_a(struct drm_device *dev)
  10612. {
  10613. struct drm_i915_private *dev_priv = dev->dev_private;
  10614. if (!IS_MOBILE(dev))
  10615. return false;
  10616. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10617. return false;
  10618. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10619. return false;
  10620. return true;
  10621. }
  10622. static bool intel_crt_present(struct drm_device *dev)
  10623. {
  10624. struct drm_i915_private *dev_priv = dev->dev_private;
  10625. if (INTEL_INFO(dev)->gen >= 9)
  10626. return false;
  10627. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10628. return false;
  10629. if (IS_CHERRYVIEW(dev))
  10630. return false;
  10631. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10632. return false;
  10633. return true;
  10634. }
  10635. static void intel_setup_outputs(struct drm_device *dev)
  10636. {
  10637. struct drm_i915_private *dev_priv = dev->dev_private;
  10638. struct intel_encoder *encoder;
  10639. struct drm_connector *connector;
  10640. bool dpd_is_edp = false;
  10641. intel_lvds_init(dev);
  10642. if (intel_crt_present(dev))
  10643. intel_crt_init(dev);
  10644. if (HAS_DDI(dev)) {
  10645. int found;
  10646. /*
  10647. * Haswell uses DDI functions to detect digital outputs.
  10648. * On SKL pre-D0 the strap isn't connected, so we assume
  10649. * it's there.
  10650. */
  10651. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10652. /* WaIgnoreDDIAStrap: skl */
  10653. if (found ||
  10654. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  10655. intel_ddi_init(dev, PORT_A);
  10656. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10657. * register */
  10658. found = I915_READ(SFUSE_STRAP);
  10659. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10660. intel_ddi_init(dev, PORT_B);
  10661. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10662. intel_ddi_init(dev, PORT_C);
  10663. if (found & SFUSE_STRAP_DDID_DETECTED)
  10664. intel_ddi_init(dev, PORT_D);
  10665. } else if (HAS_PCH_SPLIT(dev)) {
  10666. int found;
  10667. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10668. if (has_edp_a(dev))
  10669. intel_dp_init(dev, DP_A, PORT_A);
  10670. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10671. /* PCH SDVOB multiplex with HDMIB */
  10672. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10673. if (!found)
  10674. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10675. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10676. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10677. }
  10678. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10679. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10680. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10681. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10682. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10683. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10684. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10685. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10686. } else if (IS_VALLEYVIEW(dev)) {
  10687. /*
  10688. * The DP_DETECTED bit is the latched state of the DDC
  10689. * SDA pin at boot. However since eDP doesn't require DDC
  10690. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10691. * eDP ports may have been muxed to an alternate function.
  10692. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10693. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10694. * detect eDP ports.
  10695. */
  10696. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  10697. !intel_dp_is_edp(dev, PORT_B))
  10698. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10699. PORT_B);
  10700. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10701. intel_dp_is_edp(dev, PORT_B))
  10702. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10703. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  10704. !intel_dp_is_edp(dev, PORT_C))
  10705. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10706. PORT_C);
  10707. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10708. intel_dp_is_edp(dev, PORT_C))
  10709. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10710. if (IS_CHERRYVIEW(dev)) {
  10711. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10712. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10713. PORT_D);
  10714. /* eDP not supported on port D, so don't check VBT */
  10715. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10716. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10717. }
  10718. intel_dsi_init(dev);
  10719. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10720. bool found = false;
  10721. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10722. DRM_DEBUG_KMS("probing SDVOB\n");
  10723. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10724. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10725. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10726. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10727. }
  10728. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10729. intel_dp_init(dev, DP_B, PORT_B);
  10730. }
  10731. /* Before G4X SDVOC doesn't have its own detect register */
  10732. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10733. DRM_DEBUG_KMS("probing SDVOC\n");
  10734. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10735. }
  10736. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10737. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10738. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10739. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10740. }
  10741. if (SUPPORTS_INTEGRATED_DP(dev))
  10742. intel_dp_init(dev, DP_C, PORT_C);
  10743. }
  10744. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10745. (I915_READ(DP_D) & DP_DETECTED))
  10746. intel_dp_init(dev, DP_D, PORT_D);
  10747. } else if (IS_GEN2(dev))
  10748. intel_dvo_init(dev);
  10749. if (SUPPORTS_TV(dev))
  10750. intel_tv_init(dev);
  10751. /*
  10752. * FIXME: We don't have full atomic support yet, but we want to be
  10753. * able to enable/test plane updates via the atomic interface in the
  10754. * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
  10755. * will take some atomic codepaths to lookup properties during
  10756. * drmModeGetConnector() that unconditionally dereference
  10757. * connector->state.
  10758. *
  10759. * We create a dummy connector state here for each connector to ensure
  10760. * the DRM core doesn't try to dereference a NULL connector->state.
  10761. * The actual connector properties will never be updated or contain
  10762. * useful information, but since we're doing this specifically for
  10763. * testing/debug of the plane operations (and only when a specific
  10764. * kernel module option is given), that shouldn't really matter.
  10765. *
  10766. * Once atomic support for crtc's + connectors lands, this loop should
  10767. * be removed since we'll be setting up real connector state, which
  10768. * will contain Intel-specific properties.
  10769. */
  10770. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  10771. list_for_each_entry(connector,
  10772. &dev->mode_config.connector_list,
  10773. head) {
  10774. if (!WARN_ON(connector->state)) {
  10775. connector->state =
  10776. kzalloc(sizeof(*connector->state),
  10777. GFP_KERNEL);
  10778. }
  10779. }
  10780. }
  10781. intel_psr_init(dev);
  10782. for_each_intel_encoder(dev, encoder) {
  10783. encoder->base.possible_crtcs = encoder->crtc_mask;
  10784. encoder->base.possible_clones =
  10785. intel_encoder_clones(encoder);
  10786. }
  10787. intel_init_pch_refclk(dev);
  10788. drm_helper_move_panel_connectors_to_head(dev);
  10789. }
  10790. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10791. {
  10792. struct drm_device *dev = fb->dev;
  10793. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10794. drm_framebuffer_cleanup(fb);
  10795. mutex_lock(&dev->struct_mutex);
  10796. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10797. drm_gem_object_unreference(&intel_fb->obj->base);
  10798. mutex_unlock(&dev->struct_mutex);
  10799. kfree(intel_fb);
  10800. }
  10801. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10802. struct drm_file *file,
  10803. unsigned int *handle)
  10804. {
  10805. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10806. struct drm_i915_gem_object *obj = intel_fb->obj;
  10807. return drm_gem_handle_create(file, &obj->base, handle);
  10808. }
  10809. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10810. .destroy = intel_user_framebuffer_destroy,
  10811. .create_handle = intel_user_framebuffer_create_handle,
  10812. };
  10813. static
  10814. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  10815. uint32_t pixel_format)
  10816. {
  10817. u32 gen = INTEL_INFO(dev)->gen;
  10818. if (gen >= 9) {
  10819. /* "The stride in bytes must not exceed the of the size of 8K
  10820. * pixels and 32K bytes."
  10821. */
  10822. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  10823. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10824. return 32*1024;
  10825. } else if (gen >= 4) {
  10826. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  10827. return 16*1024;
  10828. else
  10829. return 32*1024;
  10830. } else if (gen >= 3) {
  10831. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  10832. return 8*1024;
  10833. else
  10834. return 16*1024;
  10835. } else {
  10836. /* XXX DSPC is limited to 4k tiled */
  10837. return 8*1024;
  10838. }
  10839. }
  10840. static int intel_framebuffer_init(struct drm_device *dev,
  10841. struct intel_framebuffer *intel_fb,
  10842. struct drm_mode_fb_cmd2 *mode_cmd,
  10843. struct drm_i915_gem_object *obj)
  10844. {
  10845. unsigned int aligned_height;
  10846. int ret;
  10847. u32 pitch_limit, stride_alignment;
  10848. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10849. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  10850. /* Enforce that fb modifier and tiling mode match, but only for
  10851. * X-tiled. This is needed for FBC. */
  10852. if (!!(obj->tiling_mode == I915_TILING_X) !=
  10853. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  10854. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  10855. return -EINVAL;
  10856. }
  10857. } else {
  10858. if (obj->tiling_mode == I915_TILING_X)
  10859. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  10860. else if (obj->tiling_mode == I915_TILING_Y) {
  10861. DRM_DEBUG("No Y tiling for legacy addfb\n");
  10862. return -EINVAL;
  10863. }
  10864. }
  10865. /* Passed in modifier sanity checking. */
  10866. switch (mode_cmd->modifier[0]) {
  10867. case I915_FORMAT_MOD_Y_TILED:
  10868. case I915_FORMAT_MOD_Yf_TILED:
  10869. if (INTEL_INFO(dev)->gen < 9) {
  10870. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  10871. mode_cmd->modifier[0]);
  10872. return -EINVAL;
  10873. }
  10874. case DRM_FORMAT_MOD_NONE:
  10875. case I915_FORMAT_MOD_X_TILED:
  10876. break;
  10877. default:
  10878. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  10879. mode_cmd->modifier[0]);
  10880. return -EINVAL;
  10881. }
  10882. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  10883. mode_cmd->pixel_format);
  10884. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  10885. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  10886. mode_cmd->pitches[0], stride_alignment);
  10887. return -EINVAL;
  10888. }
  10889. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  10890. mode_cmd->pixel_format);
  10891. if (mode_cmd->pitches[0] > pitch_limit) {
  10892. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  10893. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  10894. "tiled" : "linear",
  10895. mode_cmd->pitches[0], pitch_limit);
  10896. return -EINVAL;
  10897. }
  10898. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  10899. mode_cmd->pitches[0] != obj->stride) {
  10900. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10901. mode_cmd->pitches[0], obj->stride);
  10902. return -EINVAL;
  10903. }
  10904. /* Reject formats not supported by any plane early. */
  10905. switch (mode_cmd->pixel_format) {
  10906. case DRM_FORMAT_C8:
  10907. case DRM_FORMAT_RGB565:
  10908. case DRM_FORMAT_XRGB8888:
  10909. case DRM_FORMAT_ARGB8888:
  10910. break;
  10911. case DRM_FORMAT_XRGB1555:
  10912. case DRM_FORMAT_ARGB1555:
  10913. if (INTEL_INFO(dev)->gen > 3) {
  10914. DRM_DEBUG("unsupported pixel format: %s\n",
  10915. drm_get_format_name(mode_cmd->pixel_format));
  10916. return -EINVAL;
  10917. }
  10918. break;
  10919. case DRM_FORMAT_XBGR8888:
  10920. case DRM_FORMAT_ABGR8888:
  10921. case DRM_FORMAT_XRGB2101010:
  10922. case DRM_FORMAT_ARGB2101010:
  10923. case DRM_FORMAT_XBGR2101010:
  10924. case DRM_FORMAT_ABGR2101010:
  10925. if (INTEL_INFO(dev)->gen < 4) {
  10926. DRM_DEBUG("unsupported pixel format: %s\n",
  10927. drm_get_format_name(mode_cmd->pixel_format));
  10928. return -EINVAL;
  10929. }
  10930. break;
  10931. case DRM_FORMAT_YUYV:
  10932. case DRM_FORMAT_UYVY:
  10933. case DRM_FORMAT_YVYU:
  10934. case DRM_FORMAT_VYUY:
  10935. if (INTEL_INFO(dev)->gen < 5) {
  10936. DRM_DEBUG("unsupported pixel format: %s\n",
  10937. drm_get_format_name(mode_cmd->pixel_format));
  10938. return -EINVAL;
  10939. }
  10940. break;
  10941. default:
  10942. DRM_DEBUG("unsupported pixel format: %s\n",
  10943. drm_get_format_name(mode_cmd->pixel_format));
  10944. return -EINVAL;
  10945. }
  10946. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10947. if (mode_cmd->offsets[0] != 0)
  10948. return -EINVAL;
  10949. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  10950. mode_cmd->pixel_format,
  10951. mode_cmd->modifier[0]);
  10952. /* FIXME drm helper for size checks (especially planar formats)? */
  10953. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10954. return -EINVAL;
  10955. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10956. intel_fb->obj = obj;
  10957. intel_fb->obj->framebuffer_references++;
  10958. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10959. if (ret) {
  10960. DRM_ERROR("framebuffer init failed %d\n", ret);
  10961. return ret;
  10962. }
  10963. return 0;
  10964. }
  10965. static struct drm_framebuffer *
  10966. intel_user_framebuffer_create(struct drm_device *dev,
  10967. struct drm_file *filp,
  10968. struct drm_mode_fb_cmd2 *mode_cmd)
  10969. {
  10970. struct drm_i915_gem_object *obj;
  10971. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10972. mode_cmd->handles[0]));
  10973. if (&obj->base == NULL)
  10974. return ERR_PTR(-ENOENT);
  10975. return intel_framebuffer_create(dev, mode_cmd, obj);
  10976. }
  10977. #ifndef CONFIG_DRM_I915_FBDEV
  10978. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10979. {
  10980. }
  10981. #endif
  10982. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10983. .fb_create = intel_user_framebuffer_create,
  10984. .output_poll_changed = intel_fbdev_output_poll_changed,
  10985. .atomic_check = intel_atomic_check,
  10986. .atomic_commit = intel_atomic_commit,
  10987. };
  10988. /* Set up chip specific display functions */
  10989. static void intel_init_display(struct drm_device *dev)
  10990. {
  10991. struct drm_i915_private *dev_priv = dev->dev_private;
  10992. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10993. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10994. else if (IS_CHERRYVIEW(dev))
  10995. dev_priv->display.find_dpll = chv_find_best_dpll;
  10996. else if (IS_VALLEYVIEW(dev))
  10997. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10998. else if (IS_PINEVIEW(dev))
  10999. dev_priv->display.find_dpll = pnv_find_best_dpll;
  11000. else
  11001. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  11002. if (INTEL_INFO(dev)->gen >= 9) {
  11003. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11004. dev_priv->display.get_initial_plane_config =
  11005. skylake_get_initial_plane_config;
  11006. dev_priv->display.crtc_compute_clock =
  11007. haswell_crtc_compute_clock;
  11008. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11009. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11010. dev_priv->display.off = ironlake_crtc_off;
  11011. dev_priv->display.update_primary_plane =
  11012. skylake_update_primary_plane;
  11013. } else if (HAS_DDI(dev)) {
  11014. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11015. dev_priv->display.get_initial_plane_config =
  11016. ironlake_get_initial_plane_config;
  11017. dev_priv->display.crtc_compute_clock =
  11018. haswell_crtc_compute_clock;
  11019. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11020. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11021. dev_priv->display.off = ironlake_crtc_off;
  11022. dev_priv->display.update_primary_plane =
  11023. ironlake_update_primary_plane;
  11024. } else if (HAS_PCH_SPLIT(dev)) {
  11025. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11026. dev_priv->display.get_initial_plane_config =
  11027. ironlake_get_initial_plane_config;
  11028. dev_priv->display.crtc_compute_clock =
  11029. ironlake_crtc_compute_clock;
  11030. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11031. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11032. dev_priv->display.off = ironlake_crtc_off;
  11033. dev_priv->display.update_primary_plane =
  11034. ironlake_update_primary_plane;
  11035. } else if (IS_VALLEYVIEW(dev)) {
  11036. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11037. dev_priv->display.get_initial_plane_config =
  11038. i9xx_get_initial_plane_config;
  11039. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11040. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11041. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11042. dev_priv->display.off = i9xx_crtc_off;
  11043. dev_priv->display.update_primary_plane =
  11044. i9xx_update_primary_plane;
  11045. } else {
  11046. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11047. dev_priv->display.get_initial_plane_config =
  11048. i9xx_get_initial_plane_config;
  11049. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11050. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11051. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11052. dev_priv->display.off = i9xx_crtc_off;
  11053. dev_priv->display.update_primary_plane =
  11054. i9xx_update_primary_plane;
  11055. }
  11056. /* Returns the core display clock speed */
  11057. if (IS_VALLEYVIEW(dev))
  11058. dev_priv->display.get_display_clock_speed =
  11059. valleyview_get_display_clock_speed;
  11060. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  11061. dev_priv->display.get_display_clock_speed =
  11062. i945_get_display_clock_speed;
  11063. else if (IS_I915G(dev))
  11064. dev_priv->display.get_display_clock_speed =
  11065. i915_get_display_clock_speed;
  11066. else if (IS_I945GM(dev) || IS_845G(dev))
  11067. dev_priv->display.get_display_clock_speed =
  11068. i9xx_misc_get_display_clock_speed;
  11069. else if (IS_PINEVIEW(dev))
  11070. dev_priv->display.get_display_clock_speed =
  11071. pnv_get_display_clock_speed;
  11072. else if (IS_I915GM(dev))
  11073. dev_priv->display.get_display_clock_speed =
  11074. i915gm_get_display_clock_speed;
  11075. else if (IS_I865G(dev))
  11076. dev_priv->display.get_display_clock_speed =
  11077. i865_get_display_clock_speed;
  11078. else if (IS_I85X(dev))
  11079. dev_priv->display.get_display_clock_speed =
  11080. i855_get_display_clock_speed;
  11081. else /* 852, 830 */
  11082. dev_priv->display.get_display_clock_speed =
  11083. i830_get_display_clock_speed;
  11084. if (IS_GEN5(dev)) {
  11085. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11086. } else if (IS_GEN6(dev)) {
  11087. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11088. } else if (IS_IVYBRIDGE(dev)) {
  11089. /* FIXME: detect B0+ stepping and use auto training */
  11090. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11091. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  11092. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11093. } else if (IS_VALLEYVIEW(dev)) {
  11094. dev_priv->display.modeset_global_resources =
  11095. valleyview_modeset_global_resources;
  11096. }
  11097. switch (INTEL_INFO(dev)->gen) {
  11098. case 2:
  11099. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  11100. break;
  11101. case 3:
  11102. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  11103. break;
  11104. case 4:
  11105. case 5:
  11106. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  11107. break;
  11108. case 6:
  11109. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  11110. break;
  11111. case 7:
  11112. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  11113. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  11114. break;
  11115. case 9:
  11116. /* Drop through - unsupported since execlist only. */
  11117. default:
  11118. /* Default just returns -ENODEV to indicate unsupported */
  11119. dev_priv->display.queue_flip = intel_default_queue_flip;
  11120. }
  11121. intel_panel_init_backlight_funcs(dev);
  11122. mutex_init(&dev_priv->pps_mutex);
  11123. }
  11124. /*
  11125. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  11126. * resume, or other times. This quirk makes sure that's the case for
  11127. * affected systems.
  11128. */
  11129. static void quirk_pipea_force(struct drm_device *dev)
  11130. {
  11131. struct drm_i915_private *dev_priv = dev->dev_private;
  11132. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  11133. DRM_INFO("applying pipe a force quirk\n");
  11134. }
  11135. static void quirk_pipeb_force(struct drm_device *dev)
  11136. {
  11137. struct drm_i915_private *dev_priv = dev->dev_private;
  11138. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  11139. DRM_INFO("applying pipe b force quirk\n");
  11140. }
  11141. /*
  11142. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11143. */
  11144. static void quirk_ssc_force_disable(struct drm_device *dev)
  11145. {
  11146. struct drm_i915_private *dev_priv = dev->dev_private;
  11147. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11148. DRM_INFO("applying lvds SSC disable quirk\n");
  11149. }
  11150. /*
  11151. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11152. * brightness value
  11153. */
  11154. static void quirk_invert_brightness(struct drm_device *dev)
  11155. {
  11156. struct drm_i915_private *dev_priv = dev->dev_private;
  11157. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11158. DRM_INFO("applying inverted panel brightness quirk\n");
  11159. }
  11160. /* Some VBT's incorrectly indicate no backlight is present */
  11161. static void quirk_backlight_present(struct drm_device *dev)
  11162. {
  11163. struct drm_i915_private *dev_priv = dev->dev_private;
  11164. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11165. DRM_INFO("applying backlight present quirk\n");
  11166. }
  11167. struct intel_quirk {
  11168. int device;
  11169. int subsystem_vendor;
  11170. int subsystem_device;
  11171. void (*hook)(struct drm_device *dev);
  11172. };
  11173. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11174. struct intel_dmi_quirk {
  11175. void (*hook)(struct drm_device *dev);
  11176. const struct dmi_system_id (*dmi_id_list)[];
  11177. };
  11178. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11179. {
  11180. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11181. return 1;
  11182. }
  11183. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11184. {
  11185. .dmi_id_list = &(const struct dmi_system_id[]) {
  11186. {
  11187. .callback = intel_dmi_reverse_brightness,
  11188. .ident = "NCR Corporation",
  11189. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11190. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11191. },
  11192. },
  11193. { } /* terminating entry */
  11194. },
  11195. .hook = quirk_invert_brightness,
  11196. },
  11197. };
  11198. static struct intel_quirk intel_quirks[] = {
  11199. /* HP Mini needs pipe A force quirk (LP: #322104) */
  11200. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  11201. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  11202. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  11203. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  11204. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  11205. /* 830 needs to leave pipe A & dpll A up */
  11206. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  11207. /* 830 needs to leave pipe B & dpll B up */
  11208. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  11209. /* Lenovo U160 cannot use SSC on LVDS */
  11210. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11211. /* Sony Vaio Y cannot use SSC on LVDS */
  11212. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11213. /* Acer Aspire 5734Z must invert backlight brightness */
  11214. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11215. /* Acer/eMachines G725 */
  11216. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11217. /* Acer/eMachines e725 */
  11218. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11219. /* Acer/Packard Bell NCL20 */
  11220. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11221. /* Acer Aspire 4736Z */
  11222. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11223. /* Acer Aspire 5336 */
  11224. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11225. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11226. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11227. /* Acer C720 Chromebook (Core i3 4005U) */
  11228. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11229. /* Apple Macbook 2,1 (Core 2 T7400) */
  11230. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11231. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11232. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11233. /* HP Chromebook 14 (Celeron 2955U) */
  11234. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11235. /* Dell Chromebook 11 */
  11236. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11237. };
  11238. static void intel_init_quirks(struct drm_device *dev)
  11239. {
  11240. struct pci_dev *d = dev->pdev;
  11241. int i;
  11242. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11243. struct intel_quirk *q = &intel_quirks[i];
  11244. if (d->device == q->device &&
  11245. (d->subsystem_vendor == q->subsystem_vendor ||
  11246. q->subsystem_vendor == PCI_ANY_ID) &&
  11247. (d->subsystem_device == q->subsystem_device ||
  11248. q->subsystem_device == PCI_ANY_ID))
  11249. q->hook(dev);
  11250. }
  11251. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11252. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  11253. intel_dmi_quirks[i].hook(dev);
  11254. }
  11255. }
  11256. /* Disable the VGA plane that we never use */
  11257. static void i915_disable_vga(struct drm_device *dev)
  11258. {
  11259. struct drm_i915_private *dev_priv = dev->dev_private;
  11260. u8 sr1;
  11261. u32 vga_reg = i915_vgacntrl_reg(dev);
  11262. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  11263. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  11264. outb(SR01, VGA_SR_INDEX);
  11265. sr1 = inb(VGA_SR_DATA);
  11266. outb(sr1 | 1<<5, VGA_SR_DATA);
  11267. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  11268. udelay(300);
  11269. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  11270. POSTING_READ(vga_reg);
  11271. }
  11272. void intel_modeset_init_hw(struct drm_device *dev)
  11273. {
  11274. intel_prepare_ddi(dev);
  11275. if (IS_VALLEYVIEW(dev))
  11276. vlv_update_cdclk(dev);
  11277. intel_init_clock_gating(dev);
  11278. intel_enable_gt_powersave(dev);
  11279. }
  11280. void intel_modeset_init(struct drm_device *dev)
  11281. {
  11282. struct drm_i915_private *dev_priv = dev->dev_private;
  11283. int sprite, ret;
  11284. enum pipe pipe;
  11285. struct intel_crtc *crtc;
  11286. drm_mode_config_init(dev);
  11287. dev->mode_config.min_width = 0;
  11288. dev->mode_config.min_height = 0;
  11289. dev->mode_config.preferred_depth = 24;
  11290. dev->mode_config.prefer_shadow = 1;
  11291. dev->mode_config.allow_fb_modifiers = true;
  11292. dev->mode_config.funcs = &intel_mode_funcs;
  11293. intel_init_quirks(dev);
  11294. intel_init_pm(dev);
  11295. if (INTEL_INFO(dev)->num_pipes == 0)
  11296. return;
  11297. intel_init_display(dev);
  11298. intel_init_audio(dev);
  11299. if (IS_GEN2(dev)) {
  11300. dev->mode_config.max_width = 2048;
  11301. dev->mode_config.max_height = 2048;
  11302. } else if (IS_GEN3(dev)) {
  11303. dev->mode_config.max_width = 4096;
  11304. dev->mode_config.max_height = 4096;
  11305. } else {
  11306. dev->mode_config.max_width = 8192;
  11307. dev->mode_config.max_height = 8192;
  11308. }
  11309. if (IS_845G(dev) || IS_I865G(dev)) {
  11310. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  11311. dev->mode_config.cursor_height = 1023;
  11312. } else if (IS_GEN2(dev)) {
  11313. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  11314. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  11315. } else {
  11316. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  11317. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  11318. }
  11319. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  11320. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  11321. INTEL_INFO(dev)->num_pipes,
  11322. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  11323. for_each_pipe(dev_priv, pipe) {
  11324. intel_crtc_init(dev, pipe);
  11325. for_each_sprite(dev_priv, pipe, sprite) {
  11326. ret = intel_plane_init(dev, pipe, sprite);
  11327. if (ret)
  11328. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  11329. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  11330. }
  11331. }
  11332. intel_init_dpio(dev);
  11333. intel_shared_dpll_init(dev);
  11334. /* Just disable it once at startup */
  11335. i915_disable_vga(dev);
  11336. intel_setup_outputs(dev);
  11337. /* Just in case the BIOS is doing something questionable. */
  11338. intel_fbc_disable(dev);
  11339. drm_modeset_lock_all(dev);
  11340. intel_modeset_setup_hw_state(dev, false);
  11341. drm_modeset_unlock_all(dev);
  11342. for_each_intel_crtc(dev, crtc) {
  11343. if (!crtc->active)
  11344. continue;
  11345. /*
  11346. * Note that reserving the BIOS fb up front prevents us
  11347. * from stuffing other stolen allocations like the ring
  11348. * on top. This prevents some ugliness at boot time, and
  11349. * can even allow for smooth boot transitions if the BIOS
  11350. * fb is large enough for the active pipe configuration.
  11351. */
  11352. if (dev_priv->display.get_initial_plane_config) {
  11353. dev_priv->display.get_initial_plane_config(crtc,
  11354. &crtc->plane_config);
  11355. /*
  11356. * If the fb is shared between multiple heads, we'll
  11357. * just get the first one.
  11358. */
  11359. intel_find_plane_obj(crtc, &crtc->plane_config);
  11360. }
  11361. }
  11362. }
  11363. static void intel_enable_pipe_a(struct drm_device *dev)
  11364. {
  11365. struct intel_connector *connector;
  11366. struct drm_connector *crt = NULL;
  11367. struct intel_load_detect_pipe load_detect_temp;
  11368. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  11369. /* We can't just switch on the pipe A, we need to set things up with a
  11370. * proper mode and output configuration. As a gross hack, enable pipe A
  11371. * by enabling the load detect pipe once. */
  11372. for_each_intel_connector(dev, connector) {
  11373. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  11374. crt = &connector->base;
  11375. break;
  11376. }
  11377. }
  11378. if (!crt)
  11379. return;
  11380. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  11381. intel_release_load_detect_pipe(crt, &load_detect_temp);
  11382. }
  11383. static bool
  11384. intel_check_plane_mapping(struct intel_crtc *crtc)
  11385. {
  11386. struct drm_device *dev = crtc->base.dev;
  11387. struct drm_i915_private *dev_priv = dev->dev_private;
  11388. u32 reg, val;
  11389. if (INTEL_INFO(dev)->num_pipes == 1)
  11390. return true;
  11391. reg = DSPCNTR(!crtc->plane);
  11392. val = I915_READ(reg);
  11393. if ((val & DISPLAY_PLANE_ENABLE) &&
  11394. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  11395. return false;
  11396. return true;
  11397. }
  11398. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  11399. {
  11400. struct drm_device *dev = crtc->base.dev;
  11401. struct drm_i915_private *dev_priv = dev->dev_private;
  11402. u32 reg;
  11403. /* Clear any frame start delays used for debugging left by the BIOS */
  11404. reg = PIPECONF(crtc->config->cpu_transcoder);
  11405. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  11406. /* restore vblank interrupts to correct state */
  11407. drm_crtc_vblank_reset(&crtc->base);
  11408. if (crtc->active) {
  11409. update_scanline_offset(crtc);
  11410. drm_crtc_vblank_on(&crtc->base);
  11411. }
  11412. /* We need to sanitize the plane -> pipe mapping first because this will
  11413. * disable the crtc (and hence change the state) if it is wrong. Note
  11414. * that gen4+ has a fixed plane -> pipe mapping. */
  11415. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  11416. struct intel_connector *connector;
  11417. bool plane;
  11418. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11419. crtc->base.base.id);
  11420. /* Pipe has the wrong plane attached and the plane is active.
  11421. * Temporarily change the plane mapping and disable everything
  11422. * ... */
  11423. plane = crtc->plane;
  11424. crtc->plane = !plane;
  11425. crtc->primary_enabled = true;
  11426. dev_priv->display.crtc_disable(&crtc->base);
  11427. crtc->plane = plane;
  11428. /* ... and break all links. */
  11429. for_each_intel_connector(dev, connector) {
  11430. if (connector->encoder->base.crtc != &crtc->base)
  11431. continue;
  11432. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11433. connector->base.encoder = NULL;
  11434. }
  11435. /* multiple connectors may have the same encoder:
  11436. * handle them and break crtc link separately */
  11437. for_each_intel_connector(dev, connector)
  11438. if (connector->encoder->base.crtc == &crtc->base) {
  11439. connector->encoder->base.crtc = NULL;
  11440. connector->encoder->connectors_active = false;
  11441. }
  11442. WARN_ON(crtc->active);
  11443. crtc->base.state->enable = false;
  11444. crtc->base.enabled = false;
  11445. }
  11446. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11447. crtc->pipe == PIPE_A && !crtc->active) {
  11448. /* BIOS forgot to enable pipe A, this mostly happens after
  11449. * resume. Force-enable the pipe to fix this, the update_dpms
  11450. * call below we restore the pipe to the right state, but leave
  11451. * the required bits on. */
  11452. intel_enable_pipe_a(dev);
  11453. }
  11454. /* Adjust the state of the output pipe according to whether we
  11455. * have active connectors/encoders. */
  11456. intel_crtc_update_dpms(&crtc->base);
  11457. if (crtc->active != crtc->base.state->enable) {
  11458. struct intel_encoder *encoder;
  11459. /* This can happen either due to bugs in the get_hw_state
  11460. * functions or because the pipe is force-enabled due to the
  11461. * pipe A quirk. */
  11462. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11463. crtc->base.base.id,
  11464. crtc->base.state->enable ? "enabled" : "disabled",
  11465. crtc->active ? "enabled" : "disabled");
  11466. crtc->base.state->enable = crtc->active;
  11467. crtc->base.enabled = crtc->active;
  11468. /* Because we only establish the connector -> encoder ->
  11469. * crtc links if something is active, this means the
  11470. * crtc is now deactivated. Break the links. connector
  11471. * -> encoder links are only establish when things are
  11472. * actually up, hence no need to break them. */
  11473. WARN_ON(crtc->active);
  11474. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11475. WARN_ON(encoder->connectors_active);
  11476. encoder->base.crtc = NULL;
  11477. }
  11478. }
  11479. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11480. /*
  11481. * We start out with underrun reporting disabled to avoid races.
  11482. * For correct bookkeeping mark this on active crtcs.
  11483. *
  11484. * Also on gmch platforms we dont have any hardware bits to
  11485. * disable the underrun reporting. Which means we need to start
  11486. * out with underrun reporting disabled also on inactive pipes,
  11487. * since otherwise we'll complain about the garbage we read when
  11488. * e.g. coming up after runtime pm.
  11489. *
  11490. * No protection against concurrent access is required - at
  11491. * worst a fifo underrun happens which also sets this to false.
  11492. */
  11493. crtc->cpu_fifo_underrun_disabled = true;
  11494. crtc->pch_fifo_underrun_disabled = true;
  11495. }
  11496. }
  11497. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11498. {
  11499. struct intel_connector *connector;
  11500. struct drm_device *dev = encoder->base.dev;
  11501. /* We need to check both for a crtc link (meaning that the
  11502. * encoder is active and trying to read from a pipe) and the
  11503. * pipe itself being active. */
  11504. bool has_active_crtc = encoder->base.crtc &&
  11505. to_intel_crtc(encoder->base.crtc)->active;
  11506. if (encoder->connectors_active && !has_active_crtc) {
  11507. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11508. encoder->base.base.id,
  11509. encoder->base.name);
  11510. /* Connector is active, but has no active pipe. This is
  11511. * fallout from our resume register restoring. Disable
  11512. * the encoder manually again. */
  11513. if (encoder->base.crtc) {
  11514. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11515. encoder->base.base.id,
  11516. encoder->base.name);
  11517. encoder->disable(encoder);
  11518. if (encoder->post_disable)
  11519. encoder->post_disable(encoder);
  11520. }
  11521. encoder->base.crtc = NULL;
  11522. encoder->connectors_active = false;
  11523. /* Inconsistent output/port/pipe state happens presumably due to
  11524. * a bug in one of the get_hw_state functions. Or someplace else
  11525. * in our code, like the register restore mess on resume. Clamp
  11526. * things to off as a safer default. */
  11527. for_each_intel_connector(dev, connector) {
  11528. if (connector->encoder != encoder)
  11529. continue;
  11530. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11531. connector->base.encoder = NULL;
  11532. }
  11533. }
  11534. /* Enabled encoders without active connectors will be fixed in
  11535. * the crtc fixup. */
  11536. }
  11537. void i915_redisable_vga_power_on(struct drm_device *dev)
  11538. {
  11539. struct drm_i915_private *dev_priv = dev->dev_private;
  11540. u32 vga_reg = i915_vgacntrl_reg(dev);
  11541. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11542. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11543. i915_disable_vga(dev);
  11544. }
  11545. }
  11546. void i915_redisable_vga(struct drm_device *dev)
  11547. {
  11548. struct drm_i915_private *dev_priv = dev->dev_private;
  11549. /* This function can be called both from intel_modeset_setup_hw_state or
  11550. * at a very early point in our resume sequence, where the power well
  11551. * structures are not yet restored. Since this function is at a very
  11552. * paranoid "someone might have enabled VGA while we were not looking"
  11553. * level, just check if the power well is enabled instead of trying to
  11554. * follow the "don't touch the power well if we don't need it" policy
  11555. * the rest of the driver uses. */
  11556. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11557. return;
  11558. i915_redisable_vga_power_on(dev);
  11559. }
  11560. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11561. {
  11562. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11563. if (!crtc->active)
  11564. return false;
  11565. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11566. }
  11567. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11568. {
  11569. struct drm_i915_private *dev_priv = dev->dev_private;
  11570. enum pipe pipe;
  11571. struct intel_crtc *crtc;
  11572. struct intel_encoder *encoder;
  11573. struct intel_connector *connector;
  11574. int i;
  11575. for_each_intel_crtc(dev, crtc) {
  11576. memset(crtc->config, 0, sizeof(*crtc->config));
  11577. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11578. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11579. crtc->config);
  11580. crtc->base.state->enable = crtc->active;
  11581. crtc->base.enabled = crtc->active;
  11582. crtc->primary_enabled = primary_get_hw_state(crtc);
  11583. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11584. crtc->base.base.id,
  11585. crtc->active ? "enabled" : "disabled");
  11586. }
  11587. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11588. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11589. pll->on = pll->get_hw_state(dev_priv, pll,
  11590. &pll->config.hw_state);
  11591. pll->active = 0;
  11592. pll->config.crtc_mask = 0;
  11593. for_each_intel_crtc(dev, crtc) {
  11594. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11595. pll->active++;
  11596. pll->config.crtc_mask |= 1 << crtc->pipe;
  11597. }
  11598. }
  11599. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11600. pll->name, pll->config.crtc_mask, pll->on);
  11601. if (pll->config.crtc_mask)
  11602. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11603. }
  11604. for_each_intel_encoder(dev, encoder) {
  11605. pipe = 0;
  11606. if (encoder->get_hw_state(encoder, &pipe)) {
  11607. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11608. encoder->base.crtc = &crtc->base;
  11609. encoder->get_config(encoder, crtc->config);
  11610. } else {
  11611. encoder->base.crtc = NULL;
  11612. }
  11613. encoder->connectors_active = false;
  11614. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11615. encoder->base.base.id,
  11616. encoder->base.name,
  11617. encoder->base.crtc ? "enabled" : "disabled",
  11618. pipe_name(pipe));
  11619. }
  11620. for_each_intel_connector(dev, connector) {
  11621. if (connector->get_hw_state(connector)) {
  11622. connector->base.dpms = DRM_MODE_DPMS_ON;
  11623. connector->encoder->connectors_active = true;
  11624. connector->base.encoder = &connector->encoder->base;
  11625. } else {
  11626. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11627. connector->base.encoder = NULL;
  11628. }
  11629. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11630. connector->base.base.id,
  11631. connector->base.name,
  11632. connector->base.encoder ? "enabled" : "disabled");
  11633. }
  11634. }
  11635. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11636. * and i915 state tracking structures. */
  11637. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11638. bool force_restore)
  11639. {
  11640. struct drm_i915_private *dev_priv = dev->dev_private;
  11641. enum pipe pipe;
  11642. struct intel_crtc *crtc;
  11643. struct intel_encoder *encoder;
  11644. int i;
  11645. intel_modeset_readout_hw_state(dev);
  11646. /*
  11647. * Now that we have the config, copy it to each CRTC struct
  11648. * Note that this could go away if we move to using crtc_config
  11649. * checking everywhere.
  11650. */
  11651. for_each_intel_crtc(dev, crtc) {
  11652. if (crtc->active && i915.fastboot) {
  11653. intel_mode_from_pipe_config(&crtc->base.mode,
  11654. crtc->config);
  11655. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11656. crtc->base.base.id);
  11657. drm_mode_debug_printmodeline(&crtc->base.mode);
  11658. }
  11659. }
  11660. /* HW state is read out, now we need to sanitize this mess. */
  11661. for_each_intel_encoder(dev, encoder) {
  11662. intel_sanitize_encoder(encoder);
  11663. }
  11664. for_each_pipe(dev_priv, pipe) {
  11665. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11666. intel_sanitize_crtc(crtc);
  11667. intel_dump_pipe_config(crtc, crtc->config,
  11668. "[setup_hw_state]");
  11669. }
  11670. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11671. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11672. if (!pll->on || pll->active)
  11673. continue;
  11674. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11675. pll->disable(dev_priv, pll);
  11676. pll->on = false;
  11677. }
  11678. if (IS_GEN9(dev))
  11679. skl_wm_get_hw_state(dev);
  11680. else if (HAS_PCH_SPLIT(dev))
  11681. ilk_wm_get_hw_state(dev);
  11682. if (force_restore) {
  11683. i915_redisable_vga(dev);
  11684. /*
  11685. * We need to use raw interfaces for restoring state to avoid
  11686. * checking (bogus) intermediate states.
  11687. */
  11688. for_each_pipe(dev_priv, pipe) {
  11689. struct drm_crtc *crtc =
  11690. dev_priv->pipe_to_crtc_mapping[pipe];
  11691. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11692. crtc->primary->fb);
  11693. }
  11694. } else {
  11695. intel_modeset_update_staged_output_state(dev);
  11696. }
  11697. intel_modeset_check_state(dev);
  11698. }
  11699. void intel_modeset_gem_init(struct drm_device *dev)
  11700. {
  11701. struct drm_i915_private *dev_priv = dev->dev_private;
  11702. struct drm_crtc *c;
  11703. struct drm_i915_gem_object *obj;
  11704. mutex_lock(&dev->struct_mutex);
  11705. intel_init_gt_powersave(dev);
  11706. mutex_unlock(&dev->struct_mutex);
  11707. /*
  11708. * There may be no VBT; and if the BIOS enabled SSC we can
  11709. * just keep using it to avoid unnecessary flicker. Whereas if the
  11710. * BIOS isn't using it, don't assume it will work even if the VBT
  11711. * indicates as much.
  11712. */
  11713. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11714. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11715. DREF_SSC1_ENABLE);
  11716. intel_modeset_init_hw(dev);
  11717. intel_setup_overlay(dev);
  11718. /*
  11719. * Make sure any fbs we allocated at startup are properly
  11720. * pinned & fenced. When we do the allocation it's too early
  11721. * for this.
  11722. */
  11723. mutex_lock(&dev->struct_mutex);
  11724. for_each_crtc(dev, c) {
  11725. obj = intel_fb_obj(c->primary->fb);
  11726. if (obj == NULL)
  11727. continue;
  11728. if (intel_pin_and_fence_fb_obj(c->primary,
  11729. c->primary->fb,
  11730. c->primary->state,
  11731. NULL)) {
  11732. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11733. to_intel_crtc(c)->pipe);
  11734. drm_framebuffer_unreference(c->primary->fb);
  11735. c->primary->fb = NULL;
  11736. update_state_fb(c->primary);
  11737. }
  11738. }
  11739. mutex_unlock(&dev->struct_mutex);
  11740. intel_backlight_register(dev);
  11741. }
  11742. void intel_connector_unregister(struct intel_connector *intel_connector)
  11743. {
  11744. struct drm_connector *connector = &intel_connector->base;
  11745. intel_panel_destroy_backlight(connector);
  11746. drm_connector_unregister(connector);
  11747. }
  11748. void intel_modeset_cleanup(struct drm_device *dev)
  11749. {
  11750. struct drm_i915_private *dev_priv = dev->dev_private;
  11751. struct drm_connector *connector;
  11752. intel_disable_gt_powersave(dev);
  11753. intel_backlight_unregister(dev);
  11754. /*
  11755. * Interrupts and polling as the first thing to avoid creating havoc.
  11756. * Too much stuff here (turning of connectors, ...) would
  11757. * experience fancy races otherwise.
  11758. */
  11759. intel_irq_uninstall(dev_priv);
  11760. /*
  11761. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11762. * poll handlers. Hence disable polling after hpd handling is shut down.
  11763. */
  11764. drm_kms_helper_poll_fini(dev);
  11765. mutex_lock(&dev->struct_mutex);
  11766. intel_unregister_dsm_handler();
  11767. intel_fbc_disable(dev);
  11768. mutex_unlock(&dev->struct_mutex);
  11769. /* flush any delayed tasks or pending work */
  11770. flush_scheduled_work();
  11771. /* destroy the backlight and sysfs files before encoders/connectors */
  11772. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11773. struct intel_connector *intel_connector;
  11774. intel_connector = to_intel_connector(connector);
  11775. intel_connector->unregister(intel_connector);
  11776. }
  11777. drm_mode_config_cleanup(dev);
  11778. intel_cleanup_overlay(dev);
  11779. mutex_lock(&dev->struct_mutex);
  11780. intel_cleanup_gt_powersave(dev);
  11781. mutex_unlock(&dev->struct_mutex);
  11782. }
  11783. /*
  11784. * Return which encoder is currently attached for connector.
  11785. */
  11786. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11787. {
  11788. return &intel_attached_encoder(connector)->base;
  11789. }
  11790. void intel_connector_attach_encoder(struct intel_connector *connector,
  11791. struct intel_encoder *encoder)
  11792. {
  11793. connector->encoder = encoder;
  11794. drm_mode_connector_attach_encoder(&connector->base,
  11795. &encoder->base);
  11796. }
  11797. /*
  11798. * set vga decode state - true == enable VGA decode
  11799. */
  11800. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11801. {
  11802. struct drm_i915_private *dev_priv = dev->dev_private;
  11803. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11804. u16 gmch_ctrl;
  11805. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11806. DRM_ERROR("failed to read control word\n");
  11807. return -EIO;
  11808. }
  11809. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11810. return 0;
  11811. if (state)
  11812. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11813. else
  11814. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11815. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11816. DRM_ERROR("failed to write control word\n");
  11817. return -EIO;
  11818. }
  11819. return 0;
  11820. }
  11821. struct intel_display_error_state {
  11822. u32 power_well_driver;
  11823. int num_transcoders;
  11824. struct intel_cursor_error_state {
  11825. u32 control;
  11826. u32 position;
  11827. u32 base;
  11828. u32 size;
  11829. } cursor[I915_MAX_PIPES];
  11830. struct intel_pipe_error_state {
  11831. bool power_domain_on;
  11832. u32 source;
  11833. u32 stat;
  11834. } pipe[I915_MAX_PIPES];
  11835. struct intel_plane_error_state {
  11836. u32 control;
  11837. u32 stride;
  11838. u32 size;
  11839. u32 pos;
  11840. u32 addr;
  11841. u32 surface;
  11842. u32 tile_offset;
  11843. } plane[I915_MAX_PIPES];
  11844. struct intel_transcoder_error_state {
  11845. bool power_domain_on;
  11846. enum transcoder cpu_transcoder;
  11847. u32 conf;
  11848. u32 htotal;
  11849. u32 hblank;
  11850. u32 hsync;
  11851. u32 vtotal;
  11852. u32 vblank;
  11853. u32 vsync;
  11854. } transcoder[4];
  11855. };
  11856. struct intel_display_error_state *
  11857. intel_display_capture_error_state(struct drm_device *dev)
  11858. {
  11859. struct drm_i915_private *dev_priv = dev->dev_private;
  11860. struct intel_display_error_state *error;
  11861. int transcoders[] = {
  11862. TRANSCODER_A,
  11863. TRANSCODER_B,
  11864. TRANSCODER_C,
  11865. TRANSCODER_EDP,
  11866. };
  11867. int i;
  11868. if (INTEL_INFO(dev)->num_pipes == 0)
  11869. return NULL;
  11870. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11871. if (error == NULL)
  11872. return NULL;
  11873. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11874. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11875. for_each_pipe(dev_priv, i) {
  11876. error->pipe[i].power_domain_on =
  11877. __intel_display_power_is_enabled(dev_priv,
  11878. POWER_DOMAIN_PIPE(i));
  11879. if (!error->pipe[i].power_domain_on)
  11880. continue;
  11881. error->cursor[i].control = I915_READ(CURCNTR(i));
  11882. error->cursor[i].position = I915_READ(CURPOS(i));
  11883. error->cursor[i].base = I915_READ(CURBASE(i));
  11884. error->plane[i].control = I915_READ(DSPCNTR(i));
  11885. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11886. if (INTEL_INFO(dev)->gen <= 3) {
  11887. error->plane[i].size = I915_READ(DSPSIZE(i));
  11888. error->plane[i].pos = I915_READ(DSPPOS(i));
  11889. }
  11890. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11891. error->plane[i].addr = I915_READ(DSPADDR(i));
  11892. if (INTEL_INFO(dev)->gen >= 4) {
  11893. error->plane[i].surface = I915_READ(DSPSURF(i));
  11894. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11895. }
  11896. error->pipe[i].source = I915_READ(PIPESRC(i));
  11897. if (HAS_GMCH_DISPLAY(dev))
  11898. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11899. }
  11900. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11901. if (HAS_DDI(dev_priv->dev))
  11902. error->num_transcoders++; /* Account for eDP. */
  11903. for (i = 0; i < error->num_transcoders; i++) {
  11904. enum transcoder cpu_transcoder = transcoders[i];
  11905. error->transcoder[i].power_domain_on =
  11906. __intel_display_power_is_enabled(dev_priv,
  11907. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11908. if (!error->transcoder[i].power_domain_on)
  11909. continue;
  11910. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11911. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11912. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11913. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11914. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11915. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11916. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11917. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11918. }
  11919. return error;
  11920. }
  11921. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11922. void
  11923. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11924. struct drm_device *dev,
  11925. struct intel_display_error_state *error)
  11926. {
  11927. struct drm_i915_private *dev_priv = dev->dev_private;
  11928. int i;
  11929. if (!error)
  11930. return;
  11931. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11932. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11933. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11934. error->power_well_driver);
  11935. for_each_pipe(dev_priv, i) {
  11936. err_printf(m, "Pipe [%d]:\n", i);
  11937. err_printf(m, " Power: %s\n",
  11938. error->pipe[i].power_domain_on ? "on" : "off");
  11939. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11940. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11941. err_printf(m, "Plane [%d]:\n", i);
  11942. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11943. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11944. if (INTEL_INFO(dev)->gen <= 3) {
  11945. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11946. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11947. }
  11948. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11949. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11950. if (INTEL_INFO(dev)->gen >= 4) {
  11951. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11952. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11953. }
  11954. err_printf(m, "Cursor [%d]:\n", i);
  11955. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11956. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11957. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11958. }
  11959. for (i = 0; i < error->num_transcoders; i++) {
  11960. err_printf(m, "CPU transcoder: %c\n",
  11961. transcoder_name(error->transcoder[i].cpu_transcoder));
  11962. err_printf(m, " Power: %s\n",
  11963. error->transcoder[i].power_domain_on ? "on" : "off");
  11964. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11965. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11966. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11967. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11968. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11969. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11970. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11971. }
  11972. }
  11973. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11974. {
  11975. struct intel_crtc *crtc;
  11976. for_each_intel_crtc(dev, crtc) {
  11977. struct intel_unpin_work *work;
  11978. spin_lock_irq(&dev->event_lock);
  11979. work = crtc->unpin_work;
  11980. if (work && work->event &&
  11981. work->event->base.file_priv == file) {
  11982. kfree(work->event);
  11983. work->event = NULL;
  11984. }
  11985. spin_unlock_irq(&dev->event_lock);
  11986. }
  11987. }