intel_display.c 217 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043
  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/module.h>
  27. #include <linux/input.h>
  28. #include <linux/i2c.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/vgaarb.h>
  32. #include "drmP.h"
  33. #include "intel_drv.h"
  34. #include "i915_drm.h"
  35. #include "i915_drv.h"
  36. #include "i915_trace.h"
  37. #include "drm_dp_helper.h"
  38. #include "drm_crtc_helper.h"
  39. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  40. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  41. static void intel_update_watermarks(struct drm_device *dev);
  42. static void intel_increase_pllclock(struct drm_crtc *crtc);
  43. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  44. typedef struct {
  45. /* given values */
  46. int n;
  47. int m1, m2;
  48. int p1, p2;
  49. /* derived values */
  50. int dot;
  51. int vco;
  52. int m;
  53. int p;
  54. } intel_clock_t;
  55. typedef struct {
  56. int min, max;
  57. } intel_range_t;
  58. typedef struct {
  59. int dot_limit;
  60. int p2_slow, p2_fast;
  61. } intel_p2_t;
  62. #define INTEL_P2_NUM 2
  63. typedef struct intel_limit intel_limit_t;
  64. struct intel_limit {
  65. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  66. intel_p2_t p2;
  67. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  68. int, int, intel_clock_t *);
  69. };
  70. /* FDI */
  71. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  72. static bool
  73. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  74. int target, int refclk, intel_clock_t *best_clock);
  75. static bool
  76. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  77. int target, int refclk, intel_clock_t *best_clock);
  78. static bool
  79. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  80. int target, int refclk, intel_clock_t *best_clock);
  81. static bool
  82. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  83. int target, int refclk, intel_clock_t *best_clock);
  84. static inline u32 /* units of 100MHz */
  85. intel_fdi_link_freq(struct drm_device *dev)
  86. {
  87. if (IS_GEN5(dev)) {
  88. struct drm_i915_private *dev_priv = dev->dev_private;
  89. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  90. } else
  91. return 27;
  92. }
  93. static const intel_limit_t intel_limits_i8xx_dvo = {
  94. .dot = { .min = 25000, .max = 350000 },
  95. .vco = { .min = 930000, .max = 1400000 },
  96. .n = { .min = 3, .max = 16 },
  97. .m = { .min = 96, .max = 140 },
  98. .m1 = { .min = 18, .max = 26 },
  99. .m2 = { .min = 6, .max = 16 },
  100. .p = { .min = 4, .max = 128 },
  101. .p1 = { .min = 2, .max = 33 },
  102. .p2 = { .dot_limit = 165000,
  103. .p2_slow = 4, .p2_fast = 2 },
  104. .find_pll = intel_find_best_PLL,
  105. };
  106. static const intel_limit_t intel_limits_i8xx_lvds = {
  107. .dot = { .min = 25000, .max = 350000 },
  108. .vco = { .min = 930000, .max = 1400000 },
  109. .n = { .min = 3, .max = 16 },
  110. .m = { .min = 96, .max = 140 },
  111. .m1 = { .min = 18, .max = 26 },
  112. .m2 = { .min = 6, .max = 16 },
  113. .p = { .min = 4, .max = 128 },
  114. .p1 = { .min = 1, .max = 6 },
  115. .p2 = { .dot_limit = 165000,
  116. .p2_slow = 14, .p2_fast = 7 },
  117. .find_pll = intel_find_best_PLL,
  118. };
  119. static const intel_limit_t intel_limits_i9xx_sdvo = {
  120. .dot = { .min = 20000, .max = 400000 },
  121. .vco = { .min = 1400000, .max = 2800000 },
  122. .n = { .min = 1, .max = 6 },
  123. .m = { .min = 70, .max = 120 },
  124. .m1 = { .min = 10, .max = 22 },
  125. .m2 = { .min = 5, .max = 9 },
  126. .p = { .min = 5, .max = 80 },
  127. .p1 = { .min = 1, .max = 8 },
  128. .p2 = { .dot_limit = 200000,
  129. .p2_slow = 10, .p2_fast = 5 },
  130. .find_pll = intel_find_best_PLL,
  131. };
  132. static const intel_limit_t intel_limits_i9xx_lvds = {
  133. .dot = { .min = 20000, .max = 400000 },
  134. .vco = { .min = 1400000, .max = 2800000 },
  135. .n = { .min = 1, .max = 6 },
  136. .m = { .min = 70, .max = 120 },
  137. .m1 = { .min = 10, .max = 22 },
  138. .m2 = { .min = 5, .max = 9 },
  139. .p = { .min = 7, .max = 98 },
  140. .p1 = { .min = 1, .max = 8 },
  141. .p2 = { .dot_limit = 112000,
  142. .p2_slow = 14, .p2_fast = 7 },
  143. .find_pll = intel_find_best_PLL,
  144. };
  145. static const intel_limit_t intel_limits_g4x_sdvo = {
  146. .dot = { .min = 25000, .max = 270000 },
  147. .vco = { .min = 1750000, .max = 3500000},
  148. .n = { .min = 1, .max = 4 },
  149. .m = { .min = 104, .max = 138 },
  150. .m1 = { .min = 17, .max = 23 },
  151. .m2 = { .min = 5, .max = 11 },
  152. .p = { .min = 10, .max = 30 },
  153. .p1 = { .min = 1, .max = 3},
  154. .p2 = { .dot_limit = 270000,
  155. .p2_slow = 10,
  156. .p2_fast = 10
  157. },
  158. .find_pll = intel_g4x_find_best_PLL,
  159. };
  160. static const intel_limit_t intel_limits_g4x_hdmi = {
  161. .dot = { .min = 22000, .max = 400000 },
  162. .vco = { .min = 1750000, .max = 3500000},
  163. .n = { .min = 1, .max = 4 },
  164. .m = { .min = 104, .max = 138 },
  165. .m1 = { .min = 16, .max = 23 },
  166. .m2 = { .min = 5, .max = 11 },
  167. .p = { .min = 5, .max = 80 },
  168. .p1 = { .min = 1, .max = 8},
  169. .p2 = { .dot_limit = 165000,
  170. .p2_slow = 10, .p2_fast = 5 },
  171. .find_pll = intel_g4x_find_best_PLL,
  172. };
  173. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  174. .dot = { .min = 20000, .max = 115000 },
  175. .vco = { .min = 1750000, .max = 3500000 },
  176. .n = { .min = 1, .max = 3 },
  177. .m = { .min = 104, .max = 138 },
  178. .m1 = { .min = 17, .max = 23 },
  179. .m2 = { .min = 5, .max = 11 },
  180. .p = { .min = 28, .max = 112 },
  181. .p1 = { .min = 2, .max = 8 },
  182. .p2 = { .dot_limit = 0,
  183. .p2_slow = 14, .p2_fast = 14
  184. },
  185. .find_pll = intel_g4x_find_best_PLL,
  186. };
  187. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  188. .dot = { .min = 80000, .max = 224000 },
  189. .vco = { .min = 1750000, .max = 3500000 },
  190. .n = { .min = 1, .max = 3 },
  191. .m = { .min = 104, .max = 138 },
  192. .m1 = { .min = 17, .max = 23 },
  193. .m2 = { .min = 5, .max = 11 },
  194. .p = { .min = 14, .max = 42 },
  195. .p1 = { .min = 2, .max = 6 },
  196. .p2 = { .dot_limit = 0,
  197. .p2_slow = 7, .p2_fast = 7
  198. },
  199. .find_pll = intel_g4x_find_best_PLL,
  200. };
  201. static const intel_limit_t intel_limits_g4x_display_port = {
  202. .dot = { .min = 161670, .max = 227000 },
  203. .vco = { .min = 1750000, .max = 3500000},
  204. .n = { .min = 1, .max = 2 },
  205. .m = { .min = 97, .max = 108 },
  206. .m1 = { .min = 0x10, .max = 0x12 },
  207. .m2 = { .min = 0x05, .max = 0x06 },
  208. .p = { .min = 10, .max = 20 },
  209. .p1 = { .min = 1, .max = 2},
  210. .p2 = { .dot_limit = 0,
  211. .p2_slow = 10, .p2_fast = 10 },
  212. .find_pll = intel_find_pll_g4x_dp,
  213. };
  214. static const intel_limit_t intel_limits_pineview_sdvo = {
  215. .dot = { .min = 20000, .max = 400000},
  216. .vco = { .min = 1700000, .max = 3500000 },
  217. /* Pineview's Ncounter is a ring counter */
  218. .n = { .min = 3, .max = 6 },
  219. .m = { .min = 2, .max = 256 },
  220. /* Pineview only has one combined m divider, which we treat as m2. */
  221. .m1 = { .min = 0, .max = 0 },
  222. .m2 = { .min = 0, .max = 254 },
  223. .p = { .min = 5, .max = 80 },
  224. .p1 = { .min = 1, .max = 8 },
  225. .p2 = { .dot_limit = 200000,
  226. .p2_slow = 10, .p2_fast = 5 },
  227. .find_pll = intel_find_best_PLL,
  228. };
  229. static const intel_limit_t intel_limits_pineview_lvds = {
  230. .dot = { .min = 20000, .max = 400000 },
  231. .vco = { .min = 1700000, .max = 3500000 },
  232. .n = { .min = 3, .max = 6 },
  233. .m = { .min = 2, .max = 256 },
  234. .m1 = { .min = 0, .max = 0 },
  235. .m2 = { .min = 0, .max = 254 },
  236. .p = { .min = 7, .max = 112 },
  237. .p1 = { .min = 1, .max = 8 },
  238. .p2 = { .dot_limit = 112000,
  239. .p2_slow = 14, .p2_fast = 14 },
  240. .find_pll = intel_find_best_PLL,
  241. };
  242. /* Ironlake / Sandybridge
  243. *
  244. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  245. * the range value for them is (actual_value - 2).
  246. */
  247. static const intel_limit_t intel_limits_ironlake_dac = {
  248. .dot = { .min = 25000, .max = 350000 },
  249. .vco = { .min = 1760000, .max = 3510000 },
  250. .n = { .min = 1, .max = 5 },
  251. .m = { .min = 79, .max = 127 },
  252. .m1 = { .min = 12, .max = 22 },
  253. .m2 = { .min = 5, .max = 9 },
  254. .p = { .min = 5, .max = 80 },
  255. .p1 = { .min = 1, .max = 8 },
  256. .p2 = { .dot_limit = 225000,
  257. .p2_slow = 10, .p2_fast = 5 },
  258. .find_pll = intel_g4x_find_best_PLL,
  259. };
  260. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  261. .dot = { .min = 25000, .max = 350000 },
  262. .vco = { .min = 1760000, .max = 3510000 },
  263. .n = { .min = 1, .max = 3 },
  264. .m = { .min = 79, .max = 118 },
  265. .m1 = { .min = 12, .max = 22 },
  266. .m2 = { .min = 5, .max = 9 },
  267. .p = { .min = 28, .max = 112 },
  268. .p1 = { .min = 2, .max = 8 },
  269. .p2 = { .dot_limit = 225000,
  270. .p2_slow = 14, .p2_fast = 14 },
  271. .find_pll = intel_g4x_find_best_PLL,
  272. };
  273. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  274. .dot = { .min = 25000, .max = 350000 },
  275. .vco = { .min = 1760000, .max = 3510000 },
  276. .n = { .min = 1, .max = 3 },
  277. .m = { .min = 79, .max = 127 },
  278. .m1 = { .min = 12, .max = 22 },
  279. .m2 = { .min = 5, .max = 9 },
  280. .p = { .min = 14, .max = 56 },
  281. .p1 = { .min = 2, .max = 8 },
  282. .p2 = { .dot_limit = 225000,
  283. .p2_slow = 7, .p2_fast = 7 },
  284. .find_pll = intel_g4x_find_best_PLL,
  285. };
  286. /* LVDS 100mhz refclk limits. */
  287. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  288. .dot = { .min = 25000, .max = 350000 },
  289. .vco = { .min = 1760000, .max = 3510000 },
  290. .n = { .min = 1, .max = 2 },
  291. .m = { .min = 79, .max = 126 },
  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. .find_pll = intel_g4x_find_best_PLL,
  299. };
  300. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  301. .dot = { .min = 25000, .max = 350000 },
  302. .vco = { .min = 1760000, .max = 3510000 },
  303. .n = { .min = 1, .max = 3 },
  304. .m = { .min = 79, .max = 126 },
  305. .m1 = { .min = 12, .max = 22 },
  306. .m2 = { .min = 5, .max = 9 },
  307. .p = { .min = 14, .max = 42 },
  308. .p1 = { .min = 2,.max = 6 },
  309. .p2 = { .dot_limit = 225000,
  310. .p2_slow = 7, .p2_fast = 7 },
  311. .find_pll = intel_g4x_find_best_PLL,
  312. };
  313. static const intel_limit_t intel_limits_ironlake_display_port = {
  314. .dot = { .min = 25000, .max = 350000 },
  315. .vco = { .min = 1760000, .max = 3510000},
  316. .n = { .min = 1, .max = 2 },
  317. .m = { .min = 81, .max = 90 },
  318. .m1 = { .min = 12, .max = 22 },
  319. .m2 = { .min = 5, .max = 9 },
  320. .p = { .min = 10, .max = 20 },
  321. .p1 = { .min = 1, .max = 2},
  322. .p2 = { .dot_limit = 0,
  323. .p2_slow = 10, .p2_fast = 10 },
  324. .find_pll = intel_find_pll_ironlake_dp,
  325. };
  326. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  327. int refclk)
  328. {
  329. struct drm_device *dev = crtc->dev;
  330. struct drm_i915_private *dev_priv = dev->dev_private;
  331. const intel_limit_t *limit;
  332. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  333. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  334. LVDS_CLKB_POWER_UP) {
  335. /* LVDS dual channel */
  336. if (refclk == 100000)
  337. limit = &intel_limits_ironlake_dual_lvds_100m;
  338. else
  339. limit = &intel_limits_ironlake_dual_lvds;
  340. } else {
  341. if (refclk == 100000)
  342. limit = &intel_limits_ironlake_single_lvds_100m;
  343. else
  344. limit = &intel_limits_ironlake_single_lvds;
  345. }
  346. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  347. HAS_eDP)
  348. limit = &intel_limits_ironlake_display_port;
  349. else
  350. limit = &intel_limits_ironlake_dac;
  351. return limit;
  352. }
  353. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  354. {
  355. struct drm_device *dev = crtc->dev;
  356. struct drm_i915_private *dev_priv = dev->dev_private;
  357. const intel_limit_t *limit;
  358. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  359. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  360. LVDS_CLKB_POWER_UP)
  361. /* LVDS with dual channel */
  362. limit = &intel_limits_g4x_dual_channel_lvds;
  363. else
  364. /* LVDS with dual channel */
  365. limit = &intel_limits_g4x_single_channel_lvds;
  366. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  367. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  368. limit = &intel_limits_g4x_hdmi;
  369. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  370. limit = &intel_limits_g4x_sdvo;
  371. } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  372. limit = &intel_limits_g4x_display_port;
  373. } else /* The option is for other outputs */
  374. limit = &intel_limits_i9xx_sdvo;
  375. return limit;
  376. }
  377. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  378. {
  379. struct drm_device *dev = crtc->dev;
  380. const intel_limit_t *limit;
  381. if (HAS_PCH_SPLIT(dev))
  382. limit = intel_ironlake_limit(crtc, refclk);
  383. else if (IS_G4X(dev)) {
  384. limit = intel_g4x_limit(crtc);
  385. } else if (IS_PINEVIEW(dev)) {
  386. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  387. limit = &intel_limits_pineview_lvds;
  388. else
  389. limit = &intel_limits_pineview_sdvo;
  390. } else if (!IS_GEN2(dev)) {
  391. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  392. limit = &intel_limits_i9xx_lvds;
  393. else
  394. limit = &intel_limits_i9xx_sdvo;
  395. } else {
  396. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  397. limit = &intel_limits_i8xx_lvds;
  398. else
  399. limit = &intel_limits_i8xx_dvo;
  400. }
  401. return limit;
  402. }
  403. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  404. static void pineview_clock(int refclk, intel_clock_t *clock)
  405. {
  406. clock->m = clock->m2 + 2;
  407. clock->p = clock->p1 * clock->p2;
  408. clock->vco = refclk * clock->m / clock->n;
  409. clock->dot = clock->vco / clock->p;
  410. }
  411. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  412. {
  413. if (IS_PINEVIEW(dev)) {
  414. pineview_clock(refclk, clock);
  415. return;
  416. }
  417. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  418. clock->p = clock->p1 * clock->p2;
  419. clock->vco = refclk * clock->m / (clock->n + 2);
  420. clock->dot = clock->vco / clock->p;
  421. }
  422. /**
  423. * Returns whether any output on the specified pipe is of the specified type
  424. */
  425. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  426. {
  427. struct drm_device *dev = crtc->dev;
  428. struct drm_mode_config *mode_config = &dev->mode_config;
  429. struct intel_encoder *encoder;
  430. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  431. if (encoder->base.crtc == crtc && encoder->type == type)
  432. return true;
  433. return false;
  434. }
  435. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  436. /**
  437. * Returns whether the given set of divisors are valid for a given refclk with
  438. * the given connectors.
  439. */
  440. static bool intel_PLL_is_valid(struct drm_device *dev,
  441. const intel_limit_t *limit,
  442. const intel_clock_t *clock)
  443. {
  444. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  445. INTELPllInvalid ("p1 out of range\n");
  446. if (clock->p < limit->p.min || limit->p.max < clock->p)
  447. INTELPllInvalid ("p out of range\n");
  448. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  449. INTELPllInvalid ("m2 out of range\n");
  450. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  451. INTELPllInvalid ("m1 out of range\n");
  452. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  453. INTELPllInvalid ("m1 <= m2\n");
  454. if (clock->m < limit->m.min || limit->m.max < clock->m)
  455. INTELPllInvalid ("m out of range\n");
  456. if (clock->n < limit->n.min || limit->n.max < clock->n)
  457. INTELPllInvalid ("n out of range\n");
  458. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  459. INTELPllInvalid ("vco out of range\n");
  460. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  461. * connector, etc., rather than just a single range.
  462. */
  463. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  464. INTELPllInvalid ("dot out of range\n");
  465. return true;
  466. }
  467. static bool
  468. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  469. int target, int refclk, intel_clock_t *best_clock)
  470. {
  471. struct drm_device *dev = crtc->dev;
  472. struct drm_i915_private *dev_priv = dev->dev_private;
  473. intel_clock_t clock;
  474. int err = target;
  475. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  476. (I915_READ(LVDS)) != 0) {
  477. /*
  478. * For LVDS, if the panel is on, just rely on its current
  479. * settings for dual-channel. We haven't figured out how to
  480. * reliably set up different single/dual channel state, if we
  481. * even can.
  482. */
  483. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  484. LVDS_CLKB_POWER_UP)
  485. clock.p2 = limit->p2.p2_fast;
  486. else
  487. clock.p2 = limit->p2.p2_slow;
  488. } else {
  489. if (target < limit->p2.dot_limit)
  490. clock.p2 = limit->p2.p2_slow;
  491. else
  492. clock.p2 = limit->p2.p2_fast;
  493. }
  494. memset (best_clock, 0, sizeof (*best_clock));
  495. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  496. clock.m1++) {
  497. for (clock.m2 = limit->m2.min;
  498. clock.m2 <= limit->m2.max; clock.m2++) {
  499. /* m1 is always 0 in Pineview */
  500. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  501. break;
  502. for (clock.n = limit->n.min;
  503. clock.n <= limit->n.max; clock.n++) {
  504. for (clock.p1 = limit->p1.min;
  505. clock.p1 <= limit->p1.max; clock.p1++) {
  506. int this_err;
  507. intel_clock(dev, refclk, &clock);
  508. if (!intel_PLL_is_valid(dev, limit,
  509. &clock))
  510. continue;
  511. this_err = abs(clock.dot - target);
  512. if (this_err < err) {
  513. *best_clock = clock;
  514. err = this_err;
  515. }
  516. }
  517. }
  518. }
  519. }
  520. return (err != target);
  521. }
  522. static bool
  523. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  524. int target, int refclk, intel_clock_t *best_clock)
  525. {
  526. struct drm_device *dev = crtc->dev;
  527. struct drm_i915_private *dev_priv = dev->dev_private;
  528. intel_clock_t clock;
  529. int max_n;
  530. bool found;
  531. /* approximately equals target * 0.00585 */
  532. int err_most = (target >> 8) + (target >> 9);
  533. found = false;
  534. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  535. int lvds_reg;
  536. if (HAS_PCH_SPLIT(dev))
  537. lvds_reg = PCH_LVDS;
  538. else
  539. lvds_reg = LVDS;
  540. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  541. LVDS_CLKB_POWER_UP)
  542. clock.p2 = limit->p2.p2_fast;
  543. else
  544. clock.p2 = limit->p2.p2_slow;
  545. } else {
  546. if (target < limit->p2.dot_limit)
  547. clock.p2 = limit->p2.p2_slow;
  548. else
  549. clock.p2 = limit->p2.p2_fast;
  550. }
  551. memset(best_clock, 0, sizeof(*best_clock));
  552. max_n = limit->n.max;
  553. /* based on hardware requirement, prefer smaller n to precision */
  554. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  555. /* based on hardware requirement, prefere larger m1,m2 */
  556. for (clock.m1 = limit->m1.max;
  557. clock.m1 >= limit->m1.min; clock.m1--) {
  558. for (clock.m2 = limit->m2.max;
  559. clock.m2 >= limit->m2.min; clock.m2--) {
  560. for (clock.p1 = limit->p1.max;
  561. clock.p1 >= limit->p1.min; clock.p1--) {
  562. int this_err;
  563. intel_clock(dev, refclk, &clock);
  564. if (!intel_PLL_is_valid(dev, limit,
  565. &clock))
  566. continue;
  567. this_err = abs(clock.dot - target);
  568. if (this_err < err_most) {
  569. *best_clock = clock;
  570. err_most = this_err;
  571. max_n = clock.n;
  572. found = true;
  573. }
  574. }
  575. }
  576. }
  577. }
  578. return found;
  579. }
  580. static bool
  581. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  582. int target, int refclk, intel_clock_t *best_clock)
  583. {
  584. struct drm_device *dev = crtc->dev;
  585. intel_clock_t clock;
  586. if (target < 200000) {
  587. clock.n = 1;
  588. clock.p1 = 2;
  589. clock.p2 = 10;
  590. clock.m1 = 12;
  591. clock.m2 = 9;
  592. } else {
  593. clock.n = 2;
  594. clock.p1 = 1;
  595. clock.p2 = 10;
  596. clock.m1 = 14;
  597. clock.m2 = 8;
  598. }
  599. intel_clock(dev, refclk, &clock);
  600. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  601. return true;
  602. }
  603. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  604. static bool
  605. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  606. int target, int refclk, intel_clock_t *best_clock)
  607. {
  608. intel_clock_t clock;
  609. if (target < 200000) {
  610. clock.p1 = 2;
  611. clock.p2 = 10;
  612. clock.n = 2;
  613. clock.m1 = 23;
  614. clock.m2 = 8;
  615. } else {
  616. clock.p1 = 1;
  617. clock.p2 = 10;
  618. clock.n = 1;
  619. clock.m1 = 14;
  620. clock.m2 = 2;
  621. }
  622. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  623. clock.p = (clock.p1 * clock.p2);
  624. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  625. clock.vco = 0;
  626. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  627. return true;
  628. }
  629. /**
  630. * intel_wait_for_vblank - wait for vblank on a given pipe
  631. * @dev: drm device
  632. * @pipe: pipe to wait for
  633. *
  634. * Wait for vblank to occur on a given pipe. Needed for various bits of
  635. * mode setting code.
  636. */
  637. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  638. {
  639. struct drm_i915_private *dev_priv = dev->dev_private;
  640. int pipestat_reg = PIPESTAT(pipe);
  641. /* Clear existing vblank status. Note this will clear any other
  642. * sticky status fields as well.
  643. *
  644. * This races with i915_driver_irq_handler() with the result
  645. * that either function could miss a vblank event. Here it is not
  646. * fatal, as we will either wait upon the next vblank interrupt or
  647. * timeout. Generally speaking intel_wait_for_vblank() is only
  648. * called during modeset at which time the GPU should be idle and
  649. * should *not* be performing page flips and thus not waiting on
  650. * vblanks...
  651. * Currently, the result of us stealing a vblank from the irq
  652. * handler is that a single frame will be skipped during swapbuffers.
  653. */
  654. I915_WRITE(pipestat_reg,
  655. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  656. /* Wait for vblank interrupt bit to set */
  657. if (wait_for(I915_READ(pipestat_reg) &
  658. PIPE_VBLANK_INTERRUPT_STATUS,
  659. 50))
  660. DRM_DEBUG_KMS("vblank wait timed out\n");
  661. }
  662. /*
  663. * intel_wait_for_pipe_off - wait for pipe to turn off
  664. * @dev: drm device
  665. * @pipe: pipe to wait for
  666. *
  667. * After disabling a pipe, we can't wait for vblank in the usual way,
  668. * spinning on the vblank interrupt status bit, since we won't actually
  669. * see an interrupt when the pipe is disabled.
  670. *
  671. * On Gen4 and above:
  672. * wait for the pipe register state bit to turn off
  673. *
  674. * Otherwise:
  675. * wait for the display line value to settle (it usually
  676. * ends up stopping at the start of the next frame).
  677. *
  678. */
  679. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  680. {
  681. struct drm_i915_private *dev_priv = dev->dev_private;
  682. if (INTEL_INFO(dev)->gen >= 4) {
  683. int reg = PIPECONF(pipe);
  684. /* Wait for the Pipe State to go off */
  685. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  686. 100))
  687. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  688. } else {
  689. u32 last_line;
  690. int reg = PIPEDSL(pipe);
  691. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  692. /* Wait for the display line to settle */
  693. do {
  694. last_line = I915_READ(reg) & DSL_LINEMASK;
  695. mdelay(5);
  696. } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
  697. time_after(timeout, jiffies));
  698. if (time_after(jiffies, timeout))
  699. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  700. }
  701. }
  702. static const char *state_string(bool enabled)
  703. {
  704. return enabled ? "on" : "off";
  705. }
  706. /* Only for pre-ILK configs */
  707. static void assert_pll(struct drm_i915_private *dev_priv,
  708. enum pipe pipe, bool state)
  709. {
  710. int reg;
  711. u32 val;
  712. bool cur_state;
  713. reg = DPLL(pipe);
  714. val = I915_READ(reg);
  715. cur_state = !!(val & DPLL_VCO_ENABLE);
  716. WARN(cur_state != state,
  717. "PLL state assertion failure (expected %s, current %s)\n",
  718. state_string(state), state_string(cur_state));
  719. }
  720. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  721. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  722. /* For ILK+ */
  723. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  724. enum pipe pipe, bool state)
  725. {
  726. int reg;
  727. u32 val;
  728. bool cur_state;
  729. reg = PCH_DPLL(pipe);
  730. val = I915_READ(reg);
  731. cur_state = !!(val & DPLL_VCO_ENABLE);
  732. WARN(cur_state != state,
  733. "PCH PLL state assertion failure (expected %s, current %s)\n",
  734. state_string(state), state_string(cur_state));
  735. }
  736. #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
  737. #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
  738. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  739. enum pipe pipe, bool state)
  740. {
  741. int reg;
  742. u32 val;
  743. bool cur_state;
  744. reg = FDI_TX_CTL(pipe);
  745. val = I915_READ(reg);
  746. cur_state = !!(val & FDI_TX_ENABLE);
  747. WARN(cur_state != state,
  748. "FDI TX state assertion failure (expected %s, current %s)\n",
  749. state_string(state), state_string(cur_state));
  750. }
  751. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  752. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  753. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  754. enum pipe pipe, bool state)
  755. {
  756. int reg;
  757. u32 val;
  758. bool cur_state;
  759. reg = FDI_RX_CTL(pipe);
  760. val = I915_READ(reg);
  761. cur_state = !!(val & FDI_RX_ENABLE);
  762. WARN(cur_state != state,
  763. "FDI RX state assertion failure (expected %s, current %s)\n",
  764. state_string(state), state_string(cur_state));
  765. }
  766. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  767. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  768. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  769. enum pipe pipe)
  770. {
  771. int reg;
  772. u32 val;
  773. /* ILK FDI PLL is always enabled */
  774. if (dev_priv->info->gen == 5)
  775. return;
  776. reg = FDI_TX_CTL(pipe);
  777. val = I915_READ(reg);
  778. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  779. }
  780. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  781. enum pipe pipe)
  782. {
  783. int reg;
  784. u32 val;
  785. reg = FDI_RX_CTL(pipe);
  786. val = I915_READ(reg);
  787. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  788. }
  789. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  790. enum pipe pipe)
  791. {
  792. int pp_reg, lvds_reg;
  793. u32 val;
  794. enum pipe panel_pipe = PIPE_A;
  795. bool locked = locked;
  796. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  797. pp_reg = PCH_PP_CONTROL;
  798. lvds_reg = PCH_LVDS;
  799. } else {
  800. pp_reg = PP_CONTROL;
  801. lvds_reg = LVDS;
  802. }
  803. val = I915_READ(pp_reg);
  804. if (!(val & PANEL_POWER_ON) ||
  805. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  806. locked = false;
  807. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  808. panel_pipe = PIPE_B;
  809. WARN(panel_pipe == pipe && locked,
  810. "panel assertion failure, pipe %c regs locked\n",
  811. pipe_name(pipe));
  812. }
  813. static void assert_pipe(struct drm_i915_private *dev_priv,
  814. enum pipe pipe, bool state)
  815. {
  816. int reg;
  817. u32 val;
  818. bool cur_state;
  819. reg = PIPECONF(pipe);
  820. val = I915_READ(reg);
  821. cur_state = !!(val & PIPECONF_ENABLE);
  822. WARN(cur_state != state,
  823. "pipe %c assertion failure (expected %s, current %s)\n",
  824. pipe_name(pipe), state_string(state), state_string(cur_state));
  825. }
  826. #define assert_pipe_enabled(d, p) assert_pipe(d, p, true)
  827. #define assert_pipe_disabled(d, p) assert_pipe(d, p, false)
  828. static void assert_plane_enabled(struct drm_i915_private *dev_priv,
  829. enum plane plane)
  830. {
  831. int reg;
  832. u32 val;
  833. reg = DSPCNTR(plane);
  834. val = I915_READ(reg);
  835. WARN(!(val & DISPLAY_PLANE_ENABLE),
  836. "plane %c assertion failure, should be active but is disabled\n",
  837. plane_name(plane));
  838. }
  839. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  840. enum pipe pipe)
  841. {
  842. int reg, i;
  843. u32 val;
  844. int cur_pipe;
  845. /* Planes are fixed to pipes on ILK+ */
  846. if (HAS_PCH_SPLIT(dev_priv->dev))
  847. return;
  848. /* Need to check both planes against the pipe */
  849. for (i = 0; i < 2; i++) {
  850. reg = DSPCNTR(i);
  851. val = I915_READ(reg);
  852. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  853. DISPPLANE_SEL_PIPE_SHIFT;
  854. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  855. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  856. plane_name(i), pipe_name(pipe));
  857. }
  858. }
  859. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  860. {
  861. u32 val;
  862. bool enabled;
  863. val = I915_READ(PCH_DREF_CONTROL);
  864. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  865. DREF_SUPERSPREAD_SOURCE_MASK));
  866. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  867. }
  868. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  869. enum pipe pipe)
  870. {
  871. int reg;
  872. u32 val;
  873. bool enabled;
  874. reg = TRANSCONF(pipe);
  875. val = I915_READ(reg);
  876. enabled = !!(val & TRANS_ENABLE);
  877. WARN(enabled,
  878. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  879. pipe_name(pipe));
  880. }
  881. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  882. enum pipe pipe, int reg)
  883. {
  884. u32 val = I915_READ(reg);
  885. WARN(DP_PIPE_ENABLED(val, pipe),
  886. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  887. reg, pipe_name(pipe));
  888. }
  889. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  890. enum pipe pipe, int reg)
  891. {
  892. u32 val = I915_READ(reg);
  893. WARN(HDMI_PIPE_ENABLED(val, pipe),
  894. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  895. reg, pipe_name(pipe));
  896. }
  897. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  898. enum pipe pipe)
  899. {
  900. int reg;
  901. u32 val;
  902. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B);
  903. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C);
  904. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D);
  905. reg = PCH_ADPA;
  906. val = I915_READ(reg);
  907. WARN(ADPA_PIPE_ENABLED(val, pipe),
  908. "PCH VGA enabled on transcoder %c, should be disabled\n",
  909. pipe_name(pipe));
  910. reg = PCH_LVDS;
  911. val = I915_READ(reg);
  912. WARN(LVDS_PIPE_ENABLED(val, pipe),
  913. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  914. pipe_name(pipe));
  915. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
  916. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
  917. assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
  918. }
  919. /**
  920. * intel_enable_pll - enable a PLL
  921. * @dev_priv: i915 private structure
  922. * @pipe: pipe PLL to enable
  923. *
  924. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  925. * make sure the PLL reg is writable first though, since the panel write
  926. * protect mechanism may be enabled.
  927. *
  928. * Note! This is for pre-ILK only.
  929. */
  930. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  931. {
  932. int reg;
  933. u32 val;
  934. /* No really, not for ILK+ */
  935. BUG_ON(dev_priv->info->gen >= 5);
  936. /* PLL is protected by panel, make sure we can write it */
  937. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  938. assert_panel_unlocked(dev_priv, pipe);
  939. reg = DPLL(pipe);
  940. val = I915_READ(reg);
  941. val |= DPLL_VCO_ENABLE;
  942. /* We do this three times for luck */
  943. I915_WRITE(reg, val);
  944. POSTING_READ(reg);
  945. udelay(150); /* wait for warmup */
  946. I915_WRITE(reg, val);
  947. POSTING_READ(reg);
  948. udelay(150); /* wait for warmup */
  949. I915_WRITE(reg, val);
  950. POSTING_READ(reg);
  951. udelay(150); /* wait for warmup */
  952. }
  953. /**
  954. * intel_disable_pll - disable a PLL
  955. * @dev_priv: i915 private structure
  956. * @pipe: pipe PLL to disable
  957. *
  958. * Disable the PLL for @pipe, making sure the pipe is off first.
  959. *
  960. * Note! This is for pre-ILK only.
  961. */
  962. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  963. {
  964. int reg;
  965. u32 val;
  966. /* Don't disable pipe A or pipe A PLLs if needed */
  967. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  968. return;
  969. /* Make sure the pipe isn't still relying on us */
  970. assert_pipe_disabled(dev_priv, pipe);
  971. reg = DPLL(pipe);
  972. val = I915_READ(reg);
  973. val &= ~DPLL_VCO_ENABLE;
  974. I915_WRITE(reg, val);
  975. POSTING_READ(reg);
  976. }
  977. /**
  978. * intel_enable_pch_pll - enable PCH PLL
  979. * @dev_priv: i915 private structure
  980. * @pipe: pipe PLL to enable
  981. *
  982. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  983. * drives the transcoder clock.
  984. */
  985. static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
  986. enum pipe pipe)
  987. {
  988. int reg;
  989. u32 val;
  990. /* PCH only available on ILK+ */
  991. BUG_ON(dev_priv->info->gen < 5);
  992. /* PCH refclock must be enabled first */
  993. assert_pch_refclk_enabled(dev_priv);
  994. reg = PCH_DPLL(pipe);
  995. val = I915_READ(reg);
  996. val |= DPLL_VCO_ENABLE;
  997. I915_WRITE(reg, val);
  998. POSTING_READ(reg);
  999. udelay(200);
  1000. }
  1001. static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
  1002. enum pipe pipe)
  1003. {
  1004. int reg;
  1005. u32 val;
  1006. /* PCH only available on ILK+ */
  1007. BUG_ON(dev_priv->info->gen < 5);
  1008. /* Make sure transcoder isn't still depending on us */
  1009. assert_transcoder_disabled(dev_priv, pipe);
  1010. reg = PCH_DPLL(pipe);
  1011. val = I915_READ(reg);
  1012. val &= ~DPLL_VCO_ENABLE;
  1013. I915_WRITE(reg, val);
  1014. POSTING_READ(reg);
  1015. udelay(200);
  1016. }
  1017. static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
  1018. enum pipe pipe)
  1019. {
  1020. int reg;
  1021. u32 val;
  1022. /* PCH only available on ILK+ */
  1023. BUG_ON(dev_priv->info->gen < 5);
  1024. /* Make sure PCH DPLL is enabled */
  1025. assert_pch_pll_enabled(dev_priv, pipe);
  1026. /* FDI must be feeding us bits for PCH ports */
  1027. assert_fdi_tx_enabled(dev_priv, pipe);
  1028. assert_fdi_rx_enabled(dev_priv, pipe);
  1029. reg = TRANSCONF(pipe);
  1030. val = I915_READ(reg);
  1031. /*
  1032. * make the BPC in transcoder be consistent with
  1033. * that in pipeconf reg.
  1034. */
  1035. val &= ~PIPE_BPC_MASK;
  1036. val |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
  1037. I915_WRITE(reg, val | TRANS_ENABLE);
  1038. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1039. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1040. }
  1041. static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
  1042. enum pipe pipe)
  1043. {
  1044. int reg;
  1045. u32 val;
  1046. /* FDI relies on the transcoder */
  1047. assert_fdi_tx_disabled(dev_priv, pipe);
  1048. assert_fdi_rx_disabled(dev_priv, pipe);
  1049. /* Ports must be off as well */
  1050. assert_pch_ports_disabled(dev_priv, pipe);
  1051. reg = TRANSCONF(pipe);
  1052. val = I915_READ(reg);
  1053. val &= ~TRANS_ENABLE;
  1054. I915_WRITE(reg, val);
  1055. /* wait for PCH transcoder off, transcoder state */
  1056. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1057. DRM_ERROR("failed to disable transcoder\n");
  1058. }
  1059. /**
  1060. * intel_enable_pipe - enable a pipe, asserting requirements
  1061. * @dev_priv: i915 private structure
  1062. * @pipe: pipe to enable
  1063. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1064. *
  1065. * Enable @pipe, making sure that various hardware specific requirements
  1066. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1067. *
  1068. * @pipe should be %PIPE_A or %PIPE_B.
  1069. *
  1070. * Will wait until the pipe is actually running (i.e. first vblank) before
  1071. * returning.
  1072. */
  1073. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1074. bool pch_port)
  1075. {
  1076. int reg;
  1077. u32 val;
  1078. /*
  1079. * A pipe without a PLL won't actually be able to drive bits from
  1080. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1081. * need the check.
  1082. */
  1083. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1084. assert_pll_enabled(dev_priv, pipe);
  1085. else {
  1086. if (pch_port) {
  1087. /* if driving the PCH, we need FDI enabled */
  1088. assert_fdi_rx_pll_enabled(dev_priv, pipe);
  1089. assert_fdi_tx_pll_enabled(dev_priv, pipe);
  1090. }
  1091. /* FIXME: assert CPU port conditions for SNB+ */
  1092. }
  1093. reg = PIPECONF(pipe);
  1094. val = I915_READ(reg);
  1095. if (val & PIPECONF_ENABLE)
  1096. return;
  1097. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1098. intel_wait_for_vblank(dev_priv->dev, pipe);
  1099. }
  1100. /**
  1101. * intel_disable_pipe - disable a pipe, asserting requirements
  1102. * @dev_priv: i915 private structure
  1103. * @pipe: pipe to disable
  1104. *
  1105. * Disable @pipe, making sure that various hardware specific requirements
  1106. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1107. *
  1108. * @pipe should be %PIPE_A or %PIPE_B.
  1109. *
  1110. * Will wait until the pipe has shut down before returning.
  1111. */
  1112. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1113. enum pipe pipe)
  1114. {
  1115. int reg;
  1116. u32 val;
  1117. /*
  1118. * Make sure planes won't keep trying to pump pixels to us,
  1119. * or we might hang the display.
  1120. */
  1121. assert_planes_disabled(dev_priv, pipe);
  1122. /* Don't disable pipe A or pipe A PLLs if needed */
  1123. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1124. return;
  1125. reg = PIPECONF(pipe);
  1126. val = I915_READ(reg);
  1127. if ((val & PIPECONF_ENABLE) == 0)
  1128. return;
  1129. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1130. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1131. }
  1132. /**
  1133. * intel_enable_plane - enable a display plane on a given pipe
  1134. * @dev_priv: i915 private structure
  1135. * @plane: plane to enable
  1136. * @pipe: pipe being fed
  1137. *
  1138. * Enable @plane on @pipe, making sure that @pipe is running first.
  1139. */
  1140. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1141. enum plane plane, enum pipe pipe)
  1142. {
  1143. int reg;
  1144. u32 val;
  1145. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1146. assert_pipe_enabled(dev_priv, pipe);
  1147. reg = DSPCNTR(plane);
  1148. val = I915_READ(reg);
  1149. if (val & DISPLAY_PLANE_ENABLE)
  1150. return;
  1151. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1152. intel_wait_for_vblank(dev_priv->dev, pipe);
  1153. }
  1154. /*
  1155. * Plane regs are double buffered, going from enabled->disabled needs a
  1156. * trigger in order to latch. The display address reg provides this.
  1157. */
  1158. static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1159. enum plane plane)
  1160. {
  1161. u32 reg = DSPADDR(plane);
  1162. I915_WRITE(reg, I915_READ(reg));
  1163. }
  1164. /**
  1165. * intel_disable_plane - disable a display plane
  1166. * @dev_priv: i915 private structure
  1167. * @plane: plane to disable
  1168. * @pipe: pipe consuming the data
  1169. *
  1170. * Disable @plane; should be an independent operation.
  1171. */
  1172. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1173. enum plane plane, enum pipe pipe)
  1174. {
  1175. int reg;
  1176. u32 val;
  1177. reg = DSPCNTR(plane);
  1178. val = I915_READ(reg);
  1179. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1180. return;
  1181. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1182. intel_flush_display_plane(dev_priv, plane);
  1183. intel_wait_for_vblank(dev_priv->dev, pipe);
  1184. }
  1185. static void disable_pch_dp(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe, int reg)
  1187. {
  1188. u32 val = I915_READ(reg);
  1189. if (DP_PIPE_ENABLED(val, pipe))
  1190. I915_WRITE(reg, val & ~DP_PORT_EN);
  1191. }
  1192. static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
  1193. enum pipe pipe, int reg)
  1194. {
  1195. u32 val = I915_READ(reg);
  1196. if (HDMI_PIPE_ENABLED(val, pipe))
  1197. I915_WRITE(reg, val & ~PORT_ENABLE);
  1198. }
  1199. /* Disable any ports connected to this transcoder */
  1200. static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
  1201. enum pipe pipe)
  1202. {
  1203. u32 reg, val;
  1204. val = I915_READ(PCH_PP_CONTROL);
  1205. I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
  1206. disable_pch_dp(dev_priv, pipe, PCH_DP_B);
  1207. disable_pch_dp(dev_priv, pipe, PCH_DP_C);
  1208. disable_pch_dp(dev_priv, pipe, PCH_DP_D);
  1209. reg = PCH_ADPA;
  1210. val = I915_READ(reg);
  1211. if (ADPA_PIPE_ENABLED(val, pipe))
  1212. I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
  1213. reg = PCH_LVDS;
  1214. val = I915_READ(reg);
  1215. if (LVDS_PIPE_ENABLED(val, pipe)) {
  1216. I915_WRITE(reg, val & ~LVDS_PORT_EN);
  1217. POSTING_READ(reg);
  1218. udelay(100);
  1219. }
  1220. disable_pch_hdmi(dev_priv, pipe, HDMIB);
  1221. disable_pch_hdmi(dev_priv, pipe, HDMIC);
  1222. disable_pch_hdmi(dev_priv, pipe, HDMID);
  1223. }
  1224. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1225. {
  1226. struct drm_device *dev = crtc->dev;
  1227. struct drm_i915_private *dev_priv = dev->dev_private;
  1228. struct drm_framebuffer *fb = crtc->fb;
  1229. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1230. struct drm_i915_gem_object *obj = intel_fb->obj;
  1231. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1232. int plane, i;
  1233. u32 fbc_ctl, fbc_ctl2;
  1234. if (fb->pitch == dev_priv->cfb_pitch &&
  1235. obj->fence_reg == dev_priv->cfb_fence &&
  1236. intel_crtc->plane == dev_priv->cfb_plane &&
  1237. I915_READ(FBC_CONTROL) & FBC_CTL_EN)
  1238. return;
  1239. i8xx_disable_fbc(dev);
  1240. dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  1241. if (fb->pitch < dev_priv->cfb_pitch)
  1242. dev_priv->cfb_pitch = fb->pitch;
  1243. /* FBC_CTL wants 64B units */
  1244. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1245. dev_priv->cfb_fence = obj->fence_reg;
  1246. dev_priv->cfb_plane = intel_crtc->plane;
  1247. plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  1248. /* Clear old tags */
  1249. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  1250. I915_WRITE(FBC_TAG + (i * 4), 0);
  1251. /* Set it up... */
  1252. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
  1253. if (obj->tiling_mode != I915_TILING_NONE)
  1254. fbc_ctl2 |= FBC_CTL_CPU_FENCE;
  1255. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  1256. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  1257. /* enable it... */
  1258. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  1259. if (IS_I945GM(dev))
  1260. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  1261. fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  1262. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  1263. if (obj->tiling_mode != I915_TILING_NONE)
  1264. fbc_ctl |= dev_priv->cfb_fence;
  1265. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1266. DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
  1267. dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
  1268. }
  1269. void i8xx_disable_fbc(struct drm_device *dev)
  1270. {
  1271. struct drm_i915_private *dev_priv = dev->dev_private;
  1272. u32 fbc_ctl;
  1273. /* Disable compression */
  1274. fbc_ctl = I915_READ(FBC_CONTROL);
  1275. if ((fbc_ctl & FBC_CTL_EN) == 0)
  1276. return;
  1277. fbc_ctl &= ~FBC_CTL_EN;
  1278. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1279. /* Wait for compressing bit to clear */
  1280. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  1281. DRM_DEBUG_KMS("FBC idle timed out\n");
  1282. return;
  1283. }
  1284. DRM_DEBUG_KMS("disabled FBC\n");
  1285. }
  1286. static bool i8xx_fbc_enabled(struct drm_device *dev)
  1287. {
  1288. struct drm_i915_private *dev_priv = dev->dev_private;
  1289. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  1290. }
  1291. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1292. {
  1293. struct drm_device *dev = crtc->dev;
  1294. struct drm_i915_private *dev_priv = dev->dev_private;
  1295. struct drm_framebuffer *fb = crtc->fb;
  1296. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1297. struct drm_i915_gem_object *obj = intel_fb->obj;
  1298. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1299. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1300. unsigned long stall_watermark = 200;
  1301. u32 dpfc_ctl;
  1302. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1303. if (dpfc_ctl & DPFC_CTL_EN) {
  1304. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1305. dev_priv->cfb_fence == obj->fence_reg &&
  1306. dev_priv->cfb_plane == intel_crtc->plane &&
  1307. dev_priv->cfb_y == crtc->y)
  1308. return;
  1309. I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1310. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1311. }
  1312. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1313. dev_priv->cfb_fence = obj->fence_reg;
  1314. dev_priv->cfb_plane = intel_crtc->plane;
  1315. dev_priv->cfb_y = crtc->y;
  1316. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  1317. if (obj->tiling_mode != I915_TILING_NONE) {
  1318. dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
  1319. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  1320. } else {
  1321. I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1322. }
  1323. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1324. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1325. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1326. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1327. /* enable it... */
  1328. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1329. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1330. }
  1331. void g4x_disable_fbc(struct drm_device *dev)
  1332. {
  1333. struct drm_i915_private *dev_priv = dev->dev_private;
  1334. u32 dpfc_ctl;
  1335. /* Disable compression */
  1336. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1337. if (dpfc_ctl & DPFC_CTL_EN) {
  1338. dpfc_ctl &= ~DPFC_CTL_EN;
  1339. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1340. DRM_DEBUG_KMS("disabled FBC\n");
  1341. }
  1342. }
  1343. static bool g4x_fbc_enabled(struct drm_device *dev)
  1344. {
  1345. struct drm_i915_private *dev_priv = dev->dev_private;
  1346. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1347. }
  1348. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  1349. {
  1350. struct drm_i915_private *dev_priv = dev->dev_private;
  1351. u32 blt_ecoskpd;
  1352. /* Make sure blitter notifies FBC of writes */
  1353. gen6_gt_force_wake_get(dev_priv);
  1354. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  1355. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  1356. GEN6_BLITTER_LOCK_SHIFT;
  1357. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1358. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  1359. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1360. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  1361. GEN6_BLITTER_LOCK_SHIFT);
  1362. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1363. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  1364. gen6_gt_force_wake_put(dev_priv);
  1365. }
  1366. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1367. {
  1368. struct drm_device *dev = crtc->dev;
  1369. struct drm_i915_private *dev_priv = dev->dev_private;
  1370. struct drm_framebuffer *fb = crtc->fb;
  1371. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1372. struct drm_i915_gem_object *obj = intel_fb->obj;
  1373. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1374. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1375. unsigned long stall_watermark = 200;
  1376. u32 dpfc_ctl;
  1377. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1378. if (dpfc_ctl & DPFC_CTL_EN) {
  1379. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1380. dev_priv->cfb_fence == obj->fence_reg &&
  1381. dev_priv->cfb_plane == intel_crtc->plane &&
  1382. dev_priv->cfb_offset == obj->gtt_offset &&
  1383. dev_priv->cfb_y == crtc->y)
  1384. return;
  1385. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1386. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1387. }
  1388. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1389. dev_priv->cfb_fence = obj->fence_reg;
  1390. dev_priv->cfb_plane = intel_crtc->plane;
  1391. dev_priv->cfb_offset = obj->gtt_offset;
  1392. dev_priv->cfb_y = crtc->y;
  1393. dpfc_ctl &= DPFC_RESERVED;
  1394. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1395. if (obj->tiling_mode != I915_TILING_NONE) {
  1396. dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
  1397. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1398. } else {
  1399. I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1400. }
  1401. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1402. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1403. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1404. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1405. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  1406. /* enable it... */
  1407. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  1408. if (IS_GEN6(dev)) {
  1409. I915_WRITE(SNB_DPFC_CTL_SA,
  1410. SNB_CPU_FENCE_ENABLE | dev_priv->cfb_fence);
  1411. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  1412. sandybridge_blit_fbc_update(dev);
  1413. }
  1414. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1415. }
  1416. void ironlake_disable_fbc(struct drm_device *dev)
  1417. {
  1418. struct drm_i915_private *dev_priv = dev->dev_private;
  1419. u32 dpfc_ctl;
  1420. /* Disable compression */
  1421. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1422. if (dpfc_ctl & DPFC_CTL_EN) {
  1423. dpfc_ctl &= ~DPFC_CTL_EN;
  1424. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1425. DRM_DEBUG_KMS("disabled FBC\n");
  1426. }
  1427. }
  1428. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1429. {
  1430. struct drm_i915_private *dev_priv = dev->dev_private;
  1431. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1432. }
  1433. bool intel_fbc_enabled(struct drm_device *dev)
  1434. {
  1435. struct drm_i915_private *dev_priv = dev->dev_private;
  1436. if (!dev_priv->display.fbc_enabled)
  1437. return false;
  1438. return dev_priv->display.fbc_enabled(dev);
  1439. }
  1440. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1441. {
  1442. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  1443. if (!dev_priv->display.enable_fbc)
  1444. return;
  1445. dev_priv->display.enable_fbc(crtc, interval);
  1446. }
  1447. void intel_disable_fbc(struct drm_device *dev)
  1448. {
  1449. struct drm_i915_private *dev_priv = dev->dev_private;
  1450. if (!dev_priv->display.disable_fbc)
  1451. return;
  1452. dev_priv->display.disable_fbc(dev);
  1453. }
  1454. /**
  1455. * intel_update_fbc - enable/disable FBC as needed
  1456. * @dev: the drm_device
  1457. *
  1458. * Set up the framebuffer compression hardware at mode set time. We
  1459. * enable it if possible:
  1460. * - plane A only (on pre-965)
  1461. * - no pixel mulitply/line duplication
  1462. * - no alpha buffer discard
  1463. * - no dual wide
  1464. * - framebuffer <= 2048 in width, 1536 in height
  1465. *
  1466. * We can't assume that any compression will take place (worst case),
  1467. * so the compressed buffer has to be the same size as the uncompressed
  1468. * one. It also must reside (along with the line length buffer) in
  1469. * stolen memory.
  1470. *
  1471. * We need to enable/disable FBC on a global basis.
  1472. */
  1473. static void intel_update_fbc(struct drm_device *dev)
  1474. {
  1475. struct drm_i915_private *dev_priv = dev->dev_private;
  1476. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1477. struct intel_crtc *intel_crtc;
  1478. struct drm_framebuffer *fb;
  1479. struct intel_framebuffer *intel_fb;
  1480. struct drm_i915_gem_object *obj;
  1481. DRM_DEBUG_KMS("\n");
  1482. if (!i915_powersave)
  1483. return;
  1484. if (!I915_HAS_FBC(dev))
  1485. return;
  1486. /*
  1487. * If FBC is already on, we just have to verify that we can
  1488. * keep it that way...
  1489. * Need to disable if:
  1490. * - more than one pipe is active
  1491. * - changing FBC params (stride, fence, mode)
  1492. * - new fb is too large to fit in compressed buffer
  1493. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1494. */
  1495. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1496. if (tmp_crtc->enabled && tmp_crtc->fb) {
  1497. if (crtc) {
  1498. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1499. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1500. goto out_disable;
  1501. }
  1502. crtc = tmp_crtc;
  1503. }
  1504. }
  1505. if (!crtc || crtc->fb == NULL) {
  1506. DRM_DEBUG_KMS("no output, disabling\n");
  1507. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1508. goto out_disable;
  1509. }
  1510. intel_crtc = to_intel_crtc(crtc);
  1511. fb = crtc->fb;
  1512. intel_fb = to_intel_framebuffer(fb);
  1513. obj = intel_fb->obj;
  1514. if (!i915_enable_fbc) {
  1515. DRM_DEBUG_KMS("fbc disabled per module param (default off)\n");
  1516. dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
  1517. goto out_disable;
  1518. }
  1519. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  1520. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1521. "compression\n");
  1522. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1523. goto out_disable;
  1524. }
  1525. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1526. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1527. DRM_DEBUG_KMS("mode incompatible with compression, "
  1528. "disabling\n");
  1529. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1530. goto out_disable;
  1531. }
  1532. if ((crtc->mode.hdisplay > 2048) ||
  1533. (crtc->mode.vdisplay > 1536)) {
  1534. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1535. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1536. goto out_disable;
  1537. }
  1538. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1539. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1540. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1541. goto out_disable;
  1542. }
  1543. if (obj->tiling_mode != I915_TILING_X) {
  1544. DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
  1545. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1546. goto out_disable;
  1547. }
  1548. /* If the kernel debugger is active, always disable compression */
  1549. if (in_dbg_master())
  1550. goto out_disable;
  1551. intel_enable_fbc(crtc, 500);
  1552. return;
  1553. out_disable:
  1554. /* Multiple disables should be harmless */
  1555. if (intel_fbc_enabled(dev)) {
  1556. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1557. intel_disable_fbc(dev);
  1558. }
  1559. }
  1560. int
  1561. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1562. struct drm_i915_gem_object *obj,
  1563. struct intel_ring_buffer *pipelined)
  1564. {
  1565. struct drm_i915_private *dev_priv = dev->dev_private;
  1566. u32 alignment;
  1567. int ret;
  1568. switch (obj->tiling_mode) {
  1569. case I915_TILING_NONE:
  1570. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1571. alignment = 128 * 1024;
  1572. else if (INTEL_INFO(dev)->gen >= 4)
  1573. alignment = 4 * 1024;
  1574. else
  1575. alignment = 64 * 1024;
  1576. break;
  1577. case I915_TILING_X:
  1578. /* pin() will align the object as required by fence */
  1579. alignment = 0;
  1580. break;
  1581. case I915_TILING_Y:
  1582. /* FIXME: Is this true? */
  1583. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1584. return -EINVAL;
  1585. default:
  1586. BUG();
  1587. }
  1588. dev_priv->mm.interruptible = false;
  1589. ret = i915_gem_object_pin(obj, alignment, true);
  1590. if (ret)
  1591. goto err_interruptible;
  1592. ret = i915_gem_object_set_to_display_plane(obj, pipelined);
  1593. if (ret)
  1594. goto err_unpin;
  1595. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1596. * fence, whereas 965+ only requires a fence if using
  1597. * framebuffer compression. For simplicity, we always install
  1598. * a fence as the cost is not that onerous.
  1599. */
  1600. if (obj->tiling_mode != I915_TILING_NONE) {
  1601. ret = i915_gem_object_get_fence(obj, pipelined);
  1602. if (ret)
  1603. goto err_unpin;
  1604. }
  1605. dev_priv->mm.interruptible = true;
  1606. return 0;
  1607. err_unpin:
  1608. i915_gem_object_unpin(obj);
  1609. err_interruptible:
  1610. dev_priv->mm.interruptible = true;
  1611. return ret;
  1612. }
  1613. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1614. static int
  1615. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1616. int x, int y, enum mode_set_atomic state)
  1617. {
  1618. struct drm_device *dev = crtc->dev;
  1619. struct drm_i915_private *dev_priv = dev->dev_private;
  1620. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1621. struct intel_framebuffer *intel_fb;
  1622. struct drm_i915_gem_object *obj;
  1623. int plane = intel_crtc->plane;
  1624. unsigned long Start, Offset;
  1625. u32 dspcntr;
  1626. u32 reg;
  1627. switch (plane) {
  1628. case 0:
  1629. case 1:
  1630. break;
  1631. default:
  1632. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1633. return -EINVAL;
  1634. }
  1635. intel_fb = to_intel_framebuffer(fb);
  1636. obj = intel_fb->obj;
  1637. reg = DSPCNTR(plane);
  1638. dspcntr = I915_READ(reg);
  1639. /* Mask out pixel format bits in case we change it */
  1640. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1641. switch (fb->bits_per_pixel) {
  1642. case 8:
  1643. dspcntr |= DISPPLANE_8BPP;
  1644. break;
  1645. case 16:
  1646. if (fb->depth == 15)
  1647. dspcntr |= DISPPLANE_15_16BPP;
  1648. else
  1649. dspcntr |= DISPPLANE_16BPP;
  1650. break;
  1651. case 24:
  1652. case 32:
  1653. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1654. break;
  1655. default:
  1656. DRM_ERROR("Unknown color depth\n");
  1657. return -EINVAL;
  1658. }
  1659. if (INTEL_INFO(dev)->gen >= 4) {
  1660. if (obj->tiling_mode != I915_TILING_NONE)
  1661. dspcntr |= DISPPLANE_TILED;
  1662. else
  1663. dspcntr &= ~DISPPLANE_TILED;
  1664. }
  1665. if (HAS_PCH_SPLIT(dev))
  1666. /* must disable */
  1667. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1668. I915_WRITE(reg, dspcntr);
  1669. Start = obj->gtt_offset;
  1670. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1671. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1672. Start, Offset, x, y, fb->pitch);
  1673. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1674. if (INTEL_INFO(dev)->gen >= 4) {
  1675. I915_WRITE(DSPSURF(plane), Start);
  1676. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1677. I915_WRITE(DSPADDR(plane), Offset);
  1678. } else
  1679. I915_WRITE(DSPADDR(plane), Start + Offset);
  1680. POSTING_READ(reg);
  1681. intel_update_fbc(dev);
  1682. intel_increase_pllclock(crtc);
  1683. return 0;
  1684. }
  1685. static int
  1686. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1687. struct drm_framebuffer *old_fb)
  1688. {
  1689. struct drm_device *dev = crtc->dev;
  1690. struct drm_i915_master_private *master_priv;
  1691. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1692. int ret;
  1693. /* no fb bound */
  1694. if (!crtc->fb) {
  1695. DRM_DEBUG_KMS("No FB bound\n");
  1696. return 0;
  1697. }
  1698. switch (intel_crtc->plane) {
  1699. case 0:
  1700. case 1:
  1701. break;
  1702. default:
  1703. return -EINVAL;
  1704. }
  1705. mutex_lock(&dev->struct_mutex);
  1706. ret = intel_pin_and_fence_fb_obj(dev,
  1707. to_intel_framebuffer(crtc->fb)->obj,
  1708. NULL);
  1709. if (ret != 0) {
  1710. mutex_unlock(&dev->struct_mutex);
  1711. return ret;
  1712. }
  1713. if (old_fb) {
  1714. struct drm_i915_private *dev_priv = dev->dev_private;
  1715. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1716. wait_event(dev_priv->pending_flip_queue,
  1717. atomic_read(&dev_priv->mm.wedged) ||
  1718. atomic_read(&obj->pending_flip) == 0);
  1719. /* Big Hammer, we also need to ensure that any pending
  1720. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1721. * current scanout is retired before unpinning the old
  1722. * framebuffer.
  1723. *
  1724. * This should only fail upon a hung GPU, in which case we
  1725. * can safely continue.
  1726. */
  1727. ret = i915_gem_object_flush_gpu(obj);
  1728. (void) ret;
  1729. }
  1730. ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
  1731. LEAVE_ATOMIC_MODE_SET);
  1732. if (ret) {
  1733. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1734. mutex_unlock(&dev->struct_mutex);
  1735. return ret;
  1736. }
  1737. if (old_fb) {
  1738. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1739. i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
  1740. }
  1741. mutex_unlock(&dev->struct_mutex);
  1742. if (!dev->primary->master)
  1743. return 0;
  1744. master_priv = dev->primary->master->driver_priv;
  1745. if (!master_priv->sarea_priv)
  1746. return 0;
  1747. if (intel_crtc->pipe) {
  1748. master_priv->sarea_priv->pipeB_x = x;
  1749. master_priv->sarea_priv->pipeB_y = y;
  1750. } else {
  1751. master_priv->sarea_priv->pipeA_x = x;
  1752. master_priv->sarea_priv->pipeA_y = y;
  1753. }
  1754. return 0;
  1755. }
  1756. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1757. {
  1758. struct drm_device *dev = crtc->dev;
  1759. struct drm_i915_private *dev_priv = dev->dev_private;
  1760. u32 dpa_ctl;
  1761. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1762. dpa_ctl = I915_READ(DP_A);
  1763. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1764. if (clock < 200000) {
  1765. u32 temp;
  1766. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1767. /* workaround for 160Mhz:
  1768. 1) program 0x4600c bits 15:0 = 0x8124
  1769. 2) program 0x46010 bit 0 = 1
  1770. 3) program 0x46034 bit 24 = 1
  1771. 4) program 0x64000 bit 14 = 1
  1772. */
  1773. temp = I915_READ(0x4600c);
  1774. temp &= 0xffff0000;
  1775. I915_WRITE(0x4600c, temp | 0x8124);
  1776. temp = I915_READ(0x46010);
  1777. I915_WRITE(0x46010, temp | 1);
  1778. temp = I915_READ(0x46034);
  1779. I915_WRITE(0x46034, temp | (1 << 24));
  1780. } else {
  1781. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1782. }
  1783. I915_WRITE(DP_A, dpa_ctl);
  1784. POSTING_READ(DP_A);
  1785. udelay(500);
  1786. }
  1787. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1788. {
  1789. struct drm_device *dev = crtc->dev;
  1790. struct drm_i915_private *dev_priv = dev->dev_private;
  1791. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1792. int pipe = intel_crtc->pipe;
  1793. u32 reg, temp;
  1794. /* enable normal train */
  1795. reg = FDI_TX_CTL(pipe);
  1796. temp = I915_READ(reg);
  1797. if (IS_GEN6(dev)) {
  1798. temp &= ~FDI_LINK_TRAIN_NONE;
  1799. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1800. } else if (IS_IVYBRIDGE(dev)) {
  1801. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  1802. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  1803. }
  1804. I915_WRITE(reg, temp);
  1805. reg = FDI_RX_CTL(pipe);
  1806. temp = I915_READ(reg);
  1807. if (HAS_PCH_CPT(dev)) {
  1808. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1809. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1810. } else {
  1811. temp &= ~FDI_LINK_TRAIN_NONE;
  1812. temp |= FDI_LINK_TRAIN_NONE;
  1813. }
  1814. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1815. /* wait one idle pattern time */
  1816. POSTING_READ(reg);
  1817. udelay(1000);
  1818. /* IVB wants error correction enabled */
  1819. if (IS_IVYBRIDGE(dev))
  1820. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  1821. FDI_FE_ERRC_ENABLE);
  1822. }
  1823. /* The FDI link training functions for ILK/Ibexpeak. */
  1824. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1825. {
  1826. struct drm_device *dev = crtc->dev;
  1827. struct drm_i915_private *dev_priv = dev->dev_private;
  1828. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1829. int pipe = intel_crtc->pipe;
  1830. int plane = intel_crtc->plane;
  1831. u32 reg, temp, tries;
  1832. /* FDI needs bits from pipe & plane first */
  1833. assert_pipe_enabled(dev_priv, pipe);
  1834. assert_plane_enabled(dev_priv, plane);
  1835. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1836. for train result */
  1837. reg = FDI_RX_IMR(pipe);
  1838. temp = I915_READ(reg);
  1839. temp &= ~FDI_RX_SYMBOL_LOCK;
  1840. temp &= ~FDI_RX_BIT_LOCK;
  1841. I915_WRITE(reg, temp);
  1842. I915_READ(reg);
  1843. udelay(150);
  1844. /* enable CPU FDI TX and PCH FDI RX */
  1845. reg = FDI_TX_CTL(pipe);
  1846. temp = I915_READ(reg);
  1847. temp &= ~(7 << 19);
  1848. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1849. temp &= ~FDI_LINK_TRAIN_NONE;
  1850. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1851. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1852. reg = FDI_RX_CTL(pipe);
  1853. temp = I915_READ(reg);
  1854. temp &= ~FDI_LINK_TRAIN_NONE;
  1855. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1856. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1857. POSTING_READ(reg);
  1858. udelay(150);
  1859. /* Ironlake workaround, enable clock pointer after FDI enable*/
  1860. if (HAS_PCH_IBX(dev)) {
  1861. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  1862. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  1863. FDI_RX_PHASE_SYNC_POINTER_EN);
  1864. }
  1865. reg = FDI_RX_IIR(pipe);
  1866. for (tries = 0; tries < 5; tries++) {
  1867. temp = I915_READ(reg);
  1868. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1869. if ((temp & FDI_RX_BIT_LOCK)) {
  1870. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1871. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1872. break;
  1873. }
  1874. }
  1875. if (tries == 5)
  1876. DRM_ERROR("FDI train 1 fail!\n");
  1877. /* Train 2 */
  1878. reg = FDI_TX_CTL(pipe);
  1879. temp = I915_READ(reg);
  1880. temp &= ~FDI_LINK_TRAIN_NONE;
  1881. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1882. I915_WRITE(reg, temp);
  1883. reg = FDI_RX_CTL(pipe);
  1884. temp = I915_READ(reg);
  1885. temp &= ~FDI_LINK_TRAIN_NONE;
  1886. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1887. I915_WRITE(reg, temp);
  1888. POSTING_READ(reg);
  1889. udelay(150);
  1890. reg = FDI_RX_IIR(pipe);
  1891. for (tries = 0; tries < 5; tries++) {
  1892. temp = I915_READ(reg);
  1893. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1894. if (temp & FDI_RX_SYMBOL_LOCK) {
  1895. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1896. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1897. break;
  1898. }
  1899. }
  1900. if (tries == 5)
  1901. DRM_ERROR("FDI train 2 fail!\n");
  1902. DRM_DEBUG_KMS("FDI train done\n");
  1903. }
  1904. static const int snb_b_fdi_train_param [] = {
  1905. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1906. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1907. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1908. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1909. };
  1910. /* The FDI link training functions for SNB/Cougarpoint. */
  1911. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1912. {
  1913. struct drm_device *dev = crtc->dev;
  1914. struct drm_i915_private *dev_priv = dev->dev_private;
  1915. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1916. int pipe = intel_crtc->pipe;
  1917. u32 reg, temp, i;
  1918. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1919. for train result */
  1920. reg = FDI_RX_IMR(pipe);
  1921. temp = I915_READ(reg);
  1922. temp &= ~FDI_RX_SYMBOL_LOCK;
  1923. temp &= ~FDI_RX_BIT_LOCK;
  1924. I915_WRITE(reg, temp);
  1925. POSTING_READ(reg);
  1926. udelay(150);
  1927. /* enable CPU FDI TX and PCH FDI RX */
  1928. reg = FDI_TX_CTL(pipe);
  1929. temp = I915_READ(reg);
  1930. temp &= ~(7 << 19);
  1931. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1932. temp &= ~FDI_LINK_TRAIN_NONE;
  1933. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1934. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1935. /* SNB-B */
  1936. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1937. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1938. reg = FDI_RX_CTL(pipe);
  1939. temp = I915_READ(reg);
  1940. if (HAS_PCH_CPT(dev)) {
  1941. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1942. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1943. } else {
  1944. temp &= ~FDI_LINK_TRAIN_NONE;
  1945. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1946. }
  1947. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1948. POSTING_READ(reg);
  1949. udelay(150);
  1950. for (i = 0; i < 4; i++ ) {
  1951. reg = FDI_TX_CTL(pipe);
  1952. temp = I915_READ(reg);
  1953. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1954. temp |= snb_b_fdi_train_param[i];
  1955. I915_WRITE(reg, temp);
  1956. POSTING_READ(reg);
  1957. udelay(500);
  1958. reg = FDI_RX_IIR(pipe);
  1959. temp = I915_READ(reg);
  1960. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1961. if (temp & FDI_RX_BIT_LOCK) {
  1962. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1963. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1964. break;
  1965. }
  1966. }
  1967. if (i == 4)
  1968. DRM_ERROR("FDI train 1 fail!\n");
  1969. /* Train 2 */
  1970. reg = FDI_TX_CTL(pipe);
  1971. temp = I915_READ(reg);
  1972. temp &= ~FDI_LINK_TRAIN_NONE;
  1973. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1974. if (IS_GEN6(dev)) {
  1975. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1976. /* SNB-B */
  1977. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1978. }
  1979. I915_WRITE(reg, temp);
  1980. reg = FDI_RX_CTL(pipe);
  1981. temp = I915_READ(reg);
  1982. if (HAS_PCH_CPT(dev)) {
  1983. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1984. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  1985. } else {
  1986. temp &= ~FDI_LINK_TRAIN_NONE;
  1987. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1988. }
  1989. I915_WRITE(reg, temp);
  1990. POSTING_READ(reg);
  1991. udelay(150);
  1992. for (i = 0; i < 4; i++ ) {
  1993. reg = FDI_TX_CTL(pipe);
  1994. temp = I915_READ(reg);
  1995. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1996. temp |= snb_b_fdi_train_param[i];
  1997. I915_WRITE(reg, temp);
  1998. POSTING_READ(reg);
  1999. udelay(500);
  2000. reg = FDI_RX_IIR(pipe);
  2001. temp = I915_READ(reg);
  2002. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2003. if (temp & FDI_RX_SYMBOL_LOCK) {
  2004. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2005. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2006. break;
  2007. }
  2008. }
  2009. if (i == 4)
  2010. DRM_ERROR("FDI train 2 fail!\n");
  2011. DRM_DEBUG_KMS("FDI train done.\n");
  2012. }
  2013. /* Manual link training for Ivy Bridge A0 parts */
  2014. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2015. {
  2016. struct drm_device *dev = crtc->dev;
  2017. struct drm_i915_private *dev_priv = dev->dev_private;
  2018. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2019. int pipe = intel_crtc->pipe;
  2020. u32 reg, temp, i;
  2021. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2022. for train result */
  2023. reg = FDI_RX_IMR(pipe);
  2024. temp = I915_READ(reg);
  2025. temp &= ~FDI_RX_SYMBOL_LOCK;
  2026. temp &= ~FDI_RX_BIT_LOCK;
  2027. I915_WRITE(reg, temp);
  2028. POSTING_READ(reg);
  2029. udelay(150);
  2030. /* enable CPU FDI TX and PCH FDI RX */
  2031. reg = FDI_TX_CTL(pipe);
  2032. temp = I915_READ(reg);
  2033. temp &= ~(7 << 19);
  2034. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2035. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2036. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2037. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2038. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2039. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2040. reg = FDI_RX_CTL(pipe);
  2041. temp = I915_READ(reg);
  2042. temp &= ~FDI_LINK_TRAIN_AUTO;
  2043. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2044. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2045. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2046. POSTING_READ(reg);
  2047. udelay(150);
  2048. for (i = 0; i < 4; i++ ) {
  2049. reg = FDI_TX_CTL(pipe);
  2050. temp = I915_READ(reg);
  2051. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2052. temp |= snb_b_fdi_train_param[i];
  2053. I915_WRITE(reg, temp);
  2054. POSTING_READ(reg);
  2055. udelay(500);
  2056. reg = FDI_RX_IIR(pipe);
  2057. temp = I915_READ(reg);
  2058. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2059. if (temp & FDI_RX_BIT_LOCK ||
  2060. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2061. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2062. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2063. break;
  2064. }
  2065. }
  2066. if (i == 4)
  2067. DRM_ERROR("FDI train 1 fail!\n");
  2068. /* Train 2 */
  2069. reg = FDI_TX_CTL(pipe);
  2070. temp = I915_READ(reg);
  2071. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2072. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2073. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2074. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2075. I915_WRITE(reg, temp);
  2076. reg = FDI_RX_CTL(pipe);
  2077. temp = I915_READ(reg);
  2078. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2079. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2080. I915_WRITE(reg, temp);
  2081. POSTING_READ(reg);
  2082. udelay(150);
  2083. for (i = 0; i < 4; i++ ) {
  2084. reg = FDI_TX_CTL(pipe);
  2085. temp = I915_READ(reg);
  2086. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2087. temp |= snb_b_fdi_train_param[i];
  2088. I915_WRITE(reg, temp);
  2089. POSTING_READ(reg);
  2090. udelay(500);
  2091. reg = FDI_RX_IIR(pipe);
  2092. temp = I915_READ(reg);
  2093. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2094. if (temp & FDI_RX_SYMBOL_LOCK) {
  2095. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2096. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2097. break;
  2098. }
  2099. }
  2100. if (i == 4)
  2101. DRM_ERROR("FDI train 2 fail!\n");
  2102. DRM_DEBUG_KMS("FDI train done.\n");
  2103. }
  2104. static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
  2105. {
  2106. struct drm_device *dev = crtc->dev;
  2107. struct drm_i915_private *dev_priv = dev->dev_private;
  2108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2109. int pipe = intel_crtc->pipe;
  2110. u32 reg, temp;
  2111. /* Write the TU size bits so error detection works */
  2112. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2113. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2114. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2115. reg = FDI_RX_CTL(pipe);
  2116. temp = I915_READ(reg);
  2117. temp &= ~((0x7 << 19) | (0x7 << 16));
  2118. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2119. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2120. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2121. POSTING_READ(reg);
  2122. udelay(200);
  2123. /* Switch from Rawclk to PCDclk */
  2124. temp = I915_READ(reg);
  2125. I915_WRITE(reg, temp | FDI_PCDCLK);
  2126. POSTING_READ(reg);
  2127. udelay(200);
  2128. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2129. reg = FDI_TX_CTL(pipe);
  2130. temp = I915_READ(reg);
  2131. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2132. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2133. POSTING_READ(reg);
  2134. udelay(100);
  2135. }
  2136. }
  2137. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2138. {
  2139. struct drm_device *dev = crtc->dev;
  2140. struct drm_i915_private *dev_priv = dev->dev_private;
  2141. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2142. int pipe = intel_crtc->pipe;
  2143. u32 reg, temp;
  2144. /* disable CPU FDI tx and PCH FDI rx */
  2145. reg = FDI_TX_CTL(pipe);
  2146. temp = I915_READ(reg);
  2147. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2148. POSTING_READ(reg);
  2149. reg = FDI_RX_CTL(pipe);
  2150. temp = I915_READ(reg);
  2151. temp &= ~(0x7 << 16);
  2152. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2153. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2154. POSTING_READ(reg);
  2155. udelay(100);
  2156. /* Ironlake workaround, disable clock pointer after downing FDI */
  2157. if (HAS_PCH_IBX(dev)) {
  2158. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2159. I915_WRITE(FDI_RX_CHICKEN(pipe),
  2160. I915_READ(FDI_RX_CHICKEN(pipe) &
  2161. ~FDI_RX_PHASE_SYNC_POINTER_EN));
  2162. }
  2163. /* still set train pattern 1 */
  2164. reg = FDI_TX_CTL(pipe);
  2165. temp = I915_READ(reg);
  2166. temp &= ~FDI_LINK_TRAIN_NONE;
  2167. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2168. I915_WRITE(reg, temp);
  2169. reg = FDI_RX_CTL(pipe);
  2170. temp = I915_READ(reg);
  2171. if (HAS_PCH_CPT(dev)) {
  2172. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2173. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2174. } else {
  2175. temp &= ~FDI_LINK_TRAIN_NONE;
  2176. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2177. }
  2178. /* BPC in FDI rx is consistent with that in PIPECONF */
  2179. temp &= ~(0x07 << 16);
  2180. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2181. I915_WRITE(reg, temp);
  2182. POSTING_READ(reg);
  2183. udelay(100);
  2184. }
  2185. /*
  2186. * When we disable a pipe, we need to clear any pending scanline wait events
  2187. * to avoid hanging the ring, which we assume we are waiting on.
  2188. */
  2189. static void intel_clear_scanline_wait(struct drm_device *dev)
  2190. {
  2191. struct drm_i915_private *dev_priv = dev->dev_private;
  2192. struct intel_ring_buffer *ring;
  2193. u32 tmp;
  2194. if (IS_GEN2(dev))
  2195. /* Can't break the hang on i8xx */
  2196. return;
  2197. ring = LP_RING(dev_priv);
  2198. tmp = I915_READ_CTL(ring);
  2199. if (tmp & RING_WAIT)
  2200. I915_WRITE_CTL(ring, tmp);
  2201. }
  2202. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2203. {
  2204. struct drm_i915_gem_object *obj;
  2205. struct drm_i915_private *dev_priv;
  2206. if (crtc->fb == NULL)
  2207. return;
  2208. obj = to_intel_framebuffer(crtc->fb)->obj;
  2209. dev_priv = crtc->dev->dev_private;
  2210. wait_event(dev_priv->pending_flip_queue,
  2211. atomic_read(&obj->pending_flip) == 0);
  2212. }
  2213. static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
  2214. {
  2215. struct drm_device *dev = crtc->dev;
  2216. struct drm_mode_config *mode_config = &dev->mode_config;
  2217. struct intel_encoder *encoder;
  2218. /*
  2219. * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
  2220. * must be driven by its own crtc; no sharing is possible.
  2221. */
  2222. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  2223. if (encoder->base.crtc != crtc)
  2224. continue;
  2225. switch (encoder->type) {
  2226. case INTEL_OUTPUT_EDP:
  2227. if (!intel_encoder_is_pch_edp(&encoder->base))
  2228. return false;
  2229. continue;
  2230. }
  2231. }
  2232. return true;
  2233. }
  2234. /*
  2235. * Enable PCH resources required for PCH ports:
  2236. * - PCH PLLs
  2237. * - FDI training & RX/TX
  2238. * - update transcoder timings
  2239. * - DP transcoding bits
  2240. * - transcoder
  2241. */
  2242. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2243. {
  2244. struct drm_device *dev = crtc->dev;
  2245. struct drm_i915_private *dev_priv = dev->dev_private;
  2246. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2247. int pipe = intel_crtc->pipe;
  2248. u32 reg, temp;
  2249. /* For PCH output, training FDI link */
  2250. dev_priv->display.fdi_link_train(crtc);
  2251. intel_enable_pch_pll(dev_priv, pipe);
  2252. if (HAS_PCH_CPT(dev)) {
  2253. /* Be sure PCH DPLL SEL is set */
  2254. temp = I915_READ(PCH_DPLL_SEL);
  2255. if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
  2256. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  2257. else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
  2258. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2259. I915_WRITE(PCH_DPLL_SEL, temp);
  2260. }
  2261. /* set transcoder timing, panel must allow it */
  2262. assert_panel_unlocked(dev_priv, pipe);
  2263. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2264. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2265. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2266. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2267. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2268. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2269. intel_fdi_normal_train(crtc);
  2270. /* For PCH DP, enable TRANS_DP_CTL */
  2271. if (HAS_PCH_CPT(dev) &&
  2272. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  2273. reg = TRANS_DP_CTL(pipe);
  2274. temp = I915_READ(reg);
  2275. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2276. TRANS_DP_SYNC_MASK |
  2277. TRANS_DP_BPC_MASK);
  2278. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2279. TRANS_DP_ENH_FRAMING);
  2280. temp |= TRANS_DP_8BPC;
  2281. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2282. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2283. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2284. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2285. switch (intel_trans_dp_port_sel(crtc)) {
  2286. case PCH_DP_B:
  2287. temp |= TRANS_DP_PORT_SEL_B;
  2288. break;
  2289. case PCH_DP_C:
  2290. temp |= TRANS_DP_PORT_SEL_C;
  2291. break;
  2292. case PCH_DP_D:
  2293. temp |= TRANS_DP_PORT_SEL_D;
  2294. break;
  2295. default:
  2296. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  2297. temp |= TRANS_DP_PORT_SEL_B;
  2298. break;
  2299. }
  2300. I915_WRITE(reg, temp);
  2301. }
  2302. intel_enable_transcoder(dev_priv, pipe);
  2303. }
  2304. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2305. {
  2306. struct drm_device *dev = crtc->dev;
  2307. struct drm_i915_private *dev_priv = dev->dev_private;
  2308. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2309. int pipe = intel_crtc->pipe;
  2310. int plane = intel_crtc->plane;
  2311. u32 temp;
  2312. bool is_pch_port;
  2313. if (intel_crtc->active)
  2314. return;
  2315. intel_crtc->active = true;
  2316. intel_update_watermarks(dev);
  2317. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2318. temp = I915_READ(PCH_LVDS);
  2319. if ((temp & LVDS_PORT_EN) == 0)
  2320. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2321. }
  2322. is_pch_port = intel_crtc_driving_pch(crtc);
  2323. if (is_pch_port)
  2324. ironlake_fdi_pll_enable(crtc);
  2325. else
  2326. ironlake_fdi_disable(crtc);
  2327. /* Enable panel fitting for LVDS */
  2328. if (dev_priv->pch_pf_size &&
  2329. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  2330. /* Force use of hard-coded filter coefficients
  2331. * as some pre-programmed values are broken,
  2332. * e.g. x201.
  2333. */
  2334. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2335. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2336. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2337. }
  2338. intel_enable_pipe(dev_priv, pipe, is_pch_port);
  2339. intel_enable_plane(dev_priv, plane, pipe);
  2340. if (is_pch_port)
  2341. ironlake_pch_enable(crtc);
  2342. intel_crtc_load_lut(crtc);
  2343. mutex_lock(&dev->struct_mutex);
  2344. intel_update_fbc(dev);
  2345. mutex_unlock(&dev->struct_mutex);
  2346. intel_crtc_update_cursor(crtc, true);
  2347. }
  2348. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2349. {
  2350. struct drm_device *dev = crtc->dev;
  2351. struct drm_i915_private *dev_priv = dev->dev_private;
  2352. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2353. int pipe = intel_crtc->pipe;
  2354. int plane = intel_crtc->plane;
  2355. u32 reg, temp;
  2356. if (!intel_crtc->active)
  2357. return;
  2358. intel_crtc_wait_for_pending_flips(crtc);
  2359. drm_vblank_off(dev, pipe);
  2360. intel_crtc_update_cursor(crtc, false);
  2361. intel_disable_plane(dev_priv, plane, pipe);
  2362. if (dev_priv->cfb_plane == plane &&
  2363. dev_priv->display.disable_fbc)
  2364. dev_priv->display.disable_fbc(dev);
  2365. intel_disable_pipe(dev_priv, pipe);
  2366. /* Disable PF */
  2367. I915_WRITE(PF_CTL(pipe), 0);
  2368. I915_WRITE(PF_WIN_SZ(pipe), 0);
  2369. ironlake_fdi_disable(crtc);
  2370. /* This is a horrible layering violation; we should be doing this in
  2371. * the connector/encoder ->prepare instead, but we don't always have
  2372. * enough information there about the config to know whether it will
  2373. * actually be necessary or just cause undesired flicker.
  2374. */
  2375. intel_disable_pch_ports(dev_priv, pipe);
  2376. intel_disable_transcoder(dev_priv, pipe);
  2377. if (HAS_PCH_CPT(dev)) {
  2378. /* disable TRANS_DP_CTL */
  2379. reg = TRANS_DP_CTL(pipe);
  2380. temp = I915_READ(reg);
  2381. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2382. temp |= TRANS_DP_PORT_SEL_NONE;
  2383. I915_WRITE(reg, temp);
  2384. /* disable DPLL_SEL */
  2385. temp = I915_READ(PCH_DPLL_SEL);
  2386. switch (pipe) {
  2387. case 0:
  2388. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  2389. break;
  2390. case 1:
  2391. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2392. break;
  2393. case 2:
  2394. /* FIXME: manage transcoder PLLs? */
  2395. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2396. break;
  2397. default:
  2398. BUG(); /* wtf */
  2399. }
  2400. I915_WRITE(PCH_DPLL_SEL, temp);
  2401. }
  2402. /* disable PCH DPLL */
  2403. intel_disable_pch_pll(dev_priv, pipe);
  2404. /* Switch from PCDclk to Rawclk */
  2405. reg = FDI_RX_CTL(pipe);
  2406. temp = I915_READ(reg);
  2407. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2408. /* Disable CPU FDI TX PLL */
  2409. reg = FDI_TX_CTL(pipe);
  2410. temp = I915_READ(reg);
  2411. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2412. POSTING_READ(reg);
  2413. udelay(100);
  2414. reg = FDI_RX_CTL(pipe);
  2415. temp = I915_READ(reg);
  2416. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2417. /* Wait for the clocks to turn off. */
  2418. POSTING_READ(reg);
  2419. udelay(100);
  2420. intel_crtc->active = false;
  2421. intel_update_watermarks(dev);
  2422. mutex_lock(&dev->struct_mutex);
  2423. intel_update_fbc(dev);
  2424. intel_clear_scanline_wait(dev);
  2425. mutex_unlock(&dev->struct_mutex);
  2426. }
  2427. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2428. {
  2429. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2430. int pipe = intel_crtc->pipe;
  2431. int plane = intel_crtc->plane;
  2432. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2433. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2434. */
  2435. switch (mode) {
  2436. case DRM_MODE_DPMS_ON:
  2437. case DRM_MODE_DPMS_STANDBY:
  2438. case DRM_MODE_DPMS_SUSPEND:
  2439. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2440. ironlake_crtc_enable(crtc);
  2441. break;
  2442. case DRM_MODE_DPMS_OFF:
  2443. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2444. ironlake_crtc_disable(crtc);
  2445. break;
  2446. }
  2447. }
  2448. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2449. {
  2450. if (!enable && intel_crtc->overlay) {
  2451. struct drm_device *dev = intel_crtc->base.dev;
  2452. struct drm_i915_private *dev_priv = dev->dev_private;
  2453. mutex_lock(&dev->struct_mutex);
  2454. dev_priv->mm.interruptible = false;
  2455. (void) intel_overlay_switch_off(intel_crtc->overlay);
  2456. dev_priv->mm.interruptible = true;
  2457. mutex_unlock(&dev->struct_mutex);
  2458. }
  2459. /* Let userspace switch the overlay on again. In most cases userspace
  2460. * has to recompute where to put it anyway.
  2461. */
  2462. }
  2463. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2464. {
  2465. struct drm_device *dev = crtc->dev;
  2466. struct drm_i915_private *dev_priv = dev->dev_private;
  2467. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2468. int pipe = intel_crtc->pipe;
  2469. int plane = intel_crtc->plane;
  2470. if (intel_crtc->active)
  2471. return;
  2472. intel_crtc->active = true;
  2473. intel_update_watermarks(dev);
  2474. intel_enable_pll(dev_priv, pipe);
  2475. intel_enable_pipe(dev_priv, pipe, false);
  2476. intel_enable_plane(dev_priv, plane, pipe);
  2477. intel_crtc_load_lut(crtc);
  2478. intel_update_fbc(dev);
  2479. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2480. intel_crtc_dpms_overlay(intel_crtc, true);
  2481. intel_crtc_update_cursor(crtc, true);
  2482. }
  2483. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2484. {
  2485. struct drm_device *dev = crtc->dev;
  2486. struct drm_i915_private *dev_priv = dev->dev_private;
  2487. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2488. int pipe = intel_crtc->pipe;
  2489. int plane = intel_crtc->plane;
  2490. if (!intel_crtc->active)
  2491. return;
  2492. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2493. intel_crtc_wait_for_pending_flips(crtc);
  2494. drm_vblank_off(dev, pipe);
  2495. intel_crtc_dpms_overlay(intel_crtc, false);
  2496. intel_crtc_update_cursor(crtc, false);
  2497. if (dev_priv->cfb_plane == plane &&
  2498. dev_priv->display.disable_fbc)
  2499. dev_priv->display.disable_fbc(dev);
  2500. intel_disable_plane(dev_priv, plane, pipe);
  2501. intel_disable_pipe(dev_priv, pipe);
  2502. intel_disable_pll(dev_priv, pipe);
  2503. intel_crtc->active = false;
  2504. intel_update_fbc(dev);
  2505. intel_update_watermarks(dev);
  2506. intel_clear_scanline_wait(dev);
  2507. }
  2508. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2509. {
  2510. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2511. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2512. */
  2513. switch (mode) {
  2514. case DRM_MODE_DPMS_ON:
  2515. case DRM_MODE_DPMS_STANDBY:
  2516. case DRM_MODE_DPMS_SUSPEND:
  2517. i9xx_crtc_enable(crtc);
  2518. break;
  2519. case DRM_MODE_DPMS_OFF:
  2520. i9xx_crtc_disable(crtc);
  2521. break;
  2522. }
  2523. }
  2524. /**
  2525. * Sets the power management mode of the pipe and plane.
  2526. */
  2527. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2528. {
  2529. struct drm_device *dev = crtc->dev;
  2530. struct drm_i915_private *dev_priv = dev->dev_private;
  2531. struct drm_i915_master_private *master_priv;
  2532. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2533. int pipe = intel_crtc->pipe;
  2534. bool enabled;
  2535. if (intel_crtc->dpms_mode == mode)
  2536. return;
  2537. intel_crtc->dpms_mode = mode;
  2538. dev_priv->display.dpms(crtc, mode);
  2539. if (!dev->primary->master)
  2540. return;
  2541. master_priv = dev->primary->master->driver_priv;
  2542. if (!master_priv->sarea_priv)
  2543. return;
  2544. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2545. switch (pipe) {
  2546. case 0:
  2547. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2548. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2549. break;
  2550. case 1:
  2551. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2552. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2553. break;
  2554. default:
  2555. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  2556. break;
  2557. }
  2558. }
  2559. static void intel_crtc_disable(struct drm_crtc *crtc)
  2560. {
  2561. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2562. struct drm_device *dev = crtc->dev;
  2563. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2564. if (crtc->fb) {
  2565. mutex_lock(&dev->struct_mutex);
  2566. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  2567. mutex_unlock(&dev->struct_mutex);
  2568. }
  2569. }
  2570. /* Prepare for a mode set.
  2571. *
  2572. * Note we could be a lot smarter here. We need to figure out which outputs
  2573. * will be enabled, which disabled (in short, how the config will changes)
  2574. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2575. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2576. * panel fitting is in the proper state, etc.
  2577. */
  2578. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2579. {
  2580. i9xx_crtc_disable(crtc);
  2581. }
  2582. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2583. {
  2584. i9xx_crtc_enable(crtc);
  2585. }
  2586. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2587. {
  2588. ironlake_crtc_disable(crtc);
  2589. }
  2590. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2591. {
  2592. ironlake_crtc_enable(crtc);
  2593. }
  2594. void intel_encoder_prepare (struct drm_encoder *encoder)
  2595. {
  2596. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2597. /* lvds has its own version of prepare see intel_lvds_prepare */
  2598. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2599. }
  2600. void intel_encoder_commit (struct drm_encoder *encoder)
  2601. {
  2602. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2603. /* lvds has its own version of commit see intel_lvds_commit */
  2604. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2605. }
  2606. void intel_encoder_destroy(struct drm_encoder *encoder)
  2607. {
  2608. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2609. drm_encoder_cleanup(encoder);
  2610. kfree(intel_encoder);
  2611. }
  2612. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2613. struct drm_display_mode *mode,
  2614. struct drm_display_mode *adjusted_mode)
  2615. {
  2616. struct drm_device *dev = crtc->dev;
  2617. if (HAS_PCH_SPLIT(dev)) {
  2618. /* FDI link clock is fixed at 2.7G */
  2619. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2620. return false;
  2621. }
  2622. /* XXX some encoders set the crtcinfo, others don't.
  2623. * Obviously we need some form of conflict resolution here...
  2624. */
  2625. if (adjusted_mode->crtc_htotal == 0)
  2626. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2627. return true;
  2628. }
  2629. static int i945_get_display_clock_speed(struct drm_device *dev)
  2630. {
  2631. return 400000;
  2632. }
  2633. static int i915_get_display_clock_speed(struct drm_device *dev)
  2634. {
  2635. return 333000;
  2636. }
  2637. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2638. {
  2639. return 200000;
  2640. }
  2641. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2642. {
  2643. u16 gcfgc = 0;
  2644. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2645. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2646. return 133000;
  2647. else {
  2648. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2649. case GC_DISPLAY_CLOCK_333_MHZ:
  2650. return 333000;
  2651. default:
  2652. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2653. return 190000;
  2654. }
  2655. }
  2656. }
  2657. static int i865_get_display_clock_speed(struct drm_device *dev)
  2658. {
  2659. return 266000;
  2660. }
  2661. static int i855_get_display_clock_speed(struct drm_device *dev)
  2662. {
  2663. u16 hpllcc = 0;
  2664. /* Assume that the hardware is in the high speed state. This
  2665. * should be the default.
  2666. */
  2667. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2668. case GC_CLOCK_133_200:
  2669. case GC_CLOCK_100_200:
  2670. return 200000;
  2671. case GC_CLOCK_166_250:
  2672. return 250000;
  2673. case GC_CLOCK_100_133:
  2674. return 133000;
  2675. }
  2676. /* Shouldn't happen */
  2677. return 0;
  2678. }
  2679. static int i830_get_display_clock_speed(struct drm_device *dev)
  2680. {
  2681. return 133000;
  2682. }
  2683. struct fdi_m_n {
  2684. u32 tu;
  2685. u32 gmch_m;
  2686. u32 gmch_n;
  2687. u32 link_m;
  2688. u32 link_n;
  2689. };
  2690. static void
  2691. fdi_reduce_ratio(u32 *num, u32 *den)
  2692. {
  2693. while (*num > 0xffffff || *den > 0xffffff) {
  2694. *num >>= 1;
  2695. *den >>= 1;
  2696. }
  2697. }
  2698. static void
  2699. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2700. int link_clock, struct fdi_m_n *m_n)
  2701. {
  2702. m_n->tu = 64; /* default size */
  2703. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  2704. m_n->gmch_m = bits_per_pixel * pixel_clock;
  2705. m_n->gmch_n = link_clock * nlanes * 8;
  2706. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2707. m_n->link_m = pixel_clock;
  2708. m_n->link_n = link_clock;
  2709. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2710. }
  2711. struct intel_watermark_params {
  2712. unsigned long fifo_size;
  2713. unsigned long max_wm;
  2714. unsigned long default_wm;
  2715. unsigned long guard_size;
  2716. unsigned long cacheline_size;
  2717. };
  2718. /* Pineview has different values for various configs */
  2719. static const struct intel_watermark_params pineview_display_wm = {
  2720. PINEVIEW_DISPLAY_FIFO,
  2721. PINEVIEW_MAX_WM,
  2722. PINEVIEW_DFT_WM,
  2723. PINEVIEW_GUARD_WM,
  2724. PINEVIEW_FIFO_LINE_SIZE
  2725. };
  2726. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  2727. PINEVIEW_DISPLAY_FIFO,
  2728. PINEVIEW_MAX_WM,
  2729. PINEVIEW_DFT_HPLLOFF_WM,
  2730. PINEVIEW_GUARD_WM,
  2731. PINEVIEW_FIFO_LINE_SIZE
  2732. };
  2733. static const struct intel_watermark_params pineview_cursor_wm = {
  2734. PINEVIEW_CURSOR_FIFO,
  2735. PINEVIEW_CURSOR_MAX_WM,
  2736. PINEVIEW_CURSOR_DFT_WM,
  2737. PINEVIEW_CURSOR_GUARD_WM,
  2738. PINEVIEW_FIFO_LINE_SIZE,
  2739. };
  2740. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2741. PINEVIEW_CURSOR_FIFO,
  2742. PINEVIEW_CURSOR_MAX_WM,
  2743. PINEVIEW_CURSOR_DFT_WM,
  2744. PINEVIEW_CURSOR_GUARD_WM,
  2745. PINEVIEW_FIFO_LINE_SIZE
  2746. };
  2747. static const struct intel_watermark_params g4x_wm_info = {
  2748. G4X_FIFO_SIZE,
  2749. G4X_MAX_WM,
  2750. G4X_MAX_WM,
  2751. 2,
  2752. G4X_FIFO_LINE_SIZE,
  2753. };
  2754. static const struct intel_watermark_params g4x_cursor_wm_info = {
  2755. I965_CURSOR_FIFO,
  2756. I965_CURSOR_MAX_WM,
  2757. I965_CURSOR_DFT_WM,
  2758. 2,
  2759. G4X_FIFO_LINE_SIZE,
  2760. };
  2761. static const struct intel_watermark_params i965_cursor_wm_info = {
  2762. I965_CURSOR_FIFO,
  2763. I965_CURSOR_MAX_WM,
  2764. I965_CURSOR_DFT_WM,
  2765. 2,
  2766. I915_FIFO_LINE_SIZE,
  2767. };
  2768. static const struct intel_watermark_params i945_wm_info = {
  2769. I945_FIFO_SIZE,
  2770. I915_MAX_WM,
  2771. 1,
  2772. 2,
  2773. I915_FIFO_LINE_SIZE
  2774. };
  2775. static const struct intel_watermark_params i915_wm_info = {
  2776. I915_FIFO_SIZE,
  2777. I915_MAX_WM,
  2778. 1,
  2779. 2,
  2780. I915_FIFO_LINE_SIZE
  2781. };
  2782. static const struct intel_watermark_params i855_wm_info = {
  2783. I855GM_FIFO_SIZE,
  2784. I915_MAX_WM,
  2785. 1,
  2786. 2,
  2787. I830_FIFO_LINE_SIZE
  2788. };
  2789. static const struct intel_watermark_params i830_wm_info = {
  2790. I830_FIFO_SIZE,
  2791. I915_MAX_WM,
  2792. 1,
  2793. 2,
  2794. I830_FIFO_LINE_SIZE
  2795. };
  2796. static const struct intel_watermark_params ironlake_display_wm_info = {
  2797. ILK_DISPLAY_FIFO,
  2798. ILK_DISPLAY_MAXWM,
  2799. ILK_DISPLAY_DFTWM,
  2800. 2,
  2801. ILK_FIFO_LINE_SIZE
  2802. };
  2803. static const struct intel_watermark_params ironlake_cursor_wm_info = {
  2804. ILK_CURSOR_FIFO,
  2805. ILK_CURSOR_MAXWM,
  2806. ILK_CURSOR_DFTWM,
  2807. 2,
  2808. ILK_FIFO_LINE_SIZE
  2809. };
  2810. static const struct intel_watermark_params ironlake_display_srwm_info = {
  2811. ILK_DISPLAY_SR_FIFO,
  2812. ILK_DISPLAY_MAX_SRWM,
  2813. ILK_DISPLAY_DFT_SRWM,
  2814. 2,
  2815. ILK_FIFO_LINE_SIZE
  2816. };
  2817. static const struct intel_watermark_params ironlake_cursor_srwm_info = {
  2818. ILK_CURSOR_SR_FIFO,
  2819. ILK_CURSOR_MAX_SRWM,
  2820. ILK_CURSOR_DFT_SRWM,
  2821. 2,
  2822. ILK_FIFO_LINE_SIZE
  2823. };
  2824. static const struct intel_watermark_params sandybridge_display_wm_info = {
  2825. SNB_DISPLAY_FIFO,
  2826. SNB_DISPLAY_MAXWM,
  2827. SNB_DISPLAY_DFTWM,
  2828. 2,
  2829. SNB_FIFO_LINE_SIZE
  2830. };
  2831. static const struct intel_watermark_params sandybridge_cursor_wm_info = {
  2832. SNB_CURSOR_FIFO,
  2833. SNB_CURSOR_MAXWM,
  2834. SNB_CURSOR_DFTWM,
  2835. 2,
  2836. SNB_FIFO_LINE_SIZE
  2837. };
  2838. static const struct intel_watermark_params sandybridge_display_srwm_info = {
  2839. SNB_DISPLAY_SR_FIFO,
  2840. SNB_DISPLAY_MAX_SRWM,
  2841. SNB_DISPLAY_DFT_SRWM,
  2842. 2,
  2843. SNB_FIFO_LINE_SIZE
  2844. };
  2845. static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
  2846. SNB_CURSOR_SR_FIFO,
  2847. SNB_CURSOR_MAX_SRWM,
  2848. SNB_CURSOR_DFT_SRWM,
  2849. 2,
  2850. SNB_FIFO_LINE_SIZE
  2851. };
  2852. /**
  2853. * intel_calculate_wm - calculate watermark level
  2854. * @clock_in_khz: pixel clock
  2855. * @wm: chip FIFO params
  2856. * @pixel_size: display pixel size
  2857. * @latency_ns: memory latency for the platform
  2858. *
  2859. * Calculate the watermark level (the level at which the display plane will
  2860. * start fetching from memory again). Each chip has a different display
  2861. * FIFO size and allocation, so the caller needs to figure that out and pass
  2862. * in the correct intel_watermark_params structure.
  2863. *
  2864. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  2865. * on the pixel size. When it reaches the watermark level, it'll start
  2866. * fetching FIFO line sized based chunks from memory until the FIFO fills
  2867. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  2868. * will occur, and a display engine hang could result.
  2869. */
  2870. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  2871. const struct intel_watermark_params *wm,
  2872. int fifo_size,
  2873. int pixel_size,
  2874. unsigned long latency_ns)
  2875. {
  2876. long entries_required, wm_size;
  2877. /*
  2878. * Note: we need to make sure we don't overflow for various clock &
  2879. * latency values.
  2880. * clocks go from a few thousand to several hundred thousand.
  2881. * latency is usually a few thousand
  2882. */
  2883. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  2884. 1000;
  2885. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  2886. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
  2887. wm_size = fifo_size - (entries_required + wm->guard_size);
  2888. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  2889. /* Don't promote wm_size to unsigned... */
  2890. if (wm_size > (long)wm->max_wm)
  2891. wm_size = wm->max_wm;
  2892. if (wm_size <= 0)
  2893. wm_size = wm->default_wm;
  2894. return wm_size;
  2895. }
  2896. struct cxsr_latency {
  2897. int is_desktop;
  2898. int is_ddr3;
  2899. unsigned long fsb_freq;
  2900. unsigned long mem_freq;
  2901. unsigned long display_sr;
  2902. unsigned long display_hpll_disable;
  2903. unsigned long cursor_sr;
  2904. unsigned long cursor_hpll_disable;
  2905. };
  2906. static const struct cxsr_latency cxsr_latency_table[] = {
  2907. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  2908. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  2909. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  2910. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  2911. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  2912. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  2913. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  2914. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  2915. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  2916. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  2917. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  2918. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  2919. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  2920. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  2921. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  2922. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  2923. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  2924. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  2925. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  2926. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  2927. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  2928. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  2929. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  2930. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  2931. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  2932. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  2933. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  2934. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  2935. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  2936. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  2937. };
  2938. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  2939. int is_ddr3,
  2940. int fsb,
  2941. int mem)
  2942. {
  2943. const struct cxsr_latency *latency;
  2944. int i;
  2945. if (fsb == 0 || mem == 0)
  2946. return NULL;
  2947. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  2948. latency = &cxsr_latency_table[i];
  2949. if (is_desktop == latency->is_desktop &&
  2950. is_ddr3 == latency->is_ddr3 &&
  2951. fsb == latency->fsb_freq && mem == latency->mem_freq)
  2952. return latency;
  2953. }
  2954. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2955. return NULL;
  2956. }
  2957. static void pineview_disable_cxsr(struct drm_device *dev)
  2958. {
  2959. struct drm_i915_private *dev_priv = dev->dev_private;
  2960. /* deactivate cxsr */
  2961. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  2962. }
  2963. /*
  2964. * Latency for FIFO fetches is dependent on several factors:
  2965. * - memory configuration (speed, channels)
  2966. * - chipset
  2967. * - current MCH state
  2968. * It can be fairly high in some situations, so here we assume a fairly
  2969. * pessimal value. It's a tradeoff between extra memory fetches (if we
  2970. * set this value too high, the FIFO will fetch frequently to stay full)
  2971. * and power consumption (set it too low to save power and we might see
  2972. * FIFO underruns and display "flicker").
  2973. *
  2974. * A value of 5us seems to be a good balance; safe for very low end
  2975. * platforms but not overly aggressive on lower latency configs.
  2976. */
  2977. static const int latency_ns = 5000;
  2978. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  2979. {
  2980. struct drm_i915_private *dev_priv = dev->dev_private;
  2981. uint32_t dsparb = I915_READ(DSPARB);
  2982. int size;
  2983. size = dsparb & 0x7f;
  2984. if (plane)
  2985. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  2986. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2987. plane ? "B" : "A", size);
  2988. return size;
  2989. }
  2990. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  2991. {
  2992. struct drm_i915_private *dev_priv = dev->dev_private;
  2993. uint32_t dsparb = I915_READ(DSPARB);
  2994. int size;
  2995. size = dsparb & 0x1ff;
  2996. if (plane)
  2997. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  2998. size >>= 1; /* Convert to cachelines */
  2999. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3000. plane ? "B" : "A", size);
  3001. return size;
  3002. }
  3003. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  3004. {
  3005. struct drm_i915_private *dev_priv = dev->dev_private;
  3006. uint32_t dsparb = I915_READ(DSPARB);
  3007. int size;
  3008. size = dsparb & 0x7f;
  3009. size >>= 2; /* Convert to cachelines */
  3010. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3011. plane ? "B" : "A",
  3012. size);
  3013. return size;
  3014. }
  3015. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  3016. {
  3017. struct drm_i915_private *dev_priv = dev->dev_private;
  3018. uint32_t dsparb = I915_READ(DSPARB);
  3019. int size;
  3020. size = dsparb & 0x7f;
  3021. size >>= 1; /* Convert to cachelines */
  3022. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3023. plane ? "B" : "A", size);
  3024. return size;
  3025. }
  3026. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  3027. {
  3028. struct drm_crtc *crtc, *enabled = NULL;
  3029. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3030. if (crtc->enabled && crtc->fb) {
  3031. if (enabled)
  3032. return NULL;
  3033. enabled = crtc;
  3034. }
  3035. }
  3036. return enabled;
  3037. }
  3038. static void pineview_update_wm(struct drm_device *dev)
  3039. {
  3040. struct drm_i915_private *dev_priv = dev->dev_private;
  3041. struct drm_crtc *crtc;
  3042. const struct cxsr_latency *latency;
  3043. u32 reg;
  3044. unsigned long wm;
  3045. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  3046. dev_priv->fsb_freq, dev_priv->mem_freq);
  3047. if (!latency) {
  3048. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3049. pineview_disable_cxsr(dev);
  3050. return;
  3051. }
  3052. crtc = single_enabled_crtc(dev);
  3053. if (crtc) {
  3054. int clock = crtc->mode.clock;
  3055. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3056. /* Display SR */
  3057. wm = intel_calculate_wm(clock, &pineview_display_wm,
  3058. pineview_display_wm.fifo_size,
  3059. pixel_size, latency->display_sr);
  3060. reg = I915_READ(DSPFW1);
  3061. reg &= ~DSPFW_SR_MASK;
  3062. reg |= wm << DSPFW_SR_SHIFT;
  3063. I915_WRITE(DSPFW1, reg);
  3064. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  3065. /* cursor SR */
  3066. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  3067. pineview_display_wm.fifo_size,
  3068. pixel_size, latency->cursor_sr);
  3069. reg = I915_READ(DSPFW3);
  3070. reg &= ~DSPFW_CURSOR_SR_MASK;
  3071. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  3072. I915_WRITE(DSPFW3, reg);
  3073. /* Display HPLL off SR */
  3074. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  3075. pineview_display_hplloff_wm.fifo_size,
  3076. pixel_size, latency->display_hpll_disable);
  3077. reg = I915_READ(DSPFW3);
  3078. reg &= ~DSPFW_HPLL_SR_MASK;
  3079. reg |= wm & DSPFW_HPLL_SR_MASK;
  3080. I915_WRITE(DSPFW3, reg);
  3081. /* cursor HPLL off SR */
  3082. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  3083. pineview_display_hplloff_wm.fifo_size,
  3084. pixel_size, latency->cursor_hpll_disable);
  3085. reg = I915_READ(DSPFW3);
  3086. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  3087. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  3088. I915_WRITE(DSPFW3, reg);
  3089. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  3090. /* activate cxsr */
  3091. I915_WRITE(DSPFW3,
  3092. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  3093. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  3094. } else {
  3095. pineview_disable_cxsr(dev);
  3096. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  3097. }
  3098. }
  3099. static bool g4x_compute_wm0(struct drm_device *dev,
  3100. int plane,
  3101. const struct intel_watermark_params *display,
  3102. int display_latency_ns,
  3103. const struct intel_watermark_params *cursor,
  3104. int cursor_latency_ns,
  3105. int *plane_wm,
  3106. int *cursor_wm)
  3107. {
  3108. struct drm_crtc *crtc;
  3109. int htotal, hdisplay, clock, pixel_size;
  3110. int line_time_us, line_count;
  3111. int entries, tlb_miss;
  3112. crtc = intel_get_crtc_for_plane(dev, plane);
  3113. if (crtc->fb == NULL || !crtc->enabled) {
  3114. *cursor_wm = cursor->guard_size;
  3115. *plane_wm = display->guard_size;
  3116. return false;
  3117. }
  3118. htotal = crtc->mode.htotal;
  3119. hdisplay = crtc->mode.hdisplay;
  3120. clock = crtc->mode.clock;
  3121. pixel_size = crtc->fb->bits_per_pixel / 8;
  3122. /* Use the small buffer method to calculate plane watermark */
  3123. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  3124. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  3125. if (tlb_miss > 0)
  3126. entries += tlb_miss;
  3127. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  3128. *plane_wm = entries + display->guard_size;
  3129. if (*plane_wm > (int)display->max_wm)
  3130. *plane_wm = display->max_wm;
  3131. /* Use the large buffer method to calculate cursor watermark */
  3132. line_time_us = ((htotal * 1000) / clock);
  3133. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  3134. entries = line_count * 64 * pixel_size;
  3135. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  3136. if (tlb_miss > 0)
  3137. entries += tlb_miss;
  3138. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3139. *cursor_wm = entries + cursor->guard_size;
  3140. if (*cursor_wm > (int)cursor->max_wm)
  3141. *cursor_wm = (int)cursor->max_wm;
  3142. return true;
  3143. }
  3144. /*
  3145. * Check the wm result.
  3146. *
  3147. * If any calculated watermark values is larger than the maximum value that
  3148. * can be programmed into the associated watermark register, that watermark
  3149. * must be disabled.
  3150. */
  3151. static bool g4x_check_srwm(struct drm_device *dev,
  3152. int display_wm, int cursor_wm,
  3153. const struct intel_watermark_params *display,
  3154. const struct intel_watermark_params *cursor)
  3155. {
  3156. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  3157. display_wm, cursor_wm);
  3158. if (display_wm > display->max_wm) {
  3159. DRM_DEBUG_KMS("display watermark is too large(%d), disabling\n",
  3160. display_wm, display->max_wm);
  3161. return false;
  3162. }
  3163. if (cursor_wm > cursor->max_wm) {
  3164. DRM_DEBUG_KMS("cursor watermark is too large(%d), disabling\n",
  3165. cursor_wm, cursor->max_wm);
  3166. return false;
  3167. }
  3168. if (!(display_wm || cursor_wm)) {
  3169. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  3170. return false;
  3171. }
  3172. return true;
  3173. }
  3174. static bool g4x_compute_srwm(struct drm_device *dev,
  3175. int plane,
  3176. int latency_ns,
  3177. const struct intel_watermark_params *display,
  3178. const struct intel_watermark_params *cursor,
  3179. int *display_wm, int *cursor_wm)
  3180. {
  3181. struct drm_crtc *crtc;
  3182. int hdisplay, htotal, pixel_size, clock;
  3183. unsigned long line_time_us;
  3184. int line_count, line_size;
  3185. int small, large;
  3186. int entries;
  3187. if (!latency_ns) {
  3188. *display_wm = *cursor_wm = 0;
  3189. return false;
  3190. }
  3191. crtc = intel_get_crtc_for_plane(dev, plane);
  3192. hdisplay = crtc->mode.hdisplay;
  3193. htotal = crtc->mode.htotal;
  3194. clock = crtc->mode.clock;
  3195. pixel_size = crtc->fb->bits_per_pixel / 8;
  3196. line_time_us = (htotal * 1000) / clock;
  3197. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3198. line_size = hdisplay * pixel_size;
  3199. /* Use the minimum of the small and large buffer method for primary */
  3200. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3201. large = line_count * line_size;
  3202. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3203. *display_wm = entries + display->guard_size;
  3204. /* calculate the self-refresh watermark for display cursor */
  3205. entries = line_count * pixel_size * 64;
  3206. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3207. *cursor_wm = entries + cursor->guard_size;
  3208. return g4x_check_srwm(dev,
  3209. *display_wm, *cursor_wm,
  3210. display, cursor);
  3211. }
  3212. #define single_plane_enabled(mask) is_power_of_2(mask)
  3213. static void g4x_update_wm(struct drm_device *dev)
  3214. {
  3215. static const int sr_latency_ns = 12000;
  3216. struct drm_i915_private *dev_priv = dev->dev_private;
  3217. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  3218. int plane_sr, cursor_sr;
  3219. unsigned int enabled = 0;
  3220. if (g4x_compute_wm0(dev, 0,
  3221. &g4x_wm_info, latency_ns,
  3222. &g4x_cursor_wm_info, latency_ns,
  3223. &planea_wm, &cursora_wm))
  3224. enabled |= 1;
  3225. if (g4x_compute_wm0(dev, 1,
  3226. &g4x_wm_info, latency_ns,
  3227. &g4x_cursor_wm_info, latency_ns,
  3228. &planeb_wm, &cursorb_wm))
  3229. enabled |= 2;
  3230. plane_sr = cursor_sr = 0;
  3231. if (single_plane_enabled(enabled) &&
  3232. g4x_compute_srwm(dev, ffs(enabled) - 1,
  3233. sr_latency_ns,
  3234. &g4x_wm_info,
  3235. &g4x_cursor_wm_info,
  3236. &plane_sr, &cursor_sr))
  3237. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3238. else
  3239. I915_WRITE(FW_BLC_SELF,
  3240. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  3241. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  3242. planea_wm, cursora_wm,
  3243. planeb_wm, cursorb_wm,
  3244. plane_sr, cursor_sr);
  3245. I915_WRITE(DSPFW1,
  3246. (plane_sr << DSPFW_SR_SHIFT) |
  3247. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  3248. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  3249. planea_wm);
  3250. I915_WRITE(DSPFW2,
  3251. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  3252. (cursora_wm << DSPFW_CURSORA_SHIFT));
  3253. /* HPLL off in SR has some issues on G4x... disable it */
  3254. I915_WRITE(DSPFW3,
  3255. (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  3256. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3257. }
  3258. static void i965_update_wm(struct drm_device *dev)
  3259. {
  3260. struct drm_i915_private *dev_priv = dev->dev_private;
  3261. struct drm_crtc *crtc;
  3262. int srwm = 1;
  3263. int cursor_sr = 16;
  3264. /* Calc sr entries for one plane configs */
  3265. crtc = single_enabled_crtc(dev);
  3266. if (crtc) {
  3267. /* self-refresh has much higher latency */
  3268. static const int sr_latency_ns = 12000;
  3269. int clock = crtc->mode.clock;
  3270. int htotal = crtc->mode.htotal;
  3271. int hdisplay = crtc->mode.hdisplay;
  3272. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3273. unsigned long line_time_us;
  3274. int entries;
  3275. line_time_us = ((htotal * 1000) / clock);
  3276. /* Use ns/us then divide to preserve precision */
  3277. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3278. pixel_size * hdisplay;
  3279. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  3280. srwm = I965_FIFO_SIZE - entries;
  3281. if (srwm < 0)
  3282. srwm = 1;
  3283. srwm &= 0x1ff;
  3284. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  3285. entries, srwm);
  3286. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3287. pixel_size * 64;
  3288. entries = DIV_ROUND_UP(entries,
  3289. i965_cursor_wm_info.cacheline_size);
  3290. cursor_sr = i965_cursor_wm_info.fifo_size -
  3291. (entries + i965_cursor_wm_info.guard_size);
  3292. if (cursor_sr > i965_cursor_wm_info.max_wm)
  3293. cursor_sr = i965_cursor_wm_info.max_wm;
  3294. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  3295. "cursor %d\n", srwm, cursor_sr);
  3296. if (IS_CRESTLINE(dev))
  3297. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3298. } else {
  3299. /* Turn off self refresh if both pipes are enabled */
  3300. if (IS_CRESTLINE(dev))
  3301. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  3302. & ~FW_BLC_SELF_EN);
  3303. }
  3304. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  3305. srwm);
  3306. /* 965 has limitations... */
  3307. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  3308. (8 << 16) | (8 << 8) | (8 << 0));
  3309. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  3310. /* update cursor SR watermark */
  3311. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3312. }
  3313. static void i9xx_update_wm(struct drm_device *dev)
  3314. {
  3315. struct drm_i915_private *dev_priv = dev->dev_private;
  3316. const struct intel_watermark_params *wm_info;
  3317. uint32_t fwater_lo;
  3318. uint32_t fwater_hi;
  3319. int cwm, srwm = 1;
  3320. int fifo_size;
  3321. int planea_wm, planeb_wm;
  3322. struct drm_crtc *crtc, *enabled = NULL;
  3323. if (IS_I945GM(dev))
  3324. wm_info = &i945_wm_info;
  3325. else if (!IS_GEN2(dev))
  3326. wm_info = &i915_wm_info;
  3327. else
  3328. wm_info = &i855_wm_info;
  3329. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  3330. crtc = intel_get_crtc_for_plane(dev, 0);
  3331. if (crtc->enabled && crtc->fb) {
  3332. planea_wm = intel_calculate_wm(crtc->mode.clock,
  3333. wm_info, fifo_size,
  3334. crtc->fb->bits_per_pixel / 8,
  3335. latency_ns);
  3336. enabled = crtc;
  3337. } else
  3338. planea_wm = fifo_size - wm_info->guard_size;
  3339. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  3340. crtc = intel_get_crtc_for_plane(dev, 1);
  3341. if (crtc->enabled && crtc->fb) {
  3342. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  3343. wm_info, fifo_size,
  3344. crtc->fb->bits_per_pixel / 8,
  3345. latency_ns);
  3346. if (enabled == NULL)
  3347. enabled = crtc;
  3348. else
  3349. enabled = NULL;
  3350. } else
  3351. planeb_wm = fifo_size - wm_info->guard_size;
  3352. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  3353. /*
  3354. * Overlay gets an aggressive default since video jitter is bad.
  3355. */
  3356. cwm = 2;
  3357. /* Play safe and disable self-refresh before adjusting watermarks. */
  3358. if (IS_I945G(dev) || IS_I945GM(dev))
  3359. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  3360. else if (IS_I915GM(dev))
  3361. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  3362. /* Calc sr entries for one plane configs */
  3363. if (HAS_FW_BLC(dev) && enabled) {
  3364. /* self-refresh has much higher latency */
  3365. static const int sr_latency_ns = 6000;
  3366. int clock = enabled->mode.clock;
  3367. int htotal = enabled->mode.htotal;
  3368. int hdisplay = enabled->mode.hdisplay;
  3369. int pixel_size = enabled->fb->bits_per_pixel / 8;
  3370. unsigned long line_time_us;
  3371. int entries;
  3372. line_time_us = (htotal * 1000) / clock;
  3373. /* Use ns/us then divide to preserve precision */
  3374. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3375. pixel_size * hdisplay;
  3376. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  3377. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  3378. srwm = wm_info->fifo_size - entries;
  3379. if (srwm < 0)
  3380. srwm = 1;
  3381. if (IS_I945G(dev) || IS_I945GM(dev))
  3382. I915_WRITE(FW_BLC_SELF,
  3383. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  3384. else if (IS_I915GM(dev))
  3385. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  3386. }
  3387. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  3388. planea_wm, planeb_wm, cwm, srwm);
  3389. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  3390. fwater_hi = (cwm & 0x1f);
  3391. /* Set request length to 8 cachelines per fetch */
  3392. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  3393. fwater_hi = fwater_hi | (1 << 8);
  3394. I915_WRITE(FW_BLC, fwater_lo);
  3395. I915_WRITE(FW_BLC2, fwater_hi);
  3396. if (HAS_FW_BLC(dev)) {
  3397. if (enabled) {
  3398. if (IS_I945G(dev) || IS_I945GM(dev))
  3399. I915_WRITE(FW_BLC_SELF,
  3400. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  3401. else if (IS_I915GM(dev))
  3402. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  3403. DRM_DEBUG_KMS("memory self refresh enabled\n");
  3404. } else
  3405. DRM_DEBUG_KMS("memory self refresh disabled\n");
  3406. }
  3407. }
  3408. static void i830_update_wm(struct drm_device *dev)
  3409. {
  3410. struct drm_i915_private *dev_priv = dev->dev_private;
  3411. struct drm_crtc *crtc;
  3412. uint32_t fwater_lo;
  3413. int planea_wm;
  3414. crtc = single_enabled_crtc(dev);
  3415. if (crtc == NULL)
  3416. return;
  3417. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  3418. dev_priv->display.get_fifo_size(dev, 0),
  3419. crtc->fb->bits_per_pixel / 8,
  3420. latency_ns);
  3421. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  3422. fwater_lo |= (3<<8) | planea_wm;
  3423. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  3424. I915_WRITE(FW_BLC, fwater_lo);
  3425. }
  3426. #define ILK_LP0_PLANE_LATENCY 700
  3427. #define ILK_LP0_CURSOR_LATENCY 1300
  3428. static bool ironlake_compute_wm0(struct drm_device *dev,
  3429. int pipe,
  3430. const struct intel_watermark_params *display,
  3431. int display_latency_ns,
  3432. const struct intel_watermark_params *cursor,
  3433. int cursor_latency_ns,
  3434. int *plane_wm,
  3435. int *cursor_wm)
  3436. {
  3437. struct drm_crtc *crtc;
  3438. int htotal, hdisplay, clock, pixel_size;
  3439. int line_time_us, line_count;
  3440. int entries, tlb_miss;
  3441. crtc = intel_get_crtc_for_pipe(dev, pipe);
  3442. if (crtc->fb == NULL || !crtc->enabled)
  3443. return false;
  3444. htotal = crtc->mode.htotal;
  3445. hdisplay = crtc->mode.hdisplay;
  3446. clock = crtc->mode.clock;
  3447. pixel_size = crtc->fb->bits_per_pixel / 8;
  3448. /* Use the small buffer method to calculate plane watermark */
  3449. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  3450. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  3451. if (tlb_miss > 0)
  3452. entries += tlb_miss;
  3453. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  3454. *plane_wm = entries + display->guard_size;
  3455. if (*plane_wm > (int)display->max_wm)
  3456. *plane_wm = display->max_wm;
  3457. /* Use the large buffer method to calculate cursor watermark */
  3458. line_time_us = ((htotal * 1000) / clock);
  3459. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  3460. entries = line_count * 64 * pixel_size;
  3461. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  3462. if (tlb_miss > 0)
  3463. entries += tlb_miss;
  3464. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3465. *cursor_wm = entries + cursor->guard_size;
  3466. if (*cursor_wm > (int)cursor->max_wm)
  3467. *cursor_wm = (int)cursor->max_wm;
  3468. return true;
  3469. }
  3470. /*
  3471. * Check the wm result.
  3472. *
  3473. * If any calculated watermark values is larger than the maximum value that
  3474. * can be programmed into the associated watermark register, that watermark
  3475. * must be disabled.
  3476. */
  3477. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  3478. int fbc_wm, int display_wm, int cursor_wm,
  3479. const struct intel_watermark_params *display,
  3480. const struct intel_watermark_params *cursor)
  3481. {
  3482. struct drm_i915_private *dev_priv = dev->dev_private;
  3483. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  3484. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  3485. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  3486. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  3487. fbc_wm, SNB_FBC_MAX_SRWM, level);
  3488. /* fbc has it's own way to disable FBC WM */
  3489. I915_WRITE(DISP_ARB_CTL,
  3490. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  3491. return false;
  3492. }
  3493. if (display_wm > display->max_wm) {
  3494. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  3495. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  3496. return false;
  3497. }
  3498. if (cursor_wm > cursor->max_wm) {
  3499. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  3500. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  3501. return false;
  3502. }
  3503. if (!(fbc_wm || display_wm || cursor_wm)) {
  3504. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  3505. return false;
  3506. }
  3507. return true;
  3508. }
  3509. /*
  3510. * Compute watermark values of WM[1-3],
  3511. */
  3512. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  3513. int latency_ns,
  3514. const struct intel_watermark_params *display,
  3515. const struct intel_watermark_params *cursor,
  3516. int *fbc_wm, int *display_wm, int *cursor_wm)
  3517. {
  3518. struct drm_crtc *crtc;
  3519. unsigned long line_time_us;
  3520. int hdisplay, htotal, pixel_size, clock;
  3521. int line_count, line_size;
  3522. int small, large;
  3523. int entries;
  3524. if (!latency_ns) {
  3525. *fbc_wm = *display_wm = *cursor_wm = 0;
  3526. return false;
  3527. }
  3528. crtc = intel_get_crtc_for_plane(dev, plane);
  3529. hdisplay = crtc->mode.hdisplay;
  3530. htotal = crtc->mode.htotal;
  3531. clock = crtc->mode.clock;
  3532. pixel_size = crtc->fb->bits_per_pixel / 8;
  3533. line_time_us = (htotal * 1000) / clock;
  3534. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3535. line_size = hdisplay * pixel_size;
  3536. /* Use the minimum of the small and large buffer method for primary */
  3537. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3538. large = line_count * line_size;
  3539. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3540. *display_wm = entries + display->guard_size;
  3541. /*
  3542. * Spec says:
  3543. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  3544. */
  3545. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  3546. /* calculate the self-refresh watermark for display cursor */
  3547. entries = line_count * pixel_size * 64;
  3548. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3549. *cursor_wm = entries + cursor->guard_size;
  3550. return ironlake_check_srwm(dev, level,
  3551. *fbc_wm, *display_wm, *cursor_wm,
  3552. display, cursor);
  3553. }
  3554. static void ironlake_update_wm(struct drm_device *dev)
  3555. {
  3556. struct drm_i915_private *dev_priv = dev->dev_private;
  3557. int fbc_wm, plane_wm, cursor_wm;
  3558. unsigned int enabled;
  3559. enabled = 0;
  3560. if (ironlake_compute_wm0(dev, 0,
  3561. &ironlake_display_wm_info,
  3562. ILK_LP0_PLANE_LATENCY,
  3563. &ironlake_cursor_wm_info,
  3564. ILK_LP0_CURSOR_LATENCY,
  3565. &plane_wm, &cursor_wm)) {
  3566. I915_WRITE(WM0_PIPEA_ILK,
  3567. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3568. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3569. " plane %d, " "cursor: %d\n",
  3570. plane_wm, cursor_wm);
  3571. enabled |= 1;
  3572. }
  3573. if (ironlake_compute_wm0(dev, 1,
  3574. &ironlake_display_wm_info,
  3575. ILK_LP0_PLANE_LATENCY,
  3576. &ironlake_cursor_wm_info,
  3577. ILK_LP0_CURSOR_LATENCY,
  3578. &plane_wm, &cursor_wm)) {
  3579. I915_WRITE(WM0_PIPEB_ILK,
  3580. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3581. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3582. " plane %d, cursor: %d\n",
  3583. plane_wm, cursor_wm);
  3584. enabled |= 2;
  3585. }
  3586. /*
  3587. * Calculate and update the self-refresh watermark only when one
  3588. * display plane is used.
  3589. */
  3590. I915_WRITE(WM3_LP_ILK, 0);
  3591. I915_WRITE(WM2_LP_ILK, 0);
  3592. I915_WRITE(WM1_LP_ILK, 0);
  3593. if (!single_plane_enabled(enabled))
  3594. return;
  3595. enabled = ffs(enabled) - 1;
  3596. /* WM1 */
  3597. if (!ironlake_compute_srwm(dev, 1, enabled,
  3598. ILK_READ_WM1_LATENCY() * 500,
  3599. &ironlake_display_srwm_info,
  3600. &ironlake_cursor_srwm_info,
  3601. &fbc_wm, &plane_wm, &cursor_wm))
  3602. return;
  3603. I915_WRITE(WM1_LP_ILK,
  3604. WM1_LP_SR_EN |
  3605. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3606. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3607. (plane_wm << WM1_LP_SR_SHIFT) |
  3608. cursor_wm);
  3609. /* WM2 */
  3610. if (!ironlake_compute_srwm(dev, 2, enabled,
  3611. ILK_READ_WM2_LATENCY() * 500,
  3612. &ironlake_display_srwm_info,
  3613. &ironlake_cursor_srwm_info,
  3614. &fbc_wm, &plane_wm, &cursor_wm))
  3615. return;
  3616. I915_WRITE(WM2_LP_ILK,
  3617. WM2_LP_EN |
  3618. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3619. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3620. (plane_wm << WM1_LP_SR_SHIFT) |
  3621. cursor_wm);
  3622. /*
  3623. * WM3 is unsupported on ILK, probably because we don't have latency
  3624. * data for that power state
  3625. */
  3626. }
  3627. static void sandybridge_update_wm(struct drm_device *dev)
  3628. {
  3629. struct drm_i915_private *dev_priv = dev->dev_private;
  3630. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  3631. int fbc_wm, plane_wm, cursor_wm;
  3632. unsigned int enabled;
  3633. enabled = 0;
  3634. if (ironlake_compute_wm0(dev, 0,
  3635. &sandybridge_display_wm_info, latency,
  3636. &sandybridge_cursor_wm_info, latency,
  3637. &plane_wm, &cursor_wm)) {
  3638. I915_WRITE(WM0_PIPEA_ILK,
  3639. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3640. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3641. " plane %d, " "cursor: %d\n",
  3642. plane_wm, cursor_wm);
  3643. enabled |= 1;
  3644. }
  3645. if (ironlake_compute_wm0(dev, 1,
  3646. &sandybridge_display_wm_info, latency,
  3647. &sandybridge_cursor_wm_info, latency,
  3648. &plane_wm, &cursor_wm)) {
  3649. I915_WRITE(WM0_PIPEB_ILK,
  3650. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3651. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3652. " plane %d, cursor: %d\n",
  3653. plane_wm, cursor_wm);
  3654. enabled |= 2;
  3655. }
  3656. /*
  3657. * Calculate and update the self-refresh watermark only when one
  3658. * display plane is used.
  3659. *
  3660. * SNB support 3 levels of watermark.
  3661. *
  3662. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  3663. * and disabled in the descending order
  3664. *
  3665. */
  3666. I915_WRITE(WM3_LP_ILK, 0);
  3667. I915_WRITE(WM2_LP_ILK, 0);
  3668. I915_WRITE(WM1_LP_ILK, 0);
  3669. if (!single_plane_enabled(enabled))
  3670. return;
  3671. enabled = ffs(enabled) - 1;
  3672. /* WM1 */
  3673. if (!ironlake_compute_srwm(dev, 1, enabled,
  3674. SNB_READ_WM1_LATENCY() * 500,
  3675. &sandybridge_display_srwm_info,
  3676. &sandybridge_cursor_srwm_info,
  3677. &fbc_wm, &plane_wm, &cursor_wm))
  3678. return;
  3679. I915_WRITE(WM1_LP_ILK,
  3680. WM1_LP_SR_EN |
  3681. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3682. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3683. (plane_wm << WM1_LP_SR_SHIFT) |
  3684. cursor_wm);
  3685. /* WM2 */
  3686. if (!ironlake_compute_srwm(dev, 2, enabled,
  3687. SNB_READ_WM2_LATENCY() * 500,
  3688. &sandybridge_display_srwm_info,
  3689. &sandybridge_cursor_srwm_info,
  3690. &fbc_wm, &plane_wm, &cursor_wm))
  3691. return;
  3692. I915_WRITE(WM2_LP_ILK,
  3693. WM2_LP_EN |
  3694. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3695. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3696. (plane_wm << WM1_LP_SR_SHIFT) |
  3697. cursor_wm);
  3698. /* WM3 */
  3699. if (!ironlake_compute_srwm(dev, 3, enabled,
  3700. SNB_READ_WM3_LATENCY() * 500,
  3701. &sandybridge_display_srwm_info,
  3702. &sandybridge_cursor_srwm_info,
  3703. &fbc_wm, &plane_wm, &cursor_wm))
  3704. return;
  3705. I915_WRITE(WM3_LP_ILK,
  3706. WM3_LP_EN |
  3707. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3708. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3709. (plane_wm << WM1_LP_SR_SHIFT) |
  3710. cursor_wm);
  3711. }
  3712. /**
  3713. * intel_update_watermarks - update FIFO watermark values based on current modes
  3714. *
  3715. * Calculate watermark values for the various WM regs based on current mode
  3716. * and plane configuration.
  3717. *
  3718. * There are several cases to deal with here:
  3719. * - normal (i.e. non-self-refresh)
  3720. * - self-refresh (SR) mode
  3721. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3722. * - lines are small relative to FIFO size (buffer can hold more than 2
  3723. * lines), so need to account for TLB latency
  3724. *
  3725. * The normal calculation is:
  3726. * watermark = dotclock * bytes per pixel * latency
  3727. * where latency is platform & configuration dependent (we assume pessimal
  3728. * values here).
  3729. *
  3730. * The SR calculation is:
  3731. * watermark = (trunc(latency/line time)+1) * surface width *
  3732. * bytes per pixel
  3733. * where
  3734. * line time = htotal / dotclock
  3735. * surface width = hdisplay for normal plane and 64 for cursor
  3736. * and latency is assumed to be high, as above.
  3737. *
  3738. * The final value programmed to the register should always be rounded up,
  3739. * and include an extra 2 entries to account for clock crossings.
  3740. *
  3741. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3742. * to set the non-SR watermarks to 8.
  3743. */
  3744. static void intel_update_watermarks(struct drm_device *dev)
  3745. {
  3746. struct drm_i915_private *dev_priv = dev->dev_private;
  3747. if (dev_priv->display.update_wm)
  3748. dev_priv->display.update_wm(dev);
  3749. }
  3750. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3751. {
  3752. return dev_priv->lvds_use_ssc && i915_panel_use_ssc;
  3753. }
  3754. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  3755. struct drm_display_mode *mode,
  3756. struct drm_display_mode *adjusted_mode,
  3757. int x, int y,
  3758. struct drm_framebuffer *old_fb)
  3759. {
  3760. struct drm_device *dev = crtc->dev;
  3761. struct drm_i915_private *dev_priv = dev->dev_private;
  3762. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3763. int pipe = intel_crtc->pipe;
  3764. int plane = intel_crtc->plane;
  3765. int refclk, num_connectors = 0;
  3766. intel_clock_t clock, reduced_clock;
  3767. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3768. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  3769. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3770. struct drm_mode_config *mode_config = &dev->mode_config;
  3771. struct intel_encoder *encoder;
  3772. const intel_limit_t *limit;
  3773. int ret;
  3774. u32 temp;
  3775. u32 lvds_sync = 0;
  3776. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3777. if (encoder->base.crtc != crtc)
  3778. continue;
  3779. switch (encoder->type) {
  3780. case INTEL_OUTPUT_LVDS:
  3781. is_lvds = true;
  3782. break;
  3783. case INTEL_OUTPUT_SDVO:
  3784. case INTEL_OUTPUT_HDMI:
  3785. is_sdvo = true;
  3786. if (encoder->needs_tv_clock)
  3787. is_tv = true;
  3788. break;
  3789. case INTEL_OUTPUT_DVO:
  3790. is_dvo = true;
  3791. break;
  3792. case INTEL_OUTPUT_TVOUT:
  3793. is_tv = true;
  3794. break;
  3795. case INTEL_OUTPUT_ANALOG:
  3796. is_crt = true;
  3797. break;
  3798. case INTEL_OUTPUT_DISPLAYPORT:
  3799. is_dp = true;
  3800. break;
  3801. }
  3802. num_connectors++;
  3803. }
  3804. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3805. refclk = dev_priv->lvds_ssc_freq * 1000;
  3806. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3807. refclk / 1000);
  3808. } else if (!IS_GEN2(dev)) {
  3809. refclk = 96000;
  3810. } else {
  3811. refclk = 48000;
  3812. }
  3813. /*
  3814. * Returns a set of divisors for the desired target clock with the given
  3815. * refclk, or FALSE. The returned values represent the clock equation:
  3816. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3817. */
  3818. limit = intel_limit(crtc, refclk);
  3819. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  3820. if (!ok) {
  3821. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3822. return -EINVAL;
  3823. }
  3824. /* Ensure that the cursor is valid for the new mode before changing... */
  3825. intel_crtc_update_cursor(crtc, true);
  3826. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3827. has_reduced_clock = limit->find_pll(limit, crtc,
  3828. dev_priv->lvds_downclock,
  3829. refclk,
  3830. &reduced_clock);
  3831. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  3832. /*
  3833. * If the different P is found, it means that we can't
  3834. * switch the display clock by using the FP0/FP1.
  3835. * In such case we will disable the LVDS downclock
  3836. * feature.
  3837. */
  3838. DRM_DEBUG_KMS("Different P is found for "
  3839. "LVDS clock/downclock\n");
  3840. has_reduced_clock = 0;
  3841. }
  3842. }
  3843. /* SDVO TV has fixed PLL values depend on its clock range,
  3844. this mirrors vbios setting. */
  3845. if (is_sdvo && is_tv) {
  3846. if (adjusted_mode->clock >= 100000
  3847. && adjusted_mode->clock < 140500) {
  3848. clock.p1 = 2;
  3849. clock.p2 = 10;
  3850. clock.n = 3;
  3851. clock.m1 = 16;
  3852. clock.m2 = 8;
  3853. } else if (adjusted_mode->clock >= 140500
  3854. && adjusted_mode->clock <= 200000) {
  3855. clock.p1 = 1;
  3856. clock.p2 = 10;
  3857. clock.n = 6;
  3858. clock.m1 = 12;
  3859. clock.m2 = 8;
  3860. }
  3861. }
  3862. if (IS_PINEVIEW(dev)) {
  3863. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  3864. if (has_reduced_clock)
  3865. fp2 = (1 << reduced_clock.n) << 16 |
  3866. reduced_clock.m1 << 8 | reduced_clock.m2;
  3867. } else {
  3868. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  3869. if (has_reduced_clock)
  3870. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  3871. reduced_clock.m2;
  3872. }
  3873. dpll = DPLL_VGA_MODE_DIS;
  3874. if (!IS_GEN2(dev)) {
  3875. if (is_lvds)
  3876. dpll |= DPLLB_MODE_LVDS;
  3877. else
  3878. dpll |= DPLLB_MODE_DAC_SERIAL;
  3879. if (is_sdvo) {
  3880. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3881. if (pixel_multiplier > 1) {
  3882. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3883. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3884. }
  3885. dpll |= DPLL_DVO_HIGH_SPEED;
  3886. }
  3887. if (is_dp)
  3888. dpll |= DPLL_DVO_HIGH_SPEED;
  3889. /* compute bitmask from p1 value */
  3890. if (IS_PINEVIEW(dev))
  3891. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3892. else {
  3893. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3894. if (IS_G4X(dev) && has_reduced_clock)
  3895. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3896. }
  3897. switch (clock.p2) {
  3898. case 5:
  3899. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3900. break;
  3901. case 7:
  3902. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3903. break;
  3904. case 10:
  3905. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3906. break;
  3907. case 14:
  3908. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3909. break;
  3910. }
  3911. if (INTEL_INFO(dev)->gen >= 4)
  3912. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3913. } else {
  3914. if (is_lvds) {
  3915. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3916. } else {
  3917. if (clock.p1 == 2)
  3918. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3919. else
  3920. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3921. if (clock.p2 == 4)
  3922. dpll |= PLL_P2_DIVIDE_BY_4;
  3923. }
  3924. }
  3925. if (is_sdvo && is_tv)
  3926. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3927. else if (is_tv)
  3928. /* XXX: just matching BIOS for now */
  3929. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3930. dpll |= 3;
  3931. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3932. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3933. else
  3934. dpll |= PLL_REF_INPUT_DREFCLK;
  3935. /* setup pipeconf */
  3936. pipeconf = I915_READ(PIPECONF(pipe));
  3937. /* Set up the display plane register */
  3938. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3939. /* Ironlake's plane is forced to pipe, bit 24 is to
  3940. enable color space conversion */
  3941. if (pipe == 0)
  3942. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3943. else
  3944. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3945. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  3946. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3947. * core speed.
  3948. *
  3949. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3950. * pipe == 0 check?
  3951. */
  3952. if (mode->clock >
  3953. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3954. pipeconf |= PIPECONF_DOUBLE_WIDE;
  3955. else
  3956. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  3957. }
  3958. dpll |= DPLL_VCO_ENABLE;
  3959. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3960. drm_mode_debug_printmodeline(mode);
  3961. I915_WRITE(FP0(pipe), fp);
  3962. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3963. POSTING_READ(DPLL(pipe));
  3964. udelay(150);
  3965. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3966. * This is an exception to the general rule that mode_set doesn't turn
  3967. * things on.
  3968. */
  3969. if (is_lvds) {
  3970. temp = I915_READ(LVDS);
  3971. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3972. if (pipe == 1) {
  3973. temp |= LVDS_PIPEB_SELECT;
  3974. } else {
  3975. temp &= ~LVDS_PIPEB_SELECT;
  3976. }
  3977. /* set the corresponsding LVDS_BORDER bit */
  3978. temp |= dev_priv->lvds_border_bits;
  3979. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3980. * set the DPLLs for dual-channel mode or not.
  3981. */
  3982. if (clock.p2 == 7)
  3983. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3984. else
  3985. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3986. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3987. * appropriately here, but we need to look more thoroughly into how
  3988. * panels behave in the two modes.
  3989. */
  3990. /* set the dithering flag on LVDS as needed */
  3991. if (INTEL_INFO(dev)->gen >= 4) {
  3992. if (dev_priv->lvds_dither)
  3993. temp |= LVDS_ENABLE_DITHER;
  3994. else
  3995. temp &= ~LVDS_ENABLE_DITHER;
  3996. }
  3997. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  3998. lvds_sync |= LVDS_HSYNC_POLARITY;
  3999. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4000. lvds_sync |= LVDS_VSYNC_POLARITY;
  4001. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  4002. != lvds_sync) {
  4003. char flags[2] = "-+";
  4004. DRM_INFO("Changing LVDS panel from "
  4005. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  4006. flags[!(temp & LVDS_HSYNC_POLARITY)],
  4007. flags[!(temp & LVDS_VSYNC_POLARITY)],
  4008. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  4009. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  4010. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4011. temp |= lvds_sync;
  4012. }
  4013. I915_WRITE(LVDS, temp);
  4014. }
  4015. if (is_dp) {
  4016. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4017. }
  4018. I915_WRITE(DPLL(pipe), dpll);
  4019. /* Wait for the clocks to stabilize. */
  4020. POSTING_READ(DPLL(pipe));
  4021. udelay(150);
  4022. if (INTEL_INFO(dev)->gen >= 4) {
  4023. temp = 0;
  4024. if (is_sdvo) {
  4025. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  4026. if (temp > 1)
  4027. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4028. else
  4029. temp = 0;
  4030. }
  4031. I915_WRITE(DPLL_MD(pipe), temp);
  4032. } else {
  4033. /* The pixel multiplier can only be updated once the
  4034. * DPLL is enabled and the clocks are stable.
  4035. *
  4036. * So write it again.
  4037. */
  4038. I915_WRITE(DPLL(pipe), dpll);
  4039. }
  4040. intel_crtc->lowfreq_avail = false;
  4041. if (is_lvds && has_reduced_clock && i915_powersave) {
  4042. I915_WRITE(FP1(pipe), fp2);
  4043. intel_crtc->lowfreq_avail = true;
  4044. if (HAS_PIPE_CXSR(dev)) {
  4045. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4046. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4047. }
  4048. } else {
  4049. I915_WRITE(FP1(pipe), fp);
  4050. if (HAS_PIPE_CXSR(dev)) {
  4051. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4052. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4053. }
  4054. }
  4055. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4056. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4057. /* the chip adds 2 halflines automatically */
  4058. adjusted_mode->crtc_vdisplay -= 1;
  4059. adjusted_mode->crtc_vtotal -= 1;
  4060. adjusted_mode->crtc_vblank_start -= 1;
  4061. adjusted_mode->crtc_vblank_end -= 1;
  4062. adjusted_mode->crtc_vsync_end -= 1;
  4063. adjusted_mode->crtc_vsync_start -= 1;
  4064. } else
  4065. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  4066. I915_WRITE(HTOTAL(pipe),
  4067. (adjusted_mode->crtc_hdisplay - 1) |
  4068. ((adjusted_mode->crtc_htotal - 1) << 16));
  4069. I915_WRITE(HBLANK(pipe),
  4070. (adjusted_mode->crtc_hblank_start - 1) |
  4071. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4072. I915_WRITE(HSYNC(pipe),
  4073. (adjusted_mode->crtc_hsync_start - 1) |
  4074. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4075. I915_WRITE(VTOTAL(pipe),
  4076. (adjusted_mode->crtc_vdisplay - 1) |
  4077. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4078. I915_WRITE(VBLANK(pipe),
  4079. (adjusted_mode->crtc_vblank_start - 1) |
  4080. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4081. I915_WRITE(VSYNC(pipe),
  4082. (adjusted_mode->crtc_vsync_start - 1) |
  4083. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4084. /* pipesrc and dspsize control the size that is scaled from,
  4085. * which should always be the user's requested size.
  4086. */
  4087. I915_WRITE(DSPSIZE(plane),
  4088. ((mode->vdisplay - 1) << 16) |
  4089. (mode->hdisplay - 1));
  4090. I915_WRITE(DSPPOS(plane), 0);
  4091. I915_WRITE(PIPESRC(pipe),
  4092. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4093. I915_WRITE(PIPECONF(pipe), pipeconf);
  4094. POSTING_READ(PIPECONF(pipe));
  4095. intel_enable_pipe(dev_priv, pipe, false);
  4096. intel_wait_for_vblank(dev, pipe);
  4097. I915_WRITE(DSPCNTR(plane), dspcntr);
  4098. POSTING_READ(DSPCNTR(plane));
  4099. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4100. intel_update_watermarks(dev);
  4101. return ret;
  4102. }
  4103. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  4104. struct drm_display_mode *mode,
  4105. struct drm_display_mode *adjusted_mode,
  4106. int x, int y,
  4107. struct drm_framebuffer *old_fb)
  4108. {
  4109. struct drm_device *dev = crtc->dev;
  4110. struct drm_i915_private *dev_priv = dev->dev_private;
  4111. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4112. int pipe = intel_crtc->pipe;
  4113. int plane = intel_crtc->plane;
  4114. int refclk, num_connectors = 0;
  4115. intel_clock_t clock, reduced_clock;
  4116. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  4117. bool ok, has_reduced_clock = false, is_sdvo = false;
  4118. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  4119. struct intel_encoder *has_edp_encoder = NULL;
  4120. struct drm_mode_config *mode_config = &dev->mode_config;
  4121. struct intel_encoder *encoder;
  4122. const intel_limit_t *limit;
  4123. int ret;
  4124. struct fdi_m_n m_n = {0};
  4125. u32 temp;
  4126. u32 lvds_sync = 0;
  4127. int target_clock, pixel_multiplier, lane, link_bw, bpp, factor;
  4128. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4129. if (encoder->base.crtc != crtc)
  4130. continue;
  4131. switch (encoder->type) {
  4132. case INTEL_OUTPUT_LVDS:
  4133. is_lvds = true;
  4134. break;
  4135. case INTEL_OUTPUT_SDVO:
  4136. case INTEL_OUTPUT_HDMI:
  4137. is_sdvo = true;
  4138. if (encoder->needs_tv_clock)
  4139. is_tv = true;
  4140. break;
  4141. case INTEL_OUTPUT_TVOUT:
  4142. is_tv = true;
  4143. break;
  4144. case INTEL_OUTPUT_ANALOG:
  4145. is_crt = true;
  4146. break;
  4147. case INTEL_OUTPUT_DISPLAYPORT:
  4148. is_dp = true;
  4149. break;
  4150. case INTEL_OUTPUT_EDP:
  4151. has_edp_encoder = encoder;
  4152. break;
  4153. }
  4154. num_connectors++;
  4155. }
  4156. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4157. refclk = dev_priv->lvds_ssc_freq * 1000;
  4158. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4159. refclk / 1000);
  4160. } else {
  4161. refclk = 96000;
  4162. if (!has_edp_encoder ||
  4163. intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4164. refclk = 120000; /* 120Mhz refclk */
  4165. }
  4166. /*
  4167. * Returns a set of divisors for the desired target clock with the given
  4168. * refclk, or FALSE. The returned values represent the clock equation:
  4169. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4170. */
  4171. limit = intel_limit(crtc, refclk);
  4172. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  4173. if (!ok) {
  4174. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4175. return -EINVAL;
  4176. }
  4177. /* Ensure that the cursor is valid for the new mode before changing... */
  4178. intel_crtc_update_cursor(crtc, true);
  4179. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4180. has_reduced_clock = limit->find_pll(limit, crtc,
  4181. dev_priv->lvds_downclock,
  4182. refclk,
  4183. &reduced_clock);
  4184. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  4185. /*
  4186. * If the different P is found, it means that we can't
  4187. * switch the display clock by using the FP0/FP1.
  4188. * In such case we will disable the LVDS downclock
  4189. * feature.
  4190. */
  4191. DRM_DEBUG_KMS("Different P is found for "
  4192. "LVDS clock/downclock\n");
  4193. has_reduced_clock = 0;
  4194. }
  4195. }
  4196. /* SDVO TV has fixed PLL values depend on its clock range,
  4197. this mirrors vbios setting. */
  4198. if (is_sdvo && is_tv) {
  4199. if (adjusted_mode->clock >= 100000
  4200. && adjusted_mode->clock < 140500) {
  4201. clock.p1 = 2;
  4202. clock.p2 = 10;
  4203. clock.n = 3;
  4204. clock.m1 = 16;
  4205. clock.m2 = 8;
  4206. } else if (adjusted_mode->clock >= 140500
  4207. && adjusted_mode->clock <= 200000) {
  4208. clock.p1 = 1;
  4209. clock.p2 = 10;
  4210. clock.n = 6;
  4211. clock.m1 = 12;
  4212. clock.m2 = 8;
  4213. }
  4214. }
  4215. /* FDI link */
  4216. pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4217. lane = 0;
  4218. /* CPU eDP doesn't require FDI link, so just set DP M/N
  4219. according to current link config */
  4220. if (has_edp_encoder &&
  4221. !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4222. target_clock = mode->clock;
  4223. intel_edp_link_config(has_edp_encoder,
  4224. &lane, &link_bw);
  4225. } else {
  4226. /* [e]DP over FDI requires target mode clock
  4227. instead of link clock */
  4228. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4229. target_clock = mode->clock;
  4230. else
  4231. target_clock = adjusted_mode->clock;
  4232. /* FDI is a binary signal running at ~2.7GHz, encoding
  4233. * each output octet as 10 bits. The actual frequency
  4234. * is stored as a divider into a 100MHz clock, and the
  4235. * mode pixel clock is stored in units of 1KHz.
  4236. * Hence the bw of each lane in terms of the mode signal
  4237. * is:
  4238. */
  4239. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4240. }
  4241. /* determine panel color depth */
  4242. temp = I915_READ(PIPECONF(pipe));
  4243. temp &= ~PIPE_BPC_MASK;
  4244. if (is_lvds) {
  4245. /* the BPC will be 6 if it is 18-bit LVDS panel */
  4246. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
  4247. temp |= PIPE_8BPC;
  4248. else
  4249. temp |= PIPE_6BPC;
  4250. } else if (has_edp_encoder) {
  4251. switch (dev_priv->edp.bpp/3) {
  4252. case 8:
  4253. temp |= PIPE_8BPC;
  4254. break;
  4255. case 10:
  4256. temp |= PIPE_10BPC;
  4257. break;
  4258. case 6:
  4259. temp |= PIPE_6BPC;
  4260. break;
  4261. case 12:
  4262. temp |= PIPE_12BPC;
  4263. break;
  4264. }
  4265. } else
  4266. temp |= PIPE_8BPC;
  4267. I915_WRITE(PIPECONF(pipe), temp);
  4268. switch (temp & PIPE_BPC_MASK) {
  4269. case PIPE_8BPC:
  4270. bpp = 24;
  4271. break;
  4272. case PIPE_10BPC:
  4273. bpp = 30;
  4274. break;
  4275. case PIPE_6BPC:
  4276. bpp = 18;
  4277. break;
  4278. case PIPE_12BPC:
  4279. bpp = 36;
  4280. break;
  4281. default:
  4282. DRM_ERROR("unknown pipe bpc value\n");
  4283. bpp = 24;
  4284. }
  4285. if (!lane) {
  4286. /*
  4287. * Account for spread spectrum to avoid
  4288. * oversubscribing the link. Max center spread
  4289. * is 2.5%; use 5% for safety's sake.
  4290. */
  4291. u32 bps = target_clock * bpp * 21 / 20;
  4292. lane = bps / (link_bw * 8) + 1;
  4293. }
  4294. intel_crtc->fdi_lanes = lane;
  4295. if (pixel_multiplier > 1)
  4296. link_bw *= pixel_multiplier;
  4297. ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
  4298. /* Ironlake: try to setup display ref clock before DPLL
  4299. * enabling. This is only under driver's control after
  4300. * PCH B stepping, previous chipset stepping should be
  4301. * ignoring this setting.
  4302. */
  4303. temp = I915_READ(PCH_DREF_CONTROL);
  4304. /* Always enable nonspread source */
  4305. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  4306. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  4307. temp &= ~DREF_SSC_SOURCE_MASK;
  4308. temp |= DREF_SSC_SOURCE_ENABLE;
  4309. I915_WRITE(PCH_DREF_CONTROL, temp);
  4310. POSTING_READ(PCH_DREF_CONTROL);
  4311. udelay(200);
  4312. if (has_edp_encoder) {
  4313. if (intel_panel_use_ssc(dev_priv)) {
  4314. temp |= DREF_SSC1_ENABLE;
  4315. I915_WRITE(PCH_DREF_CONTROL, temp);
  4316. POSTING_READ(PCH_DREF_CONTROL);
  4317. udelay(200);
  4318. }
  4319. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4320. /* Enable CPU source on CPU attached eDP */
  4321. if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4322. if (intel_panel_use_ssc(dev_priv))
  4323. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4324. else
  4325. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4326. } else {
  4327. /* Enable SSC on PCH eDP if needed */
  4328. if (intel_panel_use_ssc(dev_priv)) {
  4329. DRM_ERROR("enabling SSC on PCH\n");
  4330. temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
  4331. }
  4332. }
  4333. I915_WRITE(PCH_DREF_CONTROL, temp);
  4334. POSTING_READ(PCH_DREF_CONTROL);
  4335. udelay(200);
  4336. }
  4337. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4338. if (has_reduced_clock)
  4339. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4340. reduced_clock.m2;
  4341. /* Enable autotuning of the PLL clock (if permissible) */
  4342. factor = 21;
  4343. if (is_lvds) {
  4344. if ((intel_panel_use_ssc(dev_priv) &&
  4345. dev_priv->lvds_ssc_freq == 100) ||
  4346. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  4347. factor = 25;
  4348. } else if (is_sdvo && is_tv)
  4349. factor = 20;
  4350. if (clock.m1 < factor * clock.n)
  4351. fp |= FP_CB_TUNE;
  4352. dpll = 0;
  4353. if (is_lvds)
  4354. dpll |= DPLLB_MODE_LVDS;
  4355. else
  4356. dpll |= DPLLB_MODE_DAC_SERIAL;
  4357. if (is_sdvo) {
  4358. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4359. if (pixel_multiplier > 1) {
  4360. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  4361. }
  4362. dpll |= DPLL_DVO_HIGH_SPEED;
  4363. }
  4364. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4365. dpll |= DPLL_DVO_HIGH_SPEED;
  4366. /* compute bitmask from p1 value */
  4367. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4368. /* also FPA1 */
  4369. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4370. switch (clock.p2) {
  4371. case 5:
  4372. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4373. break;
  4374. case 7:
  4375. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4376. break;
  4377. case 10:
  4378. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4379. break;
  4380. case 14:
  4381. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4382. break;
  4383. }
  4384. if (is_sdvo && is_tv)
  4385. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4386. else if (is_tv)
  4387. /* XXX: just matching BIOS for now */
  4388. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4389. dpll |= 3;
  4390. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4391. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4392. else
  4393. dpll |= PLL_REF_INPUT_DREFCLK;
  4394. /* setup pipeconf */
  4395. pipeconf = I915_READ(PIPECONF(pipe));
  4396. /* Set up the display plane register */
  4397. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4398. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  4399. drm_mode_debug_printmodeline(mode);
  4400. /* PCH eDP needs FDI, but CPU eDP does not */
  4401. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4402. I915_WRITE(PCH_FP0(pipe), fp);
  4403. I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  4404. POSTING_READ(PCH_DPLL(pipe));
  4405. udelay(150);
  4406. }
  4407. /* enable transcoder DPLL */
  4408. if (HAS_PCH_CPT(dev)) {
  4409. temp = I915_READ(PCH_DPLL_SEL);
  4410. switch (pipe) {
  4411. case 0:
  4412. temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
  4413. break;
  4414. case 1:
  4415. temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
  4416. break;
  4417. case 2:
  4418. /* FIXME: manage transcoder PLLs? */
  4419. temp |= TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL;
  4420. break;
  4421. default:
  4422. BUG();
  4423. }
  4424. I915_WRITE(PCH_DPLL_SEL, temp);
  4425. POSTING_READ(PCH_DPLL_SEL);
  4426. udelay(150);
  4427. }
  4428. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4429. * This is an exception to the general rule that mode_set doesn't turn
  4430. * things on.
  4431. */
  4432. if (is_lvds) {
  4433. temp = I915_READ(PCH_LVDS);
  4434. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  4435. if (pipe == 1) {
  4436. if (HAS_PCH_CPT(dev))
  4437. temp |= PORT_TRANS_B_SEL_CPT;
  4438. else
  4439. temp |= LVDS_PIPEB_SELECT;
  4440. } else {
  4441. if (HAS_PCH_CPT(dev))
  4442. temp &= ~PORT_TRANS_SEL_MASK;
  4443. else
  4444. temp &= ~LVDS_PIPEB_SELECT;
  4445. }
  4446. /* set the corresponsding LVDS_BORDER bit */
  4447. temp |= dev_priv->lvds_border_bits;
  4448. /* Set the B0-B3 data pairs corresponding to whether we're going to
  4449. * set the DPLLs for dual-channel mode or not.
  4450. */
  4451. if (clock.p2 == 7)
  4452. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  4453. else
  4454. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  4455. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  4456. * appropriately here, but we need to look more thoroughly into how
  4457. * panels behave in the two modes.
  4458. */
  4459. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  4460. lvds_sync |= LVDS_HSYNC_POLARITY;
  4461. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4462. lvds_sync |= LVDS_VSYNC_POLARITY;
  4463. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  4464. != lvds_sync) {
  4465. char flags[2] = "-+";
  4466. DRM_INFO("Changing LVDS panel from "
  4467. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  4468. flags[!(temp & LVDS_HSYNC_POLARITY)],
  4469. flags[!(temp & LVDS_VSYNC_POLARITY)],
  4470. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  4471. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  4472. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4473. temp |= lvds_sync;
  4474. }
  4475. I915_WRITE(PCH_LVDS, temp);
  4476. }
  4477. /* set the dithering flag and clear for anything other than a panel. */
  4478. pipeconf &= ~PIPECONF_DITHER_EN;
  4479. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  4480. if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {
  4481. pipeconf |= PIPECONF_DITHER_EN;
  4482. pipeconf |= PIPECONF_DITHER_TYPE_ST1;
  4483. }
  4484. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4485. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4486. } else {
  4487. /* For non-DP output, clear any trans DP clock recovery setting.*/
  4488. I915_WRITE(TRANSDATA_M1(pipe), 0);
  4489. I915_WRITE(TRANSDATA_N1(pipe), 0);
  4490. I915_WRITE(TRANSDPLINK_M1(pipe), 0);
  4491. I915_WRITE(TRANSDPLINK_N1(pipe), 0);
  4492. }
  4493. if (!has_edp_encoder ||
  4494. intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4495. I915_WRITE(PCH_DPLL(pipe), dpll);
  4496. /* Wait for the clocks to stabilize. */
  4497. POSTING_READ(PCH_DPLL(pipe));
  4498. udelay(150);
  4499. /* The pixel multiplier can only be updated once the
  4500. * DPLL is enabled and the clocks are stable.
  4501. *
  4502. * So write it again.
  4503. */
  4504. I915_WRITE(PCH_DPLL(pipe), dpll);
  4505. }
  4506. intel_crtc->lowfreq_avail = false;
  4507. if (is_lvds && has_reduced_clock && i915_powersave) {
  4508. I915_WRITE(PCH_FP1(pipe), fp2);
  4509. intel_crtc->lowfreq_avail = true;
  4510. if (HAS_PIPE_CXSR(dev)) {
  4511. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4512. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4513. }
  4514. } else {
  4515. I915_WRITE(PCH_FP1(pipe), fp);
  4516. if (HAS_PIPE_CXSR(dev)) {
  4517. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4518. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4519. }
  4520. }
  4521. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4522. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4523. /* the chip adds 2 halflines automatically */
  4524. adjusted_mode->crtc_vdisplay -= 1;
  4525. adjusted_mode->crtc_vtotal -= 1;
  4526. adjusted_mode->crtc_vblank_start -= 1;
  4527. adjusted_mode->crtc_vblank_end -= 1;
  4528. adjusted_mode->crtc_vsync_end -= 1;
  4529. adjusted_mode->crtc_vsync_start -= 1;
  4530. } else
  4531. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  4532. I915_WRITE(HTOTAL(pipe),
  4533. (adjusted_mode->crtc_hdisplay - 1) |
  4534. ((adjusted_mode->crtc_htotal - 1) << 16));
  4535. I915_WRITE(HBLANK(pipe),
  4536. (adjusted_mode->crtc_hblank_start - 1) |
  4537. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4538. I915_WRITE(HSYNC(pipe),
  4539. (adjusted_mode->crtc_hsync_start - 1) |
  4540. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4541. I915_WRITE(VTOTAL(pipe),
  4542. (adjusted_mode->crtc_vdisplay - 1) |
  4543. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4544. I915_WRITE(VBLANK(pipe),
  4545. (adjusted_mode->crtc_vblank_start - 1) |
  4546. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4547. I915_WRITE(VSYNC(pipe),
  4548. (adjusted_mode->crtc_vsync_start - 1) |
  4549. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4550. /* pipesrc controls the size that is scaled from, which should
  4551. * always be the user's requested size.
  4552. */
  4553. I915_WRITE(PIPESRC(pipe),
  4554. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4555. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  4556. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  4557. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  4558. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  4559. if (has_edp_encoder &&
  4560. !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4561. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  4562. }
  4563. I915_WRITE(PIPECONF(pipe), pipeconf);
  4564. POSTING_READ(PIPECONF(pipe));
  4565. intel_wait_for_vblank(dev, pipe);
  4566. if (IS_GEN5(dev)) {
  4567. /* enable address swizzle for tiling buffer */
  4568. temp = I915_READ(DISP_ARB_CTL);
  4569. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  4570. }
  4571. I915_WRITE(DSPCNTR(plane), dspcntr);
  4572. POSTING_READ(DSPCNTR(plane));
  4573. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4574. intel_update_watermarks(dev);
  4575. return ret;
  4576. }
  4577. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  4578. struct drm_display_mode *mode,
  4579. struct drm_display_mode *adjusted_mode,
  4580. int x, int y,
  4581. struct drm_framebuffer *old_fb)
  4582. {
  4583. struct drm_device *dev = crtc->dev;
  4584. struct drm_i915_private *dev_priv = dev->dev_private;
  4585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4586. int pipe = intel_crtc->pipe;
  4587. int ret;
  4588. drm_vblank_pre_modeset(dev, pipe);
  4589. ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
  4590. x, y, old_fb);
  4591. drm_vblank_post_modeset(dev, pipe);
  4592. return ret;
  4593. }
  4594. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  4595. void intel_crtc_load_lut(struct drm_crtc *crtc)
  4596. {
  4597. struct drm_device *dev = crtc->dev;
  4598. struct drm_i915_private *dev_priv = dev->dev_private;
  4599. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4600. int palreg = PALETTE(intel_crtc->pipe);
  4601. int i;
  4602. /* The clocks have to be on to load the palette. */
  4603. if (!crtc->enabled)
  4604. return;
  4605. /* use legacy palette for Ironlake */
  4606. if (HAS_PCH_SPLIT(dev))
  4607. palreg = LGC_PALETTE(intel_crtc->pipe);
  4608. for (i = 0; i < 256; i++) {
  4609. I915_WRITE(palreg + 4 * i,
  4610. (intel_crtc->lut_r[i] << 16) |
  4611. (intel_crtc->lut_g[i] << 8) |
  4612. intel_crtc->lut_b[i]);
  4613. }
  4614. }
  4615. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  4616. {
  4617. struct drm_device *dev = crtc->dev;
  4618. struct drm_i915_private *dev_priv = dev->dev_private;
  4619. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4620. bool visible = base != 0;
  4621. u32 cntl;
  4622. if (intel_crtc->cursor_visible == visible)
  4623. return;
  4624. cntl = I915_READ(_CURACNTR);
  4625. if (visible) {
  4626. /* On these chipsets we can only modify the base whilst
  4627. * the cursor is disabled.
  4628. */
  4629. I915_WRITE(_CURABASE, base);
  4630. cntl &= ~(CURSOR_FORMAT_MASK);
  4631. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  4632. cntl |= CURSOR_ENABLE |
  4633. CURSOR_GAMMA_ENABLE |
  4634. CURSOR_FORMAT_ARGB;
  4635. } else
  4636. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  4637. I915_WRITE(_CURACNTR, cntl);
  4638. intel_crtc->cursor_visible = visible;
  4639. }
  4640. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  4641. {
  4642. struct drm_device *dev = crtc->dev;
  4643. struct drm_i915_private *dev_priv = dev->dev_private;
  4644. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4645. int pipe = intel_crtc->pipe;
  4646. bool visible = base != 0;
  4647. if (intel_crtc->cursor_visible != visible) {
  4648. uint32_t cntl = I915_READ(CURCNTR(pipe));
  4649. if (base) {
  4650. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  4651. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4652. cntl |= pipe << 28; /* Connect to correct pipe */
  4653. } else {
  4654. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4655. cntl |= CURSOR_MODE_DISABLE;
  4656. }
  4657. I915_WRITE(CURCNTR(pipe), cntl);
  4658. intel_crtc->cursor_visible = visible;
  4659. }
  4660. /* and commit changes on next vblank */
  4661. I915_WRITE(CURBASE(pipe), base);
  4662. }
  4663. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  4664. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  4665. bool on)
  4666. {
  4667. struct drm_device *dev = crtc->dev;
  4668. struct drm_i915_private *dev_priv = dev->dev_private;
  4669. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4670. int pipe = intel_crtc->pipe;
  4671. int x = intel_crtc->cursor_x;
  4672. int y = intel_crtc->cursor_y;
  4673. u32 base, pos;
  4674. bool visible;
  4675. pos = 0;
  4676. if (on && crtc->enabled && crtc->fb) {
  4677. base = intel_crtc->cursor_addr;
  4678. if (x > (int) crtc->fb->width)
  4679. base = 0;
  4680. if (y > (int) crtc->fb->height)
  4681. base = 0;
  4682. } else
  4683. base = 0;
  4684. if (x < 0) {
  4685. if (x + intel_crtc->cursor_width < 0)
  4686. base = 0;
  4687. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  4688. x = -x;
  4689. }
  4690. pos |= x << CURSOR_X_SHIFT;
  4691. if (y < 0) {
  4692. if (y + intel_crtc->cursor_height < 0)
  4693. base = 0;
  4694. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  4695. y = -y;
  4696. }
  4697. pos |= y << CURSOR_Y_SHIFT;
  4698. visible = base != 0;
  4699. if (!visible && !intel_crtc->cursor_visible)
  4700. return;
  4701. I915_WRITE(CURPOS(pipe), pos);
  4702. if (IS_845G(dev) || IS_I865G(dev))
  4703. i845_update_cursor(crtc, base);
  4704. else
  4705. i9xx_update_cursor(crtc, base);
  4706. if (visible)
  4707. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  4708. }
  4709. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  4710. struct drm_file *file,
  4711. uint32_t handle,
  4712. uint32_t width, uint32_t height)
  4713. {
  4714. struct drm_device *dev = crtc->dev;
  4715. struct drm_i915_private *dev_priv = dev->dev_private;
  4716. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4717. struct drm_i915_gem_object *obj;
  4718. uint32_t addr;
  4719. int ret;
  4720. DRM_DEBUG_KMS("\n");
  4721. /* if we want to turn off the cursor ignore width and height */
  4722. if (!handle) {
  4723. DRM_DEBUG_KMS("cursor off\n");
  4724. addr = 0;
  4725. obj = NULL;
  4726. mutex_lock(&dev->struct_mutex);
  4727. goto finish;
  4728. }
  4729. /* Currently we only support 64x64 cursors */
  4730. if (width != 64 || height != 64) {
  4731. DRM_ERROR("we currently only support 64x64 cursors\n");
  4732. return -EINVAL;
  4733. }
  4734. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  4735. if (&obj->base == NULL)
  4736. return -ENOENT;
  4737. if (obj->base.size < width * height * 4) {
  4738. DRM_ERROR("buffer is to small\n");
  4739. ret = -ENOMEM;
  4740. goto fail;
  4741. }
  4742. /* we only need to pin inside GTT if cursor is non-phy */
  4743. mutex_lock(&dev->struct_mutex);
  4744. if (!dev_priv->info->cursor_needs_physical) {
  4745. if (obj->tiling_mode) {
  4746. DRM_ERROR("cursor cannot be tiled\n");
  4747. ret = -EINVAL;
  4748. goto fail_locked;
  4749. }
  4750. ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
  4751. if (ret) {
  4752. DRM_ERROR("failed to pin cursor bo\n");
  4753. goto fail_locked;
  4754. }
  4755. ret = i915_gem_object_set_to_gtt_domain(obj, 0);
  4756. if (ret) {
  4757. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4758. goto fail_unpin;
  4759. }
  4760. ret = i915_gem_object_put_fence(obj);
  4761. if (ret) {
  4762. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4763. goto fail_unpin;
  4764. }
  4765. addr = obj->gtt_offset;
  4766. } else {
  4767. int align = IS_I830(dev) ? 16 * 1024 : 256;
  4768. ret = i915_gem_attach_phys_object(dev, obj,
  4769. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  4770. align);
  4771. if (ret) {
  4772. DRM_ERROR("failed to attach phys object\n");
  4773. goto fail_locked;
  4774. }
  4775. addr = obj->phys_obj->handle->busaddr;
  4776. }
  4777. if (IS_GEN2(dev))
  4778. I915_WRITE(CURSIZE, (height << 12) | width);
  4779. finish:
  4780. if (intel_crtc->cursor_bo) {
  4781. if (dev_priv->info->cursor_needs_physical) {
  4782. if (intel_crtc->cursor_bo != obj)
  4783. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  4784. } else
  4785. i915_gem_object_unpin(intel_crtc->cursor_bo);
  4786. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  4787. }
  4788. mutex_unlock(&dev->struct_mutex);
  4789. intel_crtc->cursor_addr = addr;
  4790. intel_crtc->cursor_bo = obj;
  4791. intel_crtc->cursor_width = width;
  4792. intel_crtc->cursor_height = height;
  4793. intel_crtc_update_cursor(crtc, true);
  4794. return 0;
  4795. fail_unpin:
  4796. i915_gem_object_unpin(obj);
  4797. fail_locked:
  4798. mutex_unlock(&dev->struct_mutex);
  4799. fail:
  4800. drm_gem_object_unreference_unlocked(&obj->base);
  4801. return ret;
  4802. }
  4803. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  4804. {
  4805. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4806. intel_crtc->cursor_x = x;
  4807. intel_crtc->cursor_y = y;
  4808. intel_crtc_update_cursor(crtc, true);
  4809. return 0;
  4810. }
  4811. /** Sets the color ramps on behalf of RandR */
  4812. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  4813. u16 blue, int regno)
  4814. {
  4815. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4816. intel_crtc->lut_r[regno] = red >> 8;
  4817. intel_crtc->lut_g[regno] = green >> 8;
  4818. intel_crtc->lut_b[regno] = blue >> 8;
  4819. }
  4820. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  4821. u16 *blue, int regno)
  4822. {
  4823. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4824. *red = intel_crtc->lut_r[regno] << 8;
  4825. *green = intel_crtc->lut_g[regno] << 8;
  4826. *blue = intel_crtc->lut_b[regno] << 8;
  4827. }
  4828. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  4829. u16 *blue, uint32_t start, uint32_t size)
  4830. {
  4831. int end = (start + size > 256) ? 256 : start + size, i;
  4832. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4833. for (i = start; i < end; i++) {
  4834. intel_crtc->lut_r[i] = red[i] >> 8;
  4835. intel_crtc->lut_g[i] = green[i] >> 8;
  4836. intel_crtc->lut_b[i] = blue[i] >> 8;
  4837. }
  4838. intel_crtc_load_lut(crtc);
  4839. }
  4840. /**
  4841. * Get a pipe with a simple mode set on it for doing load-based monitor
  4842. * detection.
  4843. *
  4844. * It will be up to the load-detect code to adjust the pipe as appropriate for
  4845. * its requirements. The pipe will be connected to no other encoders.
  4846. *
  4847. * Currently this code will only succeed if there is a pipe with no encoders
  4848. * configured for it. In the future, it could choose to temporarily disable
  4849. * some outputs to free up a pipe for its use.
  4850. *
  4851. * \return crtc, or NULL if no pipes are available.
  4852. */
  4853. /* VESA 640x480x72Hz mode to set on the pipe */
  4854. static struct drm_display_mode load_detect_mode = {
  4855. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  4856. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  4857. };
  4858. static struct drm_framebuffer *
  4859. intel_framebuffer_create(struct drm_device *dev,
  4860. struct drm_mode_fb_cmd *mode_cmd,
  4861. struct drm_i915_gem_object *obj)
  4862. {
  4863. struct intel_framebuffer *intel_fb;
  4864. int ret;
  4865. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  4866. if (!intel_fb) {
  4867. drm_gem_object_unreference_unlocked(&obj->base);
  4868. return ERR_PTR(-ENOMEM);
  4869. }
  4870. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  4871. if (ret) {
  4872. drm_gem_object_unreference_unlocked(&obj->base);
  4873. kfree(intel_fb);
  4874. return ERR_PTR(ret);
  4875. }
  4876. return &intel_fb->base;
  4877. }
  4878. static u32
  4879. intel_framebuffer_pitch_for_width(int width, int bpp)
  4880. {
  4881. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  4882. return ALIGN(pitch, 64);
  4883. }
  4884. static u32
  4885. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  4886. {
  4887. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  4888. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  4889. }
  4890. static struct drm_framebuffer *
  4891. intel_framebuffer_create_for_mode(struct drm_device *dev,
  4892. struct drm_display_mode *mode,
  4893. int depth, int bpp)
  4894. {
  4895. struct drm_i915_gem_object *obj;
  4896. struct drm_mode_fb_cmd mode_cmd;
  4897. obj = i915_gem_alloc_object(dev,
  4898. intel_framebuffer_size_for_mode(mode, bpp));
  4899. if (obj == NULL)
  4900. return ERR_PTR(-ENOMEM);
  4901. mode_cmd.width = mode->hdisplay;
  4902. mode_cmd.height = mode->vdisplay;
  4903. mode_cmd.depth = depth;
  4904. mode_cmd.bpp = bpp;
  4905. mode_cmd.pitch = intel_framebuffer_pitch_for_width(mode_cmd.width, bpp);
  4906. return intel_framebuffer_create(dev, &mode_cmd, obj);
  4907. }
  4908. static struct drm_framebuffer *
  4909. mode_fits_in_fbdev(struct drm_device *dev,
  4910. struct drm_display_mode *mode)
  4911. {
  4912. struct drm_i915_private *dev_priv = dev->dev_private;
  4913. struct drm_i915_gem_object *obj;
  4914. struct drm_framebuffer *fb;
  4915. if (dev_priv->fbdev == NULL)
  4916. return NULL;
  4917. obj = dev_priv->fbdev->ifb.obj;
  4918. if (obj == NULL)
  4919. return NULL;
  4920. fb = &dev_priv->fbdev->ifb.base;
  4921. if (fb->pitch < intel_framebuffer_pitch_for_width(mode->hdisplay,
  4922. fb->bits_per_pixel))
  4923. return NULL;
  4924. if (obj->base.size < mode->vdisplay * fb->pitch)
  4925. return NULL;
  4926. return fb;
  4927. }
  4928. bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  4929. struct drm_connector *connector,
  4930. struct drm_display_mode *mode,
  4931. struct intel_load_detect_pipe *old)
  4932. {
  4933. struct intel_crtc *intel_crtc;
  4934. struct drm_crtc *possible_crtc;
  4935. struct drm_encoder *encoder = &intel_encoder->base;
  4936. struct drm_crtc *crtc = NULL;
  4937. struct drm_device *dev = encoder->dev;
  4938. struct drm_framebuffer *old_fb;
  4939. int i = -1;
  4940. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4941. connector->base.id, drm_get_connector_name(connector),
  4942. encoder->base.id, drm_get_encoder_name(encoder));
  4943. /*
  4944. * Algorithm gets a little messy:
  4945. *
  4946. * - if the connector already has an assigned crtc, use it (but make
  4947. * sure it's on first)
  4948. *
  4949. * - try to find the first unused crtc that can drive this connector,
  4950. * and use that if we find one
  4951. */
  4952. /* See if we already have a CRTC for this connector */
  4953. if (encoder->crtc) {
  4954. crtc = encoder->crtc;
  4955. intel_crtc = to_intel_crtc(crtc);
  4956. old->dpms_mode = intel_crtc->dpms_mode;
  4957. old->load_detect_temp = false;
  4958. /* Make sure the crtc and connector are running */
  4959. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4960. struct drm_encoder_helper_funcs *encoder_funcs;
  4961. struct drm_crtc_helper_funcs *crtc_funcs;
  4962. crtc_funcs = crtc->helper_private;
  4963. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4964. encoder_funcs = encoder->helper_private;
  4965. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  4966. }
  4967. return true;
  4968. }
  4969. /* Find an unused one (if possible) */
  4970. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  4971. i++;
  4972. if (!(encoder->possible_crtcs & (1 << i)))
  4973. continue;
  4974. if (!possible_crtc->enabled) {
  4975. crtc = possible_crtc;
  4976. break;
  4977. }
  4978. }
  4979. /*
  4980. * If we didn't find an unused CRTC, don't use any.
  4981. */
  4982. if (!crtc) {
  4983. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  4984. return false;
  4985. }
  4986. encoder->crtc = crtc;
  4987. connector->encoder = encoder;
  4988. intel_crtc = to_intel_crtc(crtc);
  4989. old->dpms_mode = intel_crtc->dpms_mode;
  4990. old->load_detect_temp = true;
  4991. old->release_fb = NULL;
  4992. if (!mode)
  4993. mode = &load_detect_mode;
  4994. old_fb = crtc->fb;
  4995. /* We need a framebuffer large enough to accommodate all accesses
  4996. * that the plane may generate whilst we perform load detection.
  4997. * We can not rely on the fbcon either being present (we get called
  4998. * during its initialisation to detect all boot displays, or it may
  4999. * not even exist) or that it is large enough to satisfy the
  5000. * requested mode.
  5001. */
  5002. crtc->fb = mode_fits_in_fbdev(dev, mode);
  5003. if (crtc->fb == NULL) {
  5004. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  5005. crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  5006. old->release_fb = crtc->fb;
  5007. } else
  5008. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  5009. if (IS_ERR(crtc->fb)) {
  5010. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  5011. crtc->fb = old_fb;
  5012. return false;
  5013. }
  5014. if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
  5015. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  5016. if (old->release_fb)
  5017. old->release_fb->funcs->destroy(old->release_fb);
  5018. crtc->fb = old_fb;
  5019. return false;
  5020. }
  5021. /* let the connector get through one full cycle before testing */
  5022. intel_wait_for_vblank(dev, intel_crtc->pipe);
  5023. return true;
  5024. }
  5025. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  5026. struct drm_connector *connector,
  5027. struct intel_load_detect_pipe *old)
  5028. {
  5029. struct drm_encoder *encoder = &intel_encoder->base;
  5030. struct drm_device *dev = encoder->dev;
  5031. struct drm_crtc *crtc = encoder->crtc;
  5032. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  5033. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  5034. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5035. connector->base.id, drm_get_connector_name(connector),
  5036. encoder->base.id, drm_get_encoder_name(encoder));
  5037. if (old->load_detect_temp) {
  5038. connector->encoder = NULL;
  5039. drm_helper_disable_unused_functions(dev);
  5040. if (old->release_fb)
  5041. old->release_fb->funcs->destroy(old->release_fb);
  5042. return;
  5043. }
  5044. /* Switch crtc and encoder back off if necessary */
  5045. if (old->dpms_mode != DRM_MODE_DPMS_ON) {
  5046. encoder_funcs->dpms(encoder, old->dpms_mode);
  5047. crtc_funcs->dpms(crtc, old->dpms_mode);
  5048. }
  5049. }
  5050. /* Returns the clock of the currently programmed mode of the given pipe. */
  5051. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  5052. {
  5053. struct drm_i915_private *dev_priv = dev->dev_private;
  5054. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5055. int pipe = intel_crtc->pipe;
  5056. u32 dpll = I915_READ(DPLL(pipe));
  5057. u32 fp;
  5058. intel_clock_t clock;
  5059. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  5060. fp = I915_READ(FP0(pipe));
  5061. else
  5062. fp = I915_READ(FP1(pipe));
  5063. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  5064. if (IS_PINEVIEW(dev)) {
  5065. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  5066. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5067. } else {
  5068. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  5069. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5070. }
  5071. if (!IS_GEN2(dev)) {
  5072. if (IS_PINEVIEW(dev))
  5073. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  5074. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  5075. else
  5076. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  5077. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5078. switch (dpll & DPLL_MODE_MASK) {
  5079. case DPLLB_MODE_DAC_SERIAL:
  5080. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  5081. 5 : 10;
  5082. break;
  5083. case DPLLB_MODE_LVDS:
  5084. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  5085. 7 : 14;
  5086. break;
  5087. default:
  5088. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  5089. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  5090. return 0;
  5091. }
  5092. /* XXX: Handle the 100Mhz refclk */
  5093. intel_clock(dev, 96000, &clock);
  5094. } else {
  5095. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  5096. if (is_lvds) {
  5097. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  5098. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5099. clock.p2 = 14;
  5100. if ((dpll & PLL_REF_INPUT_MASK) ==
  5101. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  5102. /* XXX: might not be 66MHz */
  5103. intel_clock(dev, 66000, &clock);
  5104. } else
  5105. intel_clock(dev, 48000, &clock);
  5106. } else {
  5107. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  5108. clock.p1 = 2;
  5109. else {
  5110. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  5111. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  5112. }
  5113. if (dpll & PLL_P2_DIVIDE_BY_4)
  5114. clock.p2 = 4;
  5115. else
  5116. clock.p2 = 2;
  5117. intel_clock(dev, 48000, &clock);
  5118. }
  5119. }
  5120. /* XXX: It would be nice to validate the clocks, but we can't reuse
  5121. * i830PllIsValid() because it relies on the xf86_config connector
  5122. * configuration being accurate, which it isn't necessarily.
  5123. */
  5124. return clock.dot;
  5125. }
  5126. /** Returns the currently programmed mode of the given pipe. */
  5127. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  5128. struct drm_crtc *crtc)
  5129. {
  5130. struct drm_i915_private *dev_priv = dev->dev_private;
  5131. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5132. int pipe = intel_crtc->pipe;
  5133. struct drm_display_mode *mode;
  5134. int htot = I915_READ(HTOTAL(pipe));
  5135. int hsync = I915_READ(HSYNC(pipe));
  5136. int vtot = I915_READ(VTOTAL(pipe));
  5137. int vsync = I915_READ(VSYNC(pipe));
  5138. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  5139. if (!mode)
  5140. return NULL;
  5141. mode->clock = intel_crtc_clock_get(dev, crtc);
  5142. mode->hdisplay = (htot & 0xffff) + 1;
  5143. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  5144. mode->hsync_start = (hsync & 0xffff) + 1;
  5145. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  5146. mode->vdisplay = (vtot & 0xffff) + 1;
  5147. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  5148. mode->vsync_start = (vsync & 0xffff) + 1;
  5149. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  5150. drm_mode_set_name(mode);
  5151. drm_mode_set_crtcinfo(mode, 0);
  5152. return mode;
  5153. }
  5154. #define GPU_IDLE_TIMEOUT 500 /* ms */
  5155. /* When this timer fires, we've been idle for awhile */
  5156. static void intel_gpu_idle_timer(unsigned long arg)
  5157. {
  5158. struct drm_device *dev = (struct drm_device *)arg;
  5159. drm_i915_private_t *dev_priv = dev->dev_private;
  5160. if (!list_empty(&dev_priv->mm.active_list)) {
  5161. /* Still processing requests, so just re-arm the timer. */
  5162. mod_timer(&dev_priv->idle_timer, jiffies +
  5163. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  5164. return;
  5165. }
  5166. dev_priv->busy = false;
  5167. queue_work(dev_priv->wq, &dev_priv->idle_work);
  5168. }
  5169. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  5170. static void intel_crtc_idle_timer(unsigned long arg)
  5171. {
  5172. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  5173. struct drm_crtc *crtc = &intel_crtc->base;
  5174. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  5175. struct intel_framebuffer *intel_fb;
  5176. intel_fb = to_intel_framebuffer(crtc->fb);
  5177. if (intel_fb && intel_fb->obj->active) {
  5178. /* The framebuffer is still being accessed by the GPU. */
  5179. mod_timer(&intel_crtc->idle_timer, jiffies +
  5180. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5181. return;
  5182. }
  5183. intel_crtc->busy = false;
  5184. queue_work(dev_priv->wq, &dev_priv->idle_work);
  5185. }
  5186. static void intel_increase_pllclock(struct drm_crtc *crtc)
  5187. {
  5188. struct drm_device *dev = crtc->dev;
  5189. drm_i915_private_t *dev_priv = dev->dev_private;
  5190. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5191. int pipe = intel_crtc->pipe;
  5192. int dpll_reg = DPLL(pipe);
  5193. int dpll;
  5194. if (HAS_PCH_SPLIT(dev))
  5195. return;
  5196. if (!dev_priv->lvds_downclock_avail)
  5197. return;
  5198. dpll = I915_READ(dpll_reg);
  5199. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  5200. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  5201. /* Unlock panel regs */
  5202. I915_WRITE(PP_CONTROL,
  5203. I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
  5204. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  5205. I915_WRITE(dpll_reg, dpll);
  5206. intel_wait_for_vblank(dev, pipe);
  5207. dpll = I915_READ(dpll_reg);
  5208. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  5209. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  5210. /* ...and lock them again */
  5211. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  5212. }
  5213. /* Schedule downclock */
  5214. mod_timer(&intel_crtc->idle_timer, jiffies +
  5215. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5216. }
  5217. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  5218. {
  5219. struct drm_device *dev = crtc->dev;
  5220. drm_i915_private_t *dev_priv = dev->dev_private;
  5221. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5222. int pipe = intel_crtc->pipe;
  5223. int dpll_reg = DPLL(pipe);
  5224. int dpll = I915_READ(dpll_reg);
  5225. if (HAS_PCH_SPLIT(dev))
  5226. return;
  5227. if (!dev_priv->lvds_downclock_avail)
  5228. return;
  5229. /*
  5230. * Since this is called by a timer, we should never get here in
  5231. * the manual case.
  5232. */
  5233. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  5234. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  5235. /* Unlock panel regs */
  5236. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  5237. PANEL_UNLOCK_REGS);
  5238. dpll |= DISPLAY_RATE_SELECT_FPA1;
  5239. I915_WRITE(dpll_reg, dpll);
  5240. intel_wait_for_vblank(dev, pipe);
  5241. dpll = I915_READ(dpll_reg);
  5242. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  5243. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  5244. /* ...and lock them again */
  5245. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  5246. }
  5247. }
  5248. /**
  5249. * intel_idle_update - adjust clocks for idleness
  5250. * @work: work struct
  5251. *
  5252. * Either the GPU or display (or both) went idle. Check the busy status
  5253. * here and adjust the CRTC and GPU clocks as necessary.
  5254. */
  5255. static void intel_idle_update(struct work_struct *work)
  5256. {
  5257. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  5258. idle_work);
  5259. struct drm_device *dev = dev_priv->dev;
  5260. struct drm_crtc *crtc;
  5261. struct intel_crtc *intel_crtc;
  5262. if (!i915_powersave)
  5263. return;
  5264. mutex_lock(&dev->struct_mutex);
  5265. i915_update_gfx_val(dev_priv);
  5266. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5267. /* Skip inactive CRTCs */
  5268. if (!crtc->fb)
  5269. continue;
  5270. intel_crtc = to_intel_crtc(crtc);
  5271. if (!intel_crtc->busy)
  5272. intel_decrease_pllclock(crtc);
  5273. }
  5274. mutex_unlock(&dev->struct_mutex);
  5275. }
  5276. /**
  5277. * intel_mark_busy - mark the GPU and possibly the display busy
  5278. * @dev: drm device
  5279. * @obj: object we're operating on
  5280. *
  5281. * Callers can use this function to indicate that the GPU is busy processing
  5282. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  5283. * buffer), we'll also mark the display as busy, so we know to increase its
  5284. * clock frequency.
  5285. */
  5286. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  5287. {
  5288. drm_i915_private_t *dev_priv = dev->dev_private;
  5289. struct drm_crtc *crtc = NULL;
  5290. struct intel_framebuffer *intel_fb;
  5291. struct intel_crtc *intel_crtc;
  5292. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  5293. return;
  5294. if (!dev_priv->busy)
  5295. dev_priv->busy = true;
  5296. else
  5297. mod_timer(&dev_priv->idle_timer, jiffies +
  5298. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  5299. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5300. if (!crtc->fb)
  5301. continue;
  5302. intel_crtc = to_intel_crtc(crtc);
  5303. intel_fb = to_intel_framebuffer(crtc->fb);
  5304. if (intel_fb->obj == obj) {
  5305. if (!intel_crtc->busy) {
  5306. /* Non-busy -> busy, upclock */
  5307. intel_increase_pllclock(crtc);
  5308. intel_crtc->busy = true;
  5309. } else {
  5310. /* Busy -> busy, put off timer */
  5311. mod_timer(&intel_crtc->idle_timer, jiffies +
  5312. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5313. }
  5314. }
  5315. }
  5316. }
  5317. static void intel_crtc_destroy(struct drm_crtc *crtc)
  5318. {
  5319. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5320. struct drm_device *dev = crtc->dev;
  5321. struct intel_unpin_work *work;
  5322. unsigned long flags;
  5323. spin_lock_irqsave(&dev->event_lock, flags);
  5324. work = intel_crtc->unpin_work;
  5325. intel_crtc->unpin_work = NULL;
  5326. spin_unlock_irqrestore(&dev->event_lock, flags);
  5327. if (work) {
  5328. cancel_work_sync(&work->work);
  5329. kfree(work);
  5330. }
  5331. drm_crtc_cleanup(crtc);
  5332. kfree(intel_crtc);
  5333. }
  5334. static void intel_unpin_work_fn(struct work_struct *__work)
  5335. {
  5336. struct intel_unpin_work *work =
  5337. container_of(__work, struct intel_unpin_work, work);
  5338. mutex_lock(&work->dev->struct_mutex);
  5339. i915_gem_object_unpin(work->old_fb_obj);
  5340. drm_gem_object_unreference(&work->pending_flip_obj->base);
  5341. drm_gem_object_unreference(&work->old_fb_obj->base);
  5342. mutex_unlock(&work->dev->struct_mutex);
  5343. kfree(work);
  5344. }
  5345. static void do_intel_finish_page_flip(struct drm_device *dev,
  5346. struct drm_crtc *crtc)
  5347. {
  5348. drm_i915_private_t *dev_priv = dev->dev_private;
  5349. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5350. struct intel_unpin_work *work;
  5351. struct drm_i915_gem_object *obj;
  5352. struct drm_pending_vblank_event *e;
  5353. struct timeval tnow, tvbl;
  5354. unsigned long flags;
  5355. /* Ignore early vblank irqs */
  5356. if (intel_crtc == NULL)
  5357. return;
  5358. do_gettimeofday(&tnow);
  5359. spin_lock_irqsave(&dev->event_lock, flags);
  5360. work = intel_crtc->unpin_work;
  5361. if (work == NULL || !work->pending) {
  5362. spin_unlock_irqrestore(&dev->event_lock, flags);
  5363. return;
  5364. }
  5365. intel_crtc->unpin_work = NULL;
  5366. if (work->event) {
  5367. e = work->event;
  5368. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  5369. /* Called before vblank count and timestamps have
  5370. * been updated for the vblank interval of flip
  5371. * completion? Need to increment vblank count and
  5372. * add one videorefresh duration to returned timestamp
  5373. * to account for this. We assume this happened if we
  5374. * get called over 0.9 frame durations after the last
  5375. * timestamped vblank.
  5376. *
  5377. * This calculation can not be used with vrefresh rates
  5378. * below 5Hz (10Hz to be on the safe side) without
  5379. * promoting to 64 integers.
  5380. */
  5381. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  5382. 9 * crtc->framedur_ns) {
  5383. e->event.sequence++;
  5384. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  5385. crtc->framedur_ns);
  5386. }
  5387. e->event.tv_sec = tvbl.tv_sec;
  5388. e->event.tv_usec = tvbl.tv_usec;
  5389. list_add_tail(&e->base.link,
  5390. &e->base.file_priv->event_list);
  5391. wake_up_interruptible(&e->base.file_priv->event_wait);
  5392. }
  5393. drm_vblank_put(dev, intel_crtc->pipe);
  5394. spin_unlock_irqrestore(&dev->event_lock, flags);
  5395. obj = work->old_fb_obj;
  5396. atomic_clear_mask(1 << intel_crtc->plane,
  5397. &obj->pending_flip.counter);
  5398. if (atomic_read(&obj->pending_flip) == 0)
  5399. wake_up(&dev_priv->pending_flip_queue);
  5400. schedule_work(&work->work);
  5401. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  5402. }
  5403. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  5404. {
  5405. drm_i915_private_t *dev_priv = dev->dev_private;
  5406. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  5407. do_intel_finish_page_flip(dev, crtc);
  5408. }
  5409. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  5410. {
  5411. drm_i915_private_t *dev_priv = dev->dev_private;
  5412. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  5413. do_intel_finish_page_flip(dev, crtc);
  5414. }
  5415. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  5416. {
  5417. drm_i915_private_t *dev_priv = dev->dev_private;
  5418. struct intel_crtc *intel_crtc =
  5419. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  5420. unsigned long flags;
  5421. spin_lock_irqsave(&dev->event_lock, flags);
  5422. if (intel_crtc->unpin_work) {
  5423. if ((++intel_crtc->unpin_work->pending) > 1)
  5424. DRM_ERROR("Prepared flip multiple times\n");
  5425. } else {
  5426. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  5427. }
  5428. spin_unlock_irqrestore(&dev->event_lock, flags);
  5429. }
  5430. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  5431. struct drm_framebuffer *fb,
  5432. struct drm_pending_vblank_event *event)
  5433. {
  5434. struct drm_device *dev = crtc->dev;
  5435. struct drm_i915_private *dev_priv = dev->dev_private;
  5436. struct intel_framebuffer *intel_fb;
  5437. struct drm_i915_gem_object *obj;
  5438. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5439. struct intel_unpin_work *work;
  5440. unsigned long flags, offset;
  5441. int pipe = intel_crtc->pipe;
  5442. u32 pf, pipesrc;
  5443. int ret;
  5444. work = kzalloc(sizeof *work, GFP_KERNEL);
  5445. if (work == NULL)
  5446. return -ENOMEM;
  5447. work->event = event;
  5448. work->dev = crtc->dev;
  5449. intel_fb = to_intel_framebuffer(crtc->fb);
  5450. work->old_fb_obj = intel_fb->obj;
  5451. INIT_WORK(&work->work, intel_unpin_work_fn);
  5452. /* We borrow the event spin lock for protecting unpin_work */
  5453. spin_lock_irqsave(&dev->event_lock, flags);
  5454. if (intel_crtc->unpin_work) {
  5455. spin_unlock_irqrestore(&dev->event_lock, flags);
  5456. kfree(work);
  5457. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  5458. return -EBUSY;
  5459. }
  5460. intel_crtc->unpin_work = work;
  5461. spin_unlock_irqrestore(&dev->event_lock, flags);
  5462. intel_fb = to_intel_framebuffer(fb);
  5463. obj = intel_fb->obj;
  5464. mutex_lock(&dev->struct_mutex);
  5465. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5466. if (ret)
  5467. goto cleanup_work;
  5468. /* Reference the objects for the scheduled work. */
  5469. drm_gem_object_reference(&work->old_fb_obj->base);
  5470. drm_gem_object_reference(&obj->base);
  5471. crtc->fb = fb;
  5472. ret = drm_vblank_get(dev, intel_crtc->pipe);
  5473. if (ret)
  5474. goto cleanup_objs;
  5475. if (IS_GEN3(dev) || IS_GEN2(dev)) {
  5476. u32 flip_mask;
  5477. /* Can't queue multiple flips, so wait for the previous
  5478. * one to finish before executing the next.
  5479. */
  5480. ret = BEGIN_LP_RING(2);
  5481. if (ret)
  5482. goto cleanup_objs;
  5483. if (intel_crtc->plane)
  5484. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5485. else
  5486. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5487. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  5488. OUT_RING(MI_NOOP);
  5489. ADVANCE_LP_RING();
  5490. }
  5491. work->pending_flip_obj = obj;
  5492. work->enable_stall_check = true;
  5493. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  5494. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  5495. ret = BEGIN_LP_RING(4);
  5496. if (ret)
  5497. goto cleanup_objs;
  5498. /* Block clients from rendering to the new back buffer until
  5499. * the flip occurs and the object is no longer visible.
  5500. */
  5501. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5502. switch (INTEL_INFO(dev)->gen) {
  5503. case 2:
  5504. OUT_RING(MI_DISPLAY_FLIP |
  5505. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5506. OUT_RING(fb->pitch);
  5507. OUT_RING(obj->gtt_offset + offset);
  5508. OUT_RING(MI_NOOP);
  5509. break;
  5510. case 3:
  5511. OUT_RING(MI_DISPLAY_FLIP_I915 |
  5512. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5513. OUT_RING(fb->pitch);
  5514. OUT_RING(obj->gtt_offset + offset);
  5515. OUT_RING(MI_NOOP);
  5516. break;
  5517. case 4:
  5518. case 5:
  5519. /* i965+ uses the linear or tiled offsets from the
  5520. * Display Registers (which do not change across a page-flip)
  5521. * so we need only reprogram the base address.
  5522. */
  5523. OUT_RING(MI_DISPLAY_FLIP |
  5524. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5525. OUT_RING(fb->pitch);
  5526. OUT_RING(obj->gtt_offset | obj->tiling_mode);
  5527. /* XXX Enabling the panel-fitter across page-flip is so far
  5528. * untested on non-native modes, so ignore it for now.
  5529. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  5530. */
  5531. pf = 0;
  5532. pipesrc = I915_READ(PIPESRC(pipe)) & 0x0fff0fff;
  5533. OUT_RING(pf | pipesrc);
  5534. break;
  5535. case 6:
  5536. case 7:
  5537. OUT_RING(MI_DISPLAY_FLIP |
  5538. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5539. OUT_RING(fb->pitch | obj->tiling_mode);
  5540. OUT_RING(obj->gtt_offset);
  5541. pf = I915_READ(PF_CTL(pipe)) & PF_ENABLE;
  5542. pipesrc = I915_READ(PIPESRC(pipe)) & 0x0fff0fff;
  5543. OUT_RING(pf | pipesrc);
  5544. break;
  5545. }
  5546. ADVANCE_LP_RING();
  5547. mutex_unlock(&dev->struct_mutex);
  5548. trace_i915_flip_request(intel_crtc->plane, obj);
  5549. return 0;
  5550. cleanup_objs:
  5551. drm_gem_object_unreference(&work->old_fb_obj->base);
  5552. drm_gem_object_unreference(&obj->base);
  5553. cleanup_work:
  5554. mutex_unlock(&dev->struct_mutex);
  5555. spin_lock_irqsave(&dev->event_lock, flags);
  5556. intel_crtc->unpin_work = NULL;
  5557. spin_unlock_irqrestore(&dev->event_lock, flags);
  5558. kfree(work);
  5559. return ret;
  5560. }
  5561. static void intel_sanitize_modesetting(struct drm_device *dev,
  5562. int pipe, int plane)
  5563. {
  5564. struct drm_i915_private *dev_priv = dev->dev_private;
  5565. u32 reg, val;
  5566. if (HAS_PCH_SPLIT(dev))
  5567. return;
  5568. /* Who knows what state these registers were left in by the BIOS or
  5569. * grub?
  5570. *
  5571. * If we leave the registers in a conflicting state (e.g. with the
  5572. * display plane reading from the other pipe than the one we intend
  5573. * to use) then when we attempt to teardown the active mode, we will
  5574. * not disable the pipes and planes in the correct order -- leaving
  5575. * a plane reading from a disabled pipe and possibly leading to
  5576. * undefined behaviour.
  5577. */
  5578. reg = DSPCNTR(plane);
  5579. val = I915_READ(reg);
  5580. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  5581. return;
  5582. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  5583. return;
  5584. /* This display plane is active and attached to the other CPU pipe. */
  5585. pipe = !pipe;
  5586. /* Disable the plane and wait for it to stop reading from the pipe. */
  5587. intel_disable_plane(dev_priv, plane, pipe);
  5588. intel_disable_pipe(dev_priv, pipe);
  5589. }
  5590. static void intel_crtc_reset(struct drm_crtc *crtc)
  5591. {
  5592. struct drm_device *dev = crtc->dev;
  5593. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5594. /* Reset flags back to the 'unknown' status so that they
  5595. * will be correctly set on the initial modeset.
  5596. */
  5597. intel_crtc->dpms_mode = -1;
  5598. /* We need to fix up any BIOS configuration that conflicts with
  5599. * our expectations.
  5600. */
  5601. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  5602. }
  5603. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  5604. .dpms = intel_crtc_dpms,
  5605. .mode_fixup = intel_crtc_mode_fixup,
  5606. .mode_set = intel_crtc_mode_set,
  5607. .mode_set_base = intel_pipe_set_base,
  5608. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  5609. .load_lut = intel_crtc_load_lut,
  5610. .disable = intel_crtc_disable,
  5611. };
  5612. static const struct drm_crtc_funcs intel_crtc_funcs = {
  5613. .reset = intel_crtc_reset,
  5614. .cursor_set = intel_crtc_cursor_set,
  5615. .cursor_move = intel_crtc_cursor_move,
  5616. .gamma_set = intel_crtc_gamma_set,
  5617. .set_config = drm_crtc_helper_set_config,
  5618. .destroy = intel_crtc_destroy,
  5619. .page_flip = intel_crtc_page_flip,
  5620. };
  5621. static void intel_crtc_init(struct drm_device *dev, int pipe)
  5622. {
  5623. drm_i915_private_t *dev_priv = dev->dev_private;
  5624. struct intel_crtc *intel_crtc;
  5625. int i;
  5626. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  5627. if (intel_crtc == NULL)
  5628. return;
  5629. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  5630. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  5631. for (i = 0; i < 256; i++) {
  5632. intel_crtc->lut_r[i] = i;
  5633. intel_crtc->lut_g[i] = i;
  5634. intel_crtc->lut_b[i] = i;
  5635. }
  5636. /* Swap pipes & planes for FBC on pre-965 */
  5637. intel_crtc->pipe = pipe;
  5638. intel_crtc->plane = pipe;
  5639. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  5640. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  5641. intel_crtc->plane = !pipe;
  5642. }
  5643. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  5644. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  5645. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  5646. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  5647. intel_crtc_reset(&intel_crtc->base);
  5648. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  5649. if (HAS_PCH_SPLIT(dev)) {
  5650. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  5651. intel_helper_funcs.commit = ironlake_crtc_commit;
  5652. } else {
  5653. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  5654. intel_helper_funcs.commit = i9xx_crtc_commit;
  5655. }
  5656. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  5657. intel_crtc->busy = false;
  5658. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  5659. (unsigned long)intel_crtc);
  5660. }
  5661. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  5662. struct drm_file *file)
  5663. {
  5664. drm_i915_private_t *dev_priv = dev->dev_private;
  5665. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  5666. struct drm_mode_object *drmmode_obj;
  5667. struct intel_crtc *crtc;
  5668. if (!dev_priv) {
  5669. DRM_ERROR("called with no initialization\n");
  5670. return -EINVAL;
  5671. }
  5672. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  5673. DRM_MODE_OBJECT_CRTC);
  5674. if (!drmmode_obj) {
  5675. DRM_ERROR("no such CRTC id\n");
  5676. return -EINVAL;
  5677. }
  5678. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  5679. pipe_from_crtc_id->pipe = crtc->pipe;
  5680. return 0;
  5681. }
  5682. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  5683. {
  5684. struct intel_encoder *encoder;
  5685. int index_mask = 0;
  5686. int entry = 0;
  5687. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5688. if (type_mask & encoder->clone_mask)
  5689. index_mask |= (1 << entry);
  5690. entry++;
  5691. }
  5692. return index_mask;
  5693. }
  5694. static bool has_edp_a(struct drm_device *dev)
  5695. {
  5696. struct drm_i915_private *dev_priv = dev->dev_private;
  5697. if (!IS_MOBILE(dev))
  5698. return false;
  5699. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  5700. return false;
  5701. if (IS_GEN5(dev) &&
  5702. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  5703. return false;
  5704. return true;
  5705. }
  5706. static void intel_setup_outputs(struct drm_device *dev)
  5707. {
  5708. struct drm_i915_private *dev_priv = dev->dev_private;
  5709. struct intel_encoder *encoder;
  5710. bool dpd_is_edp = false;
  5711. bool has_lvds = false;
  5712. if (IS_MOBILE(dev) && !IS_I830(dev))
  5713. has_lvds = intel_lvds_init(dev);
  5714. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  5715. /* disable the panel fitter on everything but LVDS */
  5716. I915_WRITE(PFIT_CONTROL, 0);
  5717. }
  5718. if (HAS_PCH_SPLIT(dev)) {
  5719. dpd_is_edp = intel_dpd_is_edp(dev);
  5720. if (has_edp_a(dev))
  5721. intel_dp_init(dev, DP_A);
  5722. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5723. intel_dp_init(dev, PCH_DP_D);
  5724. }
  5725. intel_crt_init(dev);
  5726. if (HAS_PCH_SPLIT(dev)) {
  5727. int found;
  5728. if (I915_READ(HDMIB) & PORT_DETECTED) {
  5729. /* PCH SDVOB multiplex with HDMIB */
  5730. found = intel_sdvo_init(dev, PCH_SDVOB);
  5731. if (!found)
  5732. intel_hdmi_init(dev, HDMIB);
  5733. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  5734. intel_dp_init(dev, PCH_DP_B);
  5735. }
  5736. if (I915_READ(HDMIC) & PORT_DETECTED)
  5737. intel_hdmi_init(dev, HDMIC);
  5738. if (I915_READ(HDMID) & PORT_DETECTED)
  5739. intel_hdmi_init(dev, HDMID);
  5740. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  5741. intel_dp_init(dev, PCH_DP_C);
  5742. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5743. intel_dp_init(dev, PCH_DP_D);
  5744. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  5745. bool found = false;
  5746. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5747. DRM_DEBUG_KMS("probing SDVOB\n");
  5748. found = intel_sdvo_init(dev, SDVOB);
  5749. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  5750. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  5751. intel_hdmi_init(dev, SDVOB);
  5752. }
  5753. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  5754. DRM_DEBUG_KMS("probing DP_B\n");
  5755. intel_dp_init(dev, DP_B);
  5756. }
  5757. }
  5758. /* Before G4X SDVOC doesn't have its own detect register */
  5759. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5760. DRM_DEBUG_KMS("probing SDVOC\n");
  5761. found = intel_sdvo_init(dev, SDVOC);
  5762. }
  5763. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  5764. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  5765. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  5766. intel_hdmi_init(dev, SDVOC);
  5767. }
  5768. if (SUPPORTS_INTEGRATED_DP(dev)) {
  5769. DRM_DEBUG_KMS("probing DP_C\n");
  5770. intel_dp_init(dev, DP_C);
  5771. }
  5772. }
  5773. if (SUPPORTS_INTEGRATED_DP(dev) &&
  5774. (I915_READ(DP_D) & DP_DETECTED)) {
  5775. DRM_DEBUG_KMS("probing DP_D\n");
  5776. intel_dp_init(dev, DP_D);
  5777. }
  5778. } else if (IS_GEN2(dev))
  5779. intel_dvo_init(dev);
  5780. if (SUPPORTS_TV(dev))
  5781. intel_tv_init(dev);
  5782. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5783. encoder->base.possible_crtcs = encoder->crtc_mask;
  5784. encoder->base.possible_clones =
  5785. intel_encoder_clones(dev, encoder->clone_mask);
  5786. }
  5787. intel_panel_setup_backlight(dev);
  5788. /* disable all the possible outputs/crtcs before entering KMS mode */
  5789. drm_helper_disable_unused_functions(dev);
  5790. }
  5791. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  5792. {
  5793. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5794. drm_framebuffer_cleanup(fb);
  5795. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  5796. kfree(intel_fb);
  5797. }
  5798. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  5799. struct drm_file *file,
  5800. unsigned int *handle)
  5801. {
  5802. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5803. struct drm_i915_gem_object *obj = intel_fb->obj;
  5804. return drm_gem_handle_create(file, &obj->base, handle);
  5805. }
  5806. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  5807. .destroy = intel_user_framebuffer_destroy,
  5808. .create_handle = intel_user_framebuffer_create_handle,
  5809. };
  5810. int intel_framebuffer_init(struct drm_device *dev,
  5811. struct intel_framebuffer *intel_fb,
  5812. struct drm_mode_fb_cmd *mode_cmd,
  5813. struct drm_i915_gem_object *obj)
  5814. {
  5815. int ret;
  5816. if (obj->tiling_mode == I915_TILING_Y)
  5817. return -EINVAL;
  5818. if (mode_cmd->pitch & 63)
  5819. return -EINVAL;
  5820. switch (mode_cmd->bpp) {
  5821. case 8:
  5822. case 16:
  5823. case 24:
  5824. case 32:
  5825. break;
  5826. default:
  5827. return -EINVAL;
  5828. }
  5829. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  5830. if (ret) {
  5831. DRM_ERROR("framebuffer init failed %d\n", ret);
  5832. return ret;
  5833. }
  5834. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  5835. intel_fb->obj = obj;
  5836. return 0;
  5837. }
  5838. static struct drm_framebuffer *
  5839. intel_user_framebuffer_create(struct drm_device *dev,
  5840. struct drm_file *filp,
  5841. struct drm_mode_fb_cmd *mode_cmd)
  5842. {
  5843. struct drm_i915_gem_object *obj;
  5844. obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
  5845. if (&obj->base == NULL)
  5846. return ERR_PTR(-ENOENT);
  5847. return intel_framebuffer_create(dev, mode_cmd, obj);
  5848. }
  5849. static const struct drm_mode_config_funcs intel_mode_funcs = {
  5850. .fb_create = intel_user_framebuffer_create,
  5851. .output_poll_changed = intel_fb_output_poll_changed,
  5852. };
  5853. static struct drm_i915_gem_object *
  5854. intel_alloc_context_page(struct drm_device *dev)
  5855. {
  5856. struct drm_i915_gem_object *ctx;
  5857. int ret;
  5858. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  5859. ctx = i915_gem_alloc_object(dev, 4096);
  5860. if (!ctx) {
  5861. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  5862. return NULL;
  5863. }
  5864. ret = i915_gem_object_pin(ctx, 4096, true);
  5865. if (ret) {
  5866. DRM_ERROR("failed to pin power context: %d\n", ret);
  5867. goto err_unref;
  5868. }
  5869. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  5870. if (ret) {
  5871. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  5872. goto err_unpin;
  5873. }
  5874. return ctx;
  5875. err_unpin:
  5876. i915_gem_object_unpin(ctx);
  5877. err_unref:
  5878. drm_gem_object_unreference(&ctx->base);
  5879. mutex_unlock(&dev->struct_mutex);
  5880. return NULL;
  5881. }
  5882. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  5883. {
  5884. struct drm_i915_private *dev_priv = dev->dev_private;
  5885. u16 rgvswctl;
  5886. rgvswctl = I915_READ16(MEMSWCTL);
  5887. if (rgvswctl & MEMCTL_CMD_STS) {
  5888. DRM_DEBUG("gpu busy, RCS change rejected\n");
  5889. return false; /* still busy with another command */
  5890. }
  5891. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  5892. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  5893. I915_WRITE16(MEMSWCTL, rgvswctl);
  5894. POSTING_READ16(MEMSWCTL);
  5895. rgvswctl |= MEMCTL_CMD_STS;
  5896. I915_WRITE16(MEMSWCTL, rgvswctl);
  5897. return true;
  5898. }
  5899. void ironlake_enable_drps(struct drm_device *dev)
  5900. {
  5901. struct drm_i915_private *dev_priv = dev->dev_private;
  5902. u32 rgvmodectl = I915_READ(MEMMODECTL);
  5903. u8 fmax, fmin, fstart, vstart;
  5904. /* Enable temp reporting */
  5905. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  5906. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  5907. /* 100ms RC evaluation intervals */
  5908. I915_WRITE(RCUPEI, 100000);
  5909. I915_WRITE(RCDNEI, 100000);
  5910. /* Set max/min thresholds to 90ms and 80ms respectively */
  5911. I915_WRITE(RCBMAXAVG, 90000);
  5912. I915_WRITE(RCBMINAVG, 80000);
  5913. I915_WRITE(MEMIHYST, 1);
  5914. /* Set up min, max, and cur for interrupt handling */
  5915. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  5916. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  5917. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  5918. MEMMODE_FSTART_SHIFT;
  5919. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  5920. PXVFREQ_PX_SHIFT;
  5921. dev_priv->fmax = fmax; /* IPS callback will increase this */
  5922. dev_priv->fstart = fstart;
  5923. dev_priv->max_delay = fstart;
  5924. dev_priv->min_delay = fmin;
  5925. dev_priv->cur_delay = fstart;
  5926. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  5927. fmax, fmin, fstart);
  5928. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  5929. /*
  5930. * Interrupts will be enabled in ironlake_irq_postinstall
  5931. */
  5932. I915_WRITE(VIDSTART, vstart);
  5933. POSTING_READ(VIDSTART);
  5934. rgvmodectl |= MEMMODE_SWMODE_EN;
  5935. I915_WRITE(MEMMODECTL, rgvmodectl);
  5936. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  5937. DRM_ERROR("stuck trying to change perf mode\n");
  5938. msleep(1);
  5939. ironlake_set_drps(dev, fstart);
  5940. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  5941. I915_READ(0x112e0);
  5942. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  5943. dev_priv->last_count2 = I915_READ(0x112f4);
  5944. getrawmonotonic(&dev_priv->last_time2);
  5945. }
  5946. void ironlake_disable_drps(struct drm_device *dev)
  5947. {
  5948. struct drm_i915_private *dev_priv = dev->dev_private;
  5949. u16 rgvswctl = I915_READ16(MEMSWCTL);
  5950. /* Ack interrupts, disable EFC interrupt */
  5951. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  5952. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  5953. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  5954. I915_WRITE(DEIIR, DE_PCU_EVENT);
  5955. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  5956. /* Go back to the starting frequency */
  5957. ironlake_set_drps(dev, dev_priv->fstart);
  5958. msleep(1);
  5959. rgvswctl |= MEMCTL_CMD_STS;
  5960. I915_WRITE(MEMSWCTL, rgvswctl);
  5961. msleep(1);
  5962. }
  5963. void gen6_set_rps(struct drm_device *dev, u8 val)
  5964. {
  5965. struct drm_i915_private *dev_priv = dev->dev_private;
  5966. u32 swreq;
  5967. swreq = (val & 0x3ff) << 25;
  5968. I915_WRITE(GEN6_RPNSWREQ, swreq);
  5969. }
  5970. void gen6_disable_rps(struct drm_device *dev)
  5971. {
  5972. struct drm_i915_private *dev_priv = dev->dev_private;
  5973. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  5974. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  5975. I915_WRITE(GEN6_PMIER, 0);
  5976. spin_lock_irq(&dev_priv->rps_lock);
  5977. dev_priv->pm_iir = 0;
  5978. spin_unlock_irq(&dev_priv->rps_lock);
  5979. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  5980. }
  5981. static unsigned long intel_pxfreq(u32 vidfreq)
  5982. {
  5983. unsigned long freq;
  5984. int div = (vidfreq & 0x3f0000) >> 16;
  5985. int post = (vidfreq & 0x3000) >> 12;
  5986. int pre = (vidfreq & 0x7);
  5987. if (!pre)
  5988. return 0;
  5989. freq = ((div * 133333) / ((1<<post) * pre));
  5990. return freq;
  5991. }
  5992. void intel_init_emon(struct drm_device *dev)
  5993. {
  5994. struct drm_i915_private *dev_priv = dev->dev_private;
  5995. u32 lcfuse;
  5996. u8 pxw[16];
  5997. int i;
  5998. /* Disable to program */
  5999. I915_WRITE(ECR, 0);
  6000. POSTING_READ(ECR);
  6001. /* Program energy weights for various events */
  6002. I915_WRITE(SDEW, 0x15040d00);
  6003. I915_WRITE(CSIEW0, 0x007f0000);
  6004. I915_WRITE(CSIEW1, 0x1e220004);
  6005. I915_WRITE(CSIEW2, 0x04000004);
  6006. for (i = 0; i < 5; i++)
  6007. I915_WRITE(PEW + (i * 4), 0);
  6008. for (i = 0; i < 3; i++)
  6009. I915_WRITE(DEW + (i * 4), 0);
  6010. /* Program P-state weights to account for frequency power adjustment */
  6011. for (i = 0; i < 16; i++) {
  6012. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  6013. unsigned long freq = intel_pxfreq(pxvidfreq);
  6014. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  6015. PXVFREQ_PX_SHIFT;
  6016. unsigned long val;
  6017. val = vid * vid;
  6018. val *= (freq / 1000);
  6019. val *= 255;
  6020. val /= (127*127*900);
  6021. if (val > 0xff)
  6022. DRM_ERROR("bad pxval: %ld\n", val);
  6023. pxw[i] = val;
  6024. }
  6025. /* Render standby states get 0 weight */
  6026. pxw[14] = 0;
  6027. pxw[15] = 0;
  6028. for (i = 0; i < 4; i++) {
  6029. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  6030. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  6031. I915_WRITE(PXW + (i * 4), val);
  6032. }
  6033. /* Adjust magic regs to magic values (more experimental results) */
  6034. I915_WRITE(OGW0, 0);
  6035. I915_WRITE(OGW1, 0);
  6036. I915_WRITE(EG0, 0x00007f00);
  6037. I915_WRITE(EG1, 0x0000000e);
  6038. I915_WRITE(EG2, 0x000e0000);
  6039. I915_WRITE(EG3, 0x68000300);
  6040. I915_WRITE(EG4, 0x42000000);
  6041. I915_WRITE(EG5, 0x00140031);
  6042. I915_WRITE(EG6, 0);
  6043. I915_WRITE(EG7, 0);
  6044. for (i = 0; i < 8; i++)
  6045. I915_WRITE(PXWL + (i * 4), 0);
  6046. /* Enable PMON + select events */
  6047. I915_WRITE(ECR, 0x80000019);
  6048. lcfuse = I915_READ(LCFUSE02);
  6049. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  6050. }
  6051. void gen6_enable_rps(struct drm_i915_private *dev_priv)
  6052. {
  6053. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  6054. u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  6055. u32 pcu_mbox, rc6_mask = 0;
  6056. int cur_freq, min_freq, max_freq;
  6057. int i;
  6058. /* Here begins a magic sequence of register writes to enable
  6059. * auto-downclocking.
  6060. *
  6061. * Perhaps there might be some value in exposing these to
  6062. * userspace...
  6063. */
  6064. I915_WRITE(GEN6_RC_STATE, 0);
  6065. mutex_lock(&dev_priv->dev->struct_mutex);
  6066. gen6_gt_force_wake_get(dev_priv);
  6067. /* disable the counters and set deterministic thresholds */
  6068. I915_WRITE(GEN6_RC_CONTROL, 0);
  6069. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  6070. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  6071. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  6072. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  6073. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  6074. for (i = 0; i < I915_NUM_RINGS; i++)
  6075. I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
  6076. I915_WRITE(GEN6_RC_SLEEP, 0);
  6077. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  6078. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  6079. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  6080. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  6081. if (i915_enable_rc6)
  6082. rc6_mask = GEN6_RC_CTL_RC6p_ENABLE |
  6083. GEN6_RC_CTL_RC6_ENABLE;
  6084. I915_WRITE(GEN6_RC_CONTROL,
  6085. rc6_mask |
  6086. GEN6_RC_CTL_EI_MODE(1) |
  6087. GEN6_RC_CTL_HW_ENABLE);
  6088. I915_WRITE(GEN6_RPNSWREQ,
  6089. GEN6_FREQUENCY(10) |
  6090. GEN6_OFFSET(0) |
  6091. GEN6_AGGRESSIVE_TURBO);
  6092. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  6093. GEN6_FREQUENCY(12));
  6094. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  6095. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  6096. 18 << 24 |
  6097. 6 << 16);
  6098. I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
  6099. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
  6100. I915_WRITE(GEN6_RP_UP_EI, 100000);
  6101. I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
  6102. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  6103. I915_WRITE(GEN6_RP_CONTROL,
  6104. GEN6_RP_MEDIA_TURBO |
  6105. GEN6_RP_USE_NORMAL_FREQ |
  6106. GEN6_RP_MEDIA_IS_GFX |
  6107. GEN6_RP_ENABLE |
  6108. GEN6_RP_UP_BUSY_AVG |
  6109. GEN6_RP_DOWN_IDLE_CONT);
  6110. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6111. 500))
  6112. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  6113. I915_WRITE(GEN6_PCODE_DATA, 0);
  6114. I915_WRITE(GEN6_PCODE_MAILBOX,
  6115. GEN6_PCODE_READY |
  6116. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  6117. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6118. 500))
  6119. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  6120. min_freq = (rp_state_cap & 0xff0000) >> 16;
  6121. max_freq = rp_state_cap & 0xff;
  6122. cur_freq = (gt_perf_status & 0xff00) >> 8;
  6123. /* Check for overclock support */
  6124. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6125. 500))
  6126. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  6127. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
  6128. pcu_mbox = I915_READ(GEN6_PCODE_DATA);
  6129. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6130. 500))
  6131. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  6132. if (pcu_mbox & (1<<31)) { /* OC supported */
  6133. max_freq = pcu_mbox & 0xff;
  6134. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
  6135. }
  6136. /* In units of 100MHz */
  6137. dev_priv->max_delay = max_freq;
  6138. dev_priv->min_delay = min_freq;
  6139. dev_priv->cur_delay = cur_freq;
  6140. /* requires MSI enabled */
  6141. I915_WRITE(GEN6_PMIER,
  6142. GEN6_PM_MBOX_EVENT |
  6143. GEN6_PM_THERMAL_EVENT |
  6144. GEN6_PM_RP_DOWN_TIMEOUT |
  6145. GEN6_PM_RP_UP_THRESHOLD |
  6146. GEN6_PM_RP_DOWN_THRESHOLD |
  6147. GEN6_PM_RP_UP_EI_EXPIRED |
  6148. GEN6_PM_RP_DOWN_EI_EXPIRED);
  6149. spin_lock_irq(&dev_priv->rps_lock);
  6150. WARN_ON(dev_priv->pm_iir != 0);
  6151. I915_WRITE(GEN6_PMIMR, 0);
  6152. spin_unlock_irq(&dev_priv->rps_lock);
  6153. /* enable all PM interrupts */
  6154. I915_WRITE(GEN6_PMINTRMSK, 0);
  6155. gen6_gt_force_wake_put(dev_priv);
  6156. mutex_unlock(&dev_priv->dev->struct_mutex);
  6157. }
  6158. static void ironlake_init_clock_gating(struct drm_device *dev)
  6159. {
  6160. struct drm_i915_private *dev_priv = dev->dev_private;
  6161. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6162. /* Required for FBC */
  6163. dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
  6164. DPFCRUNIT_CLOCK_GATE_DISABLE |
  6165. DPFDUNIT_CLOCK_GATE_DISABLE;
  6166. /* Required for CxSR */
  6167. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  6168. I915_WRITE(PCH_3DCGDIS0,
  6169. MARIUNIT_CLOCK_GATE_DISABLE |
  6170. SVSMUNIT_CLOCK_GATE_DISABLE);
  6171. I915_WRITE(PCH_3DCGDIS1,
  6172. VFMUNIT_CLOCK_GATE_DISABLE);
  6173. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6174. /*
  6175. * According to the spec the following bits should be set in
  6176. * order to enable memory self-refresh
  6177. * The bit 22/21 of 0x42004
  6178. * The bit 5 of 0x42020
  6179. * The bit 15 of 0x45000
  6180. */
  6181. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6182. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  6183. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  6184. I915_WRITE(ILK_DSPCLK_GATE,
  6185. (I915_READ(ILK_DSPCLK_GATE) |
  6186. ILK_DPARB_CLK_GATE));
  6187. I915_WRITE(DISP_ARB_CTL,
  6188. (I915_READ(DISP_ARB_CTL) |
  6189. DISP_FBC_WM_DIS));
  6190. I915_WRITE(WM3_LP_ILK, 0);
  6191. I915_WRITE(WM2_LP_ILK, 0);
  6192. I915_WRITE(WM1_LP_ILK, 0);
  6193. /*
  6194. * Based on the document from hardware guys the following bits
  6195. * should be set unconditionally in order to enable FBC.
  6196. * The bit 22 of 0x42000
  6197. * The bit 22 of 0x42004
  6198. * The bit 7,8,9 of 0x42020.
  6199. */
  6200. if (IS_IRONLAKE_M(dev)) {
  6201. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  6202. I915_READ(ILK_DISPLAY_CHICKEN1) |
  6203. ILK_FBCQ_DIS);
  6204. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6205. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6206. ILK_DPARB_GATE);
  6207. I915_WRITE(ILK_DSPCLK_GATE,
  6208. I915_READ(ILK_DSPCLK_GATE) |
  6209. ILK_DPFC_DIS1 |
  6210. ILK_DPFC_DIS2 |
  6211. ILK_CLK_FBC);
  6212. }
  6213. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6214. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6215. ILK_ELPIN_409_SELECT);
  6216. I915_WRITE(_3D_CHICKEN2,
  6217. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  6218. _3D_CHICKEN2_WM_READ_PIPELINED);
  6219. }
  6220. static void gen6_init_clock_gating(struct drm_device *dev)
  6221. {
  6222. struct drm_i915_private *dev_priv = dev->dev_private;
  6223. int pipe;
  6224. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6225. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6226. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6227. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6228. ILK_ELPIN_409_SELECT);
  6229. I915_WRITE(WM3_LP_ILK, 0);
  6230. I915_WRITE(WM2_LP_ILK, 0);
  6231. I915_WRITE(WM1_LP_ILK, 0);
  6232. /*
  6233. * According to the spec the following bits should be
  6234. * set in order to enable memory self-refresh and fbc:
  6235. * The bit21 and bit22 of 0x42000
  6236. * The bit21 and bit22 of 0x42004
  6237. * The bit5 and bit7 of 0x42020
  6238. * The bit14 of 0x70180
  6239. * The bit14 of 0x71180
  6240. */
  6241. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  6242. I915_READ(ILK_DISPLAY_CHICKEN1) |
  6243. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  6244. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6245. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6246. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  6247. I915_WRITE(ILK_DSPCLK_GATE,
  6248. I915_READ(ILK_DSPCLK_GATE) |
  6249. ILK_DPARB_CLK_GATE |
  6250. ILK_DPFD_CLK_GATE);
  6251. for_each_pipe(pipe)
  6252. I915_WRITE(DSPCNTR(pipe),
  6253. I915_READ(DSPCNTR(pipe)) |
  6254. DISPPLANE_TRICKLE_FEED_DISABLE);
  6255. }
  6256. static void ivybridge_init_clock_gating(struct drm_device *dev)
  6257. {
  6258. struct drm_i915_private *dev_priv = dev->dev_private;
  6259. int pipe;
  6260. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6261. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6262. I915_WRITE(WM3_LP_ILK, 0);
  6263. I915_WRITE(WM2_LP_ILK, 0);
  6264. I915_WRITE(WM1_LP_ILK, 0);
  6265. I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
  6266. for_each_pipe(pipe)
  6267. I915_WRITE(DSPCNTR(pipe),
  6268. I915_READ(DSPCNTR(pipe)) |
  6269. DISPPLANE_TRICKLE_FEED_DISABLE);
  6270. }
  6271. static void g4x_init_clock_gating(struct drm_device *dev)
  6272. {
  6273. struct drm_i915_private *dev_priv = dev->dev_private;
  6274. uint32_t dspclk_gate;
  6275. I915_WRITE(RENCLK_GATE_D1, 0);
  6276. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  6277. GS_UNIT_CLOCK_GATE_DISABLE |
  6278. CL_UNIT_CLOCK_GATE_DISABLE);
  6279. I915_WRITE(RAMCLK_GATE_D, 0);
  6280. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  6281. OVRUNIT_CLOCK_GATE_DISABLE |
  6282. OVCUNIT_CLOCK_GATE_DISABLE;
  6283. if (IS_GM45(dev))
  6284. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  6285. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  6286. }
  6287. static void crestline_init_clock_gating(struct drm_device *dev)
  6288. {
  6289. struct drm_i915_private *dev_priv = dev->dev_private;
  6290. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  6291. I915_WRITE(RENCLK_GATE_D2, 0);
  6292. I915_WRITE(DSPCLK_GATE_D, 0);
  6293. I915_WRITE(RAMCLK_GATE_D, 0);
  6294. I915_WRITE16(DEUC, 0);
  6295. }
  6296. static void broadwater_init_clock_gating(struct drm_device *dev)
  6297. {
  6298. struct drm_i915_private *dev_priv = dev->dev_private;
  6299. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  6300. I965_RCC_CLOCK_GATE_DISABLE |
  6301. I965_RCPB_CLOCK_GATE_DISABLE |
  6302. I965_ISC_CLOCK_GATE_DISABLE |
  6303. I965_FBC_CLOCK_GATE_DISABLE);
  6304. I915_WRITE(RENCLK_GATE_D2, 0);
  6305. }
  6306. static void gen3_init_clock_gating(struct drm_device *dev)
  6307. {
  6308. struct drm_i915_private *dev_priv = dev->dev_private;
  6309. u32 dstate = I915_READ(D_STATE);
  6310. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  6311. DSTATE_DOT_CLOCK_GATING;
  6312. I915_WRITE(D_STATE, dstate);
  6313. }
  6314. static void i85x_init_clock_gating(struct drm_device *dev)
  6315. {
  6316. struct drm_i915_private *dev_priv = dev->dev_private;
  6317. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  6318. }
  6319. static void i830_init_clock_gating(struct drm_device *dev)
  6320. {
  6321. struct drm_i915_private *dev_priv = dev->dev_private;
  6322. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  6323. }
  6324. static void ibx_init_clock_gating(struct drm_device *dev)
  6325. {
  6326. struct drm_i915_private *dev_priv = dev->dev_private;
  6327. /*
  6328. * On Ibex Peak and Cougar Point, we need to disable clock
  6329. * gating for the panel power sequencer or it will fail to
  6330. * start up when no ports are active.
  6331. */
  6332. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  6333. }
  6334. static void cpt_init_clock_gating(struct drm_device *dev)
  6335. {
  6336. struct drm_i915_private *dev_priv = dev->dev_private;
  6337. /*
  6338. * On Ibex Peak and Cougar Point, we need to disable clock
  6339. * gating for the panel power sequencer or it will fail to
  6340. * start up when no ports are active.
  6341. */
  6342. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  6343. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  6344. DPLS_EDP_PPS_FIX_DIS);
  6345. }
  6346. static void ironlake_teardown_rc6(struct drm_device *dev)
  6347. {
  6348. struct drm_i915_private *dev_priv = dev->dev_private;
  6349. if (dev_priv->renderctx) {
  6350. i915_gem_object_unpin(dev_priv->renderctx);
  6351. drm_gem_object_unreference(&dev_priv->renderctx->base);
  6352. dev_priv->renderctx = NULL;
  6353. }
  6354. if (dev_priv->pwrctx) {
  6355. i915_gem_object_unpin(dev_priv->pwrctx);
  6356. drm_gem_object_unreference(&dev_priv->pwrctx->base);
  6357. dev_priv->pwrctx = NULL;
  6358. }
  6359. }
  6360. static void ironlake_disable_rc6(struct drm_device *dev)
  6361. {
  6362. struct drm_i915_private *dev_priv = dev->dev_private;
  6363. if (I915_READ(PWRCTXA)) {
  6364. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  6365. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  6366. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  6367. 50);
  6368. I915_WRITE(PWRCTXA, 0);
  6369. POSTING_READ(PWRCTXA);
  6370. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  6371. POSTING_READ(RSTDBYCTL);
  6372. }
  6373. ironlake_teardown_rc6(dev);
  6374. }
  6375. static int ironlake_setup_rc6(struct drm_device *dev)
  6376. {
  6377. struct drm_i915_private *dev_priv = dev->dev_private;
  6378. if (dev_priv->renderctx == NULL)
  6379. dev_priv->renderctx = intel_alloc_context_page(dev);
  6380. if (!dev_priv->renderctx)
  6381. return -ENOMEM;
  6382. if (dev_priv->pwrctx == NULL)
  6383. dev_priv->pwrctx = intel_alloc_context_page(dev);
  6384. if (!dev_priv->pwrctx) {
  6385. ironlake_teardown_rc6(dev);
  6386. return -ENOMEM;
  6387. }
  6388. return 0;
  6389. }
  6390. void ironlake_enable_rc6(struct drm_device *dev)
  6391. {
  6392. struct drm_i915_private *dev_priv = dev->dev_private;
  6393. int ret;
  6394. /* rc6 disabled by default due to repeated reports of hanging during
  6395. * boot and resume.
  6396. */
  6397. if (!i915_enable_rc6)
  6398. return;
  6399. mutex_lock(&dev->struct_mutex);
  6400. ret = ironlake_setup_rc6(dev);
  6401. if (ret) {
  6402. mutex_unlock(&dev->struct_mutex);
  6403. return;
  6404. }
  6405. /*
  6406. * GPU can automatically power down the render unit if given a page
  6407. * to save state.
  6408. */
  6409. ret = BEGIN_LP_RING(6);
  6410. if (ret) {
  6411. ironlake_teardown_rc6(dev);
  6412. mutex_unlock(&dev->struct_mutex);
  6413. return;
  6414. }
  6415. OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  6416. OUT_RING(MI_SET_CONTEXT);
  6417. OUT_RING(dev_priv->renderctx->gtt_offset |
  6418. MI_MM_SPACE_GTT |
  6419. MI_SAVE_EXT_STATE_EN |
  6420. MI_RESTORE_EXT_STATE_EN |
  6421. MI_RESTORE_INHIBIT);
  6422. OUT_RING(MI_SUSPEND_FLUSH);
  6423. OUT_RING(MI_NOOP);
  6424. OUT_RING(MI_FLUSH);
  6425. ADVANCE_LP_RING();
  6426. /*
  6427. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  6428. * does an implicit flush, combined with MI_FLUSH above, it should be
  6429. * safe to assume that renderctx is valid
  6430. */
  6431. ret = intel_wait_ring_idle(LP_RING(dev_priv));
  6432. if (ret) {
  6433. DRM_ERROR("failed to enable ironlake power power savings\n");
  6434. ironlake_teardown_rc6(dev);
  6435. mutex_unlock(&dev->struct_mutex);
  6436. return;
  6437. }
  6438. I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
  6439. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  6440. mutex_unlock(&dev->struct_mutex);
  6441. }
  6442. void intel_init_clock_gating(struct drm_device *dev)
  6443. {
  6444. struct drm_i915_private *dev_priv = dev->dev_private;
  6445. dev_priv->display.init_clock_gating(dev);
  6446. if (dev_priv->display.init_pch_clock_gating)
  6447. dev_priv->display.init_pch_clock_gating(dev);
  6448. }
  6449. /* Set up chip specific display functions */
  6450. static void intel_init_display(struct drm_device *dev)
  6451. {
  6452. struct drm_i915_private *dev_priv = dev->dev_private;
  6453. /* We always want a DPMS function */
  6454. if (HAS_PCH_SPLIT(dev)) {
  6455. dev_priv->display.dpms = ironlake_crtc_dpms;
  6456. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  6457. } else {
  6458. dev_priv->display.dpms = i9xx_crtc_dpms;
  6459. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  6460. }
  6461. if (I915_HAS_FBC(dev)) {
  6462. if (HAS_PCH_SPLIT(dev)) {
  6463. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  6464. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  6465. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  6466. } else if (IS_GM45(dev)) {
  6467. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  6468. dev_priv->display.enable_fbc = g4x_enable_fbc;
  6469. dev_priv->display.disable_fbc = g4x_disable_fbc;
  6470. } else if (IS_CRESTLINE(dev)) {
  6471. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  6472. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  6473. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  6474. }
  6475. /* 855GM needs testing */
  6476. }
  6477. /* Returns the core display clock speed */
  6478. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  6479. dev_priv->display.get_display_clock_speed =
  6480. i945_get_display_clock_speed;
  6481. else if (IS_I915G(dev))
  6482. dev_priv->display.get_display_clock_speed =
  6483. i915_get_display_clock_speed;
  6484. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  6485. dev_priv->display.get_display_clock_speed =
  6486. i9xx_misc_get_display_clock_speed;
  6487. else if (IS_I915GM(dev))
  6488. dev_priv->display.get_display_clock_speed =
  6489. i915gm_get_display_clock_speed;
  6490. else if (IS_I865G(dev))
  6491. dev_priv->display.get_display_clock_speed =
  6492. i865_get_display_clock_speed;
  6493. else if (IS_I85X(dev))
  6494. dev_priv->display.get_display_clock_speed =
  6495. i855_get_display_clock_speed;
  6496. else /* 852, 830 */
  6497. dev_priv->display.get_display_clock_speed =
  6498. i830_get_display_clock_speed;
  6499. /* For FIFO watermark updates */
  6500. if (HAS_PCH_SPLIT(dev)) {
  6501. if (HAS_PCH_IBX(dev))
  6502. dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
  6503. else if (HAS_PCH_CPT(dev))
  6504. dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
  6505. if (IS_GEN5(dev)) {
  6506. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  6507. dev_priv->display.update_wm = ironlake_update_wm;
  6508. else {
  6509. DRM_DEBUG_KMS("Failed to get proper latency. "
  6510. "Disable CxSR\n");
  6511. dev_priv->display.update_wm = NULL;
  6512. }
  6513. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  6514. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  6515. } else if (IS_GEN6(dev)) {
  6516. if (SNB_READ_WM0_LATENCY()) {
  6517. dev_priv->display.update_wm = sandybridge_update_wm;
  6518. } else {
  6519. DRM_DEBUG_KMS("Failed to read display plane latency. "
  6520. "Disable CxSR\n");
  6521. dev_priv->display.update_wm = NULL;
  6522. }
  6523. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  6524. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  6525. } else if (IS_IVYBRIDGE(dev)) {
  6526. /* FIXME: detect B0+ stepping and use auto training */
  6527. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  6528. if (SNB_READ_WM0_LATENCY()) {
  6529. dev_priv->display.update_wm = sandybridge_update_wm;
  6530. } else {
  6531. DRM_DEBUG_KMS("Failed to read display plane latency. "
  6532. "Disable CxSR\n");
  6533. dev_priv->display.update_wm = NULL;
  6534. }
  6535. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  6536. } else
  6537. dev_priv->display.update_wm = NULL;
  6538. } else if (IS_PINEVIEW(dev)) {
  6539. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  6540. dev_priv->is_ddr3,
  6541. dev_priv->fsb_freq,
  6542. dev_priv->mem_freq)) {
  6543. DRM_INFO("failed to find known CxSR latency "
  6544. "(found ddr%s fsb freq %d, mem freq %d), "
  6545. "disabling CxSR\n",
  6546. (dev_priv->is_ddr3 == 1) ? "3": "2",
  6547. dev_priv->fsb_freq, dev_priv->mem_freq);
  6548. /* Disable CxSR and never update its watermark again */
  6549. pineview_disable_cxsr(dev);
  6550. dev_priv->display.update_wm = NULL;
  6551. } else
  6552. dev_priv->display.update_wm = pineview_update_wm;
  6553. } else if (IS_G4X(dev)) {
  6554. dev_priv->display.update_wm = g4x_update_wm;
  6555. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  6556. } else if (IS_GEN4(dev)) {
  6557. dev_priv->display.update_wm = i965_update_wm;
  6558. if (IS_CRESTLINE(dev))
  6559. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  6560. else if (IS_BROADWATER(dev))
  6561. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  6562. } else if (IS_GEN3(dev)) {
  6563. dev_priv->display.update_wm = i9xx_update_wm;
  6564. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  6565. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  6566. } else if (IS_I865G(dev)) {
  6567. dev_priv->display.update_wm = i830_update_wm;
  6568. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  6569. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  6570. } else if (IS_I85X(dev)) {
  6571. dev_priv->display.update_wm = i9xx_update_wm;
  6572. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  6573. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  6574. } else {
  6575. dev_priv->display.update_wm = i830_update_wm;
  6576. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  6577. if (IS_845G(dev))
  6578. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  6579. else
  6580. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  6581. }
  6582. }
  6583. /*
  6584. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  6585. * resume, or other times. This quirk makes sure that's the case for
  6586. * affected systems.
  6587. */
  6588. static void quirk_pipea_force (struct drm_device *dev)
  6589. {
  6590. struct drm_i915_private *dev_priv = dev->dev_private;
  6591. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  6592. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  6593. }
  6594. struct intel_quirk {
  6595. int device;
  6596. int subsystem_vendor;
  6597. int subsystem_device;
  6598. void (*hook)(struct drm_device *dev);
  6599. };
  6600. struct intel_quirk intel_quirks[] = {
  6601. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  6602. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  6603. /* HP Mini needs pipe A force quirk (LP: #322104) */
  6604. { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
  6605. /* Thinkpad R31 needs pipe A force quirk */
  6606. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  6607. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  6608. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  6609. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  6610. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  6611. /* ThinkPad X40 needs pipe A force quirk */
  6612. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  6613. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  6614. /* 855 & before need to leave pipe A & dpll A up */
  6615. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  6616. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  6617. };
  6618. static void intel_init_quirks(struct drm_device *dev)
  6619. {
  6620. struct pci_dev *d = dev->pdev;
  6621. int i;
  6622. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  6623. struct intel_quirk *q = &intel_quirks[i];
  6624. if (d->device == q->device &&
  6625. (d->subsystem_vendor == q->subsystem_vendor ||
  6626. q->subsystem_vendor == PCI_ANY_ID) &&
  6627. (d->subsystem_device == q->subsystem_device ||
  6628. q->subsystem_device == PCI_ANY_ID))
  6629. q->hook(dev);
  6630. }
  6631. }
  6632. /* Disable the VGA plane that we never use */
  6633. static void i915_disable_vga(struct drm_device *dev)
  6634. {
  6635. struct drm_i915_private *dev_priv = dev->dev_private;
  6636. u8 sr1;
  6637. u32 vga_reg;
  6638. if (HAS_PCH_SPLIT(dev))
  6639. vga_reg = CPU_VGACNTRL;
  6640. else
  6641. vga_reg = VGACNTRL;
  6642. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  6643. outb(1, VGA_SR_INDEX);
  6644. sr1 = inb(VGA_SR_DATA);
  6645. outb(sr1 | 1<<5, VGA_SR_DATA);
  6646. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  6647. udelay(300);
  6648. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  6649. POSTING_READ(vga_reg);
  6650. }
  6651. void intel_modeset_init(struct drm_device *dev)
  6652. {
  6653. struct drm_i915_private *dev_priv = dev->dev_private;
  6654. int i;
  6655. drm_mode_config_init(dev);
  6656. dev->mode_config.min_width = 0;
  6657. dev->mode_config.min_height = 0;
  6658. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  6659. intel_init_quirks(dev);
  6660. intel_init_display(dev);
  6661. if (IS_GEN2(dev)) {
  6662. dev->mode_config.max_width = 2048;
  6663. dev->mode_config.max_height = 2048;
  6664. } else if (IS_GEN3(dev)) {
  6665. dev->mode_config.max_width = 4096;
  6666. dev->mode_config.max_height = 4096;
  6667. } else {
  6668. dev->mode_config.max_width = 8192;
  6669. dev->mode_config.max_height = 8192;
  6670. }
  6671. dev->mode_config.fb_base = dev->agp->base;
  6672. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  6673. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  6674. for (i = 0; i < dev_priv->num_pipe; i++) {
  6675. intel_crtc_init(dev, i);
  6676. }
  6677. /* Just disable it once at startup */
  6678. i915_disable_vga(dev);
  6679. intel_setup_outputs(dev);
  6680. intel_init_clock_gating(dev);
  6681. if (IS_IRONLAKE_M(dev)) {
  6682. ironlake_enable_drps(dev);
  6683. intel_init_emon(dev);
  6684. }
  6685. if (IS_GEN6(dev))
  6686. gen6_enable_rps(dev_priv);
  6687. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  6688. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  6689. (unsigned long)dev);
  6690. }
  6691. void intel_modeset_gem_init(struct drm_device *dev)
  6692. {
  6693. if (IS_IRONLAKE_M(dev))
  6694. ironlake_enable_rc6(dev);
  6695. intel_setup_overlay(dev);
  6696. }
  6697. void intel_modeset_cleanup(struct drm_device *dev)
  6698. {
  6699. struct drm_i915_private *dev_priv = dev->dev_private;
  6700. struct drm_crtc *crtc;
  6701. struct intel_crtc *intel_crtc;
  6702. drm_kms_helper_poll_fini(dev);
  6703. mutex_lock(&dev->struct_mutex);
  6704. intel_unregister_dsm_handler();
  6705. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6706. /* Skip inactive CRTCs */
  6707. if (!crtc->fb)
  6708. continue;
  6709. intel_crtc = to_intel_crtc(crtc);
  6710. intel_increase_pllclock(crtc);
  6711. }
  6712. if (dev_priv->display.disable_fbc)
  6713. dev_priv->display.disable_fbc(dev);
  6714. if (IS_IRONLAKE_M(dev))
  6715. ironlake_disable_drps(dev);
  6716. if (IS_GEN6(dev))
  6717. gen6_disable_rps(dev);
  6718. if (IS_IRONLAKE_M(dev))
  6719. ironlake_disable_rc6(dev);
  6720. mutex_unlock(&dev->struct_mutex);
  6721. /* Disable the irq before mode object teardown, for the irq might
  6722. * enqueue unpin/hotplug work. */
  6723. drm_irq_uninstall(dev);
  6724. cancel_work_sync(&dev_priv->hotplug_work);
  6725. /* Shut off idle work before the crtcs get freed. */
  6726. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6727. intel_crtc = to_intel_crtc(crtc);
  6728. del_timer_sync(&intel_crtc->idle_timer);
  6729. }
  6730. del_timer_sync(&dev_priv->idle_timer);
  6731. cancel_work_sync(&dev_priv->idle_work);
  6732. drm_mode_config_cleanup(dev);
  6733. }
  6734. /*
  6735. * Return which encoder is currently attached for connector.
  6736. */
  6737. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  6738. {
  6739. return &intel_attached_encoder(connector)->base;
  6740. }
  6741. void intel_connector_attach_encoder(struct intel_connector *connector,
  6742. struct intel_encoder *encoder)
  6743. {
  6744. connector->encoder = encoder;
  6745. drm_mode_connector_attach_encoder(&connector->base,
  6746. &encoder->base);
  6747. }
  6748. /*
  6749. * set vga decode state - true == enable VGA decode
  6750. */
  6751. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  6752. {
  6753. struct drm_i915_private *dev_priv = dev->dev_private;
  6754. u16 gmch_ctrl;
  6755. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  6756. if (state)
  6757. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  6758. else
  6759. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  6760. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  6761. return 0;
  6762. }
  6763. #ifdef CONFIG_DEBUG_FS
  6764. #include <linux/seq_file.h>
  6765. struct intel_display_error_state {
  6766. struct intel_cursor_error_state {
  6767. u32 control;
  6768. u32 position;
  6769. u32 base;
  6770. u32 size;
  6771. } cursor[2];
  6772. struct intel_pipe_error_state {
  6773. u32 conf;
  6774. u32 source;
  6775. u32 htotal;
  6776. u32 hblank;
  6777. u32 hsync;
  6778. u32 vtotal;
  6779. u32 vblank;
  6780. u32 vsync;
  6781. } pipe[2];
  6782. struct intel_plane_error_state {
  6783. u32 control;
  6784. u32 stride;
  6785. u32 size;
  6786. u32 pos;
  6787. u32 addr;
  6788. u32 surface;
  6789. u32 tile_offset;
  6790. } plane[2];
  6791. };
  6792. struct intel_display_error_state *
  6793. intel_display_capture_error_state(struct drm_device *dev)
  6794. {
  6795. drm_i915_private_t *dev_priv = dev->dev_private;
  6796. struct intel_display_error_state *error;
  6797. int i;
  6798. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  6799. if (error == NULL)
  6800. return NULL;
  6801. for (i = 0; i < 2; i++) {
  6802. error->cursor[i].control = I915_READ(CURCNTR(i));
  6803. error->cursor[i].position = I915_READ(CURPOS(i));
  6804. error->cursor[i].base = I915_READ(CURBASE(i));
  6805. error->plane[i].control = I915_READ(DSPCNTR(i));
  6806. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  6807. error->plane[i].size = I915_READ(DSPSIZE(i));
  6808. error->plane[i].pos= I915_READ(DSPPOS(i));
  6809. error->plane[i].addr = I915_READ(DSPADDR(i));
  6810. if (INTEL_INFO(dev)->gen >= 4) {
  6811. error->plane[i].surface = I915_READ(DSPSURF(i));
  6812. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  6813. }
  6814. error->pipe[i].conf = I915_READ(PIPECONF(i));
  6815. error->pipe[i].source = I915_READ(PIPESRC(i));
  6816. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  6817. error->pipe[i].hblank = I915_READ(HBLANK(i));
  6818. error->pipe[i].hsync = I915_READ(HSYNC(i));
  6819. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  6820. error->pipe[i].vblank = I915_READ(VBLANK(i));
  6821. error->pipe[i].vsync = I915_READ(VSYNC(i));
  6822. }
  6823. return error;
  6824. }
  6825. void
  6826. intel_display_print_error_state(struct seq_file *m,
  6827. struct drm_device *dev,
  6828. struct intel_display_error_state *error)
  6829. {
  6830. int i;
  6831. for (i = 0; i < 2; i++) {
  6832. seq_printf(m, "Pipe [%d]:\n", i);
  6833. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  6834. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  6835. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  6836. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  6837. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  6838. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  6839. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  6840. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  6841. seq_printf(m, "Plane [%d]:\n", i);
  6842. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  6843. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  6844. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  6845. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  6846. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  6847. if (INTEL_INFO(dev)->gen >= 4) {
  6848. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  6849. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  6850. }
  6851. seq_printf(m, "Cursor [%d]:\n", i);
  6852. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  6853. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  6854. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  6855. }
  6856. }
  6857. #endif