intel_pm.c 148 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399
  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. #include <drm/i915_powerwell.h>
  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->fbc.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 %c, ",
  101. cfb_pitch, crtc->y, plane_name(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 %c\n", plane_name(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, i915_gem_obj_ggtt_offset(obj) | 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 %c\n", plane_name(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. if (IS_IVYBRIDGE(dev))
  208. /* WaFbcDisableDpfcClockGating:ivb */
  209. I915_WRITE(ILK_DSPCLK_GATE_D,
  210. I915_READ(ILK_DSPCLK_GATE_D) &
  211. ~ILK_DPFCUNIT_CLOCK_GATE_DISABLE);
  212. if (IS_HASWELL(dev))
  213. /* WaFbcDisableDpfcClockGating:hsw */
  214. I915_WRITE(HSW_CLKGATE_DISABLE_PART_1,
  215. I915_READ(HSW_CLKGATE_DISABLE_PART_1) &
  216. ~HSW_DPFC_GATING_DISABLE);
  217. DRM_DEBUG_KMS("disabled FBC\n");
  218. }
  219. }
  220. static bool ironlake_fbc_enabled(struct drm_device *dev)
  221. {
  222. struct drm_i915_private *dev_priv = dev->dev_private;
  223. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  224. }
  225. static void gen7_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  226. {
  227. struct drm_device *dev = crtc->dev;
  228. struct drm_i915_private *dev_priv = dev->dev_private;
  229. struct drm_framebuffer *fb = crtc->fb;
  230. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  231. struct drm_i915_gem_object *obj = intel_fb->obj;
  232. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  233. I915_WRITE(IVB_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj));
  234. I915_WRITE(ILK_DPFC_CONTROL, DPFC_CTL_EN | DPFC_CTL_LIMIT_1X |
  235. IVB_DPFC_CTL_FENCE_EN |
  236. intel_crtc->plane << IVB_DPFC_CTL_PLANE_SHIFT);
  237. if (IS_IVYBRIDGE(dev)) {
  238. /* WaFbcAsynchFlipDisableFbcQueue:ivb */
  239. I915_WRITE(ILK_DISPLAY_CHICKEN1, ILK_FBCQ_DIS);
  240. /* WaFbcDisableDpfcClockGating:ivb */
  241. I915_WRITE(ILK_DSPCLK_GATE_D,
  242. I915_READ(ILK_DSPCLK_GATE_D) |
  243. ILK_DPFCUNIT_CLOCK_GATE_DISABLE);
  244. } else {
  245. /* WaFbcAsynchFlipDisableFbcQueue:hsw */
  246. I915_WRITE(HSW_PIPE_SLICE_CHICKEN_1(intel_crtc->pipe),
  247. HSW_BYPASS_FBC_QUEUE);
  248. /* WaFbcDisableDpfcClockGating:hsw */
  249. I915_WRITE(HSW_CLKGATE_DISABLE_PART_1,
  250. I915_READ(HSW_CLKGATE_DISABLE_PART_1) |
  251. HSW_DPFC_GATING_DISABLE);
  252. }
  253. I915_WRITE(SNB_DPFC_CTL_SA,
  254. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  255. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  256. sandybridge_blit_fbc_update(dev);
  257. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  258. }
  259. bool intel_fbc_enabled(struct drm_device *dev)
  260. {
  261. struct drm_i915_private *dev_priv = dev->dev_private;
  262. if (!dev_priv->display.fbc_enabled)
  263. return false;
  264. return dev_priv->display.fbc_enabled(dev);
  265. }
  266. static void intel_fbc_work_fn(struct work_struct *__work)
  267. {
  268. struct intel_fbc_work *work =
  269. container_of(to_delayed_work(__work),
  270. struct intel_fbc_work, work);
  271. struct drm_device *dev = work->crtc->dev;
  272. struct drm_i915_private *dev_priv = dev->dev_private;
  273. mutex_lock(&dev->struct_mutex);
  274. if (work == dev_priv->fbc.fbc_work) {
  275. /* Double check that we haven't switched fb without cancelling
  276. * the prior work.
  277. */
  278. if (work->crtc->fb == work->fb) {
  279. dev_priv->display.enable_fbc(work->crtc,
  280. work->interval);
  281. dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
  282. dev_priv->fbc.fb_id = work->crtc->fb->base.id;
  283. dev_priv->fbc.y = work->crtc->y;
  284. }
  285. dev_priv->fbc.fbc_work = NULL;
  286. }
  287. mutex_unlock(&dev->struct_mutex);
  288. kfree(work);
  289. }
  290. static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
  291. {
  292. if (dev_priv->fbc.fbc_work == NULL)
  293. return;
  294. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  295. /* Synchronisation is provided by struct_mutex and checking of
  296. * dev_priv->fbc.fbc_work, so we can perform the cancellation
  297. * entirely asynchronously.
  298. */
  299. if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
  300. /* tasklet was killed before being run, clean up */
  301. kfree(dev_priv->fbc.fbc_work);
  302. /* Mark the work as no longer wanted so that if it does
  303. * wake-up (because the work was already running and waiting
  304. * for our mutex), it will discover that is no longer
  305. * necessary to run.
  306. */
  307. dev_priv->fbc.fbc_work = NULL;
  308. }
  309. static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  310. {
  311. struct intel_fbc_work *work;
  312. struct drm_device *dev = crtc->dev;
  313. struct drm_i915_private *dev_priv = dev->dev_private;
  314. if (!dev_priv->display.enable_fbc)
  315. return;
  316. intel_cancel_fbc_work(dev_priv);
  317. work = kzalloc(sizeof *work, GFP_KERNEL);
  318. if (work == NULL) {
  319. DRM_ERROR("Failed to allocate FBC work structure\n");
  320. dev_priv->display.enable_fbc(crtc, interval);
  321. return;
  322. }
  323. work->crtc = crtc;
  324. work->fb = crtc->fb;
  325. work->interval = interval;
  326. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  327. dev_priv->fbc.fbc_work = work;
  328. /* Delay the actual enabling to let pageflipping cease and the
  329. * display to settle before starting the compression. Note that
  330. * this delay also serves a second purpose: it allows for a
  331. * vblank to pass after disabling the FBC before we attempt
  332. * to modify the control registers.
  333. *
  334. * A more complicated solution would involve tracking vblanks
  335. * following the termination of the page-flipping sequence
  336. * and indeed performing the enable as a co-routine and not
  337. * waiting synchronously upon the vblank.
  338. *
  339. * WaFbcWaitForVBlankBeforeEnable:ilk,snb
  340. */
  341. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  342. }
  343. void intel_disable_fbc(struct drm_device *dev)
  344. {
  345. struct drm_i915_private *dev_priv = dev->dev_private;
  346. intel_cancel_fbc_work(dev_priv);
  347. if (!dev_priv->display.disable_fbc)
  348. return;
  349. dev_priv->display.disable_fbc(dev);
  350. dev_priv->fbc.plane = -1;
  351. }
  352. /**
  353. * intel_update_fbc - enable/disable FBC as needed
  354. * @dev: the drm_device
  355. *
  356. * Set up the framebuffer compression hardware at mode set time. We
  357. * enable it if possible:
  358. * - plane A only (on pre-965)
  359. * - no pixel mulitply/line duplication
  360. * - no alpha buffer discard
  361. * - no dual wide
  362. * - framebuffer <= max_hdisplay in width, max_vdisplay in height
  363. *
  364. * We can't assume that any compression will take place (worst case),
  365. * so the compressed buffer has to be the same size as the uncompressed
  366. * one. It also must reside (along with the line length buffer) in
  367. * stolen memory.
  368. *
  369. * We need to enable/disable FBC on a global basis.
  370. */
  371. void intel_update_fbc(struct drm_device *dev)
  372. {
  373. struct drm_i915_private *dev_priv = dev->dev_private;
  374. struct drm_crtc *crtc = NULL, *tmp_crtc;
  375. struct intel_crtc *intel_crtc;
  376. struct drm_framebuffer *fb;
  377. struct intel_framebuffer *intel_fb;
  378. struct drm_i915_gem_object *obj;
  379. unsigned int max_hdisplay, max_vdisplay;
  380. if (!i915_powersave)
  381. return;
  382. if (!I915_HAS_FBC(dev))
  383. return;
  384. /*
  385. * If FBC is already on, we just have to verify that we can
  386. * keep it that way...
  387. * Need to disable if:
  388. * - more than one pipe is active
  389. * - changing FBC params (stride, fence, mode)
  390. * - new fb is too large to fit in compressed buffer
  391. * - going to an unsupported config (interlace, pixel multiply, etc.)
  392. */
  393. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  394. if (intel_crtc_active(tmp_crtc) &&
  395. !to_intel_crtc(tmp_crtc)->primary_disabled) {
  396. if (crtc) {
  397. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  398. dev_priv->fbc.no_fbc_reason =
  399. FBC_MULTIPLE_PIPES;
  400. goto out_disable;
  401. }
  402. crtc = tmp_crtc;
  403. }
  404. }
  405. if (!crtc || crtc->fb == NULL) {
  406. DRM_DEBUG_KMS("no output, disabling\n");
  407. dev_priv->fbc.no_fbc_reason = FBC_NO_OUTPUT;
  408. goto out_disable;
  409. }
  410. intel_crtc = to_intel_crtc(crtc);
  411. fb = crtc->fb;
  412. intel_fb = to_intel_framebuffer(fb);
  413. obj = intel_fb->obj;
  414. if (i915_enable_fbc < 0 &&
  415. INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev)) {
  416. DRM_DEBUG_KMS("disabled per chip default\n");
  417. dev_priv->fbc.no_fbc_reason = FBC_CHIP_DEFAULT;
  418. goto out_disable;
  419. }
  420. if (!i915_enable_fbc) {
  421. DRM_DEBUG_KMS("fbc disabled per module param\n");
  422. dev_priv->fbc.no_fbc_reason = FBC_MODULE_PARAM;
  423. goto out_disable;
  424. }
  425. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  426. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  427. DRM_DEBUG_KMS("mode incompatible with compression, "
  428. "disabling\n");
  429. dev_priv->fbc.no_fbc_reason = FBC_UNSUPPORTED_MODE;
  430. goto out_disable;
  431. }
  432. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  433. max_hdisplay = 4096;
  434. max_vdisplay = 2048;
  435. } else {
  436. max_hdisplay = 2048;
  437. max_vdisplay = 1536;
  438. }
  439. if ((crtc->mode.hdisplay > max_hdisplay) ||
  440. (crtc->mode.vdisplay > max_vdisplay)) {
  441. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  442. dev_priv->fbc.no_fbc_reason = FBC_MODE_TOO_LARGE;
  443. goto out_disable;
  444. }
  445. if ((IS_I915GM(dev) || IS_I945GM(dev) || IS_HASWELL(dev)) &&
  446. intel_crtc->plane != 0) {
  447. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  448. dev_priv->fbc.no_fbc_reason = FBC_BAD_PLANE;
  449. goto out_disable;
  450. }
  451. /* The use of a CPU fence is mandatory in order to detect writes
  452. * by the CPU to the scanout and trigger updates to the FBC.
  453. */
  454. if (obj->tiling_mode != I915_TILING_X ||
  455. obj->fence_reg == I915_FENCE_REG_NONE) {
  456. DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
  457. dev_priv->fbc.no_fbc_reason = FBC_NOT_TILED;
  458. goto out_disable;
  459. }
  460. /* If the kernel debugger is active, always disable compression */
  461. if (in_dbg_master())
  462. goto out_disable;
  463. if (i915_gem_stolen_setup_compression(dev, intel_fb->obj->base.size)) {
  464. DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
  465. dev_priv->fbc.no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  466. goto out_disable;
  467. }
  468. /* If the scanout has not changed, don't modify the FBC settings.
  469. * Note that we make the fundamental assumption that the fb->obj
  470. * cannot be unpinned (and have its GTT offset and fence revoked)
  471. * without first being decoupled from the scanout and FBC disabled.
  472. */
  473. if (dev_priv->fbc.plane == intel_crtc->plane &&
  474. dev_priv->fbc.fb_id == fb->base.id &&
  475. dev_priv->fbc.y == crtc->y)
  476. return;
  477. if (intel_fbc_enabled(dev)) {
  478. /* We update FBC along two paths, after changing fb/crtc
  479. * configuration (modeswitching) and after page-flipping
  480. * finishes. For the latter, we know that not only did
  481. * we disable the FBC at the start of the page-flip
  482. * sequence, but also more than one vblank has passed.
  483. *
  484. * For the former case of modeswitching, it is possible
  485. * to switch between two FBC valid configurations
  486. * instantaneously so we do need to disable the FBC
  487. * before we can modify its control registers. We also
  488. * have to wait for the next vblank for that to take
  489. * effect. However, since we delay enabling FBC we can
  490. * assume that a vblank has passed since disabling and
  491. * that we can safely alter the registers in the deferred
  492. * callback.
  493. *
  494. * In the scenario that we go from a valid to invalid
  495. * and then back to valid FBC configuration we have
  496. * no strict enforcement that a vblank occurred since
  497. * disabling the FBC. However, along all current pipe
  498. * disabling paths we do need to wait for a vblank at
  499. * some point. And we wait before enabling FBC anyway.
  500. */
  501. DRM_DEBUG_KMS("disabling active FBC for update\n");
  502. intel_disable_fbc(dev);
  503. }
  504. intel_enable_fbc(crtc, 500);
  505. return;
  506. out_disable:
  507. /* Multiple disables should be harmless */
  508. if (intel_fbc_enabled(dev)) {
  509. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  510. intel_disable_fbc(dev);
  511. }
  512. i915_gem_stolen_cleanup_compression(dev);
  513. }
  514. static void i915_pineview_get_mem_freq(struct drm_device *dev)
  515. {
  516. drm_i915_private_t *dev_priv = dev->dev_private;
  517. u32 tmp;
  518. tmp = I915_READ(CLKCFG);
  519. switch (tmp & CLKCFG_FSB_MASK) {
  520. case CLKCFG_FSB_533:
  521. dev_priv->fsb_freq = 533; /* 133*4 */
  522. break;
  523. case CLKCFG_FSB_800:
  524. dev_priv->fsb_freq = 800; /* 200*4 */
  525. break;
  526. case CLKCFG_FSB_667:
  527. dev_priv->fsb_freq = 667; /* 167*4 */
  528. break;
  529. case CLKCFG_FSB_400:
  530. dev_priv->fsb_freq = 400; /* 100*4 */
  531. break;
  532. }
  533. switch (tmp & CLKCFG_MEM_MASK) {
  534. case CLKCFG_MEM_533:
  535. dev_priv->mem_freq = 533;
  536. break;
  537. case CLKCFG_MEM_667:
  538. dev_priv->mem_freq = 667;
  539. break;
  540. case CLKCFG_MEM_800:
  541. dev_priv->mem_freq = 800;
  542. break;
  543. }
  544. /* detect pineview DDR3 setting */
  545. tmp = I915_READ(CSHRDDR3CTL);
  546. dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
  547. }
  548. static void i915_ironlake_get_mem_freq(struct drm_device *dev)
  549. {
  550. drm_i915_private_t *dev_priv = dev->dev_private;
  551. u16 ddrpll, csipll;
  552. ddrpll = I915_READ16(DDRMPLL1);
  553. csipll = I915_READ16(CSIPLL0);
  554. switch (ddrpll & 0xff) {
  555. case 0xc:
  556. dev_priv->mem_freq = 800;
  557. break;
  558. case 0x10:
  559. dev_priv->mem_freq = 1066;
  560. break;
  561. case 0x14:
  562. dev_priv->mem_freq = 1333;
  563. break;
  564. case 0x18:
  565. dev_priv->mem_freq = 1600;
  566. break;
  567. default:
  568. DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
  569. ddrpll & 0xff);
  570. dev_priv->mem_freq = 0;
  571. break;
  572. }
  573. dev_priv->ips.r_t = dev_priv->mem_freq;
  574. switch (csipll & 0x3ff) {
  575. case 0x00c:
  576. dev_priv->fsb_freq = 3200;
  577. break;
  578. case 0x00e:
  579. dev_priv->fsb_freq = 3733;
  580. break;
  581. case 0x010:
  582. dev_priv->fsb_freq = 4266;
  583. break;
  584. case 0x012:
  585. dev_priv->fsb_freq = 4800;
  586. break;
  587. case 0x014:
  588. dev_priv->fsb_freq = 5333;
  589. break;
  590. case 0x016:
  591. dev_priv->fsb_freq = 5866;
  592. break;
  593. case 0x018:
  594. dev_priv->fsb_freq = 6400;
  595. break;
  596. default:
  597. DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
  598. csipll & 0x3ff);
  599. dev_priv->fsb_freq = 0;
  600. break;
  601. }
  602. if (dev_priv->fsb_freq == 3200) {
  603. dev_priv->ips.c_m = 0;
  604. } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
  605. dev_priv->ips.c_m = 1;
  606. } else {
  607. dev_priv->ips.c_m = 2;
  608. }
  609. }
  610. static const struct cxsr_latency cxsr_latency_table[] = {
  611. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  612. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  613. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  614. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  615. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  616. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  617. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  618. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  619. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  620. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  621. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  622. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  623. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  624. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  625. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  626. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  627. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  628. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  629. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  630. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  631. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  632. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  633. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  634. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  635. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  636. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  637. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  638. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  639. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  640. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  641. };
  642. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  643. int is_ddr3,
  644. int fsb,
  645. int mem)
  646. {
  647. const struct cxsr_latency *latency;
  648. int i;
  649. if (fsb == 0 || mem == 0)
  650. return NULL;
  651. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  652. latency = &cxsr_latency_table[i];
  653. if (is_desktop == latency->is_desktop &&
  654. is_ddr3 == latency->is_ddr3 &&
  655. fsb == latency->fsb_freq && mem == latency->mem_freq)
  656. return latency;
  657. }
  658. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  659. return NULL;
  660. }
  661. static void pineview_disable_cxsr(struct drm_device *dev)
  662. {
  663. struct drm_i915_private *dev_priv = dev->dev_private;
  664. /* deactivate cxsr */
  665. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  666. }
  667. /*
  668. * Latency for FIFO fetches is dependent on several factors:
  669. * - memory configuration (speed, channels)
  670. * - chipset
  671. * - current MCH state
  672. * It can be fairly high in some situations, so here we assume a fairly
  673. * pessimal value. It's a tradeoff between extra memory fetches (if we
  674. * set this value too high, the FIFO will fetch frequently to stay full)
  675. * and power consumption (set it too low to save power and we might see
  676. * FIFO underruns and display "flicker").
  677. *
  678. * A value of 5us seems to be a good balance; safe for very low end
  679. * platforms but not overly aggressive on lower latency configs.
  680. */
  681. static const int latency_ns = 5000;
  682. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  683. {
  684. struct drm_i915_private *dev_priv = dev->dev_private;
  685. uint32_t dsparb = I915_READ(DSPARB);
  686. int size;
  687. size = dsparb & 0x7f;
  688. if (plane)
  689. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  690. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  691. plane ? "B" : "A", size);
  692. return size;
  693. }
  694. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  695. {
  696. struct drm_i915_private *dev_priv = dev->dev_private;
  697. uint32_t dsparb = I915_READ(DSPARB);
  698. int size;
  699. size = dsparb & 0x1ff;
  700. if (plane)
  701. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  702. size >>= 1; /* Convert to cachelines */
  703. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  704. plane ? "B" : "A", size);
  705. return size;
  706. }
  707. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  708. {
  709. struct drm_i915_private *dev_priv = dev->dev_private;
  710. uint32_t dsparb = I915_READ(DSPARB);
  711. int size;
  712. size = dsparb & 0x7f;
  713. size >>= 2; /* Convert to cachelines */
  714. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  715. plane ? "B" : "A",
  716. size);
  717. return size;
  718. }
  719. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  720. {
  721. struct drm_i915_private *dev_priv = dev->dev_private;
  722. uint32_t dsparb = I915_READ(DSPARB);
  723. int size;
  724. size = dsparb & 0x7f;
  725. size >>= 1; /* Convert to cachelines */
  726. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  727. plane ? "B" : "A", size);
  728. return size;
  729. }
  730. /* Pineview has different values for various configs */
  731. static const struct intel_watermark_params pineview_display_wm = {
  732. PINEVIEW_DISPLAY_FIFO,
  733. PINEVIEW_MAX_WM,
  734. PINEVIEW_DFT_WM,
  735. PINEVIEW_GUARD_WM,
  736. PINEVIEW_FIFO_LINE_SIZE
  737. };
  738. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  739. PINEVIEW_DISPLAY_FIFO,
  740. PINEVIEW_MAX_WM,
  741. PINEVIEW_DFT_HPLLOFF_WM,
  742. PINEVIEW_GUARD_WM,
  743. PINEVIEW_FIFO_LINE_SIZE
  744. };
  745. static const struct intel_watermark_params pineview_cursor_wm = {
  746. PINEVIEW_CURSOR_FIFO,
  747. PINEVIEW_CURSOR_MAX_WM,
  748. PINEVIEW_CURSOR_DFT_WM,
  749. PINEVIEW_CURSOR_GUARD_WM,
  750. PINEVIEW_FIFO_LINE_SIZE,
  751. };
  752. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  753. PINEVIEW_CURSOR_FIFO,
  754. PINEVIEW_CURSOR_MAX_WM,
  755. PINEVIEW_CURSOR_DFT_WM,
  756. PINEVIEW_CURSOR_GUARD_WM,
  757. PINEVIEW_FIFO_LINE_SIZE
  758. };
  759. static const struct intel_watermark_params g4x_wm_info = {
  760. G4X_FIFO_SIZE,
  761. G4X_MAX_WM,
  762. G4X_MAX_WM,
  763. 2,
  764. G4X_FIFO_LINE_SIZE,
  765. };
  766. static const struct intel_watermark_params g4x_cursor_wm_info = {
  767. I965_CURSOR_FIFO,
  768. I965_CURSOR_MAX_WM,
  769. I965_CURSOR_DFT_WM,
  770. 2,
  771. G4X_FIFO_LINE_SIZE,
  772. };
  773. static const struct intel_watermark_params valleyview_wm_info = {
  774. VALLEYVIEW_FIFO_SIZE,
  775. VALLEYVIEW_MAX_WM,
  776. VALLEYVIEW_MAX_WM,
  777. 2,
  778. G4X_FIFO_LINE_SIZE,
  779. };
  780. static const struct intel_watermark_params valleyview_cursor_wm_info = {
  781. I965_CURSOR_FIFO,
  782. VALLEYVIEW_CURSOR_MAX_WM,
  783. I965_CURSOR_DFT_WM,
  784. 2,
  785. G4X_FIFO_LINE_SIZE,
  786. };
  787. static const struct intel_watermark_params i965_cursor_wm_info = {
  788. I965_CURSOR_FIFO,
  789. I965_CURSOR_MAX_WM,
  790. I965_CURSOR_DFT_WM,
  791. 2,
  792. I915_FIFO_LINE_SIZE,
  793. };
  794. static const struct intel_watermark_params i945_wm_info = {
  795. I945_FIFO_SIZE,
  796. I915_MAX_WM,
  797. 1,
  798. 2,
  799. I915_FIFO_LINE_SIZE
  800. };
  801. static const struct intel_watermark_params i915_wm_info = {
  802. I915_FIFO_SIZE,
  803. I915_MAX_WM,
  804. 1,
  805. 2,
  806. I915_FIFO_LINE_SIZE
  807. };
  808. static const struct intel_watermark_params i855_wm_info = {
  809. I855GM_FIFO_SIZE,
  810. I915_MAX_WM,
  811. 1,
  812. 2,
  813. I830_FIFO_LINE_SIZE
  814. };
  815. static const struct intel_watermark_params i830_wm_info = {
  816. I830_FIFO_SIZE,
  817. I915_MAX_WM,
  818. 1,
  819. 2,
  820. I830_FIFO_LINE_SIZE
  821. };
  822. static const struct intel_watermark_params ironlake_display_wm_info = {
  823. ILK_DISPLAY_FIFO,
  824. ILK_DISPLAY_MAXWM,
  825. ILK_DISPLAY_DFTWM,
  826. 2,
  827. ILK_FIFO_LINE_SIZE
  828. };
  829. static const struct intel_watermark_params ironlake_cursor_wm_info = {
  830. ILK_CURSOR_FIFO,
  831. ILK_CURSOR_MAXWM,
  832. ILK_CURSOR_DFTWM,
  833. 2,
  834. ILK_FIFO_LINE_SIZE
  835. };
  836. static const struct intel_watermark_params ironlake_display_srwm_info = {
  837. ILK_DISPLAY_SR_FIFO,
  838. ILK_DISPLAY_MAX_SRWM,
  839. ILK_DISPLAY_DFT_SRWM,
  840. 2,
  841. ILK_FIFO_LINE_SIZE
  842. };
  843. static const struct intel_watermark_params ironlake_cursor_srwm_info = {
  844. ILK_CURSOR_SR_FIFO,
  845. ILK_CURSOR_MAX_SRWM,
  846. ILK_CURSOR_DFT_SRWM,
  847. 2,
  848. ILK_FIFO_LINE_SIZE
  849. };
  850. static const struct intel_watermark_params sandybridge_display_wm_info = {
  851. SNB_DISPLAY_FIFO,
  852. SNB_DISPLAY_MAXWM,
  853. SNB_DISPLAY_DFTWM,
  854. 2,
  855. SNB_FIFO_LINE_SIZE
  856. };
  857. static const struct intel_watermark_params sandybridge_cursor_wm_info = {
  858. SNB_CURSOR_FIFO,
  859. SNB_CURSOR_MAXWM,
  860. SNB_CURSOR_DFTWM,
  861. 2,
  862. SNB_FIFO_LINE_SIZE
  863. };
  864. static const struct intel_watermark_params sandybridge_display_srwm_info = {
  865. SNB_DISPLAY_SR_FIFO,
  866. SNB_DISPLAY_MAX_SRWM,
  867. SNB_DISPLAY_DFT_SRWM,
  868. 2,
  869. SNB_FIFO_LINE_SIZE
  870. };
  871. static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
  872. SNB_CURSOR_SR_FIFO,
  873. SNB_CURSOR_MAX_SRWM,
  874. SNB_CURSOR_DFT_SRWM,
  875. 2,
  876. SNB_FIFO_LINE_SIZE
  877. };
  878. /**
  879. * intel_calculate_wm - calculate watermark level
  880. * @clock_in_khz: pixel clock
  881. * @wm: chip FIFO params
  882. * @pixel_size: display pixel size
  883. * @latency_ns: memory latency for the platform
  884. *
  885. * Calculate the watermark level (the level at which the display plane will
  886. * start fetching from memory again). Each chip has a different display
  887. * FIFO size and allocation, so the caller needs to figure that out and pass
  888. * in the correct intel_watermark_params structure.
  889. *
  890. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  891. * on the pixel size. When it reaches the watermark level, it'll start
  892. * fetching FIFO line sized based chunks from memory until the FIFO fills
  893. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  894. * will occur, and a display engine hang could result.
  895. */
  896. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  897. const struct intel_watermark_params *wm,
  898. int fifo_size,
  899. int pixel_size,
  900. unsigned long latency_ns)
  901. {
  902. long entries_required, wm_size;
  903. /*
  904. * Note: we need to make sure we don't overflow for various clock &
  905. * latency values.
  906. * clocks go from a few thousand to several hundred thousand.
  907. * latency is usually a few thousand
  908. */
  909. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  910. 1000;
  911. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  912. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  913. wm_size = fifo_size - (entries_required + wm->guard_size);
  914. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  915. /* Don't promote wm_size to unsigned... */
  916. if (wm_size > (long)wm->max_wm)
  917. wm_size = wm->max_wm;
  918. if (wm_size <= 0)
  919. wm_size = wm->default_wm;
  920. return wm_size;
  921. }
  922. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  923. {
  924. struct drm_crtc *crtc, *enabled = NULL;
  925. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  926. if (intel_crtc_active(crtc)) {
  927. if (enabled)
  928. return NULL;
  929. enabled = crtc;
  930. }
  931. }
  932. return enabled;
  933. }
  934. static void pineview_update_wm(struct drm_device *dev)
  935. {
  936. struct drm_i915_private *dev_priv = dev->dev_private;
  937. struct drm_crtc *crtc;
  938. const struct cxsr_latency *latency;
  939. u32 reg;
  940. unsigned long wm;
  941. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  942. dev_priv->fsb_freq, dev_priv->mem_freq);
  943. if (!latency) {
  944. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  945. pineview_disable_cxsr(dev);
  946. return;
  947. }
  948. crtc = single_enabled_crtc(dev);
  949. if (crtc) {
  950. int clock = crtc->mode.clock;
  951. int pixel_size = crtc->fb->bits_per_pixel / 8;
  952. /* Display SR */
  953. wm = intel_calculate_wm(clock, &pineview_display_wm,
  954. pineview_display_wm.fifo_size,
  955. pixel_size, latency->display_sr);
  956. reg = I915_READ(DSPFW1);
  957. reg &= ~DSPFW_SR_MASK;
  958. reg |= wm << DSPFW_SR_SHIFT;
  959. I915_WRITE(DSPFW1, reg);
  960. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  961. /* cursor SR */
  962. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  963. pineview_display_wm.fifo_size,
  964. pixel_size, latency->cursor_sr);
  965. reg = I915_READ(DSPFW3);
  966. reg &= ~DSPFW_CURSOR_SR_MASK;
  967. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  968. I915_WRITE(DSPFW3, reg);
  969. /* Display HPLL off SR */
  970. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  971. pineview_display_hplloff_wm.fifo_size,
  972. pixel_size, latency->display_hpll_disable);
  973. reg = I915_READ(DSPFW3);
  974. reg &= ~DSPFW_HPLL_SR_MASK;
  975. reg |= wm & DSPFW_HPLL_SR_MASK;
  976. I915_WRITE(DSPFW3, reg);
  977. /* cursor HPLL off SR */
  978. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  979. pineview_display_hplloff_wm.fifo_size,
  980. pixel_size, latency->cursor_hpll_disable);
  981. reg = I915_READ(DSPFW3);
  982. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  983. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  984. I915_WRITE(DSPFW3, reg);
  985. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  986. /* activate cxsr */
  987. I915_WRITE(DSPFW3,
  988. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  989. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  990. } else {
  991. pineview_disable_cxsr(dev);
  992. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  993. }
  994. }
  995. static bool g4x_compute_wm0(struct drm_device *dev,
  996. int plane,
  997. const struct intel_watermark_params *display,
  998. int display_latency_ns,
  999. const struct intel_watermark_params *cursor,
  1000. int cursor_latency_ns,
  1001. int *plane_wm,
  1002. int *cursor_wm)
  1003. {
  1004. struct drm_crtc *crtc;
  1005. int htotal, hdisplay, clock, pixel_size;
  1006. int line_time_us, line_count;
  1007. int entries, tlb_miss;
  1008. crtc = intel_get_crtc_for_plane(dev, plane);
  1009. if (!intel_crtc_active(crtc)) {
  1010. *cursor_wm = cursor->guard_size;
  1011. *plane_wm = display->guard_size;
  1012. return false;
  1013. }
  1014. htotal = crtc->mode.htotal;
  1015. hdisplay = crtc->mode.hdisplay;
  1016. clock = crtc->mode.clock;
  1017. pixel_size = crtc->fb->bits_per_pixel / 8;
  1018. /* Use the small buffer method to calculate plane watermark */
  1019. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  1020. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  1021. if (tlb_miss > 0)
  1022. entries += tlb_miss;
  1023. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  1024. *plane_wm = entries + display->guard_size;
  1025. if (*plane_wm > (int)display->max_wm)
  1026. *plane_wm = display->max_wm;
  1027. /* Use the large buffer method to calculate cursor watermark */
  1028. line_time_us = ((htotal * 1000) / clock);
  1029. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  1030. entries = line_count * 64 * pixel_size;
  1031. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  1032. if (tlb_miss > 0)
  1033. entries += tlb_miss;
  1034. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1035. *cursor_wm = entries + cursor->guard_size;
  1036. if (*cursor_wm > (int)cursor->max_wm)
  1037. *cursor_wm = (int)cursor->max_wm;
  1038. return true;
  1039. }
  1040. /*
  1041. * Check the wm result.
  1042. *
  1043. * If any calculated watermark values is larger than the maximum value that
  1044. * can be programmed into the associated watermark register, that watermark
  1045. * must be disabled.
  1046. */
  1047. static bool g4x_check_srwm(struct drm_device *dev,
  1048. int display_wm, int cursor_wm,
  1049. const struct intel_watermark_params *display,
  1050. const struct intel_watermark_params *cursor)
  1051. {
  1052. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  1053. display_wm, cursor_wm);
  1054. if (display_wm > display->max_wm) {
  1055. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  1056. display_wm, display->max_wm);
  1057. return false;
  1058. }
  1059. if (cursor_wm > cursor->max_wm) {
  1060. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  1061. cursor_wm, cursor->max_wm);
  1062. return false;
  1063. }
  1064. if (!(display_wm || cursor_wm)) {
  1065. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  1066. return false;
  1067. }
  1068. return true;
  1069. }
  1070. static bool g4x_compute_srwm(struct drm_device *dev,
  1071. int plane,
  1072. int latency_ns,
  1073. const struct intel_watermark_params *display,
  1074. const struct intel_watermark_params *cursor,
  1075. int *display_wm, int *cursor_wm)
  1076. {
  1077. struct drm_crtc *crtc;
  1078. int hdisplay, htotal, pixel_size, clock;
  1079. unsigned long line_time_us;
  1080. int line_count, line_size;
  1081. int small, large;
  1082. int entries;
  1083. if (!latency_ns) {
  1084. *display_wm = *cursor_wm = 0;
  1085. return false;
  1086. }
  1087. crtc = intel_get_crtc_for_plane(dev, plane);
  1088. hdisplay = crtc->mode.hdisplay;
  1089. htotal = crtc->mode.htotal;
  1090. clock = crtc->mode.clock;
  1091. pixel_size = crtc->fb->bits_per_pixel / 8;
  1092. line_time_us = (htotal * 1000) / clock;
  1093. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1094. line_size = hdisplay * pixel_size;
  1095. /* Use the minimum of the small and large buffer method for primary */
  1096. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1097. large = line_count * line_size;
  1098. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1099. *display_wm = entries + display->guard_size;
  1100. /* calculate the self-refresh watermark for display cursor */
  1101. entries = line_count * pixel_size * 64;
  1102. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1103. *cursor_wm = entries + cursor->guard_size;
  1104. return g4x_check_srwm(dev,
  1105. *display_wm, *cursor_wm,
  1106. display, cursor);
  1107. }
  1108. static bool vlv_compute_drain_latency(struct drm_device *dev,
  1109. int plane,
  1110. int *plane_prec_mult,
  1111. int *plane_dl,
  1112. int *cursor_prec_mult,
  1113. int *cursor_dl)
  1114. {
  1115. struct drm_crtc *crtc;
  1116. int clock, pixel_size;
  1117. int entries;
  1118. crtc = intel_get_crtc_for_plane(dev, plane);
  1119. if (!intel_crtc_active(crtc))
  1120. return false;
  1121. clock = crtc->mode.clock; /* VESA DOT Clock */
  1122. pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
  1123. entries = (clock / 1000) * pixel_size;
  1124. *plane_prec_mult = (entries > 256) ?
  1125. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  1126. *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
  1127. pixel_size);
  1128. entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
  1129. *cursor_prec_mult = (entries > 256) ?
  1130. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  1131. *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
  1132. return true;
  1133. }
  1134. /*
  1135. * Update drain latency registers of memory arbiter
  1136. *
  1137. * Valleyview SoC has a new memory arbiter and needs drain latency registers
  1138. * to be programmed. Each plane has a drain latency multiplier and a drain
  1139. * latency value.
  1140. */
  1141. static void vlv_update_drain_latency(struct drm_device *dev)
  1142. {
  1143. struct drm_i915_private *dev_priv = dev->dev_private;
  1144. int planea_prec, planea_dl, planeb_prec, planeb_dl;
  1145. int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
  1146. int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
  1147. either 16 or 32 */
  1148. /* For plane A, Cursor A */
  1149. if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
  1150. &cursor_prec_mult, &cursora_dl)) {
  1151. cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1152. DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
  1153. planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1154. DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
  1155. I915_WRITE(VLV_DDL1, cursora_prec |
  1156. (cursora_dl << DDL_CURSORA_SHIFT) |
  1157. planea_prec | planea_dl);
  1158. }
  1159. /* For plane B, Cursor B */
  1160. if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
  1161. &cursor_prec_mult, &cursorb_dl)) {
  1162. cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1163. DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
  1164. planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1165. DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
  1166. I915_WRITE(VLV_DDL2, cursorb_prec |
  1167. (cursorb_dl << DDL_CURSORB_SHIFT) |
  1168. planeb_prec | planeb_dl);
  1169. }
  1170. }
  1171. #define single_plane_enabled(mask) is_power_of_2(mask)
  1172. static void valleyview_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. int ignore_plane_sr, ignore_cursor_sr;
  1179. unsigned int enabled = 0;
  1180. vlv_update_drain_latency(dev);
  1181. if (g4x_compute_wm0(dev, PIPE_A,
  1182. &valleyview_wm_info, latency_ns,
  1183. &valleyview_cursor_wm_info, latency_ns,
  1184. &planea_wm, &cursora_wm))
  1185. enabled |= 1 << PIPE_A;
  1186. if (g4x_compute_wm0(dev, PIPE_B,
  1187. &valleyview_wm_info, latency_ns,
  1188. &valleyview_cursor_wm_info, latency_ns,
  1189. &planeb_wm, &cursorb_wm))
  1190. enabled |= 1 << PIPE_B;
  1191. if (single_plane_enabled(enabled) &&
  1192. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1193. sr_latency_ns,
  1194. &valleyview_wm_info,
  1195. &valleyview_cursor_wm_info,
  1196. &plane_sr, &ignore_cursor_sr) &&
  1197. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1198. 2*sr_latency_ns,
  1199. &valleyview_wm_info,
  1200. &valleyview_cursor_wm_info,
  1201. &ignore_plane_sr, &cursor_sr)) {
  1202. I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
  1203. } else {
  1204. I915_WRITE(FW_BLC_SELF_VLV,
  1205. I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
  1206. plane_sr = cursor_sr = 0;
  1207. }
  1208. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1209. planea_wm, cursora_wm,
  1210. planeb_wm, cursorb_wm,
  1211. plane_sr, cursor_sr);
  1212. I915_WRITE(DSPFW1,
  1213. (plane_sr << DSPFW_SR_SHIFT) |
  1214. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1215. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1216. planea_wm);
  1217. I915_WRITE(DSPFW2,
  1218. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  1219. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1220. I915_WRITE(DSPFW3,
  1221. (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
  1222. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1223. }
  1224. static void g4x_update_wm(struct drm_device *dev)
  1225. {
  1226. static const int sr_latency_ns = 12000;
  1227. struct drm_i915_private *dev_priv = dev->dev_private;
  1228. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1229. int plane_sr, cursor_sr;
  1230. unsigned int enabled = 0;
  1231. if (g4x_compute_wm0(dev, PIPE_A,
  1232. &g4x_wm_info, latency_ns,
  1233. &g4x_cursor_wm_info, latency_ns,
  1234. &planea_wm, &cursora_wm))
  1235. enabled |= 1 << PIPE_A;
  1236. if (g4x_compute_wm0(dev, PIPE_B,
  1237. &g4x_wm_info, latency_ns,
  1238. &g4x_cursor_wm_info, latency_ns,
  1239. &planeb_wm, &cursorb_wm))
  1240. enabled |= 1 << PIPE_B;
  1241. if (single_plane_enabled(enabled) &&
  1242. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1243. sr_latency_ns,
  1244. &g4x_wm_info,
  1245. &g4x_cursor_wm_info,
  1246. &plane_sr, &cursor_sr)) {
  1247. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1248. } else {
  1249. I915_WRITE(FW_BLC_SELF,
  1250. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  1251. plane_sr = cursor_sr = 0;
  1252. }
  1253. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1254. planea_wm, cursora_wm,
  1255. planeb_wm, cursorb_wm,
  1256. plane_sr, cursor_sr);
  1257. I915_WRITE(DSPFW1,
  1258. (plane_sr << DSPFW_SR_SHIFT) |
  1259. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1260. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1261. planea_wm);
  1262. I915_WRITE(DSPFW2,
  1263. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  1264. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1265. /* HPLL off in SR has some issues on G4x... disable it */
  1266. I915_WRITE(DSPFW3,
  1267. (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
  1268. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1269. }
  1270. static void i965_update_wm(struct drm_device *dev)
  1271. {
  1272. struct drm_i915_private *dev_priv = dev->dev_private;
  1273. struct drm_crtc *crtc;
  1274. int srwm = 1;
  1275. int cursor_sr = 16;
  1276. /* Calc sr entries for one plane configs */
  1277. crtc = single_enabled_crtc(dev);
  1278. if (crtc) {
  1279. /* self-refresh has much higher latency */
  1280. static const int sr_latency_ns = 12000;
  1281. int clock = crtc->mode.clock;
  1282. int htotal = crtc->mode.htotal;
  1283. int hdisplay = crtc->mode.hdisplay;
  1284. int pixel_size = crtc->fb->bits_per_pixel / 8;
  1285. unsigned long line_time_us;
  1286. int entries;
  1287. line_time_us = ((htotal * 1000) / clock);
  1288. /* Use ns/us then divide to preserve precision */
  1289. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1290. pixel_size * hdisplay;
  1291. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  1292. srwm = I965_FIFO_SIZE - entries;
  1293. if (srwm < 0)
  1294. srwm = 1;
  1295. srwm &= 0x1ff;
  1296. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  1297. entries, srwm);
  1298. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1299. pixel_size * 64;
  1300. entries = DIV_ROUND_UP(entries,
  1301. i965_cursor_wm_info.cacheline_size);
  1302. cursor_sr = i965_cursor_wm_info.fifo_size -
  1303. (entries + i965_cursor_wm_info.guard_size);
  1304. if (cursor_sr > i965_cursor_wm_info.max_wm)
  1305. cursor_sr = i965_cursor_wm_info.max_wm;
  1306. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  1307. "cursor %d\n", srwm, cursor_sr);
  1308. if (IS_CRESTLINE(dev))
  1309. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1310. } else {
  1311. /* Turn off self refresh if both pipes are enabled */
  1312. if (IS_CRESTLINE(dev))
  1313. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  1314. & ~FW_BLC_SELF_EN);
  1315. }
  1316. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  1317. srwm);
  1318. /* 965 has limitations... */
  1319. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  1320. (8 << 16) | (8 << 8) | (8 << 0));
  1321. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  1322. /* update cursor SR watermark */
  1323. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1324. }
  1325. static void i9xx_update_wm(struct drm_device *dev)
  1326. {
  1327. struct drm_i915_private *dev_priv = dev->dev_private;
  1328. const struct intel_watermark_params *wm_info;
  1329. uint32_t fwater_lo;
  1330. uint32_t fwater_hi;
  1331. int cwm, srwm = 1;
  1332. int fifo_size;
  1333. int planea_wm, planeb_wm;
  1334. struct drm_crtc *crtc, *enabled = NULL;
  1335. if (IS_I945GM(dev))
  1336. wm_info = &i945_wm_info;
  1337. else if (!IS_GEN2(dev))
  1338. wm_info = &i915_wm_info;
  1339. else
  1340. wm_info = &i855_wm_info;
  1341. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  1342. crtc = intel_get_crtc_for_plane(dev, 0);
  1343. if (intel_crtc_active(crtc)) {
  1344. int cpp = crtc->fb->bits_per_pixel / 8;
  1345. if (IS_GEN2(dev))
  1346. cpp = 4;
  1347. planea_wm = intel_calculate_wm(crtc->mode.clock,
  1348. wm_info, fifo_size, cpp,
  1349. latency_ns);
  1350. enabled = crtc;
  1351. } else
  1352. planea_wm = fifo_size - wm_info->guard_size;
  1353. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  1354. crtc = intel_get_crtc_for_plane(dev, 1);
  1355. if (intel_crtc_active(crtc)) {
  1356. int cpp = crtc->fb->bits_per_pixel / 8;
  1357. if (IS_GEN2(dev))
  1358. cpp = 4;
  1359. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  1360. wm_info, fifo_size, cpp,
  1361. latency_ns);
  1362. if (enabled == NULL)
  1363. enabled = crtc;
  1364. else
  1365. enabled = NULL;
  1366. } else
  1367. planeb_wm = fifo_size - wm_info->guard_size;
  1368. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  1369. /*
  1370. * Overlay gets an aggressive default since video jitter is bad.
  1371. */
  1372. cwm = 2;
  1373. /* Play safe and disable self-refresh before adjusting watermarks. */
  1374. if (IS_I945G(dev) || IS_I945GM(dev))
  1375. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  1376. else if (IS_I915GM(dev))
  1377. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  1378. /* Calc sr entries for one plane configs */
  1379. if (HAS_FW_BLC(dev) && enabled) {
  1380. /* self-refresh has much higher latency */
  1381. static const int sr_latency_ns = 6000;
  1382. int clock = enabled->mode.clock;
  1383. int htotal = enabled->mode.htotal;
  1384. int hdisplay = enabled->mode.hdisplay;
  1385. int pixel_size = enabled->fb->bits_per_pixel / 8;
  1386. unsigned long line_time_us;
  1387. int entries;
  1388. line_time_us = (htotal * 1000) / clock;
  1389. /* Use ns/us then divide to preserve precision */
  1390. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1391. pixel_size * hdisplay;
  1392. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  1393. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  1394. srwm = wm_info->fifo_size - entries;
  1395. if (srwm < 0)
  1396. srwm = 1;
  1397. if (IS_I945G(dev) || IS_I945GM(dev))
  1398. I915_WRITE(FW_BLC_SELF,
  1399. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  1400. else if (IS_I915GM(dev))
  1401. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  1402. }
  1403. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  1404. planea_wm, planeb_wm, cwm, srwm);
  1405. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  1406. fwater_hi = (cwm & 0x1f);
  1407. /* Set request length to 8 cachelines per fetch */
  1408. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  1409. fwater_hi = fwater_hi | (1 << 8);
  1410. I915_WRITE(FW_BLC, fwater_lo);
  1411. I915_WRITE(FW_BLC2, fwater_hi);
  1412. if (HAS_FW_BLC(dev)) {
  1413. if (enabled) {
  1414. if (IS_I945G(dev) || IS_I945GM(dev))
  1415. I915_WRITE(FW_BLC_SELF,
  1416. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  1417. else if (IS_I915GM(dev))
  1418. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  1419. DRM_DEBUG_KMS("memory self refresh enabled\n");
  1420. } else
  1421. DRM_DEBUG_KMS("memory self refresh disabled\n");
  1422. }
  1423. }
  1424. static void i830_update_wm(struct drm_device *dev)
  1425. {
  1426. struct drm_i915_private *dev_priv = dev->dev_private;
  1427. struct drm_crtc *crtc;
  1428. uint32_t fwater_lo;
  1429. int planea_wm;
  1430. crtc = single_enabled_crtc(dev);
  1431. if (crtc == NULL)
  1432. return;
  1433. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  1434. dev_priv->display.get_fifo_size(dev, 0),
  1435. 4, latency_ns);
  1436. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  1437. fwater_lo |= (3<<8) | planea_wm;
  1438. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  1439. I915_WRITE(FW_BLC, fwater_lo);
  1440. }
  1441. #define ILK_LP0_PLANE_LATENCY 700
  1442. #define ILK_LP0_CURSOR_LATENCY 1300
  1443. /*
  1444. * Check the wm result.
  1445. *
  1446. * If any calculated watermark values is larger than the maximum value that
  1447. * can be programmed into the associated watermark register, that watermark
  1448. * must be disabled.
  1449. */
  1450. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  1451. int fbc_wm, int display_wm, int cursor_wm,
  1452. const struct intel_watermark_params *display,
  1453. const struct intel_watermark_params *cursor)
  1454. {
  1455. struct drm_i915_private *dev_priv = dev->dev_private;
  1456. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  1457. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  1458. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  1459. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  1460. fbc_wm, SNB_FBC_MAX_SRWM, level);
  1461. /* fbc has it's own way to disable FBC WM */
  1462. I915_WRITE(DISP_ARB_CTL,
  1463. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  1464. return false;
  1465. } else if (INTEL_INFO(dev)->gen >= 6) {
  1466. /* enable FBC WM (except on ILK, where it must remain off) */
  1467. I915_WRITE(DISP_ARB_CTL,
  1468. I915_READ(DISP_ARB_CTL) & ~DISP_FBC_WM_DIS);
  1469. }
  1470. if (display_wm > display->max_wm) {
  1471. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  1472. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  1473. return false;
  1474. }
  1475. if (cursor_wm > cursor->max_wm) {
  1476. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  1477. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  1478. return false;
  1479. }
  1480. if (!(fbc_wm || display_wm || cursor_wm)) {
  1481. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  1482. return false;
  1483. }
  1484. return true;
  1485. }
  1486. /*
  1487. * Compute watermark values of WM[1-3],
  1488. */
  1489. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  1490. int latency_ns,
  1491. const struct intel_watermark_params *display,
  1492. const struct intel_watermark_params *cursor,
  1493. int *fbc_wm, int *display_wm, int *cursor_wm)
  1494. {
  1495. struct drm_crtc *crtc;
  1496. unsigned long line_time_us;
  1497. int hdisplay, htotal, pixel_size, clock;
  1498. int line_count, line_size;
  1499. int small, large;
  1500. int entries;
  1501. if (!latency_ns) {
  1502. *fbc_wm = *display_wm = *cursor_wm = 0;
  1503. return false;
  1504. }
  1505. crtc = intel_get_crtc_for_plane(dev, plane);
  1506. hdisplay = crtc->mode.hdisplay;
  1507. htotal = crtc->mode.htotal;
  1508. clock = crtc->mode.clock;
  1509. pixel_size = crtc->fb->bits_per_pixel / 8;
  1510. line_time_us = (htotal * 1000) / clock;
  1511. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1512. line_size = hdisplay * pixel_size;
  1513. /* Use the minimum of the small and large buffer method for primary */
  1514. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1515. large = line_count * line_size;
  1516. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1517. *display_wm = entries + display->guard_size;
  1518. /*
  1519. * Spec says:
  1520. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  1521. */
  1522. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  1523. /* calculate the self-refresh watermark for display cursor */
  1524. entries = line_count * pixel_size * 64;
  1525. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1526. *cursor_wm = entries + cursor->guard_size;
  1527. return ironlake_check_srwm(dev, level,
  1528. *fbc_wm, *display_wm, *cursor_wm,
  1529. display, cursor);
  1530. }
  1531. static void ironlake_update_wm(struct drm_device *dev)
  1532. {
  1533. struct drm_i915_private *dev_priv = dev->dev_private;
  1534. int fbc_wm, plane_wm, cursor_wm;
  1535. unsigned int enabled;
  1536. enabled = 0;
  1537. if (g4x_compute_wm0(dev, PIPE_A,
  1538. &ironlake_display_wm_info,
  1539. ILK_LP0_PLANE_LATENCY,
  1540. &ironlake_cursor_wm_info,
  1541. ILK_LP0_CURSOR_LATENCY,
  1542. &plane_wm, &cursor_wm)) {
  1543. I915_WRITE(WM0_PIPEA_ILK,
  1544. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1545. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1546. " plane %d, " "cursor: %d\n",
  1547. plane_wm, cursor_wm);
  1548. enabled |= 1 << PIPE_A;
  1549. }
  1550. if (g4x_compute_wm0(dev, PIPE_B,
  1551. &ironlake_display_wm_info,
  1552. ILK_LP0_PLANE_LATENCY,
  1553. &ironlake_cursor_wm_info,
  1554. ILK_LP0_CURSOR_LATENCY,
  1555. &plane_wm, &cursor_wm)) {
  1556. I915_WRITE(WM0_PIPEB_ILK,
  1557. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1558. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1559. " plane %d, cursor: %d\n",
  1560. plane_wm, cursor_wm);
  1561. enabled |= 1 << PIPE_B;
  1562. }
  1563. /*
  1564. * Calculate and update the self-refresh watermark only when one
  1565. * display plane is used.
  1566. */
  1567. I915_WRITE(WM3_LP_ILK, 0);
  1568. I915_WRITE(WM2_LP_ILK, 0);
  1569. I915_WRITE(WM1_LP_ILK, 0);
  1570. if (!single_plane_enabled(enabled))
  1571. return;
  1572. enabled = ffs(enabled) - 1;
  1573. /* WM1 */
  1574. if (!ironlake_compute_srwm(dev, 1, enabled,
  1575. ILK_READ_WM1_LATENCY() * 500,
  1576. &ironlake_display_srwm_info,
  1577. &ironlake_cursor_srwm_info,
  1578. &fbc_wm, &plane_wm, &cursor_wm))
  1579. return;
  1580. I915_WRITE(WM1_LP_ILK,
  1581. WM1_LP_SR_EN |
  1582. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1583. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1584. (plane_wm << WM1_LP_SR_SHIFT) |
  1585. cursor_wm);
  1586. /* WM2 */
  1587. if (!ironlake_compute_srwm(dev, 2, enabled,
  1588. ILK_READ_WM2_LATENCY() * 500,
  1589. &ironlake_display_srwm_info,
  1590. &ironlake_cursor_srwm_info,
  1591. &fbc_wm, &plane_wm, &cursor_wm))
  1592. return;
  1593. I915_WRITE(WM2_LP_ILK,
  1594. WM2_LP_EN |
  1595. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1596. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1597. (plane_wm << WM1_LP_SR_SHIFT) |
  1598. cursor_wm);
  1599. /*
  1600. * WM3 is unsupported on ILK, probably because we don't have latency
  1601. * data for that power state
  1602. */
  1603. }
  1604. static void sandybridge_update_wm(struct drm_device *dev)
  1605. {
  1606. struct drm_i915_private *dev_priv = dev->dev_private;
  1607. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1608. u32 val;
  1609. int fbc_wm, plane_wm, cursor_wm;
  1610. unsigned int enabled;
  1611. enabled = 0;
  1612. if (g4x_compute_wm0(dev, PIPE_A,
  1613. &sandybridge_display_wm_info, latency,
  1614. &sandybridge_cursor_wm_info, latency,
  1615. &plane_wm, &cursor_wm)) {
  1616. val = I915_READ(WM0_PIPEA_ILK);
  1617. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1618. I915_WRITE(WM0_PIPEA_ILK, val |
  1619. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1620. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1621. " plane %d, " "cursor: %d\n",
  1622. plane_wm, cursor_wm);
  1623. enabled |= 1 << PIPE_A;
  1624. }
  1625. if (g4x_compute_wm0(dev, PIPE_B,
  1626. &sandybridge_display_wm_info, latency,
  1627. &sandybridge_cursor_wm_info, latency,
  1628. &plane_wm, &cursor_wm)) {
  1629. val = I915_READ(WM0_PIPEB_ILK);
  1630. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1631. I915_WRITE(WM0_PIPEB_ILK, val |
  1632. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1633. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1634. " plane %d, cursor: %d\n",
  1635. plane_wm, cursor_wm);
  1636. enabled |= 1 << PIPE_B;
  1637. }
  1638. /*
  1639. * Calculate and update the self-refresh watermark only when one
  1640. * display plane is used.
  1641. *
  1642. * SNB support 3 levels of watermark.
  1643. *
  1644. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  1645. * and disabled in the descending order
  1646. *
  1647. */
  1648. I915_WRITE(WM3_LP_ILK, 0);
  1649. I915_WRITE(WM2_LP_ILK, 0);
  1650. I915_WRITE(WM1_LP_ILK, 0);
  1651. if (!single_plane_enabled(enabled) ||
  1652. dev_priv->sprite_scaling_enabled)
  1653. return;
  1654. enabled = ffs(enabled) - 1;
  1655. /* WM1 */
  1656. if (!ironlake_compute_srwm(dev, 1, enabled,
  1657. SNB_READ_WM1_LATENCY() * 500,
  1658. &sandybridge_display_srwm_info,
  1659. &sandybridge_cursor_srwm_info,
  1660. &fbc_wm, &plane_wm, &cursor_wm))
  1661. return;
  1662. I915_WRITE(WM1_LP_ILK,
  1663. WM1_LP_SR_EN |
  1664. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1665. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1666. (plane_wm << WM1_LP_SR_SHIFT) |
  1667. cursor_wm);
  1668. /* WM2 */
  1669. if (!ironlake_compute_srwm(dev, 2, enabled,
  1670. SNB_READ_WM2_LATENCY() * 500,
  1671. &sandybridge_display_srwm_info,
  1672. &sandybridge_cursor_srwm_info,
  1673. &fbc_wm, &plane_wm, &cursor_wm))
  1674. return;
  1675. I915_WRITE(WM2_LP_ILK,
  1676. WM2_LP_EN |
  1677. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1678. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1679. (plane_wm << WM1_LP_SR_SHIFT) |
  1680. cursor_wm);
  1681. /* WM3 */
  1682. if (!ironlake_compute_srwm(dev, 3, enabled,
  1683. SNB_READ_WM3_LATENCY() * 500,
  1684. &sandybridge_display_srwm_info,
  1685. &sandybridge_cursor_srwm_info,
  1686. &fbc_wm, &plane_wm, &cursor_wm))
  1687. return;
  1688. I915_WRITE(WM3_LP_ILK,
  1689. WM3_LP_EN |
  1690. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1691. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1692. (plane_wm << WM1_LP_SR_SHIFT) |
  1693. cursor_wm);
  1694. }
  1695. static void ivybridge_update_wm(struct drm_device *dev)
  1696. {
  1697. struct drm_i915_private *dev_priv = dev->dev_private;
  1698. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1699. u32 val;
  1700. int fbc_wm, plane_wm, cursor_wm;
  1701. int ignore_fbc_wm, ignore_plane_wm, ignore_cursor_wm;
  1702. unsigned int enabled;
  1703. enabled = 0;
  1704. if (g4x_compute_wm0(dev, PIPE_A,
  1705. &sandybridge_display_wm_info, latency,
  1706. &sandybridge_cursor_wm_info, latency,
  1707. &plane_wm, &cursor_wm)) {
  1708. val = I915_READ(WM0_PIPEA_ILK);
  1709. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1710. I915_WRITE(WM0_PIPEA_ILK, val |
  1711. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1712. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1713. " plane %d, " "cursor: %d\n",
  1714. plane_wm, cursor_wm);
  1715. enabled |= 1 << PIPE_A;
  1716. }
  1717. if (g4x_compute_wm0(dev, PIPE_B,
  1718. &sandybridge_display_wm_info, latency,
  1719. &sandybridge_cursor_wm_info, latency,
  1720. &plane_wm, &cursor_wm)) {
  1721. val = I915_READ(WM0_PIPEB_ILK);
  1722. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1723. I915_WRITE(WM0_PIPEB_ILK, val |
  1724. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1725. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1726. " plane %d, cursor: %d\n",
  1727. plane_wm, cursor_wm);
  1728. enabled |= 1 << PIPE_B;
  1729. }
  1730. if (g4x_compute_wm0(dev, PIPE_C,
  1731. &sandybridge_display_wm_info, latency,
  1732. &sandybridge_cursor_wm_info, latency,
  1733. &plane_wm, &cursor_wm)) {
  1734. val = I915_READ(WM0_PIPEC_IVB);
  1735. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1736. I915_WRITE(WM0_PIPEC_IVB, val |
  1737. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1738. DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
  1739. " plane %d, cursor: %d\n",
  1740. plane_wm, cursor_wm);
  1741. enabled |= 1 << PIPE_C;
  1742. }
  1743. /*
  1744. * Calculate and update the self-refresh watermark only when one
  1745. * display plane is used.
  1746. *
  1747. * SNB support 3 levels of watermark.
  1748. *
  1749. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  1750. * and disabled in the descending order
  1751. *
  1752. */
  1753. I915_WRITE(WM3_LP_ILK, 0);
  1754. I915_WRITE(WM2_LP_ILK, 0);
  1755. I915_WRITE(WM1_LP_ILK, 0);
  1756. if (!single_plane_enabled(enabled) ||
  1757. dev_priv->sprite_scaling_enabled)
  1758. return;
  1759. enabled = ffs(enabled) - 1;
  1760. /* WM1 */
  1761. if (!ironlake_compute_srwm(dev, 1, enabled,
  1762. SNB_READ_WM1_LATENCY() * 500,
  1763. &sandybridge_display_srwm_info,
  1764. &sandybridge_cursor_srwm_info,
  1765. &fbc_wm, &plane_wm, &cursor_wm))
  1766. return;
  1767. I915_WRITE(WM1_LP_ILK,
  1768. WM1_LP_SR_EN |
  1769. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1770. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1771. (plane_wm << WM1_LP_SR_SHIFT) |
  1772. cursor_wm);
  1773. /* WM2 */
  1774. if (!ironlake_compute_srwm(dev, 2, enabled,
  1775. SNB_READ_WM2_LATENCY() * 500,
  1776. &sandybridge_display_srwm_info,
  1777. &sandybridge_cursor_srwm_info,
  1778. &fbc_wm, &plane_wm, &cursor_wm))
  1779. return;
  1780. I915_WRITE(WM2_LP_ILK,
  1781. WM2_LP_EN |
  1782. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1783. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1784. (plane_wm << WM1_LP_SR_SHIFT) |
  1785. cursor_wm);
  1786. /* WM3, note we have to correct the cursor latency */
  1787. if (!ironlake_compute_srwm(dev, 3, enabled,
  1788. SNB_READ_WM3_LATENCY() * 500,
  1789. &sandybridge_display_srwm_info,
  1790. &sandybridge_cursor_srwm_info,
  1791. &fbc_wm, &plane_wm, &ignore_cursor_wm) ||
  1792. !ironlake_compute_srwm(dev, 3, enabled,
  1793. 2 * SNB_READ_WM3_LATENCY() * 500,
  1794. &sandybridge_display_srwm_info,
  1795. &sandybridge_cursor_srwm_info,
  1796. &ignore_fbc_wm, &ignore_plane_wm, &cursor_wm))
  1797. return;
  1798. I915_WRITE(WM3_LP_ILK,
  1799. WM3_LP_EN |
  1800. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1801. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1802. (plane_wm << WM1_LP_SR_SHIFT) |
  1803. cursor_wm);
  1804. }
  1805. static uint32_t hsw_wm_get_pixel_rate(struct drm_device *dev,
  1806. struct drm_crtc *crtc)
  1807. {
  1808. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1809. uint32_t pixel_rate, pfit_size;
  1810. pixel_rate = intel_crtc->config.adjusted_mode.clock;
  1811. /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
  1812. * adjust the pixel_rate here. */
  1813. pfit_size = intel_crtc->config.pch_pfit.size;
  1814. if (pfit_size) {
  1815. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  1816. pipe_w = intel_crtc->config.requested_mode.hdisplay;
  1817. pipe_h = intel_crtc->config.requested_mode.vdisplay;
  1818. pfit_w = (pfit_size >> 16) & 0xFFFF;
  1819. pfit_h = pfit_size & 0xFFFF;
  1820. if (pipe_w < pfit_w)
  1821. pipe_w = pfit_w;
  1822. if (pipe_h < pfit_h)
  1823. pipe_h = pfit_h;
  1824. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  1825. pfit_w * pfit_h);
  1826. }
  1827. return pixel_rate;
  1828. }
  1829. static uint32_t hsw_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
  1830. uint32_t latency)
  1831. {
  1832. uint64_t ret;
  1833. ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
  1834. ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
  1835. return ret;
  1836. }
  1837. static uint32_t hsw_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
  1838. uint32_t horiz_pixels, uint8_t bytes_per_pixel,
  1839. uint32_t latency)
  1840. {
  1841. uint32_t ret;
  1842. ret = (latency * pixel_rate) / (pipe_htotal * 10000);
  1843. ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
  1844. ret = DIV_ROUND_UP(ret, 64) + 2;
  1845. return ret;
  1846. }
  1847. static uint32_t hsw_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
  1848. uint8_t bytes_per_pixel)
  1849. {
  1850. return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
  1851. }
  1852. struct hsw_pipe_wm_parameters {
  1853. bool active;
  1854. bool sprite_enabled;
  1855. uint8_t pri_bytes_per_pixel;
  1856. uint8_t spr_bytes_per_pixel;
  1857. uint8_t cur_bytes_per_pixel;
  1858. uint32_t pri_horiz_pixels;
  1859. uint32_t spr_horiz_pixels;
  1860. uint32_t cur_horiz_pixels;
  1861. uint32_t pipe_htotal;
  1862. uint32_t pixel_rate;
  1863. };
  1864. struct hsw_wm_maximums {
  1865. uint16_t pri;
  1866. uint16_t spr;
  1867. uint16_t cur;
  1868. uint16_t fbc;
  1869. };
  1870. struct hsw_lp_wm_result {
  1871. bool enable;
  1872. bool fbc_enable;
  1873. uint32_t pri_val;
  1874. uint32_t spr_val;
  1875. uint32_t cur_val;
  1876. uint32_t fbc_val;
  1877. };
  1878. struct hsw_wm_values {
  1879. uint32_t wm_pipe[3];
  1880. uint32_t wm_lp[3];
  1881. uint32_t wm_lp_spr[3];
  1882. uint32_t wm_linetime[3];
  1883. bool enable_fbc_wm;
  1884. };
  1885. enum hsw_data_buf_partitioning {
  1886. HSW_DATA_BUF_PART_1_2,
  1887. HSW_DATA_BUF_PART_5_6,
  1888. };
  1889. /* For both WM_PIPE and WM_LP. */
  1890. static uint32_t hsw_compute_pri_wm(struct hsw_pipe_wm_parameters *params,
  1891. uint32_t mem_value,
  1892. bool is_lp)
  1893. {
  1894. uint32_t method1, method2;
  1895. /* TODO: for now, assume the primary plane is always enabled. */
  1896. if (!params->active)
  1897. return 0;
  1898. method1 = hsw_wm_method1(params->pixel_rate,
  1899. params->pri_bytes_per_pixel,
  1900. mem_value);
  1901. if (!is_lp)
  1902. return method1;
  1903. method2 = hsw_wm_method2(params->pixel_rate,
  1904. params->pipe_htotal,
  1905. params->pri_horiz_pixels,
  1906. params->pri_bytes_per_pixel,
  1907. mem_value);
  1908. return min(method1, method2);
  1909. }
  1910. /* For both WM_PIPE and WM_LP. */
  1911. static uint32_t hsw_compute_spr_wm(struct hsw_pipe_wm_parameters *params,
  1912. uint32_t mem_value)
  1913. {
  1914. uint32_t method1, method2;
  1915. if (!params->active || !params->sprite_enabled)
  1916. return 0;
  1917. method1 = hsw_wm_method1(params->pixel_rate,
  1918. params->spr_bytes_per_pixel,
  1919. mem_value);
  1920. method2 = hsw_wm_method2(params->pixel_rate,
  1921. params->pipe_htotal,
  1922. params->spr_horiz_pixels,
  1923. params->spr_bytes_per_pixel,
  1924. mem_value);
  1925. return min(method1, method2);
  1926. }
  1927. /* For both WM_PIPE and WM_LP. */
  1928. static uint32_t hsw_compute_cur_wm(struct hsw_pipe_wm_parameters *params,
  1929. uint32_t mem_value)
  1930. {
  1931. if (!params->active)
  1932. return 0;
  1933. return hsw_wm_method2(params->pixel_rate,
  1934. params->pipe_htotal,
  1935. params->cur_horiz_pixels,
  1936. params->cur_bytes_per_pixel,
  1937. mem_value);
  1938. }
  1939. /* Only for WM_LP. */
  1940. static uint32_t hsw_compute_fbc_wm(struct hsw_pipe_wm_parameters *params,
  1941. uint32_t pri_val,
  1942. uint32_t mem_value)
  1943. {
  1944. if (!params->active)
  1945. return 0;
  1946. return hsw_wm_fbc(pri_val,
  1947. params->pri_horiz_pixels,
  1948. params->pri_bytes_per_pixel);
  1949. }
  1950. static bool hsw_compute_lp_wm(uint32_t mem_value, struct hsw_wm_maximums *max,
  1951. struct hsw_pipe_wm_parameters *params,
  1952. struct hsw_lp_wm_result *result)
  1953. {
  1954. enum pipe pipe;
  1955. uint32_t pri_val[3], spr_val[3], cur_val[3], fbc_val[3];
  1956. for (pipe = PIPE_A; pipe <= PIPE_C; pipe++) {
  1957. struct hsw_pipe_wm_parameters *p = &params[pipe];
  1958. pri_val[pipe] = hsw_compute_pri_wm(p, mem_value, true);
  1959. spr_val[pipe] = hsw_compute_spr_wm(p, mem_value);
  1960. cur_val[pipe] = hsw_compute_cur_wm(p, mem_value);
  1961. fbc_val[pipe] = hsw_compute_fbc_wm(p, pri_val[pipe], mem_value);
  1962. }
  1963. result->pri_val = max3(pri_val[0], pri_val[1], pri_val[2]);
  1964. result->spr_val = max3(spr_val[0], spr_val[1], spr_val[2]);
  1965. result->cur_val = max3(cur_val[0], cur_val[1], cur_val[2]);
  1966. result->fbc_val = max3(fbc_val[0], fbc_val[1], fbc_val[2]);
  1967. if (result->fbc_val > max->fbc) {
  1968. result->fbc_enable = false;
  1969. result->fbc_val = 0;
  1970. } else {
  1971. result->fbc_enable = true;
  1972. }
  1973. result->enable = result->pri_val <= max->pri &&
  1974. result->spr_val <= max->spr &&
  1975. result->cur_val <= max->cur;
  1976. return result->enable;
  1977. }
  1978. static uint32_t hsw_compute_wm_pipe(struct drm_i915_private *dev_priv,
  1979. uint32_t mem_value, enum pipe pipe,
  1980. struct hsw_pipe_wm_parameters *params)
  1981. {
  1982. uint32_t pri_val, cur_val, spr_val;
  1983. pri_val = hsw_compute_pri_wm(params, mem_value, false);
  1984. spr_val = hsw_compute_spr_wm(params, mem_value);
  1985. cur_val = hsw_compute_cur_wm(params, mem_value);
  1986. WARN(pri_val > 127,
  1987. "Primary WM error, mode not supported for pipe %c\n",
  1988. pipe_name(pipe));
  1989. WARN(spr_val > 127,
  1990. "Sprite WM error, mode not supported for pipe %c\n",
  1991. pipe_name(pipe));
  1992. WARN(cur_val > 63,
  1993. "Cursor WM error, mode not supported for pipe %c\n",
  1994. pipe_name(pipe));
  1995. return (pri_val << WM0_PIPE_PLANE_SHIFT) |
  1996. (spr_val << WM0_PIPE_SPRITE_SHIFT) |
  1997. cur_val;
  1998. }
  1999. static uint32_t
  2000. hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
  2001. {
  2002. struct drm_i915_private *dev_priv = dev->dev_private;
  2003. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2004. struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
  2005. u32 linetime, ips_linetime;
  2006. if (!intel_crtc_active(crtc))
  2007. return 0;
  2008. /* The WM are computed with base on how long it takes to fill a single
  2009. * row at the given clock rate, multiplied by 8.
  2010. * */
  2011. linetime = DIV_ROUND_CLOSEST(mode->htotal * 1000 * 8, mode->clock);
  2012. ips_linetime = DIV_ROUND_CLOSEST(mode->htotal * 1000 * 8,
  2013. intel_ddi_get_cdclk_freq(dev_priv));
  2014. return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
  2015. PIPE_WM_LINETIME_TIME(linetime);
  2016. }
  2017. static void hsw_compute_wm_parameters(struct drm_device *dev,
  2018. struct hsw_pipe_wm_parameters *params,
  2019. uint32_t *wm,
  2020. struct hsw_wm_maximums *lp_max_1_2,
  2021. struct hsw_wm_maximums *lp_max_5_6)
  2022. {
  2023. struct drm_i915_private *dev_priv = dev->dev_private;
  2024. struct drm_crtc *crtc;
  2025. struct drm_plane *plane;
  2026. uint64_t sskpd = I915_READ64(MCH_SSKPD);
  2027. enum pipe pipe;
  2028. int pipes_active = 0, sprites_enabled = 0;
  2029. if ((sskpd >> 56) & 0xFF)
  2030. wm[0] = (sskpd >> 56) & 0xFF;
  2031. else
  2032. wm[0] = sskpd & 0xF;
  2033. wm[1] = ((sskpd >> 4) & 0xFF) * 5;
  2034. wm[2] = ((sskpd >> 12) & 0xFF) * 5;
  2035. wm[3] = ((sskpd >> 20) & 0x1FF) * 5;
  2036. wm[4] = ((sskpd >> 32) & 0x1FF) * 5;
  2037. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2039. struct hsw_pipe_wm_parameters *p;
  2040. pipe = intel_crtc->pipe;
  2041. p = &params[pipe];
  2042. p->active = intel_crtc_active(crtc);
  2043. if (!p->active)
  2044. continue;
  2045. pipes_active++;
  2046. p->pipe_htotal = intel_crtc->config.adjusted_mode.htotal;
  2047. p->pixel_rate = hsw_wm_get_pixel_rate(dev, crtc);
  2048. p->pri_bytes_per_pixel = crtc->fb->bits_per_pixel / 8;
  2049. p->cur_bytes_per_pixel = 4;
  2050. p->pri_horiz_pixels =
  2051. intel_crtc->config.requested_mode.hdisplay;
  2052. p->cur_horiz_pixels = 64;
  2053. }
  2054. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  2055. struct intel_plane *intel_plane = to_intel_plane(plane);
  2056. struct hsw_pipe_wm_parameters *p;
  2057. pipe = intel_plane->pipe;
  2058. p = &params[pipe];
  2059. p->sprite_enabled = intel_plane->wm.enable;
  2060. p->spr_bytes_per_pixel = intel_plane->wm.bytes_per_pixel;
  2061. p->spr_horiz_pixels = intel_plane->wm.horiz_pixels;
  2062. if (p->sprite_enabled)
  2063. sprites_enabled++;
  2064. }
  2065. if (pipes_active > 1) {
  2066. lp_max_1_2->pri = lp_max_5_6->pri = sprites_enabled ? 128 : 256;
  2067. lp_max_1_2->spr = lp_max_5_6->spr = 128;
  2068. lp_max_1_2->cur = lp_max_5_6->cur = 64;
  2069. } else {
  2070. lp_max_1_2->pri = sprites_enabled ? 384 : 768;
  2071. lp_max_5_6->pri = sprites_enabled ? 128 : 768;
  2072. lp_max_1_2->spr = 384;
  2073. lp_max_5_6->spr = 640;
  2074. lp_max_1_2->cur = lp_max_5_6->cur = 255;
  2075. }
  2076. lp_max_1_2->fbc = lp_max_5_6->fbc = 15;
  2077. }
  2078. static void hsw_compute_wm_results(struct drm_device *dev,
  2079. struct hsw_pipe_wm_parameters *params,
  2080. uint32_t *wm,
  2081. struct hsw_wm_maximums *lp_maximums,
  2082. struct hsw_wm_values *results)
  2083. {
  2084. struct drm_i915_private *dev_priv = dev->dev_private;
  2085. struct drm_crtc *crtc;
  2086. struct hsw_lp_wm_result lp_results[4] = {};
  2087. enum pipe pipe;
  2088. int level, max_level, wm_lp;
  2089. for (level = 1; level <= 4; level++)
  2090. if (!hsw_compute_lp_wm(wm[level], lp_maximums, params,
  2091. &lp_results[level - 1]))
  2092. break;
  2093. max_level = level - 1;
  2094. /* The spec says it is preferred to disable FBC WMs instead of disabling
  2095. * a WM level. */
  2096. results->enable_fbc_wm = true;
  2097. for (level = 1; level <= max_level; level++) {
  2098. if (!lp_results[level - 1].fbc_enable) {
  2099. results->enable_fbc_wm = false;
  2100. break;
  2101. }
  2102. }
  2103. memset(results, 0, sizeof(*results));
  2104. for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
  2105. const struct hsw_lp_wm_result *r;
  2106. level = (max_level == 4 && wm_lp > 1) ? wm_lp + 1 : wm_lp;
  2107. if (level > max_level)
  2108. break;
  2109. r = &lp_results[level - 1];
  2110. results->wm_lp[wm_lp - 1] = HSW_WM_LP_VAL(level * 2,
  2111. r->fbc_val,
  2112. r->pri_val,
  2113. r->cur_val);
  2114. results->wm_lp_spr[wm_lp - 1] = r->spr_val;
  2115. }
  2116. for_each_pipe(pipe)
  2117. results->wm_pipe[pipe] = hsw_compute_wm_pipe(dev_priv, wm[0],
  2118. pipe,
  2119. &params[pipe]);
  2120. for_each_pipe(pipe) {
  2121. crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  2122. results->wm_linetime[pipe] = hsw_compute_linetime_wm(dev, crtc);
  2123. }
  2124. }
  2125. /* Find the result with the highest level enabled. Check for enable_fbc_wm in
  2126. * case both are at the same level. Prefer r1 in case they're the same. */
  2127. static struct hsw_wm_values *hsw_find_best_result(struct hsw_wm_values *r1,
  2128. struct hsw_wm_values *r2)
  2129. {
  2130. int i, val_r1 = 0, val_r2 = 0;
  2131. for (i = 0; i < 3; i++) {
  2132. if (r1->wm_lp[i] & WM3_LP_EN)
  2133. val_r1 = r1->wm_lp[i] & WM1_LP_LATENCY_MASK;
  2134. if (r2->wm_lp[i] & WM3_LP_EN)
  2135. val_r2 = r2->wm_lp[i] & WM1_LP_LATENCY_MASK;
  2136. }
  2137. if (val_r1 == val_r2) {
  2138. if (r2->enable_fbc_wm && !r1->enable_fbc_wm)
  2139. return r2;
  2140. else
  2141. return r1;
  2142. } else if (val_r1 > val_r2) {
  2143. return r1;
  2144. } else {
  2145. return r2;
  2146. }
  2147. }
  2148. /*
  2149. * The spec says we shouldn't write when we don't need, because every write
  2150. * causes WMs to be re-evaluated, expending some power.
  2151. */
  2152. static void hsw_write_wm_values(struct drm_i915_private *dev_priv,
  2153. struct hsw_wm_values *results,
  2154. enum hsw_data_buf_partitioning partitioning)
  2155. {
  2156. struct hsw_wm_values previous;
  2157. uint32_t val;
  2158. enum hsw_data_buf_partitioning prev_partitioning;
  2159. bool prev_enable_fbc_wm;
  2160. previous.wm_pipe[0] = I915_READ(WM0_PIPEA_ILK);
  2161. previous.wm_pipe[1] = I915_READ(WM0_PIPEB_ILK);
  2162. previous.wm_pipe[2] = I915_READ(WM0_PIPEC_IVB);
  2163. previous.wm_lp[0] = I915_READ(WM1_LP_ILK);
  2164. previous.wm_lp[1] = I915_READ(WM2_LP_ILK);
  2165. previous.wm_lp[2] = I915_READ(WM3_LP_ILK);
  2166. previous.wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
  2167. previous.wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
  2168. previous.wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
  2169. previous.wm_linetime[0] = I915_READ(PIPE_WM_LINETIME(PIPE_A));
  2170. previous.wm_linetime[1] = I915_READ(PIPE_WM_LINETIME(PIPE_B));
  2171. previous.wm_linetime[2] = I915_READ(PIPE_WM_LINETIME(PIPE_C));
  2172. prev_partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
  2173. HSW_DATA_BUF_PART_5_6 : HSW_DATA_BUF_PART_1_2;
  2174. prev_enable_fbc_wm = !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
  2175. if (memcmp(results->wm_pipe, previous.wm_pipe,
  2176. sizeof(results->wm_pipe)) == 0 &&
  2177. memcmp(results->wm_lp, previous.wm_lp,
  2178. sizeof(results->wm_lp)) == 0 &&
  2179. memcmp(results->wm_lp_spr, previous.wm_lp_spr,
  2180. sizeof(results->wm_lp_spr)) == 0 &&
  2181. memcmp(results->wm_linetime, previous.wm_linetime,
  2182. sizeof(results->wm_linetime)) == 0 &&
  2183. partitioning == prev_partitioning &&
  2184. results->enable_fbc_wm == prev_enable_fbc_wm)
  2185. return;
  2186. if (previous.wm_lp[2] != 0)
  2187. I915_WRITE(WM3_LP_ILK, 0);
  2188. if (previous.wm_lp[1] != 0)
  2189. I915_WRITE(WM2_LP_ILK, 0);
  2190. if (previous.wm_lp[0] != 0)
  2191. I915_WRITE(WM1_LP_ILK, 0);
  2192. if (previous.wm_pipe[0] != results->wm_pipe[0])
  2193. I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
  2194. if (previous.wm_pipe[1] != results->wm_pipe[1])
  2195. I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
  2196. if (previous.wm_pipe[2] != results->wm_pipe[2])
  2197. I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
  2198. if (previous.wm_linetime[0] != results->wm_linetime[0])
  2199. I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
  2200. if (previous.wm_linetime[1] != results->wm_linetime[1])
  2201. I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
  2202. if (previous.wm_linetime[2] != results->wm_linetime[2])
  2203. I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
  2204. if (prev_partitioning != partitioning) {
  2205. val = I915_READ(WM_MISC);
  2206. if (partitioning == HSW_DATA_BUF_PART_1_2)
  2207. val &= ~WM_MISC_DATA_PARTITION_5_6;
  2208. else
  2209. val |= WM_MISC_DATA_PARTITION_5_6;
  2210. I915_WRITE(WM_MISC, val);
  2211. }
  2212. if (prev_enable_fbc_wm != results->enable_fbc_wm) {
  2213. val = I915_READ(DISP_ARB_CTL);
  2214. if (results->enable_fbc_wm)
  2215. val &= ~DISP_FBC_WM_DIS;
  2216. else
  2217. val |= DISP_FBC_WM_DIS;
  2218. I915_WRITE(DISP_ARB_CTL, val);
  2219. }
  2220. if (previous.wm_lp_spr[0] != results->wm_lp_spr[0])
  2221. I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
  2222. if (previous.wm_lp_spr[1] != results->wm_lp_spr[1])
  2223. I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
  2224. if (previous.wm_lp_spr[2] != results->wm_lp_spr[2])
  2225. I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
  2226. if (results->wm_lp[0] != 0)
  2227. I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
  2228. if (results->wm_lp[1] != 0)
  2229. I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
  2230. if (results->wm_lp[2] != 0)
  2231. I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
  2232. }
  2233. static void haswell_update_wm(struct drm_device *dev)
  2234. {
  2235. struct drm_i915_private *dev_priv = dev->dev_private;
  2236. struct hsw_wm_maximums lp_max_1_2, lp_max_5_6;
  2237. struct hsw_pipe_wm_parameters params[3];
  2238. struct hsw_wm_values results_1_2, results_5_6, *best_results;
  2239. uint32_t wm[5];
  2240. enum hsw_data_buf_partitioning partitioning;
  2241. hsw_compute_wm_parameters(dev, params, wm, &lp_max_1_2, &lp_max_5_6);
  2242. hsw_compute_wm_results(dev, params, wm, &lp_max_1_2, &results_1_2);
  2243. if (lp_max_1_2.pri != lp_max_5_6.pri) {
  2244. hsw_compute_wm_results(dev, params, wm, &lp_max_5_6,
  2245. &results_5_6);
  2246. best_results = hsw_find_best_result(&results_1_2, &results_5_6);
  2247. } else {
  2248. best_results = &results_1_2;
  2249. }
  2250. partitioning = (best_results == &results_1_2) ?
  2251. HSW_DATA_BUF_PART_1_2 : HSW_DATA_BUF_PART_5_6;
  2252. hsw_write_wm_values(dev_priv, best_results, partitioning);
  2253. }
  2254. static void haswell_update_sprite_wm(struct drm_device *dev, int pipe,
  2255. uint32_t sprite_width, int pixel_size,
  2256. bool enable)
  2257. {
  2258. struct drm_plane *plane;
  2259. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  2260. struct intel_plane *intel_plane = to_intel_plane(plane);
  2261. if (intel_plane->pipe == pipe) {
  2262. intel_plane->wm.enable = enable;
  2263. intel_plane->wm.horiz_pixels = sprite_width + 1;
  2264. intel_plane->wm.bytes_per_pixel = pixel_size;
  2265. break;
  2266. }
  2267. }
  2268. haswell_update_wm(dev);
  2269. }
  2270. static bool
  2271. sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
  2272. uint32_t sprite_width, int pixel_size,
  2273. const struct intel_watermark_params *display,
  2274. int display_latency_ns, int *sprite_wm)
  2275. {
  2276. struct drm_crtc *crtc;
  2277. int clock;
  2278. int entries, tlb_miss;
  2279. crtc = intel_get_crtc_for_plane(dev, plane);
  2280. if (!intel_crtc_active(crtc)) {
  2281. *sprite_wm = display->guard_size;
  2282. return false;
  2283. }
  2284. clock = crtc->mode.clock;
  2285. /* Use the small buffer method to calculate the sprite watermark */
  2286. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  2287. tlb_miss = display->fifo_size*display->cacheline_size -
  2288. sprite_width * 8;
  2289. if (tlb_miss > 0)
  2290. entries += tlb_miss;
  2291. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  2292. *sprite_wm = entries + display->guard_size;
  2293. if (*sprite_wm > (int)display->max_wm)
  2294. *sprite_wm = display->max_wm;
  2295. return true;
  2296. }
  2297. static bool
  2298. sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
  2299. uint32_t sprite_width, int pixel_size,
  2300. const struct intel_watermark_params *display,
  2301. int latency_ns, int *sprite_wm)
  2302. {
  2303. struct drm_crtc *crtc;
  2304. unsigned long line_time_us;
  2305. int clock;
  2306. int line_count, line_size;
  2307. int small, large;
  2308. int entries;
  2309. if (!latency_ns) {
  2310. *sprite_wm = 0;
  2311. return false;
  2312. }
  2313. crtc = intel_get_crtc_for_plane(dev, plane);
  2314. clock = crtc->mode.clock;
  2315. if (!clock) {
  2316. *sprite_wm = 0;
  2317. return false;
  2318. }
  2319. line_time_us = (sprite_width * 1000) / clock;
  2320. if (!line_time_us) {
  2321. *sprite_wm = 0;
  2322. return false;
  2323. }
  2324. line_count = (latency_ns / line_time_us + 1000) / 1000;
  2325. line_size = sprite_width * pixel_size;
  2326. /* Use the minimum of the small and large buffer method for primary */
  2327. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  2328. large = line_count * line_size;
  2329. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  2330. *sprite_wm = entries + display->guard_size;
  2331. return *sprite_wm > 0x3ff ? false : true;
  2332. }
  2333. static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
  2334. uint32_t sprite_width, int pixel_size,
  2335. bool enable)
  2336. {
  2337. struct drm_i915_private *dev_priv = dev->dev_private;
  2338. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  2339. u32 val;
  2340. int sprite_wm, reg;
  2341. int ret;
  2342. if (!enable)
  2343. return;
  2344. switch (pipe) {
  2345. case 0:
  2346. reg = WM0_PIPEA_ILK;
  2347. break;
  2348. case 1:
  2349. reg = WM0_PIPEB_ILK;
  2350. break;
  2351. case 2:
  2352. reg = WM0_PIPEC_IVB;
  2353. break;
  2354. default:
  2355. return; /* bad pipe */
  2356. }
  2357. ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
  2358. &sandybridge_display_wm_info,
  2359. latency, &sprite_wm);
  2360. if (!ret) {
  2361. DRM_DEBUG_KMS("failed to compute sprite wm for pipe %c\n",
  2362. pipe_name(pipe));
  2363. return;
  2364. }
  2365. val = I915_READ(reg);
  2366. val &= ~WM0_PIPE_SPRITE_MASK;
  2367. I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
  2368. DRM_DEBUG_KMS("sprite watermarks For pipe %c - %d\n", pipe_name(pipe), sprite_wm);
  2369. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  2370. pixel_size,
  2371. &sandybridge_display_srwm_info,
  2372. SNB_READ_WM1_LATENCY() * 500,
  2373. &sprite_wm);
  2374. if (!ret) {
  2375. DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %c\n",
  2376. pipe_name(pipe));
  2377. return;
  2378. }
  2379. I915_WRITE(WM1S_LP_ILK, sprite_wm);
  2380. /* Only IVB has two more LP watermarks for sprite */
  2381. if (!IS_IVYBRIDGE(dev))
  2382. return;
  2383. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  2384. pixel_size,
  2385. &sandybridge_display_srwm_info,
  2386. SNB_READ_WM2_LATENCY() * 500,
  2387. &sprite_wm);
  2388. if (!ret) {
  2389. DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %c\n",
  2390. pipe_name(pipe));
  2391. return;
  2392. }
  2393. I915_WRITE(WM2S_LP_IVB, sprite_wm);
  2394. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  2395. pixel_size,
  2396. &sandybridge_display_srwm_info,
  2397. SNB_READ_WM3_LATENCY() * 500,
  2398. &sprite_wm);
  2399. if (!ret) {
  2400. DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %c\n",
  2401. pipe_name(pipe));
  2402. return;
  2403. }
  2404. I915_WRITE(WM3S_LP_IVB, sprite_wm);
  2405. }
  2406. /**
  2407. * intel_update_watermarks - update FIFO watermark values based on current modes
  2408. *
  2409. * Calculate watermark values for the various WM regs based on current mode
  2410. * and plane configuration.
  2411. *
  2412. * There are several cases to deal with here:
  2413. * - normal (i.e. non-self-refresh)
  2414. * - self-refresh (SR) mode
  2415. * - lines are large relative to FIFO size (buffer can hold up to 2)
  2416. * - lines are small relative to FIFO size (buffer can hold more than 2
  2417. * lines), so need to account for TLB latency
  2418. *
  2419. * The normal calculation is:
  2420. * watermark = dotclock * bytes per pixel * latency
  2421. * where latency is platform & configuration dependent (we assume pessimal
  2422. * values here).
  2423. *
  2424. * The SR calculation is:
  2425. * watermark = (trunc(latency/line time)+1) * surface width *
  2426. * bytes per pixel
  2427. * where
  2428. * line time = htotal / dotclock
  2429. * surface width = hdisplay for normal plane and 64 for cursor
  2430. * and latency is assumed to be high, as above.
  2431. *
  2432. * The final value programmed to the register should always be rounded up,
  2433. * and include an extra 2 entries to account for clock crossings.
  2434. *
  2435. * We don't use the sprite, so we can ignore that. And on Crestline we have
  2436. * to set the non-SR watermarks to 8.
  2437. */
  2438. void intel_update_watermarks(struct drm_device *dev)
  2439. {
  2440. struct drm_i915_private *dev_priv = dev->dev_private;
  2441. if (dev_priv->display.update_wm)
  2442. dev_priv->display.update_wm(dev);
  2443. }
  2444. void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
  2445. uint32_t sprite_width, int pixel_size,
  2446. bool enable)
  2447. {
  2448. struct drm_i915_private *dev_priv = dev->dev_private;
  2449. if (dev_priv->display.update_sprite_wm)
  2450. dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
  2451. pixel_size, enable);
  2452. }
  2453. static struct drm_i915_gem_object *
  2454. intel_alloc_context_page(struct drm_device *dev)
  2455. {
  2456. struct drm_i915_gem_object *ctx;
  2457. int ret;
  2458. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2459. ctx = i915_gem_alloc_object(dev, 4096);
  2460. if (!ctx) {
  2461. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  2462. return NULL;
  2463. }
  2464. ret = i915_gem_object_pin(ctx, 4096, true, false);
  2465. if (ret) {
  2466. DRM_ERROR("failed to pin power context: %d\n", ret);
  2467. goto err_unref;
  2468. }
  2469. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  2470. if (ret) {
  2471. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  2472. goto err_unpin;
  2473. }
  2474. return ctx;
  2475. err_unpin:
  2476. i915_gem_object_unpin(ctx);
  2477. err_unref:
  2478. drm_gem_object_unreference(&ctx->base);
  2479. return NULL;
  2480. }
  2481. /**
  2482. * Lock protecting IPS related data structures
  2483. */
  2484. DEFINE_SPINLOCK(mchdev_lock);
  2485. /* Global for IPS driver to get at the current i915 device. Protected by
  2486. * mchdev_lock. */
  2487. static struct drm_i915_private *i915_mch_dev;
  2488. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  2489. {
  2490. struct drm_i915_private *dev_priv = dev->dev_private;
  2491. u16 rgvswctl;
  2492. assert_spin_locked(&mchdev_lock);
  2493. rgvswctl = I915_READ16(MEMSWCTL);
  2494. if (rgvswctl & MEMCTL_CMD_STS) {
  2495. DRM_DEBUG("gpu busy, RCS change rejected\n");
  2496. return false; /* still busy with another command */
  2497. }
  2498. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  2499. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  2500. I915_WRITE16(MEMSWCTL, rgvswctl);
  2501. POSTING_READ16(MEMSWCTL);
  2502. rgvswctl |= MEMCTL_CMD_STS;
  2503. I915_WRITE16(MEMSWCTL, rgvswctl);
  2504. return true;
  2505. }
  2506. static void ironlake_enable_drps(struct drm_device *dev)
  2507. {
  2508. struct drm_i915_private *dev_priv = dev->dev_private;
  2509. u32 rgvmodectl = I915_READ(MEMMODECTL);
  2510. u8 fmax, fmin, fstart, vstart;
  2511. spin_lock_irq(&mchdev_lock);
  2512. /* Enable temp reporting */
  2513. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  2514. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  2515. /* 100ms RC evaluation intervals */
  2516. I915_WRITE(RCUPEI, 100000);
  2517. I915_WRITE(RCDNEI, 100000);
  2518. /* Set max/min thresholds to 90ms and 80ms respectively */
  2519. I915_WRITE(RCBMAXAVG, 90000);
  2520. I915_WRITE(RCBMINAVG, 80000);
  2521. I915_WRITE(MEMIHYST, 1);
  2522. /* Set up min, max, and cur for interrupt handling */
  2523. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  2524. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  2525. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  2526. MEMMODE_FSTART_SHIFT;
  2527. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  2528. PXVFREQ_PX_SHIFT;
  2529. dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
  2530. dev_priv->ips.fstart = fstart;
  2531. dev_priv->ips.max_delay = fstart;
  2532. dev_priv->ips.min_delay = fmin;
  2533. dev_priv->ips.cur_delay = fstart;
  2534. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  2535. fmax, fmin, fstart);
  2536. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  2537. /*
  2538. * Interrupts will be enabled in ironlake_irq_postinstall
  2539. */
  2540. I915_WRITE(VIDSTART, vstart);
  2541. POSTING_READ(VIDSTART);
  2542. rgvmodectl |= MEMMODE_SWMODE_EN;
  2543. I915_WRITE(MEMMODECTL, rgvmodectl);
  2544. if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  2545. DRM_ERROR("stuck trying to change perf mode\n");
  2546. mdelay(1);
  2547. ironlake_set_drps(dev, fstart);
  2548. dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  2549. I915_READ(0x112e0);
  2550. dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
  2551. dev_priv->ips.last_count2 = I915_READ(0x112f4);
  2552. getrawmonotonic(&dev_priv->ips.last_time2);
  2553. spin_unlock_irq(&mchdev_lock);
  2554. }
  2555. static void ironlake_disable_drps(struct drm_device *dev)
  2556. {
  2557. struct drm_i915_private *dev_priv = dev->dev_private;
  2558. u16 rgvswctl;
  2559. spin_lock_irq(&mchdev_lock);
  2560. rgvswctl = I915_READ16(MEMSWCTL);
  2561. /* Ack interrupts, disable EFC interrupt */
  2562. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  2563. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  2564. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  2565. I915_WRITE(DEIIR, DE_PCU_EVENT);
  2566. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  2567. /* Go back to the starting frequency */
  2568. ironlake_set_drps(dev, dev_priv->ips.fstart);
  2569. mdelay(1);
  2570. rgvswctl |= MEMCTL_CMD_STS;
  2571. I915_WRITE(MEMSWCTL, rgvswctl);
  2572. mdelay(1);
  2573. spin_unlock_irq(&mchdev_lock);
  2574. }
  2575. /* There's a funny hw issue where the hw returns all 0 when reading from
  2576. * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
  2577. * ourselves, instead of doing a rmw cycle (which might result in us clearing
  2578. * all limits and the gpu stuck at whatever frequency it is at atm).
  2579. */
  2580. static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 *val)
  2581. {
  2582. u32 limits;
  2583. limits = 0;
  2584. if (*val >= dev_priv->rps.max_delay)
  2585. *val = dev_priv->rps.max_delay;
  2586. limits |= dev_priv->rps.max_delay << 24;
  2587. /* Only set the down limit when we've reached the lowest level to avoid
  2588. * getting more interrupts, otherwise leave this clear. This prevents a
  2589. * race in the hw when coming out of rc6: There's a tiny window where
  2590. * the hw runs at the minimal clock before selecting the desired
  2591. * frequency, if the down threshold expires in that window we will not
  2592. * receive a down interrupt. */
  2593. if (*val <= dev_priv->rps.min_delay) {
  2594. *val = dev_priv->rps.min_delay;
  2595. limits |= dev_priv->rps.min_delay << 16;
  2596. }
  2597. return limits;
  2598. }
  2599. void gen6_set_rps(struct drm_device *dev, u8 val)
  2600. {
  2601. struct drm_i915_private *dev_priv = dev->dev_private;
  2602. u32 limits = gen6_rps_limits(dev_priv, &val);
  2603. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2604. WARN_ON(val > dev_priv->rps.max_delay);
  2605. WARN_ON(val < dev_priv->rps.min_delay);
  2606. if (val == dev_priv->rps.cur_delay)
  2607. return;
  2608. if (IS_HASWELL(dev))
  2609. I915_WRITE(GEN6_RPNSWREQ,
  2610. HSW_FREQUENCY(val));
  2611. else
  2612. I915_WRITE(GEN6_RPNSWREQ,
  2613. GEN6_FREQUENCY(val) |
  2614. GEN6_OFFSET(0) |
  2615. GEN6_AGGRESSIVE_TURBO);
  2616. /* Make sure we continue to get interrupts
  2617. * until we hit the minimum or maximum frequencies.
  2618. */
  2619. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, limits);
  2620. POSTING_READ(GEN6_RPNSWREQ);
  2621. dev_priv->rps.cur_delay = val;
  2622. trace_intel_gpu_freq_change(val * 50);
  2623. }
  2624. /*
  2625. * Wait until the previous freq change has completed,
  2626. * or the timeout elapsed, and then update our notion
  2627. * of the current GPU frequency.
  2628. */
  2629. static void vlv_update_rps_cur_delay(struct drm_i915_private *dev_priv)
  2630. {
  2631. u32 pval;
  2632. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2633. if (wait_for(((pval = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS)) & GENFREQSTATUS) == 0, 10))
  2634. DRM_DEBUG_DRIVER("timed out waiting for Punit\n");
  2635. pval >>= 8;
  2636. if (pval != dev_priv->rps.cur_delay)
  2637. DRM_DEBUG_DRIVER("Punit overrode GPU freq: %d MHz (%u) requested, but got %d Mhz (%u)\n",
  2638. vlv_gpu_freq(dev_priv->mem_freq, dev_priv->rps.cur_delay),
  2639. dev_priv->rps.cur_delay,
  2640. vlv_gpu_freq(dev_priv->mem_freq, pval), pval);
  2641. dev_priv->rps.cur_delay = pval;
  2642. }
  2643. void valleyview_set_rps(struct drm_device *dev, u8 val)
  2644. {
  2645. struct drm_i915_private *dev_priv = dev->dev_private;
  2646. gen6_rps_limits(dev_priv, &val);
  2647. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2648. WARN_ON(val > dev_priv->rps.max_delay);
  2649. WARN_ON(val < dev_priv->rps.min_delay);
  2650. vlv_update_rps_cur_delay(dev_priv);
  2651. DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
  2652. vlv_gpu_freq(dev_priv->mem_freq,
  2653. dev_priv->rps.cur_delay),
  2654. dev_priv->rps.cur_delay,
  2655. vlv_gpu_freq(dev_priv->mem_freq, val), val);
  2656. if (val == dev_priv->rps.cur_delay)
  2657. return;
  2658. vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
  2659. dev_priv->rps.cur_delay = val;
  2660. trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv->mem_freq, val));
  2661. }
  2662. static void gen6_disable_rps_interrupts(struct drm_device *dev)
  2663. {
  2664. struct drm_i915_private *dev_priv = dev->dev_private;
  2665. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  2666. I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) & ~GEN6_PM_RPS_EVENTS);
  2667. /* Complete PM interrupt masking here doesn't race with the rps work
  2668. * item again unmasking PM interrupts because that is using a different
  2669. * register (PMIMR) to mask PM interrupts. The only risk is in leaving
  2670. * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
  2671. spin_lock_irq(&dev_priv->irq_lock);
  2672. dev_priv->rps.pm_iir = 0;
  2673. spin_unlock_irq(&dev_priv->irq_lock);
  2674. I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
  2675. }
  2676. static void gen6_disable_rps(struct drm_device *dev)
  2677. {
  2678. struct drm_i915_private *dev_priv = dev->dev_private;
  2679. I915_WRITE(GEN6_RC_CONTROL, 0);
  2680. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  2681. gen6_disable_rps_interrupts(dev);
  2682. }
  2683. static void valleyview_disable_rps(struct drm_device *dev)
  2684. {
  2685. struct drm_i915_private *dev_priv = dev->dev_private;
  2686. I915_WRITE(GEN6_RC_CONTROL, 0);
  2687. gen6_disable_rps_interrupts(dev);
  2688. if (dev_priv->vlv_pctx) {
  2689. drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
  2690. dev_priv->vlv_pctx = NULL;
  2691. }
  2692. }
  2693. int intel_enable_rc6(const struct drm_device *dev)
  2694. {
  2695. /* No RC6 before Ironlake */
  2696. if (INTEL_INFO(dev)->gen < 5)
  2697. return 0;
  2698. /* Respect the kernel parameter if it is set */
  2699. if (i915_enable_rc6 >= 0)
  2700. return i915_enable_rc6;
  2701. /* Disable RC6 on Ironlake */
  2702. if (INTEL_INFO(dev)->gen == 5)
  2703. return 0;
  2704. if (IS_HASWELL(dev)) {
  2705. DRM_DEBUG_DRIVER("Haswell: only RC6 available\n");
  2706. return INTEL_RC6_ENABLE;
  2707. }
  2708. /* snb/ivb have more than one rc6 state. */
  2709. if (INTEL_INFO(dev)->gen == 6) {
  2710. DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
  2711. return INTEL_RC6_ENABLE;
  2712. }
  2713. DRM_DEBUG_DRIVER("RC6 and deep RC6 enabled\n");
  2714. return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
  2715. }
  2716. static void gen6_enable_rps_interrupts(struct drm_device *dev)
  2717. {
  2718. struct drm_i915_private *dev_priv = dev->dev_private;
  2719. spin_lock_irq(&dev_priv->irq_lock);
  2720. WARN_ON(dev_priv->rps.pm_iir);
  2721. I915_WRITE(GEN6_PMIMR, I915_READ(GEN6_PMIMR) & ~GEN6_PM_RPS_EVENTS);
  2722. I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
  2723. spin_unlock_irq(&dev_priv->irq_lock);
  2724. /* unmask all PM interrupts */
  2725. I915_WRITE(GEN6_PMINTRMSK, 0);
  2726. }
  2727. static void gen6_enable_rps(struct drm_device *dev)
  2728. {
  2729. struct drm_i915_private *dev_priv = dev->dev_private;
  2730. struct intel_ring_buffer *ring;
  2731. u32 rp_state_cap;
  2732. u32 gt_perf_status;
  2733. u32 rc6vids, pcu_mbox, rc6_mask = 0;
  2734. u32 gtfifodbg;
  2735. int rc6_mode;
  2736. int i, ret;
  2737. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2738. /* Here begins a magic sequence of register writes to enable
  2739. * auto-downclocking.
  2740. *
  2741. * Perhaps there might be some value in exposing these to
  2742. * userspace...
  2743. */
  2744. I915_WRITE(GEN6_RC_STATE, 0);
  2745. /* Clear the DBG now so we don't confuse earlier errors */
  2746. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  2747. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  2748. I915_WRITE(GTFIFODBG, gtfifodbg);
  2749. }
  2750. gen6_gt_force_wake_get(dev_priv);
  2751. rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  2752. gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  2753. /* In units of 50MHz */
  2754. dev_priv->rps.hw_max = dev_priv->rps.max_delay = rp_state_cap & 0xff;
  2755. dev_priv->rps.min_delay = (rp_state_cap & 0xff0000) >> 16;
  2756. dev_priv->rps.cur_delay = 0;
  2757. /* disable the counters and set deterministic thresholds */
  2758. I915_WRITE(GEN6_RC_CONTROL, 0);
  2759. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  2760. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  2761. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  2762. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  2763. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  2764. for_each_ring(ring, dev_priv, i)
  2765. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  2766. I915_WRITE(GEN6_RC_SLEEP, 0);
  2767. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  2768. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  2769. I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
  2770. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  2771. /* Check if we are enabling RC6 */
  2772. rc6_mode = intel_enable_rc6(dev_priv->dev);
  2773. if (rc6_mode & INTEL_RC6_ENABLE)
  2774. rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
  2775. /* We don't use those on Haswell */
  2776. if (!IS_HASWELL(dev)) {
  2777. if (rc6_mode & INTEL_RC6p_ENABLE)
  2778. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  2779. if (rc6_mode & INTEL_RC6pp_ENABLE)
  2780. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  2781. }
  2782. DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
  2783. (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
  2784. (rc6_mask & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
  2785. (rc6_mask & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
  2786. I915_WRITE(GEN6_RC_CONTROL,
  2787. rc6_mask |
  2788. GEN6_RC_CTL_EI_MODE(1) |
  2789. GEN6_RC_CTL_HW_ENABLE);
  2790. if (IS_HASWELL(dev)) {
  2791. I915_WRITE(GEN6_RPNSWREQ,
  2792. HSW_FREQUENCY(10));
  2793. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  2794. HSW_FREQUENCY(12));
  2795. } else {
  2796. I915_WRITE(GEN6_RPNSWREQ,
  2797. GEN6_FREQUENCY(10) |
  2798. GEN6_OFFSET(0) |
  2799. GEN6_AGGRESSIVE_TURBO);
  2800. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  2801. GEN6_FREQUENCY(12));
  2802. }
  2803. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  2804. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  2805. dev_priv->rps.max_delay << 24 |
  2806. dev_priv->rps.min_delay << 16);
  2807. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  2808. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  2809. I915_WRITE(GEN6_RP_UP_EI, 66000);
  2810. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  2811. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  2812. I915_WRITE(GEN6_RP_CONTROL,
  2813. GEN6_RP_MEDIA_TURBO |
  2814. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  2815. GEN6_RP_MEDIA_IS_GFX |
  2816. GEN6_RP_ENABLE |
  2817. GEN6_RP_UP_BUSY_AVG |
  2818. (IS_HASWELL(dev) ? GEN7_RP_DOWN_IDLE_AVG : GEN6_RP_DOWN_IDLE_CONT));
  2819. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
  2820. if (!ret) {
  2821. pcu_mbox = 0;
  2822. ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
  2823. if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
  2824. DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
  2825. (dev_priv->rps.max_delay & 0xff) * 50,
  2826. (pcu_mbox & 0xff) * 50);
  2827. dev_priv->rps.hw_max = pcu_mbox & 0xff;
  2828. }
  2829. } else {
  2830. DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
  2831. }
  2832. gen6_set_rps(dev_priv->dev, (gt_perf_status & 0xff00) >> 8);
  2833. gen6_enable_rps_interrupts(dev);
  2834. rc6vids = 0;
  2835. ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
  2836. if (IS_GEN6(dev) && ret) {
  2837. DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
  2838. } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
  2839. DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
  2840. GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
  2841. rc6vids &= 0xffff00;
  2842. rc6vids |= GEN6_ENCODE_RC6_VID(450);
  2843. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
  2844. if (ret)
  2845. DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
  2846. }
  2847. gen6_gt_force_wake_put(dev_priv);
  2848. }
  2849. static void gen6_update_ring_freq(struct drm_device *dev)
  2850. {
  2851. struct drm_i915_private *dev_priv = dev->dev_private;
  2852. int min_freq = 15;
  2853. unsigned int gpu_freq;
  2854. unsigned int max_ia_freq, min_ring_freq;
  2855. int scaling_factor = 180;
  2856. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2857. max_ia_freq = cpufreq_quick_get_max(0);
  2858. /*
  2859. * Default to measured freq if none found, PCU will ensure we don't go
  2860. * over
  2861. */
  2862. if (!max_ia_freq)
  2863. max_ia_freq = tsc_khz;
  2864. /* Convert from kHz to MHz */
  2865. max_ia_freq /= 1000;
  2866. min_ring_freq = I915_READ(MCHBAR_MIRROR_BASE_SNB + DCLK);
  2867. /* convert DDR frequency from units of 133.3MHz to bandwidth */
  2868. min_ring_freq = (2 * 4 * min_ring_freq + 2) / 3;
  2869. /*
  2870. * For each potential GPU frequency, load a ring frequency we'd like
  2871. * to use for memory access. We do this by specifying the IA frequency
  2872. * the PCU should use as a reference to determine the ring frequency.
  2873. */
  2874. for (gpu_freq = dev_priv->rps.max_delay; gpu_freq >= dev_priv->rps.min_delay;
  2875. gpu_freq--) {
  2876. int diff = dev_priv->rps.max_delay - gpu_freq;
  2877. unsigned int ia_freq = 0, ring_freq = 0;
  2878. if (IS_HASWELL(dev)) {
  2879. ring_freq = (gpu_freq * 5 + 3) / 4;
  2880. ring_freq = max(min_ring_freq, ring_freq);
  2881. /* leave ia_freq as the default, chosen by cpufreq */
  2882. } else {
  2883. /* On older processors, there is no separate ring
  2884. * clock domain, so in order to boost the bandwidth
  2885. * of the ring, we need to upclock the CPU (ia_freq).
  2886. *
  2887. * For GPU frequencies less than 750MHz,
  2888. * just use the lowest ring freq.
  2889. */
  2890. if (gpu_freq < min_freq)
  2891. ia_freq = 800;
  2892. else
  2893. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  2894. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  2895. }
  2896. sandybridge_pcode_write(dev_priv,
  2897. GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
  2898. ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
  2899. ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
  2900. gpu_freq);
  2901. }
  2902. }
  2903. int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
  2904. {
  2905. u32 val, rp0;
  2906. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  2907. rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
  2908. /* Clamp to max */
  2909. rp0 = min_t(u32, rp0, 0xea);
  2910. return rp0;
  2911. }
  2912. static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  2913. {
  2914. u32 val, rpe;
  2915. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
  2916. rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
  2917. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
  2918. rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
  2919. return rpe;
  2920. }
  2921. int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
  2922. {
  2923. return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
  2924. }
  2925. static void vlv_rps_timer_work(struct work_struct *work)
  2926. {
  2927. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  2928. rps.vlv_work.work);
  2929. /*
  2930. * Timer fired, we must be idle. Drop to min voltage state.
  2931. * Note: we use RPe here since it should match the
  2932. * Vmin we were shooting for. That should give us better
  2933. * perf when we come back out of RC6 than if we used the
  2934. * min freq available.
  2935. */
  2936. mutex_lock(&dev_priv->rps.hw_lock);
  2937. if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
  2938. valleyview_set_rps(dev_priv->dev, dev_priv->rps.rpe_delay);
  2939. mutex_unlock(&dev_priv->rps.hw_lock);
  2940. }
  2941. static void valleyview_setup_pctx(struct drm_device *dev)
  2942. {
  2943. struct drm_i915_private *dev_priv = dev->dev_private;
  2944. struct drm_i915_gem_object *pctx;
  2945. unsigned long pctx_paddr;
  2946. u32 pcbr;
  2947. int pctx_size = 24*1024;
  2948. pcbr = I915_READ(VLV_PCBR);
  2949. if (pcbr) {
  2950. /* BIOS set it up already, grab the pre-alloc'd space */
  2951. int pcbr_offset;
  2952. pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
  2953. pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
  2954. pcbr_offset,
  2955. I915_GTT_OFFSET_NONE,
  2956. pctx_size);
  2957. goto out;
  2958. }
  2959. /*
  2960. * From the Gunit register HAS:
  2961. * The Gfx driver is expected to program this register and ensure
  2962. * proper allocation within Gfx stolen memory. For example, this
  2963. * register should be programmed such than the PCBR range does not
  2964. * overlap with other ranges, such as the frame buffer, protected
  2965. * memory, or any other relevant ranges.
  2966. */
  2967. pctx = i915_gem_object_create_stolen(dev, pctx_size);
  2968. if (!pctx) {
  2969. DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
  2970. return;
  2971. }
  2972. pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
  2973. I915_WRITE(VLV_PCBR, pctx_paddr);
  2974. out:
  2975. dev_priv->vlv_pctx = pctx;
  2976. }
  2977. static void valleyview_enable_rps(struct drm_device *dev)
  2978. {
  2979. struct drm_i915_private *dev_priv = dev->dev_private;
  2980. struct intel_ring_buffer *ring;
  2981. u32 gtfifodbg, val;
  2982. int i;
  2983. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2984. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  2985. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  2986. I915_WRITE(GTFIFODBG, gtfifodbg);
  2987. }
  2988. valleyview_setup_pctx(dev);
  2989. gen6_gt_force_wake_get(dev_priv);
  2990. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  2991. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  2992. I915_WRITE(GEN6_RP_UP_EI, 66000);
  2993. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  2994. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  2995. I915_WRITE(GEN6_RP_CONTROL,
  2996. GEN6_RP_MEDIA_TURBO |
  2997. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  2998. GEN6_RP_MEDIA_IS_GFX |
  2999. GEN6_RP_ENABLE |
  3000. GEN6_RP_UP_BUSY_AVG |
  3001. GEN6_RP_DOWN_IDLE_CONT);
  3002. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
  3003. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  3004. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  3005. for_each_ring(ring, dev_priv, i)
  3006. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  3007. I915_WRITE(GEN6_RC6_THRESHOLD, 0xc350);
  3008. /* allows RC6 residency counter to work */
  3009. I915_WRITE(0x138104, _MASKED_BIT_ENABLE(0x3));
  3010. I915_WRITE(GEN6_RC_CONTROL,
  3011. GEN7_RC_CTL_TO_MODE);
  3012. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  3013. switch ((val >> 6) & 3) {
  3014. case 0:
  3015. case 1:
  3016. dev_priv->mem_freq = 800;
  3017. break;
  3018. case 2:
  3019. dev_priv->mem_freq = 1066;
  3020. break;
  3021. case 3:
  3022. dev_priv->mem_freq = 1333;
  3023. break;
  3024. }
  3025. DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
  3026. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
  3027. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  3028. dev_priv->rps.cur_delay = (val >> 8) & 0xff;
  3029. DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
  3030. vlv_gpu_freq(dev_priv->mem_freq,
  3031. dev_priv->rps.cur_delay),
  3032. dev_priv->rps.cur_delay);
  3033. dev_priv->rps.max_delay = valleyview_rps_max_freq(dev_priv);
  3034. dev_priv->rps.hw_max = dev_priv->rps.max_delay;
  3035. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  3036. vlv_gpu_freq(dev_priv->mem_freq,
  3037. dev_priv->rps.max_delay),
  3038. dev_priv->rps.max_delay);
  3039. dev_priv->rps.rpe_delay = valleyview_rps_rpe_freq(dev_priv);
  3040. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  3041. vlv_gpu_freq(dev_priv->mem_freq,
  3042. dev_priv->rps.rpe_delay),
  3043. dev_priv->rps.rpe_delay);
  3044. dev_priv->rps.min_delay = valleyview_rps_min_freq(dev_priv);
  3045. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  3046. vlv_gpu_freq(dev_priv->mem_freq,
  3047. dev_priv->rps.min_delay),
  3048. dev_priv->rps.min_delay);
  3049. DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
  3050. vlv_gpu_freq(dev_priv->mem_freq,
  3051. dev_priv->rps.rpe_delay),
  3052. dev_priv->rps.rpe_delay);
  3053. INIT_DELAYED_WORK(&dev_priv->rps.vlv_work, vlv_rps_timer_work);
  3054. valleyview_set_rps(dev_priv->dev, dev_priv->rps.rpe_delay);
  3055. gen6_enable_rps_interrupts(dev);
  3056. gen6_gt_force_wake_put(dev_priv);
  3057. }
  3058. void ironlake_teardown_rc6(struct drm_device *dev)
  3059. {
  3060. struct drm_i915_private *dev_priv = dev->dev_private;
  3061. if (dev_priv->ips.renderctx) {
  3062. i915_gem_object_unpin(dev_priv->ips.renderctx);
  3063. drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
  3064. dev_priv->ips.renderctx = NULL;
  3065. }
  3066. if (dev_priv->ips.pwrctx) {
  3067. i915_gem_object_unpin(dev_priv->ips.pwrctx);
  3068. drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
  3069. dev_priv->ips.pwrctx = NULL;
  3070. }
  3071. }
  3072. static void ironlake_disable_rc6(struct drm_device *dev)
  3073. {
  3074. struct drm_i915_private *dev_priv = dev->dev_private;
  3075. if (I915_READ(PWRCTXA)) {
  3076. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  3077. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  3078. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  3079. 50);
  3080. I915_WRITE(PWRCTXA, 0);
  3081. POSTING_READ(PWRCTXA);
  3082. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  3083. POSTING_READ(RSTDBYCTL);
  3084. }
  3085. }
  3086. static int ironlake_setup_rc6(struct drm_device *dev)
  3087. {
  3088. struct drm_i915_private *dev_priv = dev->dev_private;
  3089. if (dev_priv->ips.renderctx == NULL)
  3090. dev_priv->ips.renderctx = intel_alloc_context_page(dev);
  3091. if (!dev_priv->ips.renderctx)
  3092. return -ENOMEM;
  3093. if (dev_priv->ips.pwrctx == NULL)
  3094. dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
  3095. if (!dev_priv->ips.pwrctx) {
  3096. ironlake_teardown_rc6(dev);
  3097. return -ENOMEM;
  3098. }
  3099. return 0;
  3100. }
  3101. static void ironlake_enable_rc6(struct drm_device *dev)
  3102. {
  3103. struct drm_i915_private *dev_priv = dev->dev_private;
  3104. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  3105. bool was_interruptible;
  3106. int ret;
  3107. /* rc6 disabled by default due to repeated reports of hanging during
  3108. * boot and resume.
  3109. */
  3110. if (!intel_enable_rc6(dev))
  3111. return;
  3112. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  3113. ret = ironlake_setup_rc6(dev);
  3114. if (ret)
  3115. return;
  3116. was_interruptible = dev_priv->mm.interruptible;
  3117. dev_priv->mm.interruptible = false;
  3118. /*
  3119. * GPU can automatically power down the render unit if given a page
  3120. * to save state.
  3121. */
  3122. ret = intel_ring_begin(ring, 6);
  3123. if (ret) {
  3124. ironlake_teardown_rc6(dev);
  3125. dev_priv->mm.interruptible = was_interruptible;
  3126. return;
  3127. }
  3128. intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  3129. intel_ring_emit(ring, MI_SET_CONTEXT);
  3130. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
  3131. MI_MM_SPACE_GTT |
  3132. MI_SAVE_EXT_STATE_EN |
  3133. MI_RESTORE_EXT_STATE_EN |
  3134. MI_RESTORE_INHIBIT);
  3135. intel_ring_emit(ring, MI_SUSPEND_FLUSH);
  3136. intel_ring_emit(ring, MI_NOOP);
  3137. intel_ring_emit(ring, MI_FLUSH);
  3138. intel_ring_advance(ring);
  3139. /*
  3140. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  3141. * does an implicit flush, combined with MI_FLUSH above, it should be
  3142. * safe to assume that renderctx is valid
  3143. */
  3144. ret = intel_ring_idle(ring);
  3145. dev_priv->mm.interruptible = was_interruptible;
  3146. if (ret) {
  3147. DRM_ERROR("failed to enable ironlake power savings\n");
  3148. ironlake_teardown_rc6(dev);
  3149. return;
  3150. }
  3151. I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
  3152. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  3153. }
  3154. static unsigned long intel_pxfreq(u32 vidfreq)
  3155. {
  3156. unsigned long freq;
  3157. int div = (vidfreq & 0x3f0000) >> 16;
  3158. int post = (vidfreq & 0x3000) >> 12;
  3159. int pre = (vidfreq & 0x7);
  3160. if (!pre)
  3161. return 0;
  3162. freq = ((div * 133333) / ((1<<post) * pre));
  3163. return freq;
  3164. }
  3165. static const struct cparams {
  3166. u16 i;
  3167. u16 t;
  3168. u16 m;
  3169. u16 c;
  3170. } cparams[] = {
  3171. { 1, 1333, 301, 28664 },
  3172. { 1, 1066, 294, 24460 },
  3173. { 1, 800, 294, 25192 },
  3174. { 0, 1333, 276, 27605 },
  3175. { 0, 1066, 276, 27605 },
  3176. { 0, 800, 231, 23784 },
  3177. };
  3178. static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
  3179. {
  3180. u64 total_count, diff, ret;
  3181. u32 count1, count2, count3, m = 0, c = 0;
  3182. unsigned long now = jiffies_to_msecs(jiffies), diff1;
  3183. int i;
  3184. assert_spin_locked(&mchdev_lock);
  3185. diff1 = now - dev_priv->ips.last_time1;
  3186. /* Prevent division-by-zero if we are asking too fast.
  3187. * Also, we don't get interesting results if we are polling
  3188. * faster than once in 10ms, so just return the saved value
  3189. * in such cases.
  3190. */
  3191. if (diff1 <= 10)
  3192. return dev_priv->ips.chipset_power;
  3193. count1 = I915_READ(DMIEC);
  3194. count2 = I915_READ(DDREC);
  3195. count3 = I915_READ(CSIEC);
  3196. total_count = count1 + count2 + count3;
  3197. /* FIXME: handle per-counter overflow */
  3198. if (total_count < dev_priv->ips.last_count1) {
  3199. diff = ~0UL - dev_priv->ips.last_count1;
  3200. diff += total_count;
  3201. } else {
  3202. diff = total_count - dev_priv->ips.last_count1;
  3203. }
  3204. for (i = 0; i < ARRAY_SIZE(cparams); i++) {
  3205. if (cparams[i].i == dev_priv->ips.c_m &&
  3206. cparams[i].t == dev_priv->ips.r_t) {
  3207. m = cparams[i].m;
  3208. c = cparams[i].c;
  3209. break;
  3210. }
  3211. }
  3212. diff = div_u64(diff, diff1);
  3213. ret = ((m * diff) + c);
  3214. ret = div_u64(ret, 10);
  3215. dev_priv->ips.last_count1 = total_count;
  3216. dev_priv->ips.last_time1 = now;
  3217. dev_priv->ips.chipset_power = ret;
  3218. return ret;
  3219. }
  3220. unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
  3221. {
  3222. unsigned long val;
  3223. if (dev_priv->info->gen != 5)
  3224. return 0;
  3225. spin_lock_irq(&mchdev_lock);
  3226. val = __i915_chipset_val(dev_priv);
  3227. spin_unlock_irq(&mchdev_lock);
  3228. return val;
  3229. }
  3230. unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
  3231. {
  3232. unsigned long m, x, b;
  3233. u32 tsfs;
  3234. tsfs = I915_READ(TSFS);
  3235. m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
  3236. x = I915_READ8(TR1);
  3237. b = tsfs & TSFS_INTR_MASK;
  3238. return ((m * x) / 127) - b;
  3239. }
  3240. static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
  3241. {
  3242. static const struct v_table {
  3243. u16 vd; /* in .1 mil */
  3244. u16 vm; /* in .1 mil */
  3245. } v_table[] = {
  3246. { 0, 0, },
  3247. { 375, 0, },
  3248. { 500, 0, },
  3249. { 625, 0, },
  3250. { 750, 0, },
  3251. { 875, 0, },
  3252. { 1000, 0, },
  3253. { 1125, 0, },
  3254. { 4125, 3000, },
  3255. { 4125, 3000, },
  3256. { 4125, 3000, },
  3257. { 4125, 3000, },
  3258. { 4125, 3000, },
  3259. { 4125, 3000, },
  3260. { 4125, 3000, },
  3261. { 4125, 3000, },
  3262. { 4125, 3000, },
  3263. { 4125, 3000, },
  3264. { 4125, 3000, },
  3265. { 4125, 3000, },
  3266. { 4125, 3000, },
  3267. { 4125, 3000, },
  3268. { 4125, 3000, },
  3269. { 4125, 3000, },
  3270. { 4125, 3000, },
  3271. { 4125, 3000, },
  3272. { 4125, 3000, },
  3273. { 4125, 3000, },
  3274. { 4125, 3000, },
  3275. { 4125, 3000, },
  3276. { 4125, 3000, },
  3277. { 4125, 3000, },
  3278. { 4250, 3125, },
  3279. { 4375, 3250, },
  3280. { 4500, 3375, },
  3281. { 4625, 3500, },
  3282. { 4750, 3625, },
  3283. { 4875, 3750, },
  3284. { 5000, 3875, },
  3285. { 5125, 4000, },
  3286. { 5250, 4125, },
  3287. { 5375, 4250, },
  3288. { 5500, 4375, },
  3289. { 5625, 4500, },
  3290. { 5750, 4625, },
  3291. { 5875, 4750, },
  3292. { 6000, 4875, },
  3293. { 6125, 5000, },
  3294. { 6250, 5125, },
  3295. { 6375, 5250, },
  3296. { 6500, 5375, },
  3297. { 6625, 5500, },
  3298. { 6750, 5625, },
  3299. { 6875, 5750, },
  3300. { 7000, 5875, },
  3301. { 7125, 6000, },
  3302. { 7250, 6125, },
  3303. { 7375, 6250, },
  3304. { 7500, 6375, },
  3305. { 7625, 6500, },
  3306. { 7750, 6625, },
  3307. { 7875, 6750, },
  3308. { 8000, 6875, },
  3309. { 8125, 7000, },
  3310. { 8250, 7125, },
  3311. { 8375, 7250, },
  3312. { 8500, 7375, },
  3313. { 8625, 7500, },
  3314. { 8750, 7625, },
  3315. { 8875, 7750, },
  3316. { 9000, 7875, },
  3317. { 9125, 8000, },
  3318. { 9250, 8125, },
  3319. { 9375, 8250, },
  3320. { 9500, 8375, },
  3321. { 9625, 8500, },
  3322. { 9750, 8625, },
  3323. { 9875, 8750, },
  3324. { 10000, 8875, },
  3325. { 10125, 9000, },
  3326. { 10250, 9125, },
  3327. { 10375, 9250, },
  3328. { 10500, 9375, },
  3329. { 10625, 9500, },
  3330. { 10750, 9625, },
  3331. { 10875, 9750, },
  3332. { 11000, 9875, },
  3333. { 11125, 10000, },
  3334. { 11250, 10125, },
  3335. { 11375, 10250, },
  3336. { 11500, 10375, },
  3337. { 11625, 10500, },
  3338. { 11750, 10625, },
  3339. { 11875, 10750, },
  3340. { 12000, 10875, },
  3341. { 12125, 11000, },
  3342. { 12250, 11125, },
  3343. { 12375, 11250, },
  3344. { 12500, 11375, },
  3345. { 12625, 11500, },
  3346. { 12750, 11625, },
  3347. { 12875, 11750, },
  3348. { 13000, 11875, },
  3349. { 13125, 12000, },
  3350. { 13250, 12125, },
  3351. { 13375, 12250, },
  3352. { 13500, 12375, },
  3353. { 13625, 12500, },
  3354. { 13750, 12625, },
  3355. { 13875, 12750, },
  3356. { 14000, 12875, },
  3357. { 14125, 13000, },
  3358. { 14250, 13125, },
  3359. { 14375, 13250, },
  3360. { 14500, 13375, },
  3361. { 14625, 13500, },
  3362. { 14750, 13625, },
  3363. { 14875, 13750, },
  3364. { 15000, 13875, },
  3365. { 15125, 14000, },
  3366. { 15250, 14125, },
  3367. { 15375, 14250, },
  3368. { 15500, 14375, },
  3369. { 15625, 14500, },
  3370. { 15750, 14625, },
  3371. { 15875, 14750, },
  3372. { 16000, 14875, },
  3373. { 16125, 15000, },
  3374. };
  3375. if (dev_priv->info->is_mobile)
  3376. return v_table[pxvid].vm;
  3377. else
  3378. return v_table[pxvid].vd;
  3379. }
  3380. static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
  3381. {
  3382. struct timespec now, diff1;
  3383. u64 diff;
  3384. unsigned long diffms;
  3385. u32 count;
  3386. assert_spin_locked(&mchdev_lock);
  3387. getrawmonotonic(&now);
  3388. diff1 = timespec_sub(now, dev_priv->ips.last_time2);
  3389. /* Don't divide by 0 */
  3390. diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
  3391. if (!diffms)
  3392. return;
  3393. count = I915_READ(GFXEC);
  3394. if (count < dev_priv->ips.last_count2) {
  3395. diff = ~0UL - dev_priv->ips.last_count2;
  3396. diff += count;
  3397. } else {
  3398. diff = count - dev_priv->ips.last_count2;
  3399. }
  3400. dev_priv->ips.last_count2 = count;
  3401. dev_priv->ips.last_time2 = now;
  3402. /* More magic constants... */
  3403. diff = diff * 1181;
  3404. diff = div_u64(diff, diffms * 10);
  3405. dev_priv->ips.gfx_power = diff;
  3406. }
  3407. void i915_update_gfx_val(struct drm_i915_private *dev_priv)
  3408. {
  3409. if (dev_priv->info->gen != 5)
  3410. return;
  3411. spin_lock_irq(&mchdev_lock);
  3412. __i915_update_gfx_val(dev_priv);
  3413. spin_unlock_irq(&mchdev_lock);
  3414. }
  3415. static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
  3416. {
  3417. unsigned long t, corr, state1, corr2, state2;
  3418. u32 pxvid, ext_v;
  3419. assert_spin_locked(&mchdev_lock);
  3420. pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_delay * 4));
  3421. pxvid = (pxvid >> 24) & 0x7f;
  3422. ext_v = pvid_to_extvid(dev_priv, pxvid);
  3423. state1 = ext_v;
  3424. t = i915_mch_val(dev_priv);
  3425. /* Revel in the empirically derived constants */
  3426. /* Correction factor in 1/100000 units */
  3427. if (t > 80)
  3428. corr = ((t * 2349) + 135940);
  3429. else if (t >= 50)
  3430. corr = ((t * 964) + 29317);
  3431. else /* < 50 */
  3432. corr = ((t * 301) + 1004);
  3433. corr = corr * ((150142 * state1) / 10000 - 78642);
  3434. corr /= 100000;
  3435. corr2 = (corr * dev_priv->ips.corr);
  3436. state2 = (corr2 * state1) / 10000;
  3437. state2 /= 100; /* convert to mW */
  3438. __i915_update_gfx_val(dev_priv);
  3439. return dev_priv->ips.gfx_power + state2;
  3440. }
  3441. unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
  3442. {
  3443. unsigned long val;
  3444. if (dev_priv->info->gen != 5)
  3445. return 0;
  3446. spin_lock_irq(&mchdev_lock);
  3447. val = __i915_gfx_val(dev_priv);
  3448. spin_unlock_irq(&mchdev_lock);
  3449. return val;
  3450. }
  3451. /**
  3452. * i915_read_mch_val - return value for IPS use
  3453. *
  3454. * Calculate and return a value for the IPS driver to use when deciding whether
  3455. * we have thermal and power headroom to increase CPU or GPU power budget.
  3456. */
  3457. unsigned long i915_read_mch_val(void)
  3458. {
  3459. struct drm_i915_private *dev_priv;
  3460. unsigned long chipset_val, graphics_val, ret = 0;
  3461. spin_lock_irq(&mchdev_lock);
  3462. if (!i915_mch_dev)
  3463. goto out_unlock;
  3464. dev_priv = i915_mch_dev;
  3465. chipset_val = __i915_chipset_val(dev_priv);
  3466. graphics_val = __i915_gfx_val(dev_priv);
  3467. ret = chipset_val + graphics_val;
  3468. out_unlock:
  3469. spin_unlock_irq(&mchdev_lock);
  3470. return ret;
  3471. }
  3472. EXPORT_SYMBOL_GPL(i915_read_mch_val);
  3473. /**
  3474. * i915_gpu_raise - raise GPU frequency limit
  3475. *
  3476. * Raise the limit; IPS indicates we have thermal headroom.
  3477. */
  3478. bool i915_gpu_raise(void)
  3479. {
  3480. struct drm_i915_private *dev_priv;
  3481. bool ret = true;
  3482. spin_lock_irq(&mchdev_lock);
  3483. if (!i915_mch_dev) {
  3484. ret = false;
  3485. goto out_unlock;
  3486. }
  3487. dev_priv = i915_mch_dev;
  3488. if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
  3489. dev_priv->ips.max_delay--;
  3490. out_unlock:
  3491. spin_unlock_irq(&mchdev_lock);
  3492. return ret;
  3493. }
  3494. EXPORT_SYMBOL_GPL(i915_gpu_raise);
  3495. /**
  3496. * i915_gpu_lower - lower GPU frequency limit
  3497. *
  3498. * IPS indicates we're close to a thermal limit, so throttle back the GPU
  3499. * frequency maximum.
  3500. */
  3501. bool i915_gpu_lower(void)
  3502. {
  3503. struct drm_i915_private *dev_priv;
  3504. bool ret = true;
  3505. spin_lock_irq(&mchdev_lock);
  3506. if (!i915_mch_dev) {
  3507. ret = false;
  3508. goto out_unlock;
  3509. }
  3510. dev_priv = i915_mch_dev;
  3511. if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
  3512. dev_priv->ips.max_delay++;
  3513. out_unlock:
  3514. spin_unlock_irq(&mchdev_lock);
  3515. return ret;
  3516. }
  3517. EXPORT_SYMBOL_GPL(i915_gpu_lower);
  3518. /**
  3519. * i915_gpu_busy - indicate GPU business to IPS
  3520. *
  3521. * Tell the IPS driver whether or not the GPU is busy.
  3522. */
  3523. bool i915_gpu_busy(void)
  3524. {
  3525. struct drm_i915_private *dev_priv;
  3526. struct intel_ring_buffer *ring;
  3527. bool ret = false;
  3528. int i;
  3529. spin_lock_irq(&mchdev_lock);
  3530. if (!i915_mch_dev)
  3531. goto out_unlock;
  3532. dev_priv = i915_mch_dev;
  3533. for_each_ring(ring, dev_priv, i)
  3534. ret |= !list_empty(&ring->request_list);
  3535. out_unlock:
  3536. spin_unlock_irq(&mchdev_lock);
  3537. return ret;
  3538. }
  3539. EXPORT_SYMBOL_GPL(i915_gpu_busy);
  3540. /**
  3541. * i915_gpu_turbo_disable - disable graphics turbo
  3542. *
  3543. * Disable graphics turbo by resetting the max frequency and setting the
  3544. * current frequency to the default.
  3545. */
  3546. bool i915_gpu_turbo_disable(void)
  3547. {
  3548. struct drm_i915_private *dev_priv;
  3549. bool ret = true;
  3550. spin_lock_irq(&mchdev_lock);
  3551. if (!i915_mch_dev) {
  3552. ret = false;
  3553. goto out_unlock;
  3554. }
  3555. dev_priv = i915_mch_dev;
  3556. dev_priv->ips.max_delay = dev_priv->ips.fstart;
  3557. if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
  3558. ret = false;
  3559. out_unlock:
  3560. spin_unlock_irq(&mchdev_lock);
  3561. return ret;
  3562. }
  3563. EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
  3564. /**
  3565. * Tells the intel_ips driver that the i915 driver is now loaded, if
  3566. * IPS got loaded first.
  3567. *
  3568. * This awkward dance is so that neither module has to depend on the
  3569. * other in order for IPS to do the appropriate communication of
  3570. * GPU turbo limits to i915.
  3571. */
  3572. static void
  3573. ips_ping_for_i915_load(void)
  3574. {
  3575. void (*link)(void);
  3576. link = symbol_get(ips_link_to_i915_driver);
  3577. if (link) {
  3578. link();
  3579. symbol_put(ips_link_to_i915_driver);
  3580. }
  3581. }
  3582. void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
  3583. {
  3584. /* We only register the i915 ips part with intel-ips once everything is
  3585. * set up, to avoid intel-ips sneaking in and reading bogus values. */
  3586. spin_lock_irq(&mchdev_lock);
  3587. i915_mch_dev = dev_priv;
  3588. spin_unlock_irq(&mchdev_lock);
  3589. ips_ping_for_i915_load();
  3590. }
  3591. void intel_gpu_ips_teardown(void)
  3592. {
  3593. spin_lock_irq(&mchdev_lock);
  3594. i915_mch_dev = NULL;
  3595. spin_unlock_irq(&mchdev_lock);
  3596. }
  3597. static void intel_init_emon(struct drm_device *dev)
  3598. {
  3599. struct drm_i915_private *dev_priv = dev->dev_private;
  3600. u32 lcfuse;
  3601. u8 pxw[16];
  3602. int i;
  3603. /* Disable to program */
  3604. I915_WRITE(ECR, 0);
  3605. POSTING_READ(ECR);
  3606. /* Program energy weights for various events */
  3607. I915_WRITE(SDEW, 0x15040d00);
  3608. I915_WRITE(CSIEW0, 0x007f0000);
  3609. I915_WRITE(CSIEW1, 0x1e220004);
  3610. I915_WRITE(CSIEW2, 0x04000004);
  3611. for (i = 0; i < 5; i++)
  3612. I915_WRITE(PEW + (i * 4), 0);
  3613. for (i = 0; i < 3; i++)
  3614. I915_WRITE(DEW + (i * 4), 0);
  3615. /* Program P-state weights to account for frequency power adjustment */
  3616. for (i = 0; i < 16; i++) {
  3617. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  3618. unsigned long freq = intel_pxfreq(pxvidfreq);
  3619. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  3620. PXVFREQ_PX_SHIFT;
  3621. unsigned long val;
  3622. val = vid * vid;
  3623. val *= (freq / 1000);
  3624. val *= 255;
  3625. val /= (127*127*900);
  3626. if (val > 0xff)
  3627. DRM_ERROR("bad pxval: %ld\n", val);
  3628. pxw[i] = val;
  3629. }
  3630. /* Render standby states get 0 weight */
  3631. pxw[14] = 0;
  3632. pxw[15] = 0;
  3633. for (i = 0; i < 4; i++) {
  3634. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  3635. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  3636. I915_WRITE(PXW + (i * 4), val);
  3637. }
  3638. /* Adjust magic regs to magic values (more experimental results) */
  3639. I915_WRITE(OGW0, 0);
  3640. I915_WRITE(OGW1, 0);
  3641. I915_WRITE(EG0, 0x00007f00);
  3642. I915_WRITE(EG1, 0x0000000e);
  3643. I915_WRITE(EG2, 0x000e0000);
  3644. I915_WRITE(EG3, 0x68000300);
  3645. I915_WRITE(EG4, 0x42000000);
  3646. I915_WRITE(EG5, 0x00140031);
  3647. I915_WRITE(EG6, 0);
  3648. I915_WRITE(EG7, 0);
  3649. for (i = 0; i < 8; i++)
  3650. I915_WRITE(PXWL + (i * 4), 0);
  3651. /* Enable PMON + select events */
  3652. I915_WRITE(ECR, 0x80000019);
  3653. lcfuse = I915_READ(LCFUSE02);
  3654. dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
  3655. }
  3656. void intel_disable_gt_powersave(struct drm_device *dev)
  3657. {
  3658. struct drm_i915_private *dev_priv = dev->dev_private;
  3659. /* Interrupts should be disabled already to avoid re-arming. */
  3660. WARN_ON(dev->irq_enabled);
  3661. if (IS_IRONLAKE_M(dev)) {
  3662. ironlake_disable_drps(dev);
  3663. ironlake_disable_rc6(dev);
  3664. } else if (INTEL_INFO(dev)->gen >= 6) {
  3665. cancel_delayed_work_sync(&dev_priv->rps.delayed_resume_work);
  3666. cancel_work_sync(&dev_priv->rps.work);
  3667. if (IS_VALLEYVIEW(dev))
  3668. cancel_delayed_work_sync(&dev_priv->rps.vlv_work);
  3669. mutex_lock(&dev_priv->rps.hw_lock);
  3670. if (IS_VALLEYVIEW(dev))
  3671. valleyview_disable_rps(dev);
  3672. else
  3673. gen6_disable_rps(dev);
  3674. mutex_unlock(&dev_priv->rps.hw_lock);
  3675. }
  3676. }
  3677. static void intel_gen6_powersave_work(struct work_struct *work)
  3678. {
  3679. struct drm_i915_private *dev_priv =
  3680. container_of(work, struct drm_i915_private,
  3681. rps.delayed_resume_work.work);
  3682. struct drm_device *dev = dev_priv->dev;
  3683. mutex_lock(&dev_priv->rps.hw_lock);
  3684. if (IS_VALLEYVIEW(dev)) {
  3685. valleyview_enable_rps(dev);
  3686. } else {
  3687. gen6_enable_rps(dev);
  3688. gen6_update_ring_freq(dev);
  3689. }
  3690. mutex_unlock(&dev_priv->rps.hw_lock);
  3691. }
  3692. void intel_enable_gt_powersave(struct drm_device *dev)
  3693. {
  3694. struct drm_i915_private *dev_priv = dev->dev_private;
  3695. if (IS_IRONLAKE_M(dev)) {
  3696. ironlake_enable_drps(dev);
  3697. ironlake_enable_rc6(dev);
  3698. intel_init_emon(dev);
  3699. } else if (IS_GEN6(dev) || IS_GEN7(dev)) {
  3700. /*
  3701. * PCU communication is slow and this doesn't need to be
  3702. * done at any specific time, so do this out of our fast path
  3703. * to make resume and init faster.
  3704. */
  3705. schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
  3706. round_jiffies_up_relative(HZ));
  3707. }
  3708. }
  3709. static void ibx_init_clock_gating(struct drm_device *dev)
  3710. {
  3711. struct drm_i915_private *dev_priv = dev->dev_private;
  3712. /*
  3713. * On Ibex Peak and Cougar Point, we need to disable clock
  3714. * gating for the panel power sequencer or it will fail to
  3715. * start up when no ports are active.
  3716. */
  3717. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  3718. }
  3719. static void g4x_disable_trickle_feed(struct drm_device *dev)
  3720. {
  3721. struct drm_i915_private *dev_priv = dev->dev_private;
  3722. int pipe;
  3723. for_each_pipe(pipe) {
  3724. I915_WRITE(DSPCNTR(pipe),
  3725. I915_READ(DSPCNTR(pipe)) |
  3726. DISPPLANE_TRICKLE_FEED_DISABLE);
  3727. intel_flush_display_plane(dev_priv, pipe);
  3728. }
  3729. }
  3730. static void ironlake_init_clock_gating(struct drm_device *dev)
  3731. {
  3732. struct drm_i915_private *dev_priv = dev->dev_private;
  3733. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  3734. /*
  3735. * Required for FBC
  3736. * WaFbcDisableDpfcClockGating:ilk
  3737. */
  3738. dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
  3739. ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
  3740. ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
  3741. I915_WRITE(PCH_3DCGDIS0,
  3742. MARIUNIT_CLOCK_GATE_DISABLE |
  3743. SVSMUNIT_CLOCK_GATE_DISABLE);
  3744. I915_WRITE(PCH_3DCGDIS1,
  3745. VFMUNIT_CLOCK_GATE_DISABLE);
  3746. /*
  3747. * According to the spec the following bits should be set in
  3748. * order to enable memory self-refresh
  3749. * The bit 22/21 of 0x42004
  3750. * The bit 5 of 0x42020
  3751. * The bit 15 of 0x45000
  3752. */
  3753. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3754. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  3755. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  3756. dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
  3757. I915_WRITE(DISP_ARB_CTL,
  3758. (I915_READ(DISP_ARB_CTL) |
  3759. DISP_FBC_WM_DIS));
  3760. I915_WRITE(WM3_LP_ILK, 0);
  3761. I915_WRITE(WM2_LP_ILK, 0);
  3762. I915_WRITE(WM1_LP_ILK, 0);
  3763. /*
  3764. * Based on the document from hardware guys the following bits
  3765. * should be set unconditionally in order to enable FBC.
  3766. * The bit 22 of 0x42000
  3767. * The bit 22 of 0x42004
  3768. * The bit 7,8,9 of 0x42020.
  3769. */
  3770. if (IS_IRONLAKE_M(dev)) {
  3771. /* WaFbcAsynchFlipDisableFbcQueue:ilk */
  3772. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  3773. I915_READ(ILK_DISPLAY_CHICKEN1) |
  3774. ILK_FBCQ_DIS);
  3775. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3776. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3777. ILK_DPARB_GATE);
  3778. }
  3779. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  3780. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3781. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3782. ILK_ELPIN_409_SELECT);
  3783. I915_WRITE(_3D_CHICKEN2,
  3784. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  3785. _3D_CHICKEN2_WM_READ_PIPELINED);
  3786. /* WaDisableRenderCachePipelinedFlush:ilk */
  3787. I915_WRITE(CACHE_MODE_0,
  3788. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  3789. g4x_disable_trickle_feed(dev);
  3790. ibx_init_clock_gating(dev);
  3791. }
  3792. static void cpt_init_clock_gating(struct drm_device *dev)
  3793. {
  3794. struct drm_i915_private *dev_priv = dev->dev_private;
  3795. int pipe;
  3796. uint32_t val;
  3797. /*
  3798. * On Ibex Peak and Cougar Point, we need to disable clock
  3799. * gating for the panel power sequencer or it will fail to
  3800. * start up when no ports are active.
  3801. */
  3802. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  3803. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  3804. DPLS_EDP_PPS_FIX_DIS);
  3805. /* The below fixes the weird display corruption, a few pixels shifted
  3806. * downward, on (only) LVDS of some HP laptops with IVY.
  3807. */
  3808. for_each_pipe(pipe) {
  3809. val = I915_READ(TRANS_CHICKEN2(pipe));
  3810. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  3811. val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  3812. if (dev_priv->vbt.fdi_rx_polarity_inverted)
  3813. val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  3814. val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
  3815. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
  3816. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
  3817. I915_WRITE(TRANS_CHICKEN2(pipe), val);
  3818. }
  3819. /* WADP0ClockGatingDisable */
  3820. for_each_pipe(pipe) {
  3821. I915_WRITE(TRANS_CHICKEN1(pipe),
  3822. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  3823. }
  3824. }
  3825. static void gen6_check_mch_setup(struct drm_device *dev)
  3826. {
  3827. struct drm_i915_private *dev_priv = dev->dev_private;
  3828. uint32_t tmp;
  3829. tmp = I915_READ(MCH_SSKPD);
  3830. if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL) {
  3831. DRM_INFO("Wrong MCH_SSKPD value: 0x%08x\n", tmp);
  3832. DRM_INFO("This can cause pipe underruns and display issues.\n");
  3833. DRM_INFO("Please upgrade your BIOS to fix this.\n");
  3834. }
  3835. }
  3836. static void gen6_init_clock_gating(struct drm_device *dev)
  3837. {
  3838. struct drm_i915_private *dev_priv = dev->dev_private;
  3839. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  3840. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  3841. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3842. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3843. ILK_ELPIN_409_SELECT);
  3844. /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
  3845. I915_WRITE(_3D_CHICKEN,
  3846. _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
  3847. /* WaSetupGtModeTdRowDispatch:snb */
  3848. if (IS_SNB_GT1(dev))
  3849. I915_WRITE(GEN6_GT_MODE,
  3850. _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
  3851. I915_WRITE(WM3_LP_ILK, 0);
  3852. I915_WRITE(WM2_LP_ILK, 0);
  3853. I915_WRITE(WM1_LP_ILK, 0);
  3854. I915_WRITE(CACHE_MODE_0,
  3855. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  3856. I915_WRITE(GEN6_UCGCTL1,
  3857. I915_READ(GEN6_UCGCTL1) |
  3858. GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
  3859. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  3860. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  3861. * gating disable must be set. Failure to set it results in
  3862. * flickering pixels due to Z write ordering failures after
  3863. * some amount of runtime in the Mesa "fire" demo, and Unigine
  3864. * Sanctuary and Tropics, and apparently anything else with
  3865. * alpha test or pixel discard.
  3866. *
  3867. * According to the spec, bit 11 (RCCUNIT) must also be set,
  3868. * but we didn't debug actual testcases to find it out.
  3869. *
  3870. * Also apply WaDisableVDSUnitClockGating:snb and
  3871. * WaDisableRCPBUnitClockGating:snb.
  3872. */
  3873. I915_WRITE(GEN6_UCGCTL2,
  3874. GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
  3875. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  3876. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  3877. /* Bspec says we need to always set all mask bits. */
  3878. I915_WRITE(_3D_CHICKEN3, (0xFFFF << 16) |
  3879. _3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL);
  3880. /*
  3881. * According to the spec the following bits should be
  3882. * set in order to enable memory self-refresh and fbc:
  3883. * The bit21 and bit22 of 0x42000
  3884. * The bit21 and bit22 of 0x42004
  3885. * The bit5 and bit7 of 0x42020
  3886. * The bit14 of 0x70180
  3887. * The bit14 of 0x71180
  3888. *
  3889. * WaFbcAsynchFlipDisableFbcQueue:snb
  3890. */
  3891. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  3892. I915_READ(ILK_DISPLAY_CHICKEN1) |
  3893. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  3894. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3895. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3896. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  3897. I915_WRITE(ILK_DSPCLK_GATE_D,
  3898. I915_READ(ILK_DSPCLK_GATE_D) |
  3899. ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
  3900. ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
  3901. /* WaMbcDriverBootEnable:snb */
  3902. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  3903. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  3904. g4x_disable_trickle_feed(dev);
  3905. /* The default value should be 0x200 according to docs, but the two
  3906. * platforms I checked have a 0 for this. (Maybe BIOS overrides?) */
  3907. I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_DISABLE(0xffff));
  3908. I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_ENABLE(GEN6_GT_MODE_HI));
  3909. cpt_init_clock_gating(dev);
  3910. gen6_check_mch_setup(dev);
  3911. }
  3912. static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
  3913. {
  3914. uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
  3915. reg &= ~GEN7_FF_SCHED_MASK;
  3916. reg |= GEN7_FF_TS_SCHED_HW;
  3917. reg |= GEN7_FF_VS_SCHED_HW;
  3918. reg |= GEN7_FF_DS_SCHED_HW;
  3919. if (IS_HASWELL(dev_priv->dev))
  3920. reg &= ~GEN7_FF_VS_REF_CNT_FFME;
  3921. I915_WRITE(GEN7_FF_THREAD_MODE, reg);
  3922. }
  3923. static void lpt_init_clock_gating(struct drm_device *dev)
  3924. {
  3925. struct drm_i915_private *dev_priv = dev->dev_private;
  3926. /*
  3927. * TODO: this bit should only be enabled when really needed, then
  3928. * disabled when not needed anymore in order to save power.
  3929. */
  3930. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
  3931. I915_WRITE(SOUTH_DSPCLK_GATE_D,
  3932. I915_READ(SOUTH_DSPCLK_GATE_D) |
  3933. PCH_LP_PARTITION_LEVEL_DISABLE);
  3934. /* WADPOClockGatingDisable:hsw */
  3935. I915_WRITE(_TRANSA_CHICKEN1,
  3936. I915_READ(_TRANSA_CHICKEN1) |
  3937. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  3938. }
  3939. static void lpt_suspend_hw(struct drm_device *dev)
  3940. {
  3941. struct drm_i915_private *dev_priv = dev->dev_private;
  3942. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  3943. uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
  3944. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  3945. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  3946. }
  3947. }
  3948. static void haswell_init_clock_gating(struct drm_device *dev)
  3949. {
  3950. struct drm_i915_private *dev_priv = dev->dev_private;
  3951. I915_WRITE(WM3_LP_ILK, 0);
  3952. I915_WRITE(WM2_LP_ILK, 0);
  3953. I915_WRITE(WM1_LP_ILK, 0);
  3954. /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  3955. * This implements the WaDisableRCZUnitClockGating:hsw workaround.
  3956. */
  3957. I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  3958. /* Apply the WaDisableRHWOOptimizationForRenderHang:hsw workaround. */
  3959. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  3960. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  3961. /* WaApplyL3ControlAndL3ChickenMode:hsw */
  3962. I915_WRITE(GEN7_L3CNTLREG1,
  3963. GEN7_WA_FOR_GEN7_L3_CONTROL);
  3964. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  3965. GEN7_WA_L3_CHICKEN_MODE);
  3966. /* This is required by WaCatErrorRejectionIssue:hsw */
  3967. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  3968. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  3969. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  3970. g4x_disable_trickle_feed(dev);
  3971. /* WaVSRefCountFullforceMissDisable:hsw */
  3972. gen7_setup_fixed_func_scheduler(dev_priv);
  3973. /* WaDisable4x2SubspanOptimization:hsw */
  3974. I915_WRITE(CACHE_MODE_1,
  3975. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  3976. /* WaMbcDriverBootEnable:hsw */
  3977. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  3978. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  3979. /* WaSwitchSolVfFArbitrationPriority:hsw */
  3980. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  3981. /* WaRsPkgCStateDisplayPMReq:hsw */
  3982. I915_WRITE(CHICKEN_PAR1_1,
  3983. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  3984. lpt_init_clock_gating(dev);
  3985. }
  3986. static void ivybridge_init_clock_gating(struct drm_device *dev)
  3987. {
  3988. struct drm_i915_private *dev_priv = dev->dev_private;
  3989. uint32_t snpcr;
  3990. I915_WRITE(WM3_LP_ILK, 0);
  3991. I915_WRITE(WM2_LP_ILK, 0);
  3992. I915_WRITE(WM1_LP_ILK, 0);
  3993. I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
  3994. /* WaDisableEarlyCull:ivb */
  3995. I915_WRITE(_3D_CHICKEN3,
  3996. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  3997. /* WaDisableBackToBackFlipFix:ivb */
  3998. I915_WRITE(IVB_CHICKEN3,
  3999. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  4000. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  4001. /* WaDisablePSDDualDispatchEnable:ivb */
  4002. if (IS_IVB_GT1(dev))
  4003. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  4004. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  4005. else
  4006. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1_GT2,
  4007. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  4008. /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
  4009. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  4010. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  4011. /* WaApplyL3ControlAndL3ChickenMode:ivb */
  4012. I915_WRITE(GEN7_L3CNTLREG1,
  4013. GEN7_WA_FOR_GEN7_L3_CONTROL);
  4014. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  4015. GEN7_WA_L3_CHICKEN_MODE);
  4016. if (IS_IVB_GT1(dev))
  4017. I915_WRITE(GEN7_ROW_CHICKEN2,
  4018. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  4019. else
  4020. I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
  4021. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  4022. /* WaForceL3Serialization:ivb */
  4023. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  4024. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  4025. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  4026. * gating disable must be set. Failure to set it results in
  4027. * flickering pixels due to Z write ordering failures after
  4028. * some amount of runtime in the Mesa "fire" demo, and Unigine
  4029. * Sanctuary and Tropics, and apparently anything else with
  4030. * alpha test or pixel discard.
  4031. *
  4032. * According to the spec, bit 11 (RCCUNIT) must also be set,
  4033. * but we didn't debug actual testcases to find it out.
  4034. *
  4035. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  4036. * This implements the WaDisableRCZUnitClockGating:ivb workaround.
  4037. */
  4038. I915_WRITE(GEN6_UCGCTL2,
  4039. GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
  4040. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  4041. /* This is required by WaCatErrorRejectionIssue:ivb */
  4042. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  4043. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  4044. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  4045. g4x_disable_trickle_feed(dev);
  4046. /* WaMbcDriverBootEnable:ivb */
  4047. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  4048. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  4049. /* WaVSRefCountFullforceMissDisable:ivb */
  4050. gen7_setup_fixed_func_scheduler(dev_priv);
  4051. /* WaDisable4x2SubspanOptimization:ivb */
  4052. I915_WRITE(CACHE_MODE_1,
  4053. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  4054. snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
  4055. snpcr &= ~GEN6_MBC_SNPCR_MASK;
  4056. snpcr |= GEN6_MBC_SNPCR_MED;
  4057. I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
  4058. if (!HAS_PCH_NOP(dev))
  4059. cpt_init_clock_gating(dev);
  4060. gen6_check_mch_setup(dev);
  4061. }
  4062. static void valleyview_init_clock_gating(struct drm_device *dev)
  4063. {
  4064. struct drm_i915_private *dev_priv = dev->dev_private;
  4065. I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
  4066. /* WaDisableEarlyCull:vlv */
  4067. I915_WRITE(_3D_CHICKEN3,
  4068. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  4069. /* WaDisableBackToBackFlipFix:vlv */
  4070. I915_WRITE(IVB_CHICKEN3,
  4071. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  4072. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  4073. /* WaDisablePSDDualDispatchEnable:vlv */
  4074. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  4075. _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
  4076. GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  4077. /* Apply the WaDisableRHWOOptimizationForRenderHang:vlv workaround. */
  4078. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  4079. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  4080. /* WaApplyL3ControlAndL3ChickenMode:vlv */
  4081. I915_WRITE(GEN7_L3CNTLREG1, I915_READ(GEN7_L3CNTLREG1) | GEN7_L3AGDIS);
  4082. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
  4083. /* WaForceL3Serialization:vlv */
  4084. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  4085. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  4086. /* WaDisableDopClockGating:vlv */
  4087. I915_WRITE(GEN7_ROW_CHICKEN2,
  4088. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  4089. /* This is required by WaCatErrorRejectionIssue:vlv */
  4090. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  4091. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  4092. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  4093. /* WaMbcDriverBootEnable:vlv */
  4094. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  4095. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  4096. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  4097. * gating disable must be set. Failure to set it results in
  4098. * flickering pixels due to Z write ordering failures after
  4099. * some amount of runtime in the Mesa "fire" demo, and Unigine
  4100. * Sanctuary and Tropics, and apparently anything else with
  4101. * alpha test or pixel discard.
  4102. *
  4103. * According to the spec, bit 11 (RCCUNIT) must also be set,
  4104. * but we didn't debug actual testcases to find it out.
  4105. *
  4106. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  4107. * This implements the WaDisableRCZUnitClockGating:vlv workaround.
  4108. *
  4109. * Also apply WaDisableVDSUnitClockGating:vlv and
  4110. * WaDisableRCPBUnitClockGating:vlv.
  4111. */
  4112. I915_WRITE(GEN6_UCGCTL2,
  4113. GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
  4114. GEN7_TDLUNIT_CLOCK_GATE_DISABLE |
  4115. GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
  4116. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  4117. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  4118. I915_WRITE(GEN7_UCGCTL4, GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
  4119. I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
  4120. I915_WRITE(CACHE_MODE_1,
  4121. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  4122. /*
  4123. * WaDisableVLVClockGating_VBIIssue:vlv
  4124. * Disable clock gating on th GCFG unit to prevent a delay
  4125. * in the reporting of vblank events.
  4126. */
  4127. I915_WRITE(VLV_GUNIT_CLOCK_GATE, 0xffffffff);
  4128. /* Conservative clock gating settings for now */
  4129. I915_WRITE(0x9400, 0xffffffff);
  4130. I915_WRITE(0x9404, 0xffffffff);
  4131. I915_WRITE(0x9408, 0xffffffff);
  4132. I915_WRITE(0x940c, 0xffffffff);
  4133. I915_WRITE(0x9410, 0xffffffff);
  4134. I915_WRITE(0x9414, 0xffffffff);
  4135. I915_WRITE(0x9418, 0xffffffff);
  4136. }
  4137. static void g4x_init_clock_gating(struct drm_device *dev)
  4138. {
  4139. struct drm_i915_private *dev_priv = dev->dev_private;
  4140. uint32_t dspclk_gate;
  4141. I915_WRITE(RENCLK_GATE_D1, 0);
  4142. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  4143. GS_UNIT_CLOCK_GATE_DISABLE |
  4144. CL_UNIT_CLOCK_GATE_DISABLE);
  4145. I915_WRITE(RAMCLK_GATE_D, 0);
  4146. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  4147. OVRUNIT_CLOCK_GATE_DISABLE |
  4148. OVCUNIT_CLOCK_GATE_DISABLE;
  4149. if (IS_GM45(dev))
  4150. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  4151. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  4152. /* WaDisableRenderCachePipelinedFlush */
  4153. I915_WRITE(CACHE_MODE_0,
  4154. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  4155. g4x_disable_trickle_feed(dev);
  4156. }
  4157. static void crestline_init_clock_gating(struct drm_device *dev)
  4158. {
  4159. struct drm_i915_private *dev_priv = dev->dev_private;
  4160. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  4161. I915_WRITE(RENCLK_GATE_D2, 0);
  4162. I915_WRITE(DSPCLK_GATE_D, 0);
  4163. I915_WRITE(RAMCLK_GATE_D, 0);
  4164. I915_WRITE16(DEUC, 0);
  4165. I915_WRITE(MI_ARB_STATE,
  4166. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  4167. }
  4168. static void broadwater_init_clock_gating(struct drm_device *dev)
  4169. {
  4170. struct drm_i915_private *dev_priv = dev->dev_private;
  4171. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  4172. I965_RCC_CLOCK_GATE_DISABLE |
  4173. I965_RCPB_CLOCK_GATE_DISABLE |
  4174. I965_ISC_CLOCK_GATE_DISABLE |
  4175. I965_FBC_CLOCK_GATE_DISABLE);
  4176. I915_WRITE(RENCLK_GATE_D2, 0);
  4177. I915_WRITE(MI_ARB_STATE,
  4178. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  4179. }
  4180. static void gen3_init_clock_gating(struct drm_device *dev)
  4181. {
  4182. struct drm_i915_private *dev_priv = dev->dev_private;
  4183. u32 dstate = I915_READ(D_STATE);
  4184. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  4185. DSTATE_DOT_CLOCK_GATING;
  4186. I915_WRITE(D_STATE, dstate);
  4187. if (IS_PINEVIEW(dev))
  4188. I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
  4189. /* IIR "flip pending" means done if this bit is set */
  4190. I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
  4191. }
  4192. static void i85x_init_clock_gating(struct drm_device *dev)
  4193. {
  4194. struct drm_i915_private *dev_priv = dev->dev_private;
  4195. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  4196. }
  4197. static void i830_init_clock_gating(struct drm_device *dev)
  4198. {
  4199. struct drm_i915_private *dev_priv = dev->dev_private;
  4200. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  4201. }
  4202. void intel_init_clock_gating(struct drm_device *dev)
  4203. {
  4204. struct drm_i915_private *dev_priv = dev->dev_private;
  4205. dev_priv->display.init_clock_gating(dev);
  4206. }
  4207. void intel_suspend_hw(struct drm_device *dev)
  4208. {
  4209. if (HAS_PCH_LPT(dev))
  4210. lpt_suspend_hw(dev);
  4211. }
  4212. /**
  4213. * We should only use the power well if we explicitly asked the hardware to
  4214. * enable it, so check if it's enabled and also check if we've requested it to
  4215. * be enabled.
  4216. */
  4217. bool intel_display_power_enabled(struct drm_device *dev,
  4218. enum intel_display_power_domain domain)
  4219. {
  4220. struct drm_i915_private *dev_priv = dev->dev_private;
  4221. if (!HAS_POWER_WELL(dev))
  4222. return true;
  4223. switch (domain) {
  4224. case POWER_DOMAIN_PIPE_A:
  4225. case POWER_DOMAIN_TRANSCODER_EDP:
  4226. return true;
  4227. case POWER_DOMAIN_PIPE_B:
  4228. case POWER_DOMAIN_PIPE_C:
  4229. case POWER_DOMAIN_PIPE_A_PANEL_FITTER:
  4230. case POWER_DOMAIN_PIPE_B_PANEL_FITTER:
  4231. case POWER_DOMAIN_PIPE_C_PANEL_FITTER:
  4232. case POWER_DOMAIN_TRANSCODER_A:
  4233. case POWER_DOMAIN_TRANSCODER_B:
  4234. case POWER_DOMAIN_TRANSCODER_C:
  4235. return I915_READ(HSW_PWR_WELL_DRIVER) ==
  4236. (HSW_PWR_WELL_ENABLE | HSW_PWR_WELL_STATE);
  4237. default:
  4238. BUG();
  4239. }
  4240. }
  4241. static void __intel_set_power_well(struct drm_device *dev, bool enable)
  4242. {
  4243. struct drm_i915_private *dev_priv = dev->dev_private;
  4244. bool is_enabled, enable_requested;
  4245. uint32_t tmp;
  4246. tmp = I915_READ(HSW_PWR_WELL_DRIVER);
  4247. is_enabled = tmp & HSW_PWR_WELL_STATE;
  4248. enable_requested = tmp & HSW_PWR_WELL_ENABLE;
  4249. if (enable) {
  4250. if (!enable_requested)
  4251. I915_WRITE(HSW_PWR_WELL_DRIVER, HSW_PWR_WELL_ENABLE);
  4252. if (!is_enabled) {
  4253. DRM_DEBUG_KMS("Enabling power well\n");
  4254. if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
  4255. HSW_PWR_WELL_STATE), 20))
  4256. DRM_ERROR("Timeout enabling power well\n");
  4257. }
  4258. } else {
  4259. if (enable_requested) {
  4260. I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
  4261. DRM_DEBUG_KMS("Requesting to disable the power well\n");
  4262. }
  4263. }
  4264. }
  4265. static struct i915_power_well *hsw_pwr;
  4266. /* Display audio driver power well request */
  4267. void i915_request_power_well(void)
  4268. {
  4269. if (WARN_ON(!hsw_pwr))
  4270. return;
  4271. spin_lock_irq(&hsw_pwr->lock);
  4272. if (!hsw_pwr->count++ &&
  4273. !hsw_pwr->i915_request)
  4274. __intel_set_power_well(hsw_pwr->device, true);
  4275. spin_unlock_irq(&hsw_pwr->lock);
  4276. }
  4277. EXPORT_SYMBOL_GPL(i915_request_power_well);
  4278. /* Display audio driver power well release */
  4279. void i915_release_power_well(void)
  4280. {
  4281. if (WARN_ON(!hsw_pwr))
  4282. return;
  4283. spin_lock_irq(&hsw_pwr->lock);
  4284. WARN_ON(!hsw_pwr->count);
  4285. if (!--hsw_pwr->count &&
  4286. !hsw_pwr->i915_request)
  4287. __intel_set_power_well(hsw_pwr->device, false);
  4288. spin_unlock_irq(&hsw_pwr->lock);
  4289. }
  4290. EXPORT_SYMBOL_GPL(i915_release_power_well);
  4291. int i915_init_power_well(struct drm_device *dev)
  4292. {
  4293. struct drm_i915_private *dev_priv = dev->dev_private;
  4294. hsw_pwr = &dev_priv->power_well;
  4295. hsw_pwr->device = dev;
  4296. spin_lock_init(&hsw_pwr->lock);
  4297. hsw_pwr->count = 0;
  4298. return 0;
  4299. }
  4300. void i915_remove_power_well(struct drm_device *dev)
  4301. {
  4302. hsw_pwr = NULL;
  4303. }
  4304. void intel_set_power_well(struct drm_device *dev, bool enable)
  4305. {
  4306. struct drm_i915_private *dev_priv = dev->dev_private;
  4307. struct i915_power_well *power_well = &dev_priv->power_well;
  4308. if (!HAS_POWER_WELL(dev))
  4309. return;
  4310. if (!i915_disable_power_well && !enable)
  4311. return;
  4312. spin_lock_irq(&power_well->lock);
  4313. power_well->i915_request = enable;
  4314. /* only reject "disable" power well request */
  4315. if (power_well->count && !enable) {
  4316. spin_unlock_irq(&power_well->lock);
  4317. return;
  4318. }
  4319. __intel_set_power_well(dev, enable);
  4320. spin_unlock_irq(&power_well->lock);
  4321. }
  4322. /*
  4323. * Starting with Haswell, we have a "Power Down Well" that can be turned off
  4324. * when not needed anymore. We have 4 registers that can request the power well
  4325. * to be enabled, and it will only be disabled if none of the registers is
  4326. * requesting it to be enabled.
  4327. */
  4328. void intel_init_power_well(struct drm_device *dev)
  4329. {
  4330. struct drm_i915_private *dev_priv = dev->dev_private;
  4331. if (!HAS_POWER_WELL(dev))
  4332. return;
  4333. /* For now, we need the power well to be always enabled. */
  4334. intel_set_power_well(dev, true);
  4335. /* We're taking over the BIOS, so clear any requests made by it since
  4336. * the driver is in charge now. */
  4337. if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE)
  4338. I915_WRITE(HSW_PWR_WELL_BIOS, 0);
  4339. }
  4340. /* Set up chip specific power management-related functions */
  4341. void intel_init_pm(struct drm_device *dev)
  4342. {
  4343. struct drm_i915_private *dev_priv = dev->dev_private;
  4344. if (I915_HAS_FBC(dev)) {
  4345. if (HAS_PCH_SPLIT(dev)) {
  4346. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  4347. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  4348. dev_priv->display.enable_fbc =
  4349. gen7_enable_fbc;
  4350. else
  4351. dev_priv->display.enable_fbc =
  4352. ironlake_enable_fbc;
  4353. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  4354. } else if (IS_GM45(dev)) {
  4355. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  4356. dev_priv->display.enable_fbc = g4x_enable_fbc;
  4357. dev_priv->display.disable_fbc = g4x_disable_fbc;
  4358. } else if (IS_CRESTLINE(dev)) {
  4359. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  4360. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  4361. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  4362. }
  4363. /* 855GM needs testing */
  4364. }
  4365. /* For cxsr */
  4366. if (IS_PINEVIEW(dev))
  4367. i915_pineview_get_mem_freq(dev);
  4368. else if (IS_GEN5(dev))
  4369. i915_ironlake_get_mem_freq(dev);
  4370. /* For FIFO watermark updates */
  4371. if (HAS_PCH_SPLIT(dev)) {
  4372. if (IS_GEN5(dev)) {
  4373. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  4374. dev_priv->display.update_wm = ironlake_update_wm;
  4375. else {
  4376. DRM_DEBUG_KMS("Failed to get proper latency. "
  4377. "Disable CxSR\n");
  4378. dev_priv->display.update_wm = NULL;
  4379. }
  4380. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  4381. } else if (IS_GEN6(dev)) {
  4382. if (SNB_READ_WM0_LATENCY()) {
  4383. dev_priv->display.update_wm = sandybridge_update_wm;
  4384. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  4385. } else {
  4386. DRM_DEBUG_KMS("Failed to read display plane latency. "
  4387. "Disable CxSR\n");
  4388. dev_priv->display.update_wm = NULL;
  4389. }
  4390. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  4391. } else if (IS_IVYBRIDGE(dev)) {
  4392. if (SNB_READ_WM0_LATENCY()) {
  4393. dev_priv->display.update_wm = ivybridge_update_wm;
  4394. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  4395. } else {
  4396. DRM_DEBUG_KMS("Failed to read display plane latency. "
  4397. "Disable CxSR\n");
  4398. dev_priv->display.update_wm = NULL;
  4399. }
  4400. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  4401. } else if (IS_HASWELL(dev)) {
  4402. if (I915_READ64(MCH_SSKPD)) {
  4403. dev_priv->display.update_wm = haswell_update_wm;
  4404. dev_priv->display.update_sprite_wm =
  4405. haswell_update_sprite_wm;
  4406. } else {
  4407. DRM_DEBUG_KMS("Failed to read display plane latency. "
  4408. "Disable CxSR\n");
  4409. dev_priv->display.update_wm = NULL;
  4410. }
  4411. dev_priv->display.init_clock_gating = haswell_init_clock_gating;
  4412. } else
  4413. dev_priv->display.update_wm = NULL;
  4414. } else if (IS_VALLEYVIEW(dev)) {
  4415. dev_priv->display.update_wm = valleyview_update_wm;
  4416. dev_priv->display.init_clock_gating =
  4417. valleyview_init_clock_gating;
  4418. } else if (IS_PINEVIEW(dev)) {
  4419. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  4420. dev_priv->is_ddr3,
  4421. dev_priv->fsb_freq,
  4422. dev_priv->mem_freq)) {
  4423. DRM_INFO("failed to find known CxSR latency "
  4424. "(found ddr%s fsb freq %d, mem freq %d), "
  4425. "disabling CxSR\n",
  4426. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  4427. dev_priv->fsb_freq, dev_priv->mem_freq);
  4428. /* Disable CxSR and never update its watermark again */
  4429. pineview_disable_cxsr(dev);
  4430. dev_priv->display.update_wm = NULL;
  4431. } else
  4432. dev_priv->display.update_wm = pineview_update_wm;
  4433. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  4434. } else if (IS_G4X(dev)) {
  4435. dev_priv->display.update_wm = g4x_update_wm;
  4436. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  4437. } else if (IS_GEN4(dev)) {
  4438. dev_priv->display.update_wm = i965_update_wm;
  4439. if (IS_CRESTLINE(dev))
  4440. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  4441. else if (IS_BROADWATER(dev))
  4442. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  4443. } else if (IS_GEN3(dev)) {
  4444. dev_priv->display.update_wm = i9xx_update_wm;
  4445. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  4446. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  4447. } else if (IS_I865G(dev)) {
  4448. dev_priv->display.update_wm = i830_update_wm;
  4449. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  4450. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  4451. } else if (IS_I85X(dev)) {
  4452. dev_priv->display.update_wm = i9xx_update_wm;
  4453. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  4454. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  4455. } else {
  4456. dev_priv->display.update_wm = i830_update_wm;
  4457. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  4458. if (IS_845G(dev))
  4459. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  4460. else
  4461. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  4462. }
  4463. }
  4464. int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
  4465. {
  4466. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  4467. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  4468. DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
  4469. return -EAGAIN;
  4470. }
  4471. I915_WRITE(GEN6_PCODE_DATA, *val);
  4472. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  4473. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  4474. 500)) {
  4475. DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
  4476. return -ETIMEDOUT;
  4477. }
  4478. *val = I915_READ(GEN6_PCODE_DATA);
  4479. I915_WRITE(GEN6_PCODE_DATA, 0);
  4480. return 0;
  4481. }
  4482. int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
  4483. {
  4484. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  4485. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  4486. DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
  4487. return -EAGAIN;
  4488. }
  4489. I915_WRITE(GEN6_PCODE_DATA, val);
  4490. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  4491. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  4492. 500)) {
  4493. DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
  4494. return -ETIMEDOUT;
  4495. }
  4496. I915_WRITE(GEN6_PCODE_DATA, 0);
  4497. return 0;
  4498. }
  4499. int vlv_gpu_freq(int ddr_freq, int val)
  4500. {
  4501. int mult, base;
  4502. switch (ddr_freq) {
  4503. case 800:
  4504. mult = 20;
  4505. base = 120;
  4506. break;
  4507. case 1066:
  4508. mult = 22;
  4509. base = 133;
  4510. break;
  4511. case 1333:
  4512. mult = 21;
  4513. base = 125;
  4514. break;
  4515. default:
  4516. return -1;
  4517. }
  4518. return ((val - 0xbd) * mult) + base;
  4519. }
  4520. int vlv_freq_opcode(int ddr_freq, int val)
  4521. {
  4522. int mult, base;
  4523. switch (ddr_freq) {
  4524. case 800:
  4525. mult = 20;
  4526. base = 120;
  4527. break;
  4528. case 1066:
  4529. mult = 22;
  4530. base = 133;
  4531. break;
  4532. case 1333:
  4533. mult = 21;
  4534. base = 125;
  4535. break;
  4536. default:
  4537. return -1;
  4538. }
  4539. val /= mult;
  4540. val -= base / mult;
  4541. val += 0xbd;
  4542. if (val > 0xea)
  4543. val = 0xea;
  4544. return val;
  4545. }
  4546. void intel_pm_init(struct drm_device *dev)
  4547. {
  4548. struct drm_i915_private *dev_priv = dev->dev_private;
  4549. INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
  4550. intel_gen6_powersave_work);
  4551. }