intel_pm.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822
  1. /*
  2. * Copyright © 2012 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 DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eugeni Dodonov <eugeni.dodonov@intel.com>
  25. *
  26. */
  27. #include <linux/cpufreq.h>
  28. #include "i915_drv.h"
  29. #include "intel_drv.h"
  30. #include "../../../platform/x86/intel_ips.h"
  31. #include <linux/module.h>
  32. /* FBC, or Frame Buffer Compression, is a technique employed to compress the
  33. * framebuffer contents in-memory, aiming at reducing the required bandwidth
  34. * during in-memory transfers and, therefore, reduce the power packet.
  35. *
  36. * The benefits of FBC are mostly visible with solid backgrounds and
  37. * variation-less patterns.
  38. *
  39. * FBC-related functionality can be enabled by the means of the
  40. * i915.i915_enable_fbc parameter
  41. */
  42. static void i8xx_disable_fbc(struct drm_device *dev)
  43. {
  44. struct drm_i915_private *dev_priv = dev->dev_private;
  45. u32 fbc_ctl;
  46. /* Disable compression */
  47. fbc_ctl = I915_READ(FBC_CONTROL);
  48. if ((fbc_ctl & FBC_CTL_EN) == 0)
  49. return;
  50. fbc_ctl &= ~FBC_CTL_EN;
  51. I915_WRITE(FBC_CONTROL, fbc_ctl);
  52. /* Wait for compressing bit to clear */
  53. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  54. DRM_DEBUG_KMS("FBC idle timed out\n");
  55. return;
  56. }
  57. DRM_DEBUG_KMS("disabled FBC\n");
  58. }
  59. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  60. {
  61. struct drm_device *dev = crtc->dev;
  62. struct drm_i915_private *dev_priv = dev->dev_private;
  63. struct drm_framebuffer *fb = crtc->fb;
  64. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  65. struct drm_i915_gem_object *obj = intel_fb->obj;
  66. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  67. int cfb_pitch;
  68. int plane, i;
  69. u32 fbc_ctl, fbc_ctl2;
  70. cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  71. if (fb->pitches[0] < cfb_pitch)
  72. cfb_pitch = fb->pitches[0];
  73. /* FBC_CTL wants 64B units */
  74. cfb_pitch = (cfb_pitch / 64) - 1;
  75. plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  76. /* Clear old tags */
  77. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  78. I915_WRITE(FBC_TAG + (i * 4), 0);
  79. /* Set it up... */
  80. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  81. fbc_ctl2 |= plane;
  82. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  83. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  84. /* enable it... */
  85. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  86. if (IS_I945GM(dev))
  87. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  88. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  89. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  90. fbc_ctl |= obj->fence_reg;
  91. I915_WRITE(FBC_CONTROL, fbc_ctl);
  92. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
  93. cfb_pitch, crtc->y, intel_crtc->plane);
  94. }
  95. static bool i8xx_fbc_enabled(struct drm_device *dev)
  96. {
  97. struct drm_i915_private *dev_priv = dev->dev_private;
  98. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  99. }
  100. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  101. {
  102. struct drm_device *dev = crtc->dev;
  103. struct drm_i915_private *dev_priv = dev->dev_private;
  104. struct drm_framebuffer *fb = crtc->fb;
  105. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  106. struct drm_i915_gem_object *obj = intel_fb->obj;
  107. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  108. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  109. unsigned long stall_watermark = 200;
  110. u32 dpfc_ctl;
  111. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  112. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  113. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  114. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  115. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  116. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  117. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  118. /* enable it... */
  119. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  120. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  121. }
  122. static void g4x_disable_fbc(struct drm_device *dev)
  123. {
  124. struct drm_i915_private *dev_priv = dev->dev_private;
  125. u32 dpfc_ctl;
  126. /* Disable compression */
  127. dpfc_ctl = I915_READ(DPFC_CONTROL);
  128. if (dpfc_ctl & DPFC_CTL_EN) {
  129. dpfc_ctl &= ~DPFC_CTL_EN;
  130. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  131. DRM_DEBUG_KMS("disabled FBC\n");
  132. }
  133. }
  134. static bool g4x_fbc_enabled(struct drm_device *dev)
  135. {
  136. struct drm_i915_private *dev_priv = dev->dev_private;
  137. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  138. }
  139. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  140. {
  141. struct drm_i915_private *dev_priv = dev->dev_private;
  142. u32 blt_ecoskpd;
  143. /* Make sure blitter notifies FBC of writes */
  144. gen6_gt_force_wake_get(dev_priv);
  145. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  146. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  147. GEN6_BLITTER_LOCK_SHIFT;
  148. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  149. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  150. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  151. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  152. GEN6_BLITTER_LOCK_SHIFT);
  153. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  154. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  155. gen6_gt_force_wake_put(dev_priv);
  156. }
  157. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  158. {
  159. struct drm_device *dev = crtc->dev;
  160. struct drm_i915_private *dev_priv = dev->dev_private;
  161. struct drm_framebuffer *fb = crtc->fb;
  162. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  163. struct drm_i915_gem_object *obj = intel_fb->obj;
  164. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  165. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  166. unsigned long stall_watermark = 200;
  167. u32 dpfc_ctl;
  168. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  169. dpfc_ctl &= DPFC_RESERVED;
  170. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  171. /* Set persistent mode for front-buffer rendering, ala X. */
  172. dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
  173. dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
  174. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  175. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  176. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  177. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  178. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  179. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  180. /* enable it... */
  181. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  182. if (IS_GEN6(dev)) {
  183. I915_WRITE(SNB_DPFC_CTL_SA,
  184. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  185. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  186. sandybridge_blit_fbc_update(dev);
  187. }
  188. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  189. }
  190. static void ironlake_disable_fbc(struct drm_device *dev)
  191. {
  192. struct drm_i915_private *dev_priv = dev->dev_private;
  193. u32 dpfc_ctl;
  194. /* Disable compression */
  195. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  196. if (dpfc_ctl & DPFC_CTL_EN) {
  197. dpfc_ctl &= ~DPFC_CTL_EN;
  198. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  199. DRM_DEBUG_KMS("disabled FBC\n");
  200. }
  201. }
  202. static bool ironlake_fbc_enabled(struct drm_device *dev)
  203. {
  204. struct drm_i915_private *dev_priv = dev->dev_private;
  205. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  206. }
  207. bool intel_fbc_enabled(struct drm_device *dev)
  208. {
  209. struct drm_i915_private *dev_priv = dev->dev_private;
  210. if (!dev_priv->display.fbc_enabled)
  211. return false;
  212. return dev_priv->display.fbc_enabled(dev);
  213. }
  214. static void intel_fbc_work_fn(struct work_struct *__work)
  215. {
  216. struct intel_fbc_work *work =
  217. container_of(to_delayed_work(__work),
  218. struct intel_fbc_work, work);
  219. struct drm_device *dev = work->crtc->dev;
  220. struct drm_i915_private *dev_priv = dev->dev_private;
  221. mutex_lock(&dev->struct_mutex);
  222. if (work == dev_priv->fbc_work) {
  223. /* Double check that we haven't switched fb without cancelling
  224. * the prior work.
  225. */
  226. if (work->crtc->fb == work->fb) {
  227. dev_priv->display.enable_fbc(work->crtc,
  228. work->interval);
  229. dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
  230. dev_priv->cfb_fb = work->crtc->fb->base.id;
  231. dev_priv->cfb_y = work->crtc->y;
  232. }
  233. dev_priv->fbc_work = NULL;
  234. }
  235. mutex_unlock(&dev->struct_mutex);
  236. kfree(work);
  237. }
  238. static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
  239. {
  240. if (dev_priv->fbc_work == NULL)
  241. return;
  242. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  243. /* Synchronisation is provided by struct_mutex and checking of
  244. * dev_priv->fbc_work, so we can perform the cancellation
  245. * entirely asynchronously.
  246. */
  247. if (cancel_delayed_work(&dev_priv->fbc_work->work))
  248. /* tasklet was killed before being run, clean up */
  249. kfree(dev_priv->fbc_work);
  250. /* Mark the work as no longer wanted so that if it does
  251. * wake-up (because the work was already running and waiting
  252. * for our mutex), it will discover that is no longer
  253. * necessary to run.
  254. */
  255. dev_priv->fbc_work = NULL;
  256. }
  257. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  258. {
  259. struct intel_fbc_work *work;
  260. struct drm_device *dev = crtc->dev;
  261. struct drm_i915_private *dev_priv = dev->dev_private;
  262. if (!dev_priv->display.enable_fbc)
  263. return;
  264. intel_cancel_fbc_work(dev_priv);
  265. work = kzalloc(sizeof *work, GFP_KERNEL);
  266. if (work == NULL) {
  267. dev_priv->display.enable_fbc(crtc, interval);
  268. return;
  269. }
  270. work->crtc = crtc;
  271. work->fb = crtc->fb;
  272. work->interval = interval;
  273. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  274. dev_priv->fbc_work = work;
  275. DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
  276. /* Delay the actual enabling to let pageflipping cease and the
  277. * display to settle before starting the compression. Note that
  278. * this delay also serves a second purpose: it allows for a
  279. * vblank to pass after disabling the FBC before we attempt
  280. * to modify the control registers.
  281. *
  282. * A more complicated solution would involve tracking vblanks
  283. * following the termination of the page-flipping sequence
  284. * and indeed performing the enable as a co-routine and not
  285. * waiting synchronously upon the vblank.
  286. */
  287. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  288. }
  289. void intel_disable_fbc(struct drm_device *dev)
  290. {
  291. struct drm_i915_private *dev_priv = dev->dev_private;
  292. intel_cancel_fbc_work(dev_priv);
  293. if (!dev_priv->display.disable_fbc)
  294. return;
  295. dev_priv->display.disable_fbc(dev);
  296. dev_priv->cfb_plane = -1;
  297. }
  298. /**
  299. * intel_update_fbc - enable/disable FBC as needed
  300. * @dev: the drm_device
  301. *
  302. * Set up the framebuffer compression hardware at mode set time. We
  303. * enable it if possible:
  304. * - plane A only (on pre-965)
  305. * - no pixel mulitply/line duplication
  306. * - no alpha buffer discard
  307. * - no dual wide
  308. * - framebuffer <= 2048 in width, 1536 in height
  309. *
  310. * We can't assume that any compression will take place (worst case),
  311. * so the compressed buffer has to be the same size as the uncompressed
  312. * one. It also must reside (along with the line length buffer) in
  313. * stolen memory.
  314. *
  315. * We need to enable/disable FBC on a global basis.
  316. */
  317. void intel_update_fbc(struct drm_device *dev)
  318. {
  319. struct drm_i915_private *dev_priv = dev->dev_private;
  320. struct drm_crtc *crtc = NULL, *tmp_crtc;
  321. struct intel_crtc *intel_crtc;
  322. struct drm_framebuffer *fb;
  323. struct intel_framebuffer *intel_fb;
  324. struct drm_i915_gem_object *obj;
  325. int enable_fbc;
  326. DRM_DEBUG_KMS("\n");
  327. if (!i915_powersave)
  328. return;
  329. if (!I915_HAS_FBC(dev))
  330. return;
  331. /*
  332. * If FBC is already on, we just have to verify that we can
  333. * keep it that way...
  334. * Need to disable if:
  335. * - more than one pipe is active
  336. * - changing FBC params (stride, fence, mode)
  337. * - new fb is too large to fit in compressed buffer
  338. * - going to an unsupported config (interlace, pixel multiply, etc.)
  339. */
  340. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  341. if (tmp_crtc->enabled &&
  342. !to_intel_crtc(tmp_crtc)->primary_disabled &&
  343. tmp_crtc->fb) {
  344. if (crtc) {
  345. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  346. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  347. goto out_disable;
  348. }
  349. crtc = tmp_crtc;
  350. }
  351. }
  352. if (!crtc || crtc->fb == NULL) {
  353. DRM_DEBUG_KMS("no output, disabling\n");
  354. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  355. goto out_disable;
  356. }
  357. intel_crtc = to_intel_crtc(crtc);
  358. fb = crtc->fb;
  359. intel_fb = to_intel_framebuffer(fb);
  360. obj = intel_fb->obj;
  361. enable_fbc = i915_enable_fbc;
  362. if (enable_fbc < 0) {
  363. DRM_DEBUG_KMS("fbc set to per-chip default\n");
  364. enable_fbc = 1;
  365. if (INTEL_INFO(dev)->gen <= 6)
  366. enable_fbc = 0;
  367. }
  368. if (!enable_fbc) {
  369. DRM_DEBUG_KMS("fbc disabled per module param\n");
  370. dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
  371. goto out_disable;
  372. }
  373. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  374. DRM_DEBUG_KMS("framebuffer too large, disabling "
  375. "compression\n");
  376. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  377. goto out_disable;
  378. }
  379. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  380. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  381. DRM_DEBUG_KMS("mode incompatible with compression, "
  382. "disabling\n");
  383. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  384. goto out_disable;
  385. }
  386. if ((crtc->mode.hdisplay > 2048) ||
  387. (crtc->mode.vdisplay > 1536)) {
  388. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  389. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  390. goto out_disable;
  391. }
  392. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  393. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  394. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  395. goto out_disable;
  396. }
  397. /* The use of a CPU fence is mandatory in order to detect writes
  398. * by the CPU to the scanout and trigger updates to the FBC.
  399. */
  400. if (obj->tiling_mode != I915_TILING_X ||
  401. obj->fence_reg == I915_FENCE_REG_NONE) {
  402. DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
  403. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  404. goto out_disable;
  405. }
  406. /* If the kernel debugger is active, always disable compression */
  407. if (in_dbg_master())
  408. goto out_disable;
  409. /* If the scanout has not changed, don't modify the FBC settings.
  410. * Note that we make the fundamental assumption that the fb->obj
  411. * cannot be unpinned (and have its GTT offset and fence revoked)
  412. * without first being decoupled from the scanout and FBC disabled.
  413. */
  414. if (dev_priv->cfb_plane == intel_crtc->plane &&
  415. dev_priv->cfb_fb == fb->base.id &&
  416. dev_priv->cfb_y == crtc->y)
  417. return;
  418. if (intel_fbc_enabled(dev)) {
  419. /* We update FBC along two paths, after changing fb/crtc
  420. * configuration (modeswitching) and after page-flipping
  421. * finishes. For the latter, we know that not only did
  422. * we disable the FBC at the start of the page-flip
  423. * sequence, but also more than one vblank has passed.
  424. *
  425. * For the former case of modeswitching, it is possible
  426. * to switch between two FBC valid configurations
  427. * instantaneously so we do need to disable the FBC
  428. * before we can modify its control registers. We also
  429. * have to wait for the next vblank for that to take
  430. * effect. However, since we delay enabling FBC we can
  431. * assume that a vblank has passed since disabling and
  432. * that we can safely alter the registers in the deferred
  433. * callback.
  434. *
  435. * In the scenario that we go from a valid to invalid
  436. * and then back to valid FBC configuration we have
  437. * no strict enforcement that a vblank occurred since
  438. * disabling the FBC. However, along all current pipe
  439. * disabling paths we do need to wait for a vblank at
  440. * some point. And we wait before enabling FBC anyway.
  441. */
  442. DRM_DEBUG_KMS("disabling active FBC for update\n");
  443. intel_disable_fbc(dev);
  444. }
  445. intel_enable_fbc(crtc, 500);
  446. return;
  447. out_disable:
  448. /* Multiple disables should be harmless */
  449. if (intel_fbc_enabled(dev)) {
  450. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  451. intel_disable_fbc(dev);
  452. }
  453. }
  454. static void i915_pineview_get_mem_freq(struct drm_device *dev)
  455. {
  456. drm_i915_private_t *dev_priv = dev->dev_private;
  457. u32 tmp;
  458. tmp = I915_READ(CLKCFG);
  459. switch (tmp & CLKCFG_FSB_MASK) {
  460. case CLKCFG_FSB_533:
  461. dev_priv->fsb_freq = 533; /* 133*4 */
  462. break;
  463. case CLKCFG_FSB_800:
  464. dev_priv->fsb_freq = 800; /* 200*4 */
  465. break;
  466. case CLKCFG_FSB_667:
  467. dev_priv->fsb_freq = 667; /* 167*4 */
  468. break;
  469. case CLKCFG_FSB_400:
  470. dev_priv->fsb_freq = 400; /* 100*4 */
  471. break;
  472. }
  473. switch (tmp & CLKCFG_MEM_MASK) {
  474. case CLKCFG_MEM_533:
  475. dev_priv->mem_freq = 533;
  476. break;
  477. case CLKCFG_MEM_667:
  478. dev_priv->mem_freq = 667;
  479. break;
  480. case CLKCFG_MEM_800:
  481. dev_priv->mem_freq = 800;
  482. break;
  483. }
  484. /* detect pineview DDR3 setting */
  485. tmp = I915_READ(CSHRDDR3CTL);
  486. dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
  487. }
  488. static void i915_ironlake_get_mem_freq(struct drm_device *dev)
  489. {
  490. drm_i915_private_t *dev_priv = dev->dev_private;
  491. u16 ddrpll, csipll;
  492. ddrpll = I915_READ16(DDRMPLL1);
  493. csipll = I915_READ16(CSIPLL0);
  494. switch (ddrpll & 0xff) {
  495. case 0xc:
  496. dev_priv->mem_freq = 800;
  497. break;
  498. case 0x10:
  499. dev_priv->mem_freq = 1066;
  500. break;
  501. case 0x14:
  502. dev_priv->mem_freq = 1333;
  503. break;
  504. case 0x18:
  505. dev_priv->mem_freq = 1600;
  506. break;
  507. default:
  508. DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
  509. ddrpll & 0xff);
  510. dev_priv->mem_freq = 0;
  511. break;
  512. }
  513. dev_priv->r_t = dev_priv->mem_freq;
  514. switch (csipll & 0x3ff) {
  515. case 0x00c:
  516. dev_priv->fsb_freq = 3200;
  517. break;
  518. case 0x00e:
  519. dev_priv->fsb_freq = 3733;
  520. break;
  521. case 0x010:
  522. dev_priv->fsb_freq = 4266;
  523. break;
  524. case 0x012:
  525. dev_priv->fsb_freq = 4800;
  526. break;
  527. case 0x014:
  528. dev_priv->fsb_freq = 5333;
  529. break;
  530. case 0x016:
  531. dev_priv->fsb_freq = 5866;
  532. break;
  533. case 0x018:
  534. dev_priv->fsb_freq = 6400;
  535. break;
  536. default:
  537. DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
  538. csipll & 0x3ff);
  539. dev_priv->fsb_freq = 0;
  540. break;
  541. }
  542. if (dev_priv->fsb_freq == 3200) {
  543. dev_priv->c_m = 0;
  544. } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
  545. dev_priv->c_m = 1;
  546. } else {
  547. dev_priv->c_m = 2;
  548. }
  549. }
  550. static const struct cxsr_latency cxsr_latency_table[] = {
  551. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  552. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  553. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  554. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  555. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  556. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  557. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  558. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  559. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  560. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  561. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  562. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  563. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  564. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  565. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  566. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  567. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  568. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  569. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  570. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  571. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  572. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  573. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  574. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  575. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  576. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  577. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  578. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  579. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  580. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  581. };
  582. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  583. int is_ddr3,
  584. int fsb,
  585. int mem)
  586. {
  587. const struct cxsr_latency *latency;
  588. int i;
  589. if (fsb == 0 || mem == 0)
  590. return NULL;
  591. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  592. latency = &cxsr_latency_table[i];
  593. if (is_desktop == latency->is_desktop &&
  594. is_ddr3 == latency->is_ddr3 &&
  595. fsb == latency->fsb_freq && mem == latency->mem_freq)
  596. return latency;
  597. }
  598. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  599. return NULL;
  600. }
  601. static void pineview_disable_cxsr(struct drm_device *dev)
  602. {
  603. struct drm_i915_private *dev_priv = dev->dev_private;
  604. /* deactivate cxsr */
  605. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  606. }
  607. /*
  608. * Latency for FIFO fetches is dependent on several factors:
  609. * - memory configuration (speed, channels)
  610. * - chipset
  611. * - current MCH state
  612. * It can be fairly high in some situations, so here we assume a fairly
  613. * pessimal value. It's a tradeoff between extra memory fetches (if we
  614. * set this value too high, the FIFO will fetch frequently to stay full)
  615. * and power consumption (set it too low to save power and we might see
  616. * FIFO underruns and display "flicker").
  617. *
  618. * A value of 5us seems to be a good balance; safe for very low end
  619. * platforms but not overly aggressive on lower latency configs.
  620. */
  621. static const int latency_ns = 5000;
  622. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  623. {
  624. struct drm_i915_private *dev_priv = dev->dev_private;
  625. uint32_t dsparb = I915_READ(DSPARB);
  626. int size;
  627. size = dsparb & 0x7f;
  628. if (plane)
  629. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  630. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  631. plane ? "B" : "A", size);
  632. return size;
  633. }
  634. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  635. {
  636. struct drm_i915_private *dev_priv = dev->dev_private;
  637. uint32_t dsparb = I915_READ(DSPARB);
  638. int size;
  639. size = dsparb & 0x1ff;
  640. if (plane)
  641. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  642. size >>= 1; /* Convert to cachelines */
  643. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  644. plane ? "B" : "A", size);
  645. return size;
  646. }
  647. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  648. {
  649. struct drm_i915_private *dev_priv = dev->dev_private;
  650. uint32_t dsparb = I915_READ(DSPARB);
  651. int size;
  652. size = dsparb & 0x7f;
  653. size >>= 2; /* Convert to cachelines */
  654. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  655. plane ? "B" : "A",
  656. size);
  657. return size;
  658. }
  659. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  660. {
  661. struct drm_i915_private *dev_priv = dev->dev_private;
  662. uint32_t dsparb = I915_READ(DSPARB);
  663. int size;
  664. size = dsparb & 0x7f;
  665. size >>= 1; /* Convert to cachelines */
  666. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  667. plane ? "B" : "A", size);
  668. return size;
  669. }
  670. /* Pineview has different values for various configs */
  671. static const struct intel_watermark_params pineview_display_wm = {
  672. PINEVIEW_DISPLAY_FIFO,
  673. PINEVIEW_MAX_WM,
  674. PINEVIEW_DFT_WM,
  675. PINEVIEW_GUARD_WM,
  676. PINEVIEW_FIFO_LINE_SIZE
  677. };
  678. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  679. PINEVIEW_DISPLAY_FIFO,
  680. PINEVIEW_MAX_WM,
  681. PINEVIEW_DFT_HPLLOFF_WM,
  682. PINEVIEW_GUARD_WM,
  683. PINEVIEW_FIFO_LINE_SIZE
  684. };
  685. static const struct intel_watermark_params pineview_cursor_wm = {
  686. PINEVIEW_CURSOR_FIFO,
  687. PINEVIEW_CURSOR_MAX_WM,
  688. PINEVIEW_CURSOR_DFT_WM,
  689. PINEVIEW_CURSOR_GUARD_WM,
  690. PINEVIEW_FIFO_LINE_SIZE,
  691. };
  692. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  693. PINEVIEW_CURSOR_FIFO,
  694. PINEVIEW_CURSOR_MAX_WM,
  695. PINEVIEW_CURSOR_DFT_WM,
  696. PINEVIEW_CURSOR_GUARD_WM,
  697. PINEVIEW_FIFO_LINE_SIZE
  698. };
  699. static const struct intel_watermark_params g4x_wm_info = {
  700. G4X_FIFO_SIZE,
  701. G4X_MAX_WM,
  702. G4X_MAX_WM,
  703. 2,
  704. G4X_FIFO_LINE_SIZE,
  705. };
  706. static const struct intel_watermark_params g4x_cursor_wm_info = {
  707. I965_CURSOR_FIFO,
  708. I965_CURSOR_MAX_WM,
  709. I965_CURSOR_DFT_WM,
  710. 2,
  711. G4X_FIFO_LINE_SIZE,
  712. };
  713. static const struct intel_watermark_params valleyview_wm_info = {
  714. VALLEYVIEW_FIFO_SIZE,
  715. VALLEYVIEW_MAX_WM,
  716. VALLEYVIEW_MAX_WM,
  717. 2,
  718. G4X_FIFO_LINE_SIZE,
  719. };
  720. static const struct intel_watermark_params valleyview_cursor_wm_info = {
  721. I965_CURSOR_FIFO,
  722. VALLEYVIEW_CURSOR_MAX_WM,
  723. I965_CURSOR_DFT_WM,
  724. 2,
  725. G4X_FIFO_LINE_SIZE,
  726. };
  727. static const struct intel_watermark_params i965_cursor_wm_info = {
  728. I965_CURSOR_FIFO,
  729. I965_CURSOR_MAX_WM,
  730. I965_CURSOR_DFT_WM,
  731. 2,
  732. I915_FIFO_LINE_SIZE,
  733. };
  734. static const struct intel_watermark_params i945_wm_info = {
  735. I945_FIFO_SIZE,
  736. I915_MAX_WM,
  737. 1,
  738. 2,
  739. I915_FIFO_LINE_SIZE
  740. };
  741. static const struct intel_watermark_params i915_wm_info = {
  742. I915_FIFO_SIZE,
  743. I915_MAX_WM,
  744. 1,
  745. 2,
  746. I915_FIFO_LINE_SIZE
  747. };
  748. static const struct intel_watermark_params i855_wm_info = {
  749. I855GM_FIFO_SIZE,
  750. I915_MAX_WM,
  751. 1,
  752. 2,
  753. I830_FIFO_LINE_SIZE
  754. };
  755. static const struct intel_watermark_params i830_wm_info = {
  756. I830_FIFO_SIZE,
  757. I915_MAX_WM,
  758. 1,
  759. 2,
  760. I830_FIFO_LINE_SIZE
  761. };
  762. static const struct intel_watermark_params ironlake_display_wm_info = {
  763. ILK_DISPLAY_FIFO,
  764. ILK_DISPLAY_MAXWM,
  765. ILK_DISPLAY_DFTWM,
  766. 2,
  767. ILK_FIFO_LINE_SIZE
  768. };
  769. static const struct intel_watermark_params ironlake_cursor_wm_info = {
  770. ILK_CURSOR_FIFO,
  771. ILK_CURSOR_MAXWM,
  772. ILK_CURSOR_DFTWM,
  773. 2,
  774. ILK_FIFO_LINE_SIZE
  775. };
  776. static const struct intel_watermark_params ironlake_display_srwm_info = {
  777. ILK_DISPLAY_SR_FIFO,
  778. ILK_DISPLAY_MAX_SRWM,
  779. ILK_DISPLAY_DFT_SRWM,
  780. 2,
  781. ILK_FIFO_LINE_SIZE
  782. };
  783. static const struct intel_watermark_params ironlake_cursor_srwm_info = {
  784. ILK_CURSOR_SR_FIFO,
  785. ILK_CURSOR_MAX_SRWM,
  786. ILK_CURSOR_DFT_SRWM,
  787. 2,
  788. ILK_FIFO_LINE_SIZE
  789. };
  790. static const struct intel_watermark_params sandybridge_display_wm_info = {
  791. SNB_DISPLAY_FIFO,
  792. SNB_DISPLAY_MAXWM,
  793. SNB_DISPLAY_DFTWM,
  794. 2,
  795. SNB_FIFO_LINE_SIZE
  796. };
  797. static const struct intel_watermark_params sandybridge_cursor_wm_info = {
  798. SNB_CURSOR_FIFO,
  799. SNB_CURSOR_MAXWM,
  800. SNB_CURSOR_DFTWM,
  801. 2,
  802. SNB_FIFO_LINE_SIZE
  803. };
  804. static const struct intel_watermark_params sandybridge_display_srwm_info = {
  805. SNB_DISPLAY_SR_FIFO,
  806. SNB_DISPLAY_MAX_SRWM,
  807. SNB_DISPLAY_DFT_SRWM,
  808. 2,
  809. SNB_FIFO_LINE_SIZE
  810. };
  811. static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
  812. SNB_CURSOR_SR_FIFO,
  813. SNB_CURSOR_MAX_SRWM,
  814. SNB_CURSOR_DFT_SRWM,
  815. 2,
  816. SNB_FIFO_LINE_SIZE
  817. };
  818. /**
  819. * intel_calculate_wm - calculate watermark level
  820. * @clock_in_khz: pixel clock
  821. * @wm: chip FIFO params
  822. * @pixel_size: display pixel size
  823. * @latency_ns: memory latency for the platform
  824. *
  825. * Calculate the watermark level (the level at which the display plane will
  826. * start fetching from memory again). Each chip has a different display
  827. * FIFO size and allocation, so the caller needs to figure that out and pass
  828. * in the correct intel_watermark_params structure.
  829. *
  830. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  831. * on the pixel size. When it reaches the watermark level, it'll start
  832. * fetching FIFO line sized based chunks from memory until the FIFO fills
  833. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  834. * will occur, and a display engine hang could result.
  835. */
  836. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  837. const struct intel_watermark_params *wm,
  838. int fifo_size,
  839. int pixel_size,
  840. unsigned long latency_ns)
  841. {
  842. long entries_required, wm_size;
  843. /*
  844. * Note: we need to make sure we don't overflow for various clock &
  845. * latency values.
  846. * clocks go from a few thousand to several hundred thousand.
  847. * latency is usually a few thousand
  848. */
  849. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  850. 1000;
  851. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  852. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  853. wm_size = fifo_size - (entries_required + wm->guard_size);
  854. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  855. /* Don't promote wm_size to unsigned... */
  856. if (wm_size > (long)wm->max_wm)
  857. wm_size = wm->max_wm;
  858. if (wm_size <= 0)
  859. wm_size = wm->default_wm;
  860. return wm_size;
  861. }
  862. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  863. {
  864. struct drm_crtc *crtc, *enabled = NULL;
  865. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  866. if (crtc->enabled && crtc->fb) {
  867. if (enabled)
  868. return NULL;
  869. enabled = crtc;
  870. }
  871. }
  872. return enabled;
  873. }
  874. static void pineview_update_wm(struct drm_device *dev)
  875. {
  876. struct drm_i915_private *dev_priv = dev->dev_private;
  877. struct drm_crtc *crtc;
  878. const struct cxsr_latency *latency;
  879. u32 reg;
  880. unsigned long wm;
  881. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  882. dev_priv->fsb_freq, dev_priv->mem_freq);
  883. if (!latency) {
  884. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  885. pineview_disable_cxsr(dev);
  886. return;
  887. }
  888. crtc = single_enabled_crtc(dev);
  889. if (crtc) {
  890. int clock = crtc->mode.clock;
  891. int pixel_size = crtc->fb->bits_per_pixel / 8;
  892. /* Display SR */
  893. wm = intel_calculate_wm(clock, &pineview_display_wm,
  894. pineview_display_wm.fifo_size,
  895. pixel_size, latency->display_sr);
  896. reg = I915_READ(DSPFW1);
  897. reg &= ~DSPFW_SR_MASK;
  898. reg |= wm << DSPFW_SR_SHIFT;
  899. I915_WRITE(DSPFW1, reg);
  900. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  901. /* cursor SR */
  902. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  903. pineview_display_wm.fifo_size,
  904. pixel_size, latency->cursor_sr);
  905. reg = I915_READ(DSPFW3);
  906. reg &= ~DSPFW_CURSOR_SR_MASK;
  907. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  908. I915_WRITE(DSPFW3, reg);
  909. /* Display HPLL off SR */
  910. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  911. pineview_display_hplloff_wm.fifo_size,
  912. pixel_size, latency->display_hpll_disable);
  913. reg = I915_READ(DSPFW3);
  914. reg &= ~DSPFW_HPLL_SR_MASK;
  915. reg |= wm & DSPFW_HPLL_SR_MASK;
  916. I915_WRITE(DSPFW3, reg);
  917. /* cursor HPLL off SR */
  918. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  919. pineview_display_hplloff_wm.fifo_size,
  920. pixel_size, latency->cursor_hpll_disable);
  921. reg = I915_READ(DSPFW3);
  922. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  923. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  924. I915_WRITE(DSPFW3, reg);
  925. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  926. /* activate cxsr */
  927. I915_WRITE(DSPFW3,
  928. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  929. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  930. } else {
  931. pineview_disable_cxsr(dev);
  932. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  933. }
  934. }
  935. static bool g4x_compute_wm0(struct drm_device *dev,
  936. int plane,
  937. const struct intel_watermark_params *display,
  938. int display_latency_ns,
  939. const struct intel_watermark_params *cursor,
  940. int cursor_latency_ns,
  941. int *plane_wm,
  942. int *cursor_wm)
  943. {
  944. struct drm_crtc *crtc;
  945. int htotal, hdisplay, clock, pixel_size;
  946. int line_time_us, line_count;
  947. int entries, tlb_miss;
  948. crtc = intel_get_crtc_for_plane(dev, plane);
  949. if (crtc->fb == NULL || !crtc->enabled) {
  950. *cursor_wm = cursor->guard_size;
  951. *plane_wm = display->guard_size;
  952. return false;
  953. }
  954. htotal = crtc->mode.htotal;
  955. hdisplay = crtc->mode.hdisplay;
  956. clock = crtc->mode.clock;
  957. pixel_size = crtc->fb->bits_per_pixel / 8;
  958. /* Use the small buffer method to calculate plane watermark */
  959. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  960. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  961. if (tlb_miss > 0)
  962. entries += tlb_miss;
  963. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  964. *plane_wm = entries + display->guard_size;
  965. if (*plane_wm > (int)display->max_wm)
  966. *plane_wm = display->max_wm;
  967. /* Use the large buffer method to calculate cursor watermark */
  968. line_time_us = ((htotal * 1000) / clock);
  969. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  970. entries = line_count * 64 * pixel_size;
  971. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  972. if (tlb_miss > 0)
  973. entries += tlb_miss;
  974. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  975. *cursor_wm = entries + cursor->guard_size;
  976. if (*cursor_wm > (int)cursor->max_wm)
  977. *cursor_wm = (int)cursor->max_wm;
  978. return true;
  979. }
  980. /*
  981. * Check the wm result.
  982. *
  983. * If any calculated watermark values is larger than the maximum value that
  984. * can be programmed into the associated watermark register, that watermark
  985. * must be disabled.
  986. */
  987. static bool g4x_check_srwm(struct drm_device *dev,
  988. int display_wm, int cursor_wm,
  989. const struct intel_watermark_params *display,
  990. const struct intel_watermark_params *cursor)
  991. {
  992. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  993. display_wm, cursor_wm);
  994. if (display_wm > display->max_wm) {
  995. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  996. display_wm, display->max_wm);
  997. return false;
  998. }
  999. if (cursor_wm > cursor->max_wm) {
  1000. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  1001. cursor_wm, cursor->max_wm);
  1002. return false;
  1003. }
  1004. if (!(display_wm || cursor_wm)) {
  1005. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  1006. return false;
  1007. }
  1008. return true;
  1009. }
  1010. static bool g4x_compute_srwm(struct drm_device *dev,
  1011. int plane,
  1012. int latency_ns,
  1013. const struct intel_watermark_params *display,
  1014. const struct intel_watermark_params *cursor,
  1015. int *display_wm, int *cursor_wm)
  1016. {
  1017. struct drm_crtc *crtc;
  1018. int hdisplay, htotal, pixel_size, clock;
  1019. unsigned long line_time_us;
  1020. int line_count, line_size;
  1021. int small, large;
  1022. int entries;
  1023. if (!latency_ns) {
  1024. *display_wm = *cursor_wm = 0;
  1025. return false;
  1026. }
  1027. crtc = intel_get_crtc_for_plane(dev, plane);
  1028. hdisplay = crtc->mode.hdisplay;
  1029. htotal = crtc->mode.htotal;
  1030. clock = crtc->mode.clock;
  1031. pixel_size = crtc->fb->bits_per_pixel / 8;
  1032. line_time_us = (htotal * 1000) / clock;
  1033. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1034. line_size = hdisplay * pixel_size;
  1035. /* Use the minimum of the small and large buffer method for primary */
  1036. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1037. large = line_count * line_size;
  1038. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1039. *display_wm = entries + display->guard_size;
  1040. /* calculate the self-refresh watermark for display cursor */
  1041. entries = line_count * pixel_size * 64;
  1042. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1043. *cursor_wm = entries + cursor->guard_size;
  1044. return g4x_check_srwm(dev,
  1045. *display_wm, *cursor_wm,
  1046. display, cursor);
  1047. }
  1048. static bool vlv_compute_drain_latency(struct drm_device *dev,
  1049. int plane,
  1050. int *plane_prec_mult,
  1051. int *plane_dl,
  1052. int *cursor_prec_mult,
  1053. int *cursor_dl)
  1054. {
  1055. struct drm_crtc *crtc;
  1056. int clock, pixel_size;
  1057. int entries;
  1058. crtc = intel_get_crtc_for_plane(dev, plane);
  1059. if (crtc->fb == NULL || !crtc->enabled)
  1060. return false;
  1061. clock = crtc->mode.clock; /* VESA DOT Clock */
  1062. pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
  1063. entries = (clock / 1000) * pixel_size;
  1064. *plane_prec_mult = (entries > 256) ?
  1065. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  1066. *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
  1067. pixel_size);
  1068. entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
  1069. *cursor_prec_mult = (entries > 256) ?
  1070. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  1071. *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
  1072. return true;
  1073. }
  1074. /*
  1075. * Update drain latency registers of memory arbiter
  1076. *
  1077. * Valleyview SoC has a new memory arbiter and needs drain latency registers
  1078. * to be programmed. Each plane has a drain latency multiplier and a drain
  1079. * latency value.
  1080. */
  1081. static void vlv_update_drain_latency(struct drm_device *dev)
  1082. {
  1083. struct drm_i915_private *dev_priv = dev->dev_private;
  1084. int planea_prec, planea_dl, planeb_prec, planeb_dl;
  1085. int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
  1086. int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
  1087. either 16 or 32 */
  1088. /* For plane A, Cursor A */
  1089. if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
  1090. &cursor_prec_mult, &cursora_dl)) {
  1091. cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1092. DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
  1093. planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1094. DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
  1095. I915_WRITE(VLV_DDL1, cursora_prec |
  1096. (cursora_dl << DDL_CURSORA_SHIFT) |
  1097. planea_prec | planea_dl);
  1098. }
  1099. /* For plane B, Cursor B */
  1100. if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
  1101. &cursor_prec_mult, &cursorb_dl)) {
  1102. cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1103. DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
  1104. planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1105. DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
  1106. I915_WRITE(VLV_DDL2, cursorb_prec |
  1107. (cursorb_dl << DDL_CURSORB_SHIFT) |
  1108. planeb_prec | planeb_dl);
  1109. }
  1110. }
  1111. #define single_plane_enabled(mask) is_power_of_2(mask)
  1112. static void valleyview_update_wm(struct drm_device *dev)
  1113. {
  1114. static const int sr_latency_ns = 12000;
  1115. struct drm_i915_private *dev_priv = dev->dev_private;
  1116. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1117. int plane_sr, cursor_sr;
  1118. unsigned int enabled = 0;
  1119. vlv_update_drain_latency(dev);
  1120. if (g4x_compute_wm0(dev, 0,
  1121. &valleyview_wm_info, latency_ns,
  1122. &valleyview_cursor_wm_info, latency_ns,
  1123. &planea_wm, &cursora_wm))
  1124. enabled |= 1;
  1125. if (g4x_compute_wm0(dev, 1,
  1126. &valleyview_wm_info, latency_ns,
  1127. &valleyview_cursor_wm_info, latency_ns,
  1128. &planeb_wm, &cursorb_wm))
  1129. enabled |= 2;
  1130. plane_sr = cursor_sr = 0;
  1131. if (single_plane_enabled(enabled) &&
  1132. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1133. sr_latency_ns,
  1134. &valleyview_wm_info,
  1135. &valleyview_cursor_wm_info,
  1136. &plane_sr, &cursor_sr))
  1137. I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
  1138. else
  1139. I915_WRITE(FW_BLC_SELF_VLV,
  1140. I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
  1141. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1142. planea_wm, cursora_wm,
  1143. planeb_wm, cursorb_wm,
  1144. plane_sr, cursor_sr);
  1145. I915_WRITE(DSPFW1,
  1146. (plane_sr << DSPFW_SR_SHIFT) |
  1147. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1148. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1149. planea_wm);
  1150. I915_WRITE(DSPFW2,
  1151. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  1152. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1153. I915_WRITE(DSPFW3,
  1154. (I915_READ(DSPFW3) | (cursor_sr << DSPFW_CURSOR_SR_SHIFT)));
  1155. }
  1156. static void g4x_update_wm(struct drm_device *dev)
  1157. {
  1158. static const int sr_latency_ns = 12000;
  1159. struct drm_i915_private *dev_priv = dev->dev_private;
  1160. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1161. int plane_sr, cursor_sr;
  1162. unsigned int enabled = 0;
  1163. if (g4x_compute_wm0(dev, 0,
  1164. &g4x_wm_info, latency_ns,
  1165. &g4x_cursor_wm_info, latency_ns,
  1166. &planea_wm, &cursora_wm))
  1167. enabled |= 1;
  1168. if (g4x_compute_wm0(dev, 1,
  1169. &g4x_wm_info, latency_ns,
  1170. &g4x_cursor_wm_info, latency_ns,
  1171. &planeb_wm, &cursorb_wm))
  1172. enabled |= 2;
  1173. plane_sr = cursor_sr = 0;
  1174. if (single_plane_enabled(enabled) &&
  1175. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1176. sr_latency_ns,
  1177. &g4x_wm_info,
  1178. &g4x_cursor_wm_info,
  1179. &plane_sr, &cursor_sr))
  1180. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1181. else
  1182. I915_WRITE(FW_BLC_SELF,
  1183. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  1184. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1185. planea_wm, cursora_wm,
  1186. planeb_wm, cursorb_wm,
  1187. plane_sr, cursor_sr);
  1188. I915_WRITE(DSPFW1,
  1189. (plane_sr << DSPFW_SR_SHIFT) |
  1190. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1191. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1192. planea_wm);
  1193. I915_WRITE(DSPFW2,
  1194. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  1195. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1196. /* HPLL off in SR has some issues on G4x... disable it */
  1197. I915_WRITE(DSPFW3,
  1198. (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  1199. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1200. }
  1201. static void i965_update_wm(struct drm_device *dev)
  1202. {
  1203. struct drm_i915_private *dev_priv = dev->dev_private;
  1204. struct drm_crtc *crtc;
  1205. int srwm = 1;
  1206. int cursor_sr = 16;
  1207. /* Calc sr entries for one plane configs */
  1208. crtc = single_enabled_crtc(dev);
  1209. if (crtc) {
  1210. /* self-refresh has much higher latency */
  1211. static const int sr_latency_ns = 12000;
  1212. int clock = crtc->mode.clock;
  1213. int htotal = crtc->mode.htotal;
  1214. int hdisplay = crtc->mode.hdisplay;
  1215. int pixel_size = crtc->fb->bits_per_pixel / 8;
  1216. unsigned long line_time_us;
  1217. int entries;
  1218. line_time_us = ((htotal * 1000) / clock);
  1219. /* Use ns/us then divide to preserve precision */
  1220. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1221. pixel_size * hdisplay;
  1222. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  1223. srwm = I965_FIFO_SIZE - entries;
  1224. if (srwm < 0)
  1225. srwm = 1;
  1226. srwm &= 0x1ff;
  1227. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  1228. entries, srwm);
  1229. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1230. pixel_size * 64;
  1231. entries = DIV_ROUND_UP(entries,
  1232. i965_cursor_wm_info.cacheline_size);
  1233. cursor_sr = i965_cursor_wm_info.fifo_size -
  1234. (entries + i965_cursor_wm_info.guard_size);
  1235. if (cursor_sr > i965_cursor_wm_info.max_wm)
  1236. cursor_sr = i965_cursor_wm_info.max_wm;
  1237. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  1238. "cursor %d\n", srwm, cursor_sr);
  1239. if (IS_CRESTLINE(dev))
  1240. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1241. } else {
  1242. /* Turn off self refresh if both pipes are enabled */
  1243. if (IS_CRESTLINE(dev))
  1244. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  1245. & ~FW_BLC_SELF_EN);
  1246. }
  1247. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  1248. srwm);
  1249. /* 965 has limitations... */
  1250. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  1251. (8 << 16) | (8 << 8) | (8 << 0));
  1252. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  1253. /* update cursor SR watermark */
  1254. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1255. }
  1256. static void i9xx_update_wm(struct drm_device *dev)
  1257. {
  1258. struct drm_i915_private *dev_priv = dev->dev_private;
  1259. const struct intel_watermark_params *wm_info;
  1260. uint32_t fwater_lo;
  1261. uint32_t fwater_hi;
  1262. int cwm, srwm = 1;
  1263. int fifo_size;
  1264. int planea_wm, planeb_wm;
  1265. struct drm_crtc *crtc, *enabled = NULL;
  1266. if (IS_I945GM(dev))
  1267. wm_info = &i945_wm_info;
  1268. else if (!IS_GEN2(dev))
  1269. wm_info = &i915_wm_info;
  1270. else
  1271. wm_info = &i855_wm_info;
  1272. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  1273. crtc = intel_get_crtc_for_plane(dev, 0);
  1274. if (crtc->enabled && crtc->fb) {
  1275. planea_wm = intel_calculate_wm(crtc->mode.clock,
  1276. wm_info, fifo_size,
  1277. crtc->fb->bits_per_pixel / 8,
  1278. latency_ns);
  1279. enabled = crtc;
  1280. } else
  1281. planea_wm = fifo_size - wm_info->guard_size;
  1282. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  1283. crtc = intel_get_crtc_for_plane(dev, 1);
  1284. if (crtc->enabled && crtc->fb) {
  1285. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  1286. wm_info, fifo_size,
  1287. crtc->fb->bits_per_pixel / 8,
  1288. latency_ns);
  1289. if (enabled == NULL)
  1290. enabled = crtc;
  1291. else
  1292. enabled = NULL;
  1293. } else
  1294. planeb_wm = fifo_size - wm_info->guard_size;
  1295. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  1296. /*
  1297. * Overlay gets an aggressive default since video jitter is bad.
  1298. */
  1299. cwm = 2;
  1300. /* Play safe and disable self-refresh before adjusting watermarks. */
  1301. if (IS_I945G(dev) || IS_I945GM(dev))
  1302. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  1303. else if (IS_I915GM(dev))
  1304. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  1305. /* Calc sr entries for one plane configs */
  1306. if (HAS_FW_BLC(dev) && enabled) {
  1307. /* self-refresh has much higher latency */
  1308. static const int sr_latency_ns = 6000;
  1309. int clock = enabled->mode.clock;
  1310. int htotal = enabled->mode.htotal;
  1311. int hdisplay = enabled->mode.hdisplay;
  1312. int pixel_size = enabled->fb->bits_per_pixel / 8;
  1313. unsigned long line_time_us;
  1314. int entries;
  1315. line_time_us = (htotal * 1000) / clock;
  1316. /* Use ns/us then divide to preserve precision */
  1317. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1318. pixel_size * hdisplay;
  1319. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  1320. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  1321. srwm = wm_info->fifo_size - entries;
  1322. if (srwm < 0)
  1323. srwm = 1;
  1324. if (IS_I945G(dev) || IS_I945GM(dev))
  1325. I915_WRITE(FW_BLC_SELF,
  1326. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  1327. else if (IS_I915GM(dev))
  1328. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  1329. }
  1330. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  1331. planea_wm, planeb_wm, cwm, srwm);
  1332. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  1333. fwater_hi = (cwm & 0x1f);
  1334. /* Set request length to 8 cachelines per fetch */
  1335. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  1336. fwater_hi = fwater_hi | (1 << 8);
  1337. I915_WRITE(FW_BLC, fwater_lo);
  1338. I915_WRITE(FW_BLC2, fwater_hi);
  1339. if (HAS_FW_BLC(dev)) {
  1340. if (enabled) {
  1341. if (IS_I945G(dev) || IS_I945GM(dev))
  1342. I915_WRITE(FW_BLC_SELF,
  1343. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  1344. else if (IS_I915GM(dev))
  1345. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  1346. DRM_DEBUG_KMS("memory self refresh enabled\n");
  1347. } else
  1348. DRM_DEBUG_KMS("memory self refresh disabled\n");
  1349. }
  1350. }
  1351. static void i830_update_wm(struct drm_device *dev)
  1352. {
  1353. struct drm_i915_private *dev_priv = dev->dev_private;
  1354. struct drm_crtc *crtc;
  1355. uint32_t fwater_lo;
  1356. int planea_wm;
  1357. crtc = single_enabled_crtc(dev);
  1358. if (crtc == NULL)
  1359. return;
  1360. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  1361. dev_priv->display.get_fifo_size(dev, 0),
  1362. crtc->fb->bits_per_pixel / 8,
  1363. latency_ns);
  1364. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  1365. fwater_lo |= (3<<8) | planea_wm;
  1366. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  1367. I915_WRITE(FW_BLC, fwater_lo);
  1368. }
  1369. #define ILK_LP0_PLANE_LATENCY 700
  1370. #define ILK_LP0_CURSOR_LATENCY 1300
  1371. /*
  1372. * Check the wm result.
  1373. *
  1374. * If any calculated watermark values is larger than the maximum value that
  1375. * can be programmed into the associated watermark register, that watermark
  1376. * must be disabled.
  1377. */
  1378. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  1379. int fbc_wm, int display_wm, int cursor_wm,
  1380. const struct intel_watermark_params *display,
  1381. const struct intel_watermark_params *cursor)
  1382. {
  1383. struct drm_i915_private *dev_priv = dev->dev_private;
  1384. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  1385. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  1386. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  1387. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  1388. fbc_wm, SNB_FBC_MAX_SRWM, level);
  1389. /* fbc has it's own way to disable FBC WM */
  1390. I915_WRITE(DISP_ARB_CTL,
  1391. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  1392. return false;
  1393. }
  1394. if (display_wm > display->max_wm) {
  1395. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  1396. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  1397. return false;
  1398. }
  1399. if (cursor_wm > cursor->max_wm) {
  1400. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  1401. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  1402. return false;
  1403. }
  1404. if (!(fbc_wm || display_wm || cursor_wm)) {
  1405. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  1406. return false;
  1407. }
  1408. return true;
  1409. }
  1410. /*
  1411. * Compute watermark values of WM[1-3],
  1412. */
  1413. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  1414. int latency_ns,
  1415. const struct intel_watermark_params *display,
  1416. const struct intel_watermark_params *cursor,
  1417. int *fbc_wm, int *display_wm, int *cursor_wm)
  1418. {
  1419. struct drm_crtc *crtc;
  1420. unsigned long line_time_us;
  1421. int hdisplay, htotal, pixel_size, clock;
  1422. int line_count, line_size;
  1423. int small, large;
  1424. int entries;
  1425. if (!latency_ns) {
  1426. *fbc_wm = *display_wm = *cursor_wm = 0;
  1427. return false;
  1428. }
  1429. crtc = intel_get_crtc_for_plane(dev, plane);
  1430. hdisplay = crtc->mode.hdisplay;
  1431. htotal = crtc->mode.htotal;
  1432. clock = crtc->mode.clock;
  1433. pixel_size = crtc->fb->bits_per_pixel / 8;
  1434. line_time_us = (htotal * 1000) / clock;
  1435. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1436. line_size = hdisplay * pixel_size;
  1437. /* Use the minimum of the small and large buffer method for primary */
  1438. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1439. large = line_count * line_size;
  1440. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1441. *display_wm = entries + display->guard_size;
  1442. /*
  1443. * Spec says:
  1444. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  1445. */
  1446. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  1447. /* calculate the self-refresh watermark for display cursor */
  1448. entries = line_count * pixel_size * 64;
  1449. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1450. *cursor_wm = entries + cursor->guard_size;
  1451. return ironlake_check_srwm(dev, level,
  1452. *fbc_wm, *display_wm, *cursor_wm,
  1453. display, cursor);
  1454. }
  1455. static void ironlake_update_wm(struct drm_device *dev)
  1456. {
  1457. struct drm_i915_private *dev_priv = dev->dev_private;
  1458. int fbc_wm, plane_wm, cursor_wm;
  1459. unsigned int enabled;
  1460. enabled = 0;
  1461. if (g4x_compute_wm0(dev, 0,
  1462. &ironlake_display_wm_info,
  1463. ILK_LP0_PLANE_LATENCY,
  1464. &ironlake_cursor_wm_info,
  1465. ILK_LP0_CURSOR_LATENCY,
  1466. &plane_wm, &cursor_wm)) {
  1467. I915_WRITE(WM0_PIPEA_ILK,
  1468. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1469. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1470. " plane %d, " "cursor: %d\n",
  1471. plane_wm, cursor_wm);
  1472. enabled |= 1;
  1473. }
  1474. if (g4x_compute_wm0(dev, 1,
  1475. &ironlake_display_wm_info,
  1476. ILK_LP0_PLANE_LATENCY,
  1477. &ironlake_cursor_wm_info,
  1478. ILK_LP0_CURSOR_LATENCY,
  1479. &plane_wm, &cursor_wm)) {
  1480. I915_WRITE(WM0_PIPEB_ILK,
  1481. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1482. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1483. " plane %d, cursor: %d\n",
  1484. plane_wm, cursor_wm);
  1485. enabled |= 2;
  1486. }
  1487. /*
  1488. * Calculate and update the self-refresh watermark only when one
  1489. * display plane is used.
  1490. */
  1491. I915_WRITE(WM3_LP_ILK, 0);
  1492. I915_WRITE(WM2_LP_ILK, 0);
  1493. I915_WRITE(WM1_LP_ILK, 0);
  1494. if (!single_plane_enabled(enabled))
  1495. return;
  1496. enabled = ffs(enabled) - 1;
  1497. /* WM1 */
  1498. if (!ironlake_compute_srwm(dev, 1, enabled,
  1499. ILK_READ_WM1_LATENCY() * 500,
  1500. &ironlake_display_srwm_info,
  1501. &ironlake_cursor_srwm_info,
  1502. &fbc_wm, &plane_wm, &cursor_wm))
  1503. return;
  1504. I915_WRITE(WM1_LP_ILK,
  1505. WM1_LP_SR_EN |
  1506. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1507. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1508. (plane_wm << WM1_LP_SR_SHIFT) |
  1509. cursor_wm);
  1510. /* WM2 */
  1511. if (!ironlake_compute_srwm(dev, 2, enabled,
  1512. ILK_READ_WM2_LATENCY() * 500,
  1513. &ironlake_display_srwm_info,
  1514. &ironlake_cursor_srwm_info,
  1515. &fbc_wm, &plane_wm, &cursor_wm))
  1516. return;
  1517. I915_WRITE(WM2_LP_ILK,
  1518. WM2_LP_EN |
  1519. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1520. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1521. (plane_wm << WM1_LP_SR_SHIFT) |
  1522. cursor_wm);
  1523. /*
  1524. * WM3 is unsupported on ILK, probably because we don't have latency
  1525. * data for that power state
  1526. */
  1527. }
  1528. static void sandybridge_update_wm(struct drm_device *dev)
  1529. {
  1530. struct drm_i915_private *dev_priv = dev->dev_private;
  1531. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1532. u32 val;
  1533. int fbc_wm, plane_wm, cursor_wm;
  1534. unsigned int enabled;
  1535. enabled = 0;
  1536. if (g4x_compute_wm0(dev, 0,
  1537. &sandybridge_display_wm_info, latency,
  1538. &sandybridge_cursor_wm_info, latency,
  1539. &plane_wm, &cursor_wm)) {
  1540. val = I915_READ(WM0_PIPEA_ILK);
  1541. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1542. I915_WRITE(WM0_PIPEA_ILK, val |
  1543. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1544. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1545. " plane %d, " "cursor: %d\n",
  1546. plane_wm, cursor_wm);
  1547. enabled |= 1;
  1548. }
  1549. if (g4x_compute_wm0(dev, 1,
  1550. &sandybridge_display_wm_info, latency,
  1551. &sandybridge_cursor_wm_info, latency,
  1552. &plane_wm, &cursor_wm)) {
  1553. val = I915_READ(WM0_PIPEB_ILK);
  1554. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1555. I915_WRITE(WM0_PIPEB_ILK, val |
  1556. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1557. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1558. " plane %d, cursor: %d\n",
  1559. plane_wm, cursor_wm);
  1560. enabled |= 2;
  1561. }
  1562. if ((dev_priv->num_pipe == 3) &&
  1563. g4x_compute_wm0(dev, 2,
  1564. &sandybridge_display_wm_info, latency,
  1565. &sandybridge_cursor_wm_info, latency,
  1566. &plane_wm, &cursor_wm)) {
  1567. val = I915_READ(WM0_PIPEC_IVB);
  1568. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1569. I915_WRITE(WM0_PIPEC_IVB, val |
  1570. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1571. DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
  1572. " plane %d, cursor: %d\n",
  1573. plane_wm, cursor_wm);
  1574. enabled |= 3;
  1575. }
  1576. /*
  1577. * Calculate and update the self-refresh watermark only when one
  1578. * display plane is used.
  1579. *
  1580. * SNB support 3 levels of watermark.
  1581. *
  1582. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  1583. * and disabled in the descending order
  1584. *
  1585. */
  1586. I915_WRITE(WM3_LP_ILK, 0);
  1587. I915_WRITE(WM2_LP_ILK, 0);
  1588. I915_WRITE(WM1_LP_ILK, 0);
  1589. if (!single_plane_enabled(enabled) ||
  1590. dev_priv->sprite_scaling_enabled)
  1591. return;
  1592. enabled = ffs(enabled) - 1;
  1593. /* WM1 */
  1594. if (!ironlake_compute_srwm(dev, 1, enabled,
  1595. SNB_READ_WM1_LATENCY() * 500,
  1596. &sandybridge_display_srwm_info,
  1597. &sandybridge_cursor_srwm_info,
  1598. &fbc_wm, &plane_wm, &cursor_wm))
  1599. return;
  1600. I915_WRITE(WM1_LP_ILK,
  1601. WM1_LP_SR_EN |
  1602. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1603. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1604. (plane_wm << WM1_LP_SR_SHIFT) |
  1605. cursor_wm);
  1606. /* WM2 */
  1607. if (!ironlake_compute_srwm(dev, 2, enabled,
  1608. SNB_READ_WM2_LATENCY() * 500,
  1609. &sandybridge_display_srwm_info,
  1610. &sandybridge_cursor_srwm_info,
  1611. &fbc_wm, &plane_wm, &cursor_wm))
  1612. return;
  1613. I915_WRITE(WM2_LP_ILK,
  1614. WM2_LP_EN |
  1615. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1616. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1617. (plane_wm << WM1_LP_SR_SHIFT) |
  1618. cursor_wm);
  1619. /* WM3 */
  1620. if (!ironlake_compute_srwm(dev, 3, enabled,
  1621. SNB_READ_WM3_LATENCY() * 500,
  1622. &sandybridge_display_srwm_info,
  1623. &sandybridge_cursor_srwm_info,
  1624. &fbc_wm, &plane_wm, &cursor_wm))
  1625. return;
  1626. I915_WRITE(WM3_LP_ILK,
  1627. WM3_LP_EN |
  1628. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1629. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1630. (plane_wm << WM1_LP_SR_SHIFT) |
  1631. cursor_wm);
  1632. }
  1633. static void
  1634. haswell_update_linetime_wm(struct drm_device *dev, int pipe,
  1635. struct drm_display_mode *mode)
  1636. {
  1637. struct drm_i915_private *dev_priv = dev->dev_private;
  1638. u32 temp;
  1639. temp = I915_READ(PIPE_WM_LINETIME(pipe));
  1640. temp &= ~PIPE_WM_LINETIME_MASK;
  1641. /* The WM are computed with base on how long it takes to fill a single
  1642. * row at the given clock rate, multiplied by 8.
  1643. * */
  1644. temp |= PIPE_WM_LINETIME_TIME(
  1645. ((mode->crtc_hdisplay * 1000) / mode->clock) * 8);
  1646. /* IPS watermarks are only used by pipe A, and are ignored by
  1647. * pipes B and C. They are calculated similarly to the common
  1648. * linetime values, except that we are using CD clock frequency
  1649. * in MHz instead of pixel rate for the division.
  1650. *
  1651. * This is a placeholder for the IPS watermark calculation code.
  1652. */
  1653. I915_WRITE(PIPE_WM_LINETIME(pipe), temp);
  1654. }
  1655. static bool
  1656. sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
  1657. uint32_t sprite_width, int pixel_size,
  1658. const struct intel_watermark_params *display,
  1659. int display_latency_ns, int *sprite_wm)
  1660. {
  1661. struct drm_crtc *crtc;
  1662. int clock;
  1663. int entries, tlb_miss;
  1664. crtc = intel_get_crtc_for_plane(dev, plane);
  1665. if (crtc->fb == NULL || !crtc->enabled) {
  1666. *sprite_wm = display->guard_size;
  1667. return false;
  1668. }
  1669. clock = crtc->mode.clock;
  1670. /* Use the small buffer method to calculate the sprite watermark */
  1671. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  1672. tlb_miss = display->fifo_size*display->cacheline_size -
  1673. sprite_width * 8;
  1674. if (tlb_miss > 0)
  1675. entries += tlb_miss;
  1676. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  1677. *sprite_wm = entries + display->guard_size;
  1678. if (*sprite_wm > (int)display->max_wm)
  1679. *sprite_wm = display->max_wm;
  1680. return true;
  1681. }
  1682. static bool
  1683. sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
  1684. uint32_t sprite_width, int pixel_size,
  1685. const struct intel_watermark_params *display,
  1686. int latency_ns, int *sprite_wm)
  1687. {
  1688. struct drm_crtc *crtc;
  1689. unsigned long line_time_us;
  1690. int clock;
  1691. int line_count, line_size;
  1692. int small, large;
  1693. int entries;
  1694. if (!latency_ns) {
  1695. *sprite_wm = 0;
  1696. return false;
  1697. }
  1698. crtc = intel_get_crtc_for_plane(dev, plane);
  1699. clock = crtc->mode.clock;
  1700. if (!clock) {
  1701. *sprite_wm = 0;
  1702. return false;
  1703. }
  1704. line_time_us = (sprite_width * 1000) / clock;
  1705. if (!line_time_us) {
  1706. *sprite_wm = 0;
  1707. return false;
  1708. }
  1709. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1710. line_size = sprite_width * pixel_size;
  1711. /* Use the minimum of the small and large buffer method for primary */
  1712. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1713. large = line_count * line_size;
  1714. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1715. *sprite_wm = entries + display->guard_size;
  1716. return *sprite_wm > 0x3ff ? false : true;
  1717. }
  1718. static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
  1719. uint32_t sprite_width, int pixel_size)
  1720. {
  1721. struct drm_i915_private *dev_priv = dev->dev_private;
  1722. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1723. u32 val;
  1724. int sprite_wm, reg;
  1725. int ret;
  1726. switch (pipe) {
  1727. case 0:
  1728. reg = WM0_PIPEA_ILK;
  1729. break;
  1730. case 1:
  1731. reg = WM0_PIPEB_ILK;
  1732. break;
  1733. case 2:
  1734. reg = WM0_PIPEC_IVB;
  1735. break;
  1736. default:
  1737. return; /* bad pipe */
  1738. }
  1739. ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
  1740. &sandybridge_display_wm_info,
  1741. latency, &sprite_wm);
  1742. if (!ret) {
  1743. DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
  1744. pipe);
  1745. return;
  1746. }
  1747. val = I915_READ(reg);
  1748. val &= ~WM0_PIPE_SPRITE_MASK;
  1749. I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
  1750. DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
  1751. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1752. pixel_size,
  1753. &sandybridge_display_srwm_info,
  1754. SNB_READ_WM1_LATENCY() * 500,
  1755. &sprite_wm);
  1756. if (!ret) {
  1757. DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
  1758. pipe);
  1759. return;
  1760. }
  1761. I915_WRITE(WM1S_LP_ILK, sprite_wm);
  1762. /* Only IVB has two more LP watermarks for sprite */
  1763. if (!IS_IVYBRIDGE(dev))
  1764. return;
  1765. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1766. pixel_size,
  1767. &sandybridge_display_srwm_info,
  1768. SNB_READ_WM2_LATENCY() * 500,
  1769. &sprite_wm);
  1770. if (!ret) {
  1771. DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
  1772. pipe);
  1773. return;
  1774. }
  1775. I915_WRITE(WM2S_LP_IVB, sprite_wm);
  1776. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1777. pixel_size,
  1778. &sandybridge_display_srwm_info,
  1779. SNB_READ_WM3_LATENCY() * 500,
  1780. &sprite_wm);
  1781. if (!ret) {
  1782. DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
  1783. pipe);
  1784. return;
  1785. }
  1786. I915_WRITE(WM3S_LP_IVB, sprite_wm);
  1787. }
  1788. /**
  1789. * intel_update_watermarks - update FIFO watermark values based on current modes
  1790. *
  1791. * Calculate watermark values for the various WM regs based on current mode
  1792. * and plane configuration.
  1793. *
  1794. * There are several cases to deal with here:
  1795. * - normal (i.e. non-self-refresh)
  1796. * - self-refresh (SR) mode
  1797. * - lines are large relative to FIFO size (buffer can hold up to 2)
  1798. * - lines are small relative to FIFO size (buffer can hold more than 2
  1799. * lines), so need to account for TLB latency
  1800. *
  1801. * The normal calculation is:
  1802. * watermark = dotclock * bytes per pixel * latency
  1803. * where latency is platform & configuration dependent (we assume pessimal
  1804. * values here).
  1805. *
  1806. * The SR calculation is:
  1807. * watermark = (trunc(latency/line time)+1) * surface width *
  1808. * bytes per pixel
  1809. * where
  1810. * line time = htotal / dotclock
  1811. * surface width = hdisplay for normal plane and 64 for cursor
  1812. * and latency is assumed to be high, as above.
  1813. *
  1814. * The final value programmed to the register should always be rounded up,
  1815. * and include an extra 2 entries to account for clock crossings.
  1816. *
  1817. * We don't use the sprite, so we can ignore that. And on Crestline we have
  1818. * to set the non-SR watermarks to 8.
  1819. */
  1820. void intel_update_watermarks(struct drm_device *dev)
  1821. {
  1822. struct drm_i915_private *dev_priv = dev->dev_private;
  1823. if (dev_priv->display.update_wm)
  1824. dev_priv->display.update_wm(dev);
  1825. }
  1826. void intel_update_linetime_watermarks(struct drm_device *dev,
  1827. int pipe, struct drm_display_mode *mode)
  1828. {
  1829. struct drm_i915_private *dev_priv = dev->dev_private;
  1830. if (dev_priv->display.update_linetime_wm)
  1831. dev_priv->display.update_linetime_wm(dev, pipe, mode);
  1832. }
  1833. void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
  1834. uint32_t sprite_width, int pixel_size)
  1835. {
  1836. struct drm_i915_private *dev_priv = dev->dev_private;
  1837. if (dev_priv->display.update_sprite_wm)
  1838. dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
  1839. pixel_size);
  1840. }
  1841. static struct drm_i915_gem_object *
  1842. intel_alloc_context_page(struct drm_device *dev)
  1843. {
  1844. struct drm_i915_gem_object *ctx;
  1845. int ret;
  1846. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1847. ctx = i915_gem_alloc_object(dev, 4096);
  1848. if (!ctx) {
  1849. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  1850. return NULL;
  1851. }
  1852. ret = i915_gem_object_pin(ctx, 4096, true);
  1853. if (ret) {
  1854. DRM_ERROR("failed to pin power context: %d\n", ret);
  1855. goto err_unref;
  1856. }
  1857. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  1858. if (ret) {
  1859. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  1860. goto err_unpin;
  1861. }
  1862. return ctx;
  1863. err_unpin:
  1864. i915_gem_object_unpin(ctx);
  1865. err_unref:
  1866. drm_gem_object_unreference(&ctx->base);
  1867. mutex_unlock(&dev->struct_mutex);
  1868. return NULL;
  1869. }
  1870. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  1871. {
  1872. struct drm_i915_private *dev_priv = dev->dev_private;
  1873. u16 rgvswctl;
  1874. rgvswctl = I915_READ16(MEMSWCTL);
  1875. if (rgvswctl & MEMCTL_CMD_STS) {
  1876. DRM_DEBUG("gpu busy, RCS change rejected\n");
  1877. return false; /* still busy with another command */
  1878. }
  1879. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  1880. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  1881. I915_WRITE16(MEMSWCTL, rgvswctl);
  1882. POSTING_READ16(MEMSWCTL);
  1883. rgvswctl |= MEMCTL_CMD_STS;
  1884. I915_WRITE16(MEMSWCTL, rgvswctl);
  1885. return true;
  1886. }
  1887. void ironlake_enable_drps(struct drm_device *dev)
  1888. {
  1889. struct drm_i915_private *dev_priv = dev->dev_private;
  1890. u32 rgvmodectl = I915_READ(MEMMODECTL);
  1891. u8 fmax, fmin, fstart, vstart;
  1892. /* Enable temp reporting */
  1893. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  1894. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  1895. /* 100ms RC evaluation intervals */
  1896. I915_WRITE(RCUPEI, 100000);
  1897. I915_WRITE(RCDNEI, 100000);
  1898. /* Set max/min thresholds to 90ms and 80ms respectively */
  1899. I915_WRITE(RCBMAXAVG, 90000);
  1900. I915_WRITE(RCBMINAVG, 80000);
  1901. I915_WRITE(MEMIHYST, 1);
  1902. /* Set up min, max, and cur for interrupt handling */
  1903. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  1904. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  1905. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  1906. MEMMODE_FSTART_SHIFT;
  1907. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  1908. PXVFREQ_PX_SHIFT;
  1909. dev_priv->fmax = fmax; /* IPS callback will increase this */
  1910. dev_priv->fstart = fstart;
  1911. dev_priv->max_delay = fstart;
  1912. dev_priv->min_delay = fmin;
  1913. dev_priv->cur_delay = fstart;
  1914. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  1915. fmax, fmin, fstart);
  1916. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  1917. /*
  1918. * Interrupts will be enabled in ironlake_irq_postinstall
  1919. */
  1920. I915_WRITE(VIDSTART, vstart);
  1921. POSTING_READ(VIDSTART);
  1922. rgvmodectl |= MEMMODE_SWMODE_EN;
  1923. I915_WRITE(MEMMODECTL, rgvmodectl);
  1924. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  1925. DRM_ERROR("stuck trying to change perf mode\n");
  1926. msleep(1);
  1927. ironlake_set_drps(dev, fstart);
  1928. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  1929. I915_READ(0x112e0);
  1930. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  1931. dev_priv->last_count2 = I915_READ(0x112f4);
  1932. getrawmonotonic(&dev_priv->last_time2);
  1933. }
  1934. void ironlake_disable_drps(struct drm_device *dev)
  1935. {
  1936. struct drm_i915_private *dev_priv = dev->dev_private;
  1937. u16 rgvswctl = I915_READ16(MEMSWCTL);
  1938. /* Ack interrupts, disable EFC interrupt */
  1939. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  1940. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  1941. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  1942. I915_WRITE(DEIIR, DE_PCU_EVENT);
  1943. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  1944. /* Go back to the starting frequency */
  1945. ironlake_set_drps(dev, dev_priv->fstart);
  1946. msleep(1);
  1947. rgvswctl |= MEMCTL_CMD_STS;
  1948. I915_WRITE(MEMSWCTL, rgvswctl);
  1949. msleep(1);
  1950. }
  1951. void gen6_set_rps(struct drm_device *dev, u8 val)
  1952. {
  1953. struct drm_i915_private *dev_priv = dev->dev_private;
  1954. u32 limits;
  1955. limits = 0;
  1956. if (val >= dev_priv->max_delay)
  1957. val = dev_priv->max_delay;
  1958. else
  1959. limits |= dev_priv->max_delay << 24;
  1960. if (val <= dev_priv->min_delay)
  1961. val = dev_priv->min_delay;
  1962. else
  1963. limits |= dev_priv->min_delay << 16;
  1964. if (val == dev_priv->cur_delay)
  1965. return;
  1966. I915_WRITE(GEN6_RPNSWREQ,
  1967. GEN6_FREQUENCY(val) |
  1968. GEN6_OFFSET(0) |
  1969. GEN6_AGGRESSIVE_TURBO);
  1970. /* Make sure we continue to get interrupts
  1971. * until we hit the minimum or maximum frequencies.
  1972. */
  1973. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, limits);
  1974. dev_priv->cur_delay = val;
  1975. }
  1976. void gen6_disable_rps(struct drm_device *dev)
  1977. {
  1978. struct drm_i915_private *dev_priv = dev->dev_private;
  1979. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  1980. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  1981. I915_WRITE(GEN6_PMIER, 0);
  1982. /* Complete PM interrupt masking here doesn't race with the rps work
  1983. * item again unmasking PM interrupts because that is using a different
  1984. * register (PMIMR) to mask PM interrupts. The only risk is in leaving
  1985. * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
  1986. spin_lock_irq(&dev_priv->rps_lock);
  1987. dev_priv->pm_iir = 0;
  1988. spin_unlock_irq(&dev_priv->rps_lock);
  1989. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  1990. }
  1991. int intel_enable_rc6(const struct drm_device *dev)
  1992. {
  1993. /*
  1994. * Respect the kernel parameter if it is set
  1995. */
  1996. if (i915_enable_rc6 >= 0)
  1997. return i915_enable_rc6;
  1998. /*
  1999. * Disable RC6 on Ironlake
  2000. */
  2001. if (INTEL_INFO(dev)->gen == 5)
  2002. return 0;
  2003. /* Sorry Haswell, no RC6 for you for now. */
  2004. if (IS_HASWELL(dev))
  2005. return 0;
  2006. /*
  2007. * Disable rc6 on Sandybridge
  2008. */
  2009. if (INTEL_INFO(dev)->gen == 6) {
  2010. DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
  2011. return INTEL_RC6_ENABLE;
  2012. }
  2013. DRM_DEBUG_DRIVER("RC6 and deep RC6 enabled\n");
  2014. return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
  2015. }
  2016. void gen6_enable_rps(struct drm_i915_private *dev_priv)
  2017. {
  2018. struct intel_ring_buffer *ring;
  2019. u32 rp_state_cap;
  2020. u32 gt_perf_status;
  2021. u32 pcu_mbox, rc6_mask = 0;
  2022. u32 gtfifodbg;
  2023. int rc6_mode;
  2024. int i;
  2025. /* Here begins a magic sequence of register writes to enable
  2026. * auto-downclocking.
  2027. *
  2028. * Perhaps there might be some value in exposing these to
  2029. * userspace...
  2030. */
  2031. I915_WRITE(GEN6_RC_STATE, 0);
  2032. mutex_lock(&dev_priv->dev->struct_mutex);
  2033. /* Clear the DBG now so we don't confuse earlier errors */
  2034. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  2035. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  2036. I915_WRITE(GTFIFODBG, gtfifodbg);
  2037. }
  2038. gen6_gt_force_wake_get(dev_priv);
  2039. rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  2040. gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  2041. /* In units of 100MHz */
  2042. dev_priv->max_delay = rp_state_cap & 0xff;
  2043. dev_priv->min_delay = (rp_state_cap & 0xff0000) >> 16;
  2044. dev_priv->cur_delay = 0;
  2045. /* disable the counters and set deterministic thresholds */
  2046. I915_WRITE(GEN6_RC_CONTROL, 0);
  2047. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  2048. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  2049. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  2050. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  2051. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  2052. for_each_ring(ring, dev_priv, i)
  2053. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  2054. I915_WRITE(GEN6_RC_SLEEP, 0);
  2055. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  2056. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  2057. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  2058. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  2059. rc6_mode = intel_enable_rc6(dev_priv->dev);
  2060. if (rc6_mode & INTEL_RC6_ENABLE)
  2061. rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
  2062. if (rc6_mode & INTEL_RC6p_ENABLE)
  2063. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  2064. if (rc6_mode & INTEL_RC6pp_ENABLE)
  2065. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  2066. DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
  2067. (rc6_mode & INTEL_RC6_ENABLE) ? "on" : "off",
  2068. (rc6_mode & INTEL_RC6p_ENABLE) ? "on" : "off",
  2069. (rc6_mode & INTEL_RC6pp_ENABLE) ? "on" : "off");
  2070. I915_WRITE(GEN6_RC_CONTROL,
  2071. rc6_mask |
  2072. GEN6_RC_CTL_EI_MODE(1) |
  2073. GEN6_RC_CTL_HW_ENABLE);
  2074. I915_WRITE(GEN6_RPNSWREQ,
  2075. GEN6_FREQUENCY(10) |
  2076. GEN6_OFFSET(0) |
  2077. GEN6_AGGRESSIVE_TURBO);
  2078. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  2079. GEN6_FREQUENCY(12));
  2080. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  2081. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  2082. dev_priv->max_delay << 24 |
  2083. dev_priv->min_delay << 16);
  2084. I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
  2085. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
  2086. I915_WRITE(GEN6_RP_UP_EI, 100000);
  2087. I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
  2088. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  2089. I915_WRITE(GEN6_RP_CONTROL,
  2090. GEN6_RP_MEDIA_TURBO |
  2091. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  2092. GEN6_RP_MEDIA_IS_GFX |
  2093. GEN6_RP_ENABLE |
  2094. GEN6_RP_UP_BUSY_AVG |
  2095. GEN6_RP_DOWN_IDLE_CONT);
  2096. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  2097. 500))
  2098. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  2099. I915_WRITE(GEN6_PCODE_DATA, 0);
  2100. I915_WRITE(GEN6_PCODE_MAILBOX,
  2101. GEN6_PCODE_READY |
  2102. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  2103. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  2104. 500))
  2105. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  2106. /* Check for overclock support */
  2107. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  2108. 500))
  2109. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  2110. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
  2111. pcu_mbox = I915_READ(GEN6_PCODE_DATA);
  2112. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  2113. 500))
  2114. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  2115. if (pcu_mbox & (1<<31)) { /* OC supported */
  2116. dev_priv->max_delay = pcu_mbox & 0xff;
  2117. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
  2118. }
  2119. gen6_set_rps(dev_priv->dev, (gt_perf_status & 0xff00) >> 8);
  2120. /* requires MSI enabled */
  2121. I915_WRITE(GEN6_PMIER,
  2122. GEN6_PM_MBOX_EVENT |
  2123. GEN6_PM_THERMAL_EVENT |
  2124. GEN6_PM_RP_DOWN_TIMEOUT |
  2125. GEN6_PM_RP_UP_THRESHOLD |
  2126. GEN6_PM_RP_DOWN_THRESHOLD |
  2127. GEN6_PM_RP_UP_EI_EXPIRED |
  2128. GEN6_PM_RP_DOWN_EI_EXPIRED);
  2129. spin_lock_irq(&dev_priv->rps_lock);
  2130. WARN_ON(dev_priv->pm_iir != 0);
  2131. I915_WRITE(GEN6_PMIMR, 0);
  2132. spin_unlock_irq(&dev_priv->rps_lock);
  2133. /* enable all PM interrupts */
  2134. I915_WRITE(GEN6_PMINTRMSK, 0);
  2135. gen6_gt_force_wake_put(dev_priv);
  2136. mutex_unlock(&dev_priv->dev->struct_mutex);
  2137. }
  2138. void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
  2139. {
  2140. int min_freq = 15;
  2141. int gpu_freq, ia_freq, max_ia_freq;
  2142. int scaling_factor = 180;
  2143. max_ia_freq = cpufreq_quick_get_max(0);
  2144. /*
  2145. * Default to measured freq if none found, PCU will ensure we don't go
  2146. * over
  2147. */
  2148. if (!max_ia_freq)
  2149. max_ia_freq = tsc_khz;
  2150. /* Convert from kHz to MHz */
  2151. max_ia_freq /= 1000;
  2152. mutex_lock(&dev_priv->dev->struct_mutex);
  2153. /*
  2154. * For each potential GPU frequency, load a ring frequency we'd like
  2155. * to use for memory access. We do this by specifying the IA frequency
  2156. * the PCU should use as a reference to determine the ring frequency.
  2157. */
  2158. for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
  2159. gpu_freq--) {
  2160. int diff = dev_priv->max_delay - gpu_freq;
  2161. /*
  2162. * For GPU frequencies less than 750MHz, just use the lowest
  2163. * ring freq.
  2164. */
  2165. if (gpu_freq < min_freq)
  2166. ia_freq = 800;
  2167. else
  2168. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  2169. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  2170. I915_WRITE(GEN6_PCODE_DATA,
  2171. (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
  2172. gpu_freq);
  2173. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
  2174. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  2175. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
  2176. GEN6_PCODE_READY) == 0, 10)) {
  2177. DRM_ERROR("pcode write of freq table timed out\n");
  2178. continue;
  2179. }
  2180. }
  2181. mutex_unlock(&dev_priv->dev->struct_mutex);
  2182. }
  2183. static void ironlake_teardown_rc6(struct drm_device *dev)
  2184. {
  2185. struct drm_i915_private *dev_priv = dev->dev_private;
  2186. if (dev_priv->renderctx) {
  2187. i915_gem_object_unpin(dev_priv->renderctx);
  2188. drm_gem_object_unreference(&dev_priv->renderctx->base);
  2189. dev_priv->renderctx = NULL;
  2190. }
  2191. if (dev_priv->pwrctx) {
  2192. i915_gem_object_unpin(dev_priv->pwrctx);
  2193. drm_gem_object_unreference(&dev_priv->pwrctx->base);
  2194. dev_priv->pwrctx = NULL;
  2195. }
  2196. }
  2197. void ironlake_disable_rc6(struct drm_device *dev)
  2198. {
  2199. struct drm_i915_private *dev_priv = dev->dev_private;
  2200. if (I915_READ(PWRCTXA)) {
  2201. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  2202. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  2203. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  2204. 50);
  2205. I915_WRITE(PWRCTXA, 0);
  2206. POSTING_READ(PWRCTXA);
  2207. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  2208. POSTING_READ(RSTDBYCTL);
  2209. }
  2210. ironlake_teardown_rc6(dev);
  2211. }
  2212. static int ironlake_setup_rc6(struct drm_device *dev)
  2213. {
  2214. struct drm_i915_private *dev_priv = dev->dev_private;
  2215. if (dev_priv->renderctx == NULL)
  2216. dev_priv->renderctx = intel_alloc_context_page(dev);
  2217. if (!dev_priv->renderctx)
  2218. return -ENOMEM;
  2219. if (dev_priv->pwrctx == NULL)
  2220. dev_priv->pwrctx = intel_alloc_context_page(dev);
  2221. if (!dev_priv->pwrctx) {
  2222. ironlake_teardown_rc6(dev);
  2223. return -ENOMEM;
  2224. }
  2225. return 0;
  2226. }
  2227. void ironlake_enable_rc6(struct drm_device *dev)
  2228. {
  2229. struct drm_i915_private *dev_priv = dev->dev_private;
  2230. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  2231. int ret;
  2232. /* rc6 disabled by default due to repeated reports of hanging during
  2233. * boot and resume.
  2234. */
  2235. if (!intel_enable_rc6(dev))
  2236. return;
  2237. mutex_lock(&dev->struct_mutex);
  2238. ret = ironlake_setup_rc6(dev);
  2239. if (ret) {
  2240. mutex_unlock(&dev->struct_mutex);
  2241. return;
  2242. }
  2243. /*
  2244. * GPU can automatically power down the render unit if given a page
  2245. * to save state.
  2246. */
  2247. ret = intel_ring_begin(ring, 6);
  2248. if (ret) {
  2249. ironlake_teardown_rc6(dev);
  2250. mutex_unlock(&dev->struct_mutex);
  2251. return;
  2252. }
  2253. intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  2254. intel_ring_emit(ring, MI_SET_CONTEXT);
  2255. intel_ring_emit(ring, dev_priv->renderctx->gtt_offset |
  2256. MI_MM_SPACE_GTT |
  2257. MI_SAVE_EXT_STATE_EN |
  2258. MI_RESTORE_EXT_STATE_EN |
  2259. MI_RESTORE_INHIBIT);
  2260. intel_ring_emit(ring, MI_SUSPEND_FLUSH);
  2261. intel_ring_emit(ring, MI_NOOP);
  2262. intel_ring_emit(ring, MI_FLUSH);
  2263. intel_ring_advance(ring);
  2264. /*
  2265. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  2266. * does an implicit flush, combined with MI_FLUSH above, it should be
  2267. * safe to assume that renderctx is valid
  2268. */
  2269. ret = intel_wait_ring_idle(ring);
  2270. if (ret) {
  2271. DRM_ERROR("failed to enable ironlake power power savings\n");
  2272. ironlake_teardown_rc6(dev);
  2273. mutex_unlock(&dev->struct_mutex);
  2274. return;
  2275. }
  2276. I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
  2277. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  2278. mutex_unlock(&dev->struct_mutex);
  2279. }
  2280. static unsigned long intel_pxfreq(u32 vidfreq)
  2281. {
  2282. unsigned long freq;
  2283. int div = (vidfreq & 0x3f0000) >> 16;
  2284. int post = (vidfreq & 0x3000) >> 12;
  2285. int pre = (vidfreq & 0x7);
  2286. if (!pre)
  2287. return 0;
  2288. freq = ((div * 133333) / ((1<<post) * pre));
  2289. return freq;
  2290. }
  2291. static const struct cparams {
  2292. u16 i;
  2293. u16 t;
  2294. u16 m;
  2295. u16 c;
  2296. } cparams[] = {
  2297. { 1, 1333, 301, 28664 },
  2298. { 1, 1066, 294, 24460 },
  2299. { 1, 800, 294, 25192 },
  2300. { 0, 1333, 276, 27605 },
  2301. { 0, 1066, 276, 27605 },
  2302. { 0, 800, 231, 23784 },
  2303. };
  2304. unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
  2305. {
  2306. u64 total_count, diff, ret;
  2307. u32 count1, count2, count3, m = 0, c = 0;
  2308. unsigned long now = jiffies_to_msecs(jiffies), diff1;
  2309. int i;
  2310. diff1 = now - dev_priv->last_time1;
  2311. /* Prevent division-by-zero if we are asking too fast.
  2312. * Also, we don't get interesting results if we are polling
  2313. * faster than once in 10ms, so just return the saved value
  2314. * in such cases.
  2315. */
  2316. if (diff1 <= 10)
  2317. return dev_priv->chipset_power;
  2318. count1 = I915_READ(DMIEC);
  2319. count2 = I915_READ(DDREC);
  2320. count3 = I915_READ(CSIEC);
  2321. total_count = count1 + count2 + count3;
  2322. /* FIXME: handle per-counter overflow */
  2323. if (total_count < dev_priv->last_count1) {
  2324. diff = ~0UL - dev_priv->last_count1;
  2325. diff += total_count;
  2326. } else {
  2327. diff = total_count - dev_priv->last_count1;
  2328. }
  2329. for (i = 0; i < ARRAY_SIZE(cparams); i++) {
  2330. if (cparams[i].i == dev_priv->c_m &&
  2331. cparams[i].t == dev_priv->r_t) {
  2332. m = cparams[i].m;
  2333. c = cparams[i].c;
  2334. break;
  2335. }
  2336. }
  2337. diff = div_u64(diff, diff1);
  2338. ret = ((m * diff) + c);
  2339. ret = div_u64(ret, 10);
  2340. dev_priv->last_count1 = total_count;
  2341. dev_priv->last_time1 = now;
  2342. dev_priv->chipset_power = ret;
  2343. return ret;
  2344. }
  2345. unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
  2346. {
  2347. unsigned long m, x, b;
  2348. u32 tsfs;
  2349. tsfs = I915_READ(TSFS);
  2350. m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
  2351. x = I915_READ8(TR1);
  2352. b = tsfs & TSFS_INTR_MASK;
  2353. return ((m * x) / 127) - b;
  2354. }
  2355. static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
  2356. {
  2357. static const struct v_table {
  2358. u16 vd; /* in .1 mil */
  2359. u16 vm; /* in .1 mil */
  2360. } v_table[] = {
  2361. { 0, 0, },
  2362. { 375, 0, },
  2363. { 500, 0, },
  2364. { 625, 0, },
  2365. { 750, 0, },
  2366. { 875, 0, },
  2367. { 1000, 0, },
  2368. { 1125, 0, },
  2369. { 4125, 3000, },
  2370. { 4125, 3000, },
  2371. { 4125, 3000, },
  2372. { 4125, 3000, },
  2373. { 4125, 3000, },
  2374. { 4125, 3000, },
  2375. { 4125, 3000, },
  2376. { 4125, 3000, },
  2377. { 4125, 3000, },
  2378. { 4125, 3000, },
  2379. { 4125, 3000, },
  2380. { 4125, 3000, },
  2381. { 4125, 3000, },
  2382. { 4125, 3000, },
  2383. { 4125, 3000, },
  2384. { 4125, 3000, },
  2385. { 4125, 3000, },
  2386. { 4125, 3000, },
  2387. { 4125, 3000, },
  2388. { 4125, 3000, },
  2389. { 4125, 3000, },
  2390. { 4125, 3000, },
  2391. { 4125, 3000, },
  2392. { 4125, 3000, },
  2393. { 4250, 3125, },
  2394. { 4375, 3250, },
  2395. { 4500, 3375, },
  2396. { 4625, 3500, },
  2397. { 4750, 3625, },
  2398. { 4875, 3750, },
  2399. { 5000, 3875, },
  2400. { 5125, 4000, },
  2401. { 5250, 4125, },
  2402. { 5375, 4250, },
  2403. { 5500, 4375, },
  2404. { 5625, 4500, },
  2405. { 5750, 4625, },
  2406. { 5875, 4750, },
  2407. { 6000, 4875, },
  2408. { 6125, 5000, },
  2409. { 6250, 5125, },
  2410. { 6375, 5250, },
  2411. { 6500, 5375, },
  2412. { 6625, 5500, },
  2413. { 6750, 5625, },
  2414. { 6875, 5750, },
  2415. { 7000, 5875, },
  2416. { 7125, 6000, },
  2417. { 7250, 6125, },
  2418. { 7375, 6250, },
  2419. { 7500, 6375, },
  2420. { 7625, 6500, },
  2421. { 7750, 6625, },
  2422. { 7875, 6750, },
  2423. { 8000, 6875, },
  2424. { 8125, 7000, },
  2425. { 8250, 7125, },
  2426. { 8375, 7250, },
  2427. { 8500, 7375, },
  2428. { 8625, 7500, },
  2429. { 8750, 7625, },
  2430. { 8875, 7750, },
  2431. { 9000, 7875, },
  2432. { 9125, 8000, },
  2433. { 9250, 8125, },
  2434. { 9375, 8250, },
  2435. { 9500, 8375, },
  2436. { 9625, 8500, },
  2437. { 9750, 8625, },
  2438. { 9875, 8750, },
  2439. { 10000, 8875, },
  2440. { 10125, 9000, },
  2441. { 10250, 9125, },
  2442. { 10375, 9250, },
  2443. { 10500, 9375, },
  2444. { 10625, 9500, },
  2445. { 10750, 9625, },
  2446. { 10875, 9750, },
  2447. { 11000, 9875, },
  2448. { 11125, 10000, },
  2449. { 11250, 10125, },
  2450. { 11375, 10250, },
  2451. { 11500, 10375, },
  2452. { 11625, 10500, },
  2453. { 11750, 10625, },
  2454. { 11875, 10750, },
  2455. { 12000, 10875, },
  2456. { 12125, 11000, },
  2457. { 12250, 11125, },
  2458. { 12375, 11250, },
  2459. { 12500, 11375, },
  2460. { 12625, 11500, },
  2461. { 12750, 11625, },
  2462. { 12875, 11750, },
  2463. { 13000, 11875, },
  2464. { 13125, 12000, },
  2465. { 13250, 12125, },
  2466. { 13375, 12250, },
  2467. { 13500, 12375, },
  2468. { 13625, 12500, },
  2469. { 13750, 12625, },
  2470. { 13875, 12750, },
  2471. { 14000, 12875, },
  2472. { 14125, 13000, },
  2473. { 14250, 13125, },
  2474. { 14375, 13250, },
  2475. { 14500, 13375, },
  2476. { 14625, 13500, },
  2477. { 14750, 13625, },
  2478. { 14875, 13750, },
  2479. { 15000, 13875, },
  2480. { 15125, 14000, },
  2481. { 15250, 14125, },
  2482. { 15375, 14250, },
  2483. { 15500, 14375, },
  2484. { 15625, 14500, },
  2485. { 15750, 14625, },
  2486. { 15875, 14750, },
  2487. { 16000, 14875, },
  2488. { 16125, 15000, },
  2489. };
  2490. if (dev_priv->info->is_mobile)
  2491. return v_table[pxvid].vm;
  2492. else
  2493. return v_table[pxvid].vd;
  2494. }
  2495. void i915_update_gfx_val(struct drm_i915_private *dev_priv)
  2496. {
  2497. struct timespec now, diff1;
  2498. u64 diff;
  2499. unsigned long diffms;
  2500. u32 count;
  2501. if (dev_priv->info->gen != 5)
  2502. return;
  2503. getrawmonotonic(&now);
  2504. diff1 = timespec_sub(now, dev_priv->last_time2);
  2505. /* Don't divide by 0 */
  2506. diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
  2507. if (!diffms)
  2508. return;
  2509. count = I915_READ(GFXEC);
  2510. if (count < dev_priv->last_count2) {
  2511. diff = ~0UL - dev_priv->last_count2;
  2512. diff += count;
  2513. } else {
  2514. diff = count - dev_priv->last_count2;
  2515. }
  2516. dev_priv->last_count2 = count;
  2517. dev_priv->last_time2 = now;
  2518. /* More magic constants... */
  2519. diff = diff * 1181;
  2520. diff = div_u64(diff, diffms * 10);
  2521. dev_priv->gfx_power = diff;
  2522. }
  2523. unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
  2524. {
  2525. unsigned long t, corr, state1, corr2, state2;
  2526. u32 pxvid, ext_v;
  2527. pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->cur_delay * 4));
  2528. pxvid = (pxvid >> 24) & 0x7f;
  2529. ext_v = pvid_to_extvid(dev_priv, pxvid);
  2530. state1 = ext_v;
  2531. t = i915_mch_val(dev_priv);
  2532. /* Revel in the empirically derived constants */
  2533. /* Correction factor in 1/100000 units */
  2534. if (t > 80)
  2535. corr = ((t * 2349) + 135940);
  2536. else if (t >= 50)
  2537. corr = ((t * 964) + 29317);
  2538. else /* < 50 */
  2539. corr = ((t * 301) + 1004);
  2540. corr = corr * ((150142 * state1) / 10000 - 78642);
  2541. corr /= 100000;
  2542. corr2 = (corr * dev_priv->corr);
  2543. state2 = (corr2 * state1) / 10000;
  2544. state2 /= 100; /* convert to mW */
  2545. i915_update_gfx_val(dev_priv);
  2546. return dev_priv->gfx_power + state2;
  2547. }
  2548. /* Global for IPS driver to get at the current i915 device */
  2549. static struct drm_i915_private *i915_mch_dev;
  2550. /*
  2551. * Lock protecting IPS related data structures
  2552. * - i915_mch_dev
  2553. * - dev_priv->max_delay
  2554. * - dev_priv->min_delay
  2555. * - dev_priv->fmax
  2556. * - dev_priv->gpu_busy
  2557. */
  2558. static DEFINE_SPINLOCK(mchdev_lock);
  2559. /**
  2560. * i915_read_mch_val - return value for IPS use
  2561. *
  2562. * Calculate and return a value for the IPS driver to use when deciding whether
  2563. * we have thermal and power headroom to increase CPU or GPU power budget.
  2564. */
  2565. unsigned long i915_read_mch_val(void)
  2566. {
  2567. struct drm_i915_private *dev_priv;
  2568. unsigned long chipset_val, graphics_val, ret = 0;
  2569. spin_lock(&mchdev_lock);
  2570. if (!i915_mch_dev)
  2571. goto out_unlock;
  2572. dev_priv = i915_mch_dev;
  2573. chipset_val = i915_chipset_val(dev_priv);
  2574. graphics_val = i915_gfx_val(dev_priv);
  2575. ret = chipset_val + graphics_val;
  2576. out_unlock:
  2577. spin_unlock(&mchdev_lock);
  2578. return ret;
  2579. }
  2580. EXPORT_SYMBOL_GPL(i915_read_mch_val);
  2581. /**
  2582. * i915_gpu_raise - raise GPU frequency limit
  2583. *
  2584. * Raise the limit; IPS indicates we have thermal headroom.
  2585. */
  2586. bool i915_gpu_raise(void)
  2587. {
  2588. struct drm_i915_private *dev_priv;
  2589. bool ret = true;
  2590. spin_lock(&mchdev_lock);
  2591. if (!i915_mch_dev) {
  2592. ret = false;
  2593. goto out_unlock;
  2594. }
  2595. dev_priv = i915_mch_dev;
  2596. if (dev_priv->max_delay > dev_priv->fmax)
  2597. dev_priv->max_delay--;
  2598. out_unlock:
  2599. spin_unlock(&mchdev_lock);
  2600. return ret;
  2601. }
  2602. EXPORT_SYMBOL_GPL(i915_gpu_raise);
  2603. /**
  2604. * i915_gpu_lower - lower GPU frequency limit
  2605. *
  2606. * IPS indicates we're close to a thermal limit, so throttle back the GPU
  2607. * frequency maximum.
  2608. */
  2609. bool i915_gpu_lower(void)
  2610. {
  2611. struct drm_i915_private *dev_priv;
  2612. bool ret = true;
  2613. spin_lock(&mchdev_lock);
  2614. if (!i915_mch_dev) {
  2615. ret = false;
  2616. goto out_unlock;
  2617. }
  2618. dev_priv = i915_mch_dev;
  2619. if (dev_priv->max_delay < dev_priv->min_delay)
  2620. dev_priv->max_delay++;
  2621. out_unlock:
  2622. spin_unlock(&mchdev_lock);
  2623. return ret;
  2624. }
  2625. EXPORT_SYMBOL_GPL(i915_gpu_lower);
  2626. /**
  2627. * i915_gpu_busy - indicate GPU business to IPS
  2628. *
  2629. * Tell the IPS driver whether or not the GPU is busy.
  2630. */
  2631. bool i915_gpu_busy(void)
  2632. {
  2633. struct drm_i915_private *dev_priv;
  2634. bool ret = false;
  2635. spin_lock(&mchdev_lock);
  2636. if (!i915_mch_dev)
  2637. goto out_unlock;
  2638. dev_priv = i915_mch_dev;
  2639. ret = dev_priv->busy;
  2640. out_unlock:
  2641. spin_unlock(&mchdev_lock);
  2642. return ret;
  2643. }
  2644. EXPORT_SYMBOL_GPL(i915_gpu_busy);
  2645. /**
  2646. * i915_gpu_turbo_disable - disable graphics turbo
  2647. *
  2648. * Disable graphics turbo by resetting the max frequency and setting the
  2649. * current frequency to the default.
  2650. */
  2651. bool i915_gpu_turbo_disable(void)
  2652. {
  2653. struct drm_i915_private *dev_priv;
  2654. bool ret = true;
  2655. spin_lock(&mchdev_lock);
  2656. if (!i915_mch_dev) {
  2657. ret = false;
  2658. goto out_unlock;
  2659. }
  2660. dev_priv = i915_mch_dev;
  2661. dev_priv->max_delay = dev_priv->fstart;
  2662. if (!ironlake_set_drps(dev_priv->dev, dev_priv->fstart))
  2663. ret = false;
  2664. out_unlock:
  2665. spin_unlock(&mchdev_lock);
  2666. return ret;
  2667. }
  2668. EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
  2669. /**
  2670. * Tells the intel_ips driver that the i915 driver is now loaded, if
  2671. * IPS got loaded first.
  2672. *
  2673. * This awkward dance is so that neither module has to depend on the
  2674. * other in order for IPS to do the appropriate communication of
  2675. * GPU turbo limits to i915.
  2676. */
  2677. static void
  2678. ips_ping_for_i915_load(void)
  2679. {
  2680. void (*link)(void);
  2681. link = symbol_get(ips_link_to_i915_driver);
  2682. if (link) {
  2683. link();
  2684. symbol_put(ips_link_to_i915_driver);
  2685. }
  2686. }
  2687. void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
  2688. {
  2689. spin_lock(&mchdev_lock);
  2690. i915_mch_dev = dev_priv;
  2691. dev_priv->mchdev_lock = &mchdev_lock;
  2692. spin_unlock(&mchdev_lock);
  2693. ips_ping_for_i915_load();
  2694. }
  2695. void intel_gpu_ips_teardown(void)
  2696. {
  2697. spin_lock(&mchdev_lock);
  2698. i915_mch_dev = NULL;
  2699. spin_unlock(&mchdev_lock);
  2700. }
  2701. void intel_init_emon(struct drm_device *dev)
  2702. {
  2703. struct drm_i915_private *dev_priv = dev->dev_private;
  2704. u32 lcfuse;
  2705. u8 pxw[16];
  2706. int i;
  2707. /* Disable to program */
  2708. I915_WRITE(ECR, 0);
  2709. POSTING_READ(ECR);
  2710. /* Program energy weights for various events */
  2711. I915_WRITE(SDEW, 0x15040d00);
  2712. I915_WRITE(CSIEW0, 0x007f0000);
  2713. I915_WRITE(CSIEW1, 0x1e220004);
  2714. I915_WRITE(CSIEW2, 0x04000004);
  2715. for (i = 0; i < 5; i++)
  2716. I915_WRITE(PEW + (i * 4), 0);
  2717. for (i = 0; i < 3; i++)
  2718. I915_WRITE(DEW + (i * 4), 0);
  2719. /* Program P-state weights to account for frequency power adjustment */
  2720. for (i = 0; i < 16; i++) {
  2721. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  2722. unsigned long freq = intel_pxfreq(pxvidfreq);
  2723. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  2724. PXVFREQ_PX_SHIFT;
  2725. unsigned long val;
  2726. val = vid * vid;
  2727. val *= (freq / 1000);
  2728. val *= 255;
  2729. val /= (127*127*900);
  2730. if (val > 0xff)
  2731. DRM_ERROR("bad pxval: %ld\n", val);
  2732. pxw[i] = val;
  2733. }
  2734. /* Render standby states get 0 weight */
  2735. pxw[14] = 0;
  2736. pxw[15] = 0;
  2737. for (i = 0; i < 4; i++) {
  2738. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  2739. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  2740. I915_WRITE(PXW + (i * 4), val);
  2741. }
  2742. /* Adjust magic regs to magic values (more experimental results) */
  2743. I915_WRITE(OGW0, 0);
  2744. I915_WRITE(OGW1, 0);
  2745. I915_WRITE(EG0, 0x00007f00);
  2746. I915_WRITE(EG1, 0x0000000e);
  2747. I915_WRITE(EG2, 0x000e0000);
  2748. I915_WRITE(EG3, 0x68000300);
  2749. I915_WRITE(EG4, 0x42000000);
  2750. I915_WRITE(EG5, 0x00140031);
  2751. I915_WRITE(EG6, 0);
  2752. I915_WRITE(EG7, 0);
  2753. for (i = 0; i < 8; i++)
  2754. I915_WRITE(PXWL + (i * 4), 0);
  2755. /* Enable PMON + select events */
  2756. I915_WRITE(ECR, 0x80000019);
  2757. lcfuse = I915_READ(LCFUSE02);
  2758. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  2759. }
  2760. static void ironlake_init_clock_gating(struct drm_device *dev)
  2761. {
  2762. struct drm_i915_private *dev_priv = dev->dev_private;
  2763. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  2764. /* Required for FBC */
  2765. dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
  2766. DPFCRUNIT_CLOCK_GATE_DISABLE |
  2767. DPFDUNIT_CLOCK_GATE_DISABLE;
  2768. /* Required for CxSR */
  2769. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  2770. I915_WRITE(PCH_3DCGDIS0,
  2771. MARIUNIT_CLOCK_GATE_DISABLE |
  2772. SVSMUNIT_CLOCK_GATE_DISABLE);
  2773. I915_WRITE(PCH_3DCGDIS1,
  2774. VFMUNIT_CLOCK_GATE_DISABLE);
  2775. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  2776. /*
  2777. * According to the spec the following bits should be set in
  2778. * order to enable memory self-refresh
  2779. * The bit 22/21 of 0x42004
  2780. * The bit 5 of 0x42020
  2781. * The bit 15 of 0x45000
  2782. */
  2783. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  2784. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  2785. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  2786. I915_WRITE(ILK_DSPCLK_GATE,
  2787. (I915_READ(ILK_DSPCLK_GATE) |
  2788. ILK_DPARB_CLK_GATE));
  2789. I915_WRITE(DISP_ARB_CTL,
  2790. (I915_READ(DISP_ARB_CTL) |
  2791. DISP_FBC_WM_DIS));
  2792. I915_WRITE(WM3_LP_ILK, 0);
  2793. I915_WRITE(WM2_LP_ILK, 0);
  2794. I915_WRITE(WM1_LP_ILK, 0);
  2795. /*
  2796. * Based on the document from hardware guys the following bits
  2797. * should be set unconditionally in order to enable FBC.
  2798. * The bit 22 of 0x42000
  2799. * The bit 22 of 0x42004
  2800. * The bit 7,8,9 of 0x42020.
  2801. */
  2802. if (IS_IRONLAKE_M(dev)) {
  2803. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  2804. I915_READ(ILK_DISPLAY_CHICKEN1) |
  2805. ILK_FBCQ_DIS);
  2806. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  2807. I915_READ(ILK_DISPLAY_CHICKEN2) |
  2808. ILK_DPARB_GATE);
  2809. I915_WRITE(ILK_DSPCLK_GATE,
  2810. I915_READ(ILK_DSPCLK_GATE) |
  2811. ILK_DPFC_DIS1 |
  2812. ILK_DPFC_DIS2 |
  2813. ILK_CLK_FBC);
  2814. }
  2815. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  2816. I915_READ(ILK_DISPLAY_CHICKEN2) |
  2817. ILK_ELPIN_409_SELECT);
  2818. I915_WRITE(_3D_CHICKEN2,
  2819. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  2820. _3D_CHICKEN2_WM_READ_PIPELINED);
  2821. }
  2822. static void gen6_init_clock_gating(struct drm_device *dev)
  2823. {
  2824. struct drm_i915_private *dev_priv = dev->dev_private;
  2825. int pipe;
  2826. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  2827. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  2828. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  2829. I915_READ(ILK_DISPLAY_CHICKEN2) |
  2830. ILK_ELPIN_409_SELECT);
  2831. I915_WRITE(WM3_LP_ILK, 0);
  2832. I915_WRITE(WM2_LP_ILK, 0);
  2833. I915_WRITE(WM1_LP_ILK, 0);
  2834. I915_WRITE(CACHE_MODE_0,
  2835. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  2836. I915_WRITE(GEN6_UCGCTL1,
  2837. I915_READ(GEN6_UCGCTL1) |
  2838. GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
  2839. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  2840. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  2841. * gating disable must be set. Failure to set it results in
  2842. * flickering pixels due to Z write ordering failures after
  2843. * some amount of runtime in the Mesa "fire" demo, and Unigine
  2844. * Sanctuary and Tropics, and apparently anything else with
  2845. * alpha test or pixel discard.
  2846. *
  2847. * According to the spec, bit 11 (RCCUNIT) must also be set,
  2848. * but we didn't debug actual testcases to find it out.
  2849. */
  2850. I915_WRITE(GEN6_UCGCTL2,
  2851. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  2852. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  2853. /* Bspec says we need to always set all mask bits. */
  2854. I915_WRITE(_3D_CHICKEN, (0xFFFF << 16) |
  2855. _3D_CHICKEN_SF_DISABLE_FASTCLIP_CULL);
  2856. /*
  2857. * According to the spec the following bits should be
  2858. * set in order to enable memory self-refresh and fbc:
  2859. * The bit21 and bit22 of 0x42000
  2860. * The bit21 and bit22 of 0x42004
  2861. * The bit5 and bit7 of 0x42020
  2862. * The bit14 of 0x70180
  2863. * The bit14 of 0x71180
  2864. */
  2865. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  2866. I915_READ(ILK_DISPLAY_CHICKEN1) |
  2867. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  2868. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  2869. I915_READ(ILK_DISPLAY_CHICKEN2) |
  2870. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  2871. I915_WRITE(ILK_DSPCLK_GATE,
  2872. I915_READ(ILK_DSPCLK_GATE) |
  2873. ILK_DPARB_CLK_GATE |
  2874. ILK_DPFD_CLK_GATE);
  2875. for_each_pipe(pipe) {
  2876. I915_WRITE(DSPCNTR(pipe),
  2877. I915_READ(DSPCNTR(pipe)) |
  2878. DISPPLANE_TRICKLE_FEED_DISABLE);
  2879. intel_flush_display_plane(dev_priv, pipe);
  2880. }
  2881. }
  2882. static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
  2883. {
  2884. uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
  2885. reg &= ~GEN7_FF_SCHED_MASK;
  2886. reg |= GEN7_FF_TS_SCHED_HW;
  2887. reg |= GEN7_FF_VS_SCHED_HW;
  2888. reg |= GEN7_FF_DS_SCHED_HW;
  2889. I915_WRITE(GEN7_FF_THREAD_MODE, reg);
  2890. }
  2891. static void ivybridge_init_clock_gating(struct drm_device *dev)
  2892. {
  2893. struct drm_i915_private *dev_priv = dev->dev_private;
  2894. int pipe;
  2895. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  2896. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  2897. I915_WRITE(WM3_LP_ILK, 0);
  2898. I915_WRITE(WM2_LP_ILK, 0);
  2899. I915_WRITE(WM1_LP_ILK, 0);
  2900. /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  2901. * This implements the WaDisableRCZUnitClockGating workaround.
  2902. */
  2903. I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  2904. I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
  2905. I915_WRITE(IVB_CHICKEN3,
  2906. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  2907. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  2908. /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
  2909. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  2910. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  2911. /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
  2912. I915_WRITE(GEN7_L3CNTLREG1,
  2913. GEN7_WA_FOR_GEN7_L3_CONTROL);
  2914. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  2915. GEN7_WA_L3_CHICKEN_MODE);
  2916. /* This is required by WaCatErrorRejectionIssue */
  2917. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  2918. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  2919. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  2920. for_each_pipe(pipe) {
  2921. I915_WRITE(DSPCNTR(pipe),
  2922. I915_READ(DSPCNTR(pipe)) |
  2923. DISPPLANE_TRICKLE_FEED_DISABLE);
  2924. intel_flush_display_plane(dev_priv, pipe);
  2925. }
  2926. gen7_setup_fixed_func_scheduler(dev_priv);
  2927. /* WaDisable4x2SubspanOptimization */
  2928. I915_WRITE(CACHE_MODE_1,
  2929. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  2930. }
  2931. static void valleyview_init_clock_gating(struct drm_device *dev)
  2932. {
  2933. struct drm_i915_private *dev_priv = dev->dev_private;
  2934. int pipe;
  2935. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  2936. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  2937. I915_WRITE(WM3_LP_ILK, 0);
  2938. I915_WRITE(WM2_LP_ILK, 0);
  2939. I915_WRITE(WM1_LP_ILK, 0);
  2940. /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  2941. * This implements the WaDisableRCZUnitClockGating workaround.
  2942. */
  2943. I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  2944. I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
  2945. I915_WRITE(IVB_CHICKEN3,
  2946. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  2947. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  2948. /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
  2949. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  2950. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  2951. /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
  2952. I915_WRITE(GEN7_L3CNTLREG1, GEN7_WA_FOR_GEN7_L3_CONTROL);
  2953. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
  2954. /* This is required by WaCatErrorRejectionIssue */
  2955. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  2956. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  2957. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  2958. for_each_pipe(pipe) {
  2959. I915_WRITE(DSPCNTR(pipe),
  2960. I915_READ(DSPCNTR(pipe)) |
  2961. DISPPLANE_TRICKLE_FEED_DISABLE);
  2962. intel_flush_display_plane(dev_priv, pipe);
  2963. }
  2964. I915_WRITE(CACHE_MODE_1,
  2965. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  2966. }
  2967. static void g4x_init_clock_gating(struct drm_device *dev)
  2968. {
  2969. struct drm_i915_private *dev_priv = dev->dev_private;
  2970. uint32_t dspclk_gate;
  2971. I915_WRITE(RENCLK_GATE_D1, 0);
  2972. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  2973. GS_UNIT_CLOCK_GATE_DISABLE |
  2974. CL_UNIT_CLOCK_GATE_DISABLE);
  2975. I915_WRITE(RAMCLK_GATE_D, 0);
  2976. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  2977. OVRUNIT_CLOCK_GATE_DISABLE |
  2978. OVCUNIT_CLOCK_GATE_DISABLE;
  2979. if (IS_GM45(dev))
  2980. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  2981. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  2982. }
  2983. static void crestline_init_clock_gating(struct drm_device *dev)
  2984. {
  2985. struct drm_i915_private *dev_priv = dev->dev_private;
  2986. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  2987. I915_WRITE(RENCLK_GATE_D2, 0);
  2988. I915_WRITE(DSPCLK_GATE_D, 0);
  2989. I915_WRITE(RAMCLK_GATE_D, 0);
  2990. I915_WRITE16(DEUC, 0);
  2991. }
  2992. static void broadwater_init_clock_gating(struct drm_device *dev)
  2993. {
  2994. struct drm_i915_private *dev_priv = dev->dev_private;
  2995. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  2996. I965_RCC_CLOCK_GATE_DISABLE |
  2997. I965_RCPB_CLOCK_GATE_DISABLE |
  2998. I965_ISC_CLOCK_GATE_DISABLE |
  2999. I965_FBC_CLOCK_GATE_DISABLE);
  3000. I915_WRITE(RENCLK_GATE_D2, 0);
  3001. }
  3002. static void gen3_init_clock_gating(struct drm_device *dev)
  3003. {
  3004. struct drm_i915_private *dev_priv = dev->dev_private;
  3005. u32 dstate = I915_READ(D_STATE);
  3006. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  3007. DSTATE_DOT_CLOCK_GATING;
  3008. I915_WRITE(D_STATE, dstate);
  3009. if (IS_PINEVIEW(dev))
  3010. I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
  3011. }
  3012. static void i85x_init_clock_gating(struct drm_device *dev)
  3013. {
  3014. struct drm_i915_private *dev_priv = dev->dev_private;
  3015. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  3016. }
  3017. static void i830_init_clock_gating(struct drm_device *dev)
  3018. {
  3019. struct drm_i915_private *dev_priv = dev->dev_private;
  3020. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  3021. }
  3022. static void ibx_init_clock_gating(struct drm_device *dev)
  3023. {
  3024. struct drm_i915_private *dev_priv = dev->dev_private;
  3025. /*
  3026. * On Ibex Peak and Cougar Point, we need to disable clock
  3027. * gating for the panel power sequencer or it will fail to
  3028. * start up when no ports are active.
  3029. */
  3030. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  3031. }
  3032. static void cpt_init_clock_gating(struct drm_device *dev)
  3033. {
  3034. struct drm_i915_private *dev_priv = dev->dev_private;
  3035. int pipe;
  3036. /*
  3037. * On Ibex Peak and Cougar Point, we need to disable clock
  3038. * gating for the panel power sequencer or it will fail to
  3039. * start up when no ports are active.
  3040. */
  3041. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  3042. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  3043. DPLS_EDP_PPS_FIX_DIS);
  3044. /* Without this, mode sets may fail silently on FDI */
  3045. for_each_pipe(pipe)
  3046. I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_AUTOTRAIN_GEN_STALL_DIS);
  3047. }
  3048. void intel_init_clock_gating(struct drm_device *dev)
  3049. {
  3050. struct drm_i915_private *dev_priv = dev->dev_private;
  3051. dev_priv->display.init_clock_gating(dev);
  3052. if (dev_priv->display.init_pch_clock_gating)
  3053. dev_priv->display.init_pch_clock_gating(dev);
  3054. }
  3055. static void gen6_sanitize_pm(struct drm_device *dev)
  3056. {
  3057. struct drm_i915_private *dev_priv = dev->dev_private;
  3058. u32 limits, delay, old;
  3059. gen6_gt_force_wake_get(dev_priv);
  3060. old = limits = I915_READ(GEN6_RP_INTERRUPT_LIMITS);
  3061. /* Make sure we continue to get interrupts
  3062. * until we hit the minimum or maximum frequencies.
  3063. */
  3064. limits &= ~(0x3f << 16 | 0x3f << 24);
  3065. delay = dev_priv->cur_delay;
  3066. if (delay < dev_priv->max_delay)
  3067. limits |= (dev_priv->max_delay & 0x3f) << 24;
  3068. if (delay > dev_priv->min_delay)
  3069. limits |= (dev_priv->min_delay & 0x3f) << 16;
  3070. if (old != limits) {
  3071. /* Note that the known failure case is to read back 0. */
  3072. DRM_DEBUG_DRIVER("Power management discrepancy: GEN6_RP_INTERRUPT_LIMITS "
  3073. "expected %08x, was %08x\n", limits, old);
  3074. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, limits);
  3075. }
  3076. gen6_gt_force_wake_put(dev_priv);
  3077. }
  3078. void intel_sanitize_pm(struct drm_device *dev)
  3079. {
  3080. struct drm_i915_private *dev_priv = dev->dev_private;
  3081. if (dev_priv->display.sanitize_pm)
  3082. dev_priv->display.sanitize_pm(dev);
  3083. }
  3084. /* Starting with Haswell, we have different power wells for
  3085. * different parts of the GPU. This attempts to enable them all.
  3086. */
  3087. void intel_init_power_wells(struct drm_device *dev)
  3088. {
  3089. struct drm_i915_private *dev_priv = dev->dev_private;
  3090. unsigned long power_wells[] = {
  3091. HSW_PWR_WELL_CTL1,
  3092. HSW_PWR_WELL_CTL2,
  3093. HSW_PWR_WELL_CTL4
  3094. };
  3095. int i;
  3096. if (!IS_HASWELL(dev))
  3097. return;
  3098. mutex_lock(&dev->struct_mutex);
  3099. for (i = 0; i < ARRAY_SIZE(power_wells); i++) {
  3100. int well = I915_READ(power_wells[i]);
  3101. if ((well & HSW_PWR_WELL_STATE) == 0) {
  3102. I915_WRITE(power_wells[i], well & HSW_PWR_WELL_ENABLE);
  3103. if (wait_for(I915_READ(power_wells[i] & HSW_PWR_WELL_STATE), 20))
  3104. DRM_ERROR("Error enabling power well %lx\n", power_wells[i]);
  3105. }
  3106. }
  3107. mutex_unlock(&dev->struct_mutex);
  3108. }
  3109. /* Set up chip specific power management-related functions */
  3110. void intel_init_pm(struct drm_device *dev)
  3111. {
  3112. struct drm_i915_private *dev_priv = dev->dev_private;
  3113. if (I915_HAS_FBC(dev)) {
  3114. if (HAS_PCH_SPLIT(dev)) {
  3115. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  3116. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  3117. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  3118. } else if (IS_GM45(dev)) {
  3119. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  3120. dev_priv->display.enable_fbc = g4x_enable_fbc;
  3121. dev_priv->display.disable_fbc = g4x_disable_fbc;
  3122. } else if (IS_CRESTLINE(dev)) {
  3123. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  3124. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  3125. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  3126. }
  3127. /* 855GM needs testing */
  3128. }
  3129. /* For cxsr */
  3130. if (IS_PINEVIEW(dev))
  3131. i915_pineview_get_mem_freq(dev);
  3132. else if (IS_GEN5(dev))
  3133. i915_ironlake_get_mem_freq(dev);
  3134. /* For FIFO watermark updates */
  3135. if (HAS_PCH_SPLIT(dev)) {
  3136. dev_priv->display.force_wake_get = __gen6_gt_force_wake_get;
  3137. dev_priv->display.force_wake_put = __gen6_gt_force_wake_put;
  3138. /* IVB configs may use multi-threaded forcewake */
  3139. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  3140. u32 ecobus;
  3141. /* A small trick here - if the bios hasn't configured MT forcewake,
  3142. * and if the device is in RC6, then force_wake_mt_get will not wake
  3143. * the device and the ECOBUS read will return zero. Which will be
  3144. * (correctly) interpreted by the test below as MT forcewake being
  3145. * disabled.
  3146. */
  3147. mutex_lock(&dev->struct_mutex);
  3148. __gen6_gt_force_wake_mt_get(dev_priv);
  3149. ecobus = I915_READ_NOTRACE(ECOBUS);
  3150. __gen6_gt_force_wake_mt_put(dev_priv);
  3151. mutex_unlock(&dev->struct_mutex);
  3152. if (ecobus & FORCEWAKE_MT_ENABLE) {
  3153. DRM_DEBUG_KMS("Using MT version of forcewake\n");
  3154. dev_priv->display.force_wake_get =
  3155. __gen6_gt_force_wake_mt_get;
  3156. dev_priv->display.force_wake_put =
  3157. __gen6_gt_force_wake_mt_put;
  3158. }
  3159. }
  3160. if (HAS_PCH_IBX(dev))
  3161. dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
  3162. else if (HAS_PCH_CPT(dev))
  3163. dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
  3164. if (IS_GEN5(dev)) {
  3165. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  3166. dev_priv->display.update_wm = ironlake_update_wm;
  3167. else {
  3168. DRM_DEBUG_KMS("Failed to get proper latency. "
  3169. "Disable CxSR\n");
  3170. dev_priv->display.update_wm = NULL;
  3171. }
  3172. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  3173. } else if (IS_GEN6(dev)) {
  3174. if (SNB_READ_WM0_LATENCY()) {
  3175. dev_priv->display.update_wm = sandybridge_update_wm;
  3176. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  3177. } else {
  3178. DRM_DEBUG_KMS("Failed to read display plane latency. "
  3179. "Disable CxSR\n");
  3180. dev_priv->display.update_wm = NULL;
  3181. }
  3182. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  3183. dev_priv->display.sanitize_pm = gen6_sanitize_pm;
  3184. } else if (IS_IVYBRIDGE(dev)) {
  3185. /* FIXME: detect B0+ stepping and use auto training */
  3186. if (SNB_READ_WM0_LATENCY()) {
  3187. dev_priv->display.update_wm = sandybridge_update_wm;
  3188. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  3189. } else {
  3190. DRM_DEBUG_KMS("Failed to read display plane latency. "
  3191. "Disable CxSR\n");
  3192. dev_priv->display.update_wm = NULL;
  3193. }
  3194. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  3195. dev_priv->display.sanitize_pm = gen6_sanitize_pm;
  3196. } else if (IS_HASWELL(dev)) {
  3197. if (SNB_READ_WM0_LATENCY()) {
  3198. dev_priv->display.update_wm = sandybridge_update_wm;
  3199. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  3200. dev_priv->display.update_linetime_wm = haswell_update_linetime_wm;
  3201. } else {
  3202. DRM_DEBUG_KMS("Failed to read display plane latency. "
  3203. "Disable CxSR\n");
  3204. dev_priv->display.update_wm = NULL;
  3205. }
  3206. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  3207. dev_priv->display.sanitize_pm = gen6_sanitize_pm;
  3208. } else
  3209. dev_priv->display.update_wm = NULL;
  3210. } else if (IS_VALLEYVIEW(dev)) {
  3211. dev_priv->display.update_wm = valleyview_update_wm;
  3212. dev_priv->display.init_clock_gating =
  3213. valleyview_init_clock_gating;
  3214. dev_priv->display.force_wake_get = vlv_force_wake_get;
  3215. dev_priv->display.force_wake_put = vlv_force_wake_put;
  3216. } else if (IS_PINEVIEW(dev)) {
  3217. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  3218. dev_priv->is_ddr3,
  3219. dev_priv->fsb_freq,
  3220. dev_priv->mem_freq)) {
  3221. DRM_INFO("failed to find known CxSR latency "
  3222. "(found ddr%s fsb freq %d, mem freq %d), "
  3223. "disabling CxSR\n",
  3224. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  3225. dev_priv->fsb_freq, dev_priv->mem_freq);
  3226. /* Disable CxSR and never update its watermark again */
  3227. pineview_disable_cxsr(dev);
  3228. dev_priv->display.update_wm = NULL;
  3229. } else
  3230. dev_priv->display.update_wm = pineview_update_wm;
  3231. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  3232. } else if (IS_G4X(dev)) {
  3233. dev_priv->display.update_wm = g4x_update_wm;
  3234. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  3235. } else if (IS_GEN4(dev)) {
  3236. dev_priv->display.update_wm = i965_update_wm;
  3237. if (IS_CRESTLINE(dev))
  3238. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  3239. else if (IS_BROADWATER(dev))
  3240. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  3241. } else if (IS_GEN3(dev)) {
  3242. dev_priv->display.update_wm = i9xx_update_wm;
  3243. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  3244. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  3245. } else if (IS_I865G(dev)) {
  3246. dev_priv->display.update_wm = i830_update_wm;
  3247. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  3248. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  3249. } else if (IS_I85X(dev)) {
  3250. dev_priv->display.update_wm = i9xx_update_wm;
  3251. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  3252. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  3253. } else {
  3254. dev_priv->display.update_wm = i830_update_wm;
  3255. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  3256. if (IS_845G(dev))
  3257. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  3258. else
  3259. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  3260. }
  3261. /* We attempt to init the necessary power wells early in the initialization
  3262. * time, so the subsystems that expect power to be enabled can work.
  3263. */
  3264. intel_init_power_wells(dev);
  3265. }