intel_display.c 141 KB

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