intel_display.c 177 KB

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