intel_display.c 161 KB

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