intel_display.c 172 KB

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