intel_display.c 221 KB

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