intel_pm.c 155 KB

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