intel_pm.c 200 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199
  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. /**
  33. * RC6 is a special power stage which allows the GPU to enter an very
  34. * low-voltage mode when idle, using down to 0V while at this stage. This
  35. * stage is entered automatically when the GPU is idle when RC6 support is
  36. * enabled, and as soon as new workload arises GPU wakes up automatically as well.
  37. *
  38. * There are different RC6 modes available in Intel GPU, which differentiate
  39. * among each other with the latency required to enter and leave RC6 and
  40. * voltage consumed by the GPU in different states.
  41. *
  42. * The combination of the following flags define which states GPU is allowed
  43. * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
  44. * RC6pp is deepest RC6. Their support by hardware varies according to the
  45. * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
  46. * which brings the most power savings; deeper states save more power, but
  47. * require higher latency to switch to and wake up.
  48. */
  49. #define INTEL_RC6_ENABLE (1<<0)
  50. #define INTEL_RC6p_ENABLE (1<<1)
  51. #define INTEL_RC6pp_ENABLE (1<<2)
  52. /* FBC, or Frame Buffer Compression, is a technique employed to compress the
  53. * framebuffer contents in-memory, aiming at reducing the required bandwidth
  54. * during in-memory transfers and, therefore, reduce the power packet.
  55. *
  56. * The benefits of FBC are mostly visible with solid backgrounds and
  57. * variation-less patterns.
  58. *
  59. * FBC-related functionality can be enabled by the means of the
  60. * i915.i915_enable_fbc parameter
  61. */
  62. static void gen9_init_clock_gating(struct drm_device *dev)
  63. {
  64. struct drm_i915_private *dev_priv = dev->dev_private;
  65. /*
  66. * WaDisableSDEUnitClockGating:skl
  67. * This seems to be a pre-production w/a.
  68. */
  69. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  70. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  71. /*
  72. * WaDisableDgMirrorFixInHalfSliceChicken5:skl
  73. * This is a pre-production w/a.
  74. */
  75. I915_WRITE(GEN9_HALF_SLICE_CHICKEN5,
  76. I915_READ(GEN9_HALF_SLICE_CHICKEN5) &
  77. ~GEN9_DG_MIRROR_FIX_ENABLE);
  78. /* Wa4x4STCOptimizationDisable:skl */
  79. I915_WRITE(CACHE_MODE_1,
  80. _MASKED_BIT_ENABLE(GEN8_4x4_STC_OPTIMIZATION_DISABLE));
  81. }
  82. static void i8xx_disable_fbc(struct drm_device *dev)
  83. {
  84. struct drm_i915_private *dev_priv = dev->dev_private;
  85. u32 fbc_ctl;
  86. dev_priv->fbc.enabled = false;
  87. /* Disable compression */
  88. fbc_ctl = I915_READ(FBC_CONTROL);
  89. if ((fbc_ctl & FBC_CTL_EN) == 0)
  90. return;
  91. fbc_ctl &= ~FBC_CTL_EN;
  92. I915_WRITE(FBC_CONTROL, fbc_ctl);
  93. /* Wait for compressing bit to clear */
  94. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  95. DRM_DEBUG_KMS("FBC idle timed out\n");
  96. return;
  97. }
  98. DRM_DEBUG_KMS("disabled FBC\n");
  99. }
  100. static void i8xx_enable_fbc(struct drm_crtc *crtc)
  101. {
  102. struct drm_device *dev = crtc->dev;
  103. struct drm_i915_private *dev_priv = dev->dev_private;
  104. struct drm_framebuffer *fb = crtc->primary->fb;
  105. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  106. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  107. int cfb_pitch;
  108. int i;
  109. u32 fbc_ctl;
  110. dev_priv->fbc.enabled = true;
  111. cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
  112. if (fb->pitches[0] < cfb_pitch)
  113. cfb_pitch = fb->pitches[0];
  114. /* FBC_CTL wants 32B or 64B units */
  115. if (IS_GEN2(dev))
  116. cfb_pitch = (cfb_pitch / 32) - 1;
  117. else
  118. cfb_pitch = (cfb_pitch / 64) - 1;
  119. /* Clear old tags */
  120. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  121. I915_WRITE(FBC_TAG + (i * 4), 0);
  122. if (IS_GEN4(dev)) {
  123. u32 fbc_ctl2;
  124. /* Set it up... */
  125. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  126. fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
  127. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  128. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  129. }
  130. /* enable it... */
  131. fbc_ctl = I915_READ(FBC_CONTROL);
  132. fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
  133. fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
  134. if (IS_I945GM(dev))
  135. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  136. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  137. fbc_ctl |= obj->fence_reg;
  138. I915_WRITE(FBC_CONTROL, fbc_ctl);
  139. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
  140. cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
  141. }
  142. static bool i8xx_fbc_enabled(struct drm_device *dev)
  143. {
  144. struct drm_i915_private *dev_priv = dev->dev_private;
  145. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  146. }
  147. static void g4x_enable_fbc(struct drm_crtc *crtc)
  148. {
  149. struct drm_device *dev = crtc->dev;
  150. struct drm_i915_private *dev_priv = dev->dev_private;
  151. struct drm_framebuffer *fb = crtc->primary->fb;
  152. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  153. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  154. u32 dpfc_ctl;
  155. dev_priv->fbc.enabled = true;
  156. dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
  157. if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
  158. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  159. else
  160. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  161. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  162. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  163. /* enable it... */
  164. I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  165. DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
  166. }
  167. static void g4x_disable_fbc(struct drm_device *dev)
  168. {
  169. struct drm_i915_private *dev_priv = dev->dev_private;
  170. u32 dpfc_ctl;
  171. dev_priv->fbc.enabled = false;
  172. /* Disable compression */
  173. dpfc_ctl = I915_READ(DPFC_CONTROL);
  174. if (dpfc_ctl & DPFC_CTL_EN) {
  175. dpfc_ctl &= ~DPFC_CTL_EN;
  176. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  177. DRM_DEBUG_KMS("disabled FBC\n");
  178. }
  179. }
  180. static bool g4x_fbc_enabled(struct drm_device *dev)
  181. {
  182. struct drm_i915_private *dev_priv = dev->dev_private;
  183. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  184. }
  185. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  186. {
  187. struct drm_i915_private *dev_priv = dev->dev_private;
  188. u32 blt_ecoskpd;
  189. /* Make sure blitter notifies FBC of writes */
  190. /* Blitter is part of Media powerwell on VLV. No impact of
  191. * his param in other platforms for now */
  192. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
  193. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  194. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  195. GEN6_BLITTER_LOCK_SHIFT;
  196. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  197. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  198. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  199. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  200. GEN6_BLITTER_LOCK_SHIFT);
  201. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  202. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  203. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
  204. }
  205. static void ironlake_enable_fbc(struct drm_crtc *crtc)
  206. {
  207. struct drm_device *dev = crtc->dev;
  208. struct drm_i915_private *dev_priv = dev->dev_private;
  209. struct drm_framebuffer *fb = crtc->primary->fb;
  210. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  211. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  212. u32 dpfc_ctl;
  213. dev_priv->fbc.enabled = true;
  214. dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
  215. if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
  216. dev_priv->fbc.threshold++;
  217. switch (dev_priv->fbc.threshold) {
  218. case 4:
  219. case 3:
  220. dpfc_ctl |= DPFC_CTL_LIMIT_4X;
  221. break;
  222. case 2:
  223. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  224. break;
  225. case 1:
  226. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  227. break;
  228. }
  229. dpfc_ctl |= DPFC_CTL_FENCE_EN;
  230. if (IS_GEN5(dev))
  231. dpfc_ctl |= obj->fence_reg;
  232. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  233. I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
  234. /* enable it... */
  235. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  236. if (IS_GEN6(dev)) {
  237. I915_WRITE(SNB_DPFC_CTL_SA,
  238. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  239. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  240. sandybridge_blit_fbc_update(dev);
  241. }
  242. DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
  243. }
  244. static void ironlake_disable_fbc(struct drm_device *dev)
  245. {
  246. struct drm_i915_private *dev_priv = dev->dev_private;
  247. u32 dpfc_ctl;
  248. dev_priv->fbc.enabled = false;
  249. /* Disable compression */
  250. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  251. if (dpfc_ctl & DPFC_CTL_EN) {
  252. dpfc_ctl &= ~DPFC_CTL_EN;
  253. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  254. DRM_DEBUG_KMS("disabled FBC\n");
  255. }
  256. }
  257. static bool ironlake_fbc_enabled(struct drm_device *dev)
  258. {
  259. struct drm_i915_private *dev_priv = dev->dev_private;
  260. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  261. }
  262. static void gen7_enable_fbc(struct drm_crtc *crtc)
  263. {
  264. struct drm_device *dev = crtc->dev;
  265. struct drm_i915_private *dev_priv = dev->dev_private;
  266. struct drm_framebuffer *fb = crtc->primary->fb;
  267. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  268. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  269. u32 dpfc_ctl;
  270. dev_priv->fbc.enabled = true;
  271. dpfc_ctl = IVB_DPFC_CTL_PLANE(intel_crtc->plane);
  272. if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
  273. dev_priv->fbc.threshold++;
  274. switch (dev_priv->fbc.threshold) {
  275. case 4:
  276. case 3:
  277. dpfc_ctl |= DPFC_CTL_LIMIT_4X;
  278. break;
  279. case 2:
  280. dpfc_ctl |= DPFC_CTL_LIMIT_2X;
  281. break;
  282. case 1:
  283. dpfc_ctl |= DPFC_CTL_LIMIT_1X;
  284. break;
  285. }
  286. dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
  287. if (dev_priv->fbc.false_color)
  288. dpfc_ctl |= FBC_CTL_FALSE_COLOR;
  289. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  290. if (IS_IVYBRIDGE(dev)) {
  291. /* WaFbcAsynchFlipDisableFbcQueue:ivb */
  292. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  293. I915_READ(ILK_DISPLAY_CHICKEN1) |
  294. ILK_FBCQ_DIS);
  295. } else {
  296. /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
  297. I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
  298. I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
  299. HSW_FBCQ_DIS);
  300. }
  301. I915_WRITE(SNB_DPFC_CTL_SA,
  302. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  303. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  304. sandybridge_blit_fbc_update(dev);
  305. DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
  306. }
  307. bool intel_fbc_enabled(struct drm_device *dev)
  308. {
  309. struct drm_i915_private *dev_priv = dev->dev_private;
  310. return dev_priv->fbc.enabled;
  311. }
  312. void bdw_fbc_sw_flush(struct drm_device *dev, u32 value)
  313. {
  314. struct drm_i915_private *dev_priv = dev->dev_private;
  315. if (!IS_GEN8(dev))
  316. return;
  317. if (!intel_fbc_enabled(dev))
  318. return;
  319. I915_WRITE(MSG_FBC_REND_STATE, value);
  320. }
  321. static void intel_fbc_work_fn(struct work_struct *__work)
  322. {
  323. struct intel_fbc_work *work =
  324. container_of(to_delayed_work(__work),
  325. struct intel_fbc_work, work);
  326. struct drm_device *dev = work->crtc->dev;
  327. struct drm_i915_private *dev_priv = dev->dev_private;
  328. mutex_lock(&dev->struct_mutex);
  329. if (work == dev_priv->fbc.fbc_work) {
  330. /* Double check that we haven't switched fb without cancelling
  331. * the prior work.
  332. */
  333. if (work->crtc->primary->fb == work->fb) {
  334. dev_priv->display.enable_fbc(work->crtc);
  335. dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
  336. dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
  337. dev_priv->fbc.y = work->crtc->y;
  338. }
  339. dev_priv->fbc.fbc_work = NULL;
  340. }
  341. mutex_unlock(&dev->struct_mutex);
  342. kfree(work);
  343. }
  344. static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
  345. {
  346. if (dev_priv->fbc.fbc_work == NULL)
  347. return;
  348. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  349. /* Synchronisation is provided by struct_mutex and checking of
  350. * dev_priv->fbc.fbc_work, so we can perform the cancellation
  351. * entirely asynchronously.
  352. */
  353. if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
  354. /* tasklet was killed before being run, clean up */
  355. kfree(dev_priv->fbc.fbc_work);
  356. /* Mark the work as no longer wanted so that if it does
  357. * wake-up (because the work was already running and waiting
  358. * for our mutex), it will discover that is no longer
  359. * necessary to run.
  360. */
  361. dev_priv->fbc.fbc_work = NULL;
  362. }
  363. static void intel_enable_fbc(struct drm_crtc *crtc)
  364. {
  365. struct intel_fbc_work *work;
  366. struct drm_device *dev = crtc->dev;
  367. struct drm_i915_private *dev_priv = dev->dev_private;
  368. if (!dev_priv->display.enable_fbc)
  369. return;
  370. intel_cancel_fbc_work(dev_priv);
  371. work = kzalloc(sizeof(*work), GFP_KERNEL);
  372. if (work == NULL) {
  373. DRM_ERROR("Failed to allocate FBC work structure\n");
  374. dev_priv->display.enable_fbc(crtc);
  375. return;
  376. }
  377. work->crtc = crtc;
  378. work->fb = crtc->primary->fb;
  379. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  380. dev_priv->fbc.fbc_work = work;
  381. /* Delay the actual enabling to let pageflipping cease and the
  382. * display to settle before starting the compression. Note that
  383. * this delay also serves a second purpose: it allows for a
  384. * vblank to pass after disabling the FBC before we attempt
  385. * to modify the control registers.
  386. *
  387. * A more complicated solution would involve tracking vblanks
  388. * following the termination of the page-flipping sequence
  389. * and indeed performing the enable as a co-routine and not
  390. * waiting synchronously upon the vblank.
  391. *
  392. * WaFbcWaitForVBlankBeforeEnable:ilk,snb
  393. */
  394. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  395. }
  396. void intel_disable_fbc(struct drm_device *dev)
  397. {
  398. struct drm_i915_private *dev_priv = dev->dev_private;
  399. intel_cancel_fbc_work(dev_priv);
  400. if (!dev_priv->display.disable_fbc)
  401. return;
  402. dev_priv->display.disable_fbc(dev);
  403. dev_priv->fbc.plane = -1;
  404. }
  405. static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
  406. enum no_fbc_reason reason)
  407. {
  408. if (dev_priv->fbc.no_fbc_reason == reason)
  409. return false;
  410. dev_priv->fbc.no_fbc_reason = reason;
  411. return true;
  412. }
  413. /**
  414. * intel_update_fbc - enable/disable FBC as needed
  415. * @dev: the drm_device
  416. *
  417. * Set up the framebuffer compression hardware at mode set time. We
  418. * enable it if possible:
  419. * - plane A only (on pre-965)
  420. * - no pixel mulitply/line duplication
  421. * - no alpha buffer discard
  422. * - no dual wide
  423. * - framebuffer <= max_hdisplay in width, max_vdisplay in height
  424. *
  425. * We can't assume that any compression will take place (worst case),
  426. * so the compressed buffer has to be the same size as the uncompressed
  427. * one. It also must reside (along with the line length buffer) in
  428. * stolen memory.
  429. *
  430. * We need to enable/disable FBC on a global basis.
  431. */
  432. void intel_update_fbc(struct drm_device *dev)
  433. {
  434. struct drm_i915_private *dev_priv = dev->dev_private;
  435. struct drm_crtc *crtc = NULL, *tmp_crtc;
  436. struct intel_crtc *intel_crtc;
  437. struct drm_framebuffer *fb;
  438. struct drm_i915_gem_object *obj;
  439. const struct drm_display_mode *adjusted_mode;
  440. unsigned int max_width, max_height;
  441. if (!HAS_FBC(dev)) {
  442. set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
  443. return;
  444. }
  445. if (!i915.powersave) {
  446. if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
  447. DRM_DEBUG_KMS("fbc disabled per module param\n");
  448. return;
  449. }
  450. /*
  451. * If FBC is already on, we just have to verify that we can
  452. * keep it that way...
  453. * Need to disable if:
  454. * - more than one pipe is active
  455. * - changing FBC params (stride, fence, mode)
  456. * - new fb is too large to fit in compressed buffer
  457. * - going to an unsupported config (interlace, pixel multiply, etc.)
  458. */
  459. for_each_crtc(dev, tmp_crtc) {
  460. if (intel_crtc_active(tmp_crtc) &&
  461. to_intel_crtc(tmp_crtc)->primary_enabled) {
  462. if (crtc) {
  463. if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
  464. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  465. goto out_disable;
  466. }
  467. crtc = tmp_crtc;
  468. }
  469. }
  470. if (!crtc || crtc->primary->fb == NULL) {
  471. if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
  472. DRM_DEBUG_KMS("no output, disabling\n");
  473. goto out_disable;
  474. }
  475. intel_crtc = to_intel_crtc(crtc);
  476. fb = crtc->primary->fb;
  477. obj = intel_fb_obj(fb);
  478. adjusted_mode = &intel_crtc->config.adjusted_mode;
  479. if (i915.enable_fbc < 0) {
  480. if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
  481. DRM_DEBUG_KMS("disabled per chip default\n");
  482. goto out_disable;
  483. }
  484. if (!i915.enable_fbc) {
  485. if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
  486. DRM_DEBUG_KMS("fbc disabled per module param\n");
  487. goto out_disable;
  488. }
  489. if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  490. (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
  491. if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
  492. DRM_DEBUG_KMS("mode incompatible with compression, "
  493. "disabling\n");
  494. goto out_disable;
  495. }
  496. if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
  497. max_width = 4096;
  498. max_height = 4096;
  499. } else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  500. max_width = 4096;
  501. max_height = 2048;
  502. } else {
  503. max_width = 2048;
  504. max_height = 1536;
  505. }
  506. if (intel_crtc->config.pipe_src_w > max_width ||
  507. intel_crtc->config.pipe_src_h > max_height) {
  508. if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
  509. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  510. goto out_disable;
  511. }
  512. if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
  513. intel_crtc->plane != PLANE_A) {
  514. if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
  515. DRM_DEBUG_KMS("plane not A, disabling compression\n");
  516. goto out_disable;
  517. }
  518. /* The use of a CPU fence is mandatory in order to detect writes
  519. * by the CPU to the scanout and trigger updates to the FBC.
  520. */
  521. if (obj->tiling_mode != I915_TILING_X ||
  522. obj->fence_reg == I915_FENCE_REG_NONE) {
  523. if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
  524. DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
  525. goto out_disable;
  526. }
  527. if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  528. to_intel_plane(crtc->primary)->rotation != BIT(DRM_ROTATE_0)) {
  529. if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
  530. DRM_DEBUG_KMS("Rotation unsupported, disabling\n");
  531. goto out_disable;
  532. }
  533. /* If the kernel debugger is active, always disable compression */
  534. if (in_dbg_master())
  535. goto out_disable;
  536. if (i915_gem_stolen_setup_compression(dev, obj->base.size,
  537. drm_format_plane_cpp(fb->pixel_format, 0))) {
  538. if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
  539. DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
  540. goto out_disable;
  541. }
  542. /* If the scanout has not changed, don't modify the FBC settings.
  543. * Note that we make the fundamental assumption that the fb->obj
  544. * cannot be unpinned (and have its GTT offset and fence revoked)
  545. * without first being decoupled from the scanout and FBC disabled.
  546. */
  547. if (dev_priv->fbc.plane == intel_crtc->plane &&
  548. dev_priv->fbc.fb_id == fb->base.id &&
  549. dev_priv->fbc.y == crtc->y)
  550. return;
  551. if (intel_fbc_enabled(dev)) {
  552. /* We update FBC along two paths, after changing fb/crtc
  553. * configuration (modeswitching) and after page-flipping
  554. * finishes. For the latter, we know that not only did
  555. * we disable the FBC at the start of the page-flip
  556. * sequence, but also more than one vblank has passed.
  557. *
  558. * For the former case of modeswitching, it is possible
  559. * to switch between two FBC valid configurations
  560. * instantaneously so we do need to disable the FBC
  561. * before we can modify its control registers. We also
  562. * have to wait for the next vblank for that to take
  563. * effect. However, since we delay enabling FBC we can
  564. * assume that a vblank has passed since disabling and
  565. * that we can safely alter the registers in the deferred
  566. * callback.
  567. *
  568. * In the scenario that we go from a valid to invalid
  569. * and then back to valid FBC configuration we have
  570. * no strict enforcement that a vblank occurred since
  571. * disabling the FBC. However, along all current pipe
  572. * disabling paths we do need to wait for a vblank at
  573. * some point. And we wait before enabling FBC anyway.
  574. */
  575. DRM_DEBUG_KMS("disabling active FBC for update\n");
  576. intel_disable_fbc(dev);
  577. }
  578. intel_enable_fbc(crtc);
  579. dev_priv->fbc.no_fbc_reason = FBC_OK;
  580. return;
  581. out_disable:
  582. /* Multiple disables should be harmless */
  583. if (intel_fbc_enabled(dev)) {
  584. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  585. intel_disable_fbc(dev);
  586. }
  587. i915_gem_stolen_cleanup_compression(dev);
  588. }
  589. static void i915_pineview_get_mem_freq(struct drm_device *dev)
  590. {
  591. struct drm_i915_private *dev_priv = dev->dev_private;
  592. u32 tmp;
  593. tmp = I915_READ(CLKCFG);
  594. switch (tmp & CLKCFG_FSB_MASK) {
  595. case CLKCFG_FSB_533:
  596. dev_priv->fsb_freq = 533; /* 133*4 */
  597. break;
  598. case CLKCFG_FSB_800:
  599. dev_priv->fsb_freq = 800; /* 200*4 */
  600. break;
  601. case CLKCFG_FSB_667:
  602. dev_priv->fsb_freq = 667; /* 167*4 */
  603. break;
  604. case CLKCFG_FSB_400:
  605. dev_priv->fsb_freq = 400; /* 100*4 */
  606. break;
  607. }
  608. switch (tmp & CLKCFG_MEM_MASK) {
  609. case CLKCFG_MEM_533:
  610. dev_priv->mem_freq = 533;
  611. break;
  612. case CLKCFG_MEM_667:
  613. dev_priv->mem_freq = 667;
  614. break;
  615. case CLKCFG_MEM_800:
  616. dev_priv->mem_freq = 800;
  617. break;
  618. }
  619. /* detect pineview DDR3 setting */
  620. tmp = I915_READ(CSHRDDR3CTL);
  621. dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
  622. }
  623. static void i915_ironlake_get_mem_freq(struct drm_device *dev)
  624. {
  625. struct drm_i915_private *dev_priv = dev->dev_private;
  626. u16 ddrpll, csipll;
  627. ddrpll = I915_READ16(DDRMPLL1);
  628. csipll = I915_READ16(CSIPLL0);
  629. switch (ddrpll & 0xff) {
  630. case 0xc:
  631. dev_priv->mem_freq = 800;
  632. break;
  633. case 0x10:
  634. dev_priv->mem_freq = 1066;
  635. break;
  636. case 0x14:
  637. dev_priv->mem_freq = 1333;
  638. break;
  639. case 0x18:
  640. dev_priv->mem_freq = 1600;
  641. break;
  642. default:
  643. DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
  644. ddrpll & 0xff);
  645. dev_priv->mem_freq = 0;
  646. break;
  647. }
  648. dev_priv->ips.r_t = dev_priv->mem_freq;
  649. switch (csipll & 0x3ff) {
  650. case 0x00c:
  651. dev_priv->fsb_freq = 3200;
  652. break;
  653. case 0x00e:
  654. dev_priv->fsb_freq = 3733;
  655. break;
  656. case 0x010:
  657. dev_priv->fsb_freq = 4266;
  658. break;
  659. case 0x012:
  660. dev_priv->fsb_freq = 4800;
  661. break;
  662. case 0x014:
  663. dev_priv->fsb_freq = 5333;
  664. break;
  665. case 0x016:
  666. dev_priv->fsb_freq = 5866;
  667. break;
  668. case 0x018:
  669. dev_priv->fsb_freq = 6400;
  670. break;
  671. default:
  672. DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
  673. csipll & 0x3ff);
  674. dev_priv->fsb_freq = 0;
  675. break;
  676. }
  677. if (dev_priv->fsb_freq == 3200) {
  678. dev_priv->ips.c_m = 0;
  679. } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
  680. dev_priv->ips.c_m = 1;
  681. } else {
  682. dev_priv->ips.c_m = 2;
  683. }
  684. }
  685. static const struct cxsr_latency cxsr_latency_table[] = {
  686. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  687. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  688. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  689. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  690. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  691. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  692. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  693. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  694. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  695. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  696. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  697. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  698. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  699. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  700. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  701. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  702. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  703. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  704. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  705. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  706. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  707. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  708. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  709. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  710. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  711. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  712. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  713. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  714. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  715. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  716. };
  717. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  718. int is_ddr3,
  719. int fsb,
  720. int mem)
  721. {
  722. const struct cxsr_latency *latency;
  723. int i;
  724. if (fsb == 0 || mem == 0)
  725. return NULL;
  726. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  727. latency = &cxsr_latency_table[i];
  728. if (is_desktop == latency->is_desktop &&
  729. is_ddr3 == latency->is_ddr3 &&
  730. fsb == latency->fsb_freq && mem == latency->mem_freq)
  731. return latency;
  732. }
  733. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  734. return NULL;
  735. }
  736. void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
  737. {
  738. struct drm_device *dev = dev_priv->dev;
  739. u32 val;
  740. if (IS_VALLEYVIEW(dev)) {
  741. I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
  742. } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
  743. I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
  744. } else if (IS_PINEVIEW(dev)) {
  745. val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
  746. val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
  747. I915_WRITE(DSPFW3, val);
  748. } else if (IS_I945G(dev) || IS_I945GM(dev)) {
  749. val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
  750. _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
  751. I915_WRITE(FW_BLC_SELF, val);
  752. } else if (IS_I915GM(dev)) {
  753. val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
  754. _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
  755. I915_WRITE(INSTPM, val);
  756. } else {
  757. return;
  758. }
  759. DRM_DEBUG_KMS("memory self-refresh is %s\n",
  760. enable ? "enabled" : "disabled");
  761. }
  762. /*
  763. * Latency for FIFO fetches is dependent on several factors:
  764. * - memory configuration (speed, channels)
  765. * - chipset
  766. * - current MCH state
  767. * It can be fairly high in some situations, so here we assume a fairly
  768. * pessimal value. It's a tradeoff between extra memory fetches (if we
  769. * set this value too high, the FIFO will fetch frequently to stay full)
  770. * and power consumption (set it too low to save power and we might see
  771. * FIFO underruns and display "flicker").
  772. *
  773. * A value of 5us seems to be a good balance; safe for very low end
  774. * platforms but not overly aggressive on lower latency configs.
  775. */
  776. static const int pessimal_latency_ns = 5000;
  777. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  778. {
  779. struct drm_i915_private *dev_priv = dev->dev_private;
  780. uint32_t dsparb = I915_READ(DSPARB);
  781. int size;
  782. size = dsparb & 0x7f;
  783. if (plane)
  784. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  785. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  786. plane ? "B" : "A", size);
  787. return size;
  788. }
  789. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  790. {
  791. struct drm_i915_private *dev_priv = dev->dev_private;
  792. uint32_t dsparb = I915_READ(DSPARB);
  793. int size;
  794. size = dsparb & 0x1ff;
  795. if (plane)
  796. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  797. size >>= 1; /* Convert to cachelines */
  798. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  799. plane ? "B" : "A", size);
  800. return size;
  801. }
  802. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  803. {
  804. struct drm_i915_private *dev_priv = dev->dev_private;
  805. uint32_t dsparb = I915_READ(DSPARB);
  806. int size;
  807. size = dsparb & 0x7f;
  808. size >>= 2; /* Convert to cachelines */
  809. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  810. plane ? "B" : "A",
  811. size);
  812. return size;
  813. }
  814. /* Pineview has different values for various configs */
  815. static const struct intel_watermark_params pineview_display_wm = {
  816. .fifo_size = PINEVIEW_DISPLAY_FIFO,
  817. .max_wm = PINEVIEW_MAX_WM,
  818. .default_wm = PINEVIEW_DFT_WM,
  819. .guard_size = PINEVIEW_GUARD_WM,
  820. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  821. };
  822. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  823. .fifo_size = PINEVIEW_DISPLAY_FIFO,
  824. .max_wm = PINEVIEW_MAX_WM,
  825. .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
  826. .guard_size = PINEVIEW_GUARD_WM,
  827. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  828. };
  829. static const struct intel_watermark_params pineview_cursor_wm = {
  830. .fifo_size = PINEVIEW_CURSOR_FIFO,
  831. .max_wm = PINEVIEW_CURSOR_MAX_WM,
  832. .default_wm = PINEVIEW_CURSOR_DFT_WM,
  833. .guard_size = PINEVIEW_CURSOR_GUARD_WM,
  834. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  835. };
  836. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  837. .fifo_size = PINEVIEW_CURSOR_FIFO,
  838. .max_wm = PINEVIEW_CURSOR_MAX_WM,
  839. .default_wm = PINEVIEW_CURSOR_DFT_WM,
  840. .guard_size = PINEVIEW_CURSOR_GUARD_WM,
  841. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  842. };
  843. static const struct intel_watermark_params g4x_wm_info = {
  844. .fifo_size = G4X_FIFO_SIZE,
  845. .max_wm = G4X_MAX_WM,
  846. .default_wm = G4X_MAX_WM,
  847. .guard_size = 2,
  848. .cacheline_size = G4X_FIFO_LINE_SIZE,
  849. };
  850. static const struct intel_watermark_params g4x_cursor_wm_info = {
  851. .fifo_size = I965_CURSOR_FIFO,
  852. .max_wm = I965_CURSOR_MAX_WM,
  853. .default_wm = I965_CURSOR_DFT_WM,
  854. .guard_size = 2,
  855. .cacheline_size = G4X_FIFO_LINE_SIZE,
  856. };
  857. static const struct intel_watermark_params valleyview_wm_info = {
  858. .fifo_size = VALLEYVIEW_FIFO_SIZE,
  859. .max_wm = VALLEYVIEW_MAX_WM,
  860. .default_wm = VALLEYVIEW_MAX_WM,
  861. .guard_size = 2,
  862. .cacheline_size = G4X_FIFO_LINE_SIZE,
  863. };
  864. static const struct intel_watermark_params valleyview_cursor_wm_info = {
  865. .fifo_size = I965_CURSOR_FIFO,
  866. .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
  867. .default_wm = I965_CURSOR_DFT_WM,
  868. .guard_size = 2,
  869. .cacheline_size = G4X_FIFO_LINE_SIZE,
  870. };
  871. static const struct intel_watermark_params i965_cursor_wm_info = {
  872. .fifo_size = I965_CURSOR_FIFO,
  873. .max_wm = I965_CURSOR_MAX_WM,
  874. .default_wm = I965_CURSOR_DFT_WM,
  875. .guard_size = 2,
  876. .cacheline_size = I915_FIFO_LINE_SIZE,
  877. };
  878. static const struct intel_watermark_params i945_wm_info = {
  879. .fifo_size = I945_FIFO_SIZE,
  880. .max_wm = I915_MAX_WM,
  881. .default_wm = 1,
  882. .guard_size = 2,
  883. .cacheline_size = I915_FIFO_LINE_SIZE,
  884. };
  885. static const struct intel_watermark_params i915_wm_info = {
  886. .fifo_size = I915_FIFO_SIZE,
  887. .max_wm = I915_MAX_WM,
  888. .default_wm = 1,
  889. .guard_size = 2,
  890. .cacheline_size = I915_FIFO_LINE_SIZE,
  891. };
  892. static const struct intel_watermark_params i830_a_wm_info = {
  893. .fifo_size = I855GM_FIFO_SIZE,
  894. .max_wm = I915_MAX_WM,
  895. .default_wm = 1,
  896. .guard_size = 2,
  897. .cacheline_size = I830_FIFO_LINE_SIZE,
  898. };
  899. static const struct intel_watermark_params i830_bc_wm_info = {
  900. .fifo_size = I855GM_FIFO_SIZE,
  901. .max_wm = I915_MAX_WM/2,
  902. .default_wm = 1,
  903. .guard_size = 2,
  904. .cacheline_size = I830_FIFO_LINE_SIZE,
  905. };
  906. static const struct intel_watermark_params i845_wm_info = {
  907. .fifo_size = I830_FIFO_SIZE,
  908. .max_wm = I915_MAX_WM,
  909. .default_wm = 1,
  910. .guard_size = 2,
  911. .cacheline_size = I830_FIFO_LINE_SIZE,
  912. };
  913. /**
  914. * intel_calculate_wm - calculate watermark level
  915. * @clock_in_khz: pixel clock
  916. * @wm: chip FIFO params
  917. * @pixel_size: display pixel size
  918. * @latency_ns: memory latency for the platform
  919. *
  920. * Calculate the watermark level (the level at which the display plane will
  921. * start fetching from memory again). Each chip has a different display
  922. * FIFO size and allocation, so the caller needs to figure that out and pass
  923. * in the correct intel_watermark_params structure.
  924. *
  925. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  926. * on the pixel size. When it reaches the watermark level, it'll start
  927. * fetching FIFO line sized based chunks from memory until the FIFO fills
  928. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  929. * will occur, and a display engine hang could result.
  930. */
  931. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  932. const struct intel_watermark_params *wm,
  933. int fifo_size,
  934. int pixel_size,
  935. unsigned long latency_ns)
  936. {
  937. long entries_required, wm_size;
  938. /*
  939. * Note: we need to make sure we don't overflow for various clock &
  940. * latency values.
  941. * clocks go from a few thousand to several hundred thousand.
  942. * latency is usually a few thousand
  943. */
  944. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  945. 1000;
  946. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  947. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  948. wm_size = fifo_size - (entries_required + wm->guard_size);
  949. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  950. /* Don't promote wm_size to unsigned... */
  951. if (wm_size > (long)wm->max_wm)
  952. wm_size = wm->max_wm;
  953. if (wm_size <= 0)
  954. wm_size = wm->default_wm;
  955. /*
  956. * Bspec seems to indicate that the value shouldn't be lower than
  957. * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
  958. * Lets go for 8 which is the burst size since certain platforms
  959. * already use a hardcoded 8 (which is what the spec says should be
  960. * done).
  961. */
  962. if (wm_size <= 8)
  963. wm_size = 8;
  964. return wm_size;
  965. }
  966. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  967. {
  968. struct drm_crtc *crtc, *enabled = NULL;
  969. for_each_crtc(dev, crtc) {
  970. if (intel_crtc_active(crtc)) {
  971. if (enabled)
  972. return NULL;
  973. enabled = crtc;
  974. }
  975. }
  976. return enabled;
  977. }
  978. static void pineview_update_wm(struct drm_crtc *unused_crtc)
  979. {
  980. struct drm_device *dev = unused_crtc->dev;
  981. struct drm_i915_private *dev_priv = dev->dev_private;
  982. struct drm_crtc *crtc;
  983. const struct cxsr_latency *latency;
  984. u32 reg;
  985. unsigned long wm;
  986. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  987. dev_priv->fsb_freq, dev_priv->mem_freq);
  988. if (!latency) {
  989. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  990. intel_set_memory_cxsr(dev_priv, false);
  991. return;
  992. }
  993. crtc = single_enabled_crtc(dev);
  994. if (crtc) {
  995. const struct drm_display_mode *adjusted_mode;
  996. int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
  997. int clock;
  998. adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
  999. clock = adjusted_mode->crtc_clock;
  1000. /* Display SR */
  1001. wm = intel_calculate_wm(clock, &pineview_display_wm,
  1002. pineview_display_wm.fifo_size,
  1003. pixel_size, latency->display_sr);
  1004. reg = I915_READ(DSPFW1);
  1005. reg &= ~DSPFW_SR_MASK;
  1006. reg |= wm << DSPFW_SR_SHIFT;
  1007. I915_WRITE(DSPFW1, reg);
  1008. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  1009. /* cursor SR */
  1010. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  1011. pineview_display_wm.fifo_size,
  1012. pixel_size, latency->cursor_sr);
  1013. reg = I915_READ(DSPFW3);
  1014. reg &= ~DSPFW_CURSOR_SR_MASK;
  1015. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  1016. I915_WRITE(DSPFW3, reg);
  1017. /* Display HPLL off SR */
  1018. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  1019. pineview_display_hplloff_wm.fifo_size,
  1020. pixel_size, latency->display_hpll_disable);
  1021. reg = I915_READ(DSPFW3);
  1022. reg &= ~DSPFW_HPLL_SR_MASK;
  1023. reg |= wm & DSPFW_HPLL_SR_MASK;
  1024. I915_WRITE(DSPFW3, reg);
  1025. /* cursor HPLL off SR */
  1026. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  1027. pineview_display_hplloff_wm.fifo_size,
  1028. pixel_size, latency->cursor_hpll_disable);
  1029. reg = I915_READ(DSPFW3);
  1030. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  1031. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  1032. I915_WRITE(DSPFW3, reg);
  1033. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  1034. intel_set_memory_cxsr(dev_priv, true);
  1035. } else {
  1036. intel_set_memory_cxsr(dev_priv, false);
  1037. }
  1038. }
  1039. static bool g4x_compute_wm0(struct drm_device *dev,
  1040. int plane,
  1041. const struct intel_watermark_params *display,
  1042. int display_latency_ns,
  1043. const struct intel_watermark_params *cursor,
  1044. int cursor_latency_ns,
  1045. int *plane_wm,
  1046. int *cursor_wm)
  1047. {
  1048. struct drm_crtc *crtc;
  1049. const struct drm_display_mode *adjusted_mode;
  1050. int htotal, hdisplay, clock, pixel_size;
  1051. int line_time_us, line_count;
  1052. int entries, tlb_miss;
  1053. crtc = intel_get_crtc_for_plane(dev, plane);
  1054. if (!intel_crtc_active(crtc)) {
  1055. *cursor_wm = cursor->guard_size;
  1056. *plane_wm = display->guard_size;
  1057. return false;
  1058. }
  1059. adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
  1060. clock = adjusted_mode->crtc_clock;
  1061. htotal = adjusted_mode->crtc_htotal;
  1062. hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
  1063. pixel_size = crtc->primary->fb->bits_per_pixel / 8;
  1064. /* Use the small buffer method to calculate plane watermark */
  1065. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  1066. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  1067. if (tlb_miss > 0)
  1068. entries += tlb_miss;
  1069. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  1070. *plane_wm = entries + display->guard_size;
  1071. if (*plane_wm > (int)display->max_wm)
  1072. *plane_wm = display->max_wm;
  1073. /* Use the large buffer method to calculate cursor watermark */
  1074. line_time_us = max(htotal * 1000 / clock, 1);
  1075. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  1076. entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
  1077. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  1078. if (tlb_miss > 0)
  1079. entries += tlb_miss;
  1080. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1081. *cursor_wm = entries + cursor->guard_size;
  1082. if (*cursor_wm > (int)cursor->max_wm)
  1083. *cursor_wm = (int)cursor->max_wm;
  1084. return true;
  1085. }
  1086. /*
  1087. * Check the wm result.
  1088. *
  1089. * If any calculated watermark values is larger than the maximum value that
  1090. * can be programmed into the associated watermark register, that watermark
  1091. * must be disabled.
  1092. */
  1093. static bool g4x_check_srwm(struct drm_device *dev,
  1094. int display_wm, int cursor_wm,
  1095. const struct intel_watermark_params *display,
  1096. const struct intel_watermark_params *cursor)
  1097. {
  1098. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  1099. display_wm, cursor_wm);
  1100. if (display_wm > display->max_wm) {
  1101. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  1102. display_wm, display->max_wm);
  1103. return false;
  1104. }
  1105. if (cursor_wm > cursor->max_wm) {
  1106. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  1107. cursor_wm, cursor->max_wm);
  1108. return false;
  1109. }
  1110. if (!(display_wm || cursor_wm)) {
  1111. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  1112. return false;
  1113. }
  1114. return true;
  1115. }
  1116. static bool g4x_compute_srwm(struct drm_device *dev,
  1117. int plane,
  1118. int latency_ns,
  1119. const struct intel_watermark_params *display,
  1120. const struct intel_watermark_params *cursor,
  1121. int *display_wm, int *cursor_wm)
  1122. {
  1123. struct drm_crtc *crtc;
  1124. const struct drm_display_mode *adjusted_mode;
  1125. int hdisplay, htotal, pixel_size, clock;
  1126. unsigned long line_time_us;
  1127. int line_count, line_size;
  1128. int small, large;
  1129. int entries;
  1130. if (!latency_ns) {
  1131. *display_wm = *cursor_wm = 0;
  1132. return false;
  1133. }
  1134. crtc = intel_get_crtc_for_plane(dev, plane);
  1135. adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
  1136. clock = adjusted_mode->crtc_clock;
  1137. htotal = adjusted_mode->crtc_htotal;
  1138. hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
  1139. pixel_size = crtc->primary->fb->bits_per_pixel / 8;
  1140. line_time_us = max(htotal * 1000 / clock, 1);
  1141. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1142. line_size = hdisplay * pixel_size;
  1143. /* Use the minimum of the small and large buffer method for primary */
  1144. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1145. large = line_count * line_size;
  1146. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1147. *display_wm = entries + display->guard_size;
  1148. /* calculate the self-refresh watermark for display cursor */
  1149. entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
  1150. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1151. *cursor_wm = entries + cursor->guard_size;
  1152. return g4x_check_srwm(dev,
  1153. *display_wm, *cursor_wm,
  1154. display, cursor);
  1155. }
  1156. static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
  1157. int pixel_size,
  1158. int *prec_mult,
  1159. int *drain_latency)
  1160. {
  1161. struct drm_device *dev = crtc->dev;
  1162. int entries;
  1163. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  1164. if (WARN(clock == 0, "Pixel clock is zero!\n"))
  1165. return false;
  1166. if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
  1167. return false;
  1168. entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
  1169. if (IS_CHERRYVIEW(dev))
  1170. *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_32 :
  1171. DRAIN_LATENCY_PRECISION_16;
  1172. else
  1173. *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
  1174. DRAIN_LATENCY_PRECISION_32;
  1175. *drain_latency = (64 * (*prec_mult) * 4) / entries;
  1176. if (*drain_latency > DRAIN_LATENCY_MASK)
  1177. *drain_latency = DRAIN_LATENCY_MASK;
  1178. return true;
  1179. }
  1180. /*
  1181. * Update drain latency registers of memory arbiter
  1182. *
  1183. * Valleyview SoC has a new memory arbiter and needs drain latency registers
  1184. * to be programmed. Each plane has a drain latency multiplier and a drain
  1185. * latency value.
  1186. */
  1187. static void vlv_update_drain_latency(struct drm_crtc *crtc)
  1188. {
  1189. struct drm_device *dev = crtc->dev;
  1190. struct drm_i915_private *dev_priv = dev->dev_private;
  1191. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1192. int pixel_size;
  1193. int drain_latency;
  1194. enum pipe pipe = intel_crtc->pipe;
  1195. int plane_prec, prec_mult, plane_dl;
  1196. const int high_precision = IS_CHERRYVIEW(dev) ?
  1197. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
  1198. plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_HIGH |
  1199. DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_HIGH |
  1200. (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
  1201. if (!intel_crtc_active(crtc)) {
  1202. I915_WRITE(VLV_DDL(pipe), plane_dl);
  1203. return;
  1204. }
  1205. /* Primary plane Drain Latency */
  1206. pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
  1207. if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
  1208. plane_prec = (prec_mult == high_precision) ?
  1209. DDL_PLANE_PRECISION_HIGH :
  1210. DDL_PLANE_PRECISION_LOW;
  1211. plane_dl |= plane_prec | drain_latency;
  1212. }
  1213. /* Cursor Drain Latency
  1214. * BPP is always 4 for cursor
  1215. */
  1216. pixel_size = 4;
  1217. /* Program cursor DL only if it is enabled */
  1218. if (intel_crtc->cursor_base &&
  1219. vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
  1220. plane_prec = (prec_mult == high_precision) ?
  1221. DDL_CURSOR_PRECISION_HIGH :
  1222. DDL_CURSOR_PRECISION_LOW;
  1223. plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
  1224. }
  1225. I915_WRITE(VLV_DDL(pipe), plane_dl);
  1226. }
  1227. #define single_plane_enabled(mask) is_power_of_2(mask)
  1228. static void valleyview_update_wm(struct drm_crtc *crtc)
  1229. {
  1230. struct drm_device *dev = crtc->dev;
  1231. static const int sr_latency_ns = 12000;
  1232. struct drm_i915_private *dev_priv = dev->dev_private;
  1233. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1234. int plane_sr, cursor_sr;
  1235. int ignore_plane_sr, ignore_cursor_sr;
  1236. unsigned int enabled = 0;
  1237. bool cxsr_enabled;
  1238. vlv_update_drain_latency(crtc);
  1239. if (g4x_compute_wm0(dev, PIPE_A,
  1240. &valleyview_wm_info, pessimal_latency_ns,
  1241. &valleyview_cursor_wm_info, pessimal_latency_ns,
  1242. &planea_wm, &cursora_wm))
  1243. enabled |= 1 << PIPE_A;
  1244. if (g4x_compute_wm0(dev, PIPE_B,
  1245. &valleyview_wm_info, pessimal_latency_ns,
  1246. &valleyview_cursor_wm_info, pessimal_latency_ns,
  1247. &planeb_wm, &cursorb_wm))
  1248. enabled |= 1 << PIPE_B;
  1249. if (single_plane_enabled(enabled) &&
  1250. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1251. sr_latency_ns,
  1252. &valleyview_wm_info,
  1253. &valleyview_cursor_wm_info,
  1254. &plane_sr, &ignore_cursor_sr) &&
  1255. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1256. 2*sr_latency_ns,
  1257. &valleyview_wm_info,
  1258. &valleyview_cursor_wm_info,
  1259. &ignore_plane_sr, &cursor_sr)) {
  1260. cxsr_enabled = true;
  1261. } else {
  1262. cxsr_enabled = false;
  1263. intel_set_memory_cxsr(dev_priv, false);
  1264. plane_sr = cursor_sr = 0;
  1265. }
  1266. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
  1267. "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1268. planea_wm, cursora_wm,
  1269. planeb_wm, cursorb_wm,
  1270. plane_sr, cursor_sr);
  1271. I915_WRITE(DSPFW1,
  1272. (plane_sr << DSPFW_SR_SHIFT) |
  1273. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1274. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1275. (planea_wm << DSPFW_PLANEA_SHIFT));
  1276. I915_WRITE(DSPFW2,
  1277. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  1278. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1279. I915_WRITE(DSPFW3,
  1280. (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
  1281. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1282. if (cxsr_enabled)
  1283. intel_set_memory_cxsr(dev_priv, true);
  1284. }
  1285. static void cherryview_update_wm(struct drm_crtc *crtc)
  1286. {
  1287. struct drm_device *dev = crtc->dev;
  1288. static const int sr_latency_ns = 12000;
  1289. struct drm_i915_private *dev_priv = dev->dev_private;
  1290. int planea_wm, planeb_wm, planec_wm;
  1291. int cursora_wm, cursorb_wm, cursorc_wm;
  1292. int plane_sr, cursor_sr;
  1293. int ignore_plane_sr, ignore_cursor_sr;
  1294. unsigned int enabled = 0;
  1295. bool cxsr_enabled;
  1296. vlv_update_drain_latency(crtc);
  1297. if (g4x_compute_wm0(dev, PIPE_A,
  1298. &valleyview_wm_info, pessimal_latency_ns,
  1299. &valleyview_cursor_wm_info, pessimal_latency_ns,
  1300. &planea_wm, &cursora_wm))
  1301. enabled |= 1 << PIPE_A;
  1302. if (g4x_compute_wm0(dev, PIPE_B,
  1303. &valleyview_wm_info, pessimal_latency_ns,
  1304. &valleyview_cursor_wm_info, pessimal_latency_ns,
  1305. &planeb_wm, &cursorb_wm))
  1306. enabled |= 1 << PIPE_B;
  1307. if (g4x_compute_wm0(dev, PIPE_C,
  1308. &valleyview_wm_info, pessimal_latency_ns,
  1309. &valleyview_cursor_wm_info, pessimal_latency_ns,
  1310. &planec_wm, &cursorc_wm))
  1311. enabled |= 1 << PIPE_C;
  1312. if (single_plane_enabled(enabled) &&
  1313. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1314. sr_latency_ns,
  1315. &valleyview_wm_info,
  1316. &valleyview_cursor_wm_info,
  1317. &plane_sr, &ignore_cursor_sr) &&
  1318. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1319. 2*sr_latency_ns,
  1320. &valleyview_wm_info,
  1321. &valleyview_cursor_wm_info,
  1322. &ignore_plane_sr, &cursor_sr)) {
  1323. cxsr_enabled = true;
  1324. } else {
  1325. cxsr_enabled = false;
  1326. intel_set_memory_cxsr(dev_priv, false);
  1327. plane_sr = cursor_sr = 0;
  1328. }
  1329. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
  1330. "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
  1331. "SR: plane=%d, cursor=%d\n",
  1332. planea_wm, cursora_wm,
  1333. planeb_wm, cursorb_wm,
  1334. planec_wm, cursorc_wm,
  1335. plane_sr, cursor_sr);
  1336. I915_WRITE(DSPFW1,
  1337. (plane_sr << DSPFW_SR_SHIFT) |
  1338. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1339. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1340. (planea_wm << DSPFW_PLANEA_SHIFT));
  1341. I915_WRITE(DSPFW2,
  1342. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  1343. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1344. I915_WRITE(DSPFW3,
  1345. (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
  1346. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1347. I915_WRITE(DSPFW9_CHV,
  1348. (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
  1349. DSPFW_CURSORC_MASK)) |
  1350. (planec_wm << DSPFW_PLANEC_SHIFT) |
  1351. (cursorc_wm << DSPFW_CURSORC_SHIFT));
  1352. if (cxsr_enabled)
  1353. intel_set_memory_cxsr(dev_priv, true);
  1354. }
  1355. static void valleyview_update_sprite_wm(struct drm_plane *plane,
  1356. struct drm_crtc *crtc,
  1357. uint32_t sprite_width,
  1358. uint32_t sprite_height,
  1359. int pixel_size,
  1360. bool enabled, bool scaled)
  1361. {
  1362. struct drm_device *dev = crtc->dev;
  1363. struct drm_i915_private *dev_priv = dev->dev_private;
  1364. int pipe = to_intel_plane(plane)->pipe;
  1365. int sprite = to_intel_plane(plane)->plane;
  1366. int drain_latency;
  1367. int plane_prec;
  1368. int sprite_dl;
  1369. int prec_mult;
  1370. const int high_precision = IS_CHERRYVIEW(dev) ?
  1371. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
  1372. sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_HIGH(sprite) |
  1373. (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
  1374. if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
  1375. &drain_latency)) {
  1376. plane_prec = (prec_mult == high_precision) ?
  1377. DDL_SPRITE_PRECISION_HIGH(sprite) :
  1378. DDL_SPRITE_PRECISION_LOW(sprite);
  1379. sprite_dl |= plane_prec |
  1380. (drain_latency << DDL_SPRITE_SHIFT(sprite));
  1381. }
  1382. I915_WRITE(VLV_DDL(pipe), sprite_dl);
  1383. }
  1384. static void g4x_update_wm(struct drm_crtc *crtc)
  1385. {
  1386. struct drm_device *dev = crtc->dev;
  1387. static const int sr_latency_ns = 12000;
  1388. struct drm_i915_private *dev_priv = dev->dev_private;
  1389. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1390. int plane_sr, cursor_sr;
  1391. unsigned int enabled = 0;
  1392. bool cxsr_enabled;
  1393. if (g4x_compute_wm0(dev, PIPE_A,
  1394. &g4x_wm_info, pessimal_latency_ns,
  1395. &g4x_cursor_wm_info, pessimal_latency_ns,
  1396. &planea_wm, &cursora_wm))
  1397. enabled |= 1 << PIPE_A;
  1398. if (g4x_compute_wm0(dev, PIPE_B,
  1399. &g4x_wm_info, pessimal_latency_ns,
  1400. &g4x_cursor_wm_info, pessimal_latency_ns,
  1401. &planeb_wm, &cursorb_wm))
  1402. enabled |= 1 << PIPE_B;
  1403. if (single_plane_enabled(enabled) &&
  1404. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1405. sr_latency_ns,
  1406. &g4x_wm_info,
  1407. &g4x_cursor_wm_info,
  1408. &plane_sr, &cursor_sr)) {
  1409. cxsr_enabled = true;
  1410. } else {
  1411. cxsr_enabled = false;
  1412. intel_set_memory_cxsr(dev_priv, false);
  1413. plane_sr = cursor_sr = 0;
  1414. }
  1415. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
  1416. "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1417. planea_wm, cursora_wm,
  1418. planeb_wm, cursorb_wm,
  1419. plane_sr, cursor_sr);
  1420. I915_WRITE(DSPFW1,
  1421. (plane_sr << DSPFW_SR_SHIFT) |
  1422. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1423. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1424. (planea_wm << DSPFW_PLANEA_SHIFT));
  1425. I915_WRITE(DSPFW2,
  1426. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  1427. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1428. /* HPLL off in SR has some issues on G4x... disable it */
  1429. I915_WRITE(DSPFW3,
  1430. (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
  1431. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1432. if (cxsr_enabled)
  1433. intel_set_memory_cxsr(dev_priv, true);
  1434. }
  1435. static void i965_update_wm(struct drm_crtc *unused_crtc)
  1436. {
  1437. struct drm_device *dev = unused_crtc->dev;
  1438. struct drm_i915_private *dev_priv = dev->dev_private;
  1439. struct drm_crtc *crtc;
  1440. int srwm = 1;
  1441. int cursor_sr = 16;
  1442. bool cxsr_enabled;
  1443. /* Calc sr entries for one plane configs */
  1444. crtc = single_enabled_crtc(dev);
  1445. if (crtc) {
  1446. /* self-refresh has much higher latency */
  1447. static const int sr_latency_ns = 12000;
  1448. const struct drm_display_mode *adjusted_mode =
  1449. &to_intel_crtc(crtc)->config.adjusted_mode;
  1450. int clock = adjusted_mode->crtc_clock;
  1451. int htotal = adjusted_mode->crtc_htotal;
  1452. int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
  1453. int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
  1454. unsigned long line_time_us;
  1455. int entries;
  1456. line_time_us = max(htotal * 1000 / clock, 1);
  1457. /* Use ns/us then divide to preserve precision */
  1458. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1459. pixel_size * hdisplay;
  1460. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  1461. srwm = I965_FIFO_SIZE - entries;
  1462. if (srwm < 0)
  1463. srwm = 1;
  1464. srwm &= 0x1ff;
  1465. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  1466. entries, srwm);
  1467. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1468. pixel_size * to_intel_crtc(crtc)->cursor_width;
  1469. entries = DIV_ROUND_UP(entries,
  1470. i965_cursor_wm_info.cacheline_size);
  1471. cursor_sr = i965_cursor_wm_info.fifo_size -
  1472. (entries + i965_cursor_wm_info.guard_size);
  1473. if (cursor_sr > i965_cursor_wm_info.max_wm)
  1474. cursor_sr = i965_cursor_wm_info.max_wm;
  1475. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  1476. "cursor %d\n", srwm, cursor_sr);
  1477. cxsr_enabled = true;
  1478. } else {
  1479. cxsr_enabled = false;
  1480. /* Turn off self refresh if both pipes are enabled */
  1481. intel_set_memory_cxsr(dev_priv, false);
  1482. }
  1483. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  1484. srwm);
  1485. /* 965 has limitations... */
  1486. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  1487. (8 << DSPFW_CURSORB_SHIFT) |
  1488. (8 << DSPFW_PLANEB_SHIFT) |
  1489. (8 << DSPFW_PLANEA_SHIFT));
  1490. I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
  1491. (8 << DSPFW_PLANEC_SHIFT_OLD));
  1492. /* update cursor SR watermark */
  1493. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1494. if (cxsr_enabled)
  1495. intel_set_memory_cxsr(dev_priv, true);
  1496. }
  1497. static void i9xx_update_wm(struct drm_crtc *unused_crtc)
  1498. {
  1499. struct drm_device *dev = unused_crtc->dev;
  1500. struct drm_i915_private *dev_priv = dev->dev_private;
  1501. const struct intel_watermark_params *wm_info;
  1502. uint32_t fwater_lo;
  1503. uint32_t fwater_hi;
  1504. int cwm, srwm = 1;
  1505. int fifo_size;
  1506. int planea_wm, planeb_wm;
  1507. struct drm_crtc *crtc, *enabled = NULL;
  1508. if (IS_I945GM(dev))
  1509. wm_info = &i945_wm_info;
  1510. else if (!IS_GEN2(dev))
  1511. wm_info = &i915_wm_info;
  1512. else
  1513. wm_info = &i830_a_wm_info;
  1514. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  1515. crtc = intel_get_crtc_for_plane(dev, 0);
  1516. if (intel_crtc_active(crtc)) {
  1517. const struct drm_display_mode *adjusted_mode;
  1518. int cpp = crtc->primary->fb->bits_per_pixel / 8;
  1519. if (IS_GEN2(dev))
  1520. cpp = 4;
  1521. adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
  1522. planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  1523. wm_info, fifo_size, cpp,
  1524. pessimal_latency_ns);
  1525. enabled = crtc;
  1526. } else {
  1527. planea_wm = fifo_size - wm_info->guard_size;
  1528. if (planea_wm > (long)wm_info->max_wm)
  1529. planea_wm = wm_info->max_wm;
  1530. }
  1531. if (IS_GEN2(dev))
  1532. wm_info = &i830_bc_wm_info;
  1533. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  1534. crtc = intel_get_crtc_for_plane(dev, 1);
  1535. if (intel_crtc_active(crtc)) {
  1536. const struct drm_display_mode *adjusted_mode;
  1537. int cpp = crtc->primary->fb->bits_per_pixel / 8;
  1538. if (IS_GEN2(dev))
  1539. cpp = 4;
  1540. adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
  1541. planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  1542. wm_info, fifo_size, cpp,
  1543. pessimal_latency_ns);
  1544. if (enabled == NULL)
  1545. enabled = crtc;
  1546. else
  1547. enabled = NULL;
  1548. } else {
  1549. planeb_wm = fifo_size - wm_info->guard_size;
  1550. if (planeb_wm > (long)wm_info->max_wm)
  1551. planeb_wm = wm_info->max_wm;
  1552. }
  1553. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  1554. if (IS_I915GM(dev) && enabled) {
  1555. struct drm_i915_gem_object *obj;
  1556. obj = intel_fb_obj(enabled->primary->fb);
  1557. /* self-refresh seems busted with untiled */
  1558. if (obj->tiling_mode == I915_TILING_NONE)
  1559. enabled = NULL;
  1560. }
  1561. /*
  1562. * Overlay gets an aggressive default since video jitter is bad.
  1563. */
  1564. cwm = 2;
  1565. /* Play safe and disable self-refresh before adjusting watermarks. */
  1566. intel_set_memory_cxsr(dev_priv, false);
  1567. /* Calc sr entries for one plane configs */
  1568. if (HAS_FW_BLC(dev) && enabled) {
  1569. /* self-refresh has much higher latency */
  1570. static const int sr_latency_ns = 6000;
  1571. const struct drm_display_mode *adjusted_mode =
  1572. &to_intel_crtc(enabled)->config.adjusted_mode;
  1573. int clock = adjusted_mode->crtc_clock;
  1574. int htotal = adjusted_mode->crtc_htotal;
  1575. int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
  1576. int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
  1577. unsigned long line_time_us;
  1578. int entries;
  1579. line_time_us = max(htotal * 1000 / clock, 1);
  1580. /* Use ns/us then divide to preserve precision */
  1581. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1582. pixel_size * hdisplay;
  1583. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  1584. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  1585. srwm = wm_info->fifo_size - entries;
  1586. if (srwm < 0)
  1587. srwm = 1;
  1588. if (IS_I945G(dev) || IS_I945GM(dev))
  1589. I915_WRITE(FW_BLC_SELF,
  1590. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  1591. else if (IS_I915GM(dev))
  1592. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  1593. }
  1594. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  1595. planea_wm, planeb_wm, cwm, srwm);
  1596. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  1597. fwater_hi = (cwm & 0x1f);
  1598. /* Set request length to 8 cachelines per fetch */
  1599. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  1600. fwater_hi = fwater_hi | (1 << 8);
  1601. I915_WRITE(FW_BLC, fwater_lo);
  1602. I915_WRITE(FW_BLC2, fwater_hi);
  1603. if (enabled)
  1604. intel_set_memory_cxsr(dev_priv, true);
  1605. }
  1606. static void i845_update_wm(struct drm_crtc *unused_crtc)
  1607. {
  1608. struct drm_device *dev = unused_crtc->dev;
  1609. struct drm_i915_private *dev_priv = dev->dev_private;
  1610. struct drm_crtc *crtc;
  1611. const struct drm_display_mode *adjusted_mode;
  1612. uint32_t fwater_lo;
  1613. int planea_wm;
  1614. crtc = single_enabled_crtc(dev);
  1615. if (crtc == NULL)
  1616. return;
  1617. adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
  1618. planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  1619. &i845_wm_info,
  1620. dev_priv->display.get_fifo_size(dev, 0),
  1621. 4, pessimal_latency_ns);
  1622. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  1623. fwater_lo |= (3<<8) | planea_wm;
  1624. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  1625. I915_WRITE(FW_BLC, fwater_lo);
  1626. }
  1627. static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
  1628. struct drm_crtc *crtc)
  1629. {
  1630. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1631. uint32_t pixel_rate;
  1632. pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
  1633. /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
  1634. * adjust the pixel_rate here. */
  1635. if (intel_crtc->config.pch_pfit.enabled) {
  1636. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  1637. uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
  1638. pipe_w = intel_crtc->config.pipe_src_w;
  1639. pipe_h = intel_crtc->config.pipe_src_h;
  1640. pfit_w = (pfit_size >> 16) & 0xFFFF;
  1641. pfit_h = pfit_size & 0xFFFF;
  1642. if (pipe_w < pfit_w)
  1643. pipe_w = pfit_w;
  1644. if (pipe_h < pfit_h)
  1645. pipe_h = pfit_h;
  1646. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  1647. pfit_w * pfit_h);
  1648. }
  1649. return pixel_rate;
  1650. }
  1651. /* latency must be in 0.1us units. */
  1652. static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
  1653. uint32_t latency)
  1654. {
  1655. uint64_t ret;
  1656. if (WARN(latency == 0, "Latency value missing\n"))
  1657. return UINT_MAX;
  1658. ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
  1659. ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
  1660. return ret;
  1661. }
  1662. /* latency must be in 0.1us units. */
  1663. static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
  1664. uint32_t horiz_pixels, uint8_t bytes_per_pixel,
  1665. uint32_t latency)
  1666. {
  1667. uint32_t ret;
  1668. if (WARN(latency == 0, "Latency value missing\n"))
  1669. return UINT_MAX;
  1670. ret = (latency * pixel_rate) / (pipe_htotal * 10000);
  1671. ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
  1672. ret = DIV_ROUND_UP(ret, 64) + 2;
  1673. return ret;
  1674. }
  1675. static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
  1676. uint8_t bytes_per_pixel)
  1677. {
  1678. return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
  1679. }
  1680. struct skl_pipe_wm_parameters {
  1681. bool active;
  1682. uint32_t pipe_htotal;
  1683. uint32_t pixel_rate; /* in KHz */
  1684. struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
  1685. struct intel_plane_wm_parameters cursor;
  1686. };
  1687. struct ilk_pipe_wm_parameters {
  1688. bool active;
  1689. uint32_t pipe_htotal;
  1690. uint32_t pixel_rate;
  1691. struct intel_plane_wm_parameters pri;
  1692. struct intel_plane_wm_parameters spr;
  1693. struct intel_plane_wm_parameters cur;
  1694. };
  1695. struct ilk_wm_maximums {
  1696. uint16_t pri;
  1697. uint16_t spr;
  1698. uint16_t cur;
  1699. uint16_t fbc;
  1700. };
  1701. /* used in computing the new watermarks state */
  1702. struct intel_wm_config {
  1703. unsigned int num_pipes_active;
  1704. bool sprites_enabled;
  1705. bool sprites_scaled;
  1706. };
  1707. /*
  1708. * For both WM_PIPE and WM_LP.
  1709. * mem_value must be in 0.1us units.
  1710. */
  1711. static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
  1712. uint32_t mem_value,
  1713. bool is_lp)
  1714. {
  1715. uint32_t method1, method2;
  1716. if (!params->active || !params->pri.enabled)
  1717. return 0;
  1718. method1 = ilk_wm_method1(params->pixel_rate,
  1719. params->pri.bytes_per_pixel,
  1720. mem_value);
  1721. if (!is_lp)
  1722. return method1;
  1723. method2 = ilk_wm_method2(params->pixel_rate,
  1724. params->pipe_htotal,
  1725. params->pri.horiz_pixels,
  1726. params->pri.bytes_per_pixel,
  1727. mem_value);
  1728. return min(method1, method2);
  1729. }
  1730. /*
  1731. * For both WM_PIPE and WM_LP.
  1732. * mem_value must be in 0.1us units.
  1733. */
  1734. static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
  1735. uint32_t mem_value)
  1736. {
  1737. uint32_t method1, method2;
  1738. if (!params->active || !params->spr.enabled)
  1739. return 0;
  1740. method1 = ilk_wm_method1(params->pixel_rate,
  1741. params->spr.bytes_per_pixel,
  1742. mem_value);
  1743. method2 = ilk_wm_method2(params->pixel_rate,
  1744. params->pipe_htotal,
  1745. params->spr.horiz_pixels,
  1746. params->spr.bytes_per_pixel,
  1747. mem_value);
  1748. return min(method1, method2);
  1749. }
  1750. /*
  1751. * For both WM_PIPE and WM_LP.
  1752. * mem_value must be in 0.1us units.
  1753. */
  1754. static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
  1755. uint32_t mem_value)
  1756. {
  1757. if (!params->active || !params->cur.enabled)
  1758. return 0;
  1759. return ilk_wm_method2(params->pixel_rate,
  1760. params->pipe_htotal,
  1761. params->cur.horiz_pixels,
  1762. params->cur.bytes_per_pixel,
  1763. mem_value);
  1764. }
  1765. /* Only for WM_LP. */
  1766. static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
  1767. uint32_t pri_val)
  1768. {
  1769. if (!params->active || !params->pri.enabled)
  1770. return 0;
  1771. return ilk_wm_fbc(pri_val,
  1772. params->pri.horiz_pixels,
  1773. params->pri.bytes_per_pixel);
  1774. }
  1775. static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
  1776. {
  1777. if (INTEL_INFO(dev)->gen >= 8)
  1778. return 3072;
  1779. else if (INTEL_INFO(dev)->gen >= 7)
  1780. return 768;
  1781. else
  1782. return 512;
  1783. }
  1784. static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
  1785. int level, bool is_sprite)
  1786. {
  1787. if (INTEL_INFO(dev)->gen >= 8)
  1788. /* BDW primary/sprite plane watermarks */
  1789. return level == 0 ? 255 : 2047;
  1790. else if (INTEL_INFO(dev)->gen >= 7)
  1791. /* IVB/HSW primary/sprite plane watermarks */
  1792. return level == 0 ? 127 : 1023;
  1793. else if (!is_sprite)
  1794. /* ILK/SNB primary plane watermarks */
  1795. return level == 0 ? 127 : 511;
  1796. else
  1797. /* ILK/SNB sprite plane watermarks */
  1798. return level == 0 ? 63 : 255;
  1799. }
  1800. static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
  1801. int level)
  1802. {
  1803. if (INTEL_INFO(dev)->gen >= 7)
  1804. return level == 0 ? 63 : 255;
  1805. else
  1806. return level == 0 ? 31 : 63;
  1807. }
  1808. static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
  1809. {
  1810. if (INTEL_INFO(dev)->gen >= 8)
  1811. return 31;
  1812. else
  1813. return 15;
  1814. }
  1815. /* Calculate the maximum primary/sprite plane watermark */
  1816. static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
  1817. int level,
  1818. const struct intel_wm_config *config,
  1819. enum intel_ddb_partitioning ddb_partitioning,
  1820. bool is_sprite)
  1821. {
  1822. unsigned int fifo_size = ilk_display_fifo_size(dev);
  1823. /* if sprites aren't enabled, sprites get nothing */
  1824. if (is_sprite && !config->sprites_enabled)
  1825. return 0;
  1826. /* HSW allows LP1+ watermarks even with multiple pipes */
  1827. if (level == 0 || config->num_pipes_active > 1) {
  1828. fifo_size /= INTEL_INFO(dev)->num_pipes;
  1829. /*
  1830. * For some reason the non self refresh
  1831. * FIFO size is only half of the self
  1832. * refresh FIFO size on ILK/SNB.
  1833. */
  1834. if (INTEL_INFO(dev)->gen <= 6)
  1835. fifo_size /= 2;
  1836. }
  1837. if (config->sprites_enabled) {
  1838. /* level 0 is always calculated with 1:1 split */
  1839. if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
  1840. if (is_sprite)
  1841. fifo_size *= 5;
  1842. fifo_size /= 6;
  1843. } else {
  1844. fifo_size /= 2;
  1845. }
  1846. }
  1847. /* clamp to max that the registers can hold */
  1848. return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
  1849. }
  1850. /* Calculate the maximum cursor plane watermark */
  1851. static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
  1852. int level,
  1853. const struct intel_wm_config *config)
  1854. {
  1855. /* HSW LP1+ watermarks w/ multiple pipes */
  1856. if (level > 0 && config->num_pipes_active > 1)
  1857. return 64;
  1858. /* otherwise just report max that registers can hold */
  1859. return ilk_cursor_wm_reg_max(dev, level);
  1860. }
  1861. static void ilk_compute_wm_maximums(const struct drm_device *dev,
  1862. int level,
  1863. const struct intel_wm_config *config,
  1864. enum intel_ddb_partitioning ddb_partitioning,
  1865. struct ilk_wm_maximums *max)
  1866. {
  1867. max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
  1868. max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
  1869. max->cur = ilk_cursor_wm_max(dev, level, config);
  1870. max->fbc = ilk_fbc_wm_reg_max(dev);
  1871. }
  1872. static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
  1873. int level,
  1874. struct ilk_wm_maximums *max)
  1875. {
  1876. max->pri = ilk_plane_wm_reg_max(dev, level, false);
  1877. max->spr = ilk_plane_wm_reg_max(dev, level, true);
  1878. max->cur = ilk_cursor_wm_reg_max(dev, level);
  1879. max->fbc = ilk_fbc_wm_reg_max(dev);
  1880. }
  1881. static bool ilk_validate_wm_level(int level,
  1882. const struct ilk_wm_maximums *max,
  1883. struct intel_wm_level *result)
  1884. {
  1885. bool ret;
  1886. /* already determined to be invalid? */
  1887. if (!result->enable)
  1888. return false;
  1889. result->enable = result->pri_val <= max->pri &&
  1890. result->spr_val <= max->spr &&
  1891. result->cur_val <= max->cur;
  1892. ret = result->enable;
  1893. /*
  1894. * HACK until we can pre-compute everything,
  1895. * and thus fail gracefully if LP0 watermarks
  1896. * are exceeded...
  1897. */
  1898. if (level == 0 && !result->enable) {
  1899. if (result->pri_val > max->pri)
  1900. DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
  1901. level, result->pri_val, max->pri);
  1902. if (result->spr_val > max->spr)
  1903. DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
  1904. level, result->spr_val, max->spr);
  1905. if (result->cur_val > max->cur)
  1906. DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
  1907. level, result->cur_val, max->cur);
  1908. result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
  1909. result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
  1910. result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
  1911. result->enable = true;
  1912. }
  1913. return ret;
  1914. }
  1915. static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
  1916. int level,
  1917. const struct ilk_pipe_wm_parameters *p,
  1918. struct intel_wm_level *result)
  1919. {
  1920. uint16_t pri_latency = dev_priv->wm.pri_latency[level];
  1921. uint16_t spr_latency = dev_priv->wm.spr_latency[level];
  1922. uint16_t cur_latency = dev_priv->wm.cur_latency[level];
  1923. /* WM1+ latency values stored in 0.5us units */
  1924. if (level > 0) {
  1925. pri_latency *= 5;
  1926. spr_latency *= 5;
  1927. cur_latency *= 5;
  1928. }
  1929. result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
  1930. result->spr_val = ilk_compute_spr_wm(p, spr_latency);
  1931. result->cur_val = ilk_compute_cur_wm(p, cur_latency);
  1932. result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
  1933. result->enable = true;
  1934. }
  1935. static uint32_t
  1936. hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
  1937. {
  1938. struct drm_i915_private *dev_priv = dev->dev_private;
  1939. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1940. struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
  1941. u32 linetime, ips_linetime;
  1942. if (!intel_crtc_active(crtc))
  1943. return 0;
  1944. /* The WM are computed with base on how long it takes to fill a single
  1945. * row at the given clock rate, multiplied by 8.
  1946. * */
  1947. linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
  1948. mode->crtc_clock);
  1949. ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
  1950. intel_ddi_get_cdclk_freq(dev_priv));
  1951. return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
  1952. PIPE_WM_LINETIME_TIME(linetime);
  1953. }
  1954. static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
  1955. {
  1956. struct drm_i915_private *dev_priv = dev->dev_private;
  1957. if (IS_GEN9(dev)) {
  1958. uint32_t val;
  1959. int ret, i;
  1960. int level, max_level = ilk_wm_max_level(dev);
  1961. /* read the first set of memory latencies[0:3] */
  1962. val = 0; /* data0 to be programmed to 0 for first set */
  1963. mutex_lock(&dev_priv->rps.hw_lock);
  1964. ret = sandybridge_pcode_read(dev_priv,
  1965. GEN9_PCODE_READ_MEM_LATENCY,
  1966. &val);
  1967. mutex_unlock(&dev_priv->rps.hw_lock);
  1968. if (ret) {
  1969. DRM_ERROR("SKL Mailbox read error = %d\n", ret);
  1970. return;
  1971. }
  1972. wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
  1973. wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
  1974. GEN9_MEM_LATENCY_LEVEL_MASK;
  1975. wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
  1976. GEN9_MEM_LATENCY_LEVEL_MASK;
  1977. wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
  1978. GEN9_MEM_LATENCY_LEVEL_MASK;
  1979. /* read the second set of memory latencies[4:7] */
  1980. val = 1; /* data0 to be programmed to 1 for second set */
  1981. mutex_lock(&dev_priv->rps.hw_lock);
  1982. ret = sandybridge_pcode_read(dev_priv,
  1983. GEN9_PCODE_READ_MEM_LATENCY,
  1984. &val);
  1985. mutex_unlock(&dev_priv->rps.hw_lock);
  1986. if (ret) {
  1987. DRM_ERROR("SKL Mailbox read error = %d\n", ret);
  1988. return;
  1989. }
  1990. wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
  1991. wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
  1992. GEN9_MEM_LATENCY_LEVEL_MASK;
  1993. wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
  1994. GEN9_MEM_LATENCY_LEVEL_MASK;
  1995. wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
  1996. GEN9_MEM_LATENCY_LEVEL_MASK;
  1997. /*
  1998. * punit doesn't take into account the read latency so we need
  1999. * to add 2us to the various latency levels we retrieve from
  2000. * the punit.
  2001. * - W0 is a bit special in that it's the only level that
  2002. * can't be disabled if we want to have display working, so
  2003. * we always add 2us there.
  2004. * - For levels >=1, punit returns 0us latency when they are
  2005. * disabled, so we respect that and don't add 2us then
  2006. *
  2007. * Additionally, if a level n (n > 1) has a 0us latency, all
  2008. * levels m (m >= n) need to be disabled. We make sure to
  2009. * sanitize the values out of the punit to satisfy this
  2010. * requirement.
  2011. */
  2012. wm[0] += 2;
  2013. for (level = 1; level <= max_level; level++)
  2014. if (wm[level] != 0)
  2015. wm[level] += 2;
  2016. else {
  2017. for (i = level + 1; i <= max_level; i++)
  2018. wm[i] = 0;
  2019. break;
  2020. }
  2021. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2022. uint64_t sskpd = I915_READ64(MCH_SSKPD);
  2023. wm[0] = (sskpd >> 56) & 0xFF;
  2024. if (wm[0] == 0)
  2025. wm[0] = sskpd & 0xF;
  2026. wm[1] = (sskpd >> 4) & 0xFF;
  2027. wm[2] = (sskpd >> 12) & 0xFF;
  2028. wm[3] = (sskpd >> 20) & 0x1FF;
  2029. wm[4] = (sskpd >> 32) & 0x1FF;
  2030. } else if (INTEL_INFO(dev)->gen >= 6) {
  2031. uint32_t sskpd = I915_READ(MCH_SSKPD);
  2032. wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
  2033. wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
  2034. wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
  2035. wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
  2036. } else if (INTEL_INFO(dev)->gen >= 5) {
  2037. uint32_t mltr = I915_READ(MLTR_ILK);
  2038. /* ILK primary LP0 latency is 700 ns */
  2039. wm[0] = 7;
  2040. wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
  2041. wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
  2042. }
  2043. }
  2044. static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
  2045. {
  2046. /* ILK sprite LP0 latency is 1300 ns */
  2047. if (INTEL_INFO(dev)->gen == 5)
  2048. wm[0] = 13;
  2049. }
  2050. static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
  2051. {
  2052. /* ILK cursor LP0 latency is 1300 ns */
  2053. if (INTEL_INFO(dev)->gen == 5)
  2054. wm[0] = 13;
  2055. /* WaDoubleCursorLP3Latency:ivb */
  2056. if (IS_IVYBRIDGE(dev))
  2057. wm[3] *= 2;
  2058. }
  2059. int ilk_wm_max_level(const struct drm_device *dev)
  2060. {
  2061. /* how many WM levels are we expecting */
  2062. if (IS_GEN9(dev))
  2063. return 7;
  2064. else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2065. return 4;
  2066. else if (INTEL_INFO(dev)->gen >= 6)
  2067. return 3;
  2068. else
  2069. return 2;
  2070. }
  2071. static void intel_print_wm_latency(struct drm_device *dev,
  2072. const char *name,
  2073. const uint16_t wm[8])
  2074. {
  2075. int level, max_level = ilk_wm_max_level(dev);
  2076. for (level = 0; level <= max_level; level++) {
  2077. unsigned int latency = wm[level];
  2078. if (latency == 0) {
  2079. DRM_ERROR("%s WM%d latency not provided\n",
  2080. name, level);
  2081. continue;
  2082. }
  2083. /*
  2084. * - latencies are in us on gen9.
  2085. * - before then, WM1+ latency values are in 0.5us units
  2086. */
  2087. if (IS_GEN9(dev))
  2088. latency *= 10;
  2089. else if (level > 0)
  2090. latency *= 5;
  2091. DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
  2092. name, level, wm[level],
  2093. latency / 10, latency % 10);
  2094. }
  2095. }
  2096. static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
  2097. uint16_t wm[5], uint16_t min)
  2098. {
  2099. int level, max_level = ilk_wm_max_level(dev_priv->dev);
  2100. if (wm[0] >= min)
  2101. return false;
  2102. wm[0] = max(wm[0], min);
  2103. for (level = 1; level <= max_level; level++)
  2104. wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
  2105. return true;
  2106. }
  2107. static void snb_wm_latency_quirk(struct drm_device *dev)
  2108. {
  2109. struct drm_i915_private *dev_priv = dev->dev_private;
  2110. bool changed;
  2111. /*
  2112. * The BIOS provided WM memory latency values are often
  2113. * inadequate for high resolution displays. Adjust them.
  2114. */
  2115. changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
  2116. ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
  2117. ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
  2118. if (!changed)
  2119. return;
  2120. DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
  2121. intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
  2122. intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
  2123. intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
  2124. }
  2125. static void ilk_setup_wm_latency(struct drm_device *dev)
  2126. {
  2127. struct drm_i915_private *dev_priv = dev->dev_private;
  2128. intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
  2129. memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
  2130. sizeof(dev_priv->wm.pri_latency));
  2131. memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
  2132. sizeof(dev_priv->wm.pri_latency));
  2133. intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
  2134. intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
  2135. intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
  2136. intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
  2137. intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
  2138. if (IS_GEN6(dev))
  2139. snb_wm_latency_quirk(dev);
  2140. }
  2141. static void skl_setup_wm_latency(struct drm_device *dev)
  2142. {
  2143. struct drm_i915_private *dev_priv = dev->dev_private;
  2144. intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
  2145. intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
  2146. }
  2147. static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
  2148. struct ilk_pipe_wm_parameters *p)
  2149. {
  2150. struct drm_device *dev = crtc->dev;
  2151. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2152. enum pipe pipe = intel_crtc->pipe;
  2153. struct drm_plane *plane;
  2154. if (!intel_crtc_active(crtc))
  2155. return;
  2156. p->active = true;
  2157. p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
  2158. p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
  2159. p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
  2160. p->cur.bytes_per_pixel = 4;
  2161. p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
  2162. p->cur.horiz_pixels = intel_crtc->cursor_width;
  2163. /* TODO: for now, assume primary and cursor planes are always enabled. */
  2164. p->pri.enabled = true;
  2165. p->cur.enabled = true;
  2166. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  2167. struct intel_plane *intel_plane = to_intel_plane(plane);
  2168. if (intel_plane->pipe == pipe) {
  2169. p->spr = intel_plane->wm;
  2170. break;
  2171. }
  2172. }
  2173. }
  2174. static void ilk_compute_wm_config(struct drm_device *dev,
  2175. struct intel_wm_config *config)
  2176. {
  2177. struct intel_crtc *intel_crtc;
  2178. /* Compute the currently _active_ config */
  2179. for_each_intel_crtc(dev, intel_crtc) {
  2180. const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
  2181. if (!wm->pipe_enabled)
  2182. continue;
  2183. config->sprites_enabled |= wm->sprites_enabled;
  2184. config->sprites_scaled |= wm->sprites_scaled;
  2185. config->num_pipes_active++;
  2186. }
  2187. }
  2188. /* Compute new watermarks for the pipe */
  2189. static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
  2190. const struct ilk_pipe_wm_parameters *params,
  2191. struct intel_pipe_wm *pipe_wm)
  2192. {
  2193. struct drm_device *dev = crtc->dev;
  2194. const struct drm_i915_private *dev_priv = dev->dev_private;
  2195. int level, max_level = ilk_wm_max_level(dev);
  2196. /* LP0 watermark maximums depend on this pipe alone */
  2197. struct intel_wm_config config = {
  2198. .num_pipes_active = 1,
  2199. .sprites_enabled = params->spr.enabled,
  2200. .sprites_scaled = params->spr.scaled,
  2201. };
  2202. struct ilk_wm_maximums max;
  2203. pipe_wm->pipe_enabled = params->active;
  2204. pipe_wm->sprites_enabled = params->spr.enabled;
  2205. pipe_wm->sprites_scaled = params->spr.scaled;
  2206. /* ILK/SNB: LP2+ watermarks only w/o sprites */
  2207. if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
  2208. max_level = 1;
  2209. /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
  2210. if (params->spr.scaled)
  2211. max_level = 0;
  2212. ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
  2213. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2214. pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
  2215. /* LP0 watermarks always use 1/2 DDB partitioning */
  2216. ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
  2217. /* At least LP0 must be valid */
  2218. if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
  2219. return false;
  2220. ilk_compute_wm_reg_maximums(dev, 1, &max);
  2221. for (level = 1; level <= max_level; level++) {
  2222. struct intel_wm_level wm = {};
  2223. ilk_compute_wm_level(dev_priv, level, params, &wm);
  2224. /*
  2225. * Disable any watermark level that exceeds the
  2226. * register maximums since such watermarks are
  2227. * always invalid.
  2228. */
  2229. if (!ilk_validate_wm_level(level, &max, &wm))
  2230. break;
  2231. pipe_wm->wm[level] = wm;
  2232. }
  2233. return true;
  2234. }
  2235. /*
  2236. * Merge the watermarks from all active pipes for a specific level.
  2237. */
  2238. static void ilk_merge_wm_level(struct drm_device *dev,
  2239. int level,
  2240. struct intel_wm_level *ret_wm)
  2241. {
  2242. const struct intel_crtc *intel_crtc;
  2243. ret_wm->enable = true;
  2244. for_each_intel_crtc(dev, intel_crtc) {
  2245. const struct intel_pipe_wm *active = &intel_crtc->wm.active;
  2246. const struct intel_wm_level *wm = &active->wm[level];
  2247. if (!active->pipe_enabled)
  2248. continue;
  2249. /*
  2250. * The watermark values may have been used in the past,
  2251. * so we must maintain them in the registers for some
  2252. * time even if the level is now disabled.
  2253. */
  2254. if (!wm->enable)
  2255. ret_wm->enable = false;
  2256. ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
  2257. ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
  2258. ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
  2259. ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
  2260. }
  2261. }
  2262. /*
  2263. * Merge all low power watermarks for all active pipes.
  2264. */
  2265. static void ilk_wm_merge(struct drm_device *dev,
  2266. const struct intel_wm_config *config,
  2267. const struct ilk_wm_maximums *max,
  2268. struct intel_pipe_wm *merged)
  2269. {
  2270. int level, max_level = ilk_wm_max_level(dev);
  2271. int last_enabled_level = max_level;
  2272. /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
  2273. if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
  2274. config->num_pipes_active > 1)
  2275. return;
  2276. /* ILK: FBC WM must be disabled always */
  2277. merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
  2278. /* merge each WM1+ level */
  2279. for (level = 1; level <= max_level; level++) {
  2280. struct intel_wm_level *wm = &merged->wm[level];
  2281. ilk_merge_wm_level(dev, level, wm);
  2282. if (level > last_enabled_level)
  2283. wm->enable = false;
  2284. else if (!ilk_validate_wm_level(level, max, wm))
  2285. /* make sure all following levels get disabled */
  2286. last_enabled_level = level - 1;
  2287. /*
  2288. * The spec says it is preferred to disable
  2289. * FBC WMs instead of disabling a WM level.
  2290. */
  2291. if (wm->fbc_val > max->fbc) {
  2292. if (wm->enable)
  2293. merged->fbc_wm_enabled = false;
  2294. wm->fbc_val = 0;
  2295. }
  2296. }
  2297. /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
  2298. /*
  2299. * FIXME this is racy. FBC might get enabled later.
  2300. * What we should check here is whether FBC can be
  2301. * enabled sometime later.
  2302. */
  2303. if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
  2304. for (level = 2; level <= max_level; level++) {
  2305. struct intel_wm_level *wm = &merged->wm[level];
  2306. wm->enable = false;
  2307. }
  2308. }
  2309. }
  2310. static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
  2311. {
  2312. /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
  2313. return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
  2314. }
  2315. /* The value we need to program into the WM_LPx latency field */
  2316. static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
  2317. {
  2318. struct drm_i915_private *dev_priv = dev->dev_private;
  2319. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2320. return 2 * level;
  2321. else
  2322. return dev_priv->wm.pri_latency[level];
  2323. }
  2324. static void ilk_compute_wm_results(struct drm_device *dev,
  2325. const struct intel_pipe_wm *merged,
  2326. enum intel_ddb_partitioning partitioning,
  2327. struct ilk_wm_values *results)
  2328. {
  2329. struct intel_crtc *intel_crtc;
  2330. int level, wm_lp;
  2331. results->enable_fbc_wm = merged->fbc_wm_enabled;
  2332. results->partitioning = partitioning;
  2333. /* LP1+ register values */
  2334. for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
  2335. const struct intel_wm_level *r;
  2336. level = ilk_wm_lp_to_level(wm_lp, merged);
  2337. r = &merged->wm[level];
  2338. /*
  2339. * Maintain the watermark values even if the level is
  2340. * disabled. Doing otherwise could cause underruns.
  2341. */
  2342. results->wm_lp[wm_lp - 1] =
  2343. (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
  2344. (r->pri_val << WM1_LP_SR_SHIFT) |
  2345. r->cur_val;
  2346. if (r->enable)
  2347. results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
  2348. if (INTEL_INFO(dev)->gen >= 8)
  2349. results->wm_lp[wm_lp - 1] |=
  2350. r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
  2351. else
  2352. results->wm_lp[wm_lp - 1] |=
  2353. r->fbc_val << WM1_LP_FBC_SHIFT;
  2354. /*
  2355. * Always set WM1S_LP_EN when spr_val != 0, even if the
  2356. * level is disabled. Doing otherwise could cause underruns.
  2357. */
  2358. if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
  2359. WARN_ON(wm_lp != 1);
  2360. results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
  2361. } else
  2362. results->wm_lp_spr[wm_lp - 1] = r->spr_val;
  2363. }
  2364. /* LP0 register values */
  2365. for_each_intel_crtc(dev, intel_crtc) {
  2366. enum pipe pipe = intel_crtc->pipe;
  2367. const struct intel_wm_level *r =
  2368. &intel_crtc->wm.active.wm[0];
  2369. if (WARN_ON(!r->enable))
  2370. continue;
  2371. results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
  2372. results->wm_pipe[pipe] =
  2373. (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
  2374. (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
  2375. r->cur_val;
  2376. }
  2377. }
  2378. /* Find the result with the highest level enabled. Check for enable_fbc_wm in
  2379. * case both are at the same level. Prefer r1 in case they're the same. */
  2380. static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
  2381. struct intel_pipe_wm *r1,
  2382. struct intel_pipe_wm *r2)
  2383. {
  2384. int level, max_level = ilk_wm_max_level(dev);
  2385. int level1 = 0, level2 = 0;
  2386. for (level = 1; level <= max_level; level++) {
  2387. if (r1->wm[level].enable)
  2388. level1 = level;
  2389. if (r2->wm[level].enable)
  2390. level2 = level;
  2391. }
  2392. if (level1 == level2) {
  2393. if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
  2394. return r2;
  2395. else
  2396. return r1;
  2397. } else if (level1 > level2) {
  2398. return r1;
  2399. } else {
  2400. return r2;
  2401. }
  2402. }
  2403. /* dirty bits used to track which watermarks need changes */
  2404. #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
  2405. #define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
  2406. #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
  2407. #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
  2408. #define WM_DIRTY_FBC (1 << 24)
  2409. #define WM_DIRTY_DDB (1 << 25)
  2410. static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
  2411. const struct ilk_wm_values *old,
  2412. const struct ilk_wm_values *new)
  2413. {
  2414. unsigned int dirty = 0;
  2415. enum pipe pipe;
  2416. int wm_lp;
  2417. for_each_pipe(dev_priv, pipe) {
  2418. if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
  2419. dirty |= WM_DIRTY_LINETIME(pipe);
  2420. /* Must disable LP1+ watermarks too */
  2421. dirty |= WM_DIRTY_LP_ALL;
  2422. }
  2423. if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
  2424. dirty |= WM_DIRTY_PIPE(pipe);
  2425. /* Must disable LP1+ watermarks too */
  2426. dirty |= WM_DIRTY_LP_ALL;
  2427. }
  2428. }
  2429. if (old->enable_fbc_wm != new->enable_fbc_wm) {
  2430. dirty |= WM_DIRTY_FBC;
  2431. /* Must disable LP1+ watermarks too */
  2432. dirty |= WM_DIRTY_LP_ALL;
  2433. }
  2434. if (old->partitioning != new->partitioning) {
  2435. dirty |= WM_DIRTY_DDB;
  2436. /* Must disable LP1+ watermarks too */
  2437. dirty |= WM_DIRTY_LP_ALL;
  2438. }
  2439. /* LP1+ watermarks already deemed dirty, no need to continue */
  2440. if (dirty & WM_DIRTY_LP_ALL)
  2441. return dirty;
  2442. /* Find the lowest numbered LP1+ watermark in need of an update... */
  2443. for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
  2444. if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
  2445. old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
  2446. break;
  2447. }
  2448. /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
  2449. for (; wm_lp <= 3; wm_lp++)
  2450. dirty |= WM_DIRTY_LP(wm_lp);
  2451. return dirty;
  2452. }
  2453. static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
  2454. unsigned int dirty)
  2455. {
  2456. struct ilk_wm_values *previous = &dev_priv->wm.hw;
  2457. bool changed = false;
  2458. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
  2459. previous->wm_lp[2] &= ~WM1_LP_SR_EN;
  2460. I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
  2461. changed = true;
  2462. }
  2463. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
  2464. previous->wm_lp[1] &= ~WM1_LP_SR_EN;
  2465. I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
  2466. changed = true;
  2467. }
  2468. if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
  2469. previous->wm_lp[0] &= ~WM1_LP_SR_EN;
  2470. I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
  2471. changed = true;
  2472. }
  2473. /*
  2474. * Don't touch WM1S_LP_EN here.
  2475. * Doing so could cause underruns.
  2476. */
  2477. return changed;
  2478. }
  2479. /*
  2480. * The spec says we shouldn't write when we don't need, because every write
  2481. * causes WMs to be re-evaluated, expending some power.
  2482. */
  2483. static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
  2484. struct ilk_wm_values *results)
  2485. {
  2486. struct drm_device *dev = dev_priv->dev;
  2487. struct ilk_wm_values *previous = &dev_priv->wm.hw;
  2488. unsigned int dirty;
  2489. uint32_t val;
  2490. dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
  2491. if (!dirty)
  2492. return;
  2493. _ilk_disable_lp_wm(dev_priv, dirty);
  2494. if (dirty & WM_DIRTY_PIPE(PIPE_A))
  2495. I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
  2496. if (dirty & WM_DIRTY_PIPE(PIPE_B))
  2497. I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
  2498. if (dirty & WM_DIRTY_PIPE(PIPE_C))
  2499. I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
  2500. if (dirty & WM_DIRTY_LINETIME(PIPE_A))
  2501. I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
  2502. if (dirty & WM_DIRTY_LINETIME(PIPE_B))
  2503. I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
  2504. if (dirty & WM_DIRTY_LINETIME(PIPE_C))
  2505. I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
  2506. if (dirty & WM_DIRTY_DDB) {
  2507. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2508. val = I915_READ(WM_MISC);
  2509. if (results->partitioning == INTEL_DDB_PART_1_2)
  2510. val &= ~WM_MISC_DATA_PARTITION_5_6;
  2511. else
  2512. val |= WM_MISC_DATA_PARTITION_5_6;
  2513. I915_WRITE(WM_MISC, val);
  2514. } else {
  2515. val = I915_READ(DISP_ARB_CTL2);
  2516. if (results->partitioning == INTEL_DDB_PART_1_2)
  2517. val &= ~DISP_DATA_PARTITION_5_6;
  2518. else
  2519. val |= DISP_DATA_PARTITION_5_6;
  2520. I915_WRITE(DISP_ARB_CTL2, val);
  2521. }
  2522. }
  2523. if (dirty & WM_DIRTY_FBC) {
  2524. val = I915_READ(DISP_ARB_CTL);
  2525. if (results->enable_fbc_wm)
  2526. val &= ~DISP_FBC_WM_DIS;
  2527. else
  2528. val |= DISP_FBC_WM_DIS;
  2529. I915_WRITE(DISP_ARB_CTL, val);
  2530. }
  2531. if (dirty & WM_DIRTY_LP(1) &&
  2532. previous->wm_lp_spr[0] != results->wm_lp_spr[0])
  2533. I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
  2534. if (INTEL_INFO(dev)->gen >= 7) {
  2535. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
  2536. I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
  2537. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
  2538. I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
  2539. }
  2540. if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
  2541. I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
  2542. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
  2543. I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
  2544. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
  2545. I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
  2546. dev_priv->wm.hw = *results;
  2547. }
  2548. static bool ilk_disable_lp_wm(struct drm_device *dev)
  2549. {
  2550. struct drm_i915_private *dev_priv = dev->dev_private;
  2551. return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
  2552. }
  2553. /*
  2554. * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
  2555. * different active planes.
  2556. */
  2557. #define SKL_DDB_SIZE 896 /* in blocks */
  2558. static void
  2559. skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
  2560. struct drm_crtc *for_crtc,
  2561. const struct intel_wm_config *config,
  2562. const struct skl_pipe_wm_parameters *params,
  2563. struct skl_ddb_entry *alloc /* out */)
  2564. {
  2565. struct drm_crtc *crtc;
  2566. unsigned int pipe_size, ddb_size;
  2567. int nth_active_pipe;
  2568. if (!params->active) {
  2569. alloc->start = 0;
  2570. alloc->end = 0;
  2571. return;
  2572. }
  2573. ddb_size = SKL_DDB_SIZE;
  2574. ddb_size -= 4; /* 4 blocks for bypass path allocation */
  2575. nth_active_pipe = 0;
  2576. for_each_crtc(dev, crtc) {
  2577. if (!intel_crtc_active(crtc))
  2578. continue;
  2579. if (crtc == for_crtc)
  2580. break;
  2581. nth_active_pipe++;
  2582. }
  2583. pipe_size = ddb_size / config->num_pipes_active;
  2584. alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
  2585. alloc->end = alloc->start + pipe_size - 1;
  2586. }
  2587. static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
  2588. {
  2589. if (config->num_pipes_active == 1)
  2590. return 32;
  2591. return 8;
  2592. }
  2593. static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
  2594. {
  2595. entry->start = reg & 0x3ff;
  2596. entry->end = (reg >> 16) & 0x3ff;
  2597. }
  2598. static void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
  2599. struct skl_ddb_allocation *ddb /* out */)
  2600. {
  2601. struct drm_device *dev = dev_priv->dev;
  2602. enum pipe pipe;
  2603. int plane;
  2604. u32 val;
  2605. for_each_pipe(dev_priv, pipe) {
  2606. for_each_plane(pipe, plane) {
  2607. val = I915_READ(PLANE_BUF_CFG(pipe, plane));
  2608. skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
  2609. val);
  2610. }
  2611. val = I915_READ(CUR_BUF_CFG(pipe));
  2612. skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
  2613. }
  2614. }
  2615. static unsigned int
  2616. skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
  2617. {
  2618. return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
  2619. }
  2620. /*
  2621. * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
  2622. * a 8192x4096@32bpp framebuffer:
  2623. * 3 * 4096 * 8192 * 4 < 2^32
  2624. */
  2625. static unsigned int
  2626. skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
  2627. const struct skl_pipe_wm_parameters *params)
  2628. {
  2629. unsigned int total_data_rate = 0;
  2630. int plane;
  2631. for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
  2632. const struct intel_plane_wm_parameters *p;
  2633. p = &params->plane[plane];
  2634. if (!p->enabled)
  2635. continue;
  2636. total_data_rate += skl_plane_relative_data_rate(p);
  2637. }
  2638. return total_data_rate;
  2639. }
  2640. static void
  2641. skl_allocate_pipe_ddb(struct drm_crtc *crtc,
  2642. const struct intel_wm_config *config,
  2643. const struct skl_pipe_wm_parameters *params,
  2644. struct skl_ddb_allocation *ddb /* out */)
  2645. {
  2646. struct drm_device *dev = crtc->dev;
  2647. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2648. enum pipe pipe = intel_crtc->pipe;
  2649. struct skl_ddb_entry alloc;
  2650. uint16_t alloc_size, start, cursor_blocks;
  2651. unsigned int total_data_rate;
  2652. int plane;
  2653. skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, &alloc);
  2654. alloc_size = skl_ddb_entry_size(&alloc);
  2655. if (alloc_size == 0) {
  2656. memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
  2657. memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
  2658. return;
  2659. }
  2660. cursor_blocks = skl_cursor_allocation(config);
  2661. ddb->cursor[pipe].start = alloc.end - cursor_blocks + 1;
  2662. ddb->cursor[pipe].end = alloc.end;
  2663. alloc_size -= cursor_blocks;
  2664. alloc.end -= cursor_blocks;
  2665. /*
  2666. * Each active plane get a portion of the remaining space, in
  2667. * proportion to the amount of data they need to fetch from memory.
  2668. *
  2669. * FIXME: we may not allocate every single block here.
  2670. */
  2671. total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
  2672. start = alloc.start;
  2673. for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
  2674. const struct intel_plane_wm_parameters *p;
  2675. unsigned int data_rate;
  2676. uint16_t plane_blocks;
  2677. p = &params->plane[plane];
  2678. if (!p->enabled)
  2679. continue;
  2680. data_rate = skl_plane_relative_data_rate(p);
  2681. /*
  2682. * promote the expression to 64 bits to avoid overflowing, the
  2683. * result is < available as data_rate / total_data_rate < 1
  2684. */
  2685. plane_blocks = div_u64((uint64_t)alloc_size * data_rate,
  2686. total_data_rate);
  2687. ddb->plane[pipe][plane].start = start;
  2688. ddb->plane[pipe][plane].end = start + plane_blocks - 1;
  2689. start += plane_blocks;
  2690. }
  2691. }
  2692. static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_config *config)
  2693. {
  2694. /* TODO: Take into account the scalers once we support them */
  2695. return config->adjusted_mode.crtc_clock;
  2696. }
  2697. /*
  2698. * The max latency should be 257 (max the punit can code is 255 and we add 2us
  2699. * for the read latency) and bytes_per_pixel should always be <= 8, so that
  2700. * should allow pixel_rate up to ~2 GHz which seems sufficient since max
  2701. * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
  2702. */
  2703. static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
  2704. uint32_t latency)
  2705. {
  2706. uint32_t wm_intermediate_val, ret;
  2707. if (latency == 0)
  2708. return UINT_MAX;
  2709. wm_intermediate_val = latency * pixel_rate * bytes_per_pixel;
  2710. ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
  2711. return ret;
  2712. }
  2713. static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
  2714. uint32_t horiz_pixels, uint8_t bytes_per_pixel,
  2715. uint32_t latency)
  2716. {
  2717. uint32_t ret, plane_bytes_per_line, wm_intermediate_val;
  2718. if (latency == 0)
  2719. return UINT_MAX;
  2720. plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
  2721. wm_intermediate_val = latency * pixel_rate;
  2722. ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
  2723. plane_bytes_per_line;
  2724. return ret;
  2725. }
  2726. static void skl_compute_transition_wm(struct drm_crtc *crtc,
  2727. struct skl_pipe_wm_parameters *params,
  2728. struct skl_pipe_wm *pipe_wm)
  2729. {
  2730. /*
  2731. * For now it is suggested to use the LP0 wm val of corresponding
  2732. * plane as transition wm val. This is done while computing results.
  2733. */
  2734. if (!params->active)
  2735. return;
  2736. }
  2737. static uint32_t
  2738. skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
  2739. {
  2740. if (!intel_crtc_active(crtc))
  2741. return 0;
  2742. return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
  2743. }
  2744. static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
  2745. const struct intel_crtc *intel_crtc)
  2746. {
  2747. struct drm_device *dev = intel_crtc->base.dev;
  2748. struct drm_i915_private *dev_priv = dev->dev_private;
  2749. const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
  2750. enum pipe pipe = intel_crtc->pipe;
  2751. if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
  2752. sizeof(new_ddb->plane[pipe])))
  2753. return true;
  2754. if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
  2755. sizeof(new_ddb->cursor[pipe])))
  2756. return true;
  2757. return false;
  2758. }
  2759. static void skl_compute_wm_global_parameters(struct drm_device *dev,
  2760. struct intel_wm_config *config)
  2761. {
  2762. struct drm_crtc *crtc;
  2763. struct drm_plane *plane;
  2764. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  2765. config->num_pipes_active += intel_crtc_active(crtc);
  2766. /* FIXME: I don't think we need those two global parameters on SKL */
  2767. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  2768. struct intel_plane *intel_plane = to_intel_plane(plane);
  2769. config->sprites_enabled |= intel_plane->wm.enabled;
  2770. config->sprites_scaled |= intel_plane->wm.scaled;
  2771. }
  2772. }
  2773. static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
  2774. struct skl_pipe_wm_parameters *p)
  2775. {
  2776. struct drm_device *dev = crtc->dev;
  2777. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2778. enum pipe pipe = intel_crtc->pipe;
  2779. struct drm_plane *plane;
  2780. int i = 1; /* Index for sprite planes start */
  2781. p->active = intel_crtc_active(crtc);
  2782. if (p->active) {
  2783. p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
  2784. p->pixel_rate = skl_pipe_pixel_rate(&intel_crtc->config);
  2785. /*
  2786. * For now, assume primary and cursor planes are always enabled.
  2787. */
  2788. p->plane[0].enabled = true;
  2789. p->plane[0].bytes_per_pixel =
  2790. crtc->primary->fb->bits_per_pixel / 8;
  2791. p->plane[0].horiz_pixels = intel_crtc->config.pipe_src_w;
  2792. p->plane[0].vert_pixels = intel_crtc->config.pipe_src_h;
  2793. p->cursor.enabled = true;
  2794. p->cursor.bytes_per_pixel = 4;
  2795. p->cursor.horiz_pixels = intel_crtc->cursor_width ?
  2796. intel_crtc->cursor_width : 64;
  2797. }
  2798. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  2799. struct intel_plane *intel_plane = to_intel_plane(plane);
  2800. if (intel_plane->pipe == pipe)
  2801. p->plane[i++] = intel_plane->wm;
  2802. }
  2803. }
  2804. static bool skl_compute_plane_wm(struct skl_pipe_wm_parameters *p,
  2805. struct intel_plane_wm_parameters *p_params,
  2806. uint16_t max_page_buff_alloc,
  2807. uint32_t mem_value,
  2808. uint16_t *res_blocks, /* out */
  2809. uint8_t *res_lines /* out */)
  2810. {
  2811. uint32_t method1, method2, plane_bytes_per_line;
  2812. uint32_t result_bytes;
  2813. if (mem_value == 0 || !p->active || !p_params->enabled)
  2814. return false;
  2815. method1 = skl_wm_method1(p->pixel_rate,
  2816. p_params->bytes_per_pixel,
  2817. mem_value);
  2818. method2 = skl_wm_method2(p->pixel_rate,
  2819. p->pipe_htotal,
  2820. p_params->horiz_pixels,
  2821. p_params->bytes_per_pixel,
  2822. mem_value);
  2823. plane_bytes_per_line = p_params->horiz_pixels *
  2824. p_params->bytes_per_pixel;
  2825. /* For now xtile and linear */
  2826. if (((max_page_buff_alloc * 512) / plane_bytes_per_line) >= 1)
  2827. result_bytes = min(method1, method2);
  2828. else
  2829. result_bytes = method1;
  2830. *res_blocks = DIV_ROUND_UP(result_bytes, 512) + 1;
  2831. *res_lines = DIV_ROUND_UP(result_bytes, plane_bytes_per_line);
  2832. return true;
  2833. }
  2834. static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
  2835. struct skl_ddb_allocation *ddb,
  2836. struct skl_pipe_wm_parameters *p,
  2837. enum pipe pipe,
  2838. int level,
  2839. int num_planes,
  2840. struct skl_wm_level *result)
  2841. {
  2842. uint16_t latency = dev_priv->wm.skl_latency[level];
  2843. uint16_t ddb_blocks;
  2844. int i;
  2845. for (i = 0; i < num_planes; i++) {
  2846. ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
  2847. result->plane_en[i] = skl_compute_plane_wm(p, &p->plane[i],
  2848. ddb_blocks,
  2849. latency,
  2850. &result->plane_res_b[i],
  2851. &result->plane_res_l[i]);
  2852. }
  2853. ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
  2854. result->cursor_en = skl_compute_plane_wm(p, &p->cursor, ddb_blocks,
  2855. latency, &result->cursor_res_b,
  2856. &result->cursor_res_l);
  2857. }
  2858. static void skl_compute_pipe_wm(struct drm_crtc *crtc,
  2859. struct skl_ddb_allocation *ddb,
  2860. struct skl_pipe_wm_parameters *params,
  2861. struct skl_pipe_wm *pipe_wm)
  2862. {
  2863. struct drm_device *dev = crtc->dev;
  2864. const struct drm_i915_private *dev_priv = dev->dev_private;
  2865. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2866. int level, max_level = ilk_wm_max_level(dev);
  2867. for (level = 0; level <= max_level; level++) {
  2868. skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
  2869. level, intel_num_planes(intel_crtc),
  2870. &pipe_wm->wm[level]);
  2871. }
  2872. pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
  2873. skl_compute_transition_wm(crtc, params, pipe_wm);
  2874. }
  2875. static void skl_compute_wm_results(struct drm_device *dev,
  2876. struct skl_pipe_wm_parameters *p,
  2877. struct skl_pipe_wm *p_wm,
  2878. struct skl_wm_values *r,
  2879. struct intel_crtc *intel_crtc)
  2880. {
  2881. int level, max_level = ilk_wm_max_level(dev);
  2882. enum pipe pipe = intel_crtc->pipe;
  2883. for (level = 0; level <= max_level; level++) {
  2884. uint16_t ddb_blocks;
  2885. uint32_t temp;
  2886. int i;
  2887. for (i = 0; i < intel_num_planes(intel_crtc); i++) {
  2888. temp = 0;
  2889. ddb_blocks = skl_ddb_entry_size(&r->ddb.plane[pipe][i]);
  2890. if ((p_wm->wm[level].plane_res_b[i] > ddb_blocks) ||
  2891. (p_wm->wm[level].plane_res_l[i] > 31))
  2892. p_wm->wm[level].plane_en[i] = false;
  2893. temp |= p_wm->wm[level].plane_res_l[i] <<
  2894. PLANE_WM_LINES_SHIFT;
  2895. temp |= p_wm->wm[level].plane_res_b[i];
  2896. if (p_wm->wm[level].plane_en[i])
  2897. temp |= PLANE_WM_EN;
  2898. r->plane[pipe][i][level] = temp;
  2899. /* Use the LP0 WM value for transition WM for now. */
  2900. if (level == 0)
  2901. r->plane_trans[pipe][i] = temp;
  2902. }
  2903. temp = 0;
  2904. ddb_blocks = skl_ddb_entry_size(&r->ddb.cursor[pipe]);
  2905. if ((p_wm->wm[level].cursor_res_b > ddb_blocks) ||
  2906. (p_wm->wm[level].cursor_res_l > 31))
  2907. p_wm->wm[level].cursor_en = false;
  2908. temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
  2909. temp |= p_wm->wm[level].cursor_res_b;
  2910. if (p_wm->wm[level].cursor_en)
  2911. temp |= PLANE_WM_EN;
  2912. r->cursor[pipe][level] = temp;
  2913. /* Use the LP0 WM value for transition WM for now. */
  2914. if (level == 0)
  2915. r->cursor_trans[pipe] = temp;
  2916. }
  2917. r->wm_linetime[pipe] = p_wm->linetime;
  2918. }
  2919. static void skl_write_wm_values(struct drm_i915_private *dev_priv,
  2920. const struct skl_wm_values *new)
  2921. {
  2922. struct drm_device *dev = dev_priv->dev;
  2923. struct intel_crtc *crtc;
  2924. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  2925. int i, level, max_level = ilk_wm_max_level(dev);
  2926. enum pipe pipe = crtc->pipe;
  2927. if (new->dirty[pipe]) {
  2928. I915_WRITE(PIPE_WM_LINETIME(pipe),
  2929. new->wm_linetime[pipe]);
  2930. for (level = 0; level <= max_level; level++) {
  2931. for (i = 0; i < intel_num_planes(crtc); i++)
  2932. I915_WRITE(PLANE_WM(pipe, i, level),
  2933. new->plane[pipe][i][level]);
  2934. I915_WRITE(CUR_WM(pipe, level),
  2935. new->cursor[pipe][level]);
  2936. }
  2937. for (i = 0; i < intel_num_planes(crtc); i++)
  2938. I915_WRITE(PLANE_WM_TRANS(pipe, i),
  2939. new->plane_trans[pipe][i]);
  2940. I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
  2941. for (i = 0; i < intel_num_planes(crtc); i++)
  2942. I915_WRITE(PLANE_BUF_CFG(pipe, i),
  2943. new->ddb.plane[pipe][i].end << 16 |
  2944. new->ddb.plane[pipe][i].start);
  2945. I915_WRITE(CUR_BUF_CFG(pipe),
  2946. new->ddb.cursor[pipe].end << 16 |
  2947. new->ddb.cursor[pipe].start);
  2948. }
  2949. }
  2950. }
  2951. static bool skl_update_pipe_wm(struct drm_crtc *crtc,
  2952. struct skl_pipe_wm_parameters *params,
  2953. struct intel_wm_config *config,
  2954. struct skl_ddb_allocation *ddb, /* out */
  2955. struct skl_pipe_wm *pipe_wm /* out */)
  2956. {
  2957. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2958. skl_compute_wm_pipe_parameters(crtc, params);
  2959. skl_allocate_pipe_ddb(crtc, config, params, ddb);
  2960. skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
  2961. if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
  2962. return false;
  2963. intel_crtc->wm.skl_active = *pipe_wm;
  2964. return true;
  2965. }
  2966. static void skl_update_other_pipe_wm(struct drm_device *dev,
  2967. struct drm_crtc *crtc,
  2968. struct intel_wm_config *config,
  2969. struct skl_wm_values *r)
  2970. {
  2971. struct intel_crtc *intel_crtc;
  2972. struct intel_crtc *this_crtc = to_intel_crtc(crtc);
  2973. /*
  2974. * If the WM update hasn't changed the allocation for this_crtc (the
  2975. * crtc we are currently computing the new WM values for), other
  2976. * enabled crtcs will keep the same allocation and we don't need to
  2977. * recompute anything for them.
  2978. */
  2979. if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
  2980. return;
  2981. /*
  2982. * Otherwise, because of this_crtc being freshly enabled/disabled, the
  2983. * other active pipes need new DDB allocation and WM values.
  2984. */
  2985. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  2986. base.head) {
  2987. struct skl_pipe_wm_parameters params = {};
  2988. struct skl_pipe_wm pipe_wm = {};
  2989. bool wm_changed;
  2990. if (this_crtc->pipe == intel_crtc->pipe)
  2991. continue;
  2992. if (!intel_crtc->active)
  2993. continue;
  2994. wm_changed = skl_update_pipe_wm(&intel_crtc->base,
  2995. &params, config,
  2996. &r->ddb, &pipe_wm);
  2997. /*
  2998. * If we end up re-computing the other pipe WM values, it's
  2999. * because it was really needed, so we expect the WM values to
  3000. * be different.
  3001. */
  3002. WARN_ON(!wm_changed);
  3003. skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
  3004. r->dirty[intel_crtc->pipe] = true;
  3005. }
  3006. }
  3007. static void skl_update_wm(struct drm_crtc *crtc)
  3008. {
  3009. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3010. struct drm_device *dev = crtc->dev;
  3011. struct drm_i915_private *dev_priv = dev->dev_private;
  3012. struct skl_pipe_wm_parameters params = {};
  3013. struct skl_wm_values *results = &dev_priv->wm.skl_results;
  3014. struct skl_pipe_wm pipe_wm = {};
  3015. struct intel_wm_config config = {};
  3016. memset(results, 0, sizeof(*results));
  3017. skl_compute_wm_global_parameters(dev, &config);
  3018. if (!skl_update_pipe_wm(crtc, &params, &config,
  3019. &results->ddb, &pipe_wm))
  3020. return;
  3021. skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
  3022. results->dirty[intel_crtc->pipe] = true;
  3023. skl_update_other_pipe_wm(dev, crtc, &config, results);
  3024. skl_write_wm_values(dev_priv, results);
  3025. /* store the new configuration */
  3026. dev_priv->wm.skl_hw = *results;
  3027. }
  3028. static void
  3029. skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
  3030. uint32_t sprite_width, uint32_t sprite_height,
  3031. int pixel_size, bool enabled, bool scaled)
  3032. {
  3033. struct intel_plane *intel_plane = to_intel_plane(plane);
  3034. intel_plane->wm.enabled = enabled;
  3035. intel_plane->wm.scaled = scaled;
  3036. intel_plane->wm.horiz_pixels = sprite_width;
  3037. intel_plane->wm.vert_pixels = sprite_height;
  3038. intel_plane->wm.bytes_per_pixel = pixel_size;
  3039. skl_update_wm(crtc);
  3040. }
  3041. static void ilk_update_wm(struct drm_crtc *crtc)
  3042. {
  3043. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3044. struct drm_device *dev = crtc->dev;
  3045. struct drm_i915_private *dev_priv = dev->dev_private;
  3046. struct ilk_wm_maximums max;
  3047. struct ilk_pipe_wm_parameters params = {};
  3048. struct ilk_wm_values results = {};
  3049. enum intel_ddb_partitioning partitioning;
  3050. struct intel_pipe_wm pipe_wm = {};
  3051. struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
  3052. struct intel_wm_config config = {};
  3053. ilk_compute_wm_parameters(crtc, &params);
  3054. intel_compute_pipe_wm(crtc, &params, &pipe_wm);
  3055. if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
  3056. return;
  3057. intel_crtc->wm.active = pipe_wm;
  3058. ilk_compute_wm_config(dev, &config);
  3059. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
  3060. ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
  3061. /* 5/6 split only in single pipe config on IVB+ */
  3062. if (INTEL_INFO(dev)->gen >= 7 &&
  3063. config.num_pipes_active == 1 && config.sprites_enabled) {
  3064. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
  3065. ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
  3066. best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
  3067. } else {
  3068. best_lp_wm = &lp_wm_1_2;
  3069. }
  3070. partitioning = (best_lp_wm == &lp_wm_1_2) ?
  3071. INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
  3072. ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
  3073. ilk_write_wm_values(dev_priv, &results);
  3074. }
  3075. static void
  3076. ilk_update_sprite_wm(struct drm_plane *plane,
  3077. struct drm_crtc *crtc,
  3078. uint32_t sprite_width, uint32_t sprite_height,
  3079. int pixel_size, bool enabled, bool scaled)
  3080. {
  3081. struct drm_device *dev = plane->dev;
  3082. struct intel_plane *intel_plane = to_intel_plane(plane);
  3083. intel_plane->wm.enabled = enabled;
  3084. intel_plane->wm.scaled = scaled;
  3085. intel_plane->wm.horiz_pixels = sprite_width;
  3086. intel_plane->wm.vert_pixels = sprite_width;
  3087. intel_plane->wm.bytes_per_pixel = pixel_size;
  3088. /*
  3089. * IVB workaround: must disable low power watermarks for at least
  3090. * one frame before enabling scaling. LP watermarks can be re-enabled
  3091. * when scaling is disabled.
  3092. *
  3093. * WaCxSRDisabledForSpriteScaling:ivb
  3094. */
  3095. if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
  3096. intel_wait_for_vblank(dev, intel_plane->pipe);
  3097. ilk_update_wm(crtc);
  3098. }
  3099. static void skl_pipe_wm_active_state(uint32_t val,
  3100. struct skl_pipe_wm *active,
  3101. bool is_transwm,
  3102. bool is_cursor,
  3103. int i,
  3104. int level)
  3105. {
  3106. bool is_enabled = (val & PLANE_WM_EN) != 0;
  3107. if (!is_transwm) {
  3108. if (!is_cursor) {
  3109. active->wm[level].plane_en[i] = is_enabled;
  3110. active->wm[level].plane_res_b[i] =
  3111. val & PLANE_WM_BLOCKS_MASK;
  3112. active->wm[level].plane_res_l[i] =
  3113. (val >> PLANE_WM_LINES_SHIFT) &
  3114. PLANE_WM_LINES_MASK;
  3115. } else {
  3116. active->wm[level].cursor_en = is_enabled;
  3117. active->wm[level].cursor_res_b =
  3118. val & PLANE_WM_BLOCKS_MASK;
  3119. active->wm[level].cursor_res_l =
  3120. (val >> PLANE_WM_LINES_SHIFT) &
  3121. PLANE_WM_LINES_MASK;
  3122. }
  3123. } else {
  3124. if (!is_cursor) {
  3125. active->trans_wm.plane_en[i] = is_enabled;
  3126. active->trans_wm.plane_res_b[i] =
  3127. val & PLANE_WM_BLOCKS_MASK;
  3128. active->trans_wm.plane_res_l[i] =
  3129. (val >> PLANE_WM_LINES_SHIFT) &
  3130. PLANE_WM_LINES_MASK;
  3131. } else {
  3132. active->trans_wm.cursor_en = is_enabled;
  3133. active->trans_wm.cursor_res_b =
  3134. val & PLANE_WM_BLOCKS_MASK;
  3135. active->trans_wm.cursor_res_l =
  3136. (val >> PLANE_WM_LINES_SHIFT) &
  3137. PLANE_WM_LINES_MASK;
  3138. }
  3139. }
  3140. }
  3141. static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
  3142. {
  3143. struct drm_device *dev = crtc->dev;
  3144. struct drm_i915_private *dev_priv = dev->dev_private;
  3145. struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
  3146. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3147. struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
  3148. enum pipe pipe = intel_crtc->pipe;
  3149. int level, i, max_level;
  3150. uint32_t temp;
  3151. max_level = ilk_wm_max_level(dev);
  3152. hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
  3153. for (level = 0; level <= max_level; level++) {
  3154. for (i = 0; i < intel_num_planes(intel_crtc); i++)
  3155. hw->plane[pipe][i][level] =
  3156. I915_READ(PLANE_WM(pipe, i, level));
  3157. hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
  3158. }
  3159. for (i = 0; i < intel_num_planes(intel_crtc); i++)
  3160. hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
  3161. hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
  3162. if (!intel_crtc_active(crtc))
  3163. return;
  3164. hw->dirty[pipe] = true;
  3165. active->linetime = hw->wm_linetime[pipe];
  3166. for (level = 0; level <= max_level; level++) {
  3167. for (i = 0; i < intel_num_planes(intel_crtc); i++) {
  3168. temp = hw->plane[pipe][i][level];
  3169. skl_pipe_wm_active_state(temp, active, false,
  3170. false, i, level);
  3171. }
  3172. temp = hw->cursor[pipe][level];
  3173. skl_pipe_wm_active_state(temp, active, false, true, i, level);
  3174. }
  3175. for (i = 0; i < intel_num_planes(intel_crtc); i++) {
  3176. temp = hw->plane_trans[pipe][i];
  3177. skl_pipe_wm_active_state(temp, active, true, false, i, 0);
  3178. }
  3179. temp = hw->cursor_trans[pipe];
  3180. skl_pipe_wm_active_state(temp, active, true, true, i, 0);
  3181. }
  3182. void skl_wm_get_hw_state(struct drm_device *dev)
  3183. {
  3184. struct drm_i915_private *dev_priv = dev->dev_private;
  3185. struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
  3186. struct drm_crtc *crtc;
  3187. skl_ddb_get_hw_state(dev_priv, ddb);
  3188. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  3189. skl_pipe_wm_get_hw_state(crtc);
  3190. }
  3191. static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
  3192. {
  3193. struct drm_device *dev = crtc->dev;
  3194. struct drm_i915_private *dev_priv = dev->dev_private;
  3195. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  3196. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3197. struct intel_pipe_wm *active = &intel_crtc->wm.active;
  3198. enum pipe pipe = intel_crtc->pipe;
  3199. static const unsigned int wm0_pipe_reg[] = {
  3200. [PIPE_A] = WM0_PIPEA_ILK,
  3201. [PIPE_B] = WM0_PIPEB_ILK,
  3202. [PIPE_C] = WM0_PIPEC_IVB,
  3203. };
  3204. hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
  3205. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  3206. hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
  3207. active->pipe_enabled = intel_crtc_active(crtc);
  3208. if (active->pipe_enabled) {
  3209. u32 tmp = hw->wm_pipe[pipe];
  3210. /*
  3211. * For active pipes LP0 watermark is marked as
  3212. * enabled, and LP1+ watermaks as disabled since
  3213. * we can't really reverse compute them in case
  3214. * multiple pipes are active.
  3215. */
  3216. active->wm[0].enable = true;
  3217. active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
  3218. active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
  3219. active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
  3220. active->linetime = hw->wm_linetime[pipe];
  3221. } else {
  3222. int level, max_level = ilk_wm_max_level(dev);
  3223. /*
  3224. * For inactive pipes, all watermark levels
  3225. * should be marked as enabled but zeroed,
  3226. * which is what we'd compute them to.
  3227. */
  3228. for (level = 0; level <= max_level; level++)
  3229. active->wm[level].enable = true;
  3230. }
  3231. }
  3232. void ilk_wm_get_hw_state(struct drm_device *dev)
  3233. {
  3234. struct drm_i915_private *dev_priv = dev->dev_private;
  3235. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  3236. struct drm_crtc *crtc;
  3237. for_each_crtc(dev, crtc)
  3238. ilk_pipe_wm_get_hw_state(crtc);
  3239. hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
  3240. hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
  3241. hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
  3242. hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
  3243. if (INTEL_INFO(dev)->gen >= 7) {
  3244. hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
  3245. hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
  3246. }
  3247. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  3248. hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
  3249. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  3250. else if (IS_IVYBRIDGE(dev))
  3251. hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
  3252. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  3253. hw->enable_fbc_wm =
  3254. !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
  3255. }
  3256. /**
  3257. * intel_update_watermarks - update FIFO watermark values based on current modes
  3258. *
  3259. * Calculate watermark values for the various WM regs based on current mode
  3260. * and plane configuration.
  3261. *
  3262. * There are several cases to deal with here:
  3263. * - normal (i.e. non-self-refresh)
  3264. * - self-refresh (SR) mode
  3265. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3266. * - lines are small relative to FIFO size (buffer can hold more than 2
  3267. * lines), so need to account for TLB latency
  3268. *
  3269. * The normal calculation is:
  3270. * watermark = dotclock * bytes per pixel * latency
  3271. * where latency is platform & configuration dependent (we assume pessimal
  3272. * values here).
  3273. *
  3274. * The SR calculation is:
  3275. * watermark = (trunc(latency/line time)+1) * surface width *
  3276. * bytes per pixel
  3277. * where
  3278. * line time = htotal / dotclock
  3279. * surface width = hdisplay for normal plane and 64 for cursor
  3280. * and latency is assumed to be high, as above.
  3281. *
  3282. * The final value programmed to the register should always be rounded up,
  3283. * and include an extra 2 entries to account for clock crossings.
  3284. *
  3285. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3286. * to set the non-SR watermarks to 8.
  3287. */
  3288. void intel_update_watermarks(struct drm_crtc *crtc)
  3289. {
  3290. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  3291. if (dev_priv->display.update_wm)
  3292. dev_priv->display.update_wm(crtc);
  3293. }
  3294. void intel_update_sprite_watermarks(struct drm_plane *plane,
  3295. struct drm_crtc *crtc,
  3296. uint32_t sprite_width,
  3297. uint32_t sprite_height,
  3298. int pixel_size,
  3299. bool enabled, bool scaled)
  3300. {
  3301. struct drm_i915_private *dev_priv = plane->dev->dev_private;
  3302. if (dev_priv->display.update_sprite_wm)
  3303. dev_priv->display.update_sprite_wm(plane, crtc,
  3304. sprite_width, sprite_height,
  3305. pixel_size, enabled, scaled);
  3306. }
  3307. static struct drm_i915_gem_object *
  3308. intel_alloc_context_page(struct drm_device *dev)
  3309. {
  3310. struct drm_i915_gem_object *ctx;
  3311. int ret;
  3312. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  3313. ctx = i915_gem_alloc_object(dev, 4096);
  3314. if (!ctx) {
  3315. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  3316. return NULL;
  3317. }
  3318. ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
  3319. if (ret) {
  3320. DRM_ERROR("failed to pin power context: %d\n", ret);
  3321. goto err_unref;
  3322. }
  3323. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  3324. if (ret) {
  3325. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  3326. goto err_unpin;
  3327. }
  3328. return ctx;
  3329. err_unpin:
  3330. i915_gem_object_ggtt_unpin(ctx);
  3331. err_unref:
  3332. drm_gem_object_unreference(&ctx->base);
  3333. return NULL;
  3334. }
  3335. /**
  3336. * Lock protecting IPS related data structures
  3337. */
  3338. DEFINE_SPINLOCK(mchdev_lock);
  3339. /* Global for IPS driver to get at the current i915 device. Protected by
  3340. * mchdev_lock. */
  3341. static struct drm_i915_private *i915_mch_dev;
  3342. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  3343. {
  3344. struct drm_i915_private *dev_priv = dev->dev_private;
  3345. u16 rgvswctl;
  3346. assert_spin_locked(&mchdev_lock);
  3347. rgvswctl = I915_READ16(MEMSWCTL);
  3348. if (rgvswctl & MEMCTL_CMD_STS) {
  3349. DRM_DEBUG("gpu busy, RCS change rejected\n");
  3350. return false; /* still busy with another command */
  3351. }
  3352. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  3353. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  3354. I915_WRITE16(MEMSWCTL, rgvswctl);
  3355. POSTING_READ16(MEMSWCTL);
  3356. rgvswctl |= MEMCTL_CMD_STS;
  3357. I915_WRITE16(MEMSWCTL, rgvswctl);
  3358. return true;
  3359. }
  3360. static void ironlake_enable_drps(struct drm_device *dev)
  3361. {
  3362. struct drm_i915_private *dev_priv = dev->dev_private;
  3363. u32 rgvmodectl = I915_READ(MEMMODECTL);
  3364. u8 fmax, fmin, fstart, vstart;
  3365. spin_lock_irq(&mchdev_lock);
  3366. /* Enable temp reporting */
  3367. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  3368. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  3369. /* 100ms RC evaluation intervals */
  3370. I915_WRITE(RCUPEI, 100000);
  3371. I915_WRITE(RCDNEI, 100000);
  3372. /* Set max/min thresholds to 90ms and 80ms respectively */
  3373. I915_WRITE(RCBMAXAVG, 90000);
  3374. I915_WRITE(RCBMINAVG, 80000);
  3375. I915_WRITE(MEMIHYST, 1);
  3376. /* Set up min, max, and cur for interrupt handling */
  3377. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  3378. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  3379. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  3380. MEMMODE_FSTART_SHIFT;
  3381. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  3382. PXVFREQ_PX_SHIFT;
  3383. dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
  3384. dev_priv->ips.fstart = fstart;
  3385. dev_priv->ips.max_delay = fstart;
  3386. dev_priv->ips.min_delay = fmin;
  3387. dev_priv->ips.cur_delay = fstart;
  3388. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  3389. fmax, fmin, fstart);
  3390. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  3391. /*
  3392. * Interrupts will be enabled in ironlake_irq_postinstall
  3393. */
  3394. I915_WRITE(VIDSTART, vstart);
  3395. POSTING_READ(VIDSTART);
  3396. rgvmodectl |= MEMMODE_SWMODE_EN;
  3397. I915_WRITE(MEMMODECTL, rgvmodectl);
  3398. if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  3399. DRM_ERROR("stuck trying to change perf mode\n");
  3400. mdelay(1);
  3401. ironlake_set_drps(dev, fstart);
  3402. dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  3403. I915_READ(0x112e0);
  3404. dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
  3405. dev_priv->ips.last_count2 = I915_READ(0x112f4);
  3406. dev_priv->ips.last_time2 = ktime_get_raw_ns();
  3407. spin_unlock_irq(&mchdev_lock);
  3408. }
  3409. static void ironlake_disable_drps(struct drm_device *dev)
  3410. {
  3411. struct drm_i915_private *dev_priv = dev->dev_private;
  3412. u16 rgvswctl;
  3413. spin_lock_irq(&mchdev_lock);
  3414. rgvswctl = I915_READ16(MEMSWCTL);
  3415. /* Ack interrupts, disable EFC interrupt */
  3416. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  3417. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  3418. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  3419. I915_WRITE(DEIIR, DE_PCU_EVENT);
  3420. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  3421. /* Go back to the starting frequency */
  3422. ironlake_set_drps(dev, dev_priv->ips.fstart);
  3423. mdelay(1);
  3424. rgvswctl |= MEMCTL_CMD_STS;
  3425. I915_WRITE(MEMSWCTL, rgvswctl);
  3426. mdelay(1);
  3427. spin_unlock_irq(&mchdev_lock);
  3428. }
  3429. /* There's a funny hw issue where the hw returns all 0 when reading from
  3430. * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
  3431. * ourselves, instead of doing a rmw cycle (which might result in us clearing
  3432. * all limits and the gpu stuck at whatever frequency it is at atm).
  3433. */
  3434. static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
  3435. {
  3436. u32 limits;
  3437. /* Only set the down limit when we've reached the lowest level to avoid
  3438. * getting more interrupts, otherwise leave this clear. This prevents a
  3439. * race in the hw when coming out of rc6: There's a tiny window where
  3440. * the hw runs at the minimal clock before selecting the desired
  3441. * frequency, if the down threshold expires in that window we will not
  3442. * receive a down interrupt. */
  3443. limits = dev_priv->rps.max_freq_softlimit << 24;
  3444. if (val <= dev_priv->rps.min_freq_softlimit)
  3445. limits |= dev_priv->rps.min_freq_softlimit << 16;
  3446. return limits;
  3447. }
  3448. static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
  3449. {
  3450. int new_power;
  3451. new_power = dev_priv->rps.power;
  3452. switch (dev_priv->rps.power) {
  3453. case LOW_POWER:
  3454. if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
  3455. new_power = BETWEEN;
  3456. break;
  3457. case BETWEEN:
  3458. if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
  3459. new_power = LOW_POWER;
  3460. else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
  3461. new_power = HIGH_POWER;
  3462. break;
  3463. case HIGH_POWER:
  3464. if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
  3465. new_power = BETWEEN;
  3466. break;
  3467. }
  3468. /* Max/min bins are special */
  3469. if (val == dev_priv->rps.min_freq_softlimit)
  3470. new_power = LOW_POWER;
  3471. if (val == dev_priv->rps.max_freq_softlimit)
  3472. new_power = HIGH_POWER;
  3473. if (new_power == dev_priv->rps.power)
  3474. return;
  3475. /* Note the units here are not exactly 1us, but 1280ns. */
  3476. switch (new_power) {
  3477. case LOW_POWER:
  3478. /* Upclock if more than 95% busy over 16ms */
  3479. I915_WRITE(GEN6_RP_UP_EI, 12500);
  3480. I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
  3481. /* Downclock if less than 85% busy over 32ms */
  3482. I915_WRITE(GEN6_RP_DOWN_EI, 25000);
  3483. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
  3484. I915_WRITE(GEN6_RP_CONTROL,
  3485. GEN6_RP_MEDIA_TURBO |
  3486. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3487. GEN6_RP_MEDIA_IS_GFX |
  3488. GEN6_RP_ENABLE |
  3489. GEN6_RP_UP_BUSY_AVG |
  3490. GEN6_RP_DOWN_IDLE_AVG);
  3491. break;
  3492. case BETWEEN:
  3493. /* Upclock if more than 90% busy over 13ms */
  3494. I915_WRITE(GEN6_RP_UP_EI, 10250);
  3495. I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
  3496. /* Downclock if less than 75% busy over 32ms */
  3497. I915_WRITE(GEN6_RP_DOWN_EI, 25000);
  3498. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
  3499. I915_WRITE(GEN6_RP_CONTROL,
  3500. GEN6_RP_MEDIA_TURBO |
  3501. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3502. GEN6_RP_MEDIA_IS_GFX |
  3503. GEN6_RP_ENABLE |
  3504. GEN6_RP_UP_BUSY_AVG |
  3505. GEN6_RP_DOWN_IDLE_AVG);
  3506. break;
  3507. case HIGH_POWER:
  3508. /* Upclock if more than 85% busy over 10ms */
  3509. I915_WRITE(GEN6_RP_UP_EI, 8000);
  3510. I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
  3511. /* Downclock if less than 60% busy over 32ms */
  3512. I915_WRITE(GEN6_RP_DOWN_EI, 25000);
  3513. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
  3514. I915_WRITE(GEN6_RP_CONTROL,
  3515. GEN6_RP_MEDIA_TURBO |
  3516. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3517. GEN6_RP_MEDIA_IS_GFX |
  3518. GEN6_RP_ENABLE |
  3519. GEN6_RP_UP_BUSY_AVG |
  3520. GEN6_RP_DOWN_IDLE_AVG);
  3521. break;
  3522. }
  3523. dev_priv->rps.power = new_power;
  3524. dev_priv->rps.last_adj = 0;
  3525. }
  3526. static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
  3527. {
  3528. u32 mask = 0;
  3529. if (val > dev_priv->rps.min_freq_softlimit)
  3530. mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
  3531. if (val < dev_priv->rps.max_freq_softlimit)
  3532. mask |= GEN6_PM_RP_UP_THRESHOLD;
  3533. mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
  3534. mask &= dev_priv->pm_rps_events;
  3535. /* IVB and SNB hard hangs on looping batchbuffer
  3536. * if GEN6_PM_UP_EI_EXPIRED is masked.
  3537. */
  3538. if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
  3539. mask |= GEN6_PM_RP_UP_EI_EXPIRED;
  3540. if (IS_GEN8(dev_priv->dev))
  3541. mask |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
  3542. return ~mask;
  3543. }
  3544. /* gen6_set_rps is called to update the frequency request, but should also be
  3545. * called when the range (min_delay and max_delay) is modified so that we can
  3546. * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
  3547. void gen6_set_rps(struct drm_device *dev, u8 val)
  3548. {
  3549. struct drm_i915_private *dev_priv = dev->dev_private;
  3550. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3551. WARN_ON(val > dev_priv->rps.max_freq_softlimit);
  3552. WARN_ON(val < dev_priv->rps.min_freq_softlimit);
  3553. /* min/max delay may still have been modified so be sure to
  3554. * write the limits value.
  3555. */
  3556. if (val != dev_priv->rps.cur_freq) {
  3557. gen6_set_rps_thresholds(dev_priv, val);
  3558. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  3559. I915_WRITE(GEN6_RPNSWREQ,
  3560. HSW_FREQUENCY(val));
  3561. else
  3562. I915_WRITE(GEN6_RPNSWREQ,
  3563. GEN6_FREQUENCY(val) |
  3564. GEN6_OFFSET(0) |
  3565. GEN6_AGGRESSIVE_TURBO);
  3566. }
  3567. /* Make sure we continue to get interrupts
  3568. * until we hit the minimum or maximum frequencies.
  3569. */
  3570. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
  3571. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  3572. POSTING_READ(GEN6_RPNSWREQ);
  3573. dev_priv->rps.cur_freq = val;
  3574. trace_intel_gpu_freq_change(val * 50);
  3575. }
  3576. /* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
  3577. *
  3578. * * If Gfx is Idle, then
  3579. * 1. Mask Turbo interrupts
  3580. * 2. Bring up Gfx clock
  3581. * 3. Change the freq to Rpn and wait till P-Unit updates freq
  3582. * 4. Clear the Force GFX CLK ON bit so that Gfx can down
  3583. * 5. Unmask Turbo interrupts
  3584. */
  3585. static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
  3586. {
  3587. struct drm_device *dev = dev_priv->dev;
  3588. /* Latest VLV doesn't need to force the gfx clock */
  3589. if (dev->pdev->revision >= 0xd) {
  3590. valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3591. return;
  3592. }
  3593. /*
  3594. * When we are idle. Drop to min voltage state.
  3595. */
  3596. if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
  3597. return;
  3598. /* Mask turbo interrupt so that they will not come in between */
  3599. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  3600. vlv_force_gfx_clock(dev_priv, true);
  3601. dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
  3602. vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
  3603. dev_priv->rps.min_freq_softlimit);
  3604. if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
  3605. & GENFREQSTATUS) == 0, 5))
  3606. DRM_ERROR("timed out waiting for Punit\n");
  3607. vlv_force_gfx_clock(dev_priv, false);
  3608. I915_WRITE(GEN6_PMINTRMSK,
  3609. gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
  3610. }
  3611. void gen6_rps_idle(struct drm_i915_private *dev_priv)
  3612. {
  3613. struct drm_device *dev = dev_priv->dev;
  3614. mutex_lock(&dev_priv->rps.hw_lock);
  3615. if (dev_priv->rps.enabled) {
  3616. if (IS_CHERRYVIEW(dev))
  3617. valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3618. else if (IS_VALLEYVIEW(dev))
  3619. vlv_set_rps_idle(dev_priv);
  3620. else
  3621. gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3622. dev_priv->rps.last_adj = 0;
  3623. }
  3624. mutex_unlock(&dev_priv->rps.hw_lock);
  3625. }
  3626. void gen6_rps_boost(struct drm_i915_private *dev_priv)
  3627. {
  3628. struct drm_device *dev = dev_priv->dev;
  3629. mutex_lock(&dev_priv->rps.hw_lock);
  3630. if (dev_priv->rps.enabled) {
  3631. if (IS_VALLEYVIEW(dev))
  3632. valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
  3633. else
  3634. gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
  3635. dev_priv->rps.last_adj = 0;
  3636. }
  3637. mutex_unlock(&dev_priv->rps.hw_lock);
  3638. }
  3639. void valleyview_set_rps(struct drm_device *dev, u8 val)
  3640. {
  3641. struct drm_i915_private *dev_priv = dev->dev_private;
  3642. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3643. WARN_ON(val > dev_priv->rps.max_freq_softlimit);
  3644. WARN_ON(val < dev_priv->rps.min_freq_softlimit);
  3645. if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
  3646. "Odd GPU freq value\n"))
  3647. val &= ~1;
  3648. if (val != dev_priv->rps.cur_freq) {
  3649. DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
  3650. vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
  3651. dev_priv->rps.cur_freq,
  3652. vlv_gpu_freq(dev_priv, val), val);
  3653. vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
  3654. }
  3655. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  3656. dev_priv->rps.cur_freq = val;
  3657. trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
  3658. }
  3659. static void gen8_disable_rps_interrupts(struct drm_device *dev)
  3660. {
  3661. struct drm_i915_private *dev_priv = dev->dev_private;
  3662. I915_WRITE(GEN6_PMINTRMSK, ~GEN8_PMINTR_REDIRECT_TO_NON_DISP);
  3663. I915_WRITE(GEN8_GT_IER(2), I915_READ(GEN8_GT_IER(2)) &
  3664. ~dev_priv->pm_rps_events);
  3665. /* Complete PM interrupt masking here doesn't race with the rps work
  3666. * item again unmasking PM interrupts because that is using a different
  3667. * register (GEN8_GT_IMR(2)) to mask PM interrupts. The only risk is in
  3668. * leaving stale bits in GEN8_GT_IIR(2) and GEN8_GT_IMR(2) which
  3669. * gen8_enable_rps will clean up. */
  3670. spin_lock_irq(&dev_priv->irq_lock);
  3671. dev_priv->rps.pm_iir = 0;
  3672. spin_unlock_irq(&dev_priv->irq_lock);
  3673. I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
  3674. }
  3675. static void gen6_disable_rps_interrupts(struct drm_device *dev)
  3676. {
  3677. struct drm_i915_private *dev_priv = dev->dev_private;
  3678. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  3679. I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) &
  3680. ~dev_priv->pm_rps_events);
  3681. /* Complete PM interrupt masking here doesn't race with the rps work
  3682. * item again unmasking PM interrupts because that is using a different
  3683. * register (PMIMR) to mask PM interrupts. The only risk is in leaving
  3684. * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
  3685. spin_lock_irq(&dev_priv->irq_lock);
  3686. dev_priv->rps.pm_iir = 0;
  3687. spin_unlock_irq(&dev_priv->irq_lock);
  3688. I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
  3689. }
  3690. static void gen6_disable_rps(struct drm_device *dev)
  3691. {
  3692. struct drm_i915_private *dev_priv = dev->dev_private;
  3693. I915_WRITE(GEN6_RC_CONTROL, 0);
  3694. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  3695. if (IS_BROADWELL(dev))
  3696. gen8_disable_rps_interrupts(dev);
  3697. else
  3698. gen6_disable_rps_interrupts(dev);
  3699. }
  3700. static void cherryview_disable_rps(struct drm_device *dev)
  3701. {
  3702. struct drm_i915_private *dev_priv = dev->dev_private;
  3703. I915_WRITE(GEN6_RC_CONTROL, 0);
  3704. gen8_disable_rps_interrupts(dev);
  3705. }
  3706. static void valleyview_disable_rps(struct drm_device *dev)
  3707. {
  3708. struct drm_i915_private *dev_priv = dev->dev_private;
  3709. /* we're doing forcewake before Disabling RC6,
  3710. * This what the BIOS expects when going into suspend */
  3711. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  3712. I915_WRITE(GEN6_RC_CONTROL, 0);
  3713. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  3714. gen6_disable_rps_interrupts(dev);
  3715. }
  3716. static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
  3717. {
  3718. if (IS_VALLEYVIEW(dev)) {
  3719. if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
  3720. mode = GEN6_RC_CTL_RC6_ENABLE;
  3721. else
  3722. mode = 0;
  3723. }
  3724. if (HAS_RC6p(dev))
  3725. DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
  3726. (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
  3727. (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
  3728. (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
  3729. else
  3730. DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
  3731. (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
  3732. }
  3733. static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
  3734. {
  3735. /* No RC6 before Ironlake */
  3736. if (INTEL_INFO(dev)->gen < 5)
  3737. return 0;
  3738. /* RC6 is only on Ironlake mobile not on desktop */
  3739. if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
  3740. return 0;
  3741. /* Respect the kernel parameter if it is set */
  3742. if (enable_rc6 >= 0) {
  3743. int mask;
  3744. if (HAS_RC6p(dev))
  3745. mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
  3746. INTEL_RC6pp_ENABLE;
  3747. else
  3748. mask = INTEL_RC6_ENABLE;
  3749. if ((enable_rc6 & mask) != enable_rc6)
  3750. DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
  3751. enable_rc6 & mask, enable_rc6, mask);
  3752. return enable_rc6 & mask;
  3753. }
  3754. /* Disable RC6 on Ironlake */
  3755. if (INTEL_INFO(dev)->gen == 5)
  3756. return 0;
  3757. if (IS_IVYBRIDGE(dev))
  3758. return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
  3759. return INTEL_RC6_ENABLE;
  3760. }
  3761. int intel_enable_rc6(const struct drm_device *dev)
  3762. {
  3763. return i915.enable_rc6;
  3764. }
  3765. static void gen8_enable_rps_interrupts(struct drm_device *dev)
  3766. {
  3767. struct drm_i915_private *dev_priv = dev->dev_private;
  3768. spin_lock_irq(&dev_priv->irq_lock);
  3769. WARN_ON(dev_priv->rps.pm_iir);
  3770. gen8_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
  3771. I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
  3772. spin_unlock_irq(&dev_priv->irq_lock);
  3773. }
  3774. static void gen6_enable_rps_interrupts(struct drm_device *dev)
  3775. {
  3776. struct drm_i915_private *dev_priv = dev->dev_private;
  3777. spin_lock_irq(&dev_priv->irq_lock);
  3778. WARN_ON(dev_priv->rps.pm_iir);
  3779. gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
  3780. I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
  3781. spin_unlock_irq(&dev_priv->irq_lock);
  3782. }
  3783. static void parse_rp_state_cap(struct drm_i915_private *dev_priv, u32 rp_state_cap)
  3784. {
  3785. /* All of these values are in units of 50MHz */
  3786. dev_priv->rps.cur_freq = 0;
  3787. /* static values from HW: RP0 < RPe < RP1 < RPn (min_freq) */
  3788. dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
  3789. dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
  3790. dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
  3791. /* XXX: only BYT has a special efficient freq */
  3792. dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
  3793. /* hw_max = RP0 until we check for overclocking */
  3794. dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
  3795. /* Preserve min/max settings in case of re-init */
  3796. if (dev_priv->rps.max_freq_softlimit == 0)
  3797. dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
  3798. if (dev_priv->rps.min_freq_softlimit == 0)
  3799. dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
  3800. }
  3801. static void gen8_enable_rps(struct drm_device *dev)
  3802. {
  3803. struct drm_i915_private *dev_priv = dev->dev_private;
  3804. struct intel_engine_cs *ring;
  3805. uint32_t rc6_mask = 0, rp_state_cap;
  3806. int unused;
  3807. /* 1a: Software RC state - RC0 */
  3808. I915_WRITE(GEN6_RC_STATE, 0);
  3809. /* 1c & 1d: Get forcewake during program sequence. Although the driver
  3810. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  3811. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  3812. /* 2a: Disable RC states. */
  3813. I915_WRITE(GEN6_RC_CONTROL, 0);
  3814. rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  3815. parse_rp_state_cap(dev_priv, rp_state_cap);
  3816. /* 2b: Program RC6 thresholds.*/
  3817. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  3818. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  3819. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  3820. for_each_ring(ring, dev_priv, unused)
  3821. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  3822. I915_WRITE(GEN6_RC_SLEEP, 0);
  3823. if (IS_BROADWELL(dev))
  3824. I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
  3825. else
  3826. I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
  3827. /* 3: Enable RC6 */
  3828. if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
  3829. rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
  3830. intel_print_rc6_info(dev, rc6_mask);
  3831. if (IS_BROADWELL(dev))
  3832. I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
  3833. GEN7_RC_CTL_TO_MODE |
  3834. rc6_mask);
  3835. else
  3836. I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
  3837. GEN6_RC_CTL_EI_MODE(1) |
  3838. rc6_mask);
  3839. /* 4 Program defaults and thresholds for RPS*/
  3840. I915_WRITE(GEN6_RPNSWREQ,
  3841. HSW_FREQUENCY(dev_priv->rps.rp1_freq));
  3842. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  3843. HSW_FREQUENCY(dev_priv->rps.rp1_freq));
  3844. /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
  3845. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
  3846. /* Docs recommend 900MHz, and 300 MHz respectively */
  3847. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  3848. dev_priv->rps.max_freq_softlimit << 24 |
  3849. dev_priv->rps.min_freq_softlimit << 16);
  3850. I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
  3851. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
  3852. I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
  3853. I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
  3854. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  3855. /* 5: Enable RPS */
  3856. I915_WRITE(GEN6_RP_CONTROL,
  3857. GEN6_RP_MEDIA_TURBO |
  3858. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3859. GEN6_RP_MEDIA_IS_GFX |
  3860. GEN6_RP_ENABLE |
  3861. GEN6_RP_UP_BUSY_AVG |
  3862. GEN6_RP_DOWN_IDLE_AVG);
  3863. /* 6: Ring frequency + overclocking (our driver does this later */
  3864. gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
  3865. gen8_enable_rps_interrupts(dev);
  3866. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  3867. }
  3868. static void gen6_enable_rps(struct drm_device *dev)
  3869. {
  3870. struct drm_i915_private *dev_priv = dev->dev_private;
  3871. struct intel_engine_cs *ring;
  3872. u32 rp_state_cap;
  3873. u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
  3874. u32 gtfifodbg;
  3875. int rc6_mode;
  3876. int i, ret;
  3877. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3878. /* Here begins a magic sequence of register writes to enable
  3879. * auto-downclocking.
  3880. *
  3881. * Perhaps there might be some value in exposing these to
  3882. * userspace...
  3883. */
  3884. I915_WRITE(GEN6_RC_STATE, 0);
  3885. /* Clear the DBG now so we don't confuse earlier errors */
  3886. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  3887. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  3888. I915_WRITE(GTFIFODBG, gtfifodbg);
  3889. }
  3890. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  3891. rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  3892. parse_rp_state_cap(dev_priv, rp_state_cap);
  3893. /* disable the counters and set deterministic thresholds */
  3894. I915_WRITE(GEN6_RC_CONTROL, 0);
  3895. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  3896. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  3897. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  3898. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  3899. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  3900. for_each_ring(ring, dev_priv, i)
  3901. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  3902. I915_WRITE(GEN6_RC_SLEEP, 0);
  3903. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  3904. if (IS_IVYBRIDGE(dev))
  3905. I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
  3906. else
  3907. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  3908. I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
  3909. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  3910. /* Check if we are enabling RC6 */
  3911. rc6_mode = intel_enable_rc6(dev_priv->dev);
  3912. if (rc6_mode & INTEL_RC6_ENABLE)
  3913. rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
  3914. /* We don't use those on Haswell */
  3915. if (!IS_HASWELL(dev)) {
  3916. if (rc6_mode & INTEL_RC6p_ENABLE)
  3917. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  3918. if (rc6_mode & INTEL_RC6pp_ENABLE)
  3919. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  3920. }
  3921. intel_print_rc6_info(dev, rc6_mask);
  3922. I915_WRITE(GEN6_RC_CONTROL,
  3923. rc6_mask |
  3924. GEN6_RC_CTL_EI_MODE(1) |
  3925. GEN6_RC_CTL_HW_ENABLE);
  3926. /* Power down if completely idle for over 50ms */
  3927. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
  3928. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  3929. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
  3930. if (ret)
  3931. DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
  3932. ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
  3933. if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
  3934. DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
  3935. (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
  3936. (pcu_mbox & 0xff) * 50);
  3937. dev_priv->rps.max_freq = pcu_mbox & 0xff;
  3938. }
  3939. dev_priv->rps.power = HIGH_POWER; /* force a reset */
  3940. gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3941. gen6_enable_rps_interrupts(dev);
  3942. rc6vids = 0;
  3943. ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
  3944. if (IS_GEN6(dev) && ret) {
  3945. DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
  3946. } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
  3947. DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
  3948. GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
  3949. rc6vids &= 0xffff00;
  3950. rc6vids |= GEN6_ENCODE_RC6_VID(450);
  3951. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
  3952. if (ret)
  3953. DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
  3954. }
  3955. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  3956. }
  3957. static void __gen6_update_ring_freq(struct drm_device *dev)
  3958. {
  3959. struct drm_i915_private *dev_priv = dev->dev_private;
  3960. int min_freq = 15;
  3961. unsigned int gpu_freq;
  3962. unsigned int max_ia_freq, min_ring_freq;
  3963. int scaling_factor = 180;
  3964. struct cpufreq_policy *policy;
  3965. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3966. policy = cpufreq_cpu_get(0);
  3967. if (policy) {
  3968. max_ia_freq = policy->cpuinfo.max_freq;
  3969. cpufreq_cpu_put(policy);
  3970. } else {
  3971. /*
  3972. * Default to measured freq if none found, PCU will ensure we
  3973. * don't go over
  3974. */
  3975. max_ia_freq = tsc_khz;
  3976. }
  3977. /* Convert from kHz to MHz */
  3978. max_ia_freq /= 1000;
  3979. min_ring_freq = I915_READ(DCLK) & 0xf;
  3980. /* convert DDR frequency from units of 266.6MHz to bandwidth */
  3981. min_ring_freq = mult_frac(min_ring_freq, 8, 3);
  3982. /*
  3983. * For each potential GPU frequency, load a ring frequency we'd like
  3984. * to use for memory access. We do this by specifying the IA frequency
  3985. * the PCU should use as a reference to determine the ring frequency.
  3986. */
  3987. for (gpu_freq = dev_priv->rps.max_freq_softlimit; gpu_freq >= dev_priv->rps.min_freq_softlimit;
  3988. gpu_freq--) {
  3989. int diff = dev_priv->rps.max_freq_softlimit - gpu_freq;
  3990. unsigned int ia_freq = 0, ring_freq = 0;
  3991. if (INTEL_INFO(dev)->gen >= 8) {
  3992. /* max(2 * GT, DDR). NB: GT is 50MHz units */
  3993. ring_freq = max(min_ring_freq, gpu_freq);
  3994. } else if (IS_HASWELL(dev)) {
  3995. ring_freq = mult_frac(gpu_freq, 5, 4);
  3996. ring_freq = max(min_ring_freq, ring_freq);
  3997. /* leave ia_freq as the default, chosen by cpufreq */
  3998. } else {
  3999. /* On older processors, there is no separate ring
  4000. * clock domain, so in order to boost the bandwidth
  4001. * of the ring, we need to upclock the CPU (ia_freq).
  4002. *
  4003. * For GPU frequencies less than 750MHz,
  4004. * just use the lowest ring freq.
  4005. */
  4006. if (gpu_freq < min_freq)
  4007. ia_freq = 800;
  4008. else
  4009. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  4010. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  4011. }
  4012. sandybridge_pcode_write(dev_priv,
  4013. GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
  4014. ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
  4015. ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
  4016. gpu_freq);
  4017. }
  4018. }
  4019. void gen6_update_ring_freq(struct drm_device *dev)
  4020. {
  4021. struct drm_i915_private *dev_priv = dev->dev_private;
  4022. if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
  4023. return;
  4024. mutex_lock(&dev_priv->rps.hw_lock);
  4025. __gen6_update_ring_freq(dev);
  4026. mutex_unlock(&dev_priv->rps.hw_lock);
  4027. }
  4028. static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
  4029. {
  4030. u32 val, rp0;
  4031. val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
  4032. rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
  4033. return rp0;
  4034. }
  4035. static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  4036. {
  4037. u32 val, rpe;
  4038. val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
  4039. rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
  4040. return rpe;
  4041. }
  4042. static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
  4043. {
  4044. u32 val, rp1;
  4045. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  4046. rp1 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
  4047. return rp1;
  4048. }
  4049. static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
  4050. {
  4051. u32 val, rpn;
  4052. val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
  4053. rpn = (val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) & PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK;
  4054. return rpn;
  4055. }
  4056. static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
  4057. {
  4058. u32 val, rp1;
  4059. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  4060. rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
  4061. return rp1;
  4062. }
  4063. static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
  4064. {
  4065. u32 val, rp0;
  4066. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  4067. rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
  4068. /* Clamp to max */
  4069. rp0 = min_t(u32, rp0, 0xea);
  4070. return rp0;
  4071. }
  4072. static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  4073. {
  4074. u32 val, rpe;
  4075. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
  4076. rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
  4077. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
  4078. rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
  4079. return rpe;
  4080. }
  4081. static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
  4082. {
  4083. return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
  4084. }
  4085. /* Check that the pctx buffer wasn't move under us. */
  4086. static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
  4087. {
  4088. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  4089. WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
  4090. dev_priv->vlv_pctx->stolen->start);
  4091. }
  4092. /* Check that the pcbr address is not empty. */
  4093. static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
  4094. {
  4095. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  4096. WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
  4097. }
  4098. static void cherryview_setup_pctx(struct drm_device *dev)
  4099. {
  4100. struct drm_i915_private *dev_priv = dev->dev_private;
  4101. unsigned long pctx_paddr, paddr;
  4102. struct i915_gtt *gtt = &dev_priv->gtt;
  4103. u32 pcbr;
  4104. int pctx_size = 32*1024;
  4105. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  4106. pcbr = I915_READ(VLV_PCBR);
  4107. if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
  4108. paddr = (dev_priv->mm.stolen_base +
  4109. (gtt->stolen_size - pctx_size));
  4110. pctx_paddr = (paddr & (~4095));
  4111. I915_WRITE(VLV_PCBR, pctx_paddr);
  4112. }
  4113. }
  4114. static void valleyview_setup_pctx(struct drm_device *dev)
  4115. {
  4116. struct drm_i915_private *dev_priv = dev->dev_private;
  4117. struct drm_i915_gem_object *pctx;
  4118. unsigned long pctx_paddr;
  4119. u32 pcbr;
  4120. int pctx_size = 24*1024;
  4121. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  4122. pcbr = I915_READ(VLV_PCBR);
  4123. if (pcbr) {
  4124. /* BIOS set it up already, grab the pre-alloc'd space */
  4125. int pcbr_offset;
  4126. pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
  4127. pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
  4128. pcbr_offset,
  4129. I915_GTT_OFFSET_NONE,
  4130. pctx_size);
  4131. goto out;
  4132. }
  4133. /*
  4134. * From the Gunit register HAS:
  4135. * The Gfx driver is expected to program this register and ensure
  4136. * proper allocation within Gfx stolen memory. For example, this
  4137. * register should be programmed such than the PCBR range does not
  4138. * overlap with other ranges, such as the frame buffer, protected
  4139. * memory, or any other relevant ranges.
  4140. */
  4141. pctx = i915_gem_object_create_stolen(dev, pctx_size);
  4142. if (!pctx) {
  4143. DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
  4144. return;
  4145. }
  4146. pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
  4147. I915_WRITE(VLV_PCBR, pctx_paddr);
  4148. out:
  4149. dev_priv->vlv_pctx = pctx;
  4150. }
  4151. static void valleyview_cleanup_pctx(struct drm_device *dev)
  4152. {
  4153. struct drm_i915_private *dev_priv = dev->dev_private;
  4154. if (WARN_ON(!dev_priv->vlv_pctx))
  4155. return;
  4156. drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
  4157. dev_priv->vlv_pctx = NULL;
  4158. }
  4159. static void valleyview_init_gt_powersave(struct drm_device *dev)
  4160. {
  4161. struct drm_i915_private *dev_priv = dev->dev_private;
  4162. u32 val;
  4163. valleyview_setup_pctx(dev);
  4164. mutex_lock(&dev_priv->rps.hw_lock);
  4165. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  4166. switch ((val >> 6) & 3) {
  4167. case 0:
  4168. case 1:
  4169. dev_priv->mem_freq = 800;
  4170. break;
  4171. case 2:
  4172. dev_priv->mem_freq = 1066;
  4173. break;
  4174. case 3:
  4175. dev_priv->mem_freq = 1333;
  4176. break;
  4177. }
  4178. DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
  4179. dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
  4180. dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
  4181. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  4182. vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
  4183. dev_priv->rps.max_freq);
  4184. dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
  4185. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  4186. vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  4187. dev_priv->rps.efficient_freq);
  4188. dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
  4189. DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
  4190. vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
  4191. dev_priv->rps.rp1_freq);
  4192. dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
  4193. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  4194. vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
  4195. dev_priv->rps.min_freq);
  4196. /* Preserve min/max settings in case of re-init */
  4197. if (dev_priv->rps.max_freq_softlimit == 0)
  4198. dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
  4199. if (dev_priv->rps.min_freq_softlimit == 0)
  4200. dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
  4201. mutex_unlock(&dev_priv->rps.hw_lock);
  4202. }
  4203. static void cherryview_init_gt_powersave(struct drm_device *dev)
  4204. {
  4205. struct drm_i915_private *dev_priv = dev->dev_private;
  4206. u32 val;
  4207. cherryview_setup_pctx(dev);
  4208. mutex_lock(&dev_priv->rps.hw_lock);
  4209. val = vlv_punit_read(dev_priv, CCK_FUSE_REG);
  4210. switch ((val >> 2) & 0x7) {
  4211. case 0:
  4212. case 1:
  4213. dev_priv->rps.cz_freq = 200;
  4214. dev_priv->mem_freq = 1600;
  4215. break;
  4216. case 2:
  4217. dev_priv->rps.cz_freq = 267;
  4218. dev_priv->mem_freq = 1600;
  4219. break;
  4220. case 3:
  4221. dev_priv->rps.cz_freq = 333;
  4222. dev_priv->mem_freq = 2000;
  4223. break;
  4224. case 4:
  4225. dev_priv->rps.cz_freq = 320;
  4226. dev_priv->mem_freq = 1600;
  4227. break;
  4228. case 5:
  4229. dev_priv->rps.cz_freq = 400;
  4230. dev_priv->mem_freq = 1600;
  4231. break;
  4232. }
  4233. DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
  4234. dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
  4235. dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
  4236. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  4237. vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
  4238. dev_priv->rps.max_freq);
  4239. dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
  4240. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  4241. vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  4242. dev_priv->rps.efficient_freq);
  4243. dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
  4244. DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
  4245. vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
  4246. dev_priv->rps.rp1_freq);
  4247. dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
  4248. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  4249. vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
  4250. dev_priv->rps.min_freq);
  4251. WARN_ONCE((dev_priv->rps.max_freq |
  4252. dev_priv->rps.efficient_freq |
  4253. dev_priv->rps.rp1_freq |
  4254. dev_priv->rps.min_freq) & 1,
  4255. "Odd GPU freq values\n");
  4256. /* Preserve min/max settings in case of re-init */
  4257. if (dev_priv->rps.max_freq_softlimit == 0)
  4258. dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
  4259. if (dev_priv->rps.min_freq_softlimit == 0)
  4260. dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
  4261. mutex_unlock(&dev_priv->rps.hw_lock);
  4262. }
  4263. static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
  4264. {
  4265. valleyview_cleanup_pctx(dev);
  4266. }
  4267. static void cherryview_enable_rps(struct drm_device *dev)
  4268. {
  4269. struct drm_i915_private *dev_priv = dev->dev_private;
  4270. struct intel_engine_cs *ring;
  4271. u32 gtfifodbg, val, rc6_mode = 0, pcbr;
  4272. int i;
  4273. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  4274. gtfifodbg = I915_READ(GTFIFODBG);
  4275. if (gtfifodbg) {
  4276. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  4277. gtfifodbg);
  4278. I915_WRITE(GTFIFODBG, gtfifodbg);
  4279. }
  4280. cherryview_check_pctx(dev_priv);
  4281. /* 1a & 1b: Get forcewake during program sequence. Although the driver
  4282. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  4283. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  4284. /* 2a: Program RC6 thresholds.*/
  4285. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  4286. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  4287. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  4288. for_each_ring(ring, dev_priv, i)
  4289. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  4290. I915_WRITE(GEN6_RC_SLEEP, 0);
  4291. I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
  4292. /* allows RC6 residency counter to work */
  4293. I915_WRITE(VLV_COUNTER_CONTROL,
  4294. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
  4295. VLV_MEDIA_RC6_COUNT_EN |
  4296. VLV_RENDER_RC6_COUNT_EN));
  4297. /* For now we assume BIOS is allocating and populating the PCBR */
  4298. pcbr = I915_READ(VLV_PCBR);
  4299. DRM_DEBUG_DRIVER("PCBR offset : 0x%x\n", pcbr);
  4300. /* 3: Enable RC6 */
  4301. if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
  4302. (pcbr >> VLV_PCBR_ADDR_SHIFT))
  4303. rc6_mode = GEN6_RC_CTL_EI_MODE(1);
  4304. I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
  4305. /* 4 Program defaults and thresholds for RPS*/
  4306. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  4307. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  4308. I915_WRITE(GEN6_RP_UP_EI, 66000);
  4309. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  4310. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  4311. /* WaDisablePwrmtrEvent:chv (pre-production hw) */
  4312. I915_WRITE(0xA80C, I915_READ(0xA80C) & 0x00ffffff);
  4313. I915_WRITE(0xA810, I915_READ(0xA810) & 0xffffff00);
  4314. /* 5: Enable RPS */
  4315. I915_WRITE(GEN6_RP_CONTROL,
  4316. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  4317. GEN6_RP_MEDIA_IS_GFX | /* WaSetMaskForGfxBusyness:chv (pre-production hw ?) */
  4318. GEN6_RP_ENABLE |
  4319. GEN6_RP_UP_BUSY_AVG |
  4320. GEN6_RP_DOWN_IDLE_AVG);
  4321. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  4322. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
  4323. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  4324. dev_priv->rps.cur_freq = (val >> 8) & 0xff;
  4325. DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
  4326. vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
  4327. dev_priv->rps.cur_freq);
  4328. DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
  4329. vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  4330. dev_priv->rps.efficient_freq);
  4331. valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
  4332. gen8_enable_rps_interrupts(dev);
  4333. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  4334. }
  4335. static void valleyview_enable_rps(struct drm_device *dev)
  4336. {
  4337. struct drm_i915_private *dev_priv = dev->dev_private;
  4338. struct intel_engine_cs *ring;
  4339. u32 gtfifodbg, val, rc6_mode = 0;
  4340. int i;
  4341. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  4342. valleyview_check_pctx(dev_priv);
  4343. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  4344. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  4345. gtfifodbg);
  4346. I915_WRITE(GTFIFODBG, gtfifodbg);
  4347. }
  4348. /* If VLV, Forcewake all wells, else re-direct to regular path */
  4349. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  4350. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  4351. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  4352. I915_WRITE(GEN6_RP_UP_EI, 66000);
  4353. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  4354. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  4355. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
  4356. I915_WRITE(GEN6_RP_CONTROL,
  4357. GEN6_RP_MEDIA_TURBO |
  4358. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  4359. GEN6_RP_MEDIA_IS_GFX |
  4360. GEN6_RP_ENABLE |
  4361. GEN6_RP_UP_BUSY_AVG |
  4362. GEN6_RP_DOWN_IDLE_CONT);
  4363. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
  4364. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  4365. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  4366. for_each_ring(ring, dev_priv, i)
  4367. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  4368. I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
  4369. /* allows RC6 residency counter to work */
  4370. I915_WRITE(VLV_COUNTER_CONTROL,
  4371. _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
  4372. VLV_RENDER_RC0_COUNT_EN |
  4373. VLV_MEDIA_RC6_COUNT_EN |
  4374. VLV_RENDER_RC6_COUNT_EN));
  4375. if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
  4376. rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
  4377. intel_print_rc6_info(dev, rc6_mode);
  4378. I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
  4379. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  4380. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
  4381. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  4382. dev_priv->rps.cur_freq = (val >> 8) & 0xff;
  4383. DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
  4384. vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
  4385. dev_priv->rps.cur_freq);
  4386. DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
  4387. vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  4388. dev_priv->rps.efficient_freq);
  4389. valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
  4390. gen6_enable_rps_interrupts(dev);
  4391. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  4392. }
  4393. void ironlake_teardown_rc6(struct drm_device *dev)
  4394. {
  4395. struct drm_i915_private *dev_priv = dev->dev_private;
  4396. if (dev_priv->ips.renderctx) {
  4397. i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
  4398. drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
  4399. dev_priv->ips.renderctx = NULL;
  4400. }
  4401. if (dev_priv->ips.pwrctx) {
  4402. i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
  4403. drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
  4404. dev_priv->ips.pwrctx = NULL;
  4405. }
  4406. }
  4407. static void ironlake_disable_rc6(struct drm_device *dev)
  4408. {
  4409. struct drm_i915_private *dev_priv = dev->dev_private;
  4410. if (I915_READ(PWRCTXA)) {
  4411. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  4412. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  4413. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  4414. 50);
  4415. I915_WRITE(PWRCTXA, 0);
  4416. POSTING_READ(PWRCTXA);
  4417. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  4418. POSTING_READ(RSTDBYCTL);
  4419. }
  4420. }
  4421. static int ironlake_setup_rc6(struct drm_device *dev)
  4422. {
  4423. struct drm_i915_private *dev_priv = dev->dev_private;
  4424. if (dev_priv->ips.renderctx == NULL)
  4425. dev_priv->ips.renderctx = intel_alloc_context_page(dev);
  4426. if (!dev_priv->ips.renderctx)
  4427. return -ENOMEM;
  4428. if (dev_priv->ips.pwrctx == NULL)
  4429. dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
  4430. if (!dev_priv->ips.pwrctx) {
  4431. ironlake_teardown_rc6(dev);
  4432. return -ENOMEM;
  4433. }
  4434. return 0;
  4435. }
  4436. static void ironlake_enable_rc6(struct drm_device *dev)
  4437. {
  4438. struct drm_i915_private *dev_priv = dev->dev_private;
  4439. struct intel_engine_cs *ring = &dev_priv->ring[RCS];
  4440. bool was_interruptible;
  4441. int ret;
  4442. /* rc6 disabled by default due to repeated reports of hanging during
  4443. * boot and resume.
  4444. */
  4445. if (!intel_enable_rc6(dev))
  4446. return;
  4447. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  4448. ret = ironlake_setup_rc6(dev);
  4449. if (ret)
  4450. return;
  4451. was_interruptible = dev_priv->mm.interruptible;
  4452. dev_priv->mm.interruptible = false;
  4453. /*
  4454. * GPU can automatically power down the render unit if given a page
  4455. * to save state.
  4456. */
  4457. ret = intel_ring_begin(ring, 6);
  4458. if (ret) {
  4459. ironlake_teardown_rc6(dev);
  4460. dev_priv->mm.interruptible = was_interruptible;
  4461. return;
  4462. }
  4463. intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  4464. intel_ring_emit(ring, MI_SET_CONTEXT);
  4465. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
  4466. MI_MM_SPACE_GTT |
  4467. MI_SAVE_EXT_STATE_EN |
  4468. MI_RESTORE_EXT_STATE_EN |
  4469. MI_RESTORE_INHIBIT);
  4470. intel_ring_emit(ring, MI_SUSPEND_FLUSH);
  4471. intel_ring_emit(ring, MI_NOOP);
  4472. intel_ring_emit(ring, MI_FLUSH);
  4473. intel_ring_advance(ring);
  4474. /*
  4475. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  4476. * does an implicit flush, combined with MI_FLUSH above, it should be
  4477. * safe to assume that renderctx is valid
  4478. */
  4479. ret = intel_ring_idle(ring);
  4480. dev_priv->mm.interruptible = was_interruptible;
  4481. if (ret) {
  4482. DRM_ERROR("failed to enable ironlake power savings\n");
  4483. ironlake_teardown_rc6(dev);
  4484. return;
  4485. }
  4486. I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
  4487. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  4488. intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
  4489. }
  4490. static unsigned long intel_pxfreq(u32 vidfreq)
  4491. {
  4492. unsigned long freq;
  4493. int div = (vidfreq & 0x3f0000) >> 16;
  4494. int post = (vidfreq & 0x3000) >> 12;
  4495. int pre = (vidfreq & 0x7);
  4496. if (!pre)
  4497. return 0;
  4498. freq = ((div * 133333) / ((1<<post) * pre));
  4499. return freq;
  4500. }
  4501. static const struct cparams {
  4502. u16 i;
  4503. u16 t;
  4504. u16 m;
  4505. u16 c;
  4506. } cparams[] = {
  4507. { 1, 1333, 301, 28664 },
  4508. { 1, 1066, 294, 24460 },
  4509. { 1, 800, 294, 25192 },
  4510. { 0, 1333, 276, 27605 },
  4511. { 0, 1066, 276, 27605 },
  4512. { 0, 800, 231, 23784 },
  4513. };
  4514. static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
  4515. {
  4516. u64 total_count, diff, ret;
  4517. u32 count1, count2, count3, m = 0, c = 0;
  4518. unsigned long now = jiffies_to_msecs(jiffies), diff1;
  4519. int i;
  4520. assert_spin_locked(&mchdev_lock);
  4521. diff1 = now - dev_priv->ips.last_time1;
  4522. /* Prevent division-by-zero if we are asking too fast.
  4523. * Also, we don't get interesting results if we are polling
  4524. * faster than once in 10ms, so just return the saved value
  4525. * in such cases.
  4526. */
  4527. if (diff1 <= 10)
  4528. return dev_priv->ips.chipset_power;
  4529. count1 = I915_READ(DMIEC);
  4530. count2 = I915_READ(DDREC);
  4531. count3 = I915_READ(CSIEC);
  4532. total_count = count1 + count2 + count3;
  4533. /* FIXME: handle per-counter overflow */
  4534. if (total_count < dev_priv->ips.last_count1) {
  4535. diff = ~0UL - dev_priv->ips.last_count1;
  4536. diff += total_count;
  4537. } else {
  4538. diff = total_count - dev_priv->ips.last_count1;
  4539. }
  4540. for (i = 0; i < ARRAY_SIZE(cparams); i++) {
  4541. if (cparams[i].i == dev_priv->ips.c_m &&
  4542. cparams[i].t == dev_priv->ips.r_t) {
  4543. m = cparams[i].m;
  4544. c = cparams[i].c;
  4545. break;
  4546. }
  4547. }
  4548. diff = div_u64(diff, diff1);
  4549. ret = ((m * diff) + c);
  4550. ret = div_u64(ret, 10);
  4551. dev_priv->ips.last_count1 = total_count;
  4552. dev_priv->ips.last_time1 = now;
  4553. dev_priv->ips.chipset_power = ret;
  4554. return ret;
  4555. }
  4556. unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
  4557. {
  4558. struct drm_device *dev = dev_priv->dev;
  4559. unsigned long val;
  4560. if (INTEL_INFO(dev)->gen != 5)
  4561. return 0;
  4562. spin_lock_irq(&mchdev_lock);
  4563. val = __i915_chipset_val(dev_priv);
  4564. spin_unlock_irq(&mchdev_lock);
  4565. return val;
  4566. }
  4567. unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
  4568. {
  4569. unsigned long m, x, b;
  4570. u32 tsfs;
  4571. tsfs = I915_READ(TSFS);
  4572. m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
  4573. x = I915_READ8(TR1);
  4574. b = tsfs & TSFS_INTR_MASK;
  4575. return ((m * x) / 127) - b;
  4576. }
  4577. static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
  4578. {
  4579. struct drm_device *dev = dev_priv->dev;
  4580. static const struct v_table {
  4581. u16 vd; /* in .1 mil */
  4582. u16 vm; /* in .1 mil */
  4583. } v_table[] = {
  4584. { 0, 0, },
  4585. { 375, 0, },
  4586. { 500, 0, },
  4587. { 625, 0, },
  4588. { 750, 0, },
  4589. { 875, 0, },
  4590. { 1000, 0, },
  4591. { 1125, 0, },
  4592. { 4125, 3000, },
  4593. { 4125, 3000, },
  4594. { 4125, 3000, },
  4595. { 4125, 3000, },
  4596. { 4125, 3000, },
  4597. { 4125, 3000, },
  4598. { 4125, 3000, },
  4599. { 4125, 3000, },
  4600. { 4125, 3000, },
  4601. { 4125, 3000, },
  4602. { 4125, 3000, },
  4603. { 4125, 3000, },
  4604. { 4125, 3000, },
  4605. { 4125, 3000, },
  4606. { 4125, 3000, },
  4607. { 4125, 3000, },
  4608. { 4125, 3000, },
  4609. { 4125, 3000, },
  4610. { 4125, 3000, },
  4611. { 4125, 3000, },
  4612. { 4125, 3000, },
  4613. { 4125, 3000, },
  4614. { 4125, 3000, },
  4615. { 4125, 3000, },
  4616. { 4250, 3125, },
  4617. { 4375, 3250, },
  4618. { 4500, 3375, },
  4619. { 4625, 3500, },
  4620. { 4750, 3625, },
  4621. { 4875, 3750, },
  4622. { 5000, 3875, },
  4623. { 5125, 4000, },
  4624. { 5250, 4125, },
  4625. { 5375, 4250, },
  4626. { 5500, 4375, },
  4627. { 5625, 4500, },
  4628. { 5750, 4625, },
  4629. { 5875, 4750, },
  4630. { 6000, 4875, },
  4631. { 6125, 5000, },
  4632. { 6250, 5125, },
  4633. { 6375, 5250, },
  4634. { 6500, 5375, },
  4635. { 6625, 5500, },
  4636. { 6750, 5625, },
  4637. { 6875, 5750, },
  4638. { 7000, 5875, },
  4639. { 7125, 6000, },
  4640. { 7250, 6125, },
  4641. { 7375, 6250, },
  4642. { 7500, 6375, },
  4643. { 7625, 6500, },
  4644. { 7750, 6625, },
  4645. { 7875, 6750, },
  4646. { 8000, 6875, },
  4647. { 8125, 7000, },
  4648. { 8250, 7125, },
  4649. { 8375, 7250, },
  4650. { 8500, 7375, },
  4651. { 8625, 7500, },
  4652. { 8750, 7625, },
  4653. { 8875, 7750, },
  4654. { 9000, 7875, },
  4655. { 9125, 8000, },
  4656. { 9250, 8125, },
  4657. { 9375, 8250, },
  4658. { 9500, 8375, },
  4659. { 9625, 8500, },
  4660. { 9750, 8625, },
  4661. { 9875, 8750, },
  4662. { 10000, 8875, },
  4663. { 10125, 9000, },
  4664. { 10250, 9125, },
  4665. { 10375, 9250, },
  4666. { 10500, 9375, },
  4667. { 10625, 9500, },
  4668. { 10750, 9625, },
  4669. { 10875, 9750, },
  4670. { 11000, 9875, },
  4671. { 11125, 10000, },
  4672. { 11250, 10125, },
  4673. { 11375, 10250, },
  4674. { 11500, 10375, },
  4675. { 11625, 10500, },
  4676. { 11750, 10625, },
  4677. { 11875, 10750, },
  4678. { 12000, 10875, },
  4679. { 12125, 11000, },
  4680. { 12250, 11125, },
  4681. { 12375, 11250, },
  4682. { 12500, 11375, },
  4683. { 12625, 11500, },
  4684. { 12750, 11625, },
  4685. { 12875, 11750, },
  4686. { 13000, 11875, },
  4687. { 13125, 12000, },
  4688. { 13250, 12125, },
  4689. { 13375, 12250, },
  4690. { 13500, 12375, },
  4691. { 13625, 12500, },
  4692. { 13750, 12625, },
  4693. { 13875, 12750, },
  4694. { 14000, 12875, },
  4695. { 14125, 13000, },
  4696. { 14250, 13125, },
  4697. { 14375, 13250, },
  4698. { 14500, 13375, },
  4699. { 14625, 13500, },
  4700. { 14750, 13625, },
  4701. { 14875, 13750, },
  4702. { 15000, 13875, },
  4703. { 15125, 14000, },
  4704. { 15250, 14125, },
  4705. { 15375, 14250, },
  4706. { 15500, 14375, },
  4707. { 15625, 14500, },
  4708. { 15750, 14625, },
  4709. { 15875, 14750, },
  4710. { 16000, 14875, },
  4711. { 16125, 15000, },
  4712. };
  4713. if (INTEL_INFO(dev)->is_mobile)
  4714. return v_table[pxvid].vm;
  4715. else
  4716. return v_table[pxvid].vd;
  4717. }
  4718. static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
  4719. {
  4720. u64 now, diff, diffms;
  4721. u32 count;
  4722. assert_spin_locked(&mchdev_lock);
  4723. now = ktime_get_raw_ns();
  4724. diffms = now - dev_priv->ips.last_time2;
  4725. do_div(diffms, NSEC_PER_MSEC);
  4726. /* Don't divide by 0 */
  4727. if (!diffms)
  4728. return;
  4729. count = I915_READ(GFXEC);
  4730. if (count < dev_priv->ips.last_count2) {
  4731. diff = ~0UL - dev_priv->ips.last_count2;
  4732. diff += count;
  4733. } else {
  4734. diff = count - dev_priv->ips.last_count2;
  4735. }
  4736. dev_priv->ips.last_count2 = count;
  4737. dev_priv->ips.last_time2 = now;
  4738. /* More magic constants... */
  4739. diff = diff * 1181;
  4740. diff = div_u64(diff, diffms * 10);
  4741. dev_priv->ips.gfx_power = diff;
  4742. }
  4743. void i915_update_gfx_val(struct drm_i915_private *dev_priv)
  4744. {
  4745. struct drm_device *dev = dev_priv->dev;
  4746. if (INTEL_INFO(dev)->gen != 5)
  4747. return;
  4748. spin_lock_irq(&mchdev_lock);
  4749. __i915_update_gfx_val(dev_priv);
  4750. spin_unlock_irq(&mchdev_lock);
  4751. }
  4752. static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
  4753. {
  4754. unsigned long t, corr, state1, corr2, state2;
  4755. u32 pxvid, ext_v;
  4756. assert_spin_locked(&mchdev_lock);
  4757. pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
  4758. pxvid = (pxvid >> 24) & 0x7f;
  4759. ext_v = pvid_to_extvid(dev_priv, pxvid);
  4760. state1 = ext_v;
  4761. t = i915_mch_val(dev_priv);
  4762. /* Revel in the empirically derived constants */
  4763. /* Correction factor in 1/100000 units */
  4764. if (t > 80)
  4765. corr = ((t * 2349) + 135940);
  4766. else if (t >= 50)
  4767. corr = ((t * 964) + 29317);
  4768. else /* < 50 */
  4769. corr = ((t * 301) + 1004);
  4770. corr = corr * ((150142 * state1) / 10000 - 78642);
  4771. corr /= 100000;
  4772. corr2 = (corr * dev_priv->ips.corr);
  4773. state2 = (corr2 * state1) / 10000;
  4774. state2 /= 100; /* convert to mW */
  4775. __i915_update_gfx_val(dev_priv);
  4776. return dev_priv->ips.gfx_power + state2;
  4777. }
  4778. unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
  4779. {
  4780. struct drm_device *dev = dev_priv->dev;
  4781. unsigned long val;
  4782. if (INTEL_INFO(dev)->gen != 5)
  4783. return 0;
  4784. spin_lock_irq(&mchdev_lock);
  4785. val = __i915_gfx_val(dev_priv);
  4786. spin_unlock_irq(&mchdev_lock);
  4787. return val;
  4788. }
  4789. /**
  4790. * i915_read_mch_val - return value for IPS use
  4791. *
  4792. * Calculate and return a value for the IPS driver to use when deciding whether
  4793. * we have thermal and power headroom to increase CPU or GPU power budget.
  4794. */
  4795. unsigned long i915_read_mch_val(void)
  4796. {
  4797. struct drm_i915_private *dev_priv;
  4798. unsigned long chipset_val, graphics_val, ret = 0;
  4799. spin_lock_irq(&mchdev_lock);
  4800. if (!i915_mch_dev)
  4801. goto out_unlock;
  4802. dev_priv = i915_mch_dev;
  4803. chipset_val = __i915_chipset_val(dev_priv);
  4804. graphics_val = __i915_gfx_val(dev_priv);
  4805. ret = chipset_val + graphics_val;
  4806. out_unlock:
  4807. spin_unlock_irq(&mchdev_lock);
  4808. return ret;
  4809. }
  4810. EXPORT_SYMBOL_GPL(i915_read_mch_val);
  4811. /**
  4812. * i915_gpu_raise - raise GPU frequency limit
  4813. *
  4814. * Raise the limit; IPS indicates we have thermal headroom.
  4815. */
  4816. bool i915_gpu_raise(void)
  4817. {
  4818. struct drm_i915_private *dev_priv;
  4819. bool ret = true;
  4820. spin_lock_irq(&mchdev_lock);
  4821. if (!i915_mch_dev) {
  4822. ret = false;
  4823. goto out_unlock;
  4824. }
  4825. dev_priv = i915_mch_dev;
  4826. if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
  4827. dev_priv->ips.max_delay--;
  4828. out_unlock:
  4829. spin_unlock_irq(&mchdev_lock);
  4830. return ret;
  4831. }
  4832. EXPORT_SYMBOL_GPL(i915_gpu_raise);
  4833. /**
  4834. * i915_gpu_lower - lower GPU frequency limit
  4835. *
  4836. * IPS indicates we're close to a thermal limit, so throttle back the GPU
  4837. * frequency maximum.
  4838. */
  4839. bool i915_gpu_lower(void)
  4840. {
  4841. struct drm_i915_private *dev_priv;
  4842. bool ret = true;
  4843. spin_lock_irq(&mchdev_lock);
  4844. if (!i915_mch_dev) {
  4845. ret = false;
  4846. goto out_unlock;
  4847. }
  4848. dev_priv = i915_mch_dev;
  4849. if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
  4850. dev_priv->ips.max_delay++;
  4851. out_unlock:
  4852. spin_unlock_irq(&mchdev_lock);
  4853. return ret;
  4854. }
  4855. EXPORT_SYMBOL_GPL(i915_gpu_lower);
  4856. /**
  4857. * i915_gpu_busy - indicate GPU business to IPS
  4858. *
  4859. * Tell the IPS driver whether or not the GPU is busy.
  4860. */
  4861. bool i915_gpu_busy(void)
  4862. {
  4863. struct drm_i915_private *dev_priv;
  4864. struct intel_engine_cs *ring;
  4865. bool ret = false;
  4866. int i;
  4867. spin_lock_irq(&mchdev_lock);
  4868. if (!i915_mch_dev)
  4869. goto out_unlock;
  4870. dev_priv = i915_mch_dev;
  4871. for_each_ring(ring, dev_priv, i)
  4872. ret |= !list_empty(&ring->request_list);
  4873. out_unlock:
  4874. spin_unlock_irq(&mchdev_lock);
  4875. return ret;
  4876. }
  4877. EXPORT_SYMBOL_GPL(i915_gpu_busy);
  4878. /**
  4879. * i915_gpu_turbo_disable - disable graphics turbo
  4880. *
  4881. * Disable graphics turbo by resetting the max frequency and setting the
  4882. * current frequency to the default.
  4883. */
  4884. bool i915_gpu_turbo_disable(void)
  4885. {
  4886. struct drm_i915_private *dev_priv;
  4887. bool ret = true;
  4888. spin_lock_irq(&mchdev_lock);
  4889. if (!i915_mch_dev) {
  4890. ret = false;
  4891. goto out_unlock;
  4892. }
  4893. dev_priv = i915_mch_dev;
  4894. dev_priv->ips.max_delay = dev_priv->ips.fstart;
  4895. if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
  4896. ret = false;
  4897. out_unlock:
  4898. spin_unlock_irq(&mchdev_lock);
  4899. return ret;
  4900. }
  4901. EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
  4902. /**
  4903. * Tells the intel_ips driver that the i915 driver is now loaded, if
  4904. * IPS got loaded first.
  4905. *
  4906. * This awkward dance is so that neither module has to depend on the
  4907. * other in order for IPS to do the appropriate communication of
  4908. * GPU turbo limits to i915.
  4909. */
  4910. static void
  4911. ips_ping_for_i915_load(void)
  4912. {
  4913. void (*link)(void);
  4914. link = symbol_get(ips_link_to_i915_driver);
  4915. if (link) {
  4916. link();
  4917. symbol_put(ips_link_to_i915_driver);
  4918. }
  4919. }
  4920. void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
  4921. {
  4922. /* We only register the i915 ips part with intel-ips once everything is
  4923. * set up, to avoid intel-ips sneaking in and reading bogus values. */
  4924. spin_lock_irq(&mchdev_lock);
  4925. i915_mch_dev = dev_priv;
  4926. spin_unlock_irq(&mchdev_lock);
  4927. ips_ping_for_i915_load();
  4928. }
  4929. void intel_gpu_ips_teardown(void)
  4930. {
  4931. spin_lock_irq(&mchdev_lock);
  4932. i915_mch_dev = NULL;
  4933. spin_unlock_irq(&mchdev_lock);
  4934. }
  4935. static void intel_init_emon(struct drm_device *dev)
  4936. {
  4937. struct drm_i915_private *dev_priv = dev->dev_private;
  4938. u32 lcfuse;
  4939. u8 pxw[16];
  4940. int i;
  4941. /* Disable to program */
  4942. I915_WRITE(ECR, 0);
  4943. POSTING_READ(ECR);
  4944. /* Program energy weights for various events */
  4945. I915_WRITE(SDEW, 0x15040d00);
  4946. I915_WRITE(CSIEW0, 0x007f0000);
  4947. I915_WRITE(CSIEW1, 0x1e220004);
  4948. I915_WRITE(CSIEW2, 0x04000004);
  4949. for (i = 0; i < 5; i++)
  4950. I915_WRITE(PEW + (i * 4), 0);
  4951. for (i = 0; i < 3; i++)
  4952. I915_WRITE(DEW + (i * 4), 0);
  4953. /* Program P-state weights to account for frequency power adjustment */
  4954. for (i = 0; i < 16; i++) {
  4955. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  4956. unsigned long freq = intel_pxfreq(pxvidfreq);
  4957. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  4958. PXVFREQ_PX_SHIFT;
  4959. unsigned long val;
  4960. val = vid * vid;
  4961. val *= (freq / 1000);
  4962. val *= 255;
  4963. val /= (127*127*900);
  4964. if (val > 0xff)
  4965. DRM_ERROR("bad pxval: %ld\n", val);
  4966. pxw[i] = val;
  4967. }
  4968. /* Render standby states get 0 weight */
  4969. pxw[14] = 0;
  4970. pxw[15] = 0;
  4971. for (i = 0; i < 4; i++) {
  4972. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  4973. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  4974. I915_WRITE(PXW + (i * 4), val);
  4975. }
  4976. /* Adjust magic regs to magic values (more experimental results) */
  4977. I915_WRITE(OGW0, 0);
  4978. I915_WRITE(OGW1, 0);
  4979. I915_WRITE(EG0, 0x00007f00);
  4980. I915_WRITE(EG1, 0x0000000e);
  4981. I915_WRITE(EG2, 0x000e0000);
  4982. I915_WRITE(EG3, 0x68000300);
  4983. I915_WRITE(EG4, 0x42000000);
  4984. I915_WRITE(EG5, 0x00140031);
  4985. I915_WRITE(EG6, 0);
  4986. I915_WRITE(EG7, 0);
  4987. for (i = 0; i < 8; i++)
  4988. I915_WRITE(PXWL + (i * 4), 0);
  4989. /* Enable PMON + select events */
  4990. I915_WRITE(ECR, 0x80000019);
  4991. lcfuse = I915_READ(LCFUSE02);
  4992. dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
  4993. }
  4994. void intel_init_gt_powersave(struct drm_device *dev)
  4995. {
  4996. i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
  4997. if (IS_CHERRYVIEW(dev))
  4998. cherryview_init_gt_powersave(dev);
  4999. else if (IS_VALLEYVIEW(dev))
  5000. valleyview_init_gt_powersave(dev);
  5001. }
  5002. void intel_cleanup_gt_powersave(struct drm_device *dev)
  5003. {
  5004. if (IS_CHERRYVIEW(dev))
  5005. return;
  5006. else if (IS_VALLEYVIEW(dev))
  5007. valleyview_cleanup_gt_powersave(dev);
  5008. }
  5009. /**
  5010. * intel_suspend_gt_powersave - suspend PM work and helper threads
  5011. * @dev: drm device
  5012. *
  5013. * We don't want to disable RC6 or other features here, we just want
  5014. * to make sure any work we've queued has finished and won't bother
  5015. * us while we're suspended.
  5016. */
  5017. void intel_suspend_gt_powersave(struct drm_device *dev)
  5018. {
  5019. struct drm_i915_private *dev_priv = dev->dev_private;
  5020. /* Interrupts should be disabled already to avoid re-arming. */
  5021. WARN_ON(intel_irqs_enabled(dev_priv));
  5022. flush_delayed_work(&dev_priv->rps.delayed_resume_work);
  5023. cancel_work_sync(&dev_priv->rps.work);
  5024. /* Force GPU to min freq during suspend */
  5025. gen6_rps_idle(dev_priv);
  5026. }
  5027. void intel_disable_gt_powersave(struct drm_device *dev)
  5028. {
  5029. struct drm_i915_private *dev_priv = dev->dev_private;
  5030. /* Interrupts should be disabled already to avoid re-arming. */
  5031. WARN_ON(intel_irqs_enabled(dev_priv));
  5032. if (IS_IRONLAKE_M(dev)) {
  5033. ironlake_disable_drps(dev);
  5034. ironlake_disable_rc6(dev);
  5035. } else if (INTEL_INFO(dev)->gen >= 6) {
  5036. intel_suspend_gt_powersave(dev);
  5037. mutex_lock(&dev_priv->rps.hw_lock);
  5038. if (IS_CHERRYVIEW(dev))
  5039. cherryview_disable_rps(dev);
  5040. else if (IS_VALLEYVIEW(dev))
  5041. valleyview_disable_rps(dev);
  5042. else
  5043. gen6_disable_rps(dev);
  5044. dev_priv->rps.enabled = false;
  5045. mutex_unlock(&dev_priv->rps.hw_lock);
  5046. }
  5047. }
  5048. static void intel_gen6_powersave_work(struct work_struct *work)
  5049. {
  5050. struct drm_i915_private *dev_priv =
  5051. container_of(work, struct drm_i915_private,
  5052. rps.delayed_resume_work.work);
  5053. struct drm_device *dev = dev_priv->dev;
  5054. mutex_lock(&dev_priv->rps.hw_lock);
  5055. if (IS_CHERRYVIEW(dev)) {
  5056. cherryview_enable_rps(dev);
  5057. } else if (IS_VALLEYVIEW(dev)) {
  5058. valleyview_enable_rps(dev);
  5059. } else if (IS_BROADWELL(dev)) {
  5060. gen8_enable_rps(dev);
  5061. __gen6_update_ring_freq(dev);
  5062. } else {
  5063. gen6_enable_rps(dev);
  5064. __gen6_update_ring_freq(dev);
  5065. }
  5066. dev_priv->rps.enabled = true;
  5067. mutex_unlock(&dev_priv->rps.hw_lock);
  5068. intel_runtime_pm_put(dev_priv);
  5069. }
  5070. void intel_enable_gt_powersave(struct drm_device *dev)
  5071. {
  5072. struct drm_i915_private *dev_priv = dev->dev_private;
  5073. if (IS_IRONLAKE_M(dev)) {
  5074. mutex_lock(&dev->struct_mutex);
  5075. ironlake_enable_drps(dev);
  5076. ironlake_enable_rc6(dev);
  5077. intel_init_emon(dev);
  5078. mutex_unlock(&dev->struct_mutex);
  5079. } else if (INTEL_INFO(dev)->gen >= 6) {
  5080. /*
  5081. * PCU communication is slow and this doesn't need to be
  5082. * done at any specific time, so do this out of our fast path
  5083. * to make resume and init faster.
  5084. *
  5085. * We depend on the HW RC6 power context save/restore
  5086. * mechanism when entering D3 through runtime PM suspend. So
  5087. * disable RPM until RPS/RC6 is properly setup. We can only
  5088. * get here via the driver load/system resume/runtime resume
  5089. * paths, so the _noresume version is enough (and in case of
  5090. * runtime resume it's necessary).
  5091. */
  5092. if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
  5093. round_jiffies_up_relative(HZ)))
  5094. intel_runtime_pm_get_noresume(dev_priv);
  5095. }
  5096. }
  5097. void intel_reset_gt_powersave(struct drm_device *dev)
  5098. {
  5099. struct drm_i915_private *dev_priv = dev->dev_private;
  5100. dev_priv->rps.enabled = false;
  5101. intel_enable_gt_powersave(dev);
  5102. }
  5103. static void ibx_init_clock_gating(struct drm_device *dev)
  5104. {
  5105. struct drm_i915_private *dev_priv = dev->dev_private;
  5106. /*
  5107. * On Ibex Peak and Cougar Point, we need to disable clock
  5108. * gating for the panel power sequencer or it will fail to
  5109. * start up when no ports are active.
  5110. */
  5111. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  5112. }
  5113. static void g4x_disable_trickle_feed(struct drm_device *dev)
  5114. {
  5115. struct drm_i915_private *dev_priv = dev->dev_private;
  5116. int pipe;
  5117. for_each_pipe(dev_priv, pipe) {
  5118. I915_WRITE(DSPCNTR(pipe),
  5119. I915_READ(DSPCNTR(pipe)) |
  5120. DISPPLANE_TRICKLE_FEED_DISABLE);
  5121. intel_flush_primary_plane(dev_priv, pipe);
  5122. }
  5123. }
  5124. static void ilk_init_lp_watermarks(struct drm_device *dev)
  5125. {
  5126. struct drm_i915_private *dev_priv = dev->dev_private;
  5127. I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
  5128. I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
  5129. I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
  5130. /*
  5131. * Don't touch WM1S_LP_EN here.
  5132. * Doing so could cause underruns.
  5133. */
  5134. }
  5135. static void ironlake_init_clock_gating(struct drm_device *dev)
  5136. {
  5137. struct drm_i915_private *dev_priv = dev->dev_private;
  5138. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  5139. /*
  5140. * Required for FBC
  5141. * WaFbcDisableDpfcClockGating:ilk
  5142. */
  5143. dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
  5144. ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
  5145. ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
  5146. I915_WRITE(PCH_3DCGDIS0,
  5147. MARIUNIT_CLOCK_GATE_DISABLE |
  5148. SVSMUNIT_CLOCK_GATE_DISABLE);
  5149. I915_WRITE(PCH_3DCGDIS1,
  5150. VFMUNIT_CLOCK_GATE_DISABLE);
  5151. /*
  5152. * According to the spec the following bits should be set in
  5153. * order to enable memory self-refresh
  5154. * The bit 22/21 of 0x42004
  5155. * The bit 5 of 0x42020
  5156. * The bit 15 of 0x45000
  5157. */
  5158. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5159. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  5160. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  5161. dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
  5162. I915_WRITE(DISP_ARB_CTL,
  5163. (I915_READ(DISP_ARB_CTL) |
  5164. DISP_FBC_WM_DIS));
  5165. ilk_init_lp_watermarks(dev);
  5166. /*
  5167. * Based on the document from hardware guys the following bits
  5168. * should be set unconditionally in order to enable FBC.
  5169. * The bit 22 of 0x42000
  5170. * The bit 22 of 0x42004
  5171. * The bit 7,8,9 of 0x42020.
  5172. */
  5173. if (IS_IRONLAKE_M(dev)) {
  5174. /* WaFbcAsynchFlipDisableFbcQueue:ilk */
  5175. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5176. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5177. ILK_FBCQ_DIS);
  5178. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5179. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5180. ILK_DPARB_GATE);
  5181. }
  5182. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  5183. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5184. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5185. ILK_ELPIN_409_SELECT);
  5186. I915_WRITE(_3D_CHICKEN2,
  5187. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  5188. _3D_CHICKEN2_WM_READ_PIPELINED);
  5189. /* WaDisableRenderCachePipelinedFlush:ilk */
  5190. I915_WRITE(CACHE_MODE_0,
  5191. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  5192. /* WaDisable_RenderCache_OperationalFlush:ilk */
  5193. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5194. g4x_disable_trickle_feed(dev);
  5195. ibx_init_clock_gating(dev);
  5196. }
  5197. static void cpt_init_clock_gating(struct drm_device *dev)
  5198. {
  5199. struct drm_i915_private *dev_priv = dev->dev_private;
  5200. int pipe;
  5201. uint32_t val;
  5202. /*
  5203. * On Ibex Peak and Cougar Point, we need to disable clock
  5204. * gating for the panel power sequencer or it will fail to
  5205. * start up when no ports are active.
  5206. */
  5207. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
  5208. PCH_DPLUNIT_CLOCK_GATE_DISABLE |
  5209. PCH_CPUNIT_CLOCK_GATE_DISABLE);
  5210. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  5211. DPLS_EDP_PPS_FIX_DIS);
  5212. /* The below fixes the weird display corruption, a few pixels shifted
  5213. * downward, on (only) LVDS of some HP laptops with IVY.
  5214. */
  5215. for_each_pipe(dev_priv, pipe) {
  5216. val = I915_READ(TRANS_CHICKEN2(pipe));
  5217. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  5218. val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  5219. if (dev_priv->vbt.fdi_rx_polarity_inverted)
  5220. val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  5221. val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
  5222. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
  5223. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
  5224. I915_WRITE(TRANS_CHICKEN2(pipe), val);
  5225. }
  5226. /* WADP0ClockGatingDisable */
  5227. for_each_pipe(dev_priv, pipe) {
  5228. I915_WRITE(TRANS_CHICKEN1(pipe),
  5229. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  5230. }
  5231. }
  5232. static void gen6_check_mch_setup(struct drm_device *dev)
  5233. {
  5234. struct drm_i915_private *dev_priv = dev->dev_private;
  5235. uint32_t tmp;
  5236. tmp = I915_READ(MCH_SSKPD);
  5237. if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
  5238. DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
  5239. tmp);
  5240. }
  5241. static void gen6_init_clock_gating(struct drm_device *dev)
  5242. {
  5243. struct drm_i915_private *dev_priv = dev->dev_private;
  5244. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  5245. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  5246. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5247. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5248. ILK_ELPIN_409_SELECT);
  5249. /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
  5250. I915_WRITE(_3D_CHICKEN,
  5251. _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
  5252. /* WaSetupGtModeTdRowDispatch:snb */
  5253. if (IS_SNB_GT1(dev))
  5254. I915_WRITE(GEN6_GT_MODE,
  5255. _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
  5256. /* WaDisable_RenderCache_OperationalFlush:snb */
  5257. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5258. /*
  5259. * BSpec recoomends 8x4 when MSAA is used,
  5260. * however in practice 16x4 seems fastest.
  5261. *
  5262. * Note that PS/WM thread counts depend on the WIZ hashing
  5263. * disable bit, which we don't touch here, but it's good
  5264. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  5265. */
  5266. I915_WRITE(GEN6_GT_MODE,
  5267. GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
  5268. ilk_init_lp_watermarks(dev);
  5269. I915_WRITE(CACHE_MODE_0,
  5270. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  5271. I915_WRITE(GEN6_UCGCTL1,
  5272. I915_READ(GEN6_UCGCTL1) |
  5273. GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
  5274. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  5275. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  5276. * gating disable must be set. Failure to set it results in
  5277. * flickering pixels due to Z write ordering failures after
  5278. * some amount of runtime in the Mesa "fire" demo, and Unigine
  5279. * Sanctuary and Tropics, and apparently anything else with
  5280. * alpha test or pixel discard.
  5281. *
  5282. * According to the spec, bit 11 (RCCUNIT) must also be set,
  5283. * but we didn't debug actual testcases to find it out.
  5284. *
  5285. * WaDisableRCCUnitClockGating:snb
  5286. * WaDisableRCPBUnitClockGating:snb
  5287. */
  5288. I915_WRITE(GEN6_UCGCTL2,
  5289. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  5290. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  5291. /* WaStripsFansDisableFastClipPerformanceFix:snb */
  5292. I915_WRITE(_3D_CHICKEN3,
  5293. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
  5294. /*
  5295. * Bspec says:
  5296. * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
  5297. * 3DSTATE_SF number of SF output attributes is more than 16."
  5298. */
  5299. I915_WRITE(_3D_CHICKEN3,
  5300. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
  5301. /*
  5302. * According to the spec the following bits should be
  5303. * set in order to enable memory self-refresh and fbc:
  5304. * The bit21 and bit22 of 0x42000
  5305. * The bit21 and bit22 of 0x42004
  5306. * The bit5 and bit7 of 0x42020
  5307. * The bit14 of 0x70180
  5308. * The bit14 of 0x71180
  5309. *
  5310. * WaFbcAsynchFlipDisableFbcQueue:snb
  5311. */
  5312. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5313. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5314. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  5315. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5316. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5317. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  5318. I915_WRITE(ILK_DSPCLK_GATE_D,
  5319. I915_READ(ILK_DSPCLK_GATE_D) |
  5320. ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
  5321. ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
  5322. g4x_disable_trickle_feed(dev);
  5323. cpt_init_clock_gating(dev);
  5324. gen6_check_mch_setup(dev);
  5325. }
  5326. static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
  5327. {
  5328. uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
  5329. /*
  5330. * WaVSThreadDispatchOverride:ivb,vlv
  5331. *
  5332. * This actually overrides the dispatch
  5333. * mode for all thread types.
  5334. */
  5335. reg &= ~GEN7_FF_SCHED_MASK;
  5336. reg |= GEN7_FF_TS_SCHED_HW;
  5337. reg |= GEN7_FF_VS_SCHED_HW;
  5338. reg |= GEN7_FF_DS_SCHED_HW;
  5339. I915_WRITE(GEN7_FF_THREAD_MODE, reg);
  5340. }
  5341. static void lpt_init_clock_gating(struct drm_device *dev)
  5342. {
  5343. struct drm_i915_private *dev_priv = dev->dev_private;
  5344. /*
  5345. * TODO: this bit should only be enabled when really needed, then
  5346. * disabled when not needed anymore in order to save power.
  5347. */
  5348. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
  5349. I915_WRITE(SOUTH_DSPCLK_GATE_D,
  5350. I915_READ(SOUTH_DSPCLK_GATE_D) |
  5351. PCH_LP_PARTITION_LEVEL_DISABLE);
  5352. /* WADPOClockGatingDisable:hsw */
  5353. I915_WRITE(_TRANSA_CHICKEN1,
  5354. I915_READ(_TRANSA_CHICKEN1) |
  5355. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  5356. }
  5357. static void lpt_suspend_hw(struct drm_device *dev)
  5358. {
  5359. struct drm_i915_private *dev_priv = dev->dev_private;
  5360. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  5361. uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
  5362. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  5363. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  5364. }
  5365. }
  5366. static void broadwell_init_clock_gating(struct drm_device *dev)
  5367. {
  5368. struct drm_i915_private *dev_priv = dev->dev_private;
  5369. enum pipe pipe;
  5370. I915_WRITE(WM3_LP_ILK, 0);
  5371. I915_WRITE(WM2_LP_ILK, 0);
  5372. I915_WRITE(WM1_LP_ILK, 0);
  5373. /* WaSwitchSolVfFArbitrationPriority:bdw */
  5374. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  5375. /* WaPsrDPAMaskVBlankInSRD:bdw */
  5376. I915_WRITE(CHICKEN_PAR1_1,
  5377. I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
  5378. /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
  5379. for_each_pipe(dev_priv, pipe) {
  5380. I915_WRITE(CHICKEN_PIPESL_1(pipe),
  5381. I915_READ(CHICKEN_PIPESL_1(pipe)) |
  5382. BDW_DPRS_MASK_VBLANK_SRD);
  5383. }
  5384. /* WaVSRefCountFullforceMissDisable:bdw */
  5385. /* WaDSRefCountFullforceMissDisable:bdw */
  5386. I915_WRITE(GEN7_FF_THREAD_MODE,
  5387. I915_READ(GEN7_FF_THREAD_MODE) &
  5388. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  5389. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  5390. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  5391. /* WaDisableSDEUnitClockGating:bdw */
  5392. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  5393. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  5394. lpt_init_clock_gating(dev);
  5395. }
  5396. static void haswell_init_clock_gating(struct drm_device *dev)
  5397. {
  5398. struct drm_i915_private *dev_priv = dev->dev_private;
  5399. ilk_init_lp_watermarks(dev);
  5400. /* L3 caching of data atomics doesn't work -- disable it. */
  5401. I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
  5402. I915_WRITE(HSW_ROW_CHICKEN3,
  5403. _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
  5404. /* This is required by WaCatErrorRejectionIssue:hsw */
  5405. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  5406. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  5407. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  5408. /* WaVSRefCountFullforceMissDisable:hsw */
  5409. I915_WRITE(GEN7_FF_THREAD_MODE,
  5410. I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
  5411. /* WaDisable_RenderCache_OperationalFlush:hsw */
  5412. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5413. /* enable HiZ Raw Stall Optimization */
  5414. I915_WRITE(CACHE_MODE_0_GEN7,
  5415. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  5416. /* WaDisable4x2SubspanOptimization:hsw */
  5417. I915_WRITE(CACHE_MODE_1,
  5418. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  5419. /*
  5420. * BSpec recommends 8x4 when MSAA is used,
  5421. * however in practice 16x4 seems fastest.
  5422. *
  5423. * Note that PS/WM thread counts depend on the WIZ hashing
  5424. * disable bit, which we don't touch here, but it's good
  5425. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  5426. */
  5427. I915_WRITE(GEN7_GT_MODE,
  5428. GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
  5429. /* WaSwitchSolVfFArbitrationPriority:hsw */
  5430. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  5431. /* WaRsPkgCStateDisplayPMReq:hsw */
  5432. I915_WRITE(CHICKEN_PAR1_1,
  5433. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  5434. lpt_init_clock_gating(dev);
  5435. }
  5436. static void ivybridge_init_clock_gating(struct drm_device *dev)
  5437. {
  5438. struct drm_i915_private *dev_priv = dev->dev_private;
  5439. uint32_t snpcr;
  5440. ilk_init_lp_watermarks(dev);
  5441. I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
  5442. /* WaDisableEarlyCull:ivb */
  5443. I915_WRITE(_3D_CHICKEN3,
  5444. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  5445. /* WaDisableBackToBackFlipFix:ivb */
  5446. I915_WRITE(IVB_CHICKEN3,
  5447. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  5448. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  5449. /* WaDisablePSDDualDispatchEnable:ivb */
  5450. if (IS_IVB_GT1(dev))
  5451. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  5452. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  5453. /* WaDisable_RenderCache_OperationalFlush:ivb */
  5454. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5455. /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
  5456. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  5457. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  5458. /* WaApplyL3ControlAndL3ChickenMode:ivb */
  5459. I915_WRITE(GEN7_L3CNTLREG1,
  5460. GEN7_WA_FOR_GEN7_L3_CONTROL);
  5461. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  5462. GEN7_WA_L3_CHICKEN_MODE);
  5463. if (IS_IVB_GT1(dev))
  5464. I915_WRITE(GEN7_ROW_CHICKEN2,
  5465. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  5466. else {
  5467. /* must write both registers */
  5468. I915_WRITE(GEN7_ROW_CHICKEN2,
  5469. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  5470. I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
  5471. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  5472. }
  5473. /* WaForceL3Serialization:ivb */
  5474. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  5475. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  5476. /*
  5477. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  5478. * This implements the WaDisableRCZUnitClockGating:ivb workaround.
  5479. */
  5480. I915_WRITE(GEN6_UCGCTL2,
  5481. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  5482. /* This is required by WaCatErrorRejectionIssue:ivb */
  5483. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  5484. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  5485. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  5486. g4x_disable_trickle_feed(dev);
  5487. gen7_setup_fixed_func_scheduler(dev_priv);
  5488. if (0) { /* causes HiZ corruption on ivb:gt1 */
  5489. /* enable HiZ Raw Stall Optimization */
  5490. I915_WRITE(CACHE_MODE_0_GEN7,
  5491. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  5492. }
  5493. /* WaDisable4x2SubspanOptimization:ivb */
  5494. I915_WRITE(CACHE_MODE_1,
  5495. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  5496. /*
  5497. * BSpec recommends 8x4 when MSAA is used,
  5498. * however in practice 16x4 seems fastest.
  5499. *
  5500. * Note that PS/WM thread counts depend on the WIZ hashing
  5501. * disable bit, which we don't touch here, but it's good
  5502. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  5503. */
  5504. I915_WRITE(GEN7_GT_MODE,
  5505. GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
  5506. snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
  5507. snpcr &= ~GEN6_MBC_SNPCR_MASK;
  5508. snpcr |= GEN6_MBC_SNPCR_MED;
  5509. I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
  5510. if (!HAS_PCH_NOP(dev))
  5511. cpt_init_clock_gating(dev);
  5512. gen6_check_mch_setup(dev);
  5513. }
  5514. static void valleyview_init_clock_gating(struct drm_device *dev)
  5515. {
  5516. struct drm_i915_private *dev_priv = dev->dev_private;
  5517. I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
  5518. /* WaDisableEarlyCull:vlv */
  5519. I915_WRITE(_3D_CHICKEN3,
  5520. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  5521. /* WaDisableBackToBackFlipFix:vlv */
  5522. I915_WRITE(IVB_CHICKEN3,
  5523. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  5524. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  5525. /* WaPsdDispatchEnable:vlv */
  5526. /* WaDisablePSDDualDispatchEnable:vlv */
  5527. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  5528. _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
  5529. GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  5530. /* WaDisable_RenderCache_OperationalFlush:vlv */
  5531. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5532. /* WaForceL3Serialization:vlv */
  5533. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  5534. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  5535. /* WaDisableDopClockGating:vlv */
  5536. I915_WRITE(GEN7_ROW_CHICKEN2,
  5537. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  5538. /* This is required by WaCatErrorRejectionIssue:vlv */
  5539. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  5540. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  5541. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  5542. gen7_setup_fixed_func_scheduler(dev_priv);
  5543. /*
  5544. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  5545. * This implements the WaDisableRCZUnitClockGating:vlv workaround.
  5546. */
  5547. I915_WRITE(GEN6_UCGCTL2,
  5548. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  5549. /* WaDisableL3Bank2xClockGate:vlv
  5550. * Disabling L3 clock gating- MMIO 940c[25] = 1
  5551. * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
  5552. I915_WRITE(GEN7_UCGCTL4,
  5553. I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
  5554. I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
  5555. /*
  5556. * BSpec says this must be set, even though
  5557. * WaDisable4x2SubspanOptimization isn't listed for VLV.
  5558. */
  5559. I915_WRITE(CACHE_MODE_1,
  5560. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  5561. /*
  5562. * WaIncreaseL3CreditsForVLVB0:vlv
  5563. * This is the hardware default actually.
  5564. */
  5565. I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
  5566. /*
  5567. * WaDisableVLVClockGating_VBIIssue:vlv
  5568. * Disable clock gating on th GCFG unit to prevent a delay
  5569. * in the reporting of vblank events.
  5570. */
  5571. I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
  5572. }
  5573. static void cherryview_init_clock_gating(struct drm_device *dev)
  5574. {
  5575. struct drm_i915_private *dev_priv = dev->dev_private;
  5576. I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
  5577. I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
  5578. /* WaVSRefCountFullforceMissDisable:chv */
  5579. /* WaDSRefCountFullforceMissDisable:chv */
  5580. I915_WRITE(GEN7_FF_THREAD_MODE,
  5581. I915_READ(GEN7_FF_THREAD_MODE) &
  5582. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  5583. /* WaDisableSemaphoreAndSyncFlipWait:chv */
  5584. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  5585. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  5586. /* WaDisableCSUnitClockGating:chv */
  5587. I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
  5588. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  5589. /* WaDisableSDEUnitClockGating:chv */
  5590. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  5591. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  5592. /* WaDisableGunitClockGating:chv (pre-production hw) */
  5593. I915_WRITE(VLV_GUNIT_CLOCK_GATE, I915_READ(VLV_GUNIT_CLOCK_GATE) |
  5594. GINT_DIS);
  5595. /* WaDisableFfDopClockGating:chv (pre-production hw) */
  5596. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  5597. _MASKED_BIT_ENABLE(GEN8_FF_DOP_CLOCK_GATE_DISABLE));
  5598. /* WaDisableDopClockGating:chv (pre-production hw) */
  5599. I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
  5600. GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
  5601. }
  5602. static void g4x_init_clock_gating(struct drm_device *dev)
  5603. {
  5604. struct drm_i915_private *dev_priv = dev->dev_private;
  5605. uint32_t dspclk_gate;
  5606. I915_WRITE(RENCLK_GATE_D1, 0);
  5607. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  5608. GS_UNIT_CLOCK_GATE_DISABLE |
  5609. CL_UNIT_CLOCK_GATE_DISABLE);
  5610. I915_WRITE(RAMCLK_GATE_D, 0);
  5611. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  5612. OVRUNIT_CLOCK_GATE_DISABLE |
  5613. OVCUNIT_CLOCK_GATE_DISABLE;
  5614. if (IS_GM45(dev))
  5615. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  5616. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  5617. /* WaDisableRenderCachePipelinedFlush */
  5618. I915_WRITE(CACHE_MODE_0,
  5619. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  5620. /* WaDisable_RenderCache_OperationalFlush:g4x */
  5621. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5622. g4x_disable_trickle_feed(dev);
  5623. }
  5624. static void crestline_init_clock_gating(struct drm_device *dev)
  5625. {
  5626. struct drm_i915_private *dev_priv = dev->dev_private;
  5627. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  5628. I915_WRITE(RENCLK_GATE_D2, 0);
  5629. I915_WRITE(DSPCLK_GATE_D, 0);
  5630. I915_WRITE(RAMCLK_GATE_D, 0);
  5631. I915_WRITE16(DEUC, 0);
  5632. I915_WRITE(MI_ARB_STATE,
  5633. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  5634. /* WaDisable_RenderCache_OperationalFlush:gen4 */
  5635. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5636. }
  5637. static void broadwater_init_clock_gating(struct drm_device *dev)
  5638. {
  5639. struct drm_i915_private *dev_priv = dev->dev_private;
  5640. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  5641. I965_RCC_CLOCK_GATE_DISABLE |
  5642. I965_RCPB_CLOCK_GATE_DISABLE |
  5643. I965_ISC_CLOCK_GATE_DISABLE |
  5644. I965_FBC_CLOCK_GATE_DISABLE);
  5645. I915_WRITE(RENCLK_GATE_D2, 0);
  5646. I915_WRITE(MI_ARB_STATE,
  5647. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  5648. /* WaDisable_RenderCache_OperationalFlush:gen4 */
  5649. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5650. }
  5651. static void gen3_init_clock_gating(struct drm_device *dev)
  5652. {
  5653. struct drm_i915_private *dev_priv = dev->dev_private;
  5654. u32 dstate = I915_READ(D_STATE);
  5655. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  5656. DSTATE_DOT_CLOCK_GATING;
  5657. I915_WRITE(D_STATE, dstate);
  5658. if (IS_PINEVIEW(dev))
  5659. I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
  5660. /* IIR "flip pending" means done if this bit is set */
  5661. I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
  5662. /* interrupts should cause a wake up from C3 */
  5663. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
  5664. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  5665. I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  5666. I915_WRITE(MI_ARB_STATE,
  5667. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  5668. }
  5669. static void i85x_init_clock_gating(struct drm_device *dev)
  5670. {
  5671. struct drm_i915_private *dev_priv = dev->dev_private;
  5672. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  5673. /* interrupts should cause a wake up from C3 */
  5674. I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
  5675. _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
  5676. I915_WRITE(MEM_MODE,
  5677. _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
  5678. }
  5679. static void i830_init_clock_gating(struct drm_device *dev)
  5680. {
  5681. struct drm_i915_private *dev_priv = dev->dev_private;
  5682. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  5683. I915_WRITE(MEM_MODE,
  5684. _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
  5685. _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
  5686. }
  5687. void intel_init_clock_gating(struct drm_device *dev)
  5688. {
  5689. struct drm_i915_private *dev_priv = dev->dev_private;
  5690. dev_priv->display.init_clock_gating(dev);
  5691. }
  5692. void intel_suspend_hw(struct drm_device *dev)
  5693. {
  5694. if (HAS_PCH_LPT(dev))
  5695. lpt_suspend_hw(dev);
  5696. }
  5697. static void intel_init_fbc(struct drm_i915_private *dev_priv)
  5698. {
  5699. if (!HAS_FBC(dev_priv)) {
  5700. dev_priv->fbc.enabled = false;
  5701. return;
  5702. }
  5703. if (INTEL_INFO(dev_priv)->gen >= 7) {
  5704. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  5705. dev_priv->display.enable_fbc = gen7_enable_fbc;
  5706. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  5707. } else if (INTEL_INFO(dev_priv)->gen >= 5) {
  5708. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  5709. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  5710. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  5711. } else if (IS_GM45(dev_priv)) {
  5712. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  5713. dev_priv->display.enable_fbc = g4x_enable_fbc;
  5714. dev_priv->display.disable_fbc = g4x_disable_fbc;
  5715. } else {
  5716. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  5717. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  5718. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  5719. /* This value was pulled out of someone's hat */
  5720. I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
  5721. }
  5722. dev_priv->fbc.enabled = dev_priv->display.fbc_enabled(dev_priv->dev);
  5723. }
  5724. /* Set up chip specific power management-related functions */
  5725. void intel_init_pm(struct drm_device *dev)
  5726. {
  5727. struct drm_i915_private *dev_priv = dev->dev_private;
  5728. intel_init_fbc(dev_priv);
  5729. /* For cxsr */
  5730. if (IS_PINEVIEW(dev))
  5731. i915_pineview_get_mem_freq(dev);
  5732. else if (IS_GEN5(dev))
  5733. i915_ironlake_get_mem_freq(dev);
  5734. /* For FIFO watermark updates */
  5735. if (IS_GEN9(dev)) {
  5736. skl_setup_wm_latency(dev);
  5737. dev_priv->display.init_clock_gating = gen9_init_clock_gating;
  5738. dev_priv->display.update_wm = skl_update_wm;
  5739. dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
  5740. } else if (HAS_PCH_SPLIT(dev)) {
  5741. ilk_setup_wm_latency(dev);
  5742. if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
  5743. dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
  5744. (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
  5745. dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
  5746. dev_priv->display.update_wm = ilk_update_wm;
  5747. dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
  5748. } else {
  5749. DRM_DEBUG_KMS("Failed to read display plane latency. "
  5750. "Disable CxSR\n");
  5751. }
  5752. if (IS_GEN5(dev))
  5753. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  5754. else if (IS_GEN6(dev))
  5755. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  5756. else if (IS_IVYBRIDGE(dev))
  5757. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  5758. else if (IS_HASWELL(dev))
  5759. dev_priv->display.init_clock_gating = haswell_init_clock_gating;
  5760. else if (INTEL_INFO(dev)->gen == 8)
  5761. dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
  5762. } else if (IS_CHERRYVIEW(dev)) {
  5763. dev_priv->display.update_wm = cherryview_update_wm;
  5764. dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
  5765. dev_priv->display.init_clock_gating =
  5766. cherryview_init_clock_gating;
  5767. } else if (IS_VALLEYVIEW(dev)) {
  5768. dev_priv->display.update_wm = valleyview_update_wm;
  5769. dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
  5770. dev_priv->display.init_clock_gating =
  5771. valleyview_init_clock_gating;
  5772. } else if (IS_PINEVIEW(dev)) {
  5773. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  5774. dev_priv->is_ddr3,
  5775. dev_priv->fsb_freq,
  5776. dev_priv->mem_freq)) {
  5777. DRM_INFO("failed to find known CxSR latency "
  5778. "(found ddr%s fsb freq %d, mem freq %d), "
  5779. "disabling CxSR\n",
  5780. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  5781. dev_priv->fsb_freq, dev_priv->mem_freq);
  5782. /* Disable CxSR and never update its watermark again */
  5783. intel_set_memory_cxsr(dev_priv, false);
  5784. dev_priv->display.update_wm = NULL;
  5785. } else
  5786. dev_priv->display.update_wm = pineview_update_wm;
  5787. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  5788. } else if (IS_G4X(dev)) {
  5789. dev_priv->display.update_wm = g4x_update_wm;
  5790. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  5791. } else if (IS_GEN4(dev)) {
  5792. dev_priv->display.update_wm = i965_update_wm;
  5793. if (IS_CRESTLINE(dev))
  5794. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  5795. else if (IS_BROADWATER(dev))
  5796. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  5797. } else if (IS_GEN3(dev)) {
  5798. dev_priv->display.update_wm = i9xx_update_wm;
  5799. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  5800. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  5801. } else if (IS_GEN2(dev)) {
  5802. if (INTEL_INFO(dev)->num_pipes == 1) {
  5803. dev_priv->display.update_wm = i845_update_wm;
  5804. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  5805. } else {
  5806. dev_priv->display.update_wm = i9xx_update_wm;
  5807. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  5808. }
  5809. if (IS_I85X(dev) || IS_I865G(dev))
  5810. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  5811. else
  5812. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  5813. } else {
  5814. DRM_ERROR("unexpected fall-through in intel_init_pm\n");
  5815. }
  5816. }
  5817. int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
  5818. {
  5819. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  5820. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  5821. DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
  5822. return -EAGAIN;
  5823. }
  5824. I915_WRITE(GEN6_PCODE_DATA, *val);
  5825. if (INTEL_INFO(dev_priv)->gen >= 9)
  5826. I915_WRITE(GEN9_PCODE_DATA1, 0);
  5827. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  5828. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5829. 500)) {
  5830. DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
  5831. return -ETIMEDOUT;
  5832. }
  5833. *val = I915_READ(GEN6_PCODE_DATA);
  5834. I915_WRITE(GEN6_PCODE_DATA, 0);
  5835. return 0;
  5836. }
  5837. int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
  5838. {
  5839. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  5840. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  5841. DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
  5842. return -EAGAIN;
  5843. }
  5844. I915_WRITE(GEN6_PCODE_DATA, val);
  5845. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  5846. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5847. 500)) {
  5848. DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
  5849. return -ETIMEDOUT;
  5850. }
  5851. I915_WRITE(GEN6_PCODE_DATA, 0);
  5852. return 0;
  5853. }
  5854. static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
  5855. {
  5856. int div;
  5857. /* 4 x czclk */
  5858. switch (dev_priv->mem_freq) {
  5859. case 800:
  5860. div = 10;
  5861. break;
  5862. case 1066:
  5863. div = 12;
  5864. break;
  5865. case 1333:
  5866. div = 16;
  5867. break;
  5868. default:
  5869. return -1;
  5870. }
  5871. return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
  5872. }
  5873. static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
  5874. {
  5875. int mul;
  5876. /* 4 x czclk */
  5877. switch (dev_priv->mem_freq) {
  5878. case 800:
  5879. mul = 10;
  5880. break;
  5881. case 1066:
  5882. mul = 12;
  5883. break;
  5884. case 1333:
  5885. mul = 16;
  5886. break;
  5887. default:
  5888. return -1;
  5889. }
  5890. return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
  5891. }
  5892. static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
  5893. {
  5894. int div, freq;
  5895. switch (dev_priv->rps.cz_freq) {
  5896. case 200:
  5897. div = 5;
  5898. break;
  5899. case 267:
  5900. div = 6;
  5901. break;
  5902. case 320:
  5903. case 333:
  5904. case 400:
  5905. div = 8;
  5906. break;
  5907. default:
  5908. return -1;
  5909. }
  5910. freq = (DIV_ROUND_CLOSEST((dev_priv->rps.cz_freq * val), 2 * div) / 2);
  5911. return freq;
  5912. }
  5913. static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
  5914. {
  5915. int mul, opcode;
  5916. switch (dev_priv->rps.cz_freq) {
  5917. case 200:
  5918. mul = 5;
  5919. break;
  5920. case 267:
  5921. mul = 6;
  5922. break;
  5923. case 320:
  5924. case 333:
  5925. case 400:
  5926. mul = 8;
  5927. break;
  5928. default:
  5929. return -1;
  5930. }
  5931. /* CHV needs even values */
  5932. opcode = (DIV_ROUND_CLOSEST((val * 2 * mul), dev_priv->rps.cz_freq) * 2);
  5933. return opcode;
  5934. }
  5935. int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
  5936. {
  5937. int ret = -1;
  5938. if (IS_CHERRYVIEW(dev_priv->dev))
  5939. ret = chv_gpu_freq(dev_priv, val);
  5940. else if (IS_VALLEYVIEW(dev_priv->dev))
  5941. ret = byt_gpu_freq(dev_priv, val);
  5942. return ret;
  5943. }
  5944. int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
  5945. {
  5946. int ret = -1;
  5947. if (IS_CHERRYVIEW(dev_priv->dev))
  5948. ret = chv_freq_opcode(dev_priv, val);
  5949. else if (IS_VALLEYVIEW(dev_priv->dev))
  5950. ret = byt_freq_opcode(dev_priv, val);
  5951. return ret;
  5952. }
  5953. void intel_pm_setup(struct drm_device *dev)
  5954. {
  5955. struct drm_i915_private *dev_priv = dev->dev_private;
  5956. mutex_init(&dev_priv->rps.hw_lock);
  5957. INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
  5958. intel_gen6_powersave_work);
  5959. dev_priv->pm.suspended = false;
  5960. }