intel_display.c 179 KB

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