intel_display.c 176 KB

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