intel_display.c 196 KB

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