intel_pm.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446
  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. #define FORCEWAKE_ACK_TIMEOUT_MS 2
  33. /* FBC, or Frame Buffer Compression, is a technique employed to compress the
  34. * framebuffer contents in-memory, aiming at reducing the required bandwidth
  35. * during in-memory transfers and, therefore, reduce the power packet.
  36. *
  37. * The benefits of FBC are mostly visible with solid backgrounds and
  38. * variation-less patterns.
  39. *
  40. * FBC-related functionality can be enabled by the means of the
  41. * i915.i915_enable_fbc parameter
  42. */
  43. static void i8xx_disable_fbc(struct drm_device *dev)
  44. {
  45. struct drm_i915_private *dev_priv = dev->dev_private;
  46. u32 fbc_ctl;
  47. /* Disable compression */
  48. fbc_ctl = I915_READ(FBC_CONTROL);
  49. if ((fbc_ctl & FBC_CTL_EN) == 0)
  50. return;
  51. fbc_ctl &= ~FBC_CTL_EN;
  52. I915_WRITE(FBC_CONTROL, fbc_ctl);
  53. /* Wait for compressing bit to clear */
  54. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  55. DRM_DEBUG_KMS("FBC idle timed out\n");
  56. return;
  57. }
  58. DRM_DEBUG_KMS("disabled FBC\n");
  59. }
  60. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  61. {
  62. struct drm_device *dev = crtc->dev;
  63. struct drm_i915_private *dev_priv = dev->dev_private;
  64. struct drm_framebuffer *fb = crtc->fb;
  65. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  66. struct drm_i915_gem_object *obj = intel_fb->obj;
  67. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  68. int cfb_pitch;
  69. int plane, i;
  70. u32 fbc_ctl, fbc_ctl2;
  71. cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  72. if (fb->pitches[0] < cfb_pitch)
  73. cfb_pitch = fb->pitches[0];
  74. /* FBC_CTL wants 64B units */
  75. cfb_pitch = (cfb_pitch / 64) - 1;
  76. plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  77. /* Clear old tags */
  78. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  79. I915_WRITE(FBC_TAG + (i * 4), 0);
  80. /* Set it up... */
  81. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  82. fbc_ctl2 |= plane;
  83. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  84. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  85. /* enable it... */
  86. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  87. if (IS_I945GM(dev))
  88. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  89. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  90. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  91. fbc_ctl |= obj->fence_reg;
  92. I915_WRITE(FBC_CONTROL, fbc_ctl);
  93. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
  94. cfb_pitch, crtc->y, intel_crtc->plane);
  95. }
  96. static bool i8xx_fbc_enabled(struct drm_device *dev)
  97. {
  98. struct drm_i915_private *dev_priv = dev->dev_private;
  99. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  100. }
  101. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  102. {
  103. struct drm_device *dev = crtc->dev;
  104. struct drm_i915_private *dev_priv = dev->dev_private;
  105. struct drm_framebuffer *fb = crtc->fb;
  106. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  107. struct drm_i915_gem_object *obj = intel_fb->obj;
  108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  109. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  110. unsigned long stall_watermark = 200;
  111. u32 dpfc_ctl;
  112. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  113. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  114. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  115. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  116. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  117. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  118. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  119. /* enable it... */
  120. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  121. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  122. }
  123. static void g4x_disable_fbc(struct drm_device *dev)
  124. {
  125. struct drm_i915_private *dev_priv = dev->dev_private;
  126. u32 dpfc_ctl;
  127. /* Disable compression */
  128. dpfc_ctl = I915_READ(DPFC_CONTROL);
  129. if (dpfc_ctl & DPFC_CTL_EN) {
  130. dpfc_ctl &= ~DPFC_CTL_EN;
  131. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  132. DRM_DEBUG_KMS("disabled FBC\n");
  133. }
  134. }
  135. static bool g4x_fbc_enabled(struct drm_device *dev)
  136. {
  137. struct drm_i915_private *dev_priv = dev->dev_private;
  138. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  139. }
  140. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  141. {
  142. struct drm_i915_private *dev_priv = dev->dev_private;
  143. u32 blt_ecoskpd;
  144. /* Make sure blitter notifies FBC of writes */
  145. gen6_gt_force_wake_get(dev_priv);
  146. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  147. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  148. GEN6_BLITTER_LOCK_SHIFT;
  149. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  150. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  151. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  152. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  153. GEN6_BLITTER_LOCK_SHIFT);
  154. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  155. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  156. gen6_gt_force_wake_put(dev_priv);
  157. }
  158. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  159. {
  160. struct drm_device *dev = crtc->dev;
  161. struct drm_i915_private *dev_priv = dev->dev_private;
  162. struct drm_framebuffer *fb = crtc->fb;
  163. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  164. struct drm_i915_gem_object *obj = intel_fb->obj;
  165. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  166. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  167. unsigned long stall_watermark = 200;
  168. u32 dpfc_ctl;
  169. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  170. dpfc_ctl &= DPFC_RESERVED;
  171. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  172. /* Set persistent mode for front-buffer rendering, ala X. */
  173. dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
  174. dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
  175. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  176. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  177. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  178. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  179. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  180. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  181. /* enable it... */
  182. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  183. if (IS_GEN6(dev)) {
  184. I915_WRITE(SNB_DPFC_CTL_SA,
  185. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  186. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  187. sandybridge_blit_fbc_update(dev);
  188. }
  189. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  190. }
  191. static void ironlake_disable_fbc(struct drm_device *dev)
  192. {
  193. struct drm_i915_private *dev_priv = dev->dev_private;
  194. u32 dpfc_ctl;
  195. /* Disable compression */
  196. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  197. if (dpfc_ctl & DPFC_CTL_EN) {
  198. dpfc_ctl &= ~DPFC_CTL_EN;
  199. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  200. DRM_DEBUG_KMS("disabled FBC\n");
  201. }
  202. }
  203. static bool ironlake_fbc_enabled(struct drm_device *dev)
  204. {
  205. struct drm_i915_private *dev_priv = dev->dev_private;
  206. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  207. }
  208. bool intel_fbc_enabled(struct drm_device *dev)
  209. {
  210. struct drm_i915_private *dev_priv = dev->dev_private;
  211. if (!dev_priv->display.fbc_enabled)
  212. return false;
  213. return dev_priv->display.fbc_enabled(dev);
  214. }
  215. static void intel_fbc_work_fn(struct work_struct *__work)
  216. {
  217. struct intel_fbc_work *work =
  218. container_of(to_delayed_work(__work),
  219. struct intel_fbc_work, work);
  220. struct drm_device *dev = work->crtc->dev;
  221. struct drm_i915_private *dev_priv = dev->dev_private;
  222. mutex_lock(&dev->struct_mutex);
  223. if (work == dev_priv->fbc_work) {
  224. /* Double check that we haven't switched fb without cancelling
  225. * the prior work.
  226. */
  227. if (work->crtc->fb == work->fb) {
  228. dev_priv->display.enable_fbc(work->crtc,
  229. work->interval);
  230. dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
  231. dev_priv->cfb_fb = work->crtc->fb->base.id;
  232. dev_priv->cfb_y = work->crtc->y;
  233. }
  234. dev_priv->fbc_work = NULL;
  235. }
  236. mutex_unlock(&dev->struct_mutex);
  237. kfree(work);
  238. }
  239. static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
  240. {
  241. if (dev_priv->fbc_work == NULL)
  242. return;
  243. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  244. /* Synchronisation is provided by struct_mutex and checking of
  245. * dev_priv->fbc_work, so we can perform the cancellation
  246. * entirely asynchronously.
  247. */
  248. if (cancel_delayed_work(&dev_priv->fbc_work->work))
  249. /* tasklet was killed before being run, clean up */
  250. kfree(dev_priv->fbc_work);
  251. /* Mark the work as no longer wanted so that if it does
  252. * wake-up (because the work was already running and waiting
  253. * for our mutex), it will discover that is no longer
  254. * necessary to run.
  255. */
  256. dev_priv->fbc_work = NULL;
  257. }
  258. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  259. {
  260. struct intel_fbc_work *work;
  261. struct drm_device *dev = crtc->dev;
  262. struct drm_i915_private *dev_priv = dev->dev_private;
  263. if (!dev_priv->display.enable_fbc)
  264. return;
  265. intel_cancel_fbc_work(dev_priv);
  266. work = kzalloc(sizeof *work, GFP_KERNEL);
  267. if (work == NULL) {
  268. dev_priv->display.enable_fbc(crtc, interval);
  269. return;
  270. }
  271. work->crtc = crtc;
  272. work->fb = crtc->fb;
  273. work->interval = interval;
  274. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  275. dev_priv->fbc_work = work;
  276. DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
  277. /* Delay the actual enabling to let pageflipping cease and the
  278. * display to settle before starting the compression. Note that
  279. * this delay also serves a second purpose: it allows for a
  280. * vblank to pass after disabling the FBC before we attempt
  281. * to modify the control registers.
  282. *
  283. * A more complicated solution would involve tracking vblanks
  284. * following the termination of the page-flipping sequence
  285. * and indeed performing the enable as a co-routine and not
  286. * waiting synchronously upon the vblank.
  287. */
  288. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  289. }
  290. void intel_disable_fbc(struct drm_device *dev)
  291. {
  292. struct drm_i915_private *dev_priv = dev->dev_private;
  293. intel_cancel_fbc_work(dev_priv);
  294. if (!dev_priv->display.disable_fbc)
  295. return;
  296. dev_priv->display.disable_fbc(dev);
  297. dev_priv->cfb_plane = -1;
  298. }
  299. /**
  300. * intel_update_fbc - enable/disable FBC as needed
  301. * @dev: the drm_device
  302. *
  303. * Set up the framebuffer compression hardware at mode set time. We
  304. * enable it if possible:
  305. * - plane A only (on pre-965)
  306. * - no pixel mulitply/line duplication
  307. * - no alpha buffer discard
  308. * - no dual wide
  309. * - framebuffer <= 2048 in width, 1536 in height
  310. *
  311. * We can't assume that any compression will take place (worst case),
  312. * so the compressed buffer has to be the same size as the uncompressed
  313. * one. It also must reside (along with the line length buffer) in
  314. * stolen memory.
  315. *
  316. * We need to enable/disable FBC on a global basis.
  317. */
  318. void intel_update_fbc(struct drm_device *dev)
  319. {
  320. struct drm_i915_private *dev_priv = dev->dev_private;
  321. struct drm_crtc *crtc = NULL, *tmp_crtc;
  322. struct intel_crtc *intel_crtc;
  323. struct drm_framebuffer *fb;
  324. struct intel_framebuffer *intel_fb;
  325. struct drm_i915_gem_object *obj;
  326. int enable_fbc;
  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->ips.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->ips.c_m = 0;
  544. } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
  545. dev_priv->ips.c_m = 1;
  546. } else {
  547. dev_priv->ips.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. int ignore_plane_sr, ignore_cursor_sr;
  1119. unsigned int enabled = 0;
  1120. vlv_update_drain_latency(dev);
  1121. if (g4x_compute_wm0(dev, 0,
  1122. &valleyview_wm_info, latency_ns,
  1123. &valleyview_cursor_wm_info, latency_ns,
  1124. &planea_wm, &cursora_wm))
  1125. enabled |= 1;
  1126. if (g4x_compute_wm0(dev, 1,
  1127. &valleyview_wm_info, latency_ns,
  1128. &valleyview_cursor_wm_info, latency_ns,
  1129. &planeb_wm, &cursorb_wm))
  1130. enabled |= 2;
  1131. plane_sr = cursor_sr = 0;
  1132. if (single_plane_enabled(enabled) &&
  1133. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1134. sr_latency_ns,
  1135. &valleyview_wm_info,
  1136. &valleyview_cursor_wm_info,
  1137. &plane_sr, &ignore_cursor_sr) &&
  1138. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1139. 2*sr_latency_ns,
  1140. &valleyview_wm_info,
  1141. &valleyview_cursor_wm_info,
  1142. &ignore_plane_sr, &cursor_sr))
  1143. I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
  1144. else
  1145. I915_WRITE(FW_BLC_SELF_VLV,
  1146. I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
  1147. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1148. planea_wm, cursora_wm,
  1149. planeb_wm, cursorb_wm,
  1150. plane_sr, cursor_sr);
  1151. I915_WRITE(DSPFW1,
  1152. (plane_sr << DSPFW_SR_SHIFT) |
  1153. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1154. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1155. planea_wm);
  1156. I915_WRITE(DSPFW2,
  1157. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  1158. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1159. I915_WRITE(DSPFW3,
  1160. (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
  1161. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1162. }
  1163. static void g4x_update_wm(struct drm_device *dev)
  1164. {
  1165. static const int sr_latency_ns = 12000;
  1166. struct drm_i915_private *dev_priv = dev->dev_private;
  1167. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1168. int plane_sr, cursor_sr;
  1169. unsigned int enabled = 0;
  1170. if (g4x_compute_wm0(dev, 0,
  1171. &g4x_wm_info, latency_ns,
  1172. &g4x_cursor_wm_info, latency_ns,
  1173. &planea_wm, &cursora_wm))
  1174. enabled |= 1;
  1175. if (g4x_compute_wm0(dev, 1,
  1176. &g4x_wm_info, latency_ns,
  1177. &g4x_cursor_wm_info, latency_ns,
  1178. &planeb_wm, &cursorb_wm))
  1179. enabled |= 2;
  1180. plane_sr = cursor_sr = 0;
  1181. if (single_plane_enabled(enabled) &&
  1182. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1183. sr_latency_ns,
  1184. &g4x_wm_info,
  1185. &g4x_cursor_wm_info,
  1186. &plane_sr, &cursor_sr))
  1187. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1188. else
  1189. I915_WRITE(FW_BLC_SELF,
  1190. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  1191. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1192. planea_wm, cursora_wm,
  1193. planeb_wm, cursorb_wm,
  1194. plane_sr, cursor_sr);
  1195. I915_WRITE(DSPFW1,
  1196. (plane_sr << DSPFW_SR_SHIFT) |
  1197. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1198. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1199. planea_wm);
  1200. I915_WRITE(DSPFW2,
  1201. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  1202. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1203. /* HPLL off in SR has some issues on G4x... disable it */
  1204. I915_WRITE(DSPFW3,
  1205. (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
  1206. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1207. }
  1208. static void i965_update_wm(struct drm_device *dev)
  1209. {
  1210. struct drm_i915_private *dev_priv = dev->dev_private;
  1211. struct drm_crtc *crtc;
  1212. int srwm = 1;
  1213. int cursor_sr = 16;
  1214. /* Calc sr entries for one plane configs */
  1215. crtc = single_enabled_crtc(dev);
  1216. if (crtc) {
  1217. /* self-refresh has much higher latency */
  1218. static const int sr_latency_ns = 12000;
  1219. int clock = crtc->mode.clock;
  1220. int htotal = crtc->mode.htotal;
  1221. int hdisplay = crtc->mode.hdisplay;
  1222. int pixel_size = crtc->fb->bits_per_pixel / 8;
  1223. unsigned long line_time_us;
  1224. int entries;
  1225. line_time_us = ((htotal * 1000) / clock);
  1226. /* Use ns/us then divide to preserve precision */
  1227. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1228. pixel_size * hdisplay;
  1229. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  1230. srwm = I965_FIFO_SIZE - entries;
  1231. if (srwm < 0)
  1232. srwm = 1;
  1233. srwm &= 0x1ff;
  1234. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  1235. entries, srwm);
  1236. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1237. pixel_size * 64;
  1238. entries = DIV_ROUND_UP(entries,
  1239. i965_cursor_wm_info.cacheline_size);
  1240. cursor_sr = i965_cursor_wm_info.fifo_size -
  1241. (entries + i965_cursor_wm_info.guard_size);
  1242. if (cursor_sr > i965_cursor_wm_info.max_wm)
  1243. cursor_sr = i965_cursor_wm_info.max_wm;
  1244. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  1245. "cursor %d\n", srwm, cursor_sr);
  1246. if (IS_CRESTLINE(dev))
  1247. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1248. } else {
  1249. /* Turn off self refresh if both pipes are enabled */
  1250. if (IS_CRESTLINE(dev))
  1251. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  1252. & ~FW_BLC_SELF_EN);
  1253. }
  1254. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  1255. srwm);
  1256. /* 965 has limitations... */
  1257. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  1258. (8 << 16) | (8 << 8) | (8 << 0));
  1259. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  1260. /* update cursor SR watermark */
  1261. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1262. }
  1263. static void i9xx_update_wm(struct drm_device *dev)
  1264. {
  1265. struct drm_i915_private *dev_priv = dev->dev_private;
  1266. const struct intel_watermark_params *wm_info;
  1267. uint32_t fwater_lo;
  1268. uint32_t fwater_hi;
  1269. int cwm, srwm = 1;
  1270. int fifo_size;
  1271. int planea_wm, planeb_wm;
  1272. struct drm_crtc *crtc, *enabled = NULL;
  1273. if (IS_I945GM(dev))
  1274. wm_info = &i945_wm_info;
  1275. else if (!IS_GEN2(dev))
  1276. wm_info = &i915_wm_info;
  1277. else
  1278. wm_info = &i855_wm_info;
  1279. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  1280. crtc = intel_get_crtc_for_plane(dev, 0);
  1281. if (crtc->enabled && crtc->fb) {
  1282. int cpp = crtc->fb->bits_per_pixel / 8;
  1283. if (IS_GEN2(dev))
  1284. cpp = 4;
  1285. planea_wm = intel_calculate_wm(crtc->mode.clock,
  1286. wm_info, fifo_size, cpp,
  1287. latency_ns);
  1288. enabled = crtc;
  1289. } else
  1290. planea_wm = fifo_size - wm_info->guard_size;
  1291. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  1292. crtc = intel_get_crtc_for_plane(dev, 1);
  1293. if (crtc->enabled && crtc->fb) {
  1294. int cpp = crtc->fb->bits_per_pixel / 8;
  1295. if (IS_GEN2(dev))
  1296. cpp = 4;
  1297. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  1298. wm_info, fifo_size, cpp,
  1299. latency_ns);
  1300. if (enabled == NULL)
  1301. enabled = crtc;
  1302. else
  1303. enabled = NULL;
  1304. } else
  1305. planeb_wm = fifo_size - wm_info->guard_size;
  1306. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  1307. /*
  1308. * Overlay gets an aggressive default since video jitter is bad.
  1309. */
  1310. cwm = 2;
  1311. /* Play safe and disable self-refresh before adjusting watermarks. */
  1312. if (IS_I945G(dev) || IS_I945GM(dev))
  1313. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  1314. else if (IS_I915GM(dev))
  1315. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  1316. /* Calc sr entries for one plane configs */
  1317. if (HAS_FW_BLC(dev) && enabled) {
  1318. /* self-refresh has much higher latency */
  1319. static const int sr_latency_ns = 6000;
  1320. int clock = enabled->mode.clock;
  1321. int htotal = enabled->mode.htotal;
  1322. int hdisplay = enabled->mode.hdisplay;
  1323. int pixel_size = enabled->fb->bits_per_pixel / 8;
  1324. unsigned long line_time_us;
  1325. int entries;
  1326. line_time_us = (htotal * 1000) / clock;
  1327. /* Use ns/us then divide to preserve precision */
  1328. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1329. pixel_size * hdisplay;
  1330. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  1331. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  1332. srwm = wm_info->fifo_size - entries;
  1333. if (srwm < 0)
  1334. srwm = 1;
  1335. if (IS_I945G(dev) || IS_I945GM(dev))
  1336. I915_WRITE(FW_BLC_SELF,
  1337. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  1338. else if (IS_I915GM(dev))
  1339. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  1340. }
  1341. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  1342. planea_wm, planeb_wm, cwm, srwm);
  1343. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  1344. fwater_hi = (cwm & 0x1f);
  1345. /* Set request length to 8 cachelines per fetch */
  1346. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  1347. fwater_hi = fwater_hi | (1 << 8);
  1348. I915_WRITE(FW_BLC, fwater_lo);
  1349. I915_WRITE(FW_BLC2, fwater_hi);
  1350. if (HAS_FW_BLC(dev)) {
  1351. if (enabled) {
  1352. if (IS_I945G(dev) || IS_I945GM(dev))
  1353. I915_WRITE(FW_BLC_SELF,
  1354. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  1355. else if (IS_I915GM(dev))
  1356. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  1357. DRM_DEBUG_KMS("memory self refresh enabled\n");
  1358. } else
  1359. DRM_DEBUG_KMS("memory self refresh disabled\n");
  1360. }
  1361. }
  1362. static void i830_update_wm(struct drm_device *dev)
  1363. {
  1364. struct drm_i915_private *dev_priv = dev->dev_private;
  1365. struct drm_crtc *crtc;
  1366. uint32_t fwater_lo;
  1367. int planea_wm;
  1368. crtc = single_enabled_crtc(dev);
  1369. if (crtc == NULL)
  1370. return;
  1371. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  1372. dev_priv->display.get_fifo_size(dev, 0),
  1373. 4, latency_ns);
  1374. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  1375. fwater_lo |= (3<<8) | planea_wm;
  1376. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  1377. I915_WRITE(FW_BLC, fwater_lo);
  1378. }
  1379. #define ILK_LP0_PLANE_LATENCY 700
  1380. #define ILK_LP0_CURSOR_LATENCY 1300
  1381. /*
  1382. * Check the wm result.
  1383. *
  1384. * If any calculated watermark values is larger than the maximum value that
  1385. * can be programmed into the associated watermark register, that watermark
  1386. * must be disabled.
  1387. */
  1388. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  1389. int fbc_wm, int display_wm, int cursor_wm,
  1390. const struct intel_watermark_params *display,
  1391. const struct intel_watermark_params *cursor)
  1392. {
  1393. struct drm_i915_private *dev_priv = dev->dev_private;
  1394. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  1395. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  1396. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  1397. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  1398. fbc_wm, SNB_FBC_MAX_SRWM, level);
  1399. /* fbc has it's own way to disable FBC WM */
  1400. I915_WRITE(DISP_ARB_CTL,
  1401. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  1402. return false;
  1403. }
  1404. if (display_wm > display->max_wm) {
  1405. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  1406. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  1407. return false;
  1408. }
  1409. if (cursor_wm > cursor->max_wm) {
  1410. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  1411. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  1412. return false;
  1413. }
  1414. if (!(fbc_wm || display_wm || cursor_wm)) {
  1415. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  1416. return false;
  1417. }
  1418. return true;
  1419. }
  1420. /*
  1421. * Compute watermark values of WM[1-3],
  1422. */
  1423. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  1424. int latency_ns,
  1425. const struct intel_watermark_params *display,
  1426. const struct intel_watermark_params *cursor,
  1427. int *fbc_wm, int *display_wm, int *cursor_wm)
  1428. {
  1429. struct drm_crtc *crtc;
  1430. unsigned long line_time_us;
  1431. int hdisplay, htotal, pixel_size, clock;
  1432. int line_count, line_size;
  1433. int small, large;
  1434. int entries;
  1435. if (!latency_ns) {
  1436. *fbc_wm = *display_wm = *cursor_wm = 0;
  1437. return false;
  1438. }
  1439. crtc = intel_get_crtc_for_plane(dev, plane);
  1440. hdisplay = crtc->mode.hdisplay;
  1441. htotal = crtc->mode.htotal;
  1442. clock = crtc->mode.clock;
  1443. pixel_size = crtc->fb->bits_per_pixel / 8;
  1444. line_time_us = (htotal * 1000) / clock;
  1445. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1446. line_size = hdisplay * pixel_size;
  1447. /* Use the minimum of the small and large buffer method for primary */
  1448. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1449. large = line_count * line_size;
  1450. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1451. *display_wm = entries + display->guard_size;
  1452. /*
  1453. * Spec says:
  1454. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  1455. */
  1456. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  1457. /* calculate the self-refresh watermark for display cursor */
  1458. entries = line_count * pixel_size * 64;
  1459. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1460. *cursor_wm = entries + cursor->guard_size;
  1461. return ironlake_check_srwm(dev, level,
  1462. *fbc_wm, *display_wm, *cursor_wm,
  1463. display, cursor);
  1464. }
  1465. static void ironlake_update_wm(struct drm_device *dev)
  1466. {
  1467. struct drm_i915_private *dev_priv = dev->dev_private;
  1468. int fbc_wm, plane_wm, cursor_wm;
  1469. unsigned int enabled;
  1470. enabled = 0;
  1471. if (g4x_compute_wm0(dev, 0,
  1472. &ironlake_display_wm_info,
  1473. ILK_LP0_PLANE_LATENCY,
  1474. &ironlake_cursor_wm_info,
  1475. ILK_LP0_CURSOR_LATENCY,
  1476. &plane_wm, &cursor_wm)) {
  1477. I915_WRITE(WM0_PIPEA_ILK,
  1478. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1479. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1480. " plane %d, " "cursor: %d\n",
  1481. plane_wm, cursor_wm);
  1482. enabled |= 1;
  1483. }
  1484. if (g4x_compute_wm0(dev, 1,
  1485. &ironlake_display_wm_info,
  1486. ILK_LP0_PLANE_LATENCY,
  1487. &ironlake_cursor_wm_info,
  1488. ILK_LP0_CURSOR_LATENCY,
  1489. &plane_wm, &cursor_wm)) {
  1490. I915_WRITE(WM0_PIPEB_ILK,
  1491. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1492. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1493. " plane %d, cursor: %d\n",
  1494. plane_wm, cursor_wm);
  1495. enabled |= 2;
  1496. }
  1497. /*
  1498. * Calculate and update the self-refresh watermark only when one
  1499. * display plane is used.
  1500. */
  1501. I915_WRITE(WM3_LP_ILK, 0);
  1502. I915_WRITE(WM2_LP_ILK, 0);
  1503. I915_WRITE(WM1_LP_ILK, 0);
  1504. if (!single_plane_enabled(enabled))
  1505. return;
  1506. enabled = ffs(enabled) - 1;
  1507. /* WM1 */
  1508. if (!ironlake_compute_srwm(dev, 1, enabled,
  1509. ILK_READ_WM1_LATENCY() * 500,
  1510. &ironlake_display_srwm_info,
  1511. &ironlake_cursor_srwm_info,
  1512. &fbc_wm, &plane_wm, &cursor_wm))
  1513. return;
  1514. I915_WRITE(WM1_LP_ILK,
  1515. WM1_LP_SR_EN |
  1516. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1517. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1518. (plane_wm << WM1_LP_SR_SHIFT) |
  1519. cursor_wm);
  1520. /* WM2 */
  1521. if (!ironlake_compute_srwm(dev, 2, enabled,
  1522. ILK_READ_WM2_LATENCY() * 500,
  1523. &ironlake_display_srwm_info,
  1524. &ironlake_cursor_srwm_info,
  1525. &fbc_wm, &plane_wm, &cursor_wm))
  1526. return;
  1527. I915_WRITE(WM2_LP_ILK,
  1528. WM2_LP_EN |
  1529. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1530. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1531. (plane_wm << WM1_LP_SR_SHIFT) |
  1532. cursor_wm);
  1533. /*
  1534. * WM3 is unsupported on ILK, probably because we don't have latency
  1535. * data for that power state
  1536. */
  1537. }
  1538. static void sandybridge_update_wm(struct drm_device *dev)
  1539. {
  1540. struct drm_i915_private *dev_priv = dev->dev_private;
  1541. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1542. u32 val;
  1543. int fbc_wm, plane_wm, cursor_wm;
  1544. unsigned int enabled;
  1545. enabled = 0;
  1546. if (g4x_compute_wm0(dev, 0,
  1547. &sandybridge_display_wm_info, latency,
  1548. &sandybridge_cursor_wm_info, latency,
  1549. &plane_wm, &cursor_wm)) {
  1550. val = I915_READ(WM0_PIPEA_ILK);
  1551. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1552. I915_WRITE(WM0_PIPEA_ILK, val |
  1553. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1554. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1555. " plane %d, " "cursor: %d\n",
  1556. plane_wm, cursor_wm);
  1557. enabled |= 1;
  1558. }
  1559. if (g4x_compute_wm0(dev, 1,
  1560. &sandybridge_display_wm_info, latency,
  1561. &sandybridge_cursor_wm_info, latency,
  1562. &plane_wm, &cursor_wm)) {
  1563. val = I915_READ(WM0_PIPEB_ILK);
  1564. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1565. I915_WRITE(WM0_PIPEB_ILK, val |
  1566. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1567. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1568. " plane %d, cursor: %d\n",
  1569. plane_wm, cursor_wm);
  1570. enabled |= 2;
  1571. }
  1572. /*
  1573. * Calculate and update the self-refresh watermark only when one
  1574. * display plane is used.
  1575. *
  1576. * SNB support 3 levels of watermark.
  1577. *
  1578. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  1579. * and disabled in the descending order
  1580. *
  1581. */
  1582. I915_WRITE(WM3_LP_ILK, 0);
  1583. I915_WRITE(WM2_LP_ILK, 0);
  1584. I915_WRITE(WM1_LP_ILK, 0);
  1585. if (!single_plane_enabled(enabled) ||
  1586. dev_priv->sprite_scaling_enabled)
  1587. return;
  1588. enabled = ffs(enabled) - 1;
  1589. /* WM1 */
  1590. if (!ironlake_compute_srwm(dev, 1, enabled,
  1591. SNB_READ_WM1_LATENCY() * 500,
  1592. &sandybridge_display_srwm_info,
  1593. &sandybridge_cursor_srwm_info,
  1594. &fbc_wm, &plane_wm, &cursor_wm))
  1595. return;
  1596. I915_WRITE(WM1_LP_ILK,
  1597. WM1_LP_SR_EN |
  1598. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1599. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1600. (plane_wm << WM1_LP_SR_SHIFT) |
  1601. cursor_wm);
  1602. /* WM2 */
  1603. if (!ironlake_compute_srwm(dev, 2, enabled,
  1604. SNB_READ_WM2_LATENCY() * 500,
  1605. &sandybridge_display_srwm_info,
  1606. &sandybridge_cursor_srwm_info,
  1607. &fbc_wm, &plane_wm, &cursor_wm))
  1608. return;
  1609. I915_WRITE(WM2_LP_ILK,
  1610. WM2_LP_EN |
  1611. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1612. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1613. (plane_wm << WM1_LP_SR_SHIFT) |
  1614. cursor_wm);
  1615. /* WM3 */
  1616. if (!ironlake_compute_srwm(dev, 3, enabled,
  1617. SNB_READ_WM3_LATENCY() * 500,
  1618. &sandybridge_display_srwm_info,
  1619. &sandybridge_cursor_srwm_info,
  1620. &fbc_wm, &plane_wm, &cursor_wm))
  1621. return;
  1622. I915_WRITE(WM3_LP_ILK,
  1623. WM3_LP_EN |
  1624. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1625. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1626. (plane_wm << WM1_LP_SR_SHIFT) |
  1627. cursor_wm);
  1628. }
  1629. static void ivybridge_update_wm(struct drm_device *dev)
  1630. {
  1631. struct drm_i915_private *dev_priv = dev->dev_private;
  1632. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1633. u32 val;
  1634. int fbc_wm, plane_wm, cursor_wm;
  1635. int ignore_fbc_wm, ignore_plane_wm, ignore_cursor_wm;
  1636. unsigned int enabled;
  1637. enabled = 0;
  1638. if (g4x_compute_wm0(dev, 0,
  1639. &sandybridge_display_wm_info, latency,
  1640. &sandybridge_cursor_wm_info, latency,
  1641. &plane_wm, &cursor_wm)) {
  1642. val = I915_READ(WM0_PIPEA_ILK);
  1643. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1644. I915_WRITE(WM0_PIPEA_ILK, val |
  1645. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1646. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1647. " plane %d, " "cursor: %d\n",
  1648. plane_wm, cursor_wm);
  1649. enabled |= 1;
  1650. }
  1651. if (g4x_compute_wm0(dev, 1,
  1652. &sandybridge_display_wm_info, latency,
  1653. &sandybridge_cursor_wm_info, latency,
  1654. &plane_wm, &cursor_wm)) {
  1655. val = I915_READ(WM0_PIPEB_ILK);
  1656. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1657. I915_WRITE(WM0_PIPEB_ILK, val |
  1658. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1659. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1660. " plane %d, cursor: %d\n",
  1661. plane_wm, cursor_wm);
  1662. enabled |= 2;
  1663. }
  1664. if (g4x_compute_wm0(dev, 2,
  1665. &sandybridge_display_wm_info, latency,
  1666. &sandybridge_cursor_wm_info, latency,
  1667. &plane_wm, &cursor_wm)) {
  1668. val = I915_READ(WM0_PIPEC_IVB);
  1669. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1670. I915_WRITE(WM0_PIPEC_IVB, val |
  1671. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1672. DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
  1673. " plane %d, cursor: %d\n",
  1674. plane_wm, cursor_wm);
  1675. enabled |= 3;
  1676. }
  1677. /*
  1678. * Calculate and update the self-refresh watermark only when one
  1679. * display plane is used.
  1680. *
  1681. * SNB support 3 levels of watermark.
  1682. *
  1683. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  1684. * and disabled in the descending order
  1685. *
  1686. */
  1687. I915_WRITE(WM3_LP_ILK, 0);
  1688. I915_WRITE(WM2_LP_ILK, 0);
  1689. I915_WRITE(WM1_LP_ILK, 0);
  1690. if (!single_plane_enabled(enabled) ||
  1691. dev_priv->sprite_scaling_enabled)
  1692. return;
  1693. enabled = ffs(enabled) - 1;
  1694. /* WM1 */
  1695. if (!ironlake_compute_srwm(dev, 1, enabled,
  1696. SNB_READ_WM1_LATENCY() * 500,
  1697. &sandybridge_display_srwm_info,
  1698. &sandybridge_cursor_srwm_info,
  1699. &fbc_wm, &plane_wm, &cursor_wm))
  1700. return;
  1701. I915_WRITE(WM1_LP_ILK,
  1702. WM1_LP_SR_EN |
  1703. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1704. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1705. (plane_wm << WM1_LP_SR_SHIFT) |
  1706. cursor_wm);
  1707. /* WM2 */
  1708. if (!ironlake_compute_srwm(dev, 2, enabled,
  1709. SNB_READ_WM2_LATENCY() * 500,
  1710. &sandybridge_display_srwm_info,
  1711. &sandybridge_cursor_srwm_info,
  1712. &fbc_wm, &plane_wm, &cursor_wm))
  1713. return;
  1714. I915_WRITE(WM2_LP_ILK,
  1715. WM2_LP_EN |
  1716. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1717. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1718. (plane_wm << WM1_LP_SR_SHIFT) |
  1719. cursor_wm);
  1720. /* WM3, note we have to correct the cursor latency */
  1721. if (!ironlake_compute_srwm(dev, 3, enabled,
  1722. SNB_READ_WM3_LATENCY() * 500,
  1723. &sandybridge_display_srwm_info,
  1724. &sandybridge_cursor_srwm_info,
  1725. &fbc_wm, &plane_wm, &ignore_cursor_wm) ||
  1726. !ironlake_compute_srwm(dev, 3, enabled,
  1727. 2 * SNB_READ_WM3_LATENCY() * 500,
  1728. &sandybridge_display_srwm_info,
  1729. &sandybridge_cursor_srwm_info,
  1730. &ignore_fbc_wm, &ignore_plane_wm, &cursor_wm))
  1731. return;
  1732. I915_WRITE(WM3_LP_ILK,
  1733. WM3_LP_EN |
  1734. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1735. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1736. (plane_wm << WM1_LP_SR_SHIFT) |
  1737. cursor_wm);
  1738. }
  1739. static void
  1740. haswell_update_linetime_wm(struct drm_device *dev, int pipe,
  1741. struct drm_display_mode *mode)
  1742. {
  1743. struct drm_i915_private *dev_priv = dev->dev_private;
  1744. u32 temp;
  1745. temp = I915_READ(PIPE_WM_LINETIME(pipe));
  1746. temp &= ~PIPE_WM_LINETIME_MASK;
  1747. /* The WM are computed with base on how long it takes to fill a single
  1748. * row at the given clock rate, multiplied by 8.
  1749. * */
  1750. temp |= PIPE_WM_LINETIME_TIME(
  1751. ((mode->crtc_hdisplay * 1000) / mode->clock) * 8);
  1752. /* IPS watermarks are only used by pipe A, and are ignored by
  1753. * pipes B and C. They are calculated similarly to the common
  1754. * linetime values, except that we are using CD clock frequency
  1755. * in MHz instead of pixel rate for the division.
  1756. *
  1757. * This is a placeholder for the IPS watermark calculation code.
  1758. */
  1759. I915_WRITE(PIPE_WM_LINETIME(pipe), temp);
  1760. }
  1761. static bool
  1762. sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
  1763. uint32_t sprite_width, int pixel_size,
  1764. const struct intel_watermark_params *display,
  1765. int display_latency_ns, int *sprite_wm)
  1766. {
  1767. struct drm_crtc *crtc;
  1768. int clock;
  1769. int entries, tlb_miss;
  1770. crtc = intel_get_crtc_for_plane(dev, plane);
  1771. if (crtc->fb == NULL || !crtc->enabled) {
  1772. *sprite_wm = display->guard_size;
  1773. return false;
  1774. }
  1775. clock = crtc->mode.clock;
  1776. /* Use the small buffer method to calculate the sprite watermark */
  1777. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  1778. tlb_miss = display->fifo_size*display->cacheline_size -
  1779. sprite_width * 8;
  1780. if (tlb_miss > 0)
  1781. entries += tlb_miss;
  1782. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  1783. *sprite_wm = entries + display->guard_size;
  1784. if (*sprite_wm > (int)display->max_wm)
  1785. *sprite_wm = display->max_wm;
  1786. return true;
  1787. }
  1788. static bool
  1789. sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
  1790. uint32_t sprite_width, int pixel_size,
  1791. const struct intel_watermark_params *display,
  1792. int latency_ns, int *sprite_wm)
  1793. {
  1794. struct drm_crtc *crtc;
  1795. unsigned long line_time_us;
  1796. int clock;
  1797. int line_count, line_size;
  1798. int small, large;
  1799. int entries;
  1800. if (!latency_ns) {
  1801. *sprite_wm = 0;
  1802. return false;
  1803. }
  1804. crtc = intel_get_crtc_for_plane(dev, plane);
  1805. clock = crtc->mode.clock;
  1806. if (!clock) {
  1807. *sprite_wm = 0;
  1808. return false;
  1809. }
  1810. line_time_us = (sprite_width * 1000) / clock;
  1811. if (!line_time_us) {
  1812. *sprite_wm = 0;
  1813. return false;
  1814. }
  1815. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1816. line_size = sprite_width * pixel_size;
  1817. /* Use the minimum of the small and large buffer method for primary */
  1818. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1819. large = line_count * line_size;
  1820. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1821. *sprite_wm = entries + display->guard_size;
  1822. return *sprite_wm > 0x3ff ? false : true;
  1823. }
  1824. static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
  1825. uint32_t sprite_width, int pixel_size)
  1826. {
  1827. struct drm_i915_private *dev_priv = dev->dev_private;
  1828. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1829. u32 val;
  1830. int sprite_wm, reg;
  1831. int ret;
  1832. switch (pipe) {
  1833. case 0:
  1834. reg = WM0_PIPEA_ILK;
  1835. break;
  1836. case 1:
  1837. reg = WM0_PIPEB_ILK;
  1838. break;
  1839. case 2:
  1840. reg = WM0_PIPEC_IVB;
  1841. break;
  1842. default:
  1843. return; /* bad pipe */
  1844. }
  1845. ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
  1846. &sandybridge_display_wm_info,
  1847. latency, &sprite_wm);
  1848. if (!ret) {
  1849. DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
  1850. pipe);
  1851. return;
  1852. }
  1853. val = I915_READ(reg);
  1854. val &= ~WM0_PIPE_SPRITE_MASK;
  1855. I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
  1856. DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
  1857. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1858. pixel_size,
  1859. &sandybridge_display_srwm_info,
  1860. SNB_READ_WM1_LATENCY() * 500,
  1861. &sprite_wm);
  1862. if (!ret) {
  1863. DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
  1864. pipe);
  1865. return;
  1866. }
  1867. I915_WRITE(WM1S_LP_ILK, sprite_wm);
  1868. /* Only IVB has two more LP watermarks for sprite */
  1869. if (!IS_IVYBRIDGE(dev))
  1870. return;
  1871. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1872. pixel_size,
  1873. &sandybridge_display_srwm_info,
  1874. SNB_READ_WM2_LATENCY() * 500,
  1875. &sprite_wm);
  1876. if (!ret) {
  1877. DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
  1878. pipe);
  1879. return;
  1880. }
  1881. I915_WRITE(WM2S_LP_IVB, sprite_wm);
  1882. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  1883. pixel_size,
  1884. &sandybridge_display_srwm_info,
  1885. SNB_READ_WM3_LATENCY() * 500,
  1886. &sprite_wm);
  1887. if (!ret) {
  1888. DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
  1889. pipe);
  1890. return;
  1891. }
  1892. I915_WRITE(WM3S_LP_IVB, sprite_wm);
  1893. }
  1894. /**
  1895. * intel_update_watermarks - update FIFO watermark values based on current modes
  1896. *
  1897. * Calculate watermark values for the various WM regs based on current mode
  1898. * and plane configuration.
  1899. *
  1900. * There are several cases to deal with here:
  1901. * - normal (i.e. non-self-refresh)
  1902. * - self-refresh (SR) mode
  1903. * - lines are large relative to FIFO size (buffer can hold up to 2)
  1904. * - lines are small relative to FIFO size (buffer can hold more than 2
  1905. * lines), so need to account for TLB latency
  1906. *
  1907. * The normal calculation is:
  1908. * watermark = dotclock * bytes per pixel * latency
  1909. * where latency is platform & configuration dependent (we assume pessimal
  1910. * values here).
  1911. *
  1912. * The SR calculation is:
  1913. * watermark = (trunc(latency/line time)+1) * surface width *
  1914. * bytes per pixel
  1915. * where
  1916. * line time = htotal / dotclock
  1917. * surface width = hdisplay for normal plane and 64 for cursor
  1918. * and latency is assumed to be high, as above.
  1919. *
  1920. * The final value programmed to the register should always be rounded up,
  1921. * and include an extra 2 entries to account for clock crossings.
  1922. *
  1923. * We don't use the sprite, so we can ignore that. And on Crestline we have
  1924. * to set the non-SR watermarks to 8.
  1925. */
  1926. void intel_update_watermarks(struct drm_device *dev)
  1927. {
  1928. struct drm_i915_private *dev_priv = dev->dev_private;
  1929. if (dev_priv->display.update_wm)
  1930. dev_priv->display.update_wm(dev);
  1931. }
  1932. void intel_update_linetime_watermarks(struct drm_device *dev,
  1933. int pipe, struct drm_display_mode *mode)
  1934. {
  1935. struct drm_i915_private *dev_priv = dev->dev_private;
  1936. if (dev_priv->display.update_linetime_wm)
  1937. dev_priv->display.update_linetime_wm(dev, pipe, mode);
  1938. }
  1939. void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
  1940. uint32_t sprite_width, int pixel_size)
  1941. {
  1942. struct drm_i915_private *dev_priv = dev->dev_private;
  1943. if (dev_priv->display.update_sprite_wm)
  1944. dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
  1945. pixel_size);
  1946. }
  1947. static struct drm_i915_gem_object *
  1948. intel_alloc_context_page(struct drm_device *dev)
  1949. {
  1950. struct drm_i915_gem_object *ctx;
  1951. int ret;
  1952. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1953. ctx = i915_gem_alloc_object(dev, 4096);
  1954. if (!ctx) {
  1955. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  1956. return NULL;
  1957. }
  1958. ret = i915_gem_object_pin(ctx, 4096, true, false);
  1959. if (ret) {
  1960. DRM_ERROR("failed to pin power context: %d\n", ret);
  1961. goto err_unref;
  1962. }
  1963. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  1964. if (ret) {
  1965. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  1966. goto err_unpin;
  1967. }
  1968. return ctx;
  1969. err_unpin:
  1970. i915_gem_object_unpin(ctx);
  1971. err_unref:
  1972. drm_gem_object_unreference(&ctx->base);
  1973. mutex_unlock(&dev->struct_mutex);
  1974. return NULL;
  1975. }
  1976. /**
  1977. * Lock protecting IPS related data structures
  1978. */
  1979. DEFINE_SPINLOCK(mchdev_lock);
  1980. /* Global for IPS driver to get at the current i915 device. Protected by
  1981. * mchdev_lock. */
  1982. static struct drm_i915_private *i915_mch_dev;
  1983. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  1984. {
  1985. struct drm_i915_private *dev_priv = dev->dev_private;
  1986. u16 rgvswctl;
  1987. assert_spin_locked(&mchdev_lock);
  1988. rgvswctl = I915_READ16(MEMSWCTL);
  1989. if (rgvswctl & MEMCTL_CMD_STS) {
  1990. DRM_DEBUG("gpu busy, RCS change rejected\n");
  1991. return false; /* still busy with another command */
  1992. }
  1993. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  1994. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  1995. I915_WRITE16(MEMSWCTL, rgvswctl);
  1996. POSTING_READ16(MEMSWCTL);
  1997. rgvswctl |= MEMCTL_CMD_STS;
  1998. I915_WRITE16(MEMSWCTL, rgvswctl);
  1999. return true;
  2000. }
  2001. static void ironlake_enable_drps(struct drm_device *dev)
  2002. {
  2003. struct drm_i915_private *dev_priv = dev->dev_private;
  2004. u32 rgvmodectl = I915_READ(MEMMODECTL);
  2005. u8 fmax, fmin, fstart, vstart;
  2006. spin_lock_irq(&mchdev_lock);
  2007. /* Enable temp reporting */
  2008. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  2009. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  2010. /* 100ms RC evaluation intervals */
  2011. I915_WRITE(RCUPEI, 100000);
  2012. I915_WRITE(RCDNEI, 100000);
  2013. /* Set max/min thresholds to 90ms and 80ms respectively */
  2014. I915_WRITE(RCBMAXAVG, 90000);
  2015. I915_WRITE(RCBMINAVG, 80000);
  2016. I915_WRITE(MEMIHYST, 1);
  2017. /* Set up min, max, and cur for interrupt handling */
  2018. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  2019. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  2020. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  2021. MEMMODE_FSTART_SHIFT;
  2022. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  2023. PXVFREQ_PX_SHIFT;
  2024. dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
  2025. dev_priv->ips.fstart = fstart;
  2026. dev_priv->ips.max_delay = fstart;
  2027. dev_priv->ips.min_delay = fmin;
  2028. dev_priv->ips.cur_delay = fstart;
  2029. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  2030. fmax, fmin, fstart);
  2031. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  2032. /*
  2033. * Interrupts will be enabled in ironlake_irq_postinstall
  2034. */
  2035. I915_WRITE(VIDSTART, vstart);
  2036. POSTING_READ(VIDSTART);
  2037. rgvmodectl |= MEMMODE_SWMODE_EN;
  2038. I915_WRITE(MEMMODECTL, rgvmodectl);
  2039. if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  2040. DRM_ERROR("stuck trying to change perf mode\n");
  2041. mdelay(1);
  2042. ironlake_set_drps(dev, fstart);
  2043. dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  2044. I915_READ(0x112e0);
  2045. dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
  2046. dev_priv->ips.last_count2 = I915_READ(0x112f4);
  2047. getrawmonotonic(&dev_priv->ips.last_time2);
  2048. spin_unlock_irq(&mchdev_lock);
  2049. }
  2050. static void ironlake_disable_drps(struct drm_device *dev)
  2051. {
  2052. struct drm_i915_private *dev_priv = dev->dev_private;
  2053. u16 rgvswctl;
  2054. spin_lock_irq(&mchdev_lock);
  2055. rgvswctl = I915_READ16(MEMSWCTL);
  2056. /* Ack interrupts, disable EFC interrupt */
  2057. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  2058. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  2059. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  2060. I915_WRITE(DEIIR, DE_PCU_EVENT);
  2061. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  2062. /* Go back to the starting frequency */
  2063. ironlake_set_drps(dev, dev_priv->ips.fstart);
  2064. mdelay(1);
  2065. rgvswctl |= MEMCTL_CMD_STS;
  2066. I915_WRITE(MEMSWCTL, rgvswctl);
  2067. mdelay(1);
  2068. spin_unlock_irq(&mchdev_lock);
  2069. }
  2070. /* There's a funny hw issue where the hw returns all 0 when reading from
  2071. * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
  2072. * ourselves, instead of doing a rmw cycle (which might result in us clearing
  2073. * all limits and the gpu stuck at whatever frequency it is at atm).
  2074. */
  2075. static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 *val)
  2076. {
  2077. u32 limits;
  2078. limits = 0;
  2079. if (*val >= dev_priv->rps.max_delay)
  2080. *val = dev_priv->rps.max_delay;
  2081. limits |= dev_priv->rps.max_delay << 24;
  2082. /* Only set the down limit when we've reached the lowest level to avoid
  2083. * getting more interrupts, otherwise leave this clear. This prevents a
  2084. * race in the hw when coming out of rc6: There's a tiny window where
  2085. * the hw runs at the minimal clock before selecting the desired
  2086. * frequency, if the down threshold expires in that window we will not
  2087. * receive a down interrupt. */
  2088. if (*val <= dev_priv->rps.min_delay) {
  2089. *val = dev_priv->rps.min_delay;
  2090. limits |= dev_priv->rps.min_delay << 16;
  2091. }
  2092. return limits;
  2093. }
  2094. void gen6_set_rps(struct drm_device *dev, u8 val)
  2095. {
  2096. struct drm_i915_private *dev_priv = dev->dev_private;
  2097. u32 limits = gen6_rps_limits(dev_priv, &val);
  2098. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2099. WARN_ON(val > dev_priv->rps.max_delay);
  2100. WARN_ON(val < dev_priv->rps.min_delay);
  2101. if (val == dev_priv->rps.cur_delay)
  2102. return;
  2103. I915_WRITE(GEN6_RPNSWREQ,
  2104. GEN6_FREQUENCY(val) |
  2105. GEN6_OFFSET(0) |
  2106. GEN6_AGGRESSIVE_TURBO);
  2107. /* Make sure we continue to get interrupts
  2108. * until we hit the minimum or maximum frequencies.
  2109. */
  2110. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, limits);
  2111. POSTING_READ(GEN6_RPNSWREQ);
  2112. dev_priv->rps.cur_delay = val;
  2113. trace_intel_gpu_freq_change(val * 50);
  2114. }
  2115. static void gen6_disable_rps(struct drm_device *dev)
  2116. {
  2117. struct drm_i915_private *dev_priv = dev->dev_private;
  2118. I915_WRITE(GEN6_RC_CONTROL, 0);
  2119. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  2120. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  2121. I915_WRITE(GEN6_PMIER, 0);
  2122. /* Complete PM interrupt masking here doesn't race with the rps work
  2123. * item again unmasking PM interrupts because that is using a different
  2124. * register (PMIMR) to mask PM interrupts. The only risk is in leaving
  2125. * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
  2126. spin_lock_irq(&dev_priv->rps.lock);
  2127. dev_priv->rps.pm_iir = 0;
  2128. spin_unlock_irq(&dev_priv->rps.lock);
  2129. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  2130. }
  2131. int intel_enable_rc6(const struct drm_device *dev)
  2132. {
  2133. /* Respect the kernel parameter if it is set */
  2134. if (i915_enable_rc6 >= 0)
  2135. return i915_enable_rc6;
  2136. if (INTEL_INFO(dev)->gen == 5) {
  2137. #ifdef CONFIG_INTEL_IOMMU
  2138. /* Disable rc6 on ilk if VT-d is on. */
  2139. if (intel_iommu_gfx_mapped)
  2140. return false;
  2141. #endif
  2142. DRM_DEBUG_DRIVER("Ironlake: only RC6 available\n");
  2143. return INTEL_RC6_ENABLE;
  2144. }
  2145. if (IS_HASWELL(dev)) {
  2146. DRM_DEBUG_DRIVER("Haswell: only RC6 available\n");
  2147. return INTEL_RC6_ENABLE;
  2148. }
  2149. /* snb/ivb have more than one rc6 state. */
  2150. if (INTEL_INFO(dev)->gen == 6) {
  2151. DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
  2152. return INTEL_RC6_ENABLE;
  2153. }
  2154. DRM_DEBUG_DRIVER("RC6 and deep RC6 enabled\n");
  2155. return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
  2156. }
  2157. static void gen6_enable_rps(struct drm_device *dev)
  2158. {
  2159. struct drm_i915_private *dev_priv = dev->dev_private;
  2160. struct intel_ring_buffer *ring;
  2161. u32 rp_state_cap;
  2162. u32 gt_perf_status;
  2163. u32 rc6vids, pcu_mbox, rc6_mask = 0;
  2164. u32 gtfifodbg;
  2165. int rc6_mode;
  2166. int i, ret;
  2167. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2168. /* Here begins a magic sequence of register writes to enable
  2169. * auto-downclocking.
  2170. *
  2171. * Perhaps there might be some value in exposing these to
  2172. * userspace...
  2173. */
  2174. I915_WRITE(GEN6_RC_STATE, 0);
  2175. /* Clear the DBG now so we don't confuse earlier errors */
  2176. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  2177. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  2178. I915_WRITE(GTFIFODBG, gtfifodbg);
  2179. }
  2180. gen6_gt_force_wake_get(dev_priv);
  2181. rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  2182. gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  2183. /* In units of 100MHz */
  2184. dev_priv->rps.max_delay = rp_state_cap & 0xff;
  2185. dev_priv->rps.min_delay = (rp_state_cap & 0xff0000) >> 16;
  2186. dev_priv->rps.cur_delay = 0;
  2187. /* disable the counters and set deterministic thresholds */
  2188. I915_WRITE(GEN6_RC_CONTROL, 0);
  2189. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  2190. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  2191. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  2192. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  2193. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  2194. for_each_ring(ring, dev_priv, i)
  2195. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  2196. I915_WRITE(GEN6_RC_SLEEP, 0);
  2197. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  2198. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  2199. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  2200. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  2201. /* Check if we are enabling RC6 */
  2202. rc6_mode = intel_enable_rc6(dev_priv->dev);
  2203. if (rc6_mode & INTEL_RC6_ENABLE)
  2204. rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
  2205. /* We don't use those on Haswell */
  2206. if (!IS_HASWELL(dev)) {
  2207. if (rc6_mode & INTEL_RC6p_ENABLE)
  2208. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  2209. if (rc6_mode & INTEL_RC6pp_ENABLE)
  2210. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  2211. }
  2212. DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
  2213. (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
  2214. (rc6_mask & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
  2215. (rc6_mask & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
  2216. I915_WRITE(GEN6_RC_CONTROL,
  2217. rc6_mask |
  2218. GEN6_RC_CTL_EI_MODE(1) |
  2219. GEN6_RC_CTL_HW_ENABLE);
  2220. I915_WRITE(GEN6_RPNSWREQ,
  2221. GEN6_FREQUENCY(10) |
  2222. GEN6_OFFSET(0) |
  2223. GEN6_AGGRESSIVE_TURBO);
  2224. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  2225. GEN6_FREQUENCY(12));
  2226. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  2227. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  2228. dev_priv->rps.max_delay << 24 |
  2229. dev_priv->rps.min_delay << 16);
  2230. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  2231. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  2232. I915_WRITE(GEN6_RP_UP_EI, 66000);
  2233. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  2234. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  2235. I915_WRITE(GEN6_RP_CONTROL,
  2236. GEN6_RP_MEDIA_TURBO |
  2237. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  2238. GEN6_RP_MEDIA_IS_GFX |
  2239. GEN6_RP_ENABLE |
  2240. GEN6_RP_UP_BUSY_AVG |
  2241. (IS_HASWELL(dev) ? GEN7_RP_DOWN_IDLE_AVG : GEN6_RP_DOWN_IDLE_CONT));
  2242. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
  2243. if (!ret) {
  2244. pcu_mbox = 0;
  2245. ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
  2246. if (ret && pcu_mbox & (1<<31)) { /* OC supported */
  2247. dev_priv->rps.max_delay = pcu_mbox & 0xff;
  2248. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
  2249. }
  2250. } else {
  2251. DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
  2252. }
  2253. gen6_set_rps(dev_priv->dev, (gt_perf_status & 0xff00) >> 8);
  2254. /* requires MSI enabled */
  2255. I915_WRITE(GEN6_PMIER, GEN6_PM_DEFERRED_EVENTS);
  2256. spin_lock_irq(&dev_priv->rps.lock);
  2257. WARN_ON(dev_priv->rps.pm_iir != 0);
  2258. I915_WRITE(GEN6_PMIMR, 0);
  2259. spin_unlock_irq(&dev_priv->rps.lock);
  2260. /* enable all PM interrupts */
  2261. I915_WRITE(GEN6_PMINTRMSK, 0);
  2262. rc6vids = 0;
  2263. ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
  2264. if (IS_GEN6(dev) && ret) {
  2265. DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
  2266. } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
  2267. DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
  2268. GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
  2269. rc6vids &= 0xffff00;
  2270. rc6vids |= GEN6_ENCODE_RC6_VID(450);
  2271. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
  2272. if (ret)
  2273. DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
  2274. }
  2275. gen6_gt_force_wake_put(dev_priv);
  2276. }
  2277. static void gen6_update_ring_freq(struct drm_device *dev)
  2278. {
  2279. struct drm_i915_private *dev_priv = dev->dev_private;
  2280. int min_freq = 15;
  2281. int gpu_freq;
  2282. unsigned int ia_freq, max_ia_freq;
  2283. int scaling_factor = 180;
  2284. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2285. max_ia_freq = cpufreq_quick_get_max(0);
  2286. /*
  2287. * Default to measured freq if none found, PCU will ensure we don't go
  2288. * over
  2289. */
  2290. if (!max_ia_freq)
  2291. max_ia_freq = tsc_khz;
  2292. /* Convert from kHz to MHz */
  2293. max_ia_freq /= 1000;
  2294. /*
  2295. * For each potential GPU frequency, load a ring frequency we'd like
  2296. * to use for memory access. We do this by specifying the IA frequency
  2297. * the PCU should use as a reference to determine the ring frequency.
  2298. */
  2299. for (gpu_freq = dev_priv->rps.max_delay; gpu_freq >= dev_priv->rps.min_delay;
  2300. gpu_freq--) {
  2301. int diff = dev_priv->rps.max_delay - gpu_freq;
  2302. /*
  2303. * For GPU frequencies less than 750MHz, just use the lowest
  2304. * ring freq.
  2305. */
  2306. if (gpu_freq < min_freq)
  2307. ia_freq = 800;
  2308. else
  2309. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  2310. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  2311. ia_freq <<= GEN6_PCODE_FREQ_IA_RATIO_SHIFT;
  2312. sandybridge_pcode_write(dev_priv,
  2313. GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
  2314. ia_freq | gpu_freq);
  2315. }
  2316. }
  2317. void ironlake_teardown_rc6(struct drm_device *dev)
  2318. {
  2319. struct drm_i915_private *dev_priv = dev->dev_private;
  2320. if (dev_priv->ips.renderctx) {
  2321. i915_gem_object_unpin(dev_priv->ips.renderctx);
  2322. drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
  2323. dev_priv->ips.renderctx = NULL;
  2324. }
  2325. if (dev_priv->ips.pwrctx) {
  2326. i915_gem_object_unpin(dev_priv->ips.pwrctx);
  2327. drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
  2328. dev_priv->ips.pwrctx = NULL;
  2329. }
  2330. }
  2331. static void ironlake_disable_rc6(struct drm_device *dev)
  2332. {
  2333. struct drm_i915_private *dev_priv = dev->dev_private;
  2334. if (I915_READ(PWRCTXA)) {
  2335. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  2336. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  2337. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  2338. 50);
  2339. I915_WRITE(PWRCTXA, 0);
  2340. POSTING_READ(PWRCTXA);
  2341. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  2342. POSTING_READ(RSTDBYCTL);
  2343. }
  2344. }
  2345. static int ironlake_setup_rc6(struct drm_device *dev)
  2346. {
  2347. struct drm_i915_private *dev_priv = dev->dev_private;
  2348. if (dev_priv->ips.renderctx == NULL)
  2349. dev_priv->ips.renderctx = intel_alloc_context_page(dev);
  2350. if (!dev_priv->ips.renderctx)
  2351. return -ENOMEM;
  2352. if (dev_priv->ips.pwrctx == NULL)
  2353. dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
  2354. if (!dev_priv->ips.pwrctx) {
  2355. ironlake_teardown_rc6(dev);
  2356. return -ENOMEM;
  2357. }
  2358. return 0;
  2359. }
  2360. static void ironlake_enable_rc6(struct drm_device *dev)
  2361. {
  2362. struct drm_i915_private *dev_priv = dev->dev_private;
  2363. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  2364. bool was_interruptible;
  2365. int ret;
  2366. /* rc6 disabled by default due to repeated reports of hanging during
  2367. * boot and resume.
  2368. */
  2369. if (!intel_enable_rc6(dev))
  2370. return;
  2371. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2372. ret = ironlake_setup_rc6(dev);
  2373. if (ret)
  2374. return;
  2375. was_interruptible = dev_priv->mm.interruptible;
  2376. dev_priv->mm.interruptible = false;
  2377. /*
  2378. * GPU can automatically power down the render unit if given a page
  2379. * to save state.
  2380. */
  2381. ret = intel_ring_begin(ring, 6);
  2382. if (ret) {
  2383. ironlake_teardown_rc6(dev);
  2384. dev_priv->mm.interruptible = was_interruptible;
  2385. return;
  2386. }
  2387. intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  2388. intel_ring_emit(ring, MI_SET_CONTEXT);
  2389. intel_ring_emit(ring, dev_priv->ips.renderctx->gtt_offset |
  2390. MI_MM_SPACE_GTT |
  2391. MI_SAVE_EXT_STATE_EN |
  2392. MI_RESTORE_EXT_STATE_EN |
  2393. MI_RESTORE_INHIBIT);
  2394. intel_ring_emit(ring, MI_SUSPEND_FLUSH);
  2395. intel_ring_emit(ring, MI_NOOP);
  2396. intel_ring_emit(ring, MI_FLUSH);
  2397. intel_ring_advance(ring);
  2398. /*
  2399. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  2400. * does an implicit flush, combined with MI_FLUSH above, it should be
  2401. * safe to assume that renderctx is valid
  2402. */
  2403. ret = intel_ring_idle(ring);
  2404. dev_priv->mm.interruptible = was_interruptible;
  2405. if (ret) {
  2406. DRM_ERROR("failed to enable ironlake power power savings\n");
  2407. ironlake_teardown_rc6(dev);
  2408. return;
  2409. }
  2410. I915_WRITE(PWRCTXA, dev_priv->ips.pwrctx->gtt_offset | PWRCTX_EN);
  2411. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  2412. }
  2413. static unsigned long intel_pxfreq(u32 vidfreq)
  2414. {
  2415. unsigned long freq;
  2416. int div = (vidfreq & 0x3f0000) >> 16;
  2417. int post = (vidfreq & 0x3000) >> 12;
  2418. int pre = (vidfreq & 0x7);
  2419. if (!pre)
  2420. return 0;
  2421. freq = ((div * 133333) / ((1<<post) * pre));
  2422. return freq;
  2423. }
  2424. static const struct cparams {
  2425. u16 i;
  2426. u16 t;
  2427. u16 m;
  2428. u16 c;
  2429. } cparams[] = {
  2430. { 1, 1333, 301, 28664 },
  2431. { 1, 1066, 294, 24460 },
  2432. { 1, 800, 294, 25192 },
  2433. { 0, 1333, 276, 27605 },
  2434. { 0, 1066, 276, 27605 },
  2435. { 0, 800, 231, 23784 },
  2436. };
  2437. static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
  2438. {
  2439. u64 total_count, diff, ret;
  2440. u32 count1, count2, count3, m = 0, c = 0;
  2441. unsigned long now = jiffies_to_msecs(jiffies), diff1;
  2442. int i;
  2443. assert_spin_locked(&mchdev_lock);
  2444. diff1 = now - dev_priv->ips.last_time1;
  2445. /* Prevent division-by-zero if we are asking too fast.
  2446. * Also, we don't get interesting results if we are polling
  2447. * faster than once in 10ms, so just return the saved value
  2448. * in such cases.
  2449. */
  2450. if (diff1 <= 10)
  2451. return dev_priv->ips.chipset_power;
  2452. count1 = I915_READ(DMIEC);
  2453. count2 = I915_READ(DDREC);
  2454. count3 = I915_READ(CSIEC);
  2455. total_count = count1 + count2 + count3;
  2456. /* FIXME: handle per-counter overflow */
  2457. if (total_count < dev_priv->ips.last_count1) {
  2458. diff = ~0UL - dev_priv->ips.last_count1;
  2459. diff += total_count;
  2460. } else {
  2461. diff = total_count - dev_priv->ips.last_count1;
  2462. }
  2463. for (i = 0; i < ARRAY_SIZE(cparams); i++) {
  2464. if (cparams[i].i == dev_priv->ips.c_m &&
  2465. cparams[i].t == dev_priv->ips.r_t) {
  2466. m = cparams[i].m;
  2467. c = cparams[i].c;
  2468. break;
  2469. }
  2470. }
  2471. diff = div_u64(diff, diff1);
  2472. ret = ((m * diff) + c);
  2473. ret = div_u64(ret, 10);
  2474. dev_priv->ips.last_count1 = total_count;
  2475. dev_priv->ips.last_time1 = now;
  2476. dev_priv->ips.chipset_power = ret;
  2477. return ret;
  2478. }
  2479. unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
  2480. {
  2481. unsigned long val;
  2482. if (dev_priv->info->gen != 5)
  2483. return 0;
  2484. spin_lock_irq(&mchdev_lock);
  2485. val = __i915_chipset_val(dev_priv);
  2486. spin_unlock_irq(&mchdev_lock);
  2487. return val;
  2488. }
  2489. unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
  2490. {
  2491. unsigned long m, x, b;
  2492. u32 tsfs;
  2493. tsfs = I915_READ(TSFS);
  2494. m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
  2495. x = I915_READ8(TR1);
  2496. b = tsfs & TSFS_INTR_MASK;
  2497. return ((m * x) / 127) - b;
  2498. }
  2499. static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
  2500. {
  2501. static const struct v_table {
  2502. u16 vd; /* in .1 mil */
  2503. u16 vm; /* in .1 mil */
  2504. } v_table[] = {
  2505. { 0, 0, },
  2506. { 375, 0, },
  2507. { 500, 0, },
  2508. { 625, 0, },
  2509. { 750, 0, },
  2510. { 875, 0, },
  2511. { 1000, 0, },
  2512. { 1125, 0, },
  2513. { 4125, 3000, },
  2514. { 4125, 3000, },
  2515. { 4125, 3000, },
  2516. { 4125, 3000, },
  2517. { 4125, 3000, },
  2518. { 4125, 3000, },
  2519. { 4125, 3000, },
  2520. { 4125, 3000, },
  2521. { 4125, 3000, },
  2522. { 4125, 3000, },
  2523. { 4125, 3000, },
  2524. { 4125, 3000, },
  2525. { 4125, 3000, },
  2526. { 4125, 3000, },
  2527. { 4125, 3000, },
  2528. { 4125, 3000, },
  2529. { 4125, 3000, },
  2530. { 4125, 3000, },
  2531. { 4125, 3000, },
  2532. { 4125, 3000, },
  2533. { 4125, 3000, },
  2534. { 4125, 3000, },
  2535. { 4125, 3000, },
  2536. { 4125, 3000, },
  2537. { 4250, 3125, },
  2538. { 4375, 3250, },
  2539. { 4500, 3375, },
  2540. { 4625, 3500, },
  2541. { 4750, 3625, },
  2542. { 4875, 3750, },
  2543. { 5000, 3875, },
  2544. { 5125, 4000, },
  2545. { 5250, 4125, },
  2546. { 5375, 4250, },
  2547. { 5500, 4375, },
  2548. { 5625, 4500, },
  2549. { 5750, 4625, },
  2550. { 5875, 4750, },
  2551. { 6000, 4875, },
  2552. { 6125, 5000, },
  2553. { 6250, 5125, },
  2554. { 6375, 5250, },
  2555. { 6500, 5375, },
  2556. { 6625, 5500, },
  2557. { 6750, 5625, },
  2558. { 6875, 5750, },
  2559. { 7000, 5875, },
  2560. { 7125, 6000, },
  2561. { 7250, 6125, },
  2562. { 7375, 6250, },
  2563. { 7500, 6375, },
  2564. { 7625, 6500, },
  2565. { 7750, 6625, },
  2566. { 7875, 6750, },
  2567. { 8000, 6875, },
  2568. { 8125, 7000, },
  2569. { 8250, 7125, },
  2570. { 8375, 7250, },
  2571. { 8500, 7375, },
  2572. { 8625, 7500, },
  2573. { 8750, 7625, },
  2574. { 8875, 7750, },
  2575. { 9000, 7875, },
  2576. { 9125, 8000, },
  2577. { 9250, 8125, },
  2578. { 9375, 8250, },
  2579. { 9500, 8375, },
  2580. { 9625, 8500, },
  2581. { 9750, 8625, },
  2582. { 9875, 8750, },
  2583. { 10000, 8875, },
  2584. { 10125, 9000, },
  2585. { 10250, 9125, },
  2586. { 10375, 9250, },
  2587. { 10500, 9375, },
  2588. { 10625, 9500, },
  2589. { 10750, 9625, },
  2590. { 10875, 9750, },
  2591. { 11000, 9875, },
  2592. { 11125, 10000, },
  2593. { 11250, 10125, },
  2594. { 11375, 10250, },
  2595. { 11500, 10375, },
  2596. { 11625, 10500, },
  2597. { 11750, 10625, },
  2598. { 11875, 10750, },
  2599. { 12000, 10875, },
  2600. { 12125, 11000, },
  2601. { 12250, 11125, },
  2602. { 12375, 11250, },
  2603. { 12500, 11375, },
  2604. { 12625, 11500, },
  2605. { 12750, 11625, },
  2606. { 12875, 11750, },
  2607. { 13000, 11875, },
  2608. { 13125, 12000, },
  2609. { 13250, 12125, },
  2610. { 13375, 12250, },
  2611. { 13500, 12375, },
  2612. { 13625, 12500, },
  2613. { 13750, 12625, },
  2614. { 13875, 12750, },
  2615. { 14000, 12875, },
  2616. { 14125, 13000, },
  2617. { 14250, 13125, },
  2618. { 14375, 13250, },
  2619. { 14500, 13375, },
  2620. { 14625, 13500, },
  2621. { 14750, 13625, },
  2622. { 14875, 13750, },
  2623. { 15000, 13875, },
  2624. { 15125, 14000, },
  2625. { 15250, 14125, },
  2626. { 15375, 14250, },
  2627. { 15500, 14375, },
  2628. { 15625, 14500, },
  2629. { 15750, 14625, },
  2630. { 15875, 14750, },
  2631. { 16000, 14875, },
  2632. { 16125, 15000, },
  2633. };
  2634. if (dev_priv->info->is_mobile)
  2635. return v_table[pxvid].vm;
  2636. else
  2637. return v_table[pxvid].vd;
  2638. }
  2639. static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
  2640. {
  2641. struct timespec now, diff1;
  2642. u64 diff;
  2643. unsigned long diffms;
  2644. u32 count;
  2645. assert_spin_locked(&mchdev_lock);
  2646. getrawmonotonic(&now);
  2647. diff1 = timespec_sub(now, dev_priv->ips.last_time2);
  2648. /* Don't divide by 0 */
  2649. diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
  2650. if (!diffms)
  2651. return;
  2652. count = I915_READ(GFXEC);
  2653. if (count < dev_priv->ips.last_count2) {
  2654. diff = ~0UL - dev_priv->ips.last_count2;
  2655. diff += count;
  2656. } else {
  2657. diff = count - dev_priv->ips.last_count2;
  2658. }
  2659. dev_priv->ips.last_count2 = count;
  2660. dev_priv->ips.last_time2 = now;
  2661. /* More magic constants... */
  2662. diff = diff * 1181;
  2663. diff = div_u64(diff, diffms * 10);
  2664. dev_priv->ips.gfx_power = diff;
  2665. }
  2666. void i915_update_gfx_val(struct drm_i915_private *dev_priv)
  2667. {
  2668. if (dev_priv->info->gen != 5)
  2669. return;
  2670. spin_lock_irq(&mchdev_lock);
  2671. __i915_update_gfx_val(dev_priv);
  2672. spin_unlock_irq(&mchdev_lock);
  2673. }
  2674. static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
  2675. {
  2676. unsigned long t, corr, state1, corr2, state2;
  2677. u32 pxvid, ext_v;
  2678. assert_spin_locked(&mchdev_lock);
  2679. pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_delay * 4));
  2680. pxvid = (pxvid >> 24) & 0x7f;
  2681. ext_v = pvid_to_extvid(dev_priv, pxvid);
  2682. state1 = ext_v;
  2683. t = i915_mch_val(dev_priv);
  2684. /* Revel in the empirically derived constants */
  2685. /* Correction factor in 1/100000 units */
  2686. if (t > 80)
  2687. corr = ((t * 2349) + 135940);
  2688. else if (t >= 50)
  2689. corr = ((t * 964) + 29317);
  2690. else /* < 50 */
  2691. corr = ((t * 301) + 1004);
  2692. corr = corr * ((150142 * state1) / 10000 - 78642);
  2693. corr /= 100000;
  2694. corr2 = (corr * dev_priv->ips.corr);
  2695. state2 = (corr2 * state1) / 10000;
  2696. state2 /= 100; /* convert to mW */
  2697. __i915_update_gfx_val(dev_priv);
  2698. return dev_priv->ips.gfx_power + state2;
  2699. }
  2700. unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
  2701. {
  2702. unsigned long val;
  2703. if (dev_priv->info->gen != 5)
  2704. return 0;
  2705. spin_lock_irq(&mchdev_lock);
  2706. val = __i915_gfx_val(dev_priv);
  2707. spin_unlock_irq(&mchdev_lock);
  2708. return val;
  2709. }
  2710. /**
  2711. * i915_read_mch_val - return value for IPS use
  2712. *
  2713. * Calculate and return a value for the IPS driver to use when deciding whether
  2714. * we have thermal and power headroom to increase CPU or GPU power budget.
  2715. */
  2716. unsigned long i915_read_mch_val(void)
  2717. {
  2718. struct drm_i915_private *dev_priv;
  2719. unsigned long chipset_val, graphics_val, ret = 0;
  2720. spin_lock_irq(&mchdev_lock);
  2721. if (!i915_mch_dev)
  2722. goto out_unlock;
  2723. dev_priv = i915_mch_dev;
  2724. chipset_val = __i915_chipset_val(dev_priv);
  2725. graphics_val = __i915_gfx_val(dev_priv);
  2726. ret = chipset_val + graphics_val;
  2727. out_unlock:
  2728. spin_unlock_irq(&mchdev_lock);
  2729. return ret;
  2730. }
  2731. EXPORT_SYMBOL_GPL(i915_read_mch_val);
  2732. /**
  2733. * i915_gpu_raise - raise GPU frequency limit
  2734. *
  2735. * Raise the limit; IPS indicates we have thermal headroom.
  2736. */
  2737. bool i915_gpu_raise(void)
  2738. {
  2739. struct drm_i915_private *dev_priv;
  2740. bool ret = true;
  2741. spin_lock_irq(&mchdev_lock);
  2742. if (!i915_mch_dev) {
  2743. ret = false;
  2744. goto out_unlock;
  2745. }
  2746. dev_priv = i915_mch_dev;
  2747. if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
  2748. dev_priv->ips.max_delay--;
  2749. out_unlock:
  2750. spin_unlock_irq(&mchdev_lock);
  2751. return ret;
  2752. }
  2753. EXPORT_SYMBOL_GPL(i915_gpu_raise);
  2754. /**
  2755. * i915_gpu_lower - lower GPU frequency limit
  2756. *
  2757. * IPS indicates we're close to a thermal limit, so throttle back the GPU
  2758. * frequency maximum.
  2759. */
  2760. bool i915_gpu_lower(void)
  2761. {
  2762. struct drm_i915_private *dev_priv;
  2763. bool ret = true;
  2764. spin_lock_irq(&mchdev_lock);
  2765. if (!i915_mch_dev) {
  2766. ret = false;
  2767. goto out_unlock;
  2768. }
  2769. dev_priv = i915_mch_dev;
  2770. if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
  2771. dev_priv->ips.max_delay++;
  2772. out_unlock:
  2773. spin_unlock_irq(&mchdev_lock);
  2774. return ret;
  2775. }
  2776. EXPORT_SYMBOL_GPL(i915_gpu_lower);
  2777. /**
  2778. * i915_gpu_busy - indicate GPU business to IPS
  2779. *
  2780. * Tell the IPS driver whether or not the GPU is busy.
  2781. */
  2782. bool i915_gpu_busy(void)
  2783. {
  2784. struct drm_i915_private *dev_priv;
  2785. struct intel_ring_buffer *ring;
  2786. bool ret = false;
  2787. int i;
  2788. spin_lock_irq(&mchdev_lock);
  2789. if (!i915_mch_dev)
  2790. goto out_unlock;
  2791. dev_priv = i915_mch_dev;
  2792. for_each_ring(ring, dev_priv, i)
  2793. ret |= !list_empty(&ring->request_list);
  2794. out_unlock:
  2795. spin_unlock_irq(&mchdev_lock);
  2796. return ret;
  2797. }
  2798. EXPORT_SYMBOL_GPL(i915_gpu_busy);
  2799. /**
  2800. * i915_gpu_turbo_disable - disable graphics turbo
  2801. *
  2802. * Disable graphics turbo by resetting the max frequency and setting the
  2803. * current frequency to the default.
  2804. */
  2805. bool i915_gpu_turbo_disable(void)
  2806. {
  2807. struct drm_i915_private *dev_priv;
  2808. bool ret = true;
  2809. spin_lock_irq(&mchdev_lock);
  2810. if (!i915_mch_dev) {
  2811. ret = false;
  2812. goto out_unlock;
  2813. }
  2814. dev_priv = i915_mch_dev;
  2815. dev_priv->ips.max_delay = dev_priv->ips.fstart;
  2816. if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
  2817. ret = false;
  2818. out_unlock:
  2819. spin_unlock_irq(&mchdev_lock);
  2820. return ret;
  2821. }
  2822. EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
  2823. /**
  2824. * Tells the intel_ips driver that the i915 driver is now loaded, if
  2825. * IPS got loaded first.
  2826. *
  2827. * This awkward dance is so that neither module has to depend on the
  2828. * other in order for IPS to do the appropriate communication of
  2829. * GPU turbo limits to i915.
  2830. */
  2831. static void
  2832. ips_ping_for_i915_load(void)
  2833. {
  2834. void (*link)(void);
  2835. link = symbol_get(ips_link_to_i915_driver);
  2836. if (link) {
  2837. link();
  2838. symbol_put(ips_link_to_i915_driver);
  2839. }
  2840. }
  2841. void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
  2842. {
  2843. /* We only register the i915 ips part with intel-ips once everything is
  2844. * set up, to avoid intel-ips sneaking in and reading bogus values. */
  2845. spin_lock_irq(&mchdev_lock);
  2846. i915_mch_dev = dev_priv;
  2847. spin_unlock_irq(&mchdev_lock);
  2848. ips_ping_for_i915_load();
  2849. }
  2850. void intel_gpu_ips_teardown(void)
  2851. {
  2852. spin_lock_irq(&mchdev_lock);
  2853. i915_mch_dev = NULL;
  2854. spin_unlock_irq(&mchdev_lock);
  2855. }
  2856. static void intel_init_emon(struct drm_device *dev)
  2857. {
  2858. struct drm_i915_private *dev_priv = dev->dev_private;
  2859. u32 lcfuse;
  2860. u8 pxw[16];
  2861. int i;
  2862. /* Disable to program */
  2863. I915_WRITE(ECR, 0);
  2864. POSTING_READ(ECR);
  2865. /* Program energy weights for various events */
  2866. I915_WRITE(SDEW, 0x15040d00);
  2867. I915_WRITE(CSIEW0, 0x007f0000);
  2868. I915_WRITE(CSIEW1, 0x1e220004);
  2869. I915_WRITE(CSIEW2, 0x04000004);
  2870. for (i = 0; i < 5; i++)
  2871. I915_WRITE(PEW + (i * 4), 0);
  2872. for (i = 0; i < 3; i++)
  2873. I915_WRITE(DEW + (i * 4), 0);
  2874. /* Program P-state weights to account for frequency power adjustment */
  2875. for (i = 0; i < 16; i++) {
  2876. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  2877. unsigned long freq = intel_pxfreq(pxvidfreq);
  2878. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  2879. PXVFREQ_PX_SHIFT;
  2880. unsigned long val;
  2881. val = vid * vid;
  2882. val *= (freq / 1000);
  2883. val *= 255;
  2884. val /= (127*127*900);
  2885. if (val > 0xff)
  2886. DRM_ERROR("bad pxval: %ld\n", val);
  2887. pxw[i] = val;
  2888. }
  2889. /* Render standby states get 0 weight */
  2890. pxw[14] = 0;
  2891. pxw[15] = 0;
  2892. for (i = 0; i < 4; i++) {
  2893. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  2894. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  2895. I915_WRITE(PXW + (i * 4), val);
  2896. }
  2897. /* Adjust magic regs to magic values (more experimental results) */
  2898. I915_WRITE(OGW0, 0);
  2899. I915_WRITE(OGW1, 0);
  2900. I915_WRITE(EG0, 0x00007f00);
  2901. I915_WRITE(EG1, 0x0000000e);
  2902. I915_WRITE(EG2, 0x000e0000);
  2903. I915_WRITE(EG3, 0x68000300);
  2904. I915_WRITE(EG4, 0x42000000);
  2905. I915_WRITE(EG5, 0x00140031);
  2906. I915_WRITE(EG6, 0);
  2907. I915_WRITE(EG7, 0);
  2908. for (i = 0; i < 8; i++)
  2909. I915_WRITE(PXWL + (i * 4), 0);
  2910. /* Enable PMON + select events */
  2911. I915_WRITE(ECR, 0x80000019);
  2912. lcfuse = I915_READ(LCFUSE02);
  2913. dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
  2914. }
  2915. void intel_disable_gt_powersave(struct drm_device *dev)
  2916. {
  2917. struct drm_i915_private *dev_priv = dev->dev_private;
  2918. if (IS_IRONLAKE_M(dev)) {
  2919. ironlake_disable_drps(dev);
  2920. ironlake_disable_rc6(dev);
  2921. } else if (INTEL_INFO(dev)->gen >= 6 && !IS_VALLEYVIEW(dev)) {
  2922. cancel_delayed_work_sync(&dev_priv->rps.delayed_resume_work);
  2923. mutex_lock(&dev_priv->rps.hw_lock);
  2924. gen6_disable_rps(dev);
  2925. mutex_unlock(&dev_priv->rps.hw_lock);
  2926. }
  2927. }
  2928. static void intel_gen6_powersave_work(struct work_struct *work)
  2929. {
  2930. struct drm_i915_private *dev_priv =
  2931. container_of(work, struct drm_i915_private,
  2932. rps.delayed_resume_work.work);
  2933. struct drm_device *dev = dev_priv->dev;
  2934. mutex_lock(&dev_priv->rps.hw_lock);
  2935. gen6_enable_rps(dev);
  2936. gen6_update_ring_freq(dev);
  2937. mutex_unlock(&dev_priv->rps.hw_lock);
  2938. }
  2939. void intel_enable_gt_powersave(struct drm_device *dev)
  2940. {
  2941. struct drm_i915_private *dev_priv = dev->dev_private;
  2942. if (IS_IRONLAKE_M(dev)) {
  2943. ironlake_enable_drps(dev);
  2944. ironlake_enable_rc6(dev);
  2945. intel_init_emon(dev);
  2946. } else if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev)) {
  2947. /*
  2948. * PCU communication is slow and this doesn't need to be
  2949. * done at any specific time, so do this out of our fast path
  2950. * to make resume and init faster.
  2951. */
  2952. schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
  2953. round_jiffies_up_relative(HZ));
  2954. }
  2955. }
  2956. static void ibx_init_clock_gating(struct drm_device *dev)
  2957. {
  2958. struct drm_i915_private *dev_priv = dev->dev_private;
  2959. /*
  2960. * On Ibex Peak and Cougar Point, we need to disable clock
  2961. * gating for the panel power sequencer or it will fail to
  2962. * start up when no ports are active.
  2963. */
  2964. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  2965. }
  2966. static void ironlake_init_clock_gating(struct drm_device *dev)
  2967. {
  2968. struct drm_i915_private *dev_priv = dev->dev_private;
  2969. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  2970. /* Required for FBC */
  2971. dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
  2972. ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
  2973. ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
  2974. I915_WRITE(PCH_3DCGDIS0,
  2975. MARIUNIT_CLOCK_GATE_DISABLE |
  2976. SVSMUNIT_CLOCK_GATE_DISABLE);
  2977. I915_WRITE(PCH_3DCGDIS1,
  2978. VFMUNIT_CLOCK_GATE_DISABLE);
  2979. /*
  2980. * According to the spec the following bits should be set in
  2981. * order to enable memory self-refresh
  2982. * The bit 22/21 of 0x42004
  2983. * The bit 5 of 0x42020
  2984. * The bit 15 of 0x45000
  2985. */
  2986. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  2987. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  2988. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  2989. dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
  2990. I915_WRITE(DISP_ARB_CTL,
  2991. (I915_READ(DISP_ARB_CTL) |
  2992. DISP_FBC_WM_DIS));
  2993. I915_WRITE(WM3_LP_ILK, 0);
  2994. I915_WRITE(WM2_LP_ILK, 0);
  2995. I915_WRITE(WM1_LP_ILK, 0);
  2996. /*
  2997. * Based on the document from hardware guys the following bits
  2998. * should be set unconditionally in order to enable FBC.
  2999. * The bit 22 of 0x42000
  3000. * The bit 22 of 0x42004
  3001. * The bit 7,8,9 of 0x42020.
  3002. */
  3003. if (IS_IRONLAKE_M(dev)) {
  3004. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  3005. I915_READ(ILK_DISPLAY_CHICKEN1) |
  3006. ILK_FBCQ_DIS);
  3007. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3008. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3009. ILK_DPARB_GATE);
  3010. }
  3011. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  3012. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3013. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3014. ILK_ELPIN_409_SELECT);
  3015. I915_WRITE(_3D_CHICKEN2,
  3016. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  3017. _3D_CHICKEN2_WM_READ_PIPELINED);
  3018. /* WaDisableRenderCachePipelinedFlush */
  3019. I915_WRITE(CACHE_MODE_0,
  3020. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  3021. ibx_init_clock_gating(dev);
  3022. }
  3023. static void cpt_init_clock_gating(struct drm_device *dev)
  3024. {
  3025. struct drm_i915_private *dev_priv = dev->dev_private;
  3026. int pipe;
  3027. /*
  3028. * On Ibex Peak and Cougar Point, we need to disable clock
  3029. * gating for the panel power sequencer or it will fail to
  3030. * start up when no ports are active.
  3031. */
  3032. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  3033. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  3034. DPLS_EDP_PPS_FIX_DIS);
  3035. /* The below fixes the weird display corruption, a few pixels shifted
  3036. * downward, on (only) LVDS of some HP laptops with IVY.
  3037. */
  3038. for_each_pipe(pipe)
  3039. I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_CHICKEN2_TIMING_OVERRIDE);
  3040. /* WADP0ClockGatingDisable */
  3041. for_each_pipe(pipe) {
  3042. I915_WRITE(TRANS_CHICKEN1(pipe),
  3043. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  3044. }
  3045. }
  3046. static void gen6_init_clock_gating(struct drm_device *dev)
  3047. {
  3048. struct drm_i915_private *dev_priv = dev->dev_private;
  3049. int pipe;
  3050. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  3051. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  3052. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3053. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3054. ILK_ELPIN_409_SELECT);
  3055. I915_WRITE(WM3_LP_ILK, 0);
  3056. I915_WRITE(WM2_LP_ILK, 0);
  3057. I915_WRITE(WM1_LP_ILK, 0);
  3058. I915_WRITE(CACHE_MODE_0,
  3059. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  3060. I915_WRITE(GEN6_UCGCTL1,
  3061. I915_READ(GEN6_UCGCTL1) |
  3062. GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
  3063. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  3064. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  3065. * gating disable must be set. Failure to set it results in
  3066. * flickering pixels due to Z write ordering failures after
  3067. * some amount of runtime in the Mesa "fire" demo, and Unigine
  3068. * Sanctuary and Tropics, and apparently anything else with
  3069. * alpha test or pixel discard.
  3070. *
  3071. * According to the spec, bit 11 (RCCUNIT) must also be set,
  3072. * but we didn't debug actual testcases to find it out.
  3073. *
  3074. * Also apply WaDisableVDSUnitClockGating and
  3075. * WaDisableRCPBUnitClockGating.
  3076. */
  3077. I915_WRITE(GEN6_UCGCTL2,
  3078. GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
  3079. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  3080. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  3081. /* Bspec says we need to always set all mask bits. */
  3082. I915_WRITE(_3D_CHICKEN3, (0xFFFF << 16) |
  3083. _3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL);
  3084. /*
  3085. * According to the spec the following bits should be
  3086. * set in order to enable memory self-refresh and fbc:
  3087. * The bit21 and bit22 of 0x42000
  3088. * The bit21 and bit22 of 0x42004
  3089. * The bit5 and bit7 of 0x42020
  3090. * The bit14 of 0x70180
  3091. * The bit14 of 0x71180
  3092. */
  3093. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  3094. I915_READ(ILK_DISPLAY_CHICKEN1) |
  3095. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  3096. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3097. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3098. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  3099. I915_WRITE(ILK_DSPCLK_GATE_D,
  3100. I915_READ(ILK_DSPCLK_GATE_D) |
  3101. ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
  3102. ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
  3103. /* WaMbcDriverBootEnable */
  3104. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  3105. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  3106. for_each_pipe(pipe) {
  3107. I915_WRITE(DSPCNTR(pipe),
  3108. I915_READ(DSPCNTR(pipe)) |
  3109. DISPPLANE_TRICKLE_FEED_DISABLE);
  3110. intel_flush_display_plane(dev_priv, pipe);
  3111. }
  3112. /* The default value should be 0x200 according to docs, but the two
  3113. * platforms I checked have a 0 for this. (Maybe BIOS overrides?) */
  3114. I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_DISABLE(0xffff));
  3115. I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_ENABLE(GEN6_GT_MODE_HI));
  3116. cpt_init_clock_gating(dev);
  3117. }
  3118. static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
  3119. {
  3120. uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
  3121. reg &= ~GEN7_FF_SCHED_MASK;
  3122. reg |= GEN7_FF_TS_SCHED_HW;
  3123. reg |= GEN7_FF_VS_SCHED_HW;
  3124. reg |= GEN7_FF_DS_SCHED_HW;
  3125. I915_WRITE(GEN7_FF_THREAD_MODE, reg);
  3126. }
  3127. static void lpt_init_clock_gating(struct drm_device *dev)
  3128. {
  3129. struct drm_i915_private *dev_priv = dev->dev_private;
  3130. /*
  3131. * TODO: this bit should only be enabled when really needed, then
  3132. * disabled when not needed anymore in order to save power.
  3133. */
  3134. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
  3135. I915_WRITE(SOUTH_DSPCLK_GATE_D,
  3136. I915_READ(SOUTH_DSPCLK_GATE_D) |
  3137. PCH_LP_PARTITION_LEVEL_DISABLE);
  3138. }
  3139. static void haswell_init_clock_gating(struct drm_device *dev)
  3140. {
  3141. struct drm_i915_private *dev_priv = dev->dev_private;
  3142. int pipe;
  3143. I915_WRITE(WM3_LP_ILK, 0);
  3144. I915_WRITE(WM2_LP_ILK, 0);
  3145. I915_WRITE(WM1_LP_ILK, 0);
  3146. /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  3147. * This implements the WaDisableRCZUnitClockGating workaround.
  3148. */
  3149. I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  3150. /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
  3151. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  3152. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  3153. /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
  3154. I915_WRITE(GEN7_L3CNTLREG1,
  3155. GEN7_WA_FOR_GEN7_L3_CONTROL);
  3156. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  3157. GEN7_WA_L3_CHICKEN_MODE);
  3158. /* This is required by WaCatErrorRejectionIssue */
  3159. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  3160. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  3161. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  3162. for_each_pipe(pipe) {
  3163. I915_WRITE(DSPCNTR(pipe),
  3164. I915_READ(DSPCNTR(pipe)) |
  3165. DISPPLANE_TRICKLE_FEED_DISABLE);
  3166. intel_flush_display_plane(dev_priv, pipe);
  3167. }
  3168. gen7_setup_fixed_func_scheduler(dev_priv);
  3169. /* WaDisable4x2SubspanOptimization */
  3170. I915_WRITE(CACHE_MODE_1,
  3171. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  3172. /* WaMbcDriverBootEnable */
  3173. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  3174. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  3175. /* XXX: This is a workaround for early silicon revisions and should be
  3176. * removed later.
  3177. */
  3178. I915_WRITE(WM_DBG,
  3179. I915_READ(WM_DBG) |
  3180. WM_DBG_DISALLOW_MULTIPLE_LP |
  3181. WM_DBG_DISALLOW_SPRITE |
  3182. WM_DBG_DISALLOW_MAXFIFO);
  3183. lpt_init_clock_gating(dev);
  3184. }
  3185. static void ivybridge_init_clock_gating(struct drm_device *dev)
  3186. {
  3187. struct drm_i915_private *dev_priv = dev->dev_private;
  3188. int pipe;
  3189. uint32_t snpcr;
  3190. I915_WRITE(WM3_LP_ILK, 0);
  3191. I915_WRITE(WM2_LP_ILK, 0);
  3192. I915_WRITE(WM1_LP_ILK, 0);
  3193. I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
  3194. /* WaDisableEarlyCull */
  3195. I915_WRITE(_3D_CHICKEN3,
  3196. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  3197. /* WaDisableBackToBackFlipFix */
  3198. I915_WRITE(IVB_CHICKEN3,
  3199. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  3200. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  3201. /* WaDisablePSDDualDispatchEnable */
  3202. if (IS_IVB_GT1(dev))
  3203. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  3204. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  3205. else
  3206. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1_GT2,
  3207. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  3208. /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
  3209. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  3210. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  3211. /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
  3212. I915_WRITE(GEN7_L3CNTLREG1,
  3213. GEN7_WA_FOR_GEN7_L3_CONTROL);
  3214. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  3215. GEN7_WA_L3_CHICKEN_MODE);
  3216. if (IS_IVB_GT1(dev))
  3217. I915_WRITE(GEN7_ROW_CHICKEN2,
  3218. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  3219. else
  3220. I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
  3221. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  3222. /* WaForceL3Serialization */
  3223. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  3224. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  3225. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  3226. * gating disable must be set. Failure to set it results in
  3227. * flickering pixels due to Z write ordering failures after
  3228. * some amount of runtime in the Mesa "fire" demo, and Unigine
  3229. * Sanctuary and Tropics, and apparently anything else with
  3230. * alpha test or pixel discard.
  3231. *
  3232. * According to the spec, bit 11 (RCCUNIT) must also be set,
  3233. * but we didn't debug actual testcases to find it out.
  3234. *
  3235. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  3236. * This implements the WaDisableRCZUnitClockGating workaround.
  3237. */
  3238. I915_WRITE(GEN6_UCGCTL2,
  3239. GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
  3240. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  3241. /* This is required by WaCatErrorRejectionIssue */
  3242. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  3243. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  3244. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  3245. for_each_pipe(pipe) {
  3246. I915_WRITE(DSPCNTR(pipe),
  3247. I915_READ(DSPCNTR(pipe)) |
  3248. DISPPLANE_TRICKLE_FEED_DISABLE);
  3249. intel_flush_display_plane(dev_priv, pipe);
  3250. }
  3251. /* WaMbcDriverBootEnable */
  3252. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  3253. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  3254. gen7_setup_fixed_func_scheduler(dev_priv);
  3255. /* WaDisable4x2SubspanOptimization */
  3256. I915_WRITE(CACHE_MODE_1,
  3257. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  3258. snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
  3259. snpcr &= ~GEN6_MBC_SNPCR_MASK;
  3260. snpcr |= GEN6_MBC_SNPCR_MED;
  3261. I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
  3262. cpt_init_clock_gating(dev);
  3263. }
  3264. static void valleyview_init_clock_gating(struct drm_device *dev)
  3265. {
  3266. struct drm_i915_private *dev_priv = dev->dev_private;
  3267. int pipe;
  3268. I915_WRITE(WM3_LP_ILK, 0);
  3269. I915_WRITE(WM2_LP_ILK, 0);
  3270. I915_WRITE(WM1_LP_ILK, 0);
  3271. I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
  3272. /* WaDisableEarlyCull */
  3273. I915_WRITE(_3D_CHICKEN3,
  3274. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  3275. /* WaDisableBackToBackFlipFix */
  3276. I915_WRITE(IVB_CHICKEN3,
  3277. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  3278. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  3279. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  3280. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  3281. /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
  3282. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  3283. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  3284. /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
  3285. I915_WRITE(GEN7_L3CNTLREG1, I915_READ(GEN7_L3CNTLREG1) | GEN7_L3AGDIS);
  3286. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
  3287. /* WaForceL3Serialization */
  3288. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  3289. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  3290. /* WaDisableDopClockGating */
  3291. I915_WRITE(GEN7_ROW_CHICKEN2,
  3292. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  3293. /* WaForceL3Serialization */
  3294. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  3295. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  3296. /* This is required by WaCatErrorRejectionIssue */
  3297. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  3298. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  3299. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  3300. /* WaMbcDriverBootEnable */
  3301. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  3302. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  3303. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  3304. * gating disable must be set. Failure to set it results in
  3305. * flickering pixels due to Z write ordering failures after
  3306. * some amount of runtime in the Mesa "fire" demo, and Unigine
  3307. * Sanctuary and Tropics, and apparently anything else with
  3308. * alpha test or pixel discard.
  3309. *
  3310. * According to the spec, bit 11 (RCCUNIT) must also be set,
  3311. * but we didn't debug actual testcases to find it out.
  3312. *
  3313. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  3314. * This implements the WaDisableRCZUnitClockGating workaround.
  3315. *
  3316. * Also apply WaDisableVDSUnitClockGating and
  3317. * WaDisableRCPBUnitClockGating.
  3318. */
  3319. I915_WRITE(GEN6_UCGCTL2,
  3320. GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
  3321. GEN7_TDLUNIT_CLOCK_GATE_DISABLE |
  3322. GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
  3323. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  3324. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  3325. I915_WRITE(GEN7_UCGCTL4, GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
  3326. for_each_pipe(pipe) {
  3327. I915_WRITE(DSPCNTR(pipe),
  3328. I915_READ(DSPCNTR(pipe)) |
  3329. DISPPLANE_TRICKLE_FEED_DISABLE);
  3330. intel_flush_display_plane(dev_priv, pipe);
  3331. }
  3332. I915_WRITE(CACHE_MODE_1,
  3333. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  3334. /*
  3335. * On ValleyView, the GUnit needs to signal the GT
  3336. * when flip and other events complete. So enable
  3337. * all the GUnit->GT interrupts here
  3338. */
  3339. I915_WRITE(VLV_DPFLIPSTAT, PIPEB_LINE_COMPARE_INT_EN |
  3340. PIPEB_HLINE_INT_EN | PIPEB_VBLANK_INT_EN |
  3341. SPRITED_FLIPDONE_INT_EN | SPRITEC_FLIPDONE_INT_EN |
  3342. PLANEB_FLIPDONE_INT_EN | PIPEA_LINE_COMPARE_INT_EN |
  3343. PIPEA_HLINE_INT_EN | PIPEA_VBLANK_INT_EN |
  3344. SPRITEB_FLIPDONE_INT_EN | SPRITEA_FLIPDONE_INT_EN |
  3345. PLANEA_FLIPDONE_INT_EN);
  3346. /*
  3347. * WaDisableVLVClockGating_VBIIssue
  3348. * Disable clock gating on th GCFG unit to prevent a delay
  3349. * in the reporting of vblank events.
  3350. */
  3351. I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
  3352. }
  3353. static void g4x_init_clock_gating(struct drm_device *dev)
  3354. {
  3355. struct drm_i915_private *dev_priv = dev->dev_private;
  3356. uint32_t dspclk_gate;
  3357. I915_WRITE(RENCLK_GATE_D1, 0);
  3358. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  3359. GS_UNIT_CLOCK_GATE_DISABLE |
  3360. CL_UNIT_CLOCK_GATE_DISABLE);
  3361. I915_WRITE(RAMCLK_GATE_D, 0);
  3362. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  3363. OVRUNIT_CLOCK_GATE_DISABLE |
  3364. OVCUNIT_CLOCK_GATE_DISABLE;
  3365. if (IS_GM45(dev))
  3366. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  3367. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  3368. /* WaDisableRenderCachePipelinedFlush */
  3369. I915_WRITE(CACHE_MODE_0,
  3370. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  3371. }
  3372. static void crestline_init_clock_gating(struct drm_device *dev)
  3373. {
  3374. struct drm_i915_private *dev_priv = dev->dev_private;
  3375. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  3376. I915_WRITE(RENCLK_GATE_D2, 0);
  3377. I915_WRITE(DSPCLK_GATE_D, 0);
  3378. I915_WRITE(RAMCLK_GATE_D, 0);
  3379. I915_WRITE16(DEUC, 0);
  3380. }
  3381. static void broadwater_init_clock_gating(struct drm_device *dev)
  3382. {
  3383. struct drm_i915_private *dev_priv = dev->dev_private;
  3384. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  3385. I965_RCC_CLOCK_GATE_DISABLE |
  3386. I965_RCPB_CLOCK_GATE_DISABLE |
  3387. I965_ISC_CLOCK_GATE_DISABLE |
  3388. I965_FBC_CLOCK_GATE_DISABLE);
  3389. I915_WRITE(RENCLK_GATE_D2, 0);
  3390. }
  3391. static void gen3_init_clock_gating(struct drm_device *dev)
  3392. {
  3393. struct drm_i915_private *dev_priv = dev->dev_private;
  3394. u32 dstate = I915_READ(D_STATE);
  3395. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  3396. DSTATE_DOT_CLOCK_GATING;
  3397. I915_WRITE(D_STATE, dstate);
  3398. if (IS_PINEVIEW(dev))
  3399. I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
  3400. /* IIR "flip pending" means done if this bit is set */
  3401. I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
  3402. }
  3403. static void i85x_init_clock_gating(struct drm_device *dev)
  3404. {
  3405. struct drm_i915_private *dev_priv = dev->dev_private;
  3406. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  3407. }
  3408. static void i830_init_clock_gating(struct drm_device *dev)
  3409. {
  3410. struct drm_i915_private *dev_priv = dev->dev_private;
  3411. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  3412. }
  3413. void intel_init_clock_gating(struct drm_device *dev)
  3414. {
  3415. struct drm_i915_private *dev_priv = dev->dev_private;
  3416. dev_priv->display.init_clock_gating(dev);
  3417. }
  3418. /* Starting with Haswell, we have different power wells for
  3419. * different parts of the GPU. This attempts to enable them all.
  3420. */
  3421. void intel_init_power_wells(struct drm_device *dev)
  3422. {
  3423. struct drm_i915_private *dev_priv = dev->dev_private;
  3424. unsigned long power_wells[] = {
  3425. HSW_PWR_WELL_CTL1,
  3426. HSW_PWR_WELL_CTL2,
  3427. HSW_PWR_WELL_CTL4
  3428. };
  3429. int i;
  3430. if (!IS_HASWELL(dev))
  3431. return;
  3432. mutex_lock(&dev->struct_mutex);
  3433. for (i = 0; i < ARRAY_SIZE(power_wells); i++) {
  3434. int well = I915_READ(power_wells[i]);
  3435. if ((well & HSW_PWR_WELL_STATE) == 0) {
  3436. I915_WRITE(power_wells[i], well & HSW_PWR_WELL_ENABLE);
  3437. if (wait_for((I915_READ(power_wells[i]) & HSW_PWR_WELL_STATE), 20))
  3438. DRM_ERROR("Error enabling power well %lx\n", power_wells[i]);
  3439. }
  3440. }
  3441. mutex_unlock(&dev->struct_mutex);
  3442. }
  3443. /* Set up chip specific power management-related functions */
  3444. void intel_init_pm(struct drm_device *dev)
  3445. {
  3446. struct drm_i915_private *dev_priv = dev->dev_private;
  3447. if (I915_HAS_FBC(dev)) {
  3448. if (HAS_PCH_SPLIT(dev)) {
  3449. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  3450. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  3451. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  3452. } else if (IS_GM45(dev)) {
  3453. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  3454. dev_priv->display.enable_fbc = g4x_enable_fbc;
  3455. dev_priv->display.disable_fbc = g4x_disable_fbc;
  3456. } else if (IS_CRESTLINE(dev)) {
  3457. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  3458. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  3459. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  3460. }
  3461. /* 855GM needs testing */
  3462. }
  3463. /* For cxsr */
  3464. if (IS_PINEVIEW(dev))
  3465. i915_pineview_get_mem_freq(dev);
  3466. else if (IS_GEN5(dev))
  3467. i915_ironlake_get_mem_freq(dev);
  3468. /* For FIFO watermark updates */
  3469. if (HAS_PCH_SPLIT(dev)) {
  3470. if (IS_GEN5(dev)) {
  3471. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  3472. dev_priv->display.update_wm = ironlake_update_wm;
  3473. else {
  3474. DRM_DEBUG_KMS("Failed to get proper latency. "
  3475. "Disable CxSR\n");
  3476. dev_priv->display.update_wm = NULL;
  3477. }
  3478. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  3479. } else if (IS_GEN6(dev)) {
  3480. if (SNB_READ_WM0_LATENCY()) {
  3481. dev_priv->display.update_wm = sandybridge_update_wm;
  3482. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  3483. } else {
  3484. DRM_DEBUG_KMS("Failed to read display plane latency. "
  3485. "Disable CxSR\n");
  3486. dev_priv->display.update_wm = NULL;
  3487. }
  3488. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  3489. } else if (IS_IVYBRIDGE(dev)) {
  3490. /* FIXME: detect B0+ stepping and use auto training */
  3491. if (SNB_READ_WM0_LATENCY()) {
  3492. dev_priv->display.update_wm = ivybridge_update_wm;
  3493. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  3494. } else {
  3495. DRM_DEBUG_KMS("Failed to read display plane latency. "
  3496. "Disable CxSR\n");
  3497. dev_priv->display.update_wm = NULL;
  3498. }
  3499. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  3500. } else if (IS_HASWELL(dev)) {
  3501. if (SNB_READ_WM0_LATENCY()) {
  3502. dev_priv->display.update_wm = sandybridge_update_wm;
  3503. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  3504. dev_priv->display.update_linetime_wm = haswell_update_linetime_wm;
  3505. } else {
  3506. DRM_DEBUG_KMS("Failed to read display plane latency. "
  3507. "Disable CxSR\n");
  3508. dev_priv->display.update_wm = NULL;
  3509. }
  3510. dev_priv->display.init_clock_gating = haswell_init_clock_gating;
  3511. } else
  3512. dev_priv->display.update_wm = NULL;
  3513. } else if (IS_VALLEYVIEW(dev)) {
  3514. dev_priv->display.update_wm = valleyview_update_wm;
  3515. dev_priv->display.init_clock_gating =
  3516. valleyview_init_clock_gating;
  3517. } else if (IS_PINEVIEW(dev)) {
  3518. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  3519. dev_priv->is_ddr3,
  3520. dev_priv->fsb_freq,
  3521. dev_priv->mem_freq)) {
  3522. DRM_INFO("failed to find known CxSR latency "
  3523. "(found ddr%s fsb freq %d, mem freq %d), "
  3524. "disabling CxSR\n",
  3525. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  3526. dev_priv->fsb_freq, dev_priv->mem_freq);
  3527. /* Disable CxSR and never update its watermark again */
  3528. pineview_disable_cxsr(dev);
  3529. dev_priv->display.update_wm = NULL;
  3530. } else
  3531. dev_priv->display.update_wm = pineview_update_wm;
  3532. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  3533. } else if (IS_G4X(dev)) {
  3534. dev_priv->display.update_wm = g4x_update_wm;
  3535. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  3536. } else if (IS_GEN4(dev)) {
  3537. dev_priv->display.update_wm = i965_update_wm;
  3538. if (IS_CRESTLINE(dev))
  3539. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  3540. else if (IS_BROADWATER(dev))
  3541. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  3542. } else if (IS_GEN3(dev)) {
  3543. dev_priv->display.update_wm = i9xx_update_wm;
  3544. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  3545. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  3546. } else if (IS_I865G(dev)) {
  3547. dev_priv->display.update_wm = i830_update_wm;
  3548. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  3549. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  3550. } else if (IS_I85X(dev)) {
  3551. dev_priv->display.update_wm = i9xx_update_wm;
  3552. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  3553. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  3554. } else {
  3555. dev_priv->display.update_wm = i830_update_wm;
  3556. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  3557. if (IS_845G(dev))
  3558. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  3559. else
  3560. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  3561. }
  3562. }
  3563. static void __gen6_gt_wait_for_thread_c0(struct drm_i915_private *dev_priv)
  3564. {
  3565. u32 gt_thread_status_mask;
  3566. if (IS_HASWELL(dev_priv->dev))
  3567. gt_thread_status_mask = GEN6_GT_THREAD_STATUS_CORE_MASK_HSW;
  3568. else
  3569. gt_thread_status_mask = GEN6_GT_THREAD_STATUS_CORE_MASK;
  3570. /* w/a for a sporadic read returning 0 by waiting for the GT
  3571. * thread to wake up.
  3572. */
  3573. if (wait_for_atomic_us((I915_READ_NOTRACE(GEN6_GT_THREAD_STATUS_REG) & gt_thread_status_mask) == 0, 500))
  3574. DRM_ERROR("GT thread status wait timed out\n");
  3575. }
  3576. static void __gen6_gt_force_wake_reset(struct drm_i915_private *dev_priv)
  3577. {
  3578. I915_WRITE_NOTRACE(FORCEWAKE, 0);
  3579. POSTING_READ(ECOBUS); /* something from same cacheline, but !FORCEWAKE */
  3580. }
  3581. static void __gen6_gt_force_wake_get(struct drm_i915_private *dev_priv)
  3582. {
  3583. u32 forcewake_ack;
  3584. if (IS_HASWELL(dev_priv->dev))
  3585. forcewake_ack = FORCEWAKE_ACK_HSW;
  3586. else
  3587. forcewake_ack = FORCEWAKE_ACK;
  3588. if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1) == 0,
  3589. FORCEWAKE_ACK_TIMEOUT_MS))
  3590. DRM_ERROR("Timed out waiting for forcewake old ack to clear.\n");
  3591. I915_WRITE_NOTRACE(FORCEWAKE, FORCEWAKE_KERNEL);
  3592. POSTING_READ(ECOBUS); /* something from same cacheline, but !FORCEWAKE */
  3593. if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1),
  3594. FORCEWAKE_ACK_TIMEOUT_MS))
  3595. DRM_ERROR("Timed out waiting for forcewake to ack request.\n");
  3596. __gen6_gt_wait_for_thread_c0(dev_priv);
  3597. }
  3598. static void __gen6_gt_force_wake_mt_reset(struct drm_i915_private *dev_priv)
  3599. {
  3600. I915_WRITE_NOTRACE(FORCEWAKE_MT, _MASKED_BIT_DISABLE(0xffff));
  3601. POSTING_READ(ECOBUS); /* something from same cacheline, but !FORCEWAKE */
  3602. }
  3603. static void __gen6_gt_force_wake_mt_get(struct drm_i915_private *dev_priv)
  3604. {
  3605. u32 forcewake_ack;
  3606. if (IS_HASWELL(dev_priv->dev))
  3607. forcewake_ack = FORCEWAKE_ACK_HSW;
  3608. else
  3609. forcewake_ack = FORCEWAKE_MT_ACK;
  3610. if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1) == 0,
  3611. FORCEWAKE_ACK_TIMEOUT_MS))
  3612. DRM_ERROR("Timed out waiting for forcewake old ack to clear.\n");
  3613. I915_WRITE_NOTRACE(FORCEWAKE_MT, _MASKED_BIT_ENABLE(FORCEWAKE_KERNEL));
  3614. POSTING_READ(ECOBUS); /* something from same cacheline, but !FORCEWAKE */
  3615. if (wait_for_atomic((I915_READ_NOTRACE(forcewake_ack) & 1),
  3616. FORCEWAKE_ACK_TIMEOUT_MS))
  3617. DRM_ERROR("Timed out waiting for forcewake to ack request.\n");
  3618. __gen6_gt_wait_for_thread_c0(dev_priv);
  3619. }
  3620. /*
  3621. * Generally this is called implicitly by the register read function. However,
  3622. * if some sequence requires the GT to not power down then this function should
  3623. * be called at the beginning of the sequence followed by a call to
  3624. * gen6_gt_force_wake_put() at the end of the sequence.
  3625. */
  3626. void gen6_gt_force_wake_get(struct drm_i915_private *dev_priv)
  3627. {
  3628. unsigned long irqflags;
  3629. spin_lock_irqsave(&dev_priv->gt_lock, irqflags);
  3630. if (dev_priv->forcewake_count++ == 0)
  3631. dev_priv->gt.force_wake_get(dev_priv);
  3632. spin_unlock_irqrestore(&dev_priv->gt_lock, irqflags);
  3633. }
  3634. void gen6_gt_check_fifodbg(struct drm_i915_private *dev_priv)
  3635. {
  3636. u32 gtfifodbg;
  3637. gtfifodbg = I915_READ_NOTRACE(GTFIFODBG);
  3638. if (WARN(gtfifodbg & GT_FIFO_CPU_ERROR_MASK,
  3639. "MMIO read or write has been dropped %x\n", gtfifodbg))
  3640. I915_WRITE_NOTRACE(GTFIFODBG, GT_FIFO_CPU_ERROR_MASK);
  3641. }
  3642. static void __gen6_gt_force_wake_put(struct drm_i915_private *dev_priv)
  3643. {
  3644. I915_WRITE_NOTRACE(FORCEWAKE, 0);
  3645. /* gen6_gt_check_fifodbg doubles as the POSTING_READ */
  3646. gen6_gt_check_fifodbg(dev_priv);
  3647. }
  3648. static void __gen6_gt_force_wake_mt_put(struct drm_i915_private *dev_priv)
  3649. {
  3650. I915_WRITE_NOTRACE(FORCEWAKE_MT, _MASKED_BIT_DISABLE(FORCEWAKE_KERNEL));
  3651. /* gen6_gt_check_fifodbg doubles as the POSTING_READ */
  3652. gen6_gt_check_fifodbg(dev_priv);
  3653. }
  3654. /*
  3655. * see gen6_gt_force_wake_get()
  3656. */
  3657. void gen6_gt_force_wake_put(struct drm_i915_private *dev_priv)
  3658. {
  3659. unsigned long irqflags;
  3660. spin_lock_irqsave(&dev_priv->gt_lock, irqflags);
  3661. if (--dev_priv->forcewake_count == 0)
  3662. dev_priv->gt.force_wake_put(dev_priv);
  3663. spin_unlock_irqrestore(&dev_priv->gt_lock, irqflags);
  3664. }
  3665. int __gen6_gt_wait_for_fifo(struct drm_i915_private *dev_priv)
  3666. {
  3667. int ret = 0;
  3668. if (dev_priv->gt_fifo_count < GT_FIFO_NUM_RESERVED_ENTRIES) {
  3669. int loop = 500;
  3670. u32 fifo = I915_READ_NOTRACE(GT_FIFO_FREE_ENTRIES);
  3671. while (fifo <= GT_FIFO_NUM_RESERVED_ENTRIES && loop--) {
  3672. udelay(10);
  3673. fifo = I915_READ_NOTRACE(GT_FIFO_FREE_ENTRIES);
  3674. }
  3675. if (WARN_ON(loop < 0 && fifo <= GT_FIFO_NUM_RESERVED_ENTRIES))
  3676. ++ret;
  3677. dev_priv->gt_fifo_count = fifo;
  3678. }
  3679. dev_priv->gt_fifo_count--;
  3680. return ret;
  3681. }
  3682. static void vlv_force_wake_reset(struct drm_i915_private *dev_priv)
  3683. {
  3684. I915_WRITE_NOTRACE(FORCEWAKE_VLV, _MASKED_BIT_DISABLE(0xffff));
  3685. }
  3686. static void vlv_force_wake_get(struct drm_i915_private *dev_priv)
  3687. {
  3688. if (wait_for_atomic((I915_READ_NOTRACE(FORCEWAKE_ACK_VLV) & 1) == 0,
  3689. FORCEWAKE_ACK_TIMEOUT_MS))
  3690. DRM_ERROR("Timed out waiting for forcewake old ack to clear.\n");
  3691. I915_WRITE_NOTRACE(FORCEWAKE_VLV, _MASKED_BIT_ENABLE(FORCEWAKE_KERNEL));
  3692. if (wait_for_atomic((I915_READ_NOTRACE(FORCEWAKE_ACK_VLV) & 1),
  3693. FORCEWAKE_ACK_TIMEOUT_MS))
  3694. DRM_ERROR("Timed out waiting for forcewake to ack request.\n");
  3695. __gen6_gt_wait_for_thread_c0(dev_priv);
  3696. }
  3697. static void vlv_force_wake_put(struct drm_i915_private *dev_priv)
  3698. {
  3699. I915_WRITE_NOTRACE(FORCEWAKE_VLV, _MASKED_BIT_DISABLE(FORCEWAKE_KERNEL));
  3700. /* The below doubles as a POSTING_READ */
  3701. gen6_gt_check_fifodbg(dev_priv);
  3702. }
  3703. void intel_gt_reset(struct drm_device *dev)
  3704. {
  3705. struct drm_i915_private *dev_priv = dev->dev_private;
  3706. if (IS_VALLEYVIEW(dev)) {
  3707. vlv_force_wake_reset(dev_priv);
  3708. } else if (INTEL_INFO(dev)->gen >= 6) {
  3709. __gen6_gt_force_wake_reset(dev_priv);
  3710. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3711. __gen6_gt_force_wake_mt_reset(dev_priv);
  3712. }
  3713. }
  3714. void intel_gt_init(struct drm_device *dev)
  3715. {
  3716. struct drm_i915_private *dev_priv = dev->dev_private;
  3717. spin_lock_init(&dev_priv->gt_lock);
  3718. intel_gt_reset(dev);
  3719. if (IS_VALLEYVIEW(dev)) {
  3720. dev_priv->gt.force_wake_get = vlv_force_wake_get;
  3721. dev_priv->gt.force_wake_put = vlv_force_wake_put;
  3722. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  3723. dev_priv->gt.force_wake_get = __gen6_gt_force_wake_mt_get;
  3724. dev_priv->gt.force_wake_put = __gen6_gt_force_wake_mt_put;
  3725. } else if (IS_GEN6(dev)) {
  3726. dev_priv->gt.force_wake_get = __gen6_gt_force_wake_get;
  3727. dev_priv->gt.force_wake_put = __gen6_gt_force_wake_put;
  3728. }
  3729. INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
  3730. intel_gen6_powersave_work);
  3731. }
  3732. int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
  3733. {
  3734. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3735. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  3736. DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
  3737. return -EAGAIN;
  3738. }
  3739. I915_WRITE(GEN6_PCODE_DATA, *val);
  3740. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  3741. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  3742. 500)) {
  3743. DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
  3744. return -ETIMEDOUT;
  3745. }
  3746. *val = I915_READ(GEN6_PCODE_DATA);
  3747. I915_WRITE(GEN6_PCODE_DATA, 0);
  3748. return 0;
  3749. }
  3750. int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
  3751. {
  3752. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3753. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  3754. DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
  3755. return -EAGAIN;
  3756. }
  3757. I915_WRITE(GEN6_PCODE_DATA, val);
  3758. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  3759. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  3760. 500)) {
  3761. DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
  3762. return -ETIMEDOUT;
  3763. }
  3764. I915_WRITE(GEN6_PCODE_DATA, 0);
  3765. return 0;
  3766. }