intel_display.c 136 KB

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