intel_display.c 136 KB

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