intel_pm.c 201 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220
  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;
  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. if (entry->end)
  2598. entry->end += 1;
  2599. }
  2600. void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
  2601. struct skl_ddb_allocation *ddb /* out */)
  2602. {
  2603. struct drm_device *dev = dev_priv->dev;
  2604. enum pipe pipe;
  2605. int plane;
  2606. u32 val;
  2607. for_each_pipe(dev_priv, pipe) {
  2608. for_each_plane(pipe, plane) {
  2609. val = I915_READ(PLANE_BUF_CFG(pipe, plane));
  2610. skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
  2611. val);
  2612. }
  2613. val = I915_READ(CUR_BUF_CFG(pipe));
  2614. skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
  2615. }
  2616. }
  2617. static unsigned int
  2618. skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
  2619. {
  2620. return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
  2621. }
  2622. /*
  2623. * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
  2624. * a 8192x4096@32bpp framebuffer:
  2625. * 3 * 4096 * 8192 * 4 < 2^32
  2626. */
  2627. static unsigned int
  2628. skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
  2629. const struct skl_pipe_wm_parameters *params)
  2630. {
  2631. unsigned int total_data_rate = 0;
  2632. int plane;
  2633. for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
  2634. const struct intel_plane_wm_parameters *p;
  2635. p = &params->plane[plane];
  2636. if (!p->enabled)
  2637. continue;
  2638. total_data_rate += skl_plane_relative_data_rate(p);
  2639. }
  2640. return total_data_rate;
  2641. }
  2642. static void
  2643. skl_allocate_pipe_ddb(struct drm_crtc *crtc,
  2644. const struct intel_wm_config *config,
  2645. const struct skl_pipe_wm_parameters *params,
  2646. struct skl_ddb_allocation *ddb /* out */)
  2647. {
  2648. struct drm_device *dev = crtc->dev;
  2649. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2650. enum pipe pipe = intel_crtc->pipe;
  2651. struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
  2652. uint16_t alloc_size, start, cursor_blocks;
  2653. unsigned int total_data_rate;
  2654. int plane;
  2655. skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
  2656. alloc_size = skl_ddb_entry_size(alloc);
  2657. if (alloc_size == 0) {
  2658. memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
  2659. memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
  2660. return;
  2661. }
  2662. cursor_blocks = skl_cursor_allocation(config);
  2663. ddb->cursor[pipe].start = alloc->end - cursor_blocks;
  2664. ddb->cursor[pipe].end = alloc->end;
  2665. alloc_size -= cursor_blocks;
  2666. alloc->end -= cursor_blocks;
  2667. /*
  2668. * Each active plane get a portion of the remaining space, in
  2669. * proportion to the amount of data they need to fetch from memory.
  2670. *
  2671. * FIXME: we may not allocate every single block here.
  2672. */
  2673. total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
  2674. start = alloc->start;
  2675. for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
  2676. const struct intel_plane_wm_parameters *p;
  2677. unsigned int data_rate;
  2678. uint16_t plane_blocks;
  2679. p = &params->plane[plane];
  2680. if (!p->enabled)
  2681. continue;
  2682. data_rate = skl_plane_relative_data_rate(p);
  2683. /*
  2684. * promote the expression to 64 bits to avoid overflowing, the
  2685. * result is < available as data_rate / total_data_rate < 1
  2686. */
  2687. plane_blocks = div_u64((uint64_t)alloc_size * data_rate,
  2688. total_data_rate);
  2689. ddb->plane[pipe][plane].start = start;
  2690. ddb->plane[pipe][plane].end = start + plane_blocks;
  2691. start += plane_blocks;
  2692. }
  2693. }
  2694. static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_config *config)
  2695. {
  2696. /* TODO: Take into account the scalers once we support them */
  2697. return config->adjusted_mode.crtc_clock;
  2698. }
  2699. /*
  2700. * The max latency should be 257 (max the punit can code is 255 and we add 2us
  2701. * for the read latency) and bytes_per_pixel should always be <= 8, so that
  2702. * should allow pixel_rate up to ~2 GHz which seems sufficient since max
  2703. * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
  2704. */
  2705. static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
  2706. uint32_t latency)
  2707. {
  2708. uint32_t wm_intermediate_val, ret;
  2709. if (latency == 0)
  2710. return UINT_MAX;
  2711. wm_intermediate_val = latency * pixel_rate * bytes_per_pixel;
  2712. ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
  2713. return ret;
  2714. }
  2715. static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
  2716. uint32_t horiz_pixels, uint8_t bytes_per_pixel,
  2717. uint32_t latency)
  2718. {
  2719. uint32_t ret, plane_bytes_per_line, wm_intermediate_val;
  2720. if (latency == 0)
  2721. return UINT_MAX;
  2722. plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
  2723. wm_intermediate_val = latency * pixel_rate;
  2724. ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
  2725. plane_bytes_per_line;
  2726. return ret;
  2727. }
  2728. static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
  2729. const struct intel_crtc *intel_crtc)
  2730. {
  2731. struct drm_device *dev = intel_crtc->base.dev;
  2732. struct drm_i915_private *dev_priv = dev->dev_private;
  2733. const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
  2734. enum pipe pipe = intel_crtc->pipe;
  2735. if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
  2736. sizeof(new_ddb->plane[pipe])))
  2737. return true;
  2738. if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
  2739. sizeof(new_ddb->cursor[pipe])))
  2740. return true;
  2741. return false;
  2742. }
  2743. static void skl_compute_wm_global_parameters(struct drm_device *dev,
  2744. struct intel_wm_config *config)
  2745. {
  2746. struct drm_crtc *crtc;
  2747. struct drm_plane *plane;
  2748. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  2749. config->num_pipes_active += intel_crtc_active(crtc);
  2750. /* FIXME: I don't think we need those two global parameters on SKL */
  2751. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  2752. struct intel_plane *intel_plane = to_intel_plane(plane);
  2753. config->sprites_enabled |= intel_plane->wm.enabled;
  2754. config->sprites_scaled |= intel_plane->wm.scaled;
  2755. }
  2756. }
  2757. static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
  2758. struct skl_pipe_wm_parameters *p)
  2759. {
  2760. struct drm_device *dev = crtc->dev;
  2761. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2762. enum pipe pipe = intel_crtc->pipe;
  2763. struct drm_plane *plane;
  2764. int i = 1; /* Index for sprite planes start */
  2765. p->active = intel_crtc_active(crtc);
  2766. if (p->active) {
  2767. p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
  2768. p->pixel_rate = skl_pipe_pixel_rate(&intel_crtc->config);
  2769. /*
  2770. * For now, assume primary and cursor planes are always enabled.
  2771. */
  2772. p->plane[0].enabled = true;
  2773. p->plane[0].bytes_per_pixel =
  2774. crtc->primary->fb->bits_per_pixel / 8;
  2775. p->plane[0].horiz_pixels = intel_crtc->config.pipe_src_w;
  2776. p->plane[0].vert_pixels = intel_crtc->config.pipe_src_h;
  2777. p->cursor.enabled = true;
  2778. p->cursor.bytes_per_pixel = 4;
  2779. p->cursor.horiz_pixels = intel_crtc->cursor_width ?
  2780. intel_crtc->cursor_width : 64;
  2781. }
  2782. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  2783. struct intel_plane *intel_plane = to_intel_plane(plane);
  2784. if (intel_plane->pipe == pipe)
  2785. p->plane[i++] = intel_plane->wm;
  2786. }
  2787. }
  2788. static bool skl_compute_plane_wm(struct skl_pipe_wm_parameters *p,
  2789. struct intel_plane_wm_parameters *p_params,
  2790. uint16_t ddb_allocation,
  2791. uint32_t mem_value,
  2792. uint16_t *out_blocks, /* out */
  2793. uint8_t *out_lines /* out */)
  2794. {
  2795. uint32_t method1, method2, plane_bytes_per_line, res_blocks, res_lines;
  2796. uint32_t result_bytes;
  2797. if (mem_value == 0 || !p->active || !p_params->enabled)
  2798. return false;
  2799. method1 = skl_wm_method1(p->pixel_rate,
  2800. p_params->bytes_per_pixel,
  2801. mem_value);
  2802. method2 = skl_wm_method2(p->pixel_rate,
  2803. p->pipe_htotal,
  2804. p_params->horiz_pixels,
  2805. p_params->bytes_per_pixel,
  2806. mem_value);
  2807. plane_bytes_per_line = p_params->horiz_pixels *
  2808. p_params->bytes_per_pixel;
  2809. /* For now xtile and linear */
  2810. if (((ddb_allocation * 512) / plane_bytes_per_line) >= 1)
  2811. result_bytes = min(method1, method2);
  2812. else
  2813. result_bytes = method1;
  2814. res_blocks = DIV_ROUND_UP(result_bytes, 512) + 1;
  2815. res_lines = DIV_ROUND_UP(result_bytes, plane_bytes_per_line);
  2816. if (res_blocks > ddb_allocation || res_lines > 31)
  2817. return false;
  2818. *out_blocks = res_blocks;
  2819. *out_lines = res_lines;
  2820. return true;
  2821. }
  2822. static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
  2823. struct skl_ddb_allocation *ddb,
  2824. struct skl_pipe_wm_parameters *p,
  2825. enum pipe pipe,
  2826. int level,
  2827. int num_planes,
  2828. struct skl_wm_level *result)
  2829. {
  2830. uint16_t latency = dev_priv->wm.skl_latency[level];
  2831. uint16_t ddb_blocks;
  2832. int i;
  2833. for (i = 0; i < num_planes; i++) {
  2834. ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
  2835. result->plane_en[i] = skl_compute_plane_wm(p, &p->plane[i],
  2836. ddb_blocks,
  2837. latency,
  2838. &result->plane_res_b[i],
  2839. &result->plane_res_l[i]);
  2840. }
  2841. ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
  2842. result->cursor_en = skl_compute_plane_wm(p, &p->cursor, ddb_blocks,
  2843. latency, &result->cursor_res_b,
  2844. &result->cursor_res_l);
  2845. }
  2846. static uint32_t
  2847. skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
  2848. {
  2849. if (!intel_crtc_active(crtc))
  2850. return 0;
  2851. return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
  2852. }
  2853. static void skl_compute_transition_wm(struct drm_crtc *crtc,
  2854. struct skl_pipe_wm_parameters *params,
  2855. struct skl_wm_level *trans_wm /* out */)
  2856. {
  2857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2858. int i;
  2859. if (!params->active)
  2860. return;
  2861. /* Until we know more, just disable transition WMs */
  2862. for (i = 0; i < intel_num_planes(intel_crtc); i++)
  2863. trans_wm->plane_en[i] = false;
  2864. trans_wm->cursor_en = false;
  2865. }
  2866. static void skl_compute_pipe_wm(struct drm_crtc *crtc,
  2867. struct skl_ddb_allocation *ddb,
  2868. struct skl_pipe_wm_parameters *params,
  2869. struct skl_pipe_wm *pipe_wm)
  2870. {
  2871. struct drm_device *dev = crtc->dev;
  2872. const struct drm_i915_private *dev_priv = dev->dev_private;
  2873. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2874. int level, max_level = ilk_wm_max_level(dev);
  2875. for (level = 0; level <= max_level; level++) {
  2876. skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
  2877. level, intel_num_planes(intel_crtc),
  2878. &pipe_wm->wm[level]);
  2879. }
  2880. pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
  2881. skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
  2882. }
  2883. static void skl_compute_wm_results(struct drm_device *dev,
  2884. struct skl_pipe_wm_parameters *p,
  2885. struct skl_pipe_wm *p_wm,
  2886. struct skl_wm_values *r,
  2887. struct intel_crtc *intel_crtc)
  2888. {
  2889. int level, max_level = ilk_wm_max_level(dev);
  2890. enum pipe pipe = intel_crtc->pipe;
  2891. uint32_t temp;
  2892. int i;
  2893. for (level = 0; level <= max_level; level++) {
  2894. for (i = 0; i < intel_num_planes(intel_crtc); i++) {
  2895. temp = 0;
  2896. temp |= p_wm->wm[level].plane_res_l[i] <<
  2897. PLANE_WM_LINES_SHIFT;
  2898. temp |= p_wm->wm[level].plane_res_b[i];
  2899. if (p_wm->wm[level].plane_en[i])
  2900. temp |= PLANE_WM_EN;
  2901. r->plane[pipe][i][level] = temp;
  2902. }
  2903. temp = 0;
  2904. temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
  2905. temp |= p_wm->wm[level].cursor_res_b;
  2906. if (p_wm->wm[level].cursor_en)
  2907. temp |= PLANE_WM_EN;
  2908. r->cursor[pipe][level] = temp;
  2909. }
  2910. /* transition WMs */
  2911. for (i = 0; i < intel_num_planes(intel_crtc); i++) {
  2912. temp = 0;
  2913. temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
  2914. temp |= p_wm->trans_wm.plane_res_b[i];
  2915. if (p_wm->trans_wm.plane_en[i])
  2916. temp |= PLANE_WM_EN;
  2917. r->plane_trans[pipe][i] = temp;
  2918. }
  2919. temp = 0;
  2920. temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
  2921. temp |= p_wm->trans_wm.cursor_res_b;
  2922. if (p_wm->trans_wm.cursor_en)
  2923. temp |= PLANE_WM_EN;
  2924. r->cursor_trans[pipe] = temp;
  2925. r->wm_linetime[pipe] = p_wm->linetime;
  2926. }
  2927. static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
  2928. const struct skl_ddb_entry *entry)
  2929. {
  2930. if (entry->end)
  2931. I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
  2932. else
  2933. I915_WRITE(reg, 0);
  2934. }
  2935. static void skl_write_wm_values(struct drm_i915_private *dev_priv,
  2936. const struct skl_wm_values *new)
  2937. {
  2938. struct drm_device *dev = dev_priv->dev;
  2939. struct intel_crtc *crtc;
  2940. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  2941. int i, level, max_level = ilk_wm_max_level(dev);
  2942. enum pipe pipe = crtc->pipe;
  2943. if (!new->dirty[pipe])
  2944. continue;
  2945. I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
  2946. for (level = 0; level <= max_level; level++) {
  2947. for (i = 0; i < intel_num_planes(crtc); i++)
  2948. I915_WRITE(PLANE_WM(pipe, i, level),
  2949. new->plane[pipe][i][level]);
  2950. I915_WRITE(CUR_WM(pipe, level),
  2951. new->cursor[pipe][level]);
  2952. }
  2953. for (i = 0; i < intel_num_planes(crtc); i++)
  2954. I915_WRITE(PLANE_WM_TRANS(pipe, i),
  2955. new->plane_trans[pipe][i]);
  2956. I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
  2957. for (i = 0; i < intel_num_planes(crtc); i++)
  2958. skl_ddb_entry_write(dev_priv,
  2959. PLANE_BUF_CFG(pipe, i),
  2960. &new->ddb.plane[pipe][i]);
  2961. skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
  2962. &new->ddb.cursor[pipe]);
  2963. }
  2964. }
  2965. static bool skl_update_pipe_wm(struct drm_crtc *crtc,
  2966. struct skl_pipe_wm_parameters *params,
  2967. struct intel_wm_config *config,
  2968. struct skl_ddb_allocation *ddb, /* out */
  2969. struct skl_pipe_wm *pipe_wm /* out */)
  2970. {
  2971. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2972. skl_compute_wm_pipe_parameters(crtc, params);
  2973. skl_allocate_pipe_ddb(crtc, config, params, ddb);
  2974. skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
  2975. if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
  2976. return false;
  2977. intel_crtc->wm.skl_active = *pipe_wm;
  2978. return true;
  2979. }
  2980. static void skl_update_other_pipe_wm(struct drm_device *dev,
  2981. struct drm_crtc *crtc,
  2982. struct intel_wm_config *config,
  2983. struct skl_wm_values *r)
  2984. {
  2985. struct intel_crtc *intel_crtc;
  2986. struct intel_crtc *this_crtc = to_intel_crtc(crtc);
  2987. /*
  2988. * If the WM update hasn't changed the allocation for this_crtc (the
  2989. * crtc we are currently computing the new WM values for), other
  2990. * enabled crtcs will keep the same allocation and we don't need to
  2991. * recompute anything for them.
  2992. */
  2993. if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
  2994. return;
  2995. /*
  2996. * Otherwise, because of this_crtc being freshly enabled/disabled, the
  2997. * other active pipes need new DDB allocation and WM values.
  2998. */
  2999. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  3000. base.head) {
  3001. struct skl_pipe_wm_parameters params = {};
  3002. struct skl_pipe_wm pipe_wm = {};
  3003. bool wm_changed;
  3004. if (this_crtc->pipe == intel_crtc->pipe)
  3005. continue;
  3006. if (!intel_crtc->active)
  3007. continue;
  3008. wm_changed = skl_update_pipe_wm(&intel_crtc->base,
  3009. &params, config,
  3010. &r->ddb, &pipe_wm);
  3011. /*
  3012. * If we end up re-computing the other pipe WM values, it's
  3013. * because it was really needed, so we expect the WM values to
  3014. * be different.
  3015. */
  3016. WARN_ON(!wm_changed);
  3017. skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
  3018. r->dirty[intel_crtc->pipe] = true;
  3019. }
  3020. }
  3021. static void skl_update_wm(struct drm_crtc *crtc)
  3022. {
  3023. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3024. struct drm_device *dev = crtc->dev;
  3025. struct drm_i915_private *dev_priv = dev->dev_private;
  3026. struct skl_pipe_wm_parameters params = {};
  3027. struct skl_wm_values *results = &dev_priv->wm.skl_results;
  3028. struct skl_pipe_wm pipe_wm = {};
  3029. struct intel_wm_config config = {};
  3030. memset(results, 0, sizeof(*results));
  3031. skl_compute_wm_global_parameters(dev, &config);
  3032. if (!skl_update_pipe_wm(crtc, &params, &config,
  3033. &results->ddb, &pipe_wm))
  3034. return;
  3035. skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
  3036. results->dirty[intel_crtc->pipe] = true;
  3037. skl_update_other_pipe_wm(dev, crtc, &config, results);
  3038. skl_write_wm_values(dev_priv, results);
  3039. /* store the new configuration */
  3040. dev_priv->wm.skl_hw = *results;
  3041. }
  3042. static void
  3043. skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
  3044. uint32_t sprite_width, uint32_t sprite_height,
  3045. int pixel_size, bool enabled, bool scaled)
  3046. {
  3047. struct intel_plane *intel_plane = to_intel_plane(plane);
  3048. intel_plane->wm.enabled = enabled;
  3049. intel_plane->wm.scaled = scaled;
  3050. intel_plane->wm.horiz_pixels = sprite_width;
  3051. intel_plane->wm.vert_pixels = sprite_height;
  3052. intel_plane->wm.bytes_per_pixel = pixel_size;
  3053. skl_update_wm(crtc);
  3054. }
  3055. static void ilk_update_wm(struct drm_crtc *crtc)
  3056. {
  3057. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3058. struct drm_device *dev = crtc->dev;
  3059. struct drm_i915_private *dev_priv = dev->dev_private;
  3060. struct ilk_wm_maximums max;
  3061. struct ilk_pipe_wm_parameters params = {};
  3062. struct ilk_wm_values results = {};
  3063. enum intel_ddb_partitioning partitioning;
  3064. struct intel_pipe_wm pipe_wm = {};
  3065. struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
  3066. struct intel_wm_config config = {};
  3067. ilk_compute_wm_parameters(crtc, &params);
  3068. intel_compute_pipe_wm(crtc, &params, &pipe_wm);
  3069. if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
  3070. return;
  3071. intel_crtc->wm.active = pipe_wm;
  3072. ilk_compute_wm_config(dev, &config);
  3073. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
  3074. ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
  3075. /* 5/6 split only in single pipe config on IVB+ */
  3076. if (INTEL_INFO(dev)->gen >= 7 &&
  3077. config.num_pipes_active == 1 && config.sprites_enabled) {
  3078. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
  3079. ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
  3080. best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
  3081. } else {
  3082. best_lp_wm = &lp_wm_1_2;
  3083. }
  3084. partitioning = (best_lp_wm == &lp_wm_1_2) ?
  3085. INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
  3086. ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
  3087. ilk_write_wm_values(dev_priv, &results);
  3088. }
  3089. static void
  3090. ilk_update_sprite_wm(struct drm_plane *plane,
  3091. struct drm_crtc *crtc,
  3092. uint32_t sprite_width, uint32_t sprite_height,
  3093. int pixel_size, bool enabled, bool scaled)
  3094. {
  3095. struct drm_device *dev = plane->dev;
  3096. struct intel_plane *intel_plane = to_intel_plane(plane);
  3097. intel_plane->wm.enabled = enabled;
  3098. intel_plane->wm.scaled = scaled;
  3099. intel_plane->wm.horiz_pixels = sprite_width;
  3100. intel_plane->wm.vert_pixels = sprite_width;
  3101. intel_plane->wm.bytes_per_pixel = pixel_size;
  3102. /*
  3103. * IVB workaround: must disable low power watermarks for at least
  3104. * one frame before enabling scaling. LP watermarks can be re-enabled
  3105. * when scaling is disabled.
  3106. *
  3107. * WaCxSRDisabledForSpriteScaling:ivb
  3108. */
  3109. if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
  3110. intel_wait_for_vblank(dev, intel_plane->pipe);
  3111. ilk_update_wm(crtc);
  3112. }
  3113. static void skl_pipe_wm_active_state(uint32_t val,
  3114. struct skl_pipe_wm *active,
  3115. bool is_transwm,
  3116. bool is_cursor,
  3117. int i,
  3118. int level)
  3119. {
  3120. bool is_enabled = (val & PLANE_WM_EN) != 0;
  3121. if (!is_transwm) {
  3122. if (!is_cursor) {
  3123. active->wm[level].plane_en[i] = is_enabled;
  3124. active->wm[level].plane_res_b[i] =
  3125. val & PLANE_WM_BLOCKS_MASK;
  3126. active->wm[level].plane_res_l[i] =
  3127. (val >> PLANE_WM_LINES_SHIFT) &
  3128. PLANE_WM_LINES_MASK;
  3129. } else {
  3130. active->wm[level].cursor_en = is_enabled;
  3131. active->wm[level].cursor_res_b =
  3132. val & PLANE_WM_BLOCKS_MASK;
  3133. active->wm[level].cursor_res_l =
  3134. (val >> PLANE_WM_LINES_SHIFT) &
  3135. PLANE_WM_LINES_MASK;
  3136. }
  3137. } else {
  3138. if (!is_cursor) {
  3139. active->trans_wm.plane_en[i] = is_enabled;
  3140. active->trans_wm.plane_res_b[i] =
  3141. val & PLANE_WM_BLOCKS_MASK;
  3142. active->trans_wm.plane_res_l[i] =
  3143. (val >> PLANE_WM_LINES_SHIFT) &
  3144. PLANE_WM_LINES_MASK;
  3145. } else {
  3146. active->trans_wm.cursor_en = is_enabled;
  3147. active->trans_wm.cursor_res_b =
  3148. val & PLANE_WM_BLOCKS_MASK;
  3149. active->trans_wm.cursor_res_l =
  3150. (val >> PLANE_WM_LINES_SHIFT) &
  3151. PLANE_WM_LINES_MASK;
  3152. }
  3153. }
  3154. }
  3155. static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
  3156. {
  3157. struct drm_device *dev = crtc->dev;
  3158. struct drm_i915_private *dev_priv = dev->dev_private;
  3159. struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
  3160. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3161. struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
  3162. enum pipe pipe = intel_crtc->pipe;
  3163. int level, i, max_level;
  3164. uint32_t temp;
  3165. max_level = ilk_wm_max_level(dev);
  3166. hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
  3167. for (level = 0; level <= max_level; level++) {
  3168. for (i = 0; i < intel_num_planes(intel_crtc); i++)
  3169. hw->plane[pipe][i][level] =
  3170. I915_READ(PLANE_WM(pipe, i, level));
  3171. hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
  3172. }
  3173. for (i = 0; i < intel_num_planes(intel_crtc); i++)
  3174. hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
  3175. hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
  3176. if (!intel_crtc_active(crtc))
  3177. return;
  3178. hw->dirty[pipe] = true;
  3179. active->linetime = hw->wm_linetime[pipe];
  3180. for (level = 0; level <= max_level; level++) {
  3181. for (i = 0; i < intel_num_planes(intel_crtc); i++) {
  3182. temp = hw->plane[pipe][i][level];
  3183. skl_pipe_wm_active_state(temp, active, false,
  3184. false, i, level);
  3185. }
  3186. temp = hw->cursor[pipe][level];
  3187. skl_pipe_wm_active_state(temp, active, false, true, i, level);
  3188. }
  3189. for (i = 0; i < intel_num_planes(intel_crtc); i++) {
  3190. temp = hw->plane_trans[pipe][i];
  3191. skl_pipe_wm_active_state(temp, active, true, false, i, 0);
  3192. }
  3193. temp = hw->cursor_trans[pipe];
  3194. skl_pipe_wm_active_state(temp, active, true, true, i, 0);
  3195. }
  3196. void skl_wm_get_hw_state(struct drm_device *dev)
  3197. {
  3198. struct drm_i915_private *dev_priv = dev->dev_private;
  3199. struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
  3200. struct drm_crtc *crtc;
  3201. skl_ddb_get_hw_state(dev_priv, ddb);
  3202. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  3203. skl_pipe_wm_get_hw_state(crtc);
  3204. }
  3205. static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
  3206. {
  3207. struct drm_device *dev = crtc->dev;
  3208. struct drm_i915_private *dev_priv = dev->dev_private;
  3209. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  3210. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3211. struct intel_pipe_wm *active = &intel_crtc->wm.active;
  3212. enum pipe pipe = intel_crtc->pipe;
  3213. static const unsigned int wm0_pipe_reg[] = {
  3214. [PIPE_A] = WM0_PIPEA_ILK,
  3215. [PIPE_B] = WM0_PIPEB_ILK,
  3216. [PIPE_C] = WM0_PIPEC_IVB,
  3217. };
  3218. hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
  3219. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  3220. hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
  3221. active->pipe_enabled = intel_crtc_active(crtc);
  3222. if (active->pipe_enabled) {
  3223. u32 tmp = hw->wm_pipe[pipe];
  3224. /*
  3225. * For active pipes LP0 watermark is marked as
  3226. * enabled, and LP1+ watermaks as disabled since
  3227. * we can't really reverse compute them in case
  3228. * multiple pipes are active.
  3229. */
  3230. active->wm[0].enable = true;
  3231. active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
  3232. active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
  3233. active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
  3234. active->linetime = hw->wm_linetime[pipe];
  3235. } else {
  3236. int level, max_level = ilk_wm_max_level(dev);
  3237. /*
  3238. * For inactive pipes, all watermark levels
  3239. * should be marked as enabled but zeroed,
  3240. * which is what we'd compute them to.
  3241. */
  3242. for (level = 0; level <= max_level; level++)
  3243. active->wm[level].enable = true;
  3244. }
  3245. }
  3246. void ilk_wm_get_hw_state(struct drm_device *dev)
  3247. {
  3248. struct drm_i915_private *dev_priv = dev->dev_private;
  3249. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  3250. struct drm_crtc *crtc;
  3251. for_each_crtc(dev, crtc)
  3252. ilk_pipe_wm_get_hw_state(crtc);
  3253. hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
  3254. hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
  3255. hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
  3256. hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
  3257. if (INTEL_INFO(dev)->gen >= 7) {
  3258. hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
  3259. hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
  3260. }
  3261. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  3262. hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
  3263. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  3264. else if (IS_IVYBRIDGE(dev))
  3265. hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
  3266. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  3267. hw->enable_fbc_wm =
  3268. !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
  3269. }
  3270. /**
  3271. * intel_update_watermarks - update FIFO watermark values based on current modes
  3272. *
  3273. * Calculate watermark values for the various WM regs based on current mode
  3274. * and plane configuration.
  3275. *
  3276. * There are several cases to deal with here:
  3277. * - normal (i.e. non-self-refresh)
  3278. * - self-refresh (SR) mode
  3279. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3280. * - lines are small relative to FIFO size (buffer can hold more than 2
  3281. * lines), so need to account for TLB latency
  3282. *
  3283. * The normal calculation is:
  3284. * watermark = dotclock * bytes per pixel * latency
  3285. * where latency is platform & configuration dependent (we assume pessimal
  3286. * values here).
  3287. *
  3288. * The SR calculation is:
  3289. * watermark = (trunc(latency/line time)+1) * surface width *
  3290. * bytes per pixel
  3291. * where
  3292. * line time = htotal / dotclock
  3293. * surface width = hdisplay for normal plane and 64 for cursor
  3294. * and latency is assumed to be high, as above.
  3295. *
  3296. * The final value programmed to the register should always be rounded up,
  3297. * and include an extra 2 entries to account for clock crossings.
  3298. *
  3299. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3300. * to set the non-SR watermarks to 8.
  3301. */
  3302. void intel_update_watermarks(struct drm_crtc *crtc)
  3303. {
  3304. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  3305. if (dev_priv->display.update_wm)
  3306. dev_priv->display.update_wm(crtc);
  3307. }
  3308. void intel_update_sprite_watermarks(struct drm_plane *plane,
  3309. struct drm_crtc *crtc,
  3310. uint32_t sprite_width,
  3311. uint32_t sprite_height,
  3312. int pixel_size,
  3313. bool enabled, bool scaled)
  3314. {
  3315. struct drm_i915_private *dev_priv = plane->dev->dev_private;
  3316. if (dev_priv->display.update_sprite_wm)
  3317. dev_priv->display.update_sprite_wm(plane, crtc,
  3318. sprite_width, sprite_height,
  3319. pixel_size, enabled, scaled);
  3320. }
  3321. static struct drm_i915_gem_object *
  3322. intel_alloc_context_page(struct drm_device *dev)
  3323. {
  3324. struct drm_i915_gem_object *ctx;
  3325. int ret;
  3326. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  3327. ctx = i915_gem_alloc_object(dev, 4096);
  3328. if (!ctx) {
  3329. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  3330. return NULL;
  3331. }
  3332. ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
  3333. if (ret) {
  3334. DRM_ERROR("failed to pin power context: %d\n", ret);
  3335. goto err_unref;
  3336. }
  3337. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  3338. if (ret) {
  3339. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  3340. goto err_unpin;
  3341. }
  3342. return ctx;
  3343. err_unpin:
  3344. i915_gem_object_ggtt_unpin(ctx);
  3345. err_unref:
  3346. drm_gem_object_unreference(&ctx->base);
  3347. return NULL;
  3348. }
  3349. /**
  3350. * Lock protecting IPS related data structures
  3351. */
  3352. DEFINE_SPINLOCK(mchdev_lock);
  3353. /* Global for IPS driver to get at the current i915 device. Protected by
  3354. * mchdev_lock. */
  3355. static struct drm_i915_private *i915_mch_dev;
  3356. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  3357. {
  3358. struct drm_i915_private *dev_priv = dev->dev_private;
  3359. u16 rgvswctl;
  3360. assert_spin_locked(&mchdev_lock);
  3361. rgvswctl = I915_READ16(MEMSWCTL);
  3362. if (rgvswctl & MEMCTL_CMD_STS) {
  3363. DRM_DEBUG("gpu busy, RCS change rejected\n");
  3364. return false; /* still busy with another command */
  3365. }
  3366. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  3367. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  3368. I915_WRITE16(MEMSWCTL, rgvswctl);
  3369. POSTING_READ16(MEMSWCTL);
  3370. rgvswctl |= MEMCTL_CMD_STS;
  3371. I915_WRITE16(MEMSWCTL, rgvswctl);
  3372. return true;
  3373. }
  3374. static void ironlake_enable_drps(struct drm_device *dev)
  3375. {
  3376. struct drm_i915_private *dev_priv = dev->dev_private;
  3377. u32 rgvmodectl = I915_READ(MEMMODECTL);
  3378. u8 fmax, fmin, fstart, vstart;
  3379. spin_lock_irq(&mchdev_lock);
  3380. /* Enable temp reporting */
  3381. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  3382. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  3383. /* 100ms RC evaluation intervals */
  3384. I915_WRITE(RCUPEI, 100000);
  3385. I915_WRITE(RCDNEI, 100000);
  3386. /* Set max/min thresholds to 90ms and 80ms respectively */
  3387. I915_WRITE(RCBMAXAVG, 90000);
  3388. I915_WRITE(RCBMINAVG, 80000);
  3389. I915_WRITE(MEMIHYST, 1);
  3390. /* Set up min, max, and cur for interrupt handling */
  3391. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  3392. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  3393. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  3394. MEMMODE_FSTART_SHIFT;
  3395. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  3396. PXVFREQ_PX_SHIFT;
  3397. dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
  3398. dev_priv->ips.fstart = fstart;
  3399. dev_priv->ips.max_delay = fstart;
  3400. dev_priv->ips.min_delay = fmin;
  3401. dev_priv->ips.cur_delay = fstart;
  3402. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  3403. fmax, fmin, fstart);
  3404. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  3405. /*
  3406. * Interrupts will be enabled in ironlake_irq_postinstall
  3407. */
  3408. I915_WRITE(VIDSTART, vstart);
  3409. POSTING_READ(VIDSTART);
  3410. rgvmodectl |= MEMMODE_SWMODE_EN;
  3411. I915_WRITE(MEMMODECTL, rgvmodectl);
  3412. if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  3413. DRM_ERROR("stuck trying to change perf mode\n");
  3414. mdelay(1);
  3415. ironlake_set_drps(dev, fstart);
  3416. dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  3417. I915_READ(0x112e0);
  3418. dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
  3419. dev_priv->ips.last_count2 = I915_READ(0x112f4);
  3420. dev_priv->ips.last_time2 = ktime_get_raw_ns();
  3421. spin_unlock_irq(&mchdev_lock);
  3422. }
  3423. static void ironlake_disable_drps(struct drm_device *dev)
  3424. {
  3425. struct drm_i915_private *dev_priv = dev->dev_private;
  3426. u16 rgvswctl;
  3427. spin_lock_irq(&mchdev_lock);
  3428. rgvswctl = I915_READ16(MEMSWCTL);
  3429. /* Ack interrupts, disable EFC interrupt */
  3430. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  3431. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  3432. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  3433. I915_WRITE(DEIIR, DE_PCU_EVENT);
  3434. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  3435. /* Go back to the starting frequency */
  3436. ironlake_set_drps(dev, dev_priv->ips.fstart);
  3437. mdelay(1);
  3438. rgvswctl |= MEMCTL_CMD_STS;
  3439. I915_WRITE(MEMSWCTL, rgvswctl);
  3440. mdelay(1);
  3441. spin_unlock_irq(&mchdev_lock);
  3442. }
  3443. /* There's a funny hw issue where the hw returns all 0 when reading from
  3444. * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
  3445. * ourselves, instead of doing a rmw cycle (which might result in us clearing
  3446. * all limits and the gpu stuck at whatever frequency it is at atm).
  3447. */
  3448. static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
  3449. {
  3450. u32 limits;
  3451. /* Only set the down limit when we've reached the lowest level to avoid
  3452. * getting more interrupts, otherwise leave this clear. This prevents a
  3453. * race in the hw when coming out of rc6: There's a tiny window where
  3454. * the hw runs at the minimal clock before selecting the desired
  3455. * frequency, if the down threshold expires in that window we will not
  3456. * receive a down interrupt. */
  3457. limits = dev_priv->rps.max_freq_softlimit << 24;
  3458. if (val <= dev_priv->rps.min_freq_softlimit)
  3459. limits |= dev_priv->rps.min_freq_softlimit << 16;
  3460. return limits;
  3461. }
  3462. static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
  3463. {
  3464. int new_power;
  3465. new_power = dev_priv->rps.power;
  3466. switch (dev_priv->rps.power) {
  3467. case LOW_POWER:
  3468. if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
  3469. new_power = BETWEEN;
  3470. break;
  3471. case BETWEEN:
  3472. if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
  3473. new_power = LOW_POWER;
  3474. else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
  3475. new_power = HIGH_POWER;
  3476. break;
  3477. case HIGH_POWER:
  3478. if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
  3479. new_power = BETWEEN;
  3480. break;
  3481. }
  3482. /* Max/min bins are special */
  3483. if (val == dev_priv->rps.min_freq_softlimit)
  3484. new_power = LOW_POWER;
  3485. if (val == dev_priv->rps.max_freq_softlimit)
  3486. new_power = HIGH_POWER;
  3487. if (new_power == dev_priv->rps.power)
  3488. return;
  3489. /* Note the units here are not exactly 1us, but 1280ns. */
  3490. switch (new_power) {
  3491. case LOW_POWER:
  3492. /* Upclock if more than 95% busy over 16ms */
  3493. I915_WRITE(GEN6_RP_UP_EI, 12500);
  3494. I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
  3495. /* Downclock if less than 85% busy over 32ms */
  3496. I915_WRITE(GEN6_RP_DOWN_EI, 25000);
  3497. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
  3498. I915_WRITE(GEN6_RP_CONTROL,
  3499. GEN6_RP_MEDIA_TURBO |
  3500. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3501. GEN6_RP_MEDIA_IS_GFX |
  3502. GEN6_RP_ENABLE |
  3503. GEN6_RP_UP_BUSY_AVG |
  3504. GEN6_RP_DOWN_IDLE_AVG);
  3505. break;
  3506. case BETWEEN:
  3507. /* Upclock if more than 90% busy over 13ms */
  3508. I915_WRITE(GEN6_RP_UP_EI, 10250);
  3509. I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
  3510. /* Downclock if less than 75% busy over 32ms */
  3511. I915_WRITE(GEN6_RP_DOWN_EI, 25000);
  3512. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
  3513. I915_WRITE(GEN6_RP_CONTROL,
  3514. GEN6_RP_MEDIA_TURBO |
  3515. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3516. GEN6_RP_MEDIA_IS_GFX |
  3517. GEN6_RP_ENABLE |
  3518. GEN6_RP_UP_BUSY_AVG |
  3519. GEN6_RP_DOWN_IDLE_AVG);
  3520. break;
  3521. case HIGH_POWER:
  3522. /* Upclock if more than 85% busy over 10ms */
  3523. I915_WRITE(GEN6_RP_UP_EI, 8000);
  3524. I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
  3525. /* Downclock if less than 60% busy over 32ms */
  3526. I915_WRITE(GEN6_RP_DOWN_EI, 25000);
  3527. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
  3528. I915_WRITE(GEN6_RP_CONTROL,
  3529. GEN6_RP_MEDIA_TURBO |
  3530. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3531. GEN6_RP_MEDIA_IS_GFX |
  3532. GEN6_RP_ENABLE |
  3533. GEN6_RP_UP_BUSY_AVG |
  3534. GEN6_RP_DOWN_IDLE_AVG);
  3535. break;
  3536. }
  3537. dev_priv->rps.power = new_power;
  3538. dev_priv->rps.last_adj = 0;
  3539. }
  3540. static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
  3541. {
  3542. u32 mask = 0;
  3543. if (val > dev_priv->rps.min_freq_softlimit)
  3544. mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
  3545. if (val < dev_priv->rps.max_freq_softlimit)
  3546. mask |= GEN6_PM_RP_UP_THRESHOLD;
  3547. mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
  3548. mask &= dev_priv->pm_rps_events;
  3549. /* IVB and SNB hard hangs on looping batchbuffer
  3550. * if GEN6_PM_UP_EI_EXPIRED is masked.
  3551. */
  3552. if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
  3553. mask |= GEN6_PM_RP_UP_EI_EXPIRED;
  3554. if (IS_GEN8(dev_priv->dev))
  3555. mask |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
  3556. return ~mask;
  3557. }
  3558. /* gen6_set_rps is called to update the frequency request, but should also be
  3559. * called when the range (min_delay and max_delay) is modified so that we can
  3560. * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
  3561. void gen6_set_rps(struct drm_device *dev, u8 val)
  3562. {
  3563. struct drm_i915_private *dev_priv = dev->dev_private;
  3564. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3565. WARN_ON(val > dev_priv->rps.max_freq_softlimit);
  3566. WARN_ON(val < dev_priv->rps.min_freq_softlimit);
  3567. /* min/max delay may still have been modified so be sure to
  3568. * write the limits value.
  3569. */
  3570. if (val != dev_priv->rps.cur_freq) {
  3571. gen6_set_rps_thresholds(dev_priv, val);
  3572. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  3573. I915_WRITE(GEN6_RPNSWREQ,
  3574. HSW_FREQUENCY(val));
  3575. else
  3576. I915_WRITE(GEN6_RPNSWREQ,
  3577. GEN6_FREQUENCY(val) |
  3578. GEN6_OFFSET(0) |
  3579. GEN6_AGGRESSIVE_TURBO);
  3580. }
  3581. /* Make sure we continue to get interrupts
  3582. * until we hit the minimum or maximum frequencies.
  3583. */
  3584. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
  3585. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  3586. POSTING_READ(GEN6_RPNSWREQ);
  3587. dev_priv->rps.cur_freq = val;
  3588. trace_intel_gpu_freq_change(val * 50);
  3589. }
  3590. /* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
  3591. *
  3592. * * If Gfx is Idle, then
  3593. * 1. Mask Turbo interrupts
  3594. * 2. Bring up Gfx clock
  3595. * 3. Change the freq to Rpn and wait till P-Unit updates freq
  3596. * 4. Clear the Force GFX CLK ON bit so that Gfx can down
  3597. * 5. Unmask Turbo interrupts
  3598. */
  3599. static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
  3600. {
  3601. struct drm_device *dev = dev_priv->dev;
  3602. /* Latest VLV doesn't need to force the gfx clock */
  3603. if (dev->pdev->revision >= 0xd) {
  3604. valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3605. return;
  3606. }
  3607. /*
  3608. * When we are idle. Drop to min voltage state.
  3609. */
  3610. if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
  3611. return;
  3612. /* Mask turbo interrupt so that they will not come in between */
  3613. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  3614. vlv_force_gfx_clock(dev_priv, true);
  3615. dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
  3616. vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
  3617. dev_priv->rps.min_freq_softlimit);
  3618. if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
  3619. & GENFREQSTATUS) == 0, 5))
  3620. DRM_ERROR("timed out waiting for Punit\n");
  3621. vlv_force_gfx_clock(dev_priv, false);
  3622. I915_WRITE(GEN6_PMINTRMSK,
  3623. gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
  3624. }
  3625. void gen6_rps_idle(struct drm_i915_private *dev_priv)
  3626. {
  3627. struct drm_device *dev = dev_priv->dev;
  3628. mutex_lock(&dev_priv->rps.hw_lock);
  3629. if (dev_priv->rps.enabled) {
  3630. if (IS_CHERRYVIEW(dev))
  3631. valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3632. else if (IS_VALLEYVIEW(dev))
  3633. vlv_set_rps_idle(dev_priv);
  3634. else
  3635. gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3636. dev_priv->rps.last_adj = 0;
  3637. }
  3638. mutex_unlock(&dev_priv->rps.hw_lock);
  3639. }
  3640. void gen6_rps_boost(struct drm_i915_private *dev_priv)
  3641. {
  3642. struct drm_device *dev = dev_priv->dev;
  3643. mutex_lock(&dev_priv->rps.hw_lock);
  3644. if (dev_priv->rps.enabled) {
  3645. if (IS_VALLEYVIEW(dev))
  3646. valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
  3647. else
  3648. gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
  3649. dev_priv->rps.last_adj = 0;
  3650. }
  3651. mutex_unlock(&dev_priv->rps.hw_lock);
  3652. }
  3653. void valleyview_set_rps(struct drm_device *dev, u8 val)
  3654. {
  3655. struct drm_i915_private *dev_priv = dev->dev_private;
  3656. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3657. WARN_ON(val > dev_priv->rps.max_freq_softlimit);
  3658. WARN_ON(val < dev_priv->rps.min_freq_softlimit);
  3659. if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
  3660. "Odd GPU freq value\n"))
  3661. val &= ~1;
  3662. if (val != dev_priv->rps.cur_freq) {
  3663. DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
  3664. vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
  3665. dev_priv->rps.cur_freq,
  3666. vlv_gpu_freq(dev_priv, val), val);
  3667. vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
  3668. }
  3669. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  3670. dev_priv->rps.cur_freq = val;
  3671. trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
  3672. }
  3673. static void gen8_disable_rps_interrupts(struct drm_device *dev)
  3674. {
  3675. struct drm_i915_private *dev_priv = dev->dev_private;
  3676. I915_WRITE(GEN6_PMINTRMSK, ~GEN8_PMINTR_REDIRECT_TO_NON_DISP);
  3677. I915_WRITE(GEN8_GT_IER(2), I915_READ(GEN8_GT_IER(2)) &
  3678. ~dev_priv->pm_rps_events);
  3679. /* Complete PM interrupt masking here doesn't race with the rps work
  3680. * item again unmasking PM interrupts because that is using a different
  3681. * register (GEN8_GT_IMR(2)) to mask PM interrupts. The only risk is in
  3682. * leaving stale bits in GEN8_GT_IIR(2) and GEN8_GT_IMR(2) which
  3683. * gen8_enable_rps will clean up. */
  3684. spin_lock_irq(&dev_priv->irq_lock);
  3685. dev_priv->rps.pm_iir = 0;
  3686. spin_unlock_irq(&dev_priv->irq_lock);
  3687. I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
  3688. }
  3689. static void gen6_disable_rps_interrupts(struct drm_device *dev)
  3690. {
  3691. struct drm_i915_private *dev_priv = dev->dev_private;
  3692. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  3693. I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) &
  3694. ~dev_priv->pm_rps_events);
  3695. /* Complete PM interrupt masking here doesn't race with the rps work
  3696. * item again unmasking PM interrupts because that is using a different
  3697. * register (PMIMR) to mask PM interrupts. The only risk is in leaving
  3698. * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
  3699. spin_lock_irq(&dev_priv->irq_lock);
  3700. dev_priv->rps.pm_iir = 0;
  3701. spin_unlock_irq(&dev_priv->irq_lock);
  3702. I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
  3703. }
  3704. static void gen6_disable_rps(struct drm_device *dev)
  3705. {
  3706. struct drm_i915_private *dev_priv = dev->dev_private;
  3707. I915_WRITE(GEN6_RC_CONTROL, 0);
  3708. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  3709. if (IS_BROADWELL(dev))
  3710. gen8_disable_rps_interrupts(dev);
  3711. else
  3712. gen6_disable_rps_interrupts(dev);
  3713. }
  3714. static void cherryview_disable_rps(struct drm_device *dev)
  3715. {
  3716. struct drm_i915_private *dev_priv = dev->dev_private;
  3717. I915_WRITE(GEN6_RC_CONTROL, 0);
  3718. gen8_disable_rps_interrupts(dev);
  3719. }
  3720. static void valleyview_disable_rps(struct drm_device *dev)
  3721. {
  3722. struct drm_i915_private *dev_priv = dev->dev_private;
  3723. /* we're doing forcewake before Disabling RC6,
  3724. * This what the BIOS expects when going into suspend */
  3725. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  3726. I915_WRITE(GEN6_RC_CONTROL, 0);
  3727. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  3728. gen6_disable_rps_interrupts(dev);
  3729. }
  3730. static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
  3731. {
  3732. if (IS_VALLEYVIEW(dev)) {
  3733. if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
  3734. mode = GEN6_RC_CTL_RC6_ENABLE;
  3735. else
  3736. mode = 0;
  3737. }
  3738. if (HAS_RC6p(dev))
  3739. DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
  3740. (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
  3741. (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
  3742. (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
  3743. else
  3744. DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
  3745. (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
  3746. }
  3747. static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
  3748. {
  3749. /* No RC6 before Ironlake */
  3750. if (INTEL_INFO(dev)->gen < 5)
  3751. return 0;
  3752. /* RC6 is only on Ironlake mobile not on desktop */
  3753. if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
  3754. return 0;
  3755. /* Respect the kernel parameter if it is set */
  3756. if (enable_rc6 >= 0) {
  3757. int mask;
  3758. if (HAS_RC6p(dev))
  3759. mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
  3760. INTEL_RC6pp_ENABLE;
  3761. else
  3762. mask = INTEL_RC6_ENABLE;
  3763. if ((enable_rc6 & mask) != enable_rc6)
  3764. DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
  3765. enable_rc6 & mask, enable_rc6, mask);
  3766. return enable_rc6 & mask;
  3767. }
  3768. /* Disable RC6 on Ironlake */
  3769. if (INTEL_INFO(dev)->gen == 5)
  3770. return 0;
  3771. if (IS_IVYBRIDGE(dev))
  3772. return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
  3773. return INTEL_RC6_ENABLE;
  3774. }
  3775. int intel_enable_rc6(const struct drm_device *dev)
  3776. {
  3777. return i915.enable_rc6;
  3778. }
  3779. static void gen8_enable_rps_interrupts(struct drm_device *dev)
  3780. {
  3781. struct drm_i915_private *dev_priv = dev->dev_private;
  3782. spin_lock_irq(&dev_priv->irq_lock);
  3783. WARN_ON(dev_priv->rps.pm_iir);
  3784. gen8_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
  3785. I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
  3786. spin_unlock_irq(&dev_priv->irq_lock);
  3787. }
  3788. static void gen6_enable_rps_interrupts(struct drm_device *dev)
  3789. {
  3790. struct drm_i915_private *dev_priv = dev->dev_private;
  3791. spin_lock_irq(&dev_priv->irq_lock);
  3792. WARN_ON(dev_priv->rps.pm_iir);
  3793. gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
  3794. I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
  3795. spin_unlock_irq(&dev_priv->irq_lock);
  3796. }
  3797. static void parse_rp_state_cap(struct drm_i915_private *dev_priv, u32 rp_state_cap)
  3798. {
  3799. /* All of these values are in units of 50MHz */
  3800. dev_priv->rps.cur_freq = 0;
  3801. /* static values from HW: RP0 < RPe < RP1 < RPn (min_freq) */
  3802. dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
  3803. dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
  3804. dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
  3805. /* XXX: only BYT has a special efficient freq */
  3806. dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
  3807. /* hw_max = RP0 until we check for overclocking */
  3808. dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
  3809. /* Preserve min/max settings in case of re-init */
  3810. if (dev_priv->rps.max_freq_softlimit == 0)
  3811. dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
  3812. if (dev_priv->rps.min_freq_softlimit == 0)
  3813. dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
  3814. }
  3815. static void gen8_enable_rps(struct drm_device *dev)
  3816. {
  3817. struct drm_i915_private *dev_priv = dev->dev_private;
  3818. struct intel_engine_cs *ring;
  3819. uint32_t rc6_mask = 0, rp_state_cap;
  3820. int unused;
  3821. /* 1a: Software RC state - RC0 */
  3822. I915_WRITE(GEN6_RC_STATE, 0);
  3823. /* 1c & 1d: Get forcewake during program sequence. Although the driver
  3824. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  3825. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  3826. /* 2a: Disable RC states. */
  3827. I915_WRITE(GEN6_RC_CONTROL, 0);
  3828. rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  3829. parse_rp_state_cap(dev_priv, rp_state_cap);
  3830. /* 2b: Program RC6 thresholds.*/
  3831. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  3832. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  3833. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  3834. for_each_ring(ring, dev_priv, unused)
  3835. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  3836. I915_WRITE(GEN6_RC_SLEEP, 0);
  3837. if (IS_BROADWELL(dev))
  3838. I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
  3839. else
  3840. I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
  3841. /* 3: Enable RC6 */
  3842. if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
  3843. rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
  3844. intel_print_rc6_info(dev, rc6_mask);
  3845. if (IS_BROADWELL(dev))
  3846. I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
  3847. GEN7_RC_CTL_TO_MODE |
  3848. rc6_mask);
  3849. else
  3850. I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
  3851. GEN6_RC_CTL_EI_MODE(1) |
  3852. rc6_mask);
  3853. /* 4 Program defaults and thresholds for RPS*/
  3854. I915_WRITE(GEN6_RPNSWREQ,
  3855. HSW_FREQUENCY(dev_priv->rps.rp1_freq));
  3856. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  3857. HSW_FREQUENCY(dev_priv->rps.rp1_freq));
  3858. /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
  3859. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
  3860. /* Docs recommend 900MHz, and 300 MHz respectively */
  3861. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  3862. dev_priv->rps.max_freq_softlimit << 24 |
  3863. dev_priv->rps.min_freq_softlimit << 16);
  3864. I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
  3865. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
  3866. I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
  3867. I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
  3868. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  3869. /* 5: Enable RPS */
  3870. I915_WRITE(GEN6_RP_CONTROL,
  3871. GEN6_RP_MEDIA_TURBO |
  3872. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3873. GEN6_RP_MEDIA_IS_GFX |
  3874. GEN6_RP_ENABLE |
  3875. GEN6_RP_UP_BUSY_AVG |
  3876. GEN6_RP_DOWN_IDLE_AVG);
  3877. /* 6: Ring frequency + overclocking (our driver does this later */
  3878. gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
  3879. gen8_enable_rps_interrupts(dev);
  3880. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  3881. }
  3882. static void gen6_enable_rps(struct drm_device *dev)
  3883. {
  3884. struct drm_i915_private *dev_priv = dev->dev_private;
  3885. struct intel_engine_cs *ring;
  3886. u32 rp_state_cap;
  3887. u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
  3888. u32 gtfifodbg;
  3889. int rc6_mode;
  3890. int i, ret;
  3891. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3892. /* Here begins a magic sequence of register writes to enable
  3893. * auto-downclocking.
  3894. *
  3895. * Perhaps there might be some value in exposing these to
  3896. * userspace...
  3897. */
  3898. I915_WRITE(GEN6_RC_STATE, 0);
  3899. /* Clear the DBG now so we don't confuse earlier errors */
  3900. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  3901. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  3902. I915_WRITE(GTFIFODBG, gtfifodbg);
  3903. }
  3904. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  3905. rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  3906. parse_rp_state_cap(dev_priv, rp_state_cap);
  3907. /* disable the counters and set deterministic thresholds */
  3908. I915_WRITE(GEN6_RC_CONTROL, 0);
  3909. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  3910. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  3911. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  3912. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  3913. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  3914. for_each_ring(ring, dev_priv, i)
  3915. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  3916. I915_WRITE(GEN6_RC_SLEEP, 0);
  3917. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  3918. if (IS_IVYBRIDGE(dev))
  3919. I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
  3920. else
  3921. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  3922. I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
  3923. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  3924. /* Check if we are enabling RC6 */
  3925. rc6_mode = intel_enable_rc6(dev_priv->dev);
  3926. if (rc6_mode & INTEL_RC6_ENABLE)
  3927. rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
  3928. /* We don't use those on Haswell */
  3929. if (!IS_HASWELL(dev)) {
  3930. if (rc6_mode & INTEL_RC6p_ENABLE)
  3931. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  3932. if (rc6_mode & INTEL_RC6pp_ENABLE)
  3933. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  3934. }
  3935. intel_print_rc6_info(dev, rc6_mask);
  3936. I915_WRITE(GEN6_RC_CONTROL,
  3937. rc6_mask |
  3938. GEN6_RC_CTL_EI_MODE(1) |
  3939. GEN6_RC_CTL_HW_ENABLE);
  3940. /* Power down if completely idle for over 50ms */
  3941. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
  3942. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  3943. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
  3944. if (ret)
  3945. DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
  3946. ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
  3947. if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
  3948. DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
  3949. (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
  3950. (pcu_mbox & 0xff) * 50);
  3951. dev_priv->rps.max_freq = pcu_mbox & 0xff;
  3952. }
  3953. dev_priv->rps.power = HIGH_POWER; /* force a reset */
  3954. gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3955. gen6_enable_rps_interrupts(dev);
  3956. rc6vids = 0;
  3957. ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
  3958. if (IS_GEN6(dev) && ret) {
  3959. DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
  3960. } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
  3961. DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
  3962. GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
  3963. rc6vids &= 0xffff00;
  3964. rc6vids |= GEN6_ENCODE_RC6_VID(450);
  3965. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
  3966. if (ret)
  3967. DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
  3968. }
  3969. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  3970. }
  3971. static void __gen6_update_ring_freq(struct drm_device *dev)
  3972. {
  3973. struct drm_i915_private *dev_priv = dev->dev_private;
  3974. int min_freq = 15;
  3975. unsigned int gpu_freq;
  3976. unsigned int max_ia_freq, min_ring_freq;
  3977. int scaling_factor = 180;
  3978. struct cpufreq_policy *policy;
  3979. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3980. policy = cpufreq_cpu_get(0);
  3981. if (policy) {
  3982. max_ia_freq = policy->cpuinfo.max_freq;
  3983. cpufreq_cpu_put(policy);
  3984. } else {
  3985. /*
  3986. * Default to measured freq if none found, PCU will ensure we
  3987. * don't go over
  3988. */
  3989. max_ia_freq = tsc_khz;
  3990. }
  3991. /* Convert from kHz to MHz */
  3992. max_ia_freq /= 1000;
  3993. min_ring_freq = I915_READ(DCLK) & 0xf;
  3994. /* convert DDR frequency from units of 266.6MHz to bandwidth */
  3995. min_ring_freq = mult_frac(min_ring_freq, 8, 3);
  3996. /*
  3997. * For each potential GPU frequency, load a ring frequency we'd like
  3998. * to use for memory access. We do this by specifying the IA frequency
  3999. * the PCU should use as a reference to determine the ring frequency.
  4000. */
  4001. for (gpu_freq = dev_priv->rps.max_freq_softlimit; gpu_freq >= dev_priv->rps.min_freq_softlimit;
  4002. gpu_freq--) {
  4003. int diff = dev_priv->rps.max_freq_softlimit - gpu_freq;
  4004. unsigned int ia_freq = 0, ring_freq = 0;
  4005. if (INTEL_INFO(dev)->gen >= 8) {
  4006. /* max(2 * GT, DDR). NB: GT is 50MHz units */
  4007. ring_freq = max(min_ring_freq, gpu_freq);
  4008. } else if (IS_HASWELL(dev)) {
  4009. ring_freq = mult_frac(gpu_freq, 5, 4);
  4010. ring_freq = max(min_ring_freq, ring_freq);
  4011. /* leave ia_freq as the default, chosen by cpufreq */
  4012. } else {
  4013. /* On older processors, there is no separate ring
  4014. * clock domain, so in order to boost the bandwidth
  4015. * of the ring, we need to upclock the CPU (ia_freq).
  4016. *
  4017. * For GPU frequencies less than 750MHz,
  4018. * just use the lowest ring freq.
  4019. */
  4020. if (gpu_freq < min_freq)
  4021. ia_freq = 800;
  4022. else
  4023. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  4024. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  4025. }
  4026. sandybridge_pcode_write(dev_priv,
  4027. GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
  4028. ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
  4029. ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
  4030. gpu_freq);
  4031. }
  4032. }
  4033. void gen6_update_ring_freq(struct drm_device *dev)
  4034. {
  4035. struct drm_i915_private *dev_priv = dev->dev_private;
  4036. if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
  4037. return;
  4038. mutex_lock(&dev_priv->rps.hw_lock);
  4039. __gen6_update_ring_freq(dev);
  4040. mutex_unlock(&dev_priv->rps.hw_lock);
  4041. }
  4042. static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
  4043. {
  4044. u32 val, rp0;
  4045. val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
  4046. rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
  4047. return rp0;
  4048. }
  4049. static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  4050. {
  4051. u32 val, rpe;
  4052. val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
  4053. rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
  4054. return rpe;
  4055. }
  4056. static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
  4057. {
  4058. u32 val, rp1;
  4059. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  4060. rp1 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
  4061. return rp1;
  4062. }
  4063. static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
  4064. {
  4065. u32 val, rpn;
  4066. val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
  4067. rpn = (val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) & PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK;
  4068. return rpn;
  4069. }
  4070. static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
  4071. {
  4072. u32 val, rp1;
  4073. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  4074. rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
  4075. return rp1;
  4076. }
  4077. static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
  4078. {
  4079. u32 val, rp0;
  4080. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  4081. rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
  4082. /* Clamp to max */
  4083. rp0 = min_t(u32, rp0, 0xea);
  4084. return rp0;
  4085. }
  4086. static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  4087. {
  4088. u32 val, rpe;
  4089. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
  4090. rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
  4091. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
  4092. rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
  4093. return rpe;
  4094. }
  4095. static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
  4096. {
  4097. return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
  4098. }
  4099. /* Check that the pctx buffer wasn't move under us. */
  4100. static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
  4101. {
  4102. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  4103. WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
  4104. dev_priv->vlv_pctx->stolen->start);
  4105. }
  4106. /* Check that the pcbr address is not empty. */
  4107. static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
  4108. {
  4109. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  4110. WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
  4111. }
  4112. static void cherryview_setup_pctx(struct drm_device *dev)
  4113. {
  4114. struct drm_i915_private *dev_priv = dev->dev_private;
  4115. unsigned long pctx_paddr, paddr;
  4116. struct i915_gtt *gtt = &dev_priv->gtt;
  4117. u32 pcbr;
  4118. int pctx_size = 32*1024;
  4119. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  4120. pcbr = I915_READ(VLV_PCBR);
  4121. if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
  4122. paddr = (dev_priv->mm.stolen_base +
  4123. (gtt->stolen_size - pctx_size));
  4124. pctx_paddr = (paddr & (~4095));
  4125. I915_WRITE(VLV_PCBR, pctx_paddr);
  4126. }
  4127. }
  4128. static void valleyview_setup_pctx(struct drm_device *dev)
  4129. {
  4130. struct drm_i915_private *dev_priv = dev->dev_private;
  4131. struct drm_i915_gem_object *pctx;
  4132. unsigned long pctx_paddr;
  4133. u32 pcbr;
  4134. int pctx_size = 24*1024;
  4135. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  4136. pcbr = I915_READ(VLV_PCBR);
  4137. if (pcbr) {
  4138. /* BIOS set it up already, grab the pre-alloc'd space */
  4139. int pcbr_offset;
  4140. pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
  4141. pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
  4142. pcbr_offset,
  4143. I915_GTT_OFFSET_NONE,
  4144. pctx_size);
  4145. goto out;
  4146. }
  4147. /*
  4148. * From the Gunit register HAS:
  4149. * The Gfx driver is expected to program this register and ensure
  4150. * proper allocation within Gfx stolen memory. For example, this
  4151. * register should be programmed such than the PCBR range does not
  4152. * overlap with other ranges, such as the frame buffer, protected
  4153. * memory, or any other relevant ranges.
  4154. */
  4155. pctx = i915_gem_object_create_stolen(dev, pctx_size);
  4156. if (!pctx) {
  4157. DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
  4158. return;
  4159. }
  4160. pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
  4161. I915_WRITE(VLV_PCBR, pctx_paddr);
  4162. out:
  4163. dev_priv->vlv_pctx = pctx;
  4164. }
  4165. static void valleyview_cleanup_pctx(struct drm_device *dev)
  4166. {
  4167. struct drm_i915_private *dev_priv = dev->dev_private;
  4168. if (WARN_ON(!dev_priv->vlv_pctx))
  4169. return;
  4170. drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
  4171. dev_priv->vlv_pctx = NULL;
  4172. }
  4173. static void valleyview_init_gt_powersave(struct drm_device *dev)
  4174. {
  4175. struct drm_i915_private *dev_priv = dev->dev_private;
  4176. u32 val;
  4177. valleyview_setup_pctx(dev);
  4178. mutex_lock(&dev_priv->rps.hw_lock);
  4179. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  4180. switch ((val >> 6) & 3) {
  4181. case 0:
  4182. case 1:
  4183. dev_priv->mem_freq = 800;
  4184. break;
  4185. case 2:
  4186. dev_priv->mem_freq = 1066;
  4187. break;
  4188. case 3:
  4189. dev_priv->mem_freq = 1333;
  4190. break;
  4191. }
  4192. DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
  4193. dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
  4194. dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
  4195. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  4196. vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
  4197. dev_priv->rps.max_freq);
  4198. dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
  4199. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  4200. vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  4201. dev_priv->rps.efficient_freq);
  4202. dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
  4203. DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
  4204. vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
  4205. dev_priv->rps.rp1_freq);
  4206. dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
  4207. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  4208. vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
  4209. dev_priv->rps.min_freq);
  4210. /* Preserve min/max settings in case of re-init */
  4211. if (dev_priv->rps.max_freq_softlimit == 0)
  4212. dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
  4213. if (dev_priv->rps.min_freq_softlimit == 0)
  4214. dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
  4215. mutex_unlock(&dev_priv->rps.hw_lock);
  4216. }
  4217. static void cherryview_init_gt_powersave(struct drm_device *dev)
  4218. {
  4219. struct drm_i915_private *dev_priv = dev->dev_private;
  4220. u32 val;
  4221. cherryview_setup_pctx(dev);
  4222. mutex_lock(&dev_priv->rps.hw_lock);
  4223. val = vlv_punit_read(dev_priv, CCK_FUSE_REG);
  4224. switch ((val >> 2) & 0x7) {
  4225. case 0:
  4226. case 1:
  4227. dev_priv->rps.cz_freq = 200;
  4228. dev_priv->mem_freq = 1600;
  4229. break;
  4230. case 2:
  4231. dev_priv->rps.cz_freq = 267;
  4232. dev_priv->mem_freq = 1600;
  4233. break;
  4234. case 3:
  4235. dev_priv->rps.cz_freq = 333;
  4236. dev_priv->mem_freq = 2000;
  4237. break;
  4238. case 4:
  4239. dev_priv->rps.cz_freq = 320;
  4240. dev_priv->mem_freq = 1600;
  4241. break;
  4242. case 5:
  4243. dev_priv->rps.cz_freq = 400;
  4244. dev_priv->mem_freq = 1600;
  4245. break;
  4246. }
  4247. DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
  4248. dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
  4249. dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
  4250. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  4251. vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
  4252. dev_priv->rps.max_freq);
  4253. dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
  4254. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  4255. vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  4256. dev_priv->rps.efficient_freq);
  4257. dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
  4258. DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
  4259. vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
  4260. dev_priv->rps.rp1_freq);
  4261. dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
  4262. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  4263. vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
  4264. dev_priv->rps.min_freq);
  4265. WARN_ONCE((dev_priv->rps.max_freq |
  4266. dev_priv->rps.efficient_freq |
  4267. dev_priv->rps.rp1_freq |
  4268. dev_priv->rps.min_freq) & 1,
  4269. "Odd GPU freq values\n");
  4270. /* Preserve min/max settings in case of re-init */
  4271. if (dev_priv->rps.max_freq_softlimit == 0)
  4272. dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
  4273. if (dev_priv->rps.min_freq_softlimit == 0)
  4274. dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
  4275. mutex_unlock(&dev_priv->rps.hw_lock);
  4276. }
  4277. static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
  4278. {
  4279. valleyview_cleanup_pctx(dev);
  4280. }
  4281. static void cherryview_enable_rps(struct drm_device *dev)
  4282. {
  4283. struct drm_i915_private *dev_priv = dev->dev_private;
  4284. struct intel_engine_cs *ring;
  4285. u32 gtfifodbg, val, rc6_mode = 0, pcbr;
  4286. int i;
  4287. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  4288. gtfifodbg = I915_READ(GTFIFODBG);
  4289. if (gtfifodbg) {
  4290. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  4291. gtfifodbg);
  4292. I915_WRITE(GTFIFODBG, gtfifodbg);
  4293. }
  4294. cherryview_check_pctx(dev_priv);
  4295. /* 1a & 1b: Get forcewake during program sequence. Although the driver
  4296. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  4297. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  4298. /* 2a: Program RC6 thresholds.*/
  4299. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  4300. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  4301. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  4302. for_each_ring(ring, dev_priv, i)
  4303. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  4304. I915_WRITE(GEN6_RC_SLEEP, 0);
  4305. I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
  4306. /* allows RC6 residency counter to work */
  4307. I915_WRITE(VLV_COUNTER_CONTROL,
  4308. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
  4309. VLV_MEDIA_RC6_COUNT_EN |
  4310. VLV_RENDER_RC6_COUNT_EN));
  4311. /* For now we assume BIOS is allocating and populating the PCBR */
  4312. pcbr = I915_READ(VLV_PCBR);
  4313. DRM_DEBUG_DRIVER("PCBR offset : 0x%x\n", pcbr);
  4314. /* 3: Enable RC6 */
  4315. if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
  4316. (pcbr >> VLV_PCBR_ADDR_SHIFT))
  4317. rc6_mode = GEN6_RC_CTL_EI_MODE(1);
  4318. I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
  4319. /* 4 Program defaults and thresholds for RPS*/
  4320. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  4321. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  4322. I915_WRITE(GEN6_RP_UP_EI, 66000);
  4323. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  4324. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  4325. /* WaDisablePwrmtrEvent:chv (pre-production hw) */
  4326. I915_WRITE(0xA80C, I915_READ(0xA80C) & 0x00ffffff);
  4327. I915_WRITE(0xA810, I915_READ(0xA810) & 0xffffff00);
  4328. /* 5: Enable RPS */
  4329. I915_WRITE(GEN6_RP_CONTROL,
  4330. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  4331. GEN6_RP_MEDIA_IS_GFX | /* WaSetMaskForGfxBusyness:chv (pre-production hw ?) */
  4332. GEN6_RP_ENABLE |
  4333. GEN6_RP_UP_BUSY_AVG |
  4334. GEN6_RP_DOWN_IDLE_AVG);
  4335. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  4336. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
  4337. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  4338. dev_priv->rps.cur_freq = (val >> 8) & 0xff;
  4339. DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
  4340. vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
  4341. dev_priv->rps.cur_freq);
  4342. DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
  4343. vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  4344. dev_priv->rps.efficient_freq);
  4345. valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
  4346. gen8_enable_rps_interrupts(dev);
  4347. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  4348. }
  4349. static void valleyview_enable_rps(struct drm_device *dev)
  4350. {
  4351. struct drm_i915_private *dev_priv = dev->dev_private;
  4352. struct intel_engine_cs *ring;
  4353. u32 gtfifodbg, val, rc6_mode = 0;
  4354. int i;
  4355. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  4356. valleyview_check_pctx(dev_priv);
  4357. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  4358. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  4359. gtfifodbg);
  4360. I915_WRITE(GTFIFODBG, gtfifodbg);
  4361. }
  4362. /* If VLV, Forcewake all wells, else re-direct to regular path */
  4363. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  4364. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  4365. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  4366. I915_WRITE(GEN6_RP_UP_EI, 66000);
  4367. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  4368. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  4369. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
  4370. I915_WRITE(GEN6_RP_CONTROL,
  4371. GEN6_RP_MEDIA_TURBO |
  4372. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  4373. GEN6_RP_MEDIA_IS_GFX |
  4374. GEN6_RP_ENABLE |
  4375. GEN6_RP_UP_BUSY_AVG |
  4376. GEN6_RP_DOWN_IDLE_CONT);
  4377. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
  4378. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  4379. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  4380. for_each_ring(ring, dev_priv, i)
  4381. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  4382. I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
  4383. /* allows RC6 residency counter to work */
  4384. I915_WRITE(VLV_COUNTER_CONTROL,
  4385. _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
  4386. VLV_RENDER_RC0_COUNT_EN |
  4387. VLV_MEDIA_RC6_COUNT_EN |
  4388. VLV_RENDER_RC6_COUNT_EN));
  4389. if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
  4390. rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
  4391. intel_print_rc6_info(dev, rc6_mode);
  4392. I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
  4393. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  4394. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
  4395. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  4396. dev_priv->rps.cur_freq = (val >> 8) & 0xff;
  4397. DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
  4398. vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
  4399. dev_priv->rps.cur_freq);
  4400. DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
  4401. vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  4402. dev_priv->rps.efficient_freq);
  4403. valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
  4404. gen6_enable_rps_interrupts(dev);
  4405. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  4406. }
  4407. void ironlake_teardown_rc6(struct drm_device *dev)
  4408. {
  4409. struct drm_i915_private *dev_priv = dev->dev_private;
  4410. if (dev_priv->ips.renderctx) {
  4411. i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
  4412. drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
  4413. dev_priv->ips.renderctx = NULL;
  4414. }
  4415. if (dev_priv->ips.pwrctx) {
  4416. i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
  4417. drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
  4418. dev_priv->ips.pwrctx = NULL;
  4419. }
  4420. }
  4421. static void ironlake_disable_rc6(struct drm_device *dev)
  4422. {
  4423. struct drm_i915_private *dev_priv = dev->dev_private;
  4424. if (I915_READ(PWRCTXA)) {
  4425. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  4426. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  4427. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  4428. 50);
  4429. I915_WRITE(PWRCTXA, 0);
  4430. POSTING_READ(PWRCTXA);
  4431. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  4432. POSTING_READ(RSTDBYCTL);
  4433. }
  4434. }
  4435. static int ironlake_setup_rc6(struct drm_device *dev)
  4436. {
  4437. struct drm_i915_private *dev_priv = dev->dev_private;
  4438. if (dev_priv->ips.renderctx == NULL)
  4439. dev_priv->ips.renderctx = intel_alloc_context_page(dev);
  4440. if (!dev_priv->ips.renderctx)
  4441. return -ENOMEM;
  4442. if (dev_priv->ips.pwrctx == NULL)
  4443. dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
  4444. if (!dev_priv->ips.pwrctx) {
  4445. ironlake_teardown_rc6(dev);
  4446. return -ENOMEM;
  4447. }
  4448. return 0;
  4449. }
  4450. static void ironlake_enable_rc6(struct drm_device *dev)
  4451. {
  4452. struct drm_i915_private *dev_priv = dev->dev_private;
  4453. struct intel_engine_cs *ring = &dev_priv->ring[RCS];
  4454. bool was_interruptible;
  4455. int ret;
  4456. /* rc6 disabled by default due to repeated reports of hanging during
  4457. * boot and resume.
  4458. */
  4459. if (!intel_enable_rc6(dev))
  4460. return;
  4461. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  4462. ret = ironlake_setup_rc6(dev);
  4463. if (ret)
  4464. return;
  4465. was_interruptible = dev_priv->mm.interruptible;
  4466. dev_priv->mm.interruptible = false;
  4467. /*
  4468. * GPU can automatically power down the render unit if given a page
  4469. * to save state.
  4470. */
  4471. ret = intel_ring_begin(ring, 6);
  4472. if (ret) {
  4473. ironlake_teardown_rc6(dev);
  4474. dev_priv->mm.interruptible = was_interruptible;
  4475. return;
  4476. }
  4477. intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  4478. intel_ring_emit(ring, MI_SET_CONTEXT);
  4479. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
  4480. MI_MM_SPACE_GTT |
  4481. MI_SAVE_EXT_STATE_EN |
  4482. MI_RESTORE_EXT_STATE_EN |
  4483. MI_RESTORE_INHIBIT);
  4484. intel_ring_emit(ring, MI_SUSPEND_FLUSH);
  4485. intel_ring_emit(ring, MI_NOOP);
  4486. intel_ring_emit(ring, MI_FLUSH);
  4487. intel_ring_advance(ring);
  4488. /*
  4489. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  4490. * does an implicit flush, combined with MI_FLUSH above, it should be
  4491. * safe to assume that renderctx is valid
  4492. */
  4493. ret = intel_ring_idle(ring);
  4494. dev_priv->mm.interruptible = was_interruptible;
  4495. if (ret) {
  4496. DRM_ERROR("failed to enable ironlake power savings\n");
  4497. ironlake_teardown_rc6(dev);
  4498. return;
  4499. }
  4500. I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
  4501. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  4502. intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
  4503. }
  4504. static unsigned long intel_pxfreq(u32 vidfreq)
  4505. {
  4506. unsigned long freq;
  4507. int div = (vidfreq & 0x3f0000) >> 16;
  4508. int post = (vidfreq & 0x3000) >> 12;
  4509. int pre = (vidfreq & 0x7);
  4510. if (!pre)
  4511. return 0;
  4512. freq = ((div * 133333) / ((1<<post) * pre));
  4513. return freq;
  4514. }
  4515. static const struct cparams {
  4516. u16 i;
  4517. u16 t;
  4518. u16 m;
  4519. u16 c;
  4520. } cparams[] = {
  4521. { 1, 1333, 301, 28664 },
  4522. { 1, 1066, 294, 24460 },
  4523. { 1, 800, 294, 25192 },
  4524. { 0, 1333, 276, 27605 },
  4525. { 0, 1066, 276, 27605 },
  4526. { 0, 800, 231, 23784 },
  4527. };
  4528. static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
  4529. {
  4530. u64 total_count, diff, ret;
  4531. u32 count1, count2, count3, m = 0, c = 0;
  4532. unsigned long now = jiffies_to_msecs(jiffies), diff1;
  4533. int i;
  4534. assert_spin_locked(&mchdev_lock);
  4535. diff1 = now - dev_priv->ips.last_time1;
  4536. /* Prevent division-by-zero if we are asking too fast.
  4537. * Also, we don't get interesting results if we are polling
  4538. * faster than once in 10ms, so just return the saved value
  4539. * in such cases.
  4540. */
  4541. if (diff1 <= 10)
  4542. return dev_priv->ips.chipset_power;
  4543. count1 = I915_READ(DMIEC);
  4544. count2 = I915_READ(DDREC);
  4545. count3 = I915_READ(CSIEC);
  4546. total_count = count1 + count2 + count3;
  4547. /* FIXME: handle per-counter overflow */
  4548. if (total_count < dev_priv->ips.last_count1) {
  4549. diff = ~0UL - dev_priv->ips.last_count1;
  4550. diff += total_count;
  4551. } else {
  4552. diff = total_count - dev_priv->ips.last_count1;
  4553. }
  4554. for (i = 0; i < ARRAY_SIZE(cparams); i++) {
  4555. if (cparams[i].i == dev_priv->ips.c_m &&
  4556. cparams[i].t == dev_priv->ips.r_t) {
  4557. m = cparams[i].m;
  4558. c = cparams[i].c;
  4559. break;
  4560. }
  4561. }
  4562. diff = div_u64(diff, diff1);
  4563. ret = ((m * diff) + c);
  4564. ret = div_u64(ret, 10);
  4565. dev_priv->ips.last_count1 = total_count;
  4566. dev_priv->ips.last_time1 = now;
  4567. dev_priv->ips.chipset_power = ret;
  4568. return ret;
  4569. }
  4570. unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
  4571. {
  4572. struct drm_device *dev = dev_priv->dev;
  4573. unsigned long val;
  4574. if (INTEL_INFO(dev)->gen != 5)
  4575. return 0;
  4576. spin_lock_irq(&mchdev_lock);
  4577. val = __i915_chipset_val(dev_priv);
  4578. spin_unlock_irq(&mchdev_lock);
  4579. return val;
  4580. }
  4581. unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
  4582. {
  4583. unsigned long m, x, b;
  4584. u32 tsfs;
  4585. tsfs = I915_READ(TSFS);
  4586. m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
  4587. x = I915_READ8(TR1);
  4588. b = tsfs & TSFS_INTR_MASK;
  4589. return ((m * x) / 127) - b;
  4590. }
  4591. static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
  4592. {
  4593. struct drm_device *dev = dev_priv->dev;
  4594. static const struct v_table {
  4595. u16 vd; /* in .1 mil */
  4596. u16 vm; /* in .1 mil */
  4597. } v_table[] = {
  4598. { 0, 0, },
  4599. { 375, 0, },
  4600. { 500, 0, },
  4601. { 625, 0, },
  4602. { 750, 0, },
  4603. { 875, 0, },
  4604. { 1000, 0, },
  4605. { 1125, 0, },
  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. { 4125, 3000, },
  4617. { 4125, 3000, },
  4618. { 4125, 3000, },
  4619. { 4125, 3000, },
  4620. { 4125, 3000, },
  4621. { 4125, 3000, },
  4622. { 4125, 3000, },
  4623. { 4125, 3000, },
  4624. { 4125, 3000, },
  4625. { 4125, 3000, },
  4626. { 4125, 3000, },
  4627. { 4125, 3000, },
  4628. { 4125, 3000, },
  4629. { 4125, 3000, },
  4630. { 4250, 3125, },
  4631. { 4375, 3250, },
  4632. { 4500, 3375, },
  4633. { 4625, 3500, },
  4634. { 4750, 3625, },
  4635. { 4875, 3750, },
  4636. { 5000, 3875, },
  4637. { 5125, 4000, },
  4638. { 5250, 4125, },
  4639. { 5375, 4250, },
  4640. { 5500, 4375, },
  4641. { 5625, 4500, },
  4642. { 5750, 4625, },
  4643. { 5875, 4750, },
  4644. { 6000, 4875, },
  4645. { 6125, 5000, },
  4646. { 6250, 5125, },
  4647. { 6375, 5250, },
  4648. { 6500, 5375, },
  4649. { 6625, 5500, },
  4650. { 6750, 5625, },
  4651. { 6875, 5750, },
  4652. { 7000, 5875, },
  4653. { 7125, 6000, },
  4654. { 7250, 6125, },
  4655. { 7375, 6250, },
  4656. { 7500, 6375, },
  4657. { 7625, 6500, },
  4658. { 7750, 6625, },
  4659. { 7875, 6750, },
  4660. { 8000, 6875, },
  4661. { 8125, 7000, },
  4662. { 8250, 7125, },
  4663. { 8375, 7250, },
  4664. { 8500, 7375, },
  4665. { 8625, 7500, },
  4666. { 8750, 7625, },
  4667. { 8875, 7750, },
  4668. { 9000, 7875, },
  4669. { 9125, 8000, },
  4670. { 9250, 8125, },
  4671. { 9375, 8250, },
  4672. { 9500, 8375, },
  4673. { 9625, 8500, },
  4674. { 9750, 8625, },
  4675. { 9875, 8750, },
  4676. { 10000, 8875, },
  4677. { 10125, 9000, },
  4678. { 10250, 9125, },
  4679. { 10375, 9250, },
  4680. { 10500, 9375, },
  4681. { 10625, 9500, },
  4682. { 10750, 9625, },
  4683. { 10875, 9750, },
  4684. { 11000, 9875, },
  4685. { 11125, 10000, },
  4686. { 11250, 10125, },
  4687. { 11375, 10250, },
  4688. { 11500, 10375, },
  4689. { 11625, 10500, },
  4690. { 11750, 10625, },
  4691. { 11875, 10750, },
  4692. { 12000, 10875, },
  4693. { 12125, 11000, },
  4694. { 12250, 11125, },
  4695. { 12375, 11250, },
  4696. { 12500, 11375, },
  4697. { 12625, 11500, },
  4698. { 12750, 11625, },
  4699. { 12875, 11750, },
  4700. { 13000, 11875, },
  4701. { 13125, 12000, },
  4702. { 13250, 12125, },
  4703. { 13375, 12250, },
  4704. { 13500, 12375, },
  4705. { 13625, 12500, },
  4706. { 13750, 12625, },
  4707. { 13875, 12750, },
  4708. { 14000, 12875, },
  4709. { 14125, 13000, },
  4710. { 14250, 13125, },
  4711. { 14375, 13250, },
  4712. { 14500, 13375, },
  4713. { 14625, 13500, },
  4714. { 14750, 13625, },
  4715. { 14875, 13750, },
  4716. { 15000, 13875, },
  4717. { 15125, 14000, },
  4718. { 15250, 14125, },
  4719. { 15375, 14250, },
  4720. { 15500, 14375, },
  4721. { 15625, 14500, },
  4722. { 15750, 14625, },
  4723. { 15875, 14750, },
  4724. { 16000, 14875, },
  4725. { 16125, 15000, },
  4726. };
  4727. if (INTEL_INFO(dev)->is_mobile)
  4728. return v_table[pxvid].vm;
  4729. else
  4730. return v_table[pxvid].vd;
  4731. }
  4732. static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
  4733. {
  4734. u64 now, diff, diffms;
  4735. u32 count;
  4736. assert_spin_locked(&mchdev_lock);
  4737. now = ktime_get_raw_ns();
  4738. diffms = now - dev_priv->ips.last_time2;
  4739. do_div(diffms, NSEC_PER_MSEC);
  4740. /* Don't divide by 0 */
  4741. if (!diffms)
  4742. return;
  4743. count = I915_READ(GFXEC);
  4744. if (count < dev_priv->ips.last_count2) {
  4745. diff = ~0UL - dev_priv->ips.last_count2;
  4746. diff += count;
  4747. } else {
  4748. diff = count - dev_priv->ips.last_count2;
  4749. }
  4750. dev_priv->ips.last_count2 = count;
  4751. dev_priv->ips.last_time2 = now;
  4752. /* More magic constants... */
  4753. diff = diff * 1181;
  4754. diff = div_u64(diff, diffms * 10);
  4755. dev_priv->ips.gfx_power = diff;
  4756. }
  4757. void i915_update_gfx_val(struct drm_i915_private *dev_priv)
  4758. {
  4759. struct drm_device *dev = dev_priv->dev;
  4760. if (INTEL_INFO(dev)->gen != 5)
  4761. return;
  4762. spin_lock_irq(&mchdev_lock);
  4763. __i915_update_gfx_val(dev_priv);
  4764. spin_unlock_irq(&mchdev_lock);
  4765. }
  4766. static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
  4767. {
  4768. unsigned long t, corr, state1, corr2, state2;
  4769. u32 pxvid, ext_v;
  4770. assert_spin_locked(&mchdev_lock);
  4771. pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
  4772. pxvid = (pxvid >> 24) & 0x7f;
  4773. ext_v = pvid_to_extvid(dev_priv, pxvid);
  4774. state1 = ext_v;
  4775. t = i915_mch_val(dev_priv);
  4776. /* Revel in the empirically derived constants */
  4777. /* Correction factor in 1/100000 units */
  4778. if (t > 80)
  4779. corr = ((t * 2349) + 135940);
  4780. else if (t >= 50)
  4781. corr = ((t * 964) + 29317);
  4782. else /* < 50 */
  4783. corr = ((t * 301) + 1004);
  4784. corr = corr * ((150142 * state1) / 10000 - 78642);
  4785. corr /= 100000;
  4786. corr2 = (corr * dev_priv->ips.corr);
  4787. state2 = (corr2 * state1) / 10000;
  4788. state2 /= 100; /* convert to mW */
  4789. __i915_update_gfx_val(dev_priv);
  4790. return dev_priv->ips.gfx_power + state2;
  4791. }
  4792. unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
  4793. {
  4794. struct drm_device *dev = dev_priv->dev;
  4795. unsigned long val;
  4796. if (INTEL_INFO(dev)->gen != 5)
  4797. return 0;
  4798. spin_lock_irq(&mchdev_lock);
  4799. val = __i915_gfx_val(dev_priv);
  4800. spin_unlock_irq(&mchdev_lock);
  4801. return val;
  4802. }
  4803. /**
  4804. * i915_read_mch_val - return value for IPS use
  4805. *
  4806. * Calculate and return a value for the IPS driver to use when deciding whether
  4807. * we have thermal and power headroom to increase CPU or GPU power budget.
  4808. */
  4809. unsigned long i915_read_mch_val(void)
  4810. {
  4811. struct drm_i915_private *dev_priv;
  4812. unsigned long chipset_val, graphics_val, ret = 0;
  4813. spin_lock_irq(&mchdev_lock);
  4814. if (!i915_mch_dev)
  4815. goto out_unlock;
  4816. dev_priv = i915_mch_dev;
  4817. chipset_val = __i915_chipset_val(dev_priv);
  4818. graphics_val = __i915_gfx_val(dev_priv);
  4819. ret = chipset_val + graphics_val;
  4820. out_unlock:
  4821. spin_unlock_irq(&mchdev_lock);
  4822. return ret;
  4823. }
  4824. EXPORT_SYMBOL_GPL(i915_read_mch_val);
  4825. /**
  4826. * i915_gpu_raise - raise GPU frequency limit
  4827. *
  4828. * Raise the limit; IPS indicates we have thermal headroom.
  4829. */
  4830. bool i915_gpu_raise(void)
  4831. {
  4832. struct drm_i915_private *dev_priv;
  4833. bool ret = true;
  4834. spin_lock_irq(&mchdev_lock);
  4835. if (!i915_mch_dev) {
  4836. ret = false;
  4837. goto out_unlock;
  4838. }
  4839. dev_priv = i915_mch_dev;
  4840. if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
  4841. dev_priv->ips.max_delay--;
  4842. out_unlock:
  4843. spin_unlock_irq(&mchdev_lock);
  4844. return ret;
  4845. }
  4846. EXPORT_SYMBOL_GPL(i915_gpu_raise);
  4847. /**
  4848. * i915_gpu_lower - lower GPU frequency limit
  4849. *
  4850. * IPS indicates we're close to a thermal limit, so throttle back the GPU
  4851. * frequency maximum.
  4852. */
  4853. bool i915_gpu_lower(void)
  4854. {
  4855. struct drm_i915_private *dev_priv;
  4856. bool ret = true;
  4857. spin_lock_irq(&mchdev_lock);
  4858. if (!i915_mch_dev) {
  4859. ret = false;
  4860. goto out_unlock;
  4861. }
  4862. dev_priv = i915_mch_dev;
  4863. if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
  4864. dev_priv->ips.max_delay++;
  4865. out_unlock:
  4866. spin_unlock_irq(&mchdev_lock);
  4867. return ret;
  4868. }
  4869. EXPORT_SYMBOL_GPL(i915_gpu_lower);
  4870. /**
  4871. * i915_gpu_busy - indicate GPU business to IPS
  4872. *
  4873. * Tell the IPS driver whether or not the GPU is busy.
  4874. */
  4875. bool i915_gpu_busy(void)
  4876. {
  4877. struct drm_i915_private *dev_priv;
  4878. struct intel_engine_cs *ring;
  4879. bool ret = false;
  4880. int i;
  4881. spin_lock_irq(&mchdev_lock);
  4882. if (!i915_mch_dev)
  4883. goto out_unlock;
  4884. dev_priv = i915_mch_dev;
  4885. for_each_ring(ring, dev_priv, i)
  4886. ret |= !list_empty(&ring->request_list);
  4887. out_unlock:
  4888. spin_unlock_irq(&mchdev_lock);
  4889. return ret;
  4890. }
  4891. EXPORT_SYMBOL_GPL(i915_gpu_busy);
  4892. /**
  4893. * i915_gpu_turbo_disable - disable graphics turbo
  4894. *
  4895. * Disable graphics turbo by resetting the max frequency and setting the
  4896. * current frequency to the default.
  4897. */
  4898. bool i915_gpu_turbo_disable(void)
  4899. {
  4900. struct drm_i915_private *dev_priv;
  4901. bool ret = true;
  4902. spin_lock_irq(&mchdev_lock);
  4903. if (!i915_mch_dev) {
  4904. ret = false;
  4905. goto out_unlock;
  4906. }
  4907. dev_priv = i915_mch_dev;
  4908. dev_priv->ips.max_delay = dev_priv->ips.fstart;
  4909. if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
  4910. ret = false;
  4911. out_unlock:
  4912. spin_unlock_irq(&mchdev_lock);
  4913. return ret;
  4914. }
  4915. EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
  4916. /**
  4917. * Tells the intel_ips driver that the i915 driver is now loaded, if
  4918. * IPS got loaded first.
  4919. *
  4920. * This awkward dance is so that neither module has to depend on the
  4921. * other in order for IPS to do the appropriate communication of
  4922. * GPU turbo limits to i915.
  4923. */
  4924. static void
  4925. ips_ping_for_i915_load(void)
  4926. {
  4927. void (*link)(void);
  4928. link = symbol_get(ips_link_to_i915_driver);
  4929. if (link) {
  4930. link();
  4931. symbol_put(ips_link_to_i915_driver);
  4932. }
  4933. }
  4934. void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
  4935. {
  4936. /* We only register the i915 ips part with intel-ips once everything is
  4937. * set up, to avoid intel-ips sneaking in and reading bogus values. */
  4938. spin_lock_irq(&mchdev_lock);
  4939. i915_mch_dev = dev_priv;
  4940. spin_unlock_irq(&mchdev_lock);
  4941. ips_ping_for_i915_load();
  4942. }
  4943. void intel_gpu_ips_teardown(void)
  4944. {
  4945. spin_lock_irq(&mchdev_lock);
  4946. i915_mch_dev = NULL;
  4947. spin_unlock_irq(&mchdev_lock);
  4948. }
  4949. static void intel_init_emon(struct drm_device *dev)
  4950. {
  4951. struct drm_i915_private *dev_priv = dev->dev_private;
  4952. u32 lcfuse;
  4953. u8 pxw[16];
  4954. int i;
  4955. /* Disable to program */
  4956. I915_WRITE(ECR, 0);
  4957. POSTING_READ(ECR);
  4958. /* Program energy weights for various events */
  4959. I915_WRITE(SDEW, 0x15040d00);
  4960. I915_WRITE(CSIEW0, 0x007f0000);
  4961. I915_WRITE(CSIEW1, 0x1e220004);
  4962. I915_WRITE(CSIEW2, 0x04000004);
  4963. for (i = 0; i < 5; i++)
  4964. I915_WRITE(PEW + (i * 4), 0);
  4965. for (i = 0; i < 3; i++)
  4966. I915_WRITE(DEW + (i * 4), 0);
  4967. /* Program P-state weights to account for frequency power adjustment */
  4968. for (i = 0; i < 16; i++) {
  4969. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  4970. unsigned long freq = intel_pxfreq(pxvidfreq);
  4971. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  4972. PXVFREQ_PX_SHIFT;
  4973. unsigned long val;
  4974. val = vid * vid;
  4975. val *= (freq / 1000);
  4976. val *= 255;
  4977. val /= (127*127*900);
  4978. if (val > 0xff)
  4979. DRM_ERROR("bad pxval: %ld\n", val);
  4980. pxw[i] = val;
  4981. }
  4982. /* Render standby states get 0 weight */
  4983. pxw[14] = 0;
  4984. pxw[15] = 0;
  4985. for (i = 0; i < 4; i++) {
  4986. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  4987. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  4988. I915_WRITE(PXW + (i * 4), val);
  4989. }
  4990. /* Adjust magic regs to magic values (more experimental results) */
  4991. I915_WRITE(OGW0, 0);
  4992. I915_WRITE(OGW1, 0);
  4993. I915_WRITE(EG0, 0x00007f00);
  4994. I915_WRITE(EG1, 0x0000000e);
  4995. I915_WRITE(EG2, 0x000e0000);
  4996. I915_WRITE(EG3, 0x68000300);
  4997. I915_WRITE(EG4, 0x42000000);
  4998. I915_WRITE(EG5, 0x00140031);
  4999. I915_WRITE(EG6, 0);
  5000. I915_WRITE(EG7, 0);
  5001. for (i = 0; i < 8; i++)
  5002. I915_WRITE(PXWL + (i * 4), 0);
  5003. /* Enable PMON + select events */
  5004. I915_WRITE(ECR, 0x80000019);
  5005. lcfuse = I915_READ(LCFUSE02);
  5006. dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
  5007. }
  5008. void intel_init_gt_powersave(struct drm_device *dev)
  5009. {
  5010. i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
  5011. if (IS_CHERRYVIEW(dev))
  5012. cherryview_init_gt_powersave(dev);
  5013. else if (IS_VALLEYVIEW(dev))
  5014. valleyview_init_gt_powersave(dev);
  5015. }
  5016. void intel_cleanup_gt_powersave(struct drm_device *dev)
  5017. {
  5018. if (IS_CHERRYVIEW(dev))
  5019. return;
  5020. else if (IS_VALLEYVIEW(dev))
  5021. valleyview_cleanup_gt_powersave(dev);
  5022. }
  5023. /**
  5024. * intel_suspend_gt_powersave - suspend PM work and helper threads
  5025. * @dev: drm device
  5026. *
  5027. * We don't want to disable RC6 or other features here, we just want
  5028. * to make sure any work we've queued has finished and won't bother
  5029. * us while we're suspended.
  5030. */
  5031. void intel_suspend_gt_powersave(struct drm_device *dev)
  5032. {
  5033. struct drm_i915_private *dev_priv = dev->dev_private;
  5034. /* Interrupts should be disabled already to avoid re-arming. */
  5035. WARN_ON(intel_irqs_enabled(dev_priv));
  5036. flush_delayed_work(&dev_priv->rps.delayed_resume_work);
  5037. cancel_work_sync(&dev_priv->rps.work);
  5038. /* Force GPU to min freq during suspend */
  5039. gen6_rps_idle(dev_priv);
  5040. }
  5041. void intel_disable_gt_powersave(struct drm_device *dev)
  5042. {
  5043. struct drm_i915_private *dev_priv = dev->dev_private;
  5044. /* Interrupts should be disabled already to avoid re-arming. */
  5045. WARN_ON(intel_irqs_enabled(dev_priv));
  5046. if (IS_IRONLAKE_M(dev)) {
  5047. ironlake_disable_drps(dev);
  5048. ironlake_disable_rc6(dev);
  5049. } else if (INTEL_INFO(dev)->gen >= 6) {
  5050. intel_suspend_gt_powersave(dev);
  5051. mutex_lock(&dev_priv->rps.hw_lock);
  5052. if (IS_CHERRYVIEW(dev))
  5053. cherryview_disable_rps(dev);
  5054. else if (IS_VALLEYVIEW(dev))
  5055. valleyview_disable_rps(dev);
  5056. else
  5057. gen6_disable_rps(dev);
  5058. dev_priv->rps.enabled = false;
  5059. mutex_unlock(&dev_priv->rps.hw_lock);
  5060. }
  5061. }
  5062. static void intel_gen6_powersave_work(struct work_struct *work)
  5063. {
  5064. struct drm_i915_private *dev_priv =
  5065. container_of(work, struct drm_i915_private,
  5066. rps.delayed_resume_work.work);
  5067. struct drm_device *dev = dev_priv->dev;
  5068. mutex_lock(&dev_priv->rps.hw_lock);
  5069. if (IS_CHERRYVIEW(dev)) {
  5070. cherryview_enable_rps(dev);
  5071. } else if (IS_VALLEYVIEW(dev)) {
  5072. valleyview_enable_rps(dev);
  5073. } else if (IS_BROADWELL(dev)) {
  5074. gen8_enable_rps(dev);
  5075. __gen6_update_ring_freq(dev);
  5076. } else {
  5077. gen6_enable_rps(dev);
  5078. __gen6_update_ring_freq(dev);
  5079. }
  5080. dev_priv->rps.enabled = true;
  5081. mutex_unlock(&dev_priv->rps.hw_lock);
  5082. intel_runtime_pm_put(dev_priv);
  5083. }
  5084. void intel_enable_gt_powersave(struct drm_device *dev)
  5085. {
  5086. struct drm_i915_private *dev_priv = dev->dev_private;
  5087. if (IS_IRONLAKE_M(dev)) {
  5088. mutex_lock(&dev->struct_mutex);
  5089. ironlake_enable_drps(dev);
  5090. ironlake_enable_rc6(dev);
  5091. intel_init_emon(dev);
  5092. mutex_unlock(&dev->struct_mutex);
  5093. } else if (INTEL_INFO(dev)->gen >= 6) {
  5094. /*
  5095. * PCU communication is slow and this doesn't need to be
  5096. * done at any specific time, so do this out of our fast path
  5097. * to make resume and init faster.
  5098. *
  5099. * We depend on the HW RC6 power context save/restore
  5100. * mechanism when entering D3 through runtime PM suspend. So
  5101. * disable RPM until RPS/RC6 is properly setup. We can only
  5102. * get here via the driver load/system resume/runtime resume
  5103. * paths, so the _noresume version is enough (and in case of
  5104. * runtime resume it's necessary).
  5105. */
  5106. if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
  5107. round_jiffies_up_relative(HZ)))
  5108. intel_runtime_pm_get_noresume(dev_priv);
  5109. }
  5110. }
  5111. void intel_reset_gt_powersave(struct drm_device *dev)
  5112. {
  5113. struct drm_i915_private *dev_priv = dev->dev_private;
  5114. dev_priv->rps.enabled = false;
  5115. intel_enable_gt_powersave(dev);
  5116. }
  5117. static void ibx_init_clock_gating(struct drm_device *dev)
  5118. {
  5119. struct drm_i915_private *dev_priv = dev->dev_private;
  5120. /*
  5121. * On Ibex Peak and Cougar Point, we need to disable clock
  5122. * gating for the panel power sequencer or it will fail to
  5123. * start up when no ports are active.
  5124. */
  5125. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  5126. }
  5127. static void g4x_disable_trickle_feed(struct drm_device *dev)
  5128. {
  5129. struct drm_i915_private *dev_priv = dev->dev_private;
  5130. int pipe;
  5131. for_each_pipe(dev_priv, pipe) {
  5132. I915_WRITE(DSPCNTR(pipe),
  5133. I915_READ(DSPCNTR(pipe)) |
  5134. DISPPLANE_TRICKLE_FEED_DISABLE);
  5135. intel_flush_primary_plane(dev_priv, pipe);
  5136. }
  5137. }
  5138. static void ilk_init_lp_watermarks(struct drm_device *dev)
  5139. {
  5140. struct drm_i915_private *dev_priv = dev->dev_private;
  5141. I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
  5142. I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
  5143. I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
  5144. /*
  5145. * Don't touch WM1S_LP_EN here.
  5146. * Doing so could cause underruns.
  5147. */
  5148. }
  5149. static void ironlake_init_clock_gating(struct drm_device *dev)
  5150. {
  5151. struct drm_i915_private *dev_priv = dev->dev_private;
  5152. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  5153. /*
  5154. * Required for FBC
  5155. * WaFbcDisableDpfcClockGating:ilk
  5156. */
  5157. dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
  5158. ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
  5159. ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
  5160. I915_WRITE(PCH_3DCGDIS0,
  5161. MARIUNIT_CLOCK_GATE_DISABLE |
  5162. SVSMUNIT_CLOCK_GATE_DISABLE);
  5163. I915_WRITE(PCH_3DCGDIS1,
  5164. VFMUNIT_CLOCK_GATE_DISABLE);
  5165. /*
  5166. * According to the spec the following bits should be set in
  5167. * order to enable memory self-refresh
  5168. * The bit 22/21 of 0x42004
  5169. * The bit 5 of 0x42020
  5170. * The bit 15 of 0x45000
  5171. */
  5172. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5173. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  5174. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  5175. dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
  5176. I915_WRITE(DISP_ARB_CTL,
  5177. (I915_READ(DISP_ARB_CTL) |
  5178. DISP_FBC_WM_DIS));
  5179. ilk_init_lp_watermarks(dev);
  5180. /*
  5181. * Based on the document from hardware guys the following bits
  5182. * should be set unconditionally in order to enable FBC.
  5183. * The bit 22 of 0x42000
  5184. * The bit 22 of 0x42004
  5185. * The bit 7,8,9 of 0x42020.
  5186. */
  5187. if (IS_IRONLAKE_M(dev)) {
  5188. /* WaFbcAsynchFlipDisableFbcQueue:ilk */
  5189. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5190. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5191. ILK_FBCQ_DIS);
  5192. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5193. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5194. ILK_DPARB_GATE);
  5195. }
  5196. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  5197. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5198. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5199. ILK_ELPIN_409_SELECT);
  5200. I915_WRITE(_3D_CHICKEN2,
  5201. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  5202. _3D_CHICKEN2_WM_READ_PIPELINED);
  5203. /* WaDisableRenderCachePipelinedFlush:ilk */
  5204. I915_WRITE(CACHE_MODE_0,
  5205. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  5206. /* WaDisable_RenderCache_OperationalFlush:ilk */
  5207. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5208. g4x_disable_trickle_feed(dev);
  5209. ibx_init_clock_gating(dev);
  5210. }
  5211. static void cpt_init_clock_gating(struct drm_device *dev)
  5212. {
  5213. struct drm_i915_private *dev_priv = dev->dev_private;
  5214. int pipe;
  5215. uint32_t val;
  5216. /*
  5217. * On Ibex Peak and Cougar Point, we need to disable clock
  5218. * gating for the panel power sequencer or it will fail to
  5219. * start up when no ports are active.
  5220. */
  5221. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
  5222. PCH_DPLUNIT_CLOCK_GATE_DISABLE |
  5223. PCH_CPUNIT_CLOCK_GATE_DISABLE);
  5224. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  5225. DPLS_EDP_PPS_FIX_DIS);
  5226. /* The below fixes the weird display corruption, a few pixels shifted
  5227. * downward, on (only) LVDS of some HP laptops with IVY.
  5228. */
  5229. for_each_pipe(dev_priv, pipe) {
  5230. val = I915_READ(TRANS_CHICKEN2(pipe));
  5231. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  5232. val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  5233. if (dev_priv->vbt.fdi_rx_polarity_inverted)
  5234. val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  5235. val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
  5236. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
  5237. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
  5238. I915_WRITE(TRANS_CHICKEN2(pipe), val);
  5239. }
  5240. /* WADP0ClockGatingDisable */
  5241. for_each_pipe(dev_priv, pipe) {
  5242. I915_WRITE(TRANS_CHICKEN1(pipe),
  5243. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  5244. }
  5245. }
  5246. static void gen6_check_mch_setup(struct drm_device *dev)
  5247. {
  5248. struct drm_i915_private *dev_priv = dev->dev_private;
  5249. uint32_t tmp;
  5250. tmp = I915_READ(MCH_SSKPD);
  5251. if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
  5252. DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
  5253. tmp);
  5254. }
  5255. static void gen6_init_clock_gating(struct drm_device *dev)
  5256. {
  5257. struct drm_i915_private *dev_priv = dev->dev_private;
  5258. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  5259. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  5260. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5261. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5262. ILK_ELPIN_409_SELECT);
  5263. /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
  5264. I915_WRITE(_3D_CHICKEN,
  5265. _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
  5266. /* WaSetupGtModeTdRowDispatch:snb */
  5267. if (IS_SNB_GT1(dev))
  5268. I915_WRITE(GEN6_GT_MODE,
  5269. _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
  5270. /* WaDisable_RenderCache_OperationalFlush:snb */
  5271. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5272. /*
  5273. * BSpec recoomends 8x4 when MSAA is used,
  5274. * however in practice 16x4 seems fastest.
  5275. *
  5276. * Note that PS/WM thread counts depend on the WIZ hashing
  5277. * disable bit, which we don't touch here, but it's good
  5278. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  5279. */
  5280. I915_WRITE(GEN6_GT_MODE,
  5281. GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
  5282. ilk_init_lp_watermarks(dev);
  5283. I915_WRITE(CACHE_MODE_0,
  5284. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  5285. I915_WRITE(GEN6_UCGCTL1,
  5286. I915_READ(GEN6_UCGCTL1) |
  5287. GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
  5288. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  5289. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  5290. * gating disable must be set. Failure to set it results in
  5291. * flickering pixels due to Z write ordering failures after
  5292. * some amount of runtime in the Mesa "fire" demo, and Unigine
  5293. * Sanctuary and Tropics, and apparently anything else with
  5294. * alpha test or pixel discard.
  5295. *
  5296. * According to the spec, bit 11 (RCCUNIT) must also be set,
  5297. * but we didn't debug actual testcases to find it out.
  5298. *
  5299. * WaDisableRCCUnitClockGating:snb
  5300. * WaDisableRCPBUnitClockGating:snb
  5301. */
  5302. I915_WRITE(GEN6_UCGCTL2,
  5303. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  5304. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  5305. /* WaStripsFansDisableFastClipPerformanceFix:snb */
  5306. I915_WRITE(_3D_CHICKEN3,
  5307. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
  5308. /*
  5309. * Bspec says:
  5310. * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
  5311. * 3DSTATE_SF number of SF output attributes is more than 16."
  5312. */
  5313. I915_WRITE(_3D_CHICKEN3,
  5314. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
  5315. /*
  5316. * According to the spec the following bits should be
  5317. * set in order to enable memory self-refresh and fbc:
  5318. * The bit21 and bit22 of 0x42000
  5319. * The bit21 and bit22 of 0x42004
  5320. * The bit5 and bit7 of 0x42020
  5321. * The bit14 of 0x70180
  5322. * The bit14 of 0x71180
  5323. *
  5324. * WaFbcAsynchFlipDisableFbcQueue:snb
  5325. */
  5326. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5327. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5328. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  5329. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5330. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5331. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  5332. I915_WRITE(ILK_DSPCLK_GATE_D,
  5333. I915_READ(ILK_DSPCLK_GATE_D) |
  5334. ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
  5335. ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
  5336. g4x_disable_trickle_feed(dev);
  5337. cpt_init_clock_gating(dev);
  5338. gen6_check_mch_setup(dev);
  5339. }
  5340. static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
  5341. {
  5342. uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
  5343. /*
  5344. * WaVSThreadDispatchOverride:ivb,vlv
  5345. *
  5346. * This actually overrides the dispatch
  5347. * mode for all thread types.
  5348. */
  5349. reg &= ~GEN7_FF_SCHED_MASK;
  5350. reg |= GEN7_FF_TS_SCHED_HW;
  5351. reg |= GEN7_FF_VS_SCHED_HW;
  5352. reg |= GEN7_FF_DS_SCHED_HW;
  5353. I915_WRITE(GEN7_FF_THREAD_MODE, reg);
  5354. }
  5355. static void lpt_init_clock_gating(struct drm_device *dev)
  5356. {
  5357. struct drm_i915_private *dev_priv = dev->dev_private;
  5358. /*
  5359. * TODO: this bit should only be enabled when really needed, then
  5360. * disabled when not needed anymore in order to save power.
  5361. */
  5362. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
  5363. I915_WRITE(SOUTH_DSPCLK_GATE_D,
  5364. I915_READ(SOUTH_DSPCLK_GATE_D) |
  5365. PCH_LP_PARTITION_LEVEL_DISABLE);
  5366. /* WADPOClockGatingDisable:hsw */
  5367. I915_WRITE(_TRANSA_CHICKEN1,
  5368. I915_READ(_TRANSA_CHICKEN1) |
  5369. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  5370. }
  5371. static void lpt_suspend_hw(struct drm_device *dev)
  5372. {
  5373. struct drm_i915_private *dev_priv = dev->dev_private;
  5374. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  5375. uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
  5376. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  5377. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  5378. }
  5379. }
  5380. static void broadwell_init_clock_gating(struct drm_device *dev)
  5381. {
  5382. struct drm_i915_private *dev_priv = dev->dev_private;
  5383. enum pipe pipe;
  5384. I915_WRITE(WM3_LP_ILK, 0);
  5385. I915_WRITE(WM2_LP_ILK, 0);
  5386. I915_WRITE(WM1_LP_ILK, 0);
  5387. /* WaSwitchSolVfFArbitrationPriority:bdw */
  5388. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  5389. /* WaPsrDPAMaskVBlankInSRD:bdw */
  5390. I915_WRITE(CHICKEN_PAR1_1,
  5391. I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
  5392. /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
  5393. for_each_pipe(dev_priv, pipe) {
  5394. I915_WRITE(CHICKEN_PIPESL_1(pipe),
  5395. I915_READ(CHICKEN_PIPESL_1(pipe)) |
  5396. BDW_DPRS_MASK_VBLANK_SRD);
  5397. }
  5398. /* WaVSRefCountFullforceMissDisable:bdw */
  5399. /* WaDSRefCountFullforceMissDisable:bdw */
  5400. I915_WRITE(GEN7_FF_THREAD_MODE,
  5401. I915_READ(GEN7_FF_THREAD_MODE) &
  5402. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  5403. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  5404. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  5405. /* WaDisableSDEUnitClockGating:bdw */
  5406. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  5407. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  5408. lpt_init_clock_gating(dev);
  5409. }
  5410. static void haswell_init_clock_gating(struct drm_device *dev)
  5411. {
  5412. struct drm_i915_private *dev_priv = dev->dev_private;
  5413. ilk_init_lp_watermarks(dev);
  5414. /* L3 caching of data atomics doesn't work -- disable it. */
  5415. I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
  5416. I915_WRITE(HSW_ROW_CHICKEN3,
  5417. _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
  5418. /* This is required by WaCatErrorRejectionIssue:hsw */
  5419. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  5420. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  5421. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  5422. /* WaVSRefCountFullforceMissDisable:hsw */
  5423. I915_WRITE(GEN7_FF_THREAD_MODE,
  5424. I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
  5425. /* WaDisable_RenderCache_OperationalFlush:hsw */
  5426. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5427. /* enable HiZ Raw Stall Optimization */
  5428. I915_WRITE(CACHE_MODE_0_GEN7,
  5429. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  5430. /* WaDisable4x2SubspanOptimization:hsw */
  5431. I915_WRITE(CACHE_MODE_1,
  5432. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  5433. /*
  5434. * BSpec recommends 8x4 when MSAA is used,
  5435. * however in practice 16x4 seems fastest.
  5436. *
  5437. * Note that PS/WM thread counts depend on the WIZ hashing
  5438. * disable bit, which we don't touch here, but it's good
  5439. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  5440. */
  5441. I915_WRITE(GEN7_GT_MODE,
  5442. GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
  5443. /* WaSwitchSolVfFArbitrationPriority:hsw */
  5444. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  5445. /* WaRsPkgCStateDisplayPMReq:hsw */
  5446. I915_WRITE(CHICKEN_PAR1_1,
  5447. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  5448. lpt_init_clock_gating(dev);
  5449. }
  5450. static void ivybridge_init_clock_gating(struct drm_device *dev)
  5451. {
  5452. struct drm_i915_private *dev_priv = dev->dev_private;
  5453. uint32_t snpcr;
  5454. ilk_init_lp_watermarks(dev);
  5455. I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
  5456. /* WaDisableEarlyCull:ivb */
  5457. I915_WRITE(_3D_CHICKEN3,
  5458. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  5459. /* WaDisableBackToBackFlipFix:ivb */
  5460. I915_WRITE(IVB_CHICKEN3,
  5461. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  5462. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  5463. /* WaDisablePSDDualDispatchEnable:ivb */
  5464. if (IS_IVB_GT1(dev))
  5465. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  5466. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  5467. /* WaDisable_RenderCache_OperationalFlush:ivb */
  5468. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5469. /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
  5470. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  5471. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  5472. /* WaApplyL3ControlAndL3ChickenMode:ivb */
  5473. I915_WRITE(GEN7_L3CNTLREG1,
  5474. GEN7_WA_FOR_GEN7_L3_CONTROL);
  5475. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  5476. GEN7_WA_L3_CHICKEN_MODE);
  5477. if (IS_IVB_GT1(dev))
  5478. I915_WRITE(GEN7_ROW_CHICKEN2,
  5479. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  5480. else {
  5481. /* must write both registers */
  5482. I915_WRITE(GEN7_ROW_CHICKEN2,
  5483. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  5484. I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
  5485. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  5486. }
  5487. /* WaForceL3Serialization:ivb */
  5488. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  5489. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  5490. /*
  5491. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  5492. * This implements the WaDisableRCZUnitClockGating:ivb workaround.
  5493. */
  5494. I915_WRITE(GEN6_UCGCTL2,
  5495. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  5496. /* This is required by WaCatErrorRejectionIssue:ivb */
  5497. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  5498. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  5499. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  5500. g4x_disable_trickle_feed(dev);
  5501. gen7_setup_fixed_func_scheduler(dev_priv);
  5502. if (0) { /* causes HiZ corruption on ivb:gt1 */
  5503. /* enable HiZ Raw Stall Optimization */
  5504. I915_WRITE(CACHE_MODE_0_GEN7,
  5505. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  5506. }
  5507. /* WaDisable4x2SubspanOptimization:ivb */
  5508. I915_WRITE(CACHE_MODE_1,
  5509. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  5510. /*
  5511. * BSpec recommends 8x4 when MSAA is used,
  5512. * however in practice 16x4 seems fastest.
  5513. *
  5514. * Note that PS/WM thread counts depend on the WIZ hashing
  5515. * disable bit, which we don't touch here, but it's good
  5516. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  5517. */
  5518. I915_WRITE(GEN7_GT_MODE,
  5519. GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
  5520. snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
  5521. snpcr &= ~GEN6_MBC_SNPCR_MASK;
  5522. snpcr |= GEN6_MBC_SNPCR_MED;
  5523. I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
  5524. if (!HAS_PCH_NOP(dev))
  5525. cpt_init_clock_gating(dev);
  5526. gen6_check_mch_setup(dev);
  5527. }
  5528. static void valleyview_init_clock_gating(struct drm_device *dev)
  5529. {
  5530. struct drm_i915_private *dev_priv = dev->dev_private;
  5531. I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
  5532. /* WaDisableEarlyCull:vlv */
  5533. I915_WRITE(_3D_CHICKEN3,
  5534. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  5535. /* WaDisableBackToBackFlipFix:vlv */
  5536. I915_WRITE(IVB_CHICKEN3,
  5537. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  5538. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  5539. /* WaPsdDispatchEnable:vlv */
  5540. /* WaDisablePSDDualDispatchEnable:vlv */
  5541. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  5542. _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
  5543. GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  5544. /* WaDisable_RenderCache_OperationalFlush:vlv */
  5545. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5546. /* WaForceL3Serialization:vlv */
  5547. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  5548. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  5549. /* WaDisableDopClockGating:vlv */
  5550. I915_WRITE(GEN7_ROW_CHICKEN2,
  5551. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  5552. /* This is required by WaCatErrorRejectionIssue:vlv */
  5553. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  5554. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  5555. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  5556. gen7_setup_fixed_func_scheduler(dev_priv);
  5557. /*
  5558. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  5559. * This implements the WaDisableRCZUnitClockGating:vlv workaround.
  5560. */
  5561. I915_WRITE(GEN6_UCGCTL2,
  5562. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  5563. /* WaDisableL3Bank2xClockGate:vlv
  5564. * Disabling L3 clock gating- MMIO 940c[25] = 1
  5565. * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
  5566. I915_WRITE(GEN7_UCGCTL4,
  5567. I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
  5568. I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
  5569. /*
  5570. * BSpec says this must be set, even though
  5571. * WaDisable4x2SubspanOptimization isn't listed for VLV.
  5572. */
  5573. I915_WRITE(CACHE_MODE_1,
  5574. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  5575. /*
  5576. * WaIncreaseL3CreditsForVLVB0:vlv
  5577. * This is the hardware default actually.
  5578. */
  5579. I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
  5580. /*
  5581. * WaDisableVLVClockGating_VBIIssue:vlv
  5582. * Disable clock gating on th GCFG unit to prevent a delay
  5583. * in the reporting of vblank events.
  5584. */
  5585. I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
  5586. }
  5587. static void cherryview_init_clock_gating(struct drm_device *dev)
  5588. {
  5589. struct drm_i915_private *dev_priv = dev->dev_private;
  5590. I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
  5591. I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
  5592. /* WaVSRefCountFullforceMissDisable:chv */
  5593. /* WaDSRefCountFullforceMissDisable:chv */
  5594. I915_WRITE(GEN7_FF_THREAD_MODE,
  5595. I915_READ(GEN7_FF_THREAD_MODE) &
  5596. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  5597. /* WaDisableSemaphoreAndSyncFlipWait:chv */
  5598. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  5599. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  5600. /* WaDisableCSUnitClockGating:chv */
  5601. I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
  5602. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  5603. /* WaDisableSDEUnitClockGating:chv */
  5604. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  5605. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  5606. /* WaDisableGunitClockGating:chv (pre-production hw) */
  5607. I915_WRITE(VLV_GUNIT_CLOCK_GATE, I915_READ(VLV_GUNIT_CLOCK_GATE) |
  5608. GINT_DIS);
  5609. /* WaDisableFfDopClockGating:chv (pre-production hw) */
  5610. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  5611. _MASKED_BIT_ENABLE(GEN8_FF_DOP_CLOCK_GATE_DISABLE));
  5612. /* WaDisableDopClockGating:chv (pre-production hw) */
  5613. I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
  5614. GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
  5615. }
  5616. static void g4x_init_clock_gating(struct drm_device *dev)
  5617. {
  5618. struct drm_i915_private *dev_priv = dev->dev_private;
  5619. uint32_t dspclk_gate;
  5620. I915_WRITE(RENCLK_GATE_D1, 0);
  5621. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  5622. GS_UNIT_CLOCK_GATE_DISABLE |
  5623. CL_UNIT_CLOCK_GATE_DISABLE);
  5624. I915_WRITE(RAMCLK_GATE_D, 0);
  5625. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  5626. OVRUNIT_CLOCK_GATE_DISABLE |
  5627. OVCUNIT_CLOCK_GATE_DISABLE;
  5628. if (IS_GM45(dev))
  5629. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  5630. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  5631. /* WaDisableRenderCachePipelinedFlush */
  5632. I915_WRITE(CACHE_MODE_0,
  5633. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  5634. /* WaDisable_RenderCache_OperationalFlush:g4x */
  5635. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5636. g4x_disable_trickle_feed(dev);
  5637. }
  5638. static void crestline_init_clock_gating(struct drm_device *dev)
  5639. {
  5640. struct drm_i915_private *dev_priv = dev->dev_private;
  5641. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  5642. I915_WRITE(RENCLK_GATE_D2, 0);
  5643. I915_WRITE(DSPCLK_GATE_D, 0);
  5644. I915_WRITE(RAMCLK_GATE_D, 0);
  5645. I915_WRITE16(DEUC, 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 broadwater_init_clock_gating(struct drm_device *dev)
  5652. {
  5653. struct drm_i915_private *dev_priv = dev->dev_private;
  5654. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  5655. I965_RCC_CLOCK_GATE_DISABLE |
  5656. I965_RCPB_CLOCK_GATE_DISABLE |
  5657. I965_ISC_CLOCK_GATE_DISABLE |
  5658. I965_FBC_CLOCK_GATE_DISABLE);
  5659. I915_WRITE(RENCLK_GATE_D2, 0);
  5660. I915_WRITE(MI_ARB_STATE,
  5661. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  5662. /* WaDisable_RenderCache_OperationalFlush:gen4 */
  5663. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5664. }
  5665. static void gen3_init_clock_gating(struct drm_device *dev)
  5666. {
  5667. struct drm_i915_private *dev_priv = dev->dev_private;
  5668. u32 dstate = I915_READ(D_STATE);
  5669. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  5670. DSTATE_DOT_CLOCK_GATING;
  5671. I915_WRITE(D_STATE, dstate);
  5672. if (IS_PINEVIEW(dev))
  5673. I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
  5674. /* IIR "flip pending" means done if this bit is set */
  5675. I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
  5676. /* interrupts should cause a wake up from C3 */
  5677. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
  5678. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  5679. I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  5680. I915_WRITE(MI_ARB_STATE,
  5681. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  5682. }
  5683. static void i85x_init_clock_gating(struct drm_device *dev)
  5684. {
  5685. struct drm_i915_private *dev_priv = dev->dev_private;
  5686. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  5687. /* interrupts should cause a wake up from C3 */
  5688. I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
  5689. _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
  5690. I915_WRITE(MEM_MODE,
  5691. _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
  5692. }
  5693. static void i830_init_clock_gating(struct drm_device *dev)
  5694. {
  5695. struct drm_i915_private *dev_priv = dev->dev_private;
  5696. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  5697. I915_WRITE(MEM_MODE,
  5698. _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
  5699. _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
  5700. }
  5701. void intel_init_clock_gating(struct drm_device *dev)
  5702. {
  5703. struct drm_i915_private *dev_priv = dev->dev_private;
  5704. dev_priv->display.init_clock_gating(dev);
  5705. }
  5706. void intel_suspend_hw(struct drm_device *dev)
  5707. {
  5708. if (HAS_PCH_LPT(dev))
  5709. lpt_suspend_hw(dev);
  5710. }
  5711. static void intel_init_fbc(struct drm_i915_private *dev_priv)
  5712. {
  5713. if (!HAS_FBC(dev_priv)) {
  5714. dev_priv->fbc.enabled = false;
  5715. return;
  5716. }
  5717. if (INTEL_INFO(dev_priv)->gen >= 7) {
  5718. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  5719. dev_priv->display.enable_fbc = gen7_enable_fbc;
  5720. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  5721. } else if (INTEL_INFO(dev_priv)->gen >= 5) {
  5722. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  5723. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  5724. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  5725. } else if (IS_GM45(dev_priv)) {
  5726. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  5727. dev_priv->display.enable_fbc = g4x_enable_fbc;
  5728. dev_priv->display.disable_fbc = g4x_disable_fbc;
  5729. } else {
  5730. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  5731. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  5732. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  5733. /* This value was pulled out of someone's hat */
  5734. I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
  5735. }
  5736. dev_priv->fbc.enabled = dev_priv->display.fbc_enabled(dev_priv->dev);
  5737. }
  5738. /* Set up chip specific power management-related functions */
  5739. void intel_init_pm(struct drm_device *dev)
  5740. {
  5741. struct drm_i915_private *dev_priv = dev->dev_private;
  5742. intel_init_fbc(dev_priv);
  5743. /* For cxsr */
  5744. if (IS_PINEVIEW(dev))
  5745. i915_pineview_get_mem_freq(dev);
  5746. else if (IS_GEN5(dev))
  5747. i915_ironlake_get_mem_freq(dev);
  5748. /* For FIFO watermark updates */
  5749. if (IS_GEN9(dev)) {
  5750. skl_setup_wm_latency(dev);
  5751. dev_priv->display.init_clock_gating = gen9_init_clock_gating;
  5752. dev_priv->display.update_wm = skl_update_wm;
  5753. dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
  5754. } else if (HAS_PCH_SPLIT(dev)) {
  5755. ilk_setup_wm_latency(dev);
  5756. if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
  5757. dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
  5758. (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
  5759. dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
  5760. dev_priv->display.update_wm = ilk_update_wm;
  5761. dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
  5762. } else {
  5763. DRM_DEBUG_KMS("Failed to read display plane latency. "
  5764. "Disable CxSR\n");
  5765. }
  5766. if (IS_GEN5(dev))
  5767. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  5768. else if (IS_GEN6(dev))
  5769. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  5770. else if (IS_IVYBRIDGE(dev))
  5771. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  5772. else if (IS_HASWELL(dev))
  5773. dev_priv->display.init_clock_gating = haswell_init_clock_gating;
  5774. else if (INTEL_INFO(dev)->gen == 8)
  5775. dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
  5776. } else if (IS_CHERRYVIEW(dev)) {
  5777. dev_priv->display.update_wm = cherryview_update_wm;
  5778. dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
  5779. dev_priv->display.init_clock_gating =
  5780. cherryview_init_clock_gating;
  5781. } else if (IS_VALLEYVIEW(dev)) {
  5782. dev_priv->display.update_wm = valleyview_update_wm;
  5783. dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
  5784. dev_priv->display.init_clock_gating =
  5785. valleyview_init_clock_gating;
  5786. } else if (IS_PINEVIEW(dev)) {
  5787. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  5788. dev_priv->is_ddr3,
  5789. dev_priv->fsb_freq,
  5790. dev_priv->mem_freq)) {
  5791. DRM_INFO("failed to find known CxSR latency "
  5792. "(found ddr%s fsb freq %d, mem freq %d), "
  5793. "disabling CxSR\n",
  5794. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  5795. dev_priv->fsb_freq, dev_priv->mem_freq);
  5796. /* Disable CxSR and never update its watermark again */
  5797. intel_set_memory_cxsr(dev_priv, false);
  5798. dev_priv->display.update_wm = NULL;
  5799. } else
  5800. dev_priv->display.update_wm = pineview_update_wm;
  5801. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  5802. } else if (IS_G4X(dev)) {
  5803. dev_priv->display.update_wm = g4x_update_wm;
  5804. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  5805. } else if (IS_GEN4(dev)) {
  5806. dev_priv->display.update_wm = i965_update_wm;
  5807. if (IS_CRESTLINE(dev))
  5808. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  5809. else if (IS_BROADWATER(dev))
  5810. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  5811. } else if (IS_GEN3(dev)) {
  5812. dev_priv->display.update_wm = i9xx_update_wm;
  5813. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  5814. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  5815. } else if (IS_GEN2(dev)) {
  5816. if (INTEL_INFO(dev)->num_pipes == 1) {
  5817. dev_priv->display.update_wm = i845_update_wm;
  5818. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  5819. } else {
  5820. dev_priv->display.update_wm = i9xx_update_wm;
  5821. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  5822. }
  5823. if (IS_I85X(dev) || IS_I865G(dev))
  5824. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  5825. else
  5826. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  5827. } else {
  5828. DRM_ERROR("unexpected fall-through in intel_init_pm\n");
  5829. }
  5830. }
  5831. int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
  5832. {
  5833. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  5834. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  5835. DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
  5836. return -EAGAIN;
  5837. }
  5838. I915_WRITE(GEN6_PCODE_DATA, *val);
  5839. if (INTEL_INFO(dev_priv)->gen >= 9)
  5840. I915_WRITE(GEN9_PCODE_DATA1, 0);
  5841. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  5842. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5843. 500)) {
  5844. DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
  5845. return -ETIMEDOUT;
  5846. }
  5847. *val = I915_READ(GEN6_PCODE_DATA);
  5848. I915_WRITE(GEN6_PCODE_DATA, 0);
  5849. return 0;
  5850. }
  5851. int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
  5852. {
  5853. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  5854. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  5855. DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
  5856. return -EAGAIN;
  5857. }
  5858. I915_WRITE(GEN6_PCODE_DATA, val);
  5859. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  5860. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5861. 500)) {
  5862. DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
  5863. return -ETIMEDOUT;
  5864. }
  5865. I915_WRITE(GEN6_PCODE_DATA, 0);
  5866. return 0;
  5867. }
  5868. static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
  5869. {
  5870. int div;
  5871. /* 4 x czclk */
  5872. switch (dev_priv->mem_freq) {
  5873. case 800:
  5874. div = 10;
  5875. break;
  5876. case 1066:
  5877. div = 12;
  5878. break;
  5879. case 1333:
  5880. div = 16;
  5881. break;
  5882. default:
  5883. return -1;
  5884. }
  5885. return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
  5886. }
  5887. static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
  5888. {
  5889. int mul;
  5890. /* 4 x czclk */
  5891. switch (dev_priv->mem_freq) {
  5892. case 800:
  5893. mul = 10;
  5894. break;
  5895. case 1066:
  5896. mul = 12;
  5897. break;
  5898. case 1333:
  5899. mul = 16;
  5900. break;
  5901. default:
  5902. return -1;
  5903. }
  5904. return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
  5905. }
  5906. static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
  5907. {
  5908. int div, freq;
  5909. switch (dev_priv->rps.cz_freq) {
  5910. case 200:
  5911. div = 5;
  5912. break;
  5913. case 267:
  5914. div = 6;
  5915. break;
  5916. case 320:
  5917. case 333:
  5918. case 400:
  5919. div = 8;
  5920. break;
  5921. default:
  5922. return -1;
  5923. }
  5924. freq = (DIV_ROUND_CLOSEST((dev_priv->rps.cz_freq * val), 2 * div) / 2);
  5925. return freq;
  5926. }
  5927. static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
  5928. {
  5929. int mul, opcode;
  5930. switch (dev_priv->rps.cz_freq) {
  5931. case 200:
  5932. mul = 5;
  5933. break;
  5934. case 267:
  5935. mul = 6;
  5936. break;
  5937. case 320:
  5938. case 333:
  5939. case 400:
  5940. mul = 8;
  5941. break;
  5942. default:
  5943. return -1;
  5944. }
  5945. /* CHV needs even values */
  5946. opcode = (DIV_ROUND_CLOSEST((val * 2 * mul), dev_priv->rps.cz_freq) * 2);
  5947. return opcode;
  5948. }
  5949. int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
  5950. {
  5951. int ret = -1;
  5952. if (IS_CHERRYVIEW(dev_priv->dev))
  5953. ret = chv_gpu_freq(dev_priv, val);
  5954. else if (IS_VALLEYVIEW(dev_priv->dev))
  5955. ret = byt_gpu_freq(dev_priv, val);
  5956. return ret;
  5957. }
  5958. int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
  5959. {
  5960. int ret = -1;
  5961. if (IS_CHERRYVIEW(dev_priv->dev))
  5962. ret = chv_freq_opcode(dev_priv, val);
  5963. else if (IS_VALLEYVIEW(dev_priv->dev))
  5964. ret = byt_freq_opcode(dev_priv, val);
  5965. return ret;
  5966. }
  5967. void intel_pm_setup(struct drm_device *dev)
  5968. {
  5969. struct drm_i915_private *dev_priv = dev->dev_private;
  5970. mutex_init(&dev_priv->rps.hw_lock);
  5971. INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
  5972. intel_gen6_powersave_work);
  5973. dev_priv->pm.suspended = false;
  5974. }