intel_display.c 126 KB

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