intel_display.c 181 KB

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