intel_display.c 233 KB

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