intel_pm.c 125 KB

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