intel_pm.c 276 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746
  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 <drm/drm_plane_helper.h>
  29. #include "i915_drv.h"
  30. #include "intel_drv.h"
  31. #include "../../../platform/x86/intel_ips.h"
  32. #include <linux/module.h>
  33. #include <drm/drm_atomic_helper.h>
  34. /**
  35. * DOC: RC6
  36. *
  37. * RC6 is a special power stage which allows the GPU to enter an very
  38. * low-voltage mode when idle, using down to 0V while at this stage. This
  39. * stage is entered automatically when the GPU is idle when RC6 support is
  40. * enabled, and as soon as new workload arises GPU wakes up automatically as well.
  41. *
  42. * There are different RC6 modes available in Intel GPU, which differentiate
  43. * among each other with the latency required to enter and leave RC6 and
  44. * voltage consumed by the GPU in different states.
  45. *
  46. * The combination of the following flags define which states GPU is allowed
  47. * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
  48. * RC6pp is deepest RC6. Their support by hardware varies according to the
  49. * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
  50. * which brings the most power savings; deeper states save more power, but
  51. * require higher latency to switch to and wake up.
  52. */
  53. static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
  54. {
  55. if (HAS_LLC(dev_priv)) {
  56. /*
  57. * WaCompressedResourceDisplayNewHashMode:skl,kbl
  58. * Display WA #0390: skl,kbl
  59. *
  60. * Must match Sampler, Pixel Back End, and Media. See
  61. * WaCompressedResourceSamplerPbeMediaNewHashMode.
  62. */
  63. I915_WRITE(CHICKEN_PAR1_1,
  64. I915_READ(CHICKEN_PAR1_1) |
  65. SKL_DE_COMPRESSED_HASH_MODE);
  66. }
  67. /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
  68. I915_WRITE(CHICKEN_PAR1_1,
  69. I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
  70. /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
  71. I915_WRITE(GEN8_CHICKEN_DCPR_1,
  72. I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
  73. /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
  74. /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
  75. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  76. DISP_FBC_WM_DIS |
  77. DISP_FBC_MEMORY_WAKE);
  78. /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
  79. I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
  80. ILK_DPFC_DISABLE_DUMMY0);
  81. if (IS_SKYLAKE(dev_priv)) {
  82. /* WaDisableDopClockGating */
  83. I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
  84. & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  85. }
  86. }
  87. static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
  88. {
  89. gen9_init_clock_gating(dev_priv);
  90. /* WaDisableSDEUnitClockGating:bxt */
  91. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  92. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  93. /*
  94. * FIXME:
  95. * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
  96. */
  97. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  98. GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
  99. /*
  100. * Wa: Backlight PWM may stop in the asserted state, causing backlight
  101. * to stay fully on.
  102. */
  103. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  104. PWM1_GATING_DIS | PWM2_GATING_DIS);
  105. }
  106. static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
  107. {
  108. gen9_init_clock_gating(dev_priv);
  109. /*
  110. * WaDisablePWMClockGating:glk
  111. * Backlight PWM may stop in the asserted state, causing backlight
  112. * to stay fully on.
  113. */
  114. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  115. PWM1_GATING_DIS | PWM2_GATING_DIS);
  116. /* WaDDIIOTimeout:glk */
  117. if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
  118. u32 val = I915_READ(CHICKEN_MISC_2);
  119. val &= ~(GLK_CL0_PWR_DOWN |
  120. GLK_CL1_PWR_DOWN |
  121. GLK_CL2_PWR_DOWN);
  122. I915_WRITE(CHICKEN_MISC_2, val);
  123. }
  124. }
  125. static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
  126. {
  127. u32 tmp;
  128. tmp = I915_READ(CLKCFG);
  129. switch (tmp & CLKCFG_FSB_MASK) {
  130. case CLKCFG_FSB_533:
  131. dev_priv->fsb_freq = 533; /* 133*4 */
  132. break;
  133. case CLKCFG_FSB_800:
  134. dev_priv->fsb_freq = 800; /* 200*4 */
  135. break;
  136. case CLKCFG_FSB_667:
  137. dev_priv->fsb_freq = 667; /* 167*4 */
  138. break;
  139. case CLKCFG_FSB_400:
  140. dev_priv->fsb_freq = 400; /* 100*4 */
  141. break;
  142. }
  143. switch (tmp & CLKCFG_MEM_MASK) {
  144. case CLKCFG_MEM_533:
  145. dev_priv->mem_freq = 533;
  146. break;
  147. case CLKCFG_MEM_667:
  148. dev_priv->mem_freq = 667;
  149. break;
  150. case CLKCFG_MEM_800:
  151. dev_priv->mem_freq = 800;
  152. break;
  153. }
  154. /* detect pineview DDR3 setting */
  155. tmp = I915_READ(CSHRDDR3CTL);
  156. dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
  157. }
  158. static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
  159. {
  160. u16 ddrpll, csipll;
  161. ddrpll = I915_READ16(DDRMPLL1);
  162. csipll = I915_READ16(CSIPLL0);
  163. switch (ddrpll & 0xff) {
  164. case 0xc:
  165. dev_priv->mem_freq = 800;
  166. break;
  167. case 0x10:
  168. dev_priv->mem_freq = 1066;
  169. break;
  170. case 0x14:
  171. dev_priv->mem_freq = 1333;
  172. break;
  173. case 0x18:
  174. dev_priv->mem_freq = 1600;
  175. break;
  176. default:
  177. DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
  178. ddrpll & 0xff);
  179. dev_priv->mem_freq = 0;
  180. break;
  181. }
  182. dev_priv->ips.r_t = dev_priv->mem_freq;
  183. switch (csipll & 0x3ff) {
  184. case 0x00c:
  185. dev_priv->fsb_freq = 3200;
  186. break;
  187. case 0x00e:
  188. dev_priv->fsb_freq = 3733;
  189. break;
  190. case 0x010:
  191. dev_priv->fsb_freq = 4266;
  192. break;
  193. case 0x012:
  194. dev_priv->fsb_freq = 4800;
  195. break;
  196. case 0x014:
  197. dev_priv->fsb_freq = 5333;
  198. break;
  199. case 0x016:
  200. dev_priv->fsb_freq = 5866;
  201. break;
  202. case 0x018:
  203. dev_priv->fsb_freq = 6400;
  204. break;
  205. default:
  206. DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
  207. csipll & 0x3ff);
  208. dev_priv->fsb_freq = 0;
  209. break;
  210. }
  211. if (dev_priv->fsb_freq == 3200) {
  212. dev_priv->ips.c_m = 0;
  213. } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
  214. dev_priv->ips.c_m = 1;
  215. } else {
  216. dev_priv->ips.c_m = 2;
  217. }
  218. }
  219. static const struct cxsr_latency cxsr_latency_table[] = {
  220. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  221. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  222. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  223. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  224. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  225. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  226. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  227. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  228. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  229. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  230. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  231. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  232. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  233. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  234. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  235. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  236. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  237. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  238. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  239. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  240. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  241. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  242. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  243. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  244. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  245. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  246. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  247. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  248. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  249. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  250. };
  251. static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
  252. bool is_ddr3,
  253. int fsb,
  254. int mem)
  255. {
  256. const struct cxsr_latency *latency;
  257. int i;
  258. if (fsb == 0 || mem == 0)
  259. return NULL;
  260. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  261. latency = &cxsr_latency_table[i];
  262. if (is_desktop == latency->is_desktop &&
  263. is_ddr3 == latency->is_ddr3 &&
  264. fsb == latency->fsb_freq && mem == latency->mem_freq)
  265. return latency;
  266. }
  267. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  268. return NULL;
  269. }
  270. static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
  271. {
  272. u32 val;
  273. mutex_lock(&dev_priv->pcu_lock);
  274. val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
  275. if (enable)
  276. val &= ~FORCE_DDR_HIGH_FREQ;
  277. else
  278. val |= FORCE_DDR_HIGH_FREQ;
  279. val &= ~FORCE_DDR_LOW_FREQ;
  280. val |= FORCE_DDR_FREQ_REQ_ACK;
  281. vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
  282. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
  283. FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
  284. DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
  285. mutex_unlock(&dev_priv->pcu_lock);
  286. }
  287. static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
  288. {
  289. u32 val;
  290. mutex_lock(&dev_priv->pcu_lock);
  291. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  292. if (enable)
  293. val |= DSP_MAXFIFO_PM5_ENABLE;
  294. else
  295. val &= ~DSP_MAXFIFO_PM5_ENABLE;
  296. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  297. mutex_unlock(&dev_priv->pcu_lock);
  298. }
  299. #define FW_WM(value, plane) \
  300. (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
  301. static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
  302. {
  303. bool was_enabled;
  304. u32 val;
  305. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  306. was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
  307. I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
  308. POSTING_READ(FW_BLC_SELF_VLV);
  309. } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
  310. was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
  311. I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
  312. POSTING_READ(FW_BLC_SELF);
  313. } else if (IS_PINEVIEW(dev_priv)) {
  314. val = I915_READ(DSPFW3);
  315. was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
  316. if (enable)
  317. val |= PINEVIEW_SELF_REFRESH_EN;
  318. else
  319. val &= ~PINEVIEW_SELF_REFRESH_EN;
  320. I915_WRITE(DSPFW3, val);
  321. POSTING_READ(DSPFW3);
  322. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
  323. was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
  324. val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
  325. _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
  326. I915_WRITE(FW_BLC_SELF, val);
  327. POSTING_READ(FW_BLC_SELF);
  328. } else if (IS_I915GM(dev_priv)) {
  329. /*
  330. * FIXME can't find a bit like this for 915G, and
  331. * and yet it does have the related watermark in
  332. * FW_BLC_SELF. What's going on?
  333. */
  334. was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
  335. val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
  336. _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
  337. I915_WRITE(INSTPM, val);
  338. POSTING_READ(INSTPM);
  339. } else {
  340. return false;
  341. }
  342. trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
  343. DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
  344. enableddisabled(enable),
  345. enableddisabled(was_enabled));
  346. return was_enabled;
  347. }
  348. /**
  349. * intel_set_memory_cxsr - Configure CxSR state
  350. * @dev_priv: i915 device
  351. * @enable: Allow vs. disallow CxSR
  352. *
  353. * Allow or disallow the system to enter a special CxSR
  354. * (C-state self refresh) state. What typically happens in CxSR mode
  355. * is that several display FIFOs may get combined into a single larger
  356. * FIFO for a particular plane (so called max FIFO mode) to allow the
  357. * system to defer memory fetches longer, and the memory will enter
  358. * self refresh.
  359. *
  360. * Note that enabling CxSR does not guarantee that the system enter
  361. * this special mode, nor does it guarantee that the system stays
  362. * in that mode once entered. So this just allows/disallows the system
  363. * to autonomously utilize the CxSR mode. Other factors such as core
  364. * C-states will affect when/if the system actually enters/exits the
  365. * CxSR mode.
  366. *
  367. * Note that on VLV/CHV this actually only controls the max FIFO mode,
  368. * and the system is free to enter/exit memory self refresh at any time
  369. * even when the use of CxSR has been disallowed.
  370. *
  371. * While the system is actually in the CxSR/max FIFO mode, some plane
  372. * control registers will not get latched on vblank. Thus in order to
  373. * guarantee the system will respond to changes in the plane registers
  374. * we must always disallow CxSR prior to making changes to those registers.
  375. * Unfortunately the system will re-evaluate the CxSR conditions at
  376. * frame start which happens after vblank start (which is when the plane
  377. * registers would get latched), so we can't proceed with the plane update
  378. * during the same frame where we disallowed CxSR.
  379. *
  380. * Certain platforms also have a deeper HPLL SR mode. Fortunately the
  381. * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
  382. * the hardware w.r.t. HPLL SR when writing to plane registers.
  383. * Disallowing just CxSR is sufficient.
  384. */
  385. bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
  386. {
  387. bool ret;
  388. mutex_lock(&dev_priv->wm.wm_mutex);
  389. ret = _intel_set_memory_cxsr(dev_priv, enable);
  390. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  391. dev_priv->wm.vlv.cxsr = enable;
  392. else if (IS_G4X(dev_priv))
  393. dev_priv->wm.g4x.cxsr = enable;
  394. mutex_unlock(&dev_priv->wm.wm_mutex);
  395. return ret;
  396. }
  397. /*
  398. * Latency for FIFO fetches is dependent on several factors:
  399. * - memory configuration (speed, channels)
  400. * - chipset
  401. * - current MCH state
  402. * It can be fairly high in some situations, so here we assume a fairly
  403. * pessimal value. It's a tradeoff between extra memory fetches (if we
  404. * set this value too high, the FIFO will fetch frequently to stay full)
  405. * and power consumption (set it too low to save power and we might see
  406. * FIFO underruns and display "flicker").
  407. *
  408. * A value of 5us seems to be a good balance; safe for very low end
  409. * platforms but not overly aggressive on lower latency configs.
  410. */
  411. static const int pessimal_latency_ns = 5000;
  412. #define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
  413. ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
  414. static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
  415. {
  416. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  417. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  418. struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
  419. enum pipe pipe = crtc->pipe;
  420. int sprite0_start, sprite1_start;
  421. switch (pipe) {
  422. uint32_t dsparb, dsparb2, dsparb3;
  423. case PIPE_A:
  424. dsparb = I915_READ(DSPARB);
  425. dsparb2 = I915_READ(DSPARB2);
  426. sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
  427. sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
  428. break;
  429. case PIPE_B:
  430. dsparb = I915_READ(DSPARB);
  431. dsparb2 = I915_READ(DSPARB2);
  432. sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
  433. sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
  434. break;
  435. case PIPE_C:
  436. dsparb2 = I915_READ(DSPARB2);
  437. dsparb3 = I915_READ(DSPARB3);
  438. sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
  439. sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
  440. break;
  441. default:
  442. MISSING_CASE(pipe);
  443. return;
  444. }
  445. fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
  446. fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
  447. fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
  448. fifo_state->plane[PLANE_CURSOR] = 63;
  449. }
  450. static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
  451. enum i9xx_plane_id i9xx_plane)
  452. {
  453. uint32_t dsparb = I915_READ(DSPARB);
  454. int size;
  455. size = dsparb & 0x7f;
  456. if (i9xx_plane == PLANE_B)
  457. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  458. DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
  459. dsparb, plane_name(i9xx_plane), size);
  460. return size;
  461. }
  462. static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
  463. enum i9xx_plane_id i9xx_plane)
  464. {
  465. uint32_t dsparb = I915_READ(DSPARB);
  466. int size;
  467. size = dsparb & 0x1ff;
  468. if (i9xx_plane == PLANE_B)
  469. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  470. size >>= 1; /* Convert to cachelines */
  471. DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
  472. dsparb, plane_name(i9xx_plane), size);
  473. return size;
  474. }
  475. static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
  476. enum i9xx_plane_id i9xx_plane)
  477. {
  478. uint32_t dsparb = I915_READ(DSPARB);
  479. int size;
  480. size = dsparb & 0x7f;
  481. size >>= 2; /* Convert to cachelines */
  482. DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
  483. dsparb, plane_name(i9xx_plane), size);
  484. return size;
  485. }
  486. /* Pineview has different values for various configs */
  487. static const struct intel_watermark_params pineview_display_wm = {
  488. .fifo_size = PINEVIEW_DISPLAY_FIFO,
  489. .max_wm = PINEVIEW_MAX_WM,
  490. .default_wm = PINEVIEW_DFT_WM,
  491. .guard_size = PINEVIEW_GUARD_WM,
  492. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  493. };
  494. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  495. .fifo_size = PINEVIEW_DISPLAY_FIFO,
  496. .max_wm = PINEVIEW_MAX_WM,
  497. .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
  498. .guard_size = PINEVIEW_GUARD_WM,
  499. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  500. };
  501. static const struct intel_watermark_params pineview_cursor_wm = {
  502. .fifo_size = PINEVIEW_CURSOR_FIFO,
  503. .max_wm = PINEVIEW_CURSOR_MAX_WM,
  504. .default_wm = PINEVIEW_CURSOR_DFT_WM,
  505. .guard_size = PINEVIEW_CURSOR_GUARD_WM,
  506. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  507. };
  508. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  509. .fifo_size = PINEVIEW_CURSOR_FIFO,
  510. .max_wm = PINEVIEW_CURSOR_MAX_WM,
  511. .default_wm = PINEVIEW_CURSOR_DFT_WM,
  512. .guard_size = PINEVIEW_CURSOR_GUARD_WM,
  513. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  514. };
  515. static const struct intel_watermark_params i965_cursor_wm_info = {
  516. .fifo_size = I965_CURSOR_FIFO,
  517. .max_wm = I965_CURSOR_MAX_WM,
  518. .default_wm = I965_CURSOR_DFT_WM,
  519. .guard_size = 2,
  520. .cacheline_size = I915_FIFO_LINE_SIZE,
  521. };
  522. static const struct intel_watermark_params i945_wm_info = {
  523. .fifo_size = I945_FIFO_SIZE,
  524. .max_wm = I915_MAX_WM,
  525. .default_wm = 1,
  526. .guard_size = 2,
  527. .cacheline_size = I915_FIFO_LINE_SIZE,
  528. };
  529. static const struct intel_watermark_params i915_wm_info = {
  530. .fifo_size = I915_FIFO_SIZE,
  531. .max_wm = I915_MAX_WM,
  532. .default_wm = 1,
  533. .guard_size = 2,
  534. .cacheline_size = I915_FIFO_LINE_SIZE,
  535. };
  536. static const struct intel_watermark_params i830_a_wm_info = {
  537. .fifo_size = I855GM_FIFO_SIZE,
  538. .max_wm = I915_MAX_WM,
  539. .default_wm = 1,
  540. .guard_size = 2,
  541. .cacheline_size = I830_FIFO_LINE_SIZE,
  542. };
  543. static const struct intel_watermark_params i830_bc_wm_info = {
  544. .fifo_size = I855GM_FIFO_SIZE,
  545. .max_wm = I915_MAX_WM/2,
  546. .default_wm = 1,
  547. .guard_size = 2,
  548. .cacheline_size = I830_FIFO_LINE_SIZE,
  549. };
  550. static const struct intel_watermark_params i845_wm_info = {
  551. .fifo_size = I830_FIFO_SIZE,
  552. .max_wm = I915_MAX_WM,
  553. .default_wm = 1,
  554. .guard_size = 2,
  555. .cacheline_size = I830_FIFO_LINE_SIZE,
  556. };
  557. /**
  558. * intel_wm_method1 - Method 1 / "small buffer" watermark formula
  559. * @pixel_rate: Pipe pixel rate in kHz
  560. * @cpp: Plane bytes per pixel
  561. * @latency: Memory wakeup latency in 0.1us units
  562. *
  563. * Compute the watermark using the method 1 or "small buffer"
  564. * formula. The caller may additonally add extra cachelines
  565. * to account for TLB misses and clock crossings.
  566. *
  567. * This method is concerned with the short term drain rate
  568. * of the FIFO, ie. it does not account for blanking periods
  569. * which would effectively reduce the average drain rate across
  570. * a longer period. The name "small" refers to the fact the
  571. * FIFO is relatively small compared to the amount of data
  572. * fetched.
  573. *
  574. * The FIFO level vs. time graph might look something like:
  575. *
  576. * |\ |\
  577. * | \ | \
  578. * __---__---__ (- plane active, _ blanking)
  579. * -> time
  580. *
  581. * or perhaps like this:
  582. *
  583. * |\|\ |\|\
  584. * __----__----__ (- plane active, _ blanking)
  585. * -> time
  586. *
  587. * Returns:
  588. * The watermark in bytes
  589. */
  590. static unsigned int intel_wm_method1(unsigned int pixel_rate,
  591. unsigned int cpp,
  592. unsigned int latency)
  593. {
  594. uint64_t ret;
  595. ret = (uint64_t) pixel_rate * cpp * latency;
  596. ret = DIV_ROUND_UP_ULL(ret, 10000);
  597. return ret;
  598. }
  599. /**
  600. * intel_wm_method2 - Method 2 / "large buffer" watermark formula
  601. * @pixel_rate: Pipe pixel rate in kHz
  602. * @htotal: Pipe horizontal total
  603. * @width: Plane width in pixels
  604. * @cpp: Plane bytes per pixel
  605. * @latency: Memory wakeup latency in 0.1us units
  606. *
  607. * Compute the watermark using the method 2 or "large buffer"
  608. * formula. The caller may additonally add extra cachelines
  609. * to account for TLB misses and clock crossings.
  610. *
  611. * This method is concerned with the long term drain rate
  612. * of the FIFO, ie. it does account for blanking periods
  613. * which effectively reduce the average drain rate across
  614. * a longer period. The name "large" refers to the fact the
  615. * FIFO is relatively large compared to the amount of data
  616. * fetched.
  617. *
  618. * The FIFO level vs. time graph might look something like:
  619. *
  620. * |\___ |\___
  621. * | \___ | \___
  622. * | \ | \
  623. * __ --__--__--__--__--__--__ (- plane active, _ blanking)
  624. * -> time
  625. *
  626. * Returns:
  627. * The watermark in bytes
  628. */
  629. static unsigned int intel_wm_method2(unsigned int pixel_rate,
  630. unsigned int htotal,
  631. unsigned int width,
  632. unsigned int cpp,
  633. unsigned int latency)
  634. {
  635. unsigned int ret;
  636. /*
  637. * FIXME remove once all users are computing
  638. * watermarks in the correct place.
  639. */
  640. if (WARN_ON_ONCE(htotal == 0))
  641. htotal = 1;
  642. ret = (latency * pixel_rate) / (htotal * 10000);
  643. ret = (ret + 1) * width * cpp;
  644. return ret;
  645. }
  646. /**
  647. * intel_calculate_wm - calculate watermark level
  648. * @pixel_rate: pixel clock
  649. * @wm: chip FIFO params
  650. * @fifo_size: size of the FIFO buffer
  651. * @cpp: bytes per pixel
  652. * @latency_ns: memory latency for the platform
  653. *
  654. * Calculate the watermark level (the level at which the display plane will
  655. * start fetching from memory again). Each chip has a different display
  656. * FIFO size and allocation, so the caller needs to figure that out and pass
  657. * in the correct intel_watermark_params structure.
  658. *
  659. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  660. * on the pixel size. When it reaches the watermark level, it'll start
  661. * fetching FIFO line sized based chunks from memory until the FIFO fills
  662. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  663. * will occur, and a display engine hang could result.
  664. */
  665. static unsigned int intel_calculate_wm(int pixel_rate,
  666. const struct intel_watermark_params *wm,
  667. int fifo_size, int cpp,
  668. unsigned int latency_ns)
  669. {
  670. int entries, wm_size;
  671. /*
  672. * Note: we need to make sure we don't overflow for various clock &
  673. * latency values.
  674. * clocks go from a few thousand to several hundred thousand.
  675. * latency is usually a few thousand
  676. */
  677. entries = intel_wm_method1(pixel_rate, cpp,
  678. latency_ns / 100);
  679. entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
  680. wm->guard_size;
  681. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
  682. wm_size = fifo_size - entries;
  683. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  684. /* Don't promote wm_size to unsigned... */
  685. if (wm_size > wm->max_wm)
  686. wm_size = wm->max_wm;
  687. if (wm_size <= 0)
  688. wm_size = wm->default_wm;
  689. /*
  690. * Bspec seems to indicate that the value shouldn't be lower than
  691. * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
  692. * Lets go for 8 which is the burst size since certain platforms
  693. * already use a hardcoded 8 (which is what the spec says should be
  694. * done).
  695. */
  696. if (wm_size <= 8)
  697. wm_size = 8;
  698. return wm_size;
  699. }
  700. static bool is_disabling(int old, int new, int threshold)
  701. {
  702. return old >= threshold && new < threshold;
  703. }
  704. static bool is_enabling(int old, int new, int threshold)
  705. {
  706. return old < threshold && new >= threshold;
  707. }
  708. static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
  709. {
  710. return dev_priv->wm.max_level + 1;
  711. }
  712. static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
  713. const struct intel_plane_state *plane_state)
  714. {
  715. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  716. /* FIXME check the 'enable' instead */
  717. if (!crtc_state->base.active)
  718. return false;
  719. /*
  720. * Treat cursor with fb as always visible since cursor updates
  721. * can happen faster than the vrefresh rate, and the current
  722. * watermark code doesn't handle that correctly. Cursor updates
  723. * which set/clear the fb or change the cursor size are going
  724. * to get throttled by intel_legacy_cursor_update() to work
  725. * around this problem with the watermark code.
  726. */
  727. if (plane->id == PLANE_CURSOR)
  728. return plane_state->base.fb != NULL;
  729. else
  730. return plane_state->base.visible;
  731. }
  732. static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
  733. {
  734. struct intel_crtc *crtc, *enabled = NULL;
  735. for_each_intel_crtc(&dev_priv->drm, crtc) {
  736. if (intel_crtc_active(crtc)) {
  737. if (enabled)
  738. return NULL;
  739. enabled = crtc;
  740. }
  741. }
  742. return enabled;
  743. }
  744. static void pineview_update_wm(struct intel_crtc *unused_crtc)
  745. {
  746. struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
  747. struct intel_crtc *crtc;
  748. const struct cxsr_latency *latency;
  749. u32 reg;
  750. unsigned int wm;
  751. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
  752. dev_priv->is_ddr3,
  753. dev_priv->fsb_freq,
  754. dev_priv->mem_freq);
  755. if (!latency) {
  756. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  757. intel_set_memory_cxsr(dev_priv, false);
  758. return;
  759. }
  760. crtc = single_enabled_crtc(dev_priv);
  761. if (crtc) {
  762. const struct drm_display_mode *adjusted_mode =
  763. &crtc->config->base.adjusted_mode;
  764. const struct drm_framebuffer *fb =
  765. crtc->base.primary->state->fb;
  766. int cpp = fb->format->cpp[0];
  767. int clock = adjusted_mode->crtc_clock;
  768. /* Display SR */
  769. wm = intel_calculate_wm(clock, &pineview_display_wm,
  770. pineview_display_wm.fifo_size,
  771. cpp, latency->display_sr);
  772. reg = I915_READ(DSPFW1);
  773. reg &= ~DSPFW_SR_MASK;
  774. reg |= FW_WM(wm, SR);
  775. I915_WRITE(DSPFW1, reg);
  776. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  777. /* cursor SR */
  778. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  779. pineview_display_wm.fifo_size,
  780. 4, latency->cursor_sr);
  781. reg = I915_READ(DSPFW3);
  782. reg &= ~DSPFW_CURSOR_SR_MASK;
  783. reg |= FW_WM(wm, CURSOR_SR);
  784. I915_WRITE(DSPFW3, reg);
  785. /* Display HPLL off SR */
  786. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  787. pineview_display_hplloff_wm.fifo_size,
  788. cpp, latency->display_hpll_disable);
  789. reg = I915_READ(DSPFW3);
  790. reg &= ~DSPFW_HPLL_SR_MASK;
  791. reg |= FW_WM(wm, HPLL_SR);
  792. I915_WRITE(DSPFW3, reg);
  793. /* cursor HPLL off SR */
  794. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  795. pineview_display_hplloff_wm.fifo_size,
  796. 4, latency->cursor_hpll_disable);
  797. reg = I915_READ(DSPFW3);
  798. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  799. reg |= FW_WM(wm, HPLL_CURSOR);
  800. I915_WRITE(DSPFW3, reg);
  801. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  802. intel_set_memory_cxsr(dev_priv, true);
  803. } else {
  804. intel_set_memory_cxsr(dev_priv, false);
  805. }
  806. }
  807. /*
  808. * Documentation says:
  809. * "If the line size is small, the TLB fetches can get in the way of the
  810. * data fetches, causing some lag in the pixel data return which is not
  811. * accounted for in the above formulas. The following adjustment only
  812. * needs to be applied if eight whole lines fit in the buffer at once.
  813. * The WM is adjusted upwards by the difference between the FIFO size
  814. * and the size of 8 whole lines. This adjustment is always performed
  815. * in the actual pixel depth regardless of whether FBC is enabled or not."
  816. */
  817. static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
  818. {
  819. int tlb_miss = fifo_size * 64 - width * cpp * 8;
  820. return max(0, tlb_miss);
  821. }
  822. static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
  823. const struct g4x_wm_values *wm)
  824. {
  825. enum pipe pipe;
  826. for_each_pipe(dev_priv, pipe)
  827. trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
  828. I915_WRITE(DSPFW1,
  829. FW_WM(wm->sr.plane, SR) |
  830. FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
  831. FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
  832. FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
  833. I915_WRITE(DSPFW2,
  834. (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
  835. FW_WM(wm->sr.fbc, FBC_SR) |
  836. FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
  837. FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
  838. FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
  839. FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
  840. I915_WRITE(DSPFW3,
  841. (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
  842. FW_WM(wm->sr.cursor, CURSOR_SR) |
  843. FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
  844. FW_WM(wm->hpll.plane, HPLL_SR));
  845. POSTING_READ(DSPFW1);
  846. }
  847. #define FW_WM_VLV(value, plane) \
  848. (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
  849. static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
  850. const struct vlv_wm_values *wm)
  851. {
  852. enum pipe pipe;
  853. for_each_pipe(dev_priv, pipe) {
  854. trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
  855. I915_WRITE(VLV_DDL(pipe),
  856. (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
  857. (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
  858. (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
  859. (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
  860. }
  861. /*
  862. * Zero the (unused) WM1 watermarks, and also clear all the
  863. * high order bits so that there are no out of bounds values
  864. * present in the registers during the reprogramming.
  865. */
  866. I915_WRITE(DSPHOWM, 0);
  867. I915_WRITE(DSPHOWM1, 0);
  868. I915_WRITE(DSPFW4, 0);
  869. I915_WRITE(DSPFW5, 0);
  870. I915_WRITE(DSPFW6, 0);
  871. I915_WRITE(DSPFW1,
  872. FW_WM(wm->sr.plane, SR) |
  873. FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
  874. FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
  875. FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
  876. I915_WRITE(DSPFW2,
  877. FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
  878. FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
  879. FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
  880. I915_WRITE(DSPFW3,
  881. FW_WM(wm->sr.cursor, CURSOR_SR));
  882. if (IS_CHERRYVIEW(dev_priv)) {
  883. I915_WRITE(DSPFW7_CHV,
  884. FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
  885. FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
  886. I915_WRITE(DSPFW8_CHV,
  887. FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
  888. FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
  889. I915_WRITE(DSPFW9_CHV,
  890. FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
  891. FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
  892. I915_WRITE(DSPHOWM,
  893. FW_WM(wm->sr.plane >> 9, SR_HI) |
  894. FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
  895. FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
  896. FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
  897. FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
  898. FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
  899. FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
  900. FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
  901. FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
  902. FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
  903. } else {
  904. I915_WRITE(DSPFW7,
  905. FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
  906. FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
  907. I915_WRITE(DSPHOWM,
  908. FW_WM(wm->sr.plane >> 9, SR_HI) |
  909. FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
  910. FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
  911. FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
  912. FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
  913. FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
  914. FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
  915. }
  916. POSTING_READ(DSPFW1);
  917. }
  918. #undef FW_WM_VLV
  919. static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
  920. {
  921. /* all latencies in usec */
  922. dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
  923. dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
  924. dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
  925. dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
  926. }
  927. static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
  928. {
  929. /*
  930. * DSPCNTR[13] supposedly controls whether the
  931. * primary plane can use the FIFO space otherwise
  932. * reserved for the sprite plane. It's not 100% clear
  933. * what the actual FIFO size is, but it looks like we
  934. * can happily set both primary and sprite watermarks
  935. * up to 127 cachelines. So that would seem to mean
  936. * that either DSPCNTR[13] doesn't do anything, or that
  937. * the total FIFO is >= 256 cachelines in size. Either
  938. * way, we don't seem to have to worry about this
  939. * repartitioning as the maximum watermark value the
  940. * register can hold for each plane is lower than the
  941. * minimum FIFO size.
  942. */
  943. switch (plane_id) {
  944. case PLANE_CURSOR:
  945. return 63;
  946. case PLANE_PRIMARY:
  947. return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
  948. case PLANE_SPRITE0:
  949. return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
  950. default:
  951. MISSING_CASE(plane_id);
  952. return 0;
  953. }
  954. }
  955. static int g4x_fbc_fifo_size(int level)
  956. {
  957. switch (level) {
  958. case G4X_WM_LEVEL_SR:
  959. return 7;
  960. case G4X_WM_LEVEL_HPLL:
  961. return 15;
  962. default:
  963. MISSING_CASE(level);
  964. return 0;
  965. }
  966. }
  967. static uint16_t g4x_compute_wm(const struct intel_crtc_state *crtc_state,
  968. const struct intel_plane_state *plane_state,
  969. int level)
  970. {
  971. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  972. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  973. const struct drm_display_mode *adjusted_mode =
  974. &crtc_state->base.adjusted_mode;
  975. unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
  976. unsigned int clock, htotal, cpp, width, wm;
  977. if (latency == 0)
  978. return USHRT_MAX;
  979. if (!intel_wm_plane_visible(crtc_state, plane_state))
  980. return 0;
  981. /*
  982. * Not 100% sure which way ELK should go here as the
  983. * spec only says CL/CTG should assume 32bpp and BW
  984. * doesn't need to. But as these things followed the
  985. * mobile vs. desktop lines on gen3 as well, let's
  986. * assume ELK doesn't need this.
  987. *
  988. * The spec also fails to list such a restriction for
  989. * the HPLL watermark, which seems a little strange.
  990. * Let's use 32bpp for the HPLL watermark as well.
  991. */
  992. if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
  993. level != G4X_WM_LEVEL_NORMAL)
  994. cpp = 4;
  995. else
  996. cpp = plane_state->base.fb->format->cpp[0];
  997. clock = adjusted_mode->crtc_clock;
  998. htotal = adjusted_mode->crtc_htotal;
  999. if (plane->id == PLANE_CURSOR)
  1000. width = plane_state->base.crtc_w;
  1001. else
  1002. width = drm_rect_width(&plane_state->base.dst);
  1003. if (plane->id == PLANE_CURSOR) {
  1004. wm = intel_wm_method2(clock, htotal, width, cpp, latency);
  1005. } else if (plane->id == PLANE_PRIMARY &&
  1006. level == G4X_WM_LEVEL_NORMAL) {
  1007. wm = intel_wm_method1(clock, cpp, latency);
  1008. } else {
  1009. unsigned int small, large;
  1010. small = intel_wm_method1(clock, cpp, latency);
  1011. large = intel_wm_method2(clock, htotal, width, cpp, latency);
  1012. wm = min(small, large);
  1013. }
  1014. wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
  1015. width, cpp);
  1016. wm = DIV_ROUND_UP(wm, 64) + 2;
  1017. return min_t(unsigned int, wm, USHRT_MAX);
  1018. }
  1019. static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
  1020. int level, enum plane_id plane_id, u16 value)
  1021. {
  1022. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1023. bool dirty = false;
  1024. for (; level < intel_wm_num_levels(dev_priv); level++) {
  1025. struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
  1026. dirty |= raw->plane[plane_id] != value;
  1027. raw->plane[plane_id] = value;
  1028. }
  1029. return dirty;
  1030. }
  1031. static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
  1032. int level, u16 value)
  1033. {
  1034. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1035. bool dirty = false;
  1036. /* NORMAL level doesn't have an FBC watermark */
  1037. level = max(level, G4X_WM_LEVEL_SR);
  1038. for (; level < intel_wm_num_levels(dev_priv); level++) {
  1039. struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
  1040. dirty |= raw->fbc != value;
  1041. raw->fbc = value;
  1042. }
  1043. return dirty;
  1044. }
  1045. static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
  1046. const struct intel_plane_state *pstate,
  1047. uint32_t pri_val);
  1048. static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
  1049. const struct intel_plane_state *plane_state)
  1050. {
  1051. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1052. int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
  1053. enum plane_id plane_id = plane->id;
  1054. bool dirty = false;
  1055. int level;
  1056. if (!intel_wm_plane_visible(crtc_state, plane_state)) {
  1057. dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
  1058. if (plane_id == PLANE_PRIMARY)
  1059. dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
  1060. goto out;
  1061. }
  1062. for (level = 0; level < num_levels; level++) {
  1063. struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
  1064. int wm, max_wm;
  1065. wm = g4x_compute_wm(crtc_state, plane_state, level);
  1066. max_wm = g4x_plane_fifo_size(plane_id, level);
  1067. if (wm > max_wm)
  1068. break;
  1069. dirty |= raw->plane[plane_id] != wm;
  1070. raw->plane[plane_id] = wm;
  1071. if (plane_id != PLANE_PRIMARY ||
  1072. level == G4X_WM_LEVEL_NORMAL)
  1073. continue;
  1074. wm = ilk_compute_fbc_wm(crtc_state, plane_state,
  1075. raw->plane[plane_id]);
  1076. max_wm = g4x_fbc_fifo_size(level);
  1077. /*
  1078. * FBC wm is not mandatory as we
  1079. * can always just disable its use.
  1080. */
  1081. if (wm > max_wm)
  1082. wm = USHRT_MAX;
  1083. dirty |= raw->fbc != wm;
  1084. raw->fbc = wm;
  1085. }
  1086. /* mark watermarks as invalid */
  1087. dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
  1088. if (plane_id == PLANE_PRIMARY)
  1089. dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
  1090. out:
  1091. if (dirty) {
  1092. DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
  1093. plane->base.name,
  1094. crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
  1095. crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
  1096. crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
  1097. if (plane_id == PLANE_PRIMARY)
  1098. DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
  1099. crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
  1100. crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
  1101. }
  1102. return dirty;
  1103. }
  1104. static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
  1105. enum plane_id plane_id, int level)
  1106. {
  1107. const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
  1108. return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
  1109. }
  1110. static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
  1111. int level)
  1112. {
  1113. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1114. if (level > dev_priv->wm.max_level)
  1115. return false;
  1116. return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
  1117. g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
  1118. g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
  1119. }
  1120. /* mark all levels starting from 'level' as invalid */
  1121. static void g4x_invalidate_wms(struct intel_crtc *crtc,
  1122. struct g4x_wm_state *wm_state, int level)
  1123. {
  1124. if (level <= G4X_WM_LEVEL_NORMAL) {
  1125. enum plane_id plane_id;
  1126. for_each_plane_id_on_crtc(crtc, plane_id)
  1127. wm_state->wm.plane[plane_id] = USHRT_MAX;
  1128. }
  1129. if (level <= G4X_WM_LEVEL_SR) {
  1130. wm_state->cxsr = false;
  1131. wm_state->sr.cursor = USHRT_MAX;
  1132. wm_state->sr.plane = USHRT_MAX;
  1133. wm_state->sr.fbc = USHRT_MAX;
  1134. }
  1135. if (level <= G4X_WM_LEVEL_HPLL) {
  1136. wm_state->hpll_en = false;
  1137. wm_state->hpll.cursor = USHRT_MAX;
  1138. wm_state->hpll.plane = USHRT_MAX;
  1139. wm_state->hpll.fbc = USHRT_MAX;
  1140. }
  1141. }
  1142. static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
  1143. {
  1144. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1145. struct intel_atomic_state *state =
  1146. to_intel_atomic_state(crtc_state->base.state);
  1147. struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
  1148. int num_active_planes = hweight32(crtc_state->active_planes &
  1149. ~BIT(PLANE_CURSOR));
  1150. const struct g4x_pipe_wm *raw;
  1151. const struct intel_plane_state *old_plane_state;
  1152. const struct intel_plane_state *new_plane_state;
  1153. struct intel_plane *plane;
  1154. enum plane_id plane_id;
  1155. int i, level;
  1156. unsigned int dirty = 0;
  1157. for_each_oldnew_intel_plane_in_state(state, plane,
  1158. old_plane_state,
  1159. new_plane_state, i) {
  1160. if (new_plane_state->base.crtc != &crtc->base &&
  1161. old_plane_state->base.crtc != &crtc->base)
  1162. continue;
  1163. if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
  1164. dirty |= BIT(plane->id);
  1165. }
  1166. if (!dirty)
  1167. return 0;
  1168. level = G4X_WM_LEVEL_NORMAL;
  1169. if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
  1170. goto out;
  1171. raw = &crtc_state->wm.g4x.raw[level];
  1172. for_each_plane_id_on_crtc(crtc, plane_id)
  1173. wm_state->wm.plane[plane_id] = raw->plane[plane_id];
  1174. level = G4X_WM_LEVEL_SR;
  1175. if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
  1176. goto out;
  1177. raw = &crtc_state->wm.g4x.raw[level];
  1178. wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
  1179. wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
  1180. wm_state->sr.fbc = raw->fbc;
  1181. wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
  1182. level = G4X_WM_LEVEL_HPLL;
  1183. if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
  1184. goto out;
  1185. raw = &crtc_state->wm.g4x.raw[level];
  1186. wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
  1187. wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
  1188. wm_state->hpll.fbc = raw->fbc;
  1189. wm_state->hpll_en = wm_state->cxsr;
  1190. level++;
  1191. out:
  1192. if (level == G4X_WM_LEVEL_NORMAL)
  1193. return -EINVAL;
  1194. /* invalidate the higher levels */
  1195. g4x_invalidate_wms(crtc, wm_state, level);
  1196. /*
  1197. * Determine if the FBC watermark(s) can be used. IF
  1198. * this isn't the case we prefer to disable the FBC
  1199. ( watermark(s) rather than disable the SR/HPLL
  1200. * level(s) entirely.
  1201. */
  1202. wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
  1203. if (level >= G4X_WM_LEVEL_SR &&
  1204. wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
  1205. wm_state->fbc_en = false;
  1206. else if (level >= G4X_WM_LEVEL_HPLL &&
  1207. wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
  1208. wm_state->fbc_en = false;
  1209. return 0;
  1210. }
  1211. static int g4x_compute_intermediate_wm(struct drm_device *dev,
  1212. struct intel_crtc *crtc,
  1213. struct intel_crtc_state *new_crtc_state)
  1214. {
  1215. struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
  1216. const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
  1217. struct intel_atomic_state *intel_state =
  1218. to_intel_atomic_state(new_crtc_state->base.state);
  1219. const struct intel_crtc_state *old_crtc_state =
  1220. intel_atomic_get_old_crtc_state(intel_state, crtc);
  1221. const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
  1222. enum plane_id plane_id;
  1223. if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
  1224. *intermediate = *optimal;
  1225. intermediate->cxsr = false;
  1226. intermediate->hpll_en = false;
  1227. goto out;
  1228. }
  1229. intermediate->cxsr = optimal->cxsr && active->cxsr &&
  1230. !new_crtc_state->disable_cxsr;
  1231. intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
  1232. !new_crtc_state->disable_cxsr;
  1233. intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
  1234. for_each_plane_id_on_crtc(crtc, plane_id) {
  1235. intermediate->wm.plane[plane_id] =
  1236. max(optimal->wm.plane[plane_id],
  1237. active->wm.plane[plane_id]);
  1238. WARN_ON(intermediate->wm.plane[plane_id] >
  1239. g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
  1240. }
  1241. intermediate->sr.plane = max(optimal->sr.plane,
  1242. active->sr.plane);
  1243. intermediate->sr.cursor = max(optimal->sr.cursor,
  1244. active->sr.cursor);
  1245. intermediate->sr.fbc = max(optimal->sr.fbc,
  1246. active->sr.fbc);
  1247. intermediate->hpll.plane = max(optimal->hpll.plane,
  1248. active->hpll.plane);
  1249. intermediate->hpll.cursor = max(optimal->hpll.cursor,
  1250. active->hpll.cursor);
  1251. intermediate->hpll.fbc = max(optimal->hpll.fbc,
  1252. active->hpll.fbc);
  1253. WARN_ON((intermediate->sr.plane >
  1254. g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
  1255. intermediate->sr.cursor >
  1256. g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
  1257. intermediate->cxsr);
  1258. WARN_ON((intermediate->sr.plane >
  1259. g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
  1260. intermediate->sr.cursor >
  1261. g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
  1262. intermediate->hpll_en);
  1263. WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
  1264. intermediate->fbc_en && intermediate->cxsr);
  1265. WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
  1266. intermediate->fbc_en && intermediate->hpll_en);
  1267. out:
  1268. /*
  1269. * If our intermediate WM are identical to the final WM, then we can
  1270. * omit the post-vblank programming; only update if it's different.
  1271. */
  1272. if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
  1273. new_crtc_state->wm.need_postvbl_update = true;
  1274. return 0;
  1275. }
  1276. static void g4x_merge_wm(struct drm_i915_private *dev_priv,
  1277. struct g4x_wm_values *wm)
  1278. {
  1279. struct intel_crtc *crtc;
  1280. int num_active_crtcs = 0;
  1281. wm->cxsr = true;
  1282. wm->hpll_en = true;
  1283. wm->fbc_en = true;
  1284. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1285. const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
  1286. if (!crtc->active)
  1287. continue;
  1288. if (!wm_state->cxsr)
  1289. wm->cxsr = false;
  1290. if (!wm_state->hpll_en)
  1291. wm->hpll_en = false;
  1292. if (!wm_state->fbc_en)
  1293. wm->fbc_en = false;
  1294. num_active_crtcs++;
  1295. }
  1296. if (num_active_crtcs != 1) {
  1297. wm->cxsr = false;
  1298. wm->hpll_en = false;
  1299. wm->fbc_en = false;
  1300. }
  1301. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1302. const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
  1303. enum pipe pipe = crtc->pipe;
  1304. wm->pipe[pipe] = wm_state->wm;
  1305. if (crtc->active && wm->cxsr)
  1306. wm->sr = wm_state->sr;
  1307. if (crtc->active && wm->hpll_en)
  1308. wm->hpll = wm_state->hpll;
  1309. }
  1310. }
  1311. static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
  1312. {
  1313. struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
  1314. struct g4x_wm_values new_wm = {};
  1315. g4x_merge_wm(dev_priv, &new_wm);
  1316. if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
  1317. return;
  1318. if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
  1319. _intel_set_memory_cxsr(dev_priv, false);
  1320. g4x_write_wm_values(dev_priv, &new_wm);
  1321. if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
  1322. _intel_set_memory_cxsr(dev_priv, true);
  1323. *old_wm = new_wm;
  1324. }
  1325. static void g4x_initial_watermarks(struct intel_atomic_state *state,
  1326. struct intel_crtc_state *crtc_state)
  1327. {
  1328. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1329. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1330. mutex_lock(&dev_priv->wm.wm_mutex);
  1331. crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
  1332. g4x_program_watermarks(dev_priv);
  1333. mutex_unlock(&dev_priv->wm.wm_mutex);
  1334. }
  1335. static void g4x_optimize_watermarks(struct intel_atomic_state *state,
  1336. struct intel_crtc_state *crtc_state)
  1337. {
  1338. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1339. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  1340. if (!crtc_state->wm.need_postvbl_update)
  1341. return;
  1342. mutex_lock(&dev_priv->wm.wm_mutex);
  1343. intel_crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
  1344. g4x_program_watermarks(dev_priv);
  1345. mutex_unlock(&dev_priv->wm.wm_mutex);
  1346. }
  1347. /* latency must be in 0.1us units. */
  1348. static unsigned int vlv_wm_method2(unsigned int pixel_rate,
  1349. unsigned int htotal,
  1350. unsigned int width,
  1351. unsigned int cpp,
  1352. unsigned int latency)
  1353. {
  1354. unsigned int ret;
  1355. ret = intel_wm_method2(pixel_rate, htotal,
  1356. width, cpp, latency);
  1357. ret = DIV_ROUND_UP(ret, 64);
  1358. return ret;
  1359. }
  1360. static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
  1361. {
  1362. /* all latencies in usec */
  1363. dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
  1364. dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
  1365. if (IS_CHERRYVIEW(dev_priv)) {
  1366. dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
  1367. dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
  1368. dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
  1369. }
  1370. }
  1371. static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
  1372. const struct intel_plane_state *plane_state,
  1373. int level)
  1374. {
  1375. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1376. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  1377. const struct drm_display_mode *adjusted_mode =
  1378. &crtc_state->base.adjusted_mode;
  1379. unsigned int clock, htotal, cpp, width, wm;
  1380. if (dev_priv->wm.pri_latency[level] == 0)
  1381. return USHRT_MAX;
  1382. if (!intel_wm_plane_visible(crtc_state, plane_state))
  1383. return 0;
  1384. cpp = plane_state->base.fb->format->cpp[0];
  1385. clock = adjusted_mode->crtc_clock;
  1386. htotal = adjusted_mode->crtc_htotal;
  1387. width = crtc_state->pipe_src_w;
  1388. if (plane->id == PLANE_CURSOR) {
  1389. /*
  1390. * FIXME the formula gives values that are
  1391. * too big for the cursor FIFO, and hence we
  1392. * would never be able to use cursors. For
  1393. * now just hardcode the watermark.
  1394. */
  1395. wm = 63;
  1396. } else {
  1397. wm = vlv_wm_method2(clock, htotal, width, cpp,
  1398. dev_priv->wm.pri_latency[level] * 10);
  1399. }
  1400. return min_t(unsigned int, wm, USHRT_MAX);
  1401. }
  1402. static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
  1403. {
  1404. return (active_planes & (BIT(PLANE_SPRITE0) |
  1405. BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
  1406. }
  1407. static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
  1408. {
  1409. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1410. const struct g4x_pipe_wm *raw =
  1411. &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
  1412. struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
  1413. unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
  1414. int num_active_planes = hweight32(active_planes);
  1415. const int fifo_size = 511;
  1416. int fifo_extra, fifo_left = fifo_size;
  1417. int sprite0_fifo_extra = 0;
  1418. unsigned int total_rate;
  1419. enum plane_id plane_id;
  1420. /*
  1421. * When enabling sprite0 after sprite1 has already been enabled
  1422. * we tend to get an underrun unless sprite0 already has some
  1423. * FIFO space allcoated. Hence we always allocate at least one
  1424. * cacheline for sprite0 whenever sprite1 is enabled.
  1425. *
  1426. * All other plane enable sequences appear immune to this problem.
  1427. */
  1428. if (vlv_need_sprite0_fifo_workaround(active_planes))
  1429. sprite0_fifo_extra = 1;
  1430. total_rate = raw->plane[PLANE_PRIMARY] +
  1431. raw->plane[PLANE_SPRITE0] +
  1432. raw->plane[PLANE_SPRITE1] +
  1433. sprite0_fifo_extra;
  1434. if (total_rate > fifo_size)
  1435. return -EINVAL;
  1436. if (total_rate == 0)
  1437. total_rate = 1;
  1438. for_each_plane_id_on_crtc(crtc, plane_id) {
  1439. unsigned int rate;
  1440. if ((active_planes & BIT(plane_id)) == 0) {
  1441. fifo_state->plane[plane_id] = 0;
  1442. continue;
  1443. }
  1444. rate = raw->plane[plane_id];
  1445. fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
  1446. fifo_left -= fifo_state->plane[plane_id];
  1447. }
  1448. fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
  1449. fifo_left -= sprite0_fifo_extra;
  1450. fifo_state->plane[PLANE_CURSOR] = 63;
  1451. fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
  1452. /* spread the remainder evenly */
  1453. for_each_plane_id_on_crtc(crtc, plane_id) {
  1454. int plane_extra;
  1455. if (fifo_left == 0)
  1456. break;
  1457. if ((active_planes & BIT(plane_id)) == 0)
  1458. continue;
  1459. plane_extra = min(fifo_extra, fifo_left);
  1460. fifo_state->plane[plane_id] += plane_extra;
  1461. fifo_left -= plane_extra;
  1462. }
  1463. WARN_ON(active_planes != 0 && fifo_left != 0);
  1464. /* give it all to the first plane if none are active */
  1465. if (active_planes == 0) {
  1466. WARN_ON(fifo_left != fifo_size);
  1467. fifo_state->plane[PLANE_PRIMARY] = fifo_left;
  1468. }
  1469. return 0;
  1470. }
  1471. /* mark all levels starting from 'level' as invalid */
  1472. static void vlv_invalidate_wms(struct intel_crtc *crtc,
  1473. struct vlv_wm_state *wm_state, int level)
  1474. {
  1475. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1476. for (; level < intel_wm_num_levels(dev_priv); level++) {
  1477. enum plane_id plane_id;
  1478. for_each_plane_id_on_crtc(crtc, plane_id)
  1479. wm_state->wm[level].plane[plane_id] = USHRT_MAX;
  1480. wm_state->sr[level].cursor = USHRT_MAX;
  1481. wm_state->sr[level].plane = USHRT_MAX;
  1482. }
  1483. }
  1484. static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
  1485. {
  1486. if (wm > fifo_size)
  1487. return USHRT_MAX;
  1488. else
  1489. return fifo_size - wm;
  1490. }
  1491. /*
  1492. * Starting from 'level' set all higher
  1493. * levels to 'value' in the "raw" watermarks.
  1494. */
  1495. static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
  1496. int level, enum plane_id plane_id, u16 value)
  1497. {
  1498. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1499. int num_levels = intel_wm_num_levels(dev_priv);
  1500. bool dirty = false;
  1501. for (; level < num_levels; level++) {
  1502. struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
  1503. dirty |= raw->plane[plane_id] != value;
  1504. raw->plane[plane_id] = value;
  1505. }
  1506. return dirty;
  1507. }
  1508. static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
  1509. const struct intel_plane_state *plane_state)
  1510. {
  1511. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1512. enum plane_id plane_id = plane->id;
  1513. int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
  1514. int level;
  1515. bool dirty = false;
  1516. if (!intel_wm_plane_visible(crtc_state, plane_state)) {
  1517. dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
  1518. goto out;
  1519. }
  1520. for (level = 0; level < num_levels; level++) {
  1521. struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
  1522. int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
  1523. int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
  1524. if (wm > max_wm)
  1525. break;
  1526. dirty |= raw->plane[plane_id] != wm;
  1527. raw->plane[plane_id] = wm;
  1528. }
  1529. /* mark all higher levels as invalid */
  1530. dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
  1531. out:
  1532. if (dirty)
  1533. DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
  1534. plane->base.name,
  1535. crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
  1536. crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
  1537. crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
  1538. return dirty;
  1539. }
  1540. static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
  1541. enum plane_id plane_id, int level)
  1542. {
  1543. const struct g4x_pipe_wm *raw =
  1544. &crtc_state->wm.vlv.raw[level];
  1545. const struct vlv_fifo_state *fifo_state =
  1546. &crtc_state->wm.vlv.fifo_state;
  1547. return raw->plane[plane_id] <= fifo_state->plane[plane_id];
  1548. }
  1549. static bool vlv_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
  1550. {
  1551. return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
  1552. vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
  1553. vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
  1554. vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
  1555. }
  1556. static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
  1557. {
  1558. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1559. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1560. struct intel_atomic_state *state =
  1561. to_intel_atomic_state(crtc_state->base.state);
  1562. struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
  1563. const struct vlv_fifo_state *fifo_state =
  1564. &crtc_state->wm.vlv.fifo_state;
  1565. int num_active_planes = hweight32(crtc_state->active_planes &
  1566. ~BIT(PLANE_CURSOR));
  1567. bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
  1568. const struct intel_plane_state *old_plane_state;
  1569. const struct intel_plane_state *new_plane_state;
  1570. struct intel_plane *plane;
  1571. enum plane_id plane_id;
  1572. int level, ret, i;
  1573. unsigned int dirty = 0;
  1574. for_each_oldnew_intel_plane_in_state(state, plane,
  1575. old_plane_state,
  1576. new_plane_state, i) {
  1577. if (new_plane_state->base.crtc != &crtc->base &&
  1578. old_plane_state->base.crtc != &crtc->base)
  1579. continue;
  1580. if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
  1581. dirty |= BIT(plane->id);
  1582. }
  1583. /*
  1584. * DSPARB registers may have been reset due to the
  1585. * power well being turned off. Make sure we restore
  1586. * them to a consistent state even if no primary/sprite
  1587. * planes are initially active.
  1588. */
  1589. if (needs_modeset)
  1590. crtc_state->fifo_changed = true;
  1591. if (!dirty)
  1592. return 0;
  1593. /* cursor changes don't warrant a FIFO recompute */
  1594. if (dirty & ~BIT(PLANE_CURSOR)) {
  1595. const struct intel_crtc_state *old_crtc_state =
  1596. intel_atomic_get_old_crtc_state(state, crtc);
  1597. const struct vlv_fifo_state *old_fifo_state =
  1598. &old_crtc_state->wm.vlv.fifo_state;
  1599. ret = vlv_compute_fifo(crtc_state);
  1600. if (ret)
  1601. return ret;
  1602. if (needs_modeset ||
  1603. memcmp(old_fifo_state, fifo_state,
  1604. sizeof(*fifo_state)) != 0)
  1605. crtc_state->fifo_changed = true;
  1606. }
  1607. /* initially allow all levels */
  1608. wm_state->num_levels = intel_wm_num_levels(dev_priv);
  1609. /*
  1610. * Note that enabling cxsr with no primary/sprite planes
  1611. * enabled can wedge the pipe. Hence we only allow cxsr
  1612. * with exactly one enabled primary/sprite plane.
  1613. */
  1614. wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
  1615. for (level = 0; level < wm_state->num_levels; level++) {
  1616. const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
  1617. const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
  1618. if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
  1619. break;
  1620. for_each_plane_id_on_crtc(crtc, plane_id) {
  1621. wm_state->wm[level].plane[plane_id] =
  1622. vlv_invert_wm_value(raw->plane[plane_id],
  1623. fifo_state->plane[plane_id]);
  1624. }
  1625. wm_state->sr[level].plane =
  1626. vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
  1627. raw->plane[PLANE_SPRITE0],
  1628. raw->plane[PLANE_SPRITE1]),
  1629. sr_fifo_size);
  1630. wm_state->sr[level].cursor =
  1631. vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
  1632. 63);
  1633. }
  1634. if (level == 0)
  1635. return -EINVAL;
  1636. /* limit to only levels we can actually handle */
  1637. wm_state->num_levels = level;
  1638. /* invalidate the higher levels */
  1639. vlv_invalidate_wms(crtc, wm_state, level);
  1640. return 0;
  1641. }
  1642. #define VLV_FIFO(plane, value) \
  1643. (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
  1644. static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
  1645. struct intel_crtc_state *crtc_state)
  1646. {
  1647. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1648. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1649. const struct vlv_fifo_state *fifo_state =
  1650. &crtc_state->wm.vlv.fifo_state;
  1651. int sprite0_start, sprite1_start, fifo_size;
  1652. if (!crtc_state->fifo_changed)
  1653. return;
  1654. sprite0_start = fifo_state->plane[PLANE_PRIMARY];
  1655. sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
  1656. fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
  1657. WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
  1658. WARN_ON(fifo_size != 511);
  1659. trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
  1660. /*
  1661. * uncore.lock serves a double purpose here. It allows us to
  1662. * use the less expensive I915_{READ,WRITE}_FW() functions, and
  1663. * it protects the DSPARB registers from getting clobbered by
  1664. * parallel updates from multiple pipes.
  1665. *
  1666. * intel_pipe_update_start() has already disabled interrupts
  1667. * for us, so a plain spin_lock() is sufficient here.
  1668. */
  1669. spin_lock(&dev_priv->uncore.lock);
  1670. switch (crtc->pipe) {
  1671. uint32_t dsparb, dsparb2, dsparb3;
  1672. case PIPE_A:
  1673. dsparb = I915_READ_FW(DSPARB);
  1674. dsparb2 = I915_READ_FW(DSPARB2);
  1675. dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
  1676. VLV_FIFO(SPRITEB, 0xff));
  1677. dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
  1678. VLV_FIFO(SPRITEB, sprite1_start));
  1679. dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
  1680. VLV_FIFO(SPRITEB_HI, 0x1));
  1681. dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
  1682. VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
  1683. I915_WRITE_FW(DSPARB, dsparb);
  1684. I915_WRITE_FW(DSPARB2, dsparb2);
  1685. break;
  1686. case PIPE_B:
  1687. dsparb = I915_READ_FW(DSPARB);
  1688. dsparb2 = I915_READ_FW(DSPARB2);
  1689. dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
  1690. VLV_FIFO(SPRITED, 0xff));
  1691. dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
  1692. VLV_FIFO(SPRITED, sprite1_start));
  1693. dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
  1694. VLV_FIFO(SPRITED_HI, 0xff));
  1695. dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
  1696. VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
  1697. I915_WRITE_FW(DSPARB, dsparb);
  1698. I915_WRITE_FW(DSPARB2, dsparb2);
  1699. break;
  1700. case PIPE_C:
  1701. dsparb3 = I915_READ_FW(DSPARB3);
  1702. dsparb2 = I915_READ_FW(DSPARB2);
  1703. dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
  1704. VLV_FIFO(SPRITEF, 0xff));
  1705. dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
  1706. VLV_FIFO(SPRITEF, sprite1_start));
  1707. dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
  1708. VLV_FIFO(SPRITEF_HI, 0xff));
  1709. dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
  1710. VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
  1711. I915_WRITE_FW(DSPARB3, dsparb3);
  1712. I915_WRITE_FW(DSPARB2, dsparb2);
  1713. break;
  1714. default:
  1715. break;
  1716. }
  1717. POSTING_READ_FW(DSPARB);
  1718. spin_unlock(&dev_priv->uncore.lock);
  1719. }
  1720. #undef VLV_FIFO
  1721. static int vlv_compute_intermediate_wm(struct drm_device *dev,
  1722. struct intel_crtc *crtc,
  1723. struct intel_crtc_state *new_crtc_state)
  1724. {
  1725. struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
  1726. const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
  1727. struct intel_atomic_state *intel_state =
  1728. to_intel_atomic_state(new_crtc_state->base.state);
  1729. const struct intel_crtc_state *old_crtc_state =
  1730. intel_atomic_get_old_crtc_state(intel_state, crtc);
  1731. const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
  1732. int level;
  1733. if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
  1734. *intermediate = *optimal;
  1735. intermediate->cxsr = false;
  1736. goto out;
  1737. }
  1738. intermediate->num_levels = min(optimal->num_levels, active->num_levels);
  1739. intermediate->cxsr = optimal->cxsr && active->cxsr &&
  1740. !new_crtc_state->disable_cxsr;
  1741. for (level = 0; level < intermediate->num_levels; level++) {
  1742. enum plane_id plane_id;
  1743. for_each_plane_id_on_crtc(crtc, plane_id) {
  1744. intermediate->wm[level].plane[plane_id] =
  1745. min(optimal->wm[level].plane[plane_id],
  1746. active->wm[level].plane[plane_id]);
  1747. }
  1748. intermediate->sr[level].plane = min(optimal->sr[level].plane,
  1749. active->sr[level].plane);
  1750. intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
  1751. active->sr[level].cursor);
  1752. }
  1753. vlv_invalidate_wms(crtc, intermediate, level);
  1754. out:
  1755. /*
  1756. * If our intermediate WM are identical to the final WM, then we can
  1757. * omit the post-vblank programming; only update if it's different.
  1758. */
  1759. if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
  1760. new_crtc_state->wm.need_postvbl_update = true;
  1761. return 0;
  1762. }
  1763. static void vlv_merge_wm(struct drm_i915_private *dev_priv,
  1764. struct vlv_wm_values *wm)
  1765. {
  1766. struct intel_crtc *crtc;
  1767. int num_active_crtcs = 0;
  1768. wm->level = dev_priv->wm.max_level;
  1769. wm->cxsr = true;
  1770. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1771. const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
  1772. if (!crtc->active)
  1773. continue;
  1774. if (!wm_state->cxsr)
  1775. wm->cxsr = false;
  1776. num_active_crtcs++;
  1777. wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
  1778. }
  1779. if (num_active_crtcs != 1)
  1780. wm->cxsr = false;
  1781. if (num_active_crtcs > 1)
  1782. wm->level = VLV_WM_LEVEL_PM2;
  1783. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1784. const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
  1785. enum pipe pipe = crtc->pipe;
  1786. wm->pipe[pipe] = wm_state->wm[wm->level];
  1787. if (crtc->active && wm->cxsr)
  1788. wm->sr = wm_state->sr[wm->level];
  1789. wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
  1790. wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
  1791. wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
  1792. wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
  1793. }
  1794. }
  1795. static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
  1796. {
  1797. struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
  1798. struct vlv_wm_values new_wm = {};
  1799. vlv_merge_wm(dev_priv, &new_wm);
  1800. if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
  1801. return;
  1802. if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
  1803. chv_set_memory_dvfs(dev_priv, false);
  1804. if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
  1805. chv_set_memory_pm5(dev_priv, false);
  1806. if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
  1807. _intel_set_memory_cxsr(dev_priv, false);
  1808. vlv_write_wm_values(dev_priv, &new_wm);
  1809. if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
  1810. _intel_set_memory_cxsr(dev_priv, true);
  1811. if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
  1812. chv_set_memory_pm5(dev_priv, true);
  1813. if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
  1814. chv_set_memory_dvfs(dev_priv, true);
  1815. *old_wm = new_wm;
  1816. }
  1817. static void vlv_initial_watermarks(struct intel_atomic_state *state,
  1818. struct intel_crtc_state *crtc_state)
  1819. {
  1820. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1821. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1822. mutex_lock(&dev_priv->wm.wm_mutex);
  1823. crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
  1824. vlv_program_watermarks(dev_priv);
  1825. mutex_unlock(&dev_priv->wm.wm_mutex);
  1826. }
  1827. static void vlv_optimize_watermarks(struct intel_atomic_state *state,
  1828. struct intel_crtc_state *crtc_state)
  1829. {
  1830. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  1832. if (!crtc_state->wm.need_postvbl_update)
  1833. return;
  1834. mutex_lock(&dev_priv->wm.wm_mutex);
  1835. intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
  1836. vlv_program_watermarks(dev_priv);
  1837. mutex_unlock(&dev_priv->wm.wm_mutex);
  1838. }
  1839. static void i965_update_wm(struct intel_crtc *unused_crtc)
  1840. {
  1841. struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
  1842. struct intel_crtc *crtc;
  1843. int srwm = 1;
  1844. int cursor_sr = 16;
  1845. bool cxsr_enabled;
  1846. /* Calc sr entries for one plane configs */
  1847. crtc = single_enabled_crtc(dev_priv);
  1848. if (crtc) {
  1849. /* self-refresh has much higher latency */
  1850. static const int sr_latency_ns = 12000;
  1851. const struct drm_display_mode *adjusted_mode =
  1852. &crtc->config->base.adjusted_mode;
  1853. const struct drm_framebuffer *fb =
  1854. crtc->base.primary->state->fb;
  1855. int clock = adjusted_mode->crtc_clock;
  1856. int htotal = adjusted_mode->crtc_htotal;
  1857. int hdisplay = crtc->config->pipe_src_w;
  1858. int cpp = fb->format->cpp[0];
  1859. int entries;
  1860. entries = intel_wm_method2(clock, htotal,
  1861. hdisplay, cpp, sr_latency_ns / 100);
  1862. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  1863. srwm = I965_FIFO_SIZE - entries;
  1864. if (srwm < 0)
  1865. srwm = 1;
  1866. srwm &= 0x1ff;
  1867. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  1868. entries, srwm);
  1869. entries = intel_wm_method2(clock, htotal,
  1870. crtc->base.cursor->state->crtc_w, 4,
  1871. sr_latency_ns / 100);
  1872. entries = DIV_ROUND_UP(entries,
  1873. i965_cursor_wm_info.cacheline_size) +
  1874. i965_cursor_wm_info.guard_size;
  1875. cursor_sr = i965_cursor_wm_info.fifo_size - entries;
  1876. if (cursor_sr > i965_cursor_wm_info.max_wm)
  1877. cursor_sr = i965_cursor_wm_info.max_wm;
  1878. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  1879. "cursor %d\n", srwm, cursor_sr);
  1880. cxsr_enabled = true;
  1881. } else {
  1882. cxsr_enabled = false;
  1883. /* Turn off self refresh if both pipes are enabled */
  1884. intel_set_memory_cxsr(dev_priv, false);
  1885. }
  1886. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  1887. srwm);
  1888. /* 965 has limitations... */
  1889. I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
  1890. FW_WM(8, CURSORB) |
  1891. FW_WM(8, PLANEB) |
  1892. FW_WM(8, PLANEA));
  1893. I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
  1894. FW_WM(8, PLANEC_OLD));
  1895. /* update cursor SR watermark */
  1896. I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
  1897. if (cxsr_enabled)
  1898. intel_set_memory_cxsr(dev_priv, true);
  1899. }
  1900. #undef FW_WM
  1901. static void i9xx_update_wm(struct intel_crtc *unused_crtc)
  1902. {
  1903. struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
  1904. const struct intel_watermark_params *wm_info;
  1905. uint32_t fwater_lo;
  1906. uint32_t fwater_hi;
  1907. int cwm, srwm = 1;
  1908. int fifo_size;
  1909. int planea_wm, planeb_wm;
  1910. struct intel_crtc *crtc, *enabled = NULL;
  1911. if (IS_I945GM(dev_priv))
  1912. wm_info = &i945_wm_info;
  1913. else if (!IS_GEN2(dev_priv))
  1914. wm_info = &i915_wm_info;
  1915. else
  1916. wm_info = &i830_a_wm_info;
  1917. fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
  1918. crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
  1919. if (intel_crtc_active(crtc)) {
  1920. const struct drm_display_mode *adjusted_mode =
  1921. &crtc->config->base.adjusted_mode;
  1922. const struct drm_framebuffer *fb =
  1923. crtc->base.primary->state->fb;
  1924. int cpp;
  1925. if (IS_GEN2(dev_priv))
  1926. cpp = 4;
  1927. else
  1928. cpp = fb->format->cpp[0];
  1929. planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  1930. wm_info, fifo_size, cpp,
  1931. pessimal_latency_ns);
  1932. enabled = crtc;
  1933. } else {
  1934. planea_wm = fifo_size - wm_info->guard_size;
  1935. if (planea_wm > (long)wm_info->max_wm)
  1936. planea_wm = wm_info->max_wm;
  1937. }
  1938. if (IS_GEN2(dev_priv))
  1939. wm_info = &i830_bc_wm_info;
  1940. fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
  1941. crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
  1942. if (intel_crtc_active(crtc)) {
  1943. const struct drm_display_mode *adjusted_mode =
  1944. &crtc->config->base.adjusted_mode;
  1945. const struct drm_framebuffer *fb =
  1946. crtc->base.primary->state->fb;
  1947. int cpp;
  1948. if (IS_GEN2(dev_priv))
  1949. cpp = 4;
  1950. else
  1951. cpp = fb->format->cpp[0];
  1952. planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  1953. wm_info, fifo_size, cpp,
  1954. pessimal_latency_ns);
  1955. if (enabled == NULL)
  1956. enabled = crtc;
  1957. else
  1958. enabled = NULL;
  1959. } else {
  1960. planeb_wm = fifo_size - wm_info->guard_size;
  1961. if (planeb_wm > (long)wm_info->max_wm)
  1962. planeb_wm = wm_info->max_wm;
  1963. }
  1964. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  1965. if (IS_I915GM(dev_priv) && enabled) {
  1966. struct drm_i915_gem_object *obj;
  1967. obj = intel_fb_obj(enabled->base.primary->state->fb);
  1968. /* self-refresh seems busted with untiled */
  1969. if (!i915_gem_object_is_tiled(obj))
  1970. enabled = NULL;
  1971. }
  1972. /*
  1973. * Overlay gets an aggressive default since video jitter is bad.
  1974. */
  1975. cwm = 2;
  1976. /* Play safe and disable self-refresh before adjusting watermarks. */
  1977. intel_set_memory_cxsr(dev_priv, false);
  1978. /* Calc sr entries for one plane configs */
  1979. if (HAS_FW_BLC(dev_priv) && enabled) {
  1980. /* self-refresh has much higher latency */
  1981. static const int sr_latency_ns = 6000;
  1982. const struct drm_display_mode *adjusted_mode =
  1983. &enabled->config->base.adjusted_mode;
  1984. const struct drm_framebuffer *fb =
  1985. enabled->base.primary->state->fb;
  1986. int clock = adjusted_mode->crtc_clock;
  1987. int htotal = adjusted_mode->crtc_htotal;
  1988. int hdisplay = enabled->config->pipe_src_w;
  1989. int cpp;
  1990. int entries;
  1991. if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
  1992. cpp = 4;
  1993. else
  1994. cpp = fb->format->cpp[0];
  1995. entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
  1996. sr_latency_ns / 100);
  1997. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  1998. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  1999. srwm = wm_info->fifo_size - entries;
  2000. if (srwm < 0)
  2001. srwm = 1;
  2002. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
  2003. I915_WRITE(FW_BLC_SELF,
  2004. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  2005. else
  2006. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  2007. }
  2008. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  2009. planea_wm, planeb_wm, cwm, srwm);
  2010. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  2011. fwater_hi = (cwm & 0x1f);
  2012. /* Set request length to 8 cachelines per fetch */
  2013. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  2014. fwater_hi = fwater_hi | (1 << 8);
  2015. I915_WRITE(FW_BLC, fwater_lo);
  2016. I915_WRITE(FW_BLC2, fwater_hi);
  2017. if (enabled)
  2018. intel_set_memory_cxsr(dev_priv, true);
  2019. }
  2020. static void i845_update_wm(struct intel_crtc *unused_crtc)
  2021. {
  2022. struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
  2023. struct intel_crtc *crtc;
  2024. const struct drm_display_mode *adjusted_mode;
  2025. uint32_t fwater_lo;
  2026. int planea_wm;
  2027. crtc = single_enabled_crtc(dev_priv);
  2028. if (crtc == NULL)
  2029. return;
  2030. adjusted_mode = &crtc->config->base.adjusted_mode;
  2031. planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  2032. &i845_wm_info,
  2033. dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
  2034. 4, pessimal_latency_ns);
  2035. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  2036. fwater_lo |= (3<<8) | planea_wm;
  2037. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  2038. I915_WRITE(FW_BLC, fwater_lo);
  2039. }
  2040. /* latency must be in 0.1us units. */
  2041. static unsigned int ilk_wm_method1(unsigned int pixel_rate,
  2042. unsigned int cpp,
  2043. unsigned int latency)
  2044. {
  2045. unsigned int ret;
  2046. ret = intel_wm_method1(pixel_rate, cpp, latency);
  2047. ret = DIV_ROUND_UP(ret, 64) + 2;
  2048. return ret;
  2049. }
  2050. /* latency must be in 0.1us units. */
  2051. static unsigned int ilk_wm_method2(unsigned int pixel_rate,
  2052. unsigned int htotal,
  2053. unsigned int width,
  2054. unsigned int cpp,
  2055. unsigned int latency)
  2056. {
  2057. unsigned int ret;
  2058. ret = intel_wm_method2(pixel_rate, htotal,
  2059. width, cpp, latency);
  2060. ret = DIV_ROUND_UP(ret, 64) + 2;
  2061. return ret;
  2062. }
  2063. static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
  2064. uint8_t cpp)
  2065. {
  2066. /*
  2067. * Neither of these should be possible since this function shouldn't be
  2068. * called if the CRTC is off or the plane is invisible. But let's be
  2069. * extra paranoid to avoid a potential divide-by-zero if we screw up
  2070. * elsewhere in the driver.
  2071. */
  2072. if (WARN_ON(!cpp))
  2073. return 0;
  2074. if (WARN_ON(!horiz_pixels))
  2075. return 0;
  2076. return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
  2077. }
  2078. struct ilk_wm_maximums {
  2079. uint16_t pri;
  2080. uint16_t spr;
  2081. uint16_t cur;
  2082. uint16_t fbc;
  2083. };
  2084. /*
  2085. * For both WM_PIPE and WM_LP.
  2086. * mem_value must be in 0.1us units.
  2087. */
  2088. static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
  2089. const struct intel_plane_state *pstate,
  2090. uint32_t mem_value,
  2091. bool is_lp)
  2092. {
  2093. uint32_t method1, method2;
  2094. int cpp;
  2095. if (!intel_wm_plane_visible(cstate, pstate))
  2096. return 0;
  2097. cpp = pstate->base.fb->format->cpp[0];
  2098. method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
  2099. if (!is_lp)
  2100. return method1;
  2101. method2 = ilk_wm_method2(cstate->pixel_rate,
  2102. cstate->base.adjusted_mode.crtc_htotal,
  2103. drm_rect_width(&pstate->base.dst),
  2104. cpp, mem_value);
  2105. return min(method1, method2);
  2106. }
  2107. /*
  2108. * For both WM_PIPE and WM_LP.
  2109. * mem_value must be in 0.1us units.
  2110. */
  2111. static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
  2112. const struct intel_plane_state *pstate,
  2113. uint32_t mem_value)
  2114. {
  2115. uint32_t method1, method2;
  2116. int cpp;
  2117. if (!intel_wm_plane_visible(cstate, pstate))
  2118. return 0;
  2119. cpp = pstate->base.fb->format->cpp[0];
  2120. method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
  2121. method2 = ilk_wm_method2(cstate->pixel_rate,
  2122. cstate->base.adjusted_mode.crtc_htotal,
  2123. drm_rect_width(&pstate->base.dst),
  2124. cpp, mem_value);
  2125. return min(method1, method2);
  2126. }
  2127. /*
  2128. * For both WM_PIPE and WM_LP.
  2129. * mem_value must be in 0.1us units.
  2130. */
  2131. static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
  2132. const struct intel_plane_state *pstate,
  2133. uint32_t mem_value)
  2134. {
  2135. int cpp;
  2136. if (!intel_wm_plane_visible(cstate, pstate))
  2137. return 0;
  2138. cpp = pstate->base.fb->format->cpp[0];
  2139. return ilk_wm_method2(cstate->pixel_rate,
  2140. cstate->base.adjusted_mode.crtc_htotal,
  2141. pstate->base.crtc_w, cpp, mem_value);
  2142. }
  2143. /* Only for WM_LP. */
  2144. static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
  2145. const struct intel_plane_state *pstate,
  2146. uint32_t pri_val)
  2147. {
  2148. int cpp;
  2149. if (!intel_wm_plane_visible(cstate, pstate))
  2150. return 0;
  2151. cpp = pstate->base.fb->format->cpp[0];
  2152. return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
  2153. }
  2154. static unsigned int
  2155. ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
  2156. {
  2157. if (INTEL_GEN(dev_priv) >= 8)
  2158. return 3072;
  2159. else if (INTEL_GEN(dev_priv) >= 7)
  2160. return 768;
  2161. else
  2162. return 512;
  2163. }
  2164. static unsigned int
  2165. ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
  2166. int level, bool is_sprite)
  2167. {
  2168. if (INTEL_GEN(dev_priv) >= 8)
  2169. /* BDW primary/sprite plane watermarks */
  2170. return level == 0 ? 255 : 2047;
  2171. else if (INTEL_GEN(dev_priv) >= 7)
  2172. /* IVB/HSW primary/sprite plane watermarks */
  2173. return level == 0 ? 127 : 1023;
  2174. else if (!is_sprite)
  2175. /* ILK/SNB primary plane watermarks */
  2176. return level == 0 ? 127 : 511;
  2177. else
  2178. /* ILK/SNB sprite plane watermarks */
  2179. return level == 0 ? 63 : 255;
  2180. }
  2181. static unsigned int
  2182. ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
  2183. {
  2184. if (INTEL_GEN(dev_priv) >= 7)
  2185. return level == 0 ? 63 : 255;
  2186. else
  2187. return level == 0 ? 31 : 63;
  2188. }
  2189. static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
  2190. {
  2191. if (INTEL_GEN(dev_priv) >= 8)
  2192. return 31;
  2193. else
  2194. return 15;
  2195. }
  2196. /* Calculate the maximum primary/sprite plane watermark */
  2197. static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
  2198. int level,
  2199. const struct intel_wm_config *config,
  2200. enum intel_ddb_partitioning ddb_partitioning,
  2201. bool is_sprite)
  2202. {
  2203. struct drm_i915_private *dev_priv = to_i915(dev);
  2204. unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
  2205. /* if sprites aren't enabled, sprites get nothing */
  2206. if (is_sprite && !config->sprites_enabled)
  2207. return 0;
  2208. /* HSW allows LP1+ watermarks even with multiple pipes */
  2209. if (level == 0 || config->num_pipes_active > 1) {
  2210. fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
  2211. /*
  2212. * For some reason the non self refresh
  2213. * FIFO size is only half of the self
  2214. * refresh FIFO size on ILK/SNB.
  2215. */
  2216. if (INTEL_GEN(dev_priv) <= 6)
  2217. fifo_size /= 2;
  2218. }
  2219. if (config->sprites_enabled) {
  2220. /* level 0 is always calculated with 1:1 split */
  2221. if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
  2222. if (is_sprite)
  2223. fifo_size *= 5;
  2224. fifo_size /= 6;
  2225. } else {
  2226. fifo_size /= 2;
  2227. }
  2228. }
  2229. /* clamp to max that the registers can hold */
  2230. return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
  2231. }
  2232. /* Calculate the maximum cursor plane watermark */
  2233. static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
  2234. int level,
  2235. const struct intel_wm_config *config)
  2236. {
  2237. /* HSW LP1+ watermarks w/ multiple pipes */
  2238. if (level > 0 && config->num_pipes_active > 1)
  2239. return 64;
  2240. /* otherwise just report max that registers can hold */
  2241. return ilk_cursor_wm_reg_max(to_i915(dev), level);
  2242. }
  2243. static void ilk_compute_wm_maximums(const struct drm_device *dev,
  2244. int level,
  2245. const struct intel_wm_config *config,
  2246. enum intel_ddb_partitioning ddb_partitioning,
  2247. struct ilk_wm_maximums *max)
  2248. {
  2249. max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
  2250. max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
  2251. max->cur = ilk_cursor_wm_max(dev, level, config);
  2252. max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
  2253. }
  2254. static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
  2255. int level,
  2256. struct ilk_wm_maximums *max)
  2257. {
  2258. max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
  2259. max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
  2260. max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
  2261. max->fbc = ilk_fbc_wm_reg_max(dev_priv);
  2262. }
  2263. static bool ilk_validate_wm_level(int level,
  2264. const struct ilk_wm_maximums *max,
  2265. struct intel_wm_level *result)
  2266. {
  2267. bool ret;
  2268. /* already determined to be invalid? */
  2269. if (!result->enable)
  2270. return false;
  2271. result->enable = result->pri_val <= max->pri &&
  2272. result->spr_val <= max->spr &&
  2273. result->cur_val <= max->cur;
  2274. ret = result->enable;
  2275. /*
  2276. * HACK until we can pre-compute everything,
  2277. * and thus fail gracefully if LP0 watermarks
  2278. * are exceeded...
  2279. */
  2280. if (level == 0 && !result->enable) {
  2281. if (result->pri_val > max->pri)
  2282. DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
  2283. level, result->pri_val, max->pri);
  2284. if (result->spr_val > max->spr)
  2285. DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
  2286. level, result->spr_val, max->spr);
  2287. if (result->cur_val > max->cur)
  2288. DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
  2289. level, result->cur_val, max->cur);
  2290. result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
  2291. result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
  2292. result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
  2293. result->enable = true;
  2294. }
  2295. return ret;
  2296. }
  2297. static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
  2298. const struct intel_crtc *intel_crtc,
  2299. int level,
  2300. struct intel_crtc_state *cstate,
  2301. const struct intel_plane_state *pristate,
  2302. const struct intel_plane_state *sprstate,
  2303. const struct intel_plane_state *curstate,
  2304. struct intel_wm_level *result)
  2305. {
  2306. uint16_t pri_latency = dev_priv->wm.pri_latency[level];
  2307. uint16_t spr_latency = dev_priv->wm.spr_latency[level];
  2308. uint16_t cur_latency = dev_priv->wm.cur_latency[level];
  2309. /* WM1+ latency values stored in 0.5us units */
  2310. if (level > 0) {
  2311. pri_latency *= 5;
  2312. spr_latency *= 5;
  2313. cur_latency *= 5;
  2314. }
  2315. if (pristate) {
  2316. result->pri_val = ilk_compute_pri_wm(cstate, pristate,
  2317. pri_latency, level);
  2318. result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
  2319. }
  2320. if (sprstate)
  2321. result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
  2322. if (curstate)
  2323. result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
  2324. result->enable = true;
  2325. }
  2326. static uint32_t
  2327. hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
  2328. {
  2329. const struct intel_atomic_state *intel_state =
  2330. to_intel_atomic_state(cstate->base.state);
  2331. const struct drm_display_mode *adjusted_mode =
  2332. &cstate->base.adjusted_mode;
  2333. u32 linetime, ips_linetime;
  2334. if (!cstate->base.active)
  2335. return 0;
  2336. if (WARN_ON(adjusted_mode->crtc_clock == 0))
  2337. return 0;
  2338. if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
  2339. return 0;
  2340. /* The WM are computed with base on how long it takes to fill a single
  2341. * row at the given clock rate, multiplied by 8.
  2342. * */
  2343. linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
  2344. adjusted_mode->crtc_clock);
  2345. ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
  2346. intel_state->cdclk.logical.cdclk);
  2347. return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
  2348. PIPE_WM_LINETIME_TIME(linetime);
  2349. }
  2350. static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
  2351. uint16_t wm[8])
  2352. {
  2353. if (INTEL_GEN(dev_priv) >= 9) {
  2354. uint32_t val;
  2355. int ret, i;
  2356. int level, max_level = ilk_wm_max_level(dev_priv);
  2357. /* read the first set of memory latencies[0:3] */
  2358. val = 0; /* data0 to be programmed to 0 for first set */
  2359. mutex_lock(&dev_priv->pcu_lock);
  2360. ret = sandybridge_pcode_read(dev_priv,
  2361. GEN9_PCODE_READ_MEM_LATENCY,
  2362. &val);
  2363. mutex_unlock(&dev_priv->pcu_lock);
  2364. if (ret) {
  2365. DRM_ERROR("SKL Mailbox read error = %d\n", ret);
  2366. return;
  2367. }
  2368. wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
  2369. wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
  2370. GEN9_MEM_LATENCY_LEVEL_MASK;
  2371. wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
  2372. GEN9_MEM_LATENCY_LEVEL_MASK;
  2373. wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
  2374. GEN9_MEM_LATENCY_LEVEL_MASK;
  2375. /* read the second set of memory latencies[4:7] */
  2376. val = 1; /* data0 to be programmed to 1 for second set */
  2377. mutex_lock(&dev_priv->pcu_lock);
  2378. ret = sandybridge_pcode_read(dev_priv,
  2379. GEN9_PCODE_READ_MEM_LATENCY,
  2380. &val);
  2381. mutex_unlock(&dev_priv->pcu_lock);
  2382. if (ret) {
  2383. DRM_ERROR("SKL Mailbox read error = %d\n", ret);
  2384. return;
  2385. }
  2386. wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
  2387. wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
  2388. GEN9_MEM_LATENCY_LEVEL_MASK;
  2389. wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
  2390. GEN9_MEM_LATENCY_LEVEL_MASK;
  2391. wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
  2392. GEN9_MEM_LATENCY_LEVEL_MASK;
  2393. /*
  2394. * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
  2395. * need to be disabled. We make sure to sanitize the values out
  2396. * of the punit to satisfy this requirement.
  2397. */
  2398. for (level = 1; level <= max_level; level++) {
  2399. if (wm[level] == 0) {
  2400. for (i = level + 1; i <= max_level; i++)
  2401. wm[i] = 0;
  2402. break;
  2403. }
  2404. }
  2405. /*
  2406. * WaWmMemoryReadLatency:skl+,glk
  2407. *
  2408. * punit doesn't take into account the read latency so we need
  2409. * to add 2us to the various latency levels we retrieve from the
  2410. * punit when level 0 response data us 0us.
  2411. */
  2412. if (wm[0] == 0) {
  2413. wm[0] += 2;
  2414. for (level = 1; level <= max_level; level++) {
  2415. if (wm[level] == 0)
  2416. break;
  2417. wm[level] += 2;
  2418. }
  2419. }
  2420. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2421. uint64_t sskpd = I915_READ64(MCH_SSKPD);
  2422. wm[0] = (sskpd >> 56) & 0xFF;
  2423. if (wm[0] == 0)
  2424. wm[0] = sskpd & 0xF;
  2425. wm[1] = (sskpd >> 4) & 0xFF;
  2426. wm[2] = (sskpd >> 12) & 0xFF;
  2427. wm[3] = (sskpd >> 20) & 0x1FF;
  2428. wm[4] = (sskpd >> 32) & 0x1FF;
  2429. } else if (INTEL_GEN(dev_priv) >= 6) {
  2430. uint32_t sskpd = I915_READ(MCH_SSKPD);
  2431. wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
  2432. wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
  2433. wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
  2434. wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
  2435. } else if (INTEL_GEN(dev_priv) >= 5) {
  2436. uint32_t mltr = I915_READ(MLTR_ILK);
  2437. /* ILK primary LP0 latency is 700 ns */
  2438. wm[0] = 7;
  2439. wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
  2440. wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
  2441. } else {
  2442. MISSING_CASE(INTEL_DEVID(dev_priv));
  2443. }
  2444. }
  2445. static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
  2446. uint16_t wm[5])
  2447. {
  2448. /* ILK sprite LP0 latency is 1300 ns */
  2449. if (IS_GEN5(dev_priv))
  2450. wm[0] = 13;
  2451. }
  2452. static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
  2453. uint16_t wm[5])
  2454. {
  2455. /* ILK cursor LP0 latency is 1300 ns */
  2456. if (IS_GEN5(dev_priv))
  2457. wm[0] = 13;
  2458. }
  2459. int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
  2460. {
  2461. /* how many WM levels are we expecting */
  2462. if (INTEL_GEN(dev_priv) >= 9)
  2463. return 7;
  2464. else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2465. return 4;
  2466. else if (INTEL_GEN(dev_priv) >= 6)
  2467. return 3;
  2468. else
  2469. return 2;
  2470. }
  2471. static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
  2472. const char *name,
  2473. const uint16_t wm[8])
  2474. {
  2475. int level, max_level = ilk_wm_max_level(dev_priv);
  2476. for (level = 0; level <= max_level; level++) {
  2477. unsigned int latency = wm[level];
  2478. if (latency == 0) {
  2479. DRM_ERROR("%s WM%d latency not provided\n",
  2480. name, level);
  2481. continue;
  2482. }
  2483. /*
  2484. * - latencies are in us on gen9.
  2485. * - before then, WM1+ latency values are in 0.5us units
  2486. */
  2487. if (INTEL_GEN(dev_priv) >= 9)
  2488. latency *= 10;
  2489. else if (level > 0)
  2490. latency *= 5;
  2491. DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
  2492. name, level, wm[level],
  2493. latency / 10, latency % 10);
  2494. }
  2495. }
  2496. static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
  2497. uint16_t wm[5], uint16_t min)
  2498. {
  2499. int level, max_level = ilk_wm_max_level(dev_priv);
  2500. if (wm[0] >= min)
  2501. return false;
  2502. wm[0] = max(wm[0], min);
  2503. for (level = 1; level <= max_level; level++)
  2504. wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
  2505. return true;
  2506. }
  2507. static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
  2508. {
  2509. bool changed;
  2510. /*
  2511. * The BIOS provided WM memory latency values are often
  2512. * inadequate for high resolution displays. Adjust them.
  2513. */
  2514. changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
  2515. ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
  2516. ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
  2517. if (!changed)
  2518. return;
  2519. DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
  2520. intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
  2521. intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
  2522. intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
  2523. }
  2524. static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
  2525. {
  2526. intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
  2527. memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
  2528. sizeof(dev_priv->wm.pri_latency));
  2529. memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
  2530. sizeof(dev_priv->wm.pri_latency));
  2531. intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
  2532. intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
  2533. intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
  2534. intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
  2535. intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
  2536. if (IS_GEN6(dev_priv))
  2537. snb_wm_latency_quirk(dev_priv);
  2538. }
  2539. static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
  2540. {
  2541. intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
  2542. intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
  2543. }
  2544. static bool ilk_validate_pipe_wm(struct drm_device *dev,
  2545. struct intel_pipe_wm *pipe_wm)
  2546. {
  2547. /* LP0 watermark maximums depend on this pipe alone */
  2548. const struct intel_wm_config config = {
  2549. .num_pipes_active = 1,
  2550. .sprites_enabled = pipe_wm->sprites_enabled,
  2551. .sprites_scaled = pipe_wm->sprites_scaled,
  2552. };
  2553. struct ilk_wm_maximums max;
  2554. /* LP0 watermarks always use 1/2 DDB partitioning */
  2555. ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
  2556. /* At least LP0 must be valid */
  2557. if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
  2558. DRM_DEBUG_KMS("LP0 watermark invalid\n");
  2559. return false;
  2560. }
  2561. return true;
  2562. }
  2563. /* Compute new watermarks for the pipe */
  2564. static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
  2565. {
  2566. struct drm_atomic_state *state = cstate->base.state;
  2567. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  2568. struct intel_pipe_wm *pipe_wm;
  2569. struct drm_device *dev = state->dev;
  2570. const struct drm_i915_private *dev_priv = to_i915(dev);
  2571. struct drm_plane *plane;
  2572. const struct drm_plane_state *plane_state;
  2573. const struct intel_plane_state *pristate = NULL;
  2574. const struct intel_plane_state *sprstate = NULL;
  2575. const struct intel_plane_state *curstate = NULL;
  2576. int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
  2577. struct ilk_wm_maximums max;
  2578. pipe_wm = &cstate->wm.ilk.optimal;
  2579. drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, &cstate->base) {
  2580. const struct intel_plane_state *ps = to_intel_plane_state(plane_state);
  2581. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  2582. pristate = ps;
  2583. else if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  2584. sprstate = ps;
  2585. else if (plane->type == DRM_PLANE_TYPE_CURSOR)
  2586. curstate = ps;
  2587. }
  2588. pipe_wm->pipe_enabled = cstate->base.active;
  2589. if (sprstate) {
  2590. pipe_wm->sprites_enabled = sprstate->base.visible;
  2591. pipe_wm->sprites_scaled = sprstate->base.visible &&
  2592. (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
  2593. drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
  2594. }
  2595. usable_level = max_level;
  2596. /* ILK/SNB: LP2+ watermarks only w/o sprites */
  2597. if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
  2598. usable_level = 1;
  2599. /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
  2600. if (pipe_wm->sprites_scaled)
  2601. usable_level = 0;
  2602. memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
  2603. ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
  2604. pristate, sprstate, curstate, &pipe_wm->wm[0]);
  2605. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2606. pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
  2607. if (!ilk_validate_pipe_wm(dev, pipe_wm))
  2608. return -EINVAL;
  2609. ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
  2610. for (level = 1; level <= usable_level; level++) {
  2611. struct intel_wm_level *wm = &pipe_wm->wm[level];
  2612. ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
  2613. pristate, sprstate, curstate, wm);
  2614. /*
  2615. * Disable any watermark level that exceeds the
  2616. * register maximums since such watermarks are
  2617. * always invalid.
  2618. */
  2619. if (!ilk_validate_wm_level(level, &max, wm)) {
  2620. memset(wm, 0, sizeof(*wm));
  2621. break;
  2622. }
  2623. }
  2624. return 0;
  2625. }
  2626. /*
  2627. * Build a set of 'intermediate' watermark values that satisfy both the old
  2628. * state and the new state. These can be programmed to the hardware
  2629. * immediately.
  2630. */
  2631. static int ilk_compute_intermediate_wm(struct drm_device *dev,
  2632. struct intel_crtc *intel_crtc,
  2633. struct intel_crtc_state *newstate)
  2634. {
  2635. struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
  2636. struct intel_atomic_state *intel_state =
  2637. to_intel_atomic_state(newstate->base.state);
  2638. const struct intel_crtc_state *oldstate =
  2639. intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
  2640. const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
  2641. int level, max_level = ilk_wm_max_level(to_i915(dev));
  2642. /*
  2643. * Start with the final, target watermarks, then combine with the
  2644. * currently active watermarks to get values that are safe both before
  2645. * and after the vblank.
  2646. */
  2647. *a = newstate->wm.ilk.optimal;
  2648. if (!newstate->base.active || drm_atomic_crtc_needs_modeset(&newstate->base))
  2649. return 0;
  2650. a->pipe_enabled |= b->pipe_enabled;
  2651. a->sprites_enabled |= b->sprites_enabled;
  2652. a->sprites_scaled |= b->sprites_scaled;
  2653. for (level = 0; level <= max_level; level++) {
  2654. struct intel_wm_level *a_wm = &a->wm[level];
  2655. const struct intel_wm_level *b_wm = &b->wm[level];
  2656. a_wm->enable &= b_wm->enable;
  2657. a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
  2658. a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
  2659. a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
  2660. a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
  2661. }
  2662. /*
  2663. * We need to make sure that these merged watermark values are
  2664. * actually a valid configuration themselves. If they're not,
  2665. * there's no safe way to transition from the old state to
  2666. * the new state, so we need to fail the atomic transaction.
  2667. */
  2668. if (!ilk_validate_pipe_wm(dev, a))
  2669. return -EINVAL;
  2670. /*
  2671. * If our intermediate WM are identical to the final WM, then we can
  2672. * omit the post-vblank programming; only update if it's different.
  2673. */
  2674. if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
  2675. newstate->wm.need_postvbl_update = true;
  2676. return 0;
  2677. }
  2678. /*
  2679. * Merge the watermarks from all active pipes for a specific level.
  2680. */
  2681. static void ilk_merge_wm_level(struct drm_device *dev,
  2682. int level,
  2683. struct intel_wm_level *ret_wm)
  2684. {
  2685. const struct intel_crtc *intel_crtc;
  2686. ret_wm->enable = true;
  2687. for_each_intel_crtc(dev, intel_crtc) {
  2688. const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
  2689. const struct intel_wm_level *wm = &active->wm[level];
  2690. if (!active->pipe_enabled)
  2691. continue;
  2692. /*
  2693. * The watermark values may have been used in the past,
  2694. * so we must maintain them in the registers for some
  2695. * time even if the level is now disabled.
  2696. */
  2697. if (!wm->enable)
  2698. ret_wm->enable = false;
  2699. ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
  2700. ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
  2701. ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
  2702. ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
  2703. }
  2704. }
  2705. /*
  2706. * Merge all low power watermarks for all active pipes.
  2707. */
  2708. static void ilk_wm_merge(struct drm_device *dev,
  2709. const struct intel_wm_config *config,
  2710. const struct ilk_wm_maximums *max,
  2711. struct intel_pipe_wm *merged)
  2712. {
  2713. struct drm_i915_private *dev_priv = to_i915(dev);
  2714. int level, max_level = ilk_wm_max_level(dev_priv);
  2715. int last_enabled_level = max_level;
  2716. /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
  2717. if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
  2718. config->num_pipes_active > 1)
  2719. last_enabled_level = 0;
  2720. /* ILK: FBC WM must be disabled always */
  2721. merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
  2722. /* merge each WM1+ level */
  2723. for (level = 1; level <= max_level; level++) {
  2724. struct intel_wm_level *wm = &merged->wm[level];
  2725. ilk_merge_wm_level(dev, level, wm);
  2726. if (level > last_enabled_level)
  2727. wm->enable = false;
  2728. else if (!ilk_validate_wm_level(level, max, wm))
  2729. /* make sure all following levels get disabled */
  2730. last_enabled_level = level - 1;
  2731. /*
  2732. * The spec says it is preferred to disable
  2733. * FBC WMs instead of disabling a WM level.
  2734. */
  2735. if (wm->fbc_val > max->fbc) {
  2736. if (wm->enable)
  2737. merged->fbc_wm_enabled = false;
  2738. wm->fbc_val = 0;
  2739. }
  2740. }
  2741. /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
  2742. /*
  2743. * FIXME this is racy. FBC might get enabled later.
  2744. * What we should check here is whether FBC can be
  2745. * enabled sometime later.
  2746. */
  2747. if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
  2748. intel_fbc_is_active(dev_priv)) {
  2749. for (level = 2; level <= max_level; level++) {
  2750. struct intel_wm_level *wm = &merged->wm[level];
  2751. wm->enable = false;
  2752. }
  2753. }
  2754. }
  2755. static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
  2756. {
  2757. /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
  2758. return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
  2759. }
  2760. /* The value we need to program into the WM_LPx latency field */
  2761. static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
  2762. {
  2763. struct drm_i915_private *dev_priv = to_i915(dev);
  2764. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2765. return 2 * level;
  2766. else
  2767. return dev_priv->wm.pri_latency[level];
  2768. }
  2769. static void ilk_compute_wm_results(struct drm_device *dev,
  2770. const struct intel_pipe_wm *merged,
  2771. enum intel_ddb_partitioning partitioning,
  2772. struct ilk_wm_values *results)
  2773. {
  2774. struct drm_i915_private *dev_priv = to_i915(dev);
  2775. struct intel_crtc *intel_crtc;
  2776. int level, wm_lp;
  2777. results->enable_fbc_wm = merged->fbc_wm_enabled;
  2778. results->partitioning = partitioning;
  2779. /* LP1+ register values */
  2780. for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
  2781. const struct intel_wm_level *r;
  2782. level = ilk_wm_lp_to_level(wm_lp, merged);
  2783. r = &merged->wm[level];
  2784. /*
  2785. * Maintain the watermark values even if the level is
  2786. * disabled. Doing otherwise could cause underruns.
  2787. */
  2788. results->wm_lp[wm_lp - 1] =
  2789. (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
  2790. (r->pri_val << WM1_LP_SR_SHIFT) |
  2791. r->cur_val;
  2792. if (r->enable)
  2793. results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
  2794. if (INTEL_GEN(dev_priv) >= 8)
  2795. results->wm_lp[wm_lp - 1] |=
  2796. r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
  2797. else
  2798. results->wm_lp[wm_lp - 1] |=
  2799. r->fbc_val << WM1_LP_FBC_SHIFT;
  2800. /*
  2801. * Always set WM1S_LP_EN when spr_val != 0, even if the
  2802. * level is disabled. Doing otherwise could cause underruns.
  2803. */
  2804. if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
  2805. WARN_ON(wm_lp != 1);
  2806. results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
  2807. } else
  2808. results->wm_lp_spr[wm_lp - 1] = r->spr_val;
  2809. }
  2810. /* LP0 register values */
  2811. for_each_intel_crtc(dev, intel_crtc) {
  2812. enum pipe pipe = intel_crtc->pipe;
  2813. const struct intel_wm_level *r =
  2814. &intel_crtc->wm.active.ilk.wm[0];
  2815. if (WARN_ON(!r->enable))
  2816. continue;
  2817. results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
  2818. results->wm_pipe[pipe] =
  2819. (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
  2820. (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
  2821. r->cur_val;
  2822. }
  2823. }
  2824. /* Find the result with the highest level enabled. Check for enable_fbc_wm in
  2825. * case both are at the same level. Prefer r1 in case they're the same. */
  2826. static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
  2827. struct intel_pipe_wm *r1,
  2828. struct intel_pipe_wm *r2)
  2829. {
  2830. int level, max_level = ilk_wm_max_level(to_i915(dev));
  2831. int level1 = 0, level2 = 0;
  2832. for (level = 1; level <= max_level; level++) {
  2833. if (r1->wm[level].enable)
  2834. level1 = level;
  2835. if (r2->wm[level].enable)
  2836. level2 = level;
  2837. }
  2838. if (level1 == level2) {
  2839. if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
  2840. return r2;
  2841. else
  2842. return r1;
  2843. } else if (level1 > level2) {
  2844. return r1;
  2845. } else {
  2846. return r2;
  2847. }
  2848. }
  2849. /* dirty bits used to track which watermarks need changes */
  2850. #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
  2851. #define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
  2852. #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
  2853. #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
  2854. #define WM_DIRTY_FBC (1 << 24)
  2855. #define WM_DIRTY_DDB (1 << 25)
  2856. static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
  2857. const struct ilk_wm_values *old,
  2858. const struct ilk_wm_values *new)
  2859. {
  2860. unsigned int dirty = 0;
  2861. enum pipe pipe;
  2862. int wm_lp;
  2863. for_each_pipe(dev_priv, pipe) {
  2864. if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
  2865. dirty |= WM_DIRTY_LINETIME(pipe);
  2866. /* Must disable LP1+ watermarks too */
  2867. dirty |= WM_DIRTY_LP_ALL;
  2868. }
  2869. if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
  2870. dirty |= WM_DIRTY_PIPE(pipe);
  2871. /* Must disable LP1+ watermarks too */
  2872. dirty |= WM_DIRTY_LP_ALL;
  2873. }
  2874. }
  2875. if (old->enable_fbc_wm != new->enable_fbc_wm) {
  2876. dirty |= WM_DIRTY_FBC;
  2877. /* Must disable LP1+ watermarks too */
  2878. dirty |= WM_DIRTY_LP_ALL;
  2879. }
  2880. if (old->partitioning != new->partitioning) {
  2881. dirty |= WM_DIRTY_DDB;
  2882. /* Must disable LP1+ watermarks too */
  2883. dirty |= WM_DIRTY_LP_ALL;
  2884. }
  2885. /* LP1+ watermarks already deemed dirty, no need to continue */
  2886. if (dirty & WM_DIRTY_LP_ALL)
  2887. return dirty;
  2888. /* Find the lowest numbered LP1+ watermark in need of an update... */
  2889. for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
  2890. if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
  2891. old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
  2892. break;
  2893. }
  2894. /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
  2895. for (; wm_lp <= 3; wm_lp++)
  2896. dirty |= WM_DIRTY_LP(wm_lp);
  2897. return dirty;
  2898. }
  2899. static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
  2900. unsigned int dirty)
  2901. {
  2902. struct ilk_wm_values *previous = &dev_priv->wm.hw;
  2903. bool changed = false;
  2904. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
  2905. previous->wm_lp[2] &= ~WM1_LP_SR_EN;
  2906. I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
  2907. changed = true;
  2908. }
  2909. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
  2910. previous->wm_lp[1] &= ~WM1_LP_SR_EN;
  2911. I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
  2912. changed = true;
  2913. }
  2914. if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
  2915. previous->wm_lp[0] &= ~WM1_LP_SR_EN;
  2916. I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
  2917. changed = true;
  2918. }
  2919. /*
  2920. * Don't touch WM1S_LP_EN here.
  2921. * Doing so could cause underruns.
  2922. */
  2923. return changed;
  2924. }
  2925. /*
  2926. * The spec says we shouldn't write when we don't need, because every write
  2927. * causes WMs to be re-evaluated, expending some power.
  2928. */
  2929. static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
  2930. struct ilk_wm_values *results)
  2931. {
  2932. struct ilk_wm_values *previous = &dev_priv->wm.hw;
  2933. unsigned int dirty;
  2934. uint32_t val;
  2935. dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
  2936. if (!dirty)
  2937. return;
  2938. _ilk_disable_lp_wm(dev_priv, dirty);
  2939. if (dirty & WM_DIRTY_PIPE(PIPE_A))
  2940. I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
  2941. if (dirty & WM_DIRTY_PIPE(PIPE_B))
  2942. I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
  2943. if (dirty & WM_DIRTY_PIPE(PIPE_C))
  2944. I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
  2945. if (dirty & WM_DIRTY_LINETIME(PIPE_A))
  2946. I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
  2947. if (dirty & WM_DIRTY_LINETIME(PIPE_B))
  2948. I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
  2949. if (dirty & WM_DIRTY_LINETIME(PIPE_C))
  2950. I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
  2951. if (dirty & WM_DIRTY_DDB) {
  2952. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2953. val = I915_READ(WM_MISC);
  2954. if (results->partitioning == INTEL_DDB_PART_1_2)
  2955. val &= ~WM_MISC_DATA_PARTITION_5_6;
  2956. else
  2957. val |= WM_MISC_DATA_PARTITION_5_6;
  2958. I915_WRITE(WM_MISC, val);
  2959. } else {
  2960. val = I915_READ(DISP_ARB_CTL2);
  2961. if (results->partitioning == INTEL_DDB_PART_1_2)
  2962. val &= ~DISP_DATA_PARTITION_5_6;
  2963. else
  2964. val |= DISP_DATA_PARTITION_5_6;
  2965. I915_WRITE(DISP_ARB_CTL2, val);
  2966. }
  2967. }
  2968. if (dirty & WM_DIRTY_FBC) {
  2969. val = I915_READ(DISP_ARB_CTL);
  2970. if (results->enable_fbc_wm)
  2971. val &= ~DISP_FBC_WM_DIS;
  2972. else
  2973. val |= DISP_FBC_WM_DIS;
  2974. I915_WRITE(DISP_ARB_CTL, val);
  2975. }
  2976. if (dirty & WM_DIRTY_LP(1) &&
  2977. previous->wm_lp_spr[0] != results->wm_lp_spr[0])
  2978. I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
  2979. if (INTEL_GEN(dev_priv) >= 7) {
  2980. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
  2981. I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
  2982. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
  2983. I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
  2984. }
  2985. if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
  2986. I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
  2987. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
  2988. I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
  2989. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
  2990. I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
  2991. dev_priv->wm.hw = *results;
  2992. }
  2993. bool ilk_disable_lp_wm(struct drm_device *dev)
  2994. {
  2995. struct drm_i915_private *dev_priv = to_i915(dev);
  2996. return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
  2997. }
  2998. static u8 intel_enabled_dbuf_slices_num(struct drm_i915_private *dev_priv)
  2999. {
  3000. u8 enabled_slices;
  3001. /* Slice 1 will always be enabled */
  3002. enabled_slices = 1;
  3003. /* Gen prior to GEN11 have only one DBuf slice */
  3004. if (INTEL_GEN(dev_priv) < 11)
  3005. return enabled_slices;
  3006. if (I915_READ(DBUF_CTL_S2) & DBUF_POWER_STATE)
  3007. enabled_slices++;
  3008. return enabled_slices;
  3009. }
  3010. /*
  3011. * FIXME: We still don't have the proper code detect if we need to apply the WA,
  3012. * so assume we'll always need it in order to avoid underruns.
  3013. */
  3014. static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
  3015. {
  3016. struct drm_i915_private *dev_priv = to_i915(state->base.dev);
  3017. if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
  3018. return true;
  3019. return false;
  3020. }
  3021. static bool
  3022. intel_has_sagv(struct drm_i915_private *dev_priv)
  3023. {
  3024. if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) ||
  3025. IS_CANNONLAKE(dev_priv))
  3026. return true;
  3027. if (IS_SKYLAKE(dev_priv) &&
  3028. dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
  3029. return true;
  3030. return false;
  3031. }
  3032. /*
  3033. * SAGV dynamically adjusts the system agent voltage and clock frequencies
  3034. * depending on power and performance requirements. The display engine access
  3035. * to system memory is blocked during the adjustment time. Because of the
  3036. * blocking time, having this enabled can cause full system hangs and/or pipe
  3037. * underruns if we don't meet all of the following requirements:
  3038. *
  3039. * - <= 1 pipe enabled
  3040. * - All planes can enable watermarks for latencies >= SAGV engine block time
  3041. * - We're not using an interlaced display configuration
  3042. */
  3043. int
  3044. intel_enable_sagv(struct drm_i915_private *dev_priv)
  3045. {
  3046. int ret;
  3047. if (!intel_has_sagv(dev_priv))
  3048. return 0;
  3049. if (dev_priv->sagv_status == I915_SAGV_ENABLED)
  3050. return 0;
  3051. DRM_DEBUG_KMS("Enabling the SAGV\n");
  3052. mutex_lock(&dev_priv->pcu_lock);
  3053. ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
  3054. GEN9_SAGV_ENABLE);
  3055. /* We don't need to wait for the SAGV when enabling */
  3056. mutex_unlock(&dev_priv->pcu_lock);
  3057. /*
  3058. * Some skl systems, pre-release machines in particular,
  3059. * don't actually have an SAGV.
  3060. */
  3061. if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
  3062. DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
  3063. dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
  3064. return 0;
  3065. } else if (ret < 0) {
  3066. DRM_ERROR("Failed to enable the SAGV\n");
  3067. return ret;
  3068. }
  3069. dev_priv->sagv_status = I915_SAGV_ENABLED;
  3070. return 0;
  3071. }
  3072. int
  3073. intel_disable_sagv(struct drm_i915_private *dev_priv)
  3074. {
  3075. int ret;
  3076. if (!intel_has_sagv(dev_priv))
  3077. return 0;
  3078. if (dev_priv->sagv_status == I915_SAGV_DISABLED)
  3079. return 0;
  3080. DRM_DEBUG_KMS("Disabling the SAGV\n");
  3081. mutex_lock(&dev_priv->pcu_lock);
  3082. /* bspec says to keep retrying for at least 1 ms */
  3083. ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
  3084. GEN9_SAGV_DISABLE,
  3085. GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
  3086. 1);
  3087. mutex_unlock(&dev_priv->pcu_lock);
  3088. /*
  3089. * Some skl systems, pre-release machines in particular,
  3090. * don't actually have an SAGV.
  3091. */
  3092. if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
  3093. DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
  3094. dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
  3095. return 0;
  3096. } else if (ret < 0) {
  3097. DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
  3098. return ret;
  3099. }
  3100. dev_priv->sagv_status = I915_SAGV_DISABLED;
  3101. return 0;
  3102. }
  3103. bool intel_can_enable_sagv(struct drm_atomic_state *state)
  3104. {
  3105. struct drm_device *dev = state->dev;
  3106. struct drm_i915_private *dev_priv = to_i915(dev);
  3107. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  3108. struct intel_crtc *crtc;
  3109. struct intel_plane *plane;
  3110. struct intel_crtc_state *cstate;
  3111. enum pipe pipe;
  3112. int level, latency;
  3113. int sagv_block_time_us;
  3114. if (!intel_has_sagv(dev_priv))
  3115. return false;
  3116. if (IS_GEN9(dev_priv))
  3117. sagv_block_time_us = 30;
  3118. else if (IS_GEN10(dev_priv))
  3119. sagv_block_time_us = 20;
  3120. else
  3121. sagv_block_time_us = 10;
  3122. /*
  3123. * SKL+ workaround: bspec recommends we disable the SAGV when we have
  3124. * more then one pipe enabled
  3125. *
  3126. * If there are no active CRTCs, no additional checks need be performed
  3127. */
  3128. if (hweight32(intel_state->active_crtcs) == 0)
  3129. return true;
  3130. else if (hweight32(intel_state->active_crtcs) > 1)
  3131. return false;
  3132. /* Since we're now guaranteed to only have one active CRTC... */
  3133. pipe = ffs(intel_state->active_crtcs) - 1;
  3134. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  3135. cstate = to_intel_crtc_state(crtc->base.state);
  3136. if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  3137. return false;
  3138. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  3139. struct skl_plane_wm *wm =
  3140. &cstate->wm.skl.optimal.planes[plane->id];
  3141. /* Skip this plane if it's not enabled */
  3142. if (!wm->wm[0].plane_en)
  3143. continue;
  3144. /* Find the highest enabled wm level for this plane */
  3145. for (level = ilk_wm_max_level(dev_priv);
  3146. !wm->wm[level].plane_en; --level)
  3147. { }
  3148. latency = dev_priv->wm.skl_latency[level];
  3149. if (skl_needs_memory_bw_wa(intel_state) &&
  3150. plane->base.state->fb->modifier ==
  3151. I915_FORMAT_MOD_X_TILED)
  3152. latency += 15;
  3153. /*
  3154. * If any of the planes on this pipe don't enable wm levels that
  3155. * incur memory latencies higher than sagv_block_time_us we
  3156. * can't enable the SAGV.
  3157. */
  3158. if (latency < sagv_block_time_us)
  3159. return false;
  3160. }
  3161. return true;
  3162. }
  3163. static unsigned int intel_get_ddb_size(struct drm_i915_private *dev_priv,
  3164. const struct intel_crtc_state *cstate,
  3165. const unsigned int total_data_rate,
  3166. const int num_active,
  3167. struct skl_ddb_allocation *ddb)
  3168. {
  3169. const struct drm_display_mode *adjusted_mode;
  3170. u64 total_data_bw;
  3171. u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
  3172. WARN_ON(ddb_size == 0);
  3173. if (INTEL_GEN(dev_priv) < 11)
  3174. return ddb_size - 4; /* 4 blocks for bypass path allocation */
  3175. adjusted_mode = &cstate->base.adjusted_mode;
  3176. total_data_bw = (u64)total_data_rate * drm_mode_vrefresh(adjusted_mode);
  3177. /*
  3178. * 12GB/s is maximum BW supported by single DBuf slice.
  3179. */
  3180. if (total_data_bw >= GBps(12) || num_active > 1) {
  3181. ddb->enabled_slices = 2;
  3182. } else {
  3183. ddb->enabled_slices = 1;
  3184. ddb_size /= 2;
  3185. }
  3186. return ddb_size;
  3187. }
  3188. static void
  3189. skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
  3190. const struct intel_crtc_state *cstate,
  3191. const unsigned int total_data_rate,
  3192. struct skl_ddb_allocation *ddb,
  3193. struct skl_ddb_entry *alloc, /* out */
  3194. int *num_active /* out */)
  3195. {
  3196. struct drm_atomic_state *state = cstate->base.state;
  3197. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  3198. struct drm_i915_private *dev_priv = to_i915(dev);
  3199. struct drm_crtc *for_crtc = cstate->base.crtc;
  3200. unsigned int pipe_size, ddb_size;
  3201. int nth_active_pipe;
  3202. if (WARN_ON(!state) || !cstate->base.active) {
  3203. alloc->start = 0;
  3204. alloc->end = 0;
  3205. *num_active = hweight32(dev_priv->active_crtcs);
  3206. return;
  3207. }
  3208. if (intel_state->active_pipe_changes)
  3209. *num_active = hweight32(intel_state->active_crtcs);
  3210. else
  3211. *num_active = hweight32(dev_priv->active_crtcs);
  3212. ddb_size = intel_get_ddb_size(dev_priv, cstate, total_data_rate,
  3213. *num_active, ddb);
  3214. /*
  3215. * If the state doesn't change the active CRTC's, then there's
  3216. * no need to recalculate; the existing pipe allocation limits
  3217. * should remain unchanged. Note that we're safe from racing
  3218. * commits since any racing commit that changes the active CRTC
  3219. * list would need to grab _all_ crtc locks, including the one
  3220. * we currently hold.
  3221. */
  3222. if (!intel_state->active_pipe_changes) {
  3223. /*
  3224. * alloc may be cleared by clear_intel_crtc_state,
  3225. * copy from old state to be sure
  3226. */
  3227. *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
  3228. return;
  3229. }
  3230. nth_active_pipe = hweight32(intel_state->active_crtcs &
  3231. (drm_crtc_mask(for_crtc) - 1));
  3232. pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
  3233. alloc->start = nth_active_pipe * ddb_size / *num_active;
  3234. alloc->end = alloc->start + pipe_size;
  3235. }
  3236. static unsigned int skl_cursor_allocation(int num_active)
  3237. {
  3238. if (num_active == 1)
  3239. return 32;
  3240. return 8;
  3241. }
  3242. static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
  3243. struct skl_ddb_entry *entry, u32 reg)
  3244. {
  3245. u16 mask;
  3246. if (INTEL_GEN(dev_priv) >= 11)
  3247. mask = ICL_DDB_ENTRY_MASK;
  3248. else
  3249. mask = SKL_DDB_ENTRY_MASK;
  3250. entry->start = reg & mask;
  3251. entry->end = (reg >> DDB_ENTRY_END_SHIFT) & mask;
  3252. if (entry->end)
  3253. entry->end += 1;
  3254. }
  3255. static void
  3256. skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
  3257. const enum pipe pipe,
  3258. const enum plane_id plane_id,
  3259. struct skl_ddb_allocation *ddb /* out */)
  3260. {
  3261. u32 val, val2 = 0;
  3262. int fourcc, pixel_format;
  3263. /* Cursor doesn't support NV12/planar, so no extra calculation needed */
  3264. if (plane_id == PLANE_CURSOR) {
  3265. val = I915_READ(CUR_BUF_CFG(pipe));
  3266. skl_ddb_entry_init_from_hw(dev_priv,
  3267. &ddb->plane[pipe][plane_id], val);
  3268. return;
  3269. }
  3270. val = I915_READ(PLANE_CTL(pipe, plane_id));
  3271. /* No DDB allocated for disabled planes */
  3272. if (!(val & PLANE_CTL_ENABLE))
  3273. return;
  3274. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  3275. fourcc = skl_format_to_fourcc(pixel_format,
  3276. val & PLANE_CTL_ORDER_RGBX,
  3277. val & PLANE_CTL_ALPHA_MASK);
  3278. val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
  3279. val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
  3280. if (fourcc == DRM_FORMAT_NV12) {
  3281. skl_ddb_entry_init_from_hw(dev_priv,
  3282. &ddb->plane[pipe][plane_id], val2);
  3283. skl_ddb_entry_init_from_hw(dev_priv,
  3284. &ddb->uv_plane[pipe][plane_id], val);
  3285. } else {
  3286. skl_ddb_entry_init_from_hw(dev_priv,
  3287. &ddb->plane[pipe][plane_id], val);
  3288. }
  3289. }
  3290. void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
  3291. struct skl_ddb_allocation *ddb /* out */)
  3292. {
  3293. struct intel_crtc *crtc;
  3294. memset(ddb, 0, sizeof(*ddb));
  3295. ddb->enabled_slices = intel_enabled_dbuf_slices_num(dev_priv);
  3296. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3297. enum intel_display_power_domain power_domain;
  3298. enum plane_id plane_id;
  3299. enum pipe pipe = crtc->pipe;
  3300. power_domain = POWER_DOMAIN_PIPE(pipe);
  3301. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  3302. continue;
  3303. for_each_plane_id_on_crtc(crtc, plane_id)
  3304. skl_ddb_get_hw_plane_state(dev_priv, pipe,
  3305. plane_id, ddb);
  3306. intel_display_power_put(dev_priv, power_domain);
  3307. }
  3308. }
  3309. /*
  3310. * Determines the downscale amount of a plane for the purposes of watermark calculations.
  3311. * The bspec defines downscale amount as:
  3312. *
  3313. * """
  3314. * Horizontal down scale amount = maximum[1, Horizontal source size /
  3315. * Horizontal destination size]
  3316. * Vertical down scale amount = maximum[1, Vertical source size /
  3317. * Vertical destination size]
  3318. * Total down scale amount = Horizontal down scale amount *
  3319. * Vertical down scale amount
  3320. * """
  3321. *
  3322. * Return value is provided in 16.16 fixed point form to retain fractional part.
  3323. * Caller should take care of dividing & rounding off the value.
  3324. */
  3325. static uint_fixed_16_16_t
  3326. skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
  3327. const struct intel_plane_state *pstate)
  3328. {
  3329. struct intel_plane *plane = to_intel_plane(pstate->base.plane);
  3330. uint32_t src_w, src_h, dst_w, dst_h;
  3331. uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
  3332. uint_fixed_16_16_t downscale_h, downscale_w;
  3333. if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
  3334. return u32_to_fixed16(0);
  3335. /* n.b., src is 16.16 fixed point, dst is whole integer */
  3336. if (plane->id == PLANE_CURSOR) {
  3337. /*
  3338. * Cursors only support 0/180 degree rotation,
  3339. * hence no need to account for rotation here.
  3340. */
  3341. src_w = pstate->base.src_w >> 16;
  3342. src_h = pstate->base.src_h >> 16;
  3343. dst_w = pstate->base.crtc_w;
  3344. dst_h = pstate->base.crtc_h;
  3345. } else {
  3346. /*
  3347. * Src coordinates are already rotated by 270 degrees for
  3348. * the 90/270 degree plane rotation cases (to match the
  3349. * GTT mapping), hence no need to account for rotation here.
  3350. */
  3351. src_w = drm_rect_width(&pstate->base.src) >> 16;
  3352. src_h = drm_rect_height(&pstate->base.src) >> 16;
  3353. dst_w = drm_rect_width(&pstate->base.dst);
  3354. dst_h = drm_rect_height(&pstate->base.dst);
  3355. }
  3356. fp_w_ratio = div_fixed16(src_w, dst_w);
  3357. fp_h_ratio = div_fixed16(src_h, dst_h);
  3358. downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
  3359. downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
  3360. return mul_fixed16(downscale_w, downscale_h);
  3361. }
  3362. static uint_fixed_16_16_t
  3363. skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
  3364. {
  3365. uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
  3366. if (!crtc_state->base.enable)
  3367. return pipe_downscale;
  3368. if (crtc_state->pch_pfit.enabled) {
  3369. uint32_t src_w, src_h, dst_w, dst_h;
  3370. uint32_t pfit_size = crtc_state->pch_pfit.size;
  3371. uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
  3372. uint_fixed_16_16_t downscale_h, downscale_w;
  3373. src_w = crtc_state->pipe_src_w;
  3374. src_h = crtc_state->pipe_src_h;
  3375. dst_w = pfit_size >> 16;
  3376. dst_h = pfit_size & 0xffff;
  3377. if (!dst_w || !dst_h)
  3378. return pipe_downscale;
  3379. fp_w_ratio = div_fixed16(src_w, dst_w);
  3380. fp_h_ratio = div_fixed16(src_h, dst_h);
  3381. downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
  3382. downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
  3383. pipe_downscale = mul_fixed16(downscale_w, downscale_h);
  3384. }
  3385. return pipe_downscale;
  3386. }
  3387. int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
  3388. struct intel_crtc_state *cstate)
  3389. {
  3390. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3391. struct drm_crtc_state *crtc_state = &cstate->base;
  3392. struct drm_atomic_state *state = crtc_state->state;
  3393. struct drm_plane *plane;
  3394. const struct drm_plane_state *pstate;
  3395. struct intel_plane_state *intel_pstate;
  3396. int crtc_clock, dotclk;
  3397. uint32_t pipe_max_pixel_rate;
  3398. uint_fixed_16_16_t pipe_downscale;
  3399. uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
  3400. if (!cstate->base.enable)
  3401. return 0;
  3402. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
  3403. uint_fixed_16_16_t plane_downscale;
  3404. uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
  3405. int bpp;
  3406. if (!intel_wm_plane_visible(cstate,
  3407. to_intel_plane_state(pstate)))
  3408. continue;
  3409. if (WARN_ON(!pstate->fb))
  3410. return -EINVAL;
  3411. intel_pstate = to_intel_plane_state(pstate);
  3412. plane_downscale = skl_plane_downscale_amount(cstate,
  3413. intel_pstate);
  3414. bpp = pstate->fb->format->cpp[0] * 8;
  3415. if (bpp == 64)
  3416. plane_downscale = mul_fixed16(plane_downscale,
  3417. fp_9_div_8);
  3418. max_downscale = max_fixed16(plane_downscale, max_downscale);
  3419. }
  3420. pipe_downscale = skl_pipe_downscale_amount(cstate);
  3421. pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
  3422. crtc_clock = crtc_state->adjusted_mode.crtc_clock;
  3423. dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
  3424. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  3425. dotclk *= 2;
  3426. pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
  3427. if (pipe_max_pixel_rate < crtc_clock) {
  3428. DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
  3429. return -EINVAL;
  3430. }
  3431. return 0;
  3432. }
  3433. static unsigned int
  3434. skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
  3435. const struct drm_plane_state *pstate,
  3436. const int plane)
  3437. {
  3438. struct intel_plane *intel_plane = to_intel_plane(pstate->plane);
  3439. struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
  3440. uint32_t data_rate;
  3441. uint32_t width = 0, height = 0;
  3442. struct drm_framebuffer *fb;
  3443. u32 format;
  3444. uint_fixed_16_16_t down_scale_amount;
  3445. if (!intel_pstate->base.visible)
  3446. return 0;
  3447. fb = pstate->fb;
  3448. format = fb->format->format;
  3449. if (intel_plane->id == PLANE_CURSOR)
  3450. return 0;
  3451. if (plane == 1 && format != DRM_FORMAT_NV12)
  3452. return 0;
  3453. /*
  3454. * Src coordinates are already rotated by 270 degrees for
  3455. * the 90/270 degree plane rotation cases (to match the
  3456. * GTT mapping), hence no need to account for rotation here.
  3457. */
  3458. width = drm_rect_width(&intel_pstate->base.src) >> 16;
  3459. height = drm_rect_height(&intel_pstate->base.src) >> 16;
  3460. /* UV plane does 1/2 pixel sub-sampling */
  3461. if (plane == 1 && format == DRM_FORMAT_NV12) {
  3462. width /= 2;
  3463. height /= 2;
  3464. }
  3465. data_rate = width * height * fb->format->cpp[plane];
  3466. down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
  3467. return mul_round_up_u32_fixed16(data_rate, down_scale_amount);
  3468. }
  3469. /*
  3470. * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
  3471. * a 8192x4096@32bpp framebuffer:
  3472. * 3 * 4096 * 8192 * 4 < 2^32
  3473. */
  3474. static unsigned int
  3475. skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
  3476. unsigned int *plane_data_rate,
  3477. unsigned int *uv_plane_data_rate)
  3478. {
  3479. struct drm_crtc_state *cstate = &intel_cstate->base;
  3480. struct drm_atomic_state *state = cstate->state;
  3481. struct drm_plane *plane;
  3482. const struct drm_plane_state *pstate;
  3483. unsigned int total_data_rate = 0;
  3484. if (WARN_ON(!state))
  3485. return 0;
  3486. /* Calculate and cache data rate for each plane */
  3487. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
  3488. enum plane_id plane_id = to_intel_plane(plane)->id;
  3489. unsigned int rate;
  3490. /* packed/y */
  3491. rate = skl_plane_relative_data_rate(intel_cstate,
  3492. pstate, 0);
  3493. plane_data_rate[plane_id] = rate;
  3494. total_data_rate += rate;
  3495. /* uv-plane */
  3496. rate = skl_plane_relative_data_rate(intel_cstate,
  3497. pstate, 1);
  3498. uv_plane_data_rate[plane_id] = rate;
  3499. total_data_rate += rate;
  3500. }
  3501. return total_data_rate;
  3502. }
  3503. static uint16_t
  3504. skl_ddb_min_alloc(const struct drm_plane_state *pstate, const int plane)
  3505. {
  3506. struct drm_framebuffer *fb = pstate->fb;
  3507. struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
  3508. uint32_t src_w, src_h;
  3509. uint32_t min_scanlines = 8;
  3510. uint8_t plane_bpp;
  3511. if (WARN_ON(!fb))
  3512. return 0;
  3513. /* For packed formats, and uv-plane, return 0 */
  3514. if (plane == 1 && fb->format->format != DRM_FORMAT_NV12)
  3515. return 0;
  3516. /* For Non Y-tile return 8-blocks */
  3517. if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
  3518. fb->modifier != I915_FORMAT_MOD_Yf_TILED &&
  3519. fb->modifier != I915_FORMAT_MOD_Y_TILED_CCS &&
  3520. fb->modifier != I915_FORMAT_MOD_Yf_TILED_CCS)
  3521. return 8;
  3522. /*
  3523. * Src coordinates are already rotated by 270 degrees for
  3524. * the 90/270 degree plane rotation cases (to match the
  3525. * GTT mapping), hence no need to account for rotation here.
  3526. */
  3527. src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
  3528. src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
  3529. /* Halve UV plane width and height for NV12 */
  3530. if (plane == 1) {
  3531. src_w /= 2;
  3532. src_h /= 2;
  3533. }
  3534. plane_bpp = fb->format->cpp[plane];
  3535. if (drm_rotation_90_or_270(pstate->rotation)) {
  3536. switch (plane_bpp) {
  3537. case 1:
  3538. min_scanlines = 32;
  3539. break;
  3540. case 2:
  3541. min_scanlines = 16;
  3542. break;
  3543. case 4:
  3544. min_scanlines = 8;
  3545. break;
  3546. case 8:
  3547. min_scanlines = 4;
  3548. break;
  3549. default:
  3550. WARN(1, "Unsupported pixel depth %u for rotation",
  3551. plane_bpp);
  3552. min_scanlines = 32;
  3553. }
  3554. }
  3555. return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
  3556. }
  3557. static void
  3558. skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
  3559. uint16_t *minimum, uint16_t *uv_minimum)
  3560. {
  3561. const struct drm_plane_state *pstate;
  3562. struct drm_plane *plane;
  3563. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
  3564. enum plane_id plane_id = to_intel_plane(plane)->id;
  3565. if (plane_id == PLANE_CURSOR)
  3566. continue;
  3567. if (!pstate->visible)
  3568. continue;
  3569. minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
  3570. uv_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
  3571. }
  3572. minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
  3573. }
  3574. static int
  3575. skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
  3576. struct skl_ddb_allocation *ddb /* out */)
  3577. {
  3578. struct drm_atomic_state *state = cstate->base.state;
  3579. struct drm_crtc *crtc = cstate->base.crtc;
  3580. struct drm_device *dev = crtc->dev;
  3581. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3582. enum pipe pipe = intel_crtc->pipe;
  3583. struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
  3584. uint16_t alloc_size, start;
  3585. uint16_t minimum[I915_MAX_PLANES] = {};
  3586. uint16_t uv_minimum[I915_MAX_PLANES] = {};
  3587. unsigned int total_data_rate;
  3588. enum plane_id plane_id;
  3589. int num_active;
  3590. unsigned int plane_data_rate[I915_MAX_PLANES] = {};
  3591. unsigned int uv_plane_data_rate[I915_MAX_PLANES] = {};
  3592. uint16_t total_min_blocks = 0;
  3593. /* Clear the partitioning for disabled planes. */
  3594. memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
  3595. memset(ddb->uv_plane[pipe], 0, sizeof(ddb->uv_plane[pipe]));
  3596. if (WARN_ON(!state))
  3597. return 0;
  3598. if (!cstate->base.active) {
  3599. alloc->start = alloc->end = 0;
  3600. return 0;
  3601. }
  3602. total_data_rate = skl_get_total_relative_data_rate(cstate,
  3603. plane_data_rate,
  3604. uv_plane_data_rate);
  3605. skl_ddb_get_pipe_allocation_limits(dev, cstate, total_data_rate, ddb,
  3606. alloc, &num_active);
  3607. alloc_size = skl_ddb_entry_size(alloc);
  3608. if (alloc_size == 0)
  3609. return 0;
  3610. skl_ddb_calc_min(cstate, num_active, minimum, uv_minimum);
  3611. /*
  3612. * 1. Allocate the mininum required blocks for each active plane
  3613. * and allocate the cursor, it doesn't require extra allocation
  3614. * proportional to the data rate.
  3615. */
  3616. for_each_plane_id_on_crtc(intel_crtc, plane_id) {
  3617. total_min_blocks += minimum[plane_id];
  3618. total_min_blocks += uv_minimum[plane_id];
  3619. }
  3620. if (total_min_blocks > alloc_size) {
  3621. DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
  3622. DRM_DEBUG_KMS("minimum required %d/%d\n", total_min_blocks,
  3623. alloc_size);
  3624. return -EINVAL;
  3625. }
  3626. alloc_size -= total_min_blocks;
  3627. ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
  3628. ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
  3629. /*
  3630. * 2. Distribute the remaining space in proportion to the amount of
  3631. * data each plane needs to fetch from memory.
  3632. *
  3633. * FIXME: we may not allocate every single block here.
  3634. */
  3635. if (total_data_rate == 0)
  3636. return 0;
  3637. start = alloc->start;
  3638. for_each_plane_id_on_crtc(intel_crtc, plane_id) {
  3639. unsigned int data_rate, uv_data_rate;
  3640. uint16_t plane_blocks, uv_plane_blocks;
  3641. if (plane_id == PLANE_CURSOR)
  3642. continue;
  3643. data_rate = plane_data_rate[plane_id];
  3644. /*
  3645. * allocation for (packed formats) or (uv-plane part of planar format):
  3646. * promote the expression to 64 bits to avoid overflowing, the
  3647. * result is < available as data_rate / total_data_rate < 1
  3648. */
  3649. plane_blocks = minimum[plane_id];
  3650. plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
  3651. total_data_rate);
  3652. /* Leave disabled planes at (0,0) */
  3653. if (data_rate) {
  3654. ddb->plane[pipe][plane_id].start = start;
  3655. ddb->plane[pipe][plane_id].end = start + plane_blocks;
  3656. }
  3657. start += plane_blocks;
  3658. /* Allocate DDB for UV plane for planar format/NV12 */
  3659. uv_data_rate = uv_plane_data_rate[plane_id];
  3660. uv_plane_blocks = uv_minimum[plane_id];
  3661. uv_plane_blocks += div_u64((uint64_t)alloc_size * uv_data_rate,
  3662. total_data_rate);
  3663. if (uv_data_rate) {
  3664. ddb->uv_plane[pipe][plane_id].start = start;
  3665. ddb->uv_plane[pipe][plane_id].end =
  3666. start + uv_plane_blocks;
  3667. }
  3668. start += uv_plane_blocks;
  3669. }
  3670. return 0;
  3671. }
  3672. /*
  3673. * The max latency should be 257 (max the punit can code is 255 and we add 2us
  3674. * for the read latency) and cpp should always be <= 8, so that
  3675. * should allow pixel_rate up to ~2 GHz which seems sufficient since max
  3676. * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
  3677. */
  3678. static uint_fixed_16_16_t
  3679. skl_wm_method1(const struct drm_i915_private *dev_priv, uint32_t pixel_rate,
  3680. uint8_t cpp, uint32_t latency, uint32_t dbuf_block_size)
  3681. {
  3682. uint32_t wm_intermediate_val;
  3683. uint_fixed_16_16_t ret;
  3684. if (latency == 0)
  3685. return FP_16_16_MAX;
  3686. wm_intermediate_val = latency * pixel_rate * cpp;
  3687. ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
  3688. if (INTEL_GEN(dev_priv) >= 10)
  3689. ret = add_fixed16_u32(ret, 1);
  3690. return ret;
  3691. }
  3692. static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
  3693. uint32_t pipe_htotal,
  3694. uint32_t latency,
  3695. uint_fixed_16_16_t plane_blocks_per_line)
  3696. {
  3697. uint32_t wm_intermediate_val;
  3698. uint_fixed_16_16_t ret;
  3699. if (latency == 0)
  3700. return FP_16_16_MAX;
  3701. wm_intermediate_val = latency * pixel_rate;
  3702. wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
  3703. pipe_htotal * 1000);
  3704. ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
  3705. return ret;
  3706. }
  3707. static uint_fixed_16_16_t
  3708. intel_get_linetime_us(struct intel_crtc_state *cstate)
  3709. {
  3710. uint32_t pixel_rate;
  3711. uint32_t crtc_htotal;
  3712. uint_fixed_16_16_t linetime_us;
  3713. if (!cstate->base.active)
  3714. return u32_to_fixed16(0);
  3715. pixel_rate = cstate->pixel_rate;
  3716. if (WARN_ON(pixel_rate == 0))
  3717. return u32_to_fixed16(0);
  3718. crtc_htotal = cstate->base.adjusted_mode.crtc_htotal;
  3719. linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
  3720. return linetime_us;
  3721. }
  3722. static uint32_t
  3723. skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
  3724. const struct intel_plane_state *pstate)
  3725. {
  3726. uint64_t adjusted_pixel_rate;
  3727. uint_fixed_16_16_t downscale_amount;
  3728. /* Shouldn't reach here on disabled planes... */
  3729. if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
  3730. return 0;
  3731. /*
  3732. * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
  3733. * with additional adjustments for plane-specific scaling.
  3734. */
  3735. adjusted_pixel_rate = cstate->pixel_rate;
  3736. downscale_amount = skl_plane_downscale_amount(cstate, pstate);
  3737. return mul_round_up_u32_fixed16(adjusted_pixel_rate,
  3738. downscale_amount);
  3739. }
  3740. static int
  3741. skl_compute_plane_wm_params(const struct drm_i915_private *dev_priv,
  3742. struct intel_crtc_state *cstate,
  3743. const struct intel_plane_state *intel_pstate,
  3744. struct skl_wm_params *wp, int plane_id)
  3745. {
  3746. struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
  3747. const struct drm_plane_state *pstate = &intel_pstate->base;
  3748. const struct drm_framebuffer *fb = pstate->fb;
  3749. uint32_t interm_pbpl;
  3750. struct intel_atomic_state *state =
  3751. to_intel_atomic_state(cstate->base.state);
  3752. bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
  3753. if (!intel_wm_plane_visible(cstate, intel_pstate))
  3754. return 0;
  3755. /* only NV12 format has two planes */
  3756. if (plane_id == 1 && fb->format->format != DRM_FORMAT_NV12) {
  3757. DRM_DEBUG_KMS("Non NV12 format have single plane\n");
  3758. return -EINVAL;
  3759. }
  3760. wp->y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
  3761. fb->modifier == I915_FORMAT_MOD_Yf_TILED ||
  3762. fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  3763. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
  3764. wp->x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
  3765. wp->rc_surface = fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  3766. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
  3767. wp->is_planar = fb->format->format == DRM_FORMAT_NV12;
  3768. if (plane->id == PLANE_CURSOR) {
  3769. wp->width = intel_pstate->base.crtc_w;
  3770. } else {
  3771. /*
  3772. * Src coordinates are already rotated by 270 degrees for
  3773. * the 90/270 degree plane rotation cases (to match the
  3774. * GTT mapping), hence no need to account for rotation here.
  3775. */
  3776. wp->width = drm_rect_width(&intel_pstate->base.src) >> 16;
  3777. }
  3778. if (plane_id == 1 && wp->is_planar)
  3779. wp->width /= 2;
  3780. wp->cpp = fb->format->cpp[plane_id];
  3781. wp->plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate,
  3782. intel_pstate);
  3783. if (INTEL_GEN(dev_priv) >= 11 &&
  3784. fb->modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 8)
  3785. wp->dbuf_block_size = 256;
  3786. else
  3787. wp->dbuf_block_size = 512;
  3788. if (drm_rotation_90_or_270(pstate->rotation)) {
  3789. switch (wp->cpp) {
  3790. case 1:
  3791. wp->y_min_scanlines = 16;
  3792. break;
  3793. case 2:
  3794. wp->y_min_scanlines = 8;
  3795. break;
  3796. case 4:
  3797. wp->y_min_scanlines = 4;
  3798. break;
  3799. default:
  3800. MISSING_CASE(wp->cpp);
  3801. return -EINVAL;
  3802. }
  3803. } else {
  3804. wp->y_min_scanlines = 4;
  3805. }
  3806. if (apply_memory_bw_wa)
  3807. wp->y_min_scanlines *= 2;
  3808. wp->plane_bytes_per_line = wp->width * wp->cpp;
  3809. if (wp->y_tiled) {
  3810. interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
  3811. wp->y_min_scanlines,
  3812. wp->dbuf_block_size);
  3813. if (INTEL_GEN(dev_priv) >= 10)
  3814. interm_pbpl++;
  3815. wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
  3816. wp->y_min_scanlines);
  3817. } else if (wp->x_tiled && IS_GEN9(dev_priv)) {
  3818. interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
  3819. wp->dbuf_block_size);
  3820. wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
  3821. } else {
  3822. interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
  3823. wp->dbuf_block_size) + 1;
  3824. wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
  3825. }
  3826. wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
  3827. wp->plane_blocks_per_line);
  3828. wp->linetime_us = fixed16_to_u32_round_up(
  3829. intel_get_linetime_us(cstate));
  3830. return 0;
  3831. }
  3832. static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
  3833. struct intel_crtc_state *cstate,
  3834. const struct intel_plane_state *intel_pstate,
  3835. uint16_t ddb_allocation,
  3836. int level,
  3837. const struct skl_wm_params *wp,
  3838. const struct skl_wm_level *result_prev,
  3839. struct skl_wm_level *result /* out */)
  3840. {
  3841. const struct drm_plane_state *pstate = &intel_pstate->base;
  3842. uint32_t latency = dev_priv->wm.skl_latency[level];
  3843. uint_fixed_16_16_t method1, method2;
  3844. uint_fixed_16_16_t selected_result;
  3845. uint32_t res_blocks, res_lines;
  3846. struct intel_atomic_state *state =
  3847. to_intel_atomic_state(cstate->base.state);
  3848. bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
  3849. uint32_t min_disp_buf_needed;
  3850. if (latency == 0 ||
  3851. !intel_wm_plane_visible(cstate, intel_pstate)) {
  3852. result->plane_en = false;
  3853. return 0;
  3854. }
  3855. /* Display WA #1141: kbl,cfl */
  3856. if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) ||
  3857. IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0)) &&
  3858. dev_priv->ipc_enabled)
  3859. latency += 4;
  3860. if (apply_memory_bw_wa && wp->x_tiled)
  3861. latency += 15;
  3862. method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
  3863. wp->cpp, latency, wp->dbuf_block_size);
  3864. method2 = skl_wm_method2(wp->plane_pixel_rate,
  3865. cstate->base.adjusted_mode.crtc_htotal,
  3866. latency,
  3867. wp->plane_blocks_per_line);
  3868. if (wp->y_tiled) {
  3869. selected_result = max_fixed16(method2, wp->y_tile_minimum);
  3870. } else {
  3871. if ((wp->cpp * cstate->base.adjusted_mode.crtc_htotal /
  3872. wp->dbuf_block_size < 1) &&
  3873. (wp->plane_bytes_per_line / wp->dbuf_block_size < 1))
  3874. selected_result = method2;
  3875. else if (ddb_allocation >=
  3876. fixed16_to_u32_round_up(wp->plane_blocks_per_line))
  3877. selected_result = min_fixed16(method1, method2);
  3878. else if (latency >= wp->linetime_us)
  3879. selected_result = min_fixed16(method1, method2);
  3880. else
  3881. selected_result = method1;
  3882. }
  3883. res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
  3884. res_lines = div_round_up_fixed16(selected_result,
  3885. wp->plane_blocks_per_line);
  3886. /* Display WA #1125: skl,bxt,kbl,glk */
  3887. if (level == 0 && wp->rc_surface)
  3888. res_blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
  3889. /* Display WA #1126: skl,bxt,kbl,glk */
  3890. if (level >= 1 && level <= 7) {
  3891. if (wp->y_tiled) {
  3892. res_blocks += fixed16_to_u32_round_up(
  3893. wp->y_tile_minimum);
  3894. res_lines += wp->y_min_scanlines;
  3895. } else {
  3896. res_blocks++;
  3897. }
  3898. /*
  3899. * Make sure result blocks for higher latency levels are atleast
  3900. * as high as level below the current level.
  3901. * Assumption in DDB algorithm optimization for special cases.
  3902. * Also covers Display WA #1125 for RC.
  3903. */
  3904. if (result_prev->plane_res_b > res_blocks)
  3905. res_blocks = result_prev->plane_res_b;
  3906. }
  3907. if (INTEL_GEN(dev_priv) >= 11) {
  3908. if (wp->y_tiled) {
  3909. uint32_t extra_lines;
  3910. uint_fixed_16_16_t fp_min_disp_buf_needed;
  3911. if (res_lines % wp->y_min_scanlines == 0)
  3912. extra_lines = wp->y_min_scanlines;
  3913. else
  3914. extra_lines = wp->y_min_scanlines * 2 -
  3915. res_lines % wp->y_min_scanlines;
  3916. fp_min_disp_buf_needed = mul_u32_fixed16(res_lines +
  3917. extra_lines,
  3918. wp->plane_blocks_per_line);
  3919. min_disp_buf_needed = fixed16_to_u32_round_up(
  3920. fp_min_disp_buf_needed);
  3921. } else {
  3922. min_disp_buf_needed = DIV_ROUND_UP(res_blocks * 11, 10);
  3923. }
  3924. } else {
  3925. min_disp_buf_needed = res_blocks;
  3926. }
  3927. if ((level > 0 && res_lines > 31) ||
  3928. res_blocks >= ddb_allocation ||
  3929. min_disp_buf_needed >= ddb_allocation) {
  3930. result->plane_en = false;
  3931. /*
  3932. * If there are no valid level 0 watermarks, then we can't
  3933. * support this display configuration.
  3934. */
  3935. if (level) {
  3936. return 0;
  3937. } else {
  3938. struct drm_plane *plane = pstate->plane;
  3939. DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
  3940. DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
  3941. plane->base.id, plane->name,
  3942. res_blocks, ddb_allocation, res_lines);
  3943. return -EINVAL;
  3944. }
  3945. }
  3946. /*
  3947. * Display WA #826 (SKL:ALL, BXT:ALL) & #1059 (CNL:A)
  3948. * disable wm level 1-7 on NV12 planes
  3949. */
  3950. if (wp->is_planar && level >= 1 &&
  3951. (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv) ||
  3952. IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))) {
  3953. result->plane_en = false;
  3954. return 0;
  3955. }
  3956. /* The number of lines are ignored for the level 0 watermark. */
  3957. result->plane_res_b = res_blocks;
  3958. result->plane_res_l = res_lines;
  3959. result->plane_en = true;
  3960. return 0;
  3961. }
  3962. static int
  3963. skl_compute_wm_levels(const struct drm_i915_private *dev_priv,
  3964. struct skl_ddb_allocation *ddb,
  3965. struct intel_crtc_state *cstate,
  3966. const struct intel_plane_state *intel_pstate,
  3967. const struct skl_wm_params *wm_params,
  3968. struct skl_plane_wm *wm,
  3969. int plane_id)
  3970. {
  3971. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  3972. struct drm_plane *plane = intel_pstate->base.plane;
  3973. struct intel_plane *intel_plane = to_intel_plane(plane);
  3974. uint16_t ddb_blocks;
  3975. enum pipe pipe = intel_crtc->pipe;
  3976. int level, max_level = ilk_wm_max_level(dev_priv);
  3977. enum plane_id intel_plane_id = intel_plane->id;
  3978. int ret;
  3979. if (WARN_ON(!intel_pstate->base.fb))
  3980. return -EINVAL;
  3981. ddb_blocks = plane_id ?
  3982. skl_ddb_entry_size(&ddb->uv_plane[pipe][intel_plane_id]) :
  3983. skl_ddb_entry_size(&ddb->plane[pipe][intel_plane_id]);
  3984. for (level = 0; level <= max_level; level++) {
  3985. struct skl_wm_level *result = plane_id ? &wm->uv_wm[level] :
  3986. &wm->wm[level];
  3987. struct skl_wm_level *result_prev;
  3988. if (level)
  3989. result_prev = plane_id ? &wm->uv_wm[level - 1] :
  3990. &wm->wm[level - 1];
  3991. else
  3992. result_prev = plane_id ? &wm->uv_wm[0] : &wm->wm[0];
  3993. ret = skl_compute_plane_wm(dev_priv,
  3994. cstate,
  3995. intel_pstate,
  3996. ddb_blocks,
  3997. level,
  3998. wm_params,
  3999. result_prev,
  4000. result);
  4001. if (ret)
  4002. return ret;
  4003. }
  4004. if (intel_pstate->base.fb->format->format == DRM_FORMAT_NV12)
  4005. wm->is_planar = true;
  4006. return 0;
  4007. }
  4008. static uint32_t
  4009. skl_compute_linetime_wm(struct intel_crtc_state *cstate)
  4010. {
  4011. struct drm_atomic_state *state = cstate->base.state;
  4012. struct drm_i915_private *dev_priv = to_i915(state->dev);
  4013. uint_fixed_16_16_t linetime_us;
  4014. uint32_t linetime_wm;
  4015. linetime_us = intel_get_linetime_us(cstate);
  4016. if (is_fixed16_zero(linetime_us))
  4017. return 0;
  4018. linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
  4019. /* Display WA #1135: bxt:ALL GLK:ALL */
  4020. if ((IS_BROXTON(dev_priv) || IS_GEMINILAKE(dev_priv)) &&
  4021. dev_priv->ipc_enabled)
  4022. linetime_wm /= 2;
  4023. return linetime_wm;
  4024. }
  4025. static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
  4026. struct skl_wm_params *wp,
  4027. struct skl_wm_level *wm_l0,
  4028. uint16_t ddb_allocation,
  4029. struct skl_wm_level *trans_wm /* out */)
  4030. {
  4031. struct drm_device *dev = cstate->base.crtc->dev;
  4032. const struct drm_i915_private *dev_priv = to_i915(dev);
  4033. uint16_t trans_min, trans_y_tile_min;
  4034. const uint16_t trans_amount = 10; /* This is configurable amount */
  4035. uint16_t trans_offset_b, res_blocks;
  4036. if (!cstate->base.active)
  4037. goto exit;
  4038. /* Transition WM are not recommended by HW team for GEN9 */
  4039. if (INTEL_GEN(dev_priv) <= 9)
  4040. goto exit;
  4041. /* Transition WM don't make any sense if ipc is disabled */
  4042. if (!dev_priv->ipc_enabled)
  4043. goto exit;
  4044. trans_min = 0;
  4045. if (INTEL_GEN(dev_priv) >= 10)
  4046. trans_min = 4;
  4047. trans_offset_b = trans_min + trans_amount;
  4048. if (wp->y_tiled) {
  4049. trans_y_tile_min = (uint16_t) mul_round_up_u32_fixed16(2,
  4050. wp->y_tile_minimum);
  4051. res_blocks = max(wm_l0->plane_res_b, trans_y_tile_min) +
  4052. trans_offset_b;
  4053. } else {
  4054. res_blocks = wm_l0->plane_res_b + trans_offset_b;
  4055. /* WA BUG:1938466 add one block for non y-tile planes */
  4056. if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
  4057. res_blocks += 1;
  4058. }
  4059. res_blocks += 1;
  4060. if (res_blocks < ddb_allocation) {
  4061. trans_wm->plane_res_b = res_blocks;
  4062. trans_wm->plane_en = true;
  4063. return;
  4064. }
  4065. exit:
  4066. trans_wm->plane_en = false;
  4067. }
  4068. static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
  4069. struct skl_ddb_allocation *ddb,
  4070. struct skl_pipe_wm *pipe_wm)
  4071. {
  4072. struct drm_device *dev = cstate->base.crtc->dev;
  4073. struct drm_crtc_state *crtc_state = &cstate->base;
  4074. const struct drm_i915_private *dev_priv = to_i915(dev);
  4075. struct drm_plane *plane;
  4076. const struct drm_plane_state *pstate;
  4077. struct skl_plane_wm *wm;
  4078. int ret;
  4079. /*
  4080. * We'll only calculate watermarks for planes that are actually
  4081. * enabled, so make sure all other planes are set as disabled.
  4082. */
  4083. memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
  4084. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
  4085. const struct intel_plane_state *intel_pstate =
  4086. to_intel_plane_state(pstate);
  4087. enum plane_id plane_id = to_intel_plane(plane)->id;
  4088. struct skl_wm_params wm_params;
  4089. enum pipe pipe = to_intel_crtc(cstate->base.crtc)->pipe;
  4090. uint16_t ddb_blocks;
  4091. wm = &pipe_wm->planes[plane_id];
  4092. ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][plane_id]);
  4093. ret = skl_compute_plane_wm_params(dev_priv, cstate,
  4094. intel_pstate, &wm_params, 0);
  4095. if (ret)
  4096. return ret;
  4097. ret = skl_compute_wm_levels(dev_priv, ddb, cstate,
  4098. intel_pstate, &wm_params, wm, 0);
  4099. if (ret)
  4100. return ret;
  4101. skl_compute_transition_wm(cstate, &wm_params, &wm->wm[0],
  4102. ddb_blocks, &wm->trans_wm);
  4103. /* uv plane watermarks must also be validated for NV12/Planar */
  4104. if (wm_params.is_planar) {
  4105. memset(&wm_params, 0, sizeof(struct skl_wm_params));
  4106. wm->is_planar = true;
  4107. ret = skl_compute_plane_wm_params(dev_priv, cstate,
  4108. intel_pstate,
  4109. &wm_params, 1);
  4110. if (ret)
  4111. return ret;
  4112. ret = skl_compute_wm_levels(dev_priv, ddb, cstate,
  4113. intel_pstate, &wm_params,
  4114. wm, 1);
  4115. if (ret)
  4116. return ret;
  4117. }
  4118. }
  4119. pipe_wm->linetime = skl_compute_linetime_wm(cstate);
  4120. return 0;
  4121. }
  4122. static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
  4123. i915_reg_t reg,
  4124. const struct skl_ddb_entry *entry)
  4125. {
  4126. if (entry->end)
  4127. I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
  4128. else
  4129. I915_WRITE(reg, 0);
  4130. }
  4131. static void skl_write_wm_level(struct drm_i915_private *dev_priv,
  4132. i915_reg_t reg,
  4133. const struct skl_wm_level *level)
  4134. {
  4135. uint32_t val = 0;
  4136. if (level->plane_en) {
  4137. val |= PLANE_WM_EN;
  4138. val |= level->plane_res_b;
  4139. val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
  4140. }
  4141. I915_WRITE(reg, val);
  4142. }
  4143. static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
  4144. const struct skl_plane_wm *wm,
  4145. const struct skl_ddb_allocation *ddb,
  4146. enum plane_id plane_id)
  4147. {
  4148. struct drm_crtc *crtc = &intel_crtc->base;
  4149. struct drm_device *dev = crtc->dev;
  4150. struct drm_i915_private *dev_priv = to_i915(dev);
  4151. int level, max_level = ilk_wm_max_level(dev_priv);
  4152. enum pipe pipe = intel_crtc->pipe;
  4153. for (level = 0; level <= max_level; level++) {
  4154. skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
  4155. &wm->wm[level]);
  4156. }
  4157. skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
  4158. &wm->trans_wm);
  4159. skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
  4160. &ddb->plane[pipe][plane_id]);
  4161. if (INTEL_GEN(dev_priv) >= 11)
  4162. return skl_ddb_entry_write(dev_priv,
  4163. PLANE_BUF_CFG(pipe, plane_id),
  4164. &ddb->plane[pipe][plane_id]);
  4165. if (wm->is_planar) {
  4166. skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
  4167. &ddb->uv_plane[pipe][plane_id]);
  4168. skl_ddb_entry_write(dev_priv,
  4169. PLANE_NV12_BUF_CFG(pipe, plane_id),
  4170. &ddb->plane[pipe][plane_id]);
  4171. } else {
  4172. skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
  4173. &ddb->plane[pipe][plane_id]);
  4174. I915_WRITE(PLANE_NV12_BUF_CFG(pipe, plane_id), 0x0);
  4175. }
  4176. }
  4177. static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
  4178. const struct skl_plane_wm *wm,
  4179. const struct skl_ddb_allocation *ddb)
  4180. {
  4181. struct drm_crtc *crtc = &intel_crtc->base;
  4182. struct drm_device *dev = crtc->dev;
  4183. struct drm_i915_private *dev_priv = to_i915(dev);
  4184. int level, max_level = ilk_wm_max_level(dev_priv);
  4185. enum pipe pipe = intel_crtc->pipe;
  4186. for (level = 0; level <= max_level; level++) {
  4187. skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
  4188. &wm->wm[level]);
  4189. }
  4190. skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
  4191. skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
  4192. &ddb->plane[pipe][PLANE_CURSOR]);
  4193. }
  4194. bool skl_wm_level_equals(const struct skl_wm_level *l1,
  4195. const struct skl_wm_level *l2)
  4196. {
  4197. if (l1->plane_en != l2->plane_en)
  4198. return false;
  4199. /* If both planes aren't enabled, the rest shouldn't matter */
  4200. if (!l1->plane_en)
  4201. return true;
  4202. return (l1->plane_res_l == l2->plane_res_l &&
  4203. l1->plane_res_b == l2->plane_res_b);
  4204. }
  4205. static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
  4206. const struct skl_ddb_entry *b)
  4207. {
  4208. return a->start < b->end && b->start < a->end;
  4209. }
  4210. bool skl_ddb_allocation_overlaps(struct drm_i915_private *dev_priv,
  4211. const struct skl_ddb_entry **entries,
  4212. const struct skl_ddb_entry *ddb,
  4213. int ignore)
  4214. {
  4215. enum pipe pipe;
  4216. for_each_pipe(dev_priv, pipe) {
  4217. if (pipe != ignore && entries[pipe] &&
  4218. skl_ddb_entries_overlap(ddb, entries[pipe]))
  4219. return true;
  4220. }
  4221. return false;
  4222. }
  4223. static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
  4224. const struct skl_pipe_wm *old_pipe_wm,
  4225. struct skl_pipe_wm *pipe_wm, /* out */
  4226. struct skl_ddb_allocation *ddb, /* out */
  4227. bool *changed /* out */)
  4228. {
  4229. struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
  4230. int ret;
  4231. ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
  4232. if (ret)
  4233. return ret;
  4234. if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
  4235. *changed = false;
  4236. else
  4237. *changed = true;
  4238. return 0;
  4239. }
  4240. static uint32_t
  4241. pipes_modified(struct drm_atomic_state *state)
  4242. {
  4243. struct drm_crtc *crtc;
  4244. struct drm_crtc_state *cstate;
  4245. uint32_t i, ret = 0;
  4246. for_each_new_crtc_in_state(state, crtc, cstate, i)
  4247. ret |= drm_crtc_mask(crtc);
  4248. return ret;
  4249. }
  4250. static int
  4251. skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
  4252. {
  4253. struct drm_atomic_state *state = cstate->base.state;
  4254. struct drm_device *dev = state->dev;
  4255. struct drm_crtc *crtc = cstate->base.crtc;
  4256. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4257. struct drm_i915_private *dev_priv = to_i915(dev);
  4258. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  4259. struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
  4260. struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
  4261. struct drm_plane_state *plane_state;
  4262. struct drm_plane *plane;
  4263. enum pipe pipe = intel_crtc->pipe;
  4264. drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
  4265. enum plane_id plane_id = to_intel_plane(plane)->id;
  4266. if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
  4267. &new_ddb->plane[pipe][plane_id]) &&
  4268. skl_ddb_entry_equal(&cur_ddb->uv_plane[pipe][plane_id],
  4269. &new_ddb->uv_plane[pipe][plane_id]))
  4270. continue;
  4271. plane_state = drm_atomic_get_plane_state(state, plane);
  4272. if (IS_ERR(plane_state))
  4273. return PTR_ERR(plane_state);
  4274. }
  4275. return 0;
  4276. }
  4277. static int
  4278. skl_compute_ddb(struct drm_atomic_state *state)
  4279. {
  4280. const struct drm_i915_private *dev_priv = to_i915(state->dev);
  4281. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  4282. struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
  4283. struct intel_crtc *crtc;
  4284. struct intel_crtc_state *cstate;
  4285. int ret, i;
  4286. memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
  4287. for_each_new_intel_crtc_in_state(intel_state, crtc, cstate, i) {
  4288. ret = skl_allocate_pipe_ddb(cstate, ddb);
  4289. if (ret)
  4290. return ret;
  4291. ret = skl_ddb_add_affected_planes(cstate);
  4292. if (ret)
  4293. return ret;
  4294. }
  4295. return 0;
  4296. }
  4297. static void
  4298. skl_copy_ddb_for_pipe(struct skl_ddb_values *dst,
  4299. struct skl_ddb_values *src,
  4300. enum pipe pipe)
  4301. {
  4302. memcpy(dst->ddb.uv_plane[pipe], src->ddb.uv_plane[pipe],
  4303. sizeof(dst->ddb.uv_plane[pipe]));
  4304. memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
  4305. sizeof(dst->ddb.plane[pipe]));
  4306. }
  4307. static void
  4308. skl_print_wm_changes(const struct drm_atomic_state *state)
  4309. {
  4310. const struct drm_device *dev = state->dev;
  4311. const struct drm_i915_private *dev_priv = to_i915(dev);
  4312. const struct intel_atomic_state *intel_state =
  4313. to_intel_atomic_state(state);
  4314. const struct drm_crtc *crtc;
  4315. const struct drm_crtc_state *cstate;
  4316. const struct intel_plane *intel_plane;
  4317. const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
  4318. const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
  4319. int i;
  4320. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  4321. const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4322. enum pipe pipe = intel_crtc->pipe;
  4323. for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
  4324. enum plane_id plane_id = intel_plane->id;
  4325. const struct skl_ddb_entry *old, *new;
  4326. old = &old_ddb->plane[pipe][plane_id];
  4327. new = &new_ddb->plane[pipe][plane_id];
  4328. if (skl_ddb_entry_equal(old, new))
  4329. continue;
  4330. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
  4331. intel_plane->base.base.id,
  4332. intel_plane->base.name,
  4333. old->start, old->end,
  4334. new->start, new->end);
  4335. }
  4336. }
  4337. }
  4338. static int
  4339. skl_ddb_add_affected_pipes(struct drm_atomic_state *state, bool *changed)
  4340. {
  4341. struct drm_device *dev = state->dev;
  4342. const struct drm_i915_private *dev_priv = to_i915(dev);
  4343. const struct drm_crtc *crtc;
  4344. const struct drm_crtc_state *cstate;
  4345. struct intel_crtc *intel_crtc;
  4346. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  4347. uint32_t realloc_pipes = pipes_modified(state);
  4348. int ret, i;
  4349. /*
  4350. * When we distrust bios wm we always need to recompute to set the
  4351. * expected DDB allocations for each CRTC.
  4352. */
  4353. if (dev_priv->wm.distrust_bios_wm)
  4354. (*changed) = true;
  4355. /*
  4356. * If this transaction isn't actually touching any CRTC's, don't
  4357. * bother with watermark calculation. Note that if we pass this
  4358. * test, we're guaranteed to hold at least one CRTC state mutex,
  4359. * which means we can safely use values like dev_priv->active_crtcs
  4360. * since any racing commits that want to update them would need to
  4361. * hold _all_ CRTC state mutexes.
  4362. */
  4363. for_each_new_crtc_in_state(state, crtc, cstate, i)
  4364. (*changed) = true;
  4365. if (!*changed)
  4366. return 0;
  4367. /*
  4368. * If this is our first atomic update following hardware readout,
  4369. * we can't trust the DDB that the BIOS programmed for us. Let's
  4370. * pretend that all pipes switched active status so that we'll
  4371. * ensure a full DDB recompute.
  4372. */
  4373. if (dev_priv->wm.distrust_bios_wm) {
  4374. ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
  4375. state->acquire_ctx);
  4376. if (ret)
  4377. return ret;
  4378. intel_state->active_pipe_changes = ~0;
  4379. /*
  4380. * We usually only initialize intel_state->active_crtcs if we
  4381. * we're doing a modeset; make sure this field is always
  4382. * initialized during the sanitization process that happens
  4383. * on the first commit too.
  4384. */
  4385. if (!intel_state->modeset)
  4386. intel_state->active_crtcs = dev_priv->active_crtcs;
  4387. }
  4388. /*
  4389. * If the modeset changes which CRTC's are active, we need to
  4390. * recompute the DDB allocation for *all* active pipes, even
  4391. * those that weren't otherwise being modified in any way by this
  4392. * atomic commit. Due to the shrinking of the per-pipe allocations
  4393. * when new active CRTC's are added, it's possible for a pipe that
  4394. * we were already using and aren't changing at all here to suddenly
  4395. * become invalid if its DDB needs exceeds its new allocation.
  4396. *
  4397. * Note that if we wind up doing a full DDB recompute, we can't let
  4398. * any other display updates race with this transaction, so we need
  4399. * to grab the lock on *all* CRTC's.
  4400. */
  4401. if (intel_state->active_pipe_changes) {
  4402. realloc_pipes = ~0;
  4403. intel_state->wm_results.dirty_pipes = ~0;
  4404. }
  4405. /*
  4406. * We're not recomputing for the pipes not included in the commit, so
  4407. * make sure we start with the current state.
  4408. */
  4409. for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
  4410. struct intel_crtc_state *cstate;
  4411. cstate = intel_atomic_get_crtc_state(state, intel_crtc);
  4412. if (IS_ERR(cstate))
  4413. return PTR_ERR(cstate);
  4414. }
  4415. return 0;
  4416. }
  4417. static int
  4418. skl_compute_wm(struct drm_atomic_state *state)
  4419. {
  4420. struct drm_crtc *crtc;
  4421. struct drm_crtc_state *cstate;
  4422. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  4423. struct skl_ddb_values *results = &intel_state->wm_results;
  4424. struct skl_pipe_wm *pipe_wm;
  4425. bool changed = false;
  4426. int ret, i;
  4427. /* Clear all dirty flags */
  4428. results->dirty_pipes = 0;
  4429. ret = skl_ddb_add_affected_pipes(state, &changed);
  4430. if (ret || !changed)
  4431. return ret;
  4432. ret = skl_compute_ddb(state);
  4433. if (ret)
  4434. return ret;
  4435. /*
  4436. * Calculate WM's for all pipes that are part of this transaction.
  4437. * Note that the DDB allocation above may have added more CRTC's that
  4438. * weren't otherwise being modified (and set bits in dirty_pipes) if
  4439. * pipe allocations had to change.
  4440. *
  4441. * FIXME: Now that we're doing this in the atomic check phase, we
  4442. * should allow skl_update_pipe_wm() to return failure in cases where
  4443. * no suitable watermark values can be found.
  4444. */
  4445. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  4446. struct intel_crtc_state *intel_cstate =
  4447. to_intel_crtc_state(cstate);
  4448. const struct skl_pipe_wm *old_pipe_wm =
  4449. &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
  4450. pipe_wm = &intel_cstate->wm.skl.optimal;
  4451. ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
  4452. &results->ddb, &changed);
  4453. if (ret)
  4454. return ret;
  4455. if (changed)
  4456. results->dirty_pipes |= drm_crtc_mask(crtc);
  4457. if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
  4458. /* This pipe's WM's did not change */
  4459. continue;
  4460. intel_cstate->update_wm_pre = true;
  4461. }
  4462. skl_print_wm_changes(state);
  4463. return 0;
  4464. }
  4465. static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
  4466. struct intel_crtc_state *cstate)
  4467. {
  4468. struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
  4469. struct drm_i915_private *dev_priv = to_i915(state->base.dev);
  4470. struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
  4471. const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
  4472. enum pipe pipe = crtc->pipe;
  4473. enum plane_id plane_id;
  4474. if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
  4475. return;
  4476. I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
  4477. for_each_plane_id_on_crtc(crtc, plane_id) {
  4478. if (plane_id != PLANE_CURSOR)
  4479. skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
  4480. ddb, plane_id);
  4481. else
  4482. skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
  4483. ddb);
  4484. }
  4485. }
  4486. static void skl_initial_wm(struct intel_atomic_state *state,
  4487. struct intel_crtc_state *cstate)
  4488. {
  4489. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  4490. struct drm_device *dev = intel_crtc->base.dev;
  4491. struct drm_i915_private *dev_priv = to_i915(dev);
  4492. struct skl_ddb_values *results = &state->wm_results;
  4493. struct skl_ddb_values *hw_vals = &dev_priv->wm.skl_hw;
  4494. enum pipe pipe = intel_crtc->pipe;
  4495. if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
  4496. return;
  4497. mutex_lock(&dev_priv->wm.wm_mutex);
  4498. if (cstate->base.active_changed)
  4499. skl_atomic_update_crtc_wm(state, cstate);
  4500. skl_copy_ddb_for_pipe(hw_vals, results, pipe);
  4501. mutex_unlock(&dev_priv->wm.wm_mutex);
  4502. }
  4503. static void ilk_compute_wm_config(struct drm_device *dev,
  4504. struct intel_wm_config *config)
  4505. {
  4506. struct intel_crtc *crtc;
  4507. /* Compute the currently _active_ config */
  4508. for_each_intel_crtc(dev, crtc) {
  4509. const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
  4510. if (!wm->pipe_enabled)
  4511. continue;
  4512. config->sprites_enabled |= wm->sprites_enabled;
  4513. config->sprites_scaled |= wm->sprites_scaled;
  4514. config->num_pipes_active++;
  4515. }
  4516. }
  4517. static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
  4518. {
  4519. struct drm_device *dev = &dev_priv->drm;
  4520. struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
  4521. struct ilk_wm_maximums max;
  4522. struct intel_wm_config config = {};
  4523. struct ilk_wm_values results = {};
  4524. enum intel_ddb_partitioning partitioning;
  4525. ilk_compute_wm_config(dev, &config);
  4526. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
  4527. ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
  4528. /* 5/6 split only in single pipe config on IVB+ */
  4529. if (INTEL_GEN(dev_priv) >= 7 &&
  4530. config.num_pipes_active == 1 && config.sprites_enabled) {
  4531. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
  4532. ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
  4533. best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
  4534. } else {
  4535. best_lp_wm = &lp_wm_1_2;
  4536. }
  4537. partitioning = (best_lp_wm == &lp_wm_1_2) ?
  4538. INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
  4539. ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
  4540. ilk_write_wm_values(dev_priv, &results);
  4541. }
  4542. static void ilk_initial_watermarks(struct intel_atomic_state *state,
  4543. struct intel_crtc_state *cstate)
  4544. {
  4545. struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
  4546. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  4547. mutex_lock(&dev_priv->wm.wm_mutex);
  4548. intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
  4549. ilk_program_watermarks(dev_priv);
  4550. mutex_unlock(&dev_priv->wm.wm_mutex);
  4551. }
  4552. static void ilk_optimize_watermarks(struct intel_atomic_state *state,
  4553. struct intel_crtc_state *cstate)
  4554. {
  4555. struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
  4556. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  4557. mutex_lock(&dev_priv->wm.wm_mutex);
  4558. if (cstate->wm.need_postvbl_update) {
  4559. intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
  4560. ilk_program_watermarks(dev_priv);
  4561. }
  4562. mutex_unlock(&dev_priv->wm.wm_mutex);
  4563. }
  4564. static inline void skl_wm_level_from_reg_val(uint32_t val,
  4565. struct skl_wm_level *level)
  4566. {
  4567. level->plane_en = val & PLANE_WM_EN;
  4568. level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
  4569. level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
  4570. PLANE_WM_LINES_MASK;
  4571. }
  4572. void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
  4573. struct skl_pipe_wm *out)
  4574. {
  4575. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4576. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4577. enum pipe pipe = intel_crtc->pipe;
  4578. int level, max_level;
  4579. enum plane_id plane_id;
  4580. uint32_t val;
  4581. max_level = ilk_wm_max_level(dev_priv);
  4582. for_each_plane_id_on_crtc(intel_crtc, plane_id) {
  4583. struct skl_plane_wm *wm = &out->planes[plane_id];
  4584. for (level = 0; level <= max_level; level++) {
  4585. if (plane_id != PLANE_CURSOR)
  4586. val = I915_READ(PLANE_WM(pipe, plane_id, level));
  4587. else
  4588. val = I915_READ(CUR_WM(pipe, level));
  4589. skl_wm_level_from_reg_val(val, &wm->wm[level]);
  4590. }
  4591. if (plane_id != PLANE_CURSOR)
  4592. val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
  4593. else
  4594. val = I915_READ(CUR_WM_TRANS(pipe));
  4595. skl_wm_level_from_reg_val(val, &wm->trans_wm);
  4596. }
  4597. if (!intel_crtc->active)
  4598. return;
  4599. out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
  4600. }
  4601. void skl_wm_get_hw_state(struct drm_device *dev)
  4602. {
  4603. struct drm_i915_private *dev_priv = to_i915(dev);
  4604. struct skl_ddb_values *hw = &dev_priv->wm.skl_hw;
  4605. struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
  4606. struct drm_crtc *crtc;
  4607. struct intel_crtc *intel_crtc;
  4608. struct intel_crtc_state *cstate;
  4609. skl_ddb_get_hw_state(dev_priv, ddb);
  4610. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4611. intel_crtc = to_intel_crtc(crtc);
  4612. cstate = to_intel_crtc_state(crtc->state);
  4613. skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
  4614. if (intel_crtc->active)
  4615. hw->dirty_pipes |= drm_crtc_mask(crtc);
  4616. }
  4617. if (dev_priv->active_crtcs) {
  4618. /* Fully recompute DDB on first atomic commit */
  4619. dev_priv->wm.distrust_bios_wm = true;
  4620. } else {
  4621. /*
  4622. * Easy/common case; just sanitize DDB now if everything off
  4623. * Keep dbuf slice info intact
  4624. */
  4625. memset(ddb->plane, 0, sizeof(ddb->plane));
  4626. memset(ddb->uv_plane, 0, sizeof(ddb->uv_plane));
  4627. }
  4628. }
  4629. static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
  4630. {
  4631. struct drm_device *dev = crtc->dev;
  4632. struct drm_i915_private *dev_priv = to_i915(dev);
  4633. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  4634. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4635. struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
  4636. struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
  4637. enum pipe pipe = intel_crtc->pipe;
  4638. static const i915_reg_t wm0_pipe_reg[] = {
  4639. [PIPE_A] = WM0_PIPEA_ILK,
  4640. [PIPE_B] = WM0_PIPEB_ILK,
  4641. [PIPE_C] = WM0_PIPEC_IVB,
  4642. };
  4643. hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
  4644. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4645. hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
  4646. memset(active, 0, sizeof(*active));
  4647. active->pipe_enabled = intel_crtc->active;
  4648. if (active->pipe_enabled) {
  4649. u32 tmp = hw->wm_pipe[pipe];
  4650. /*
  4651. * For active pipes LP0 watermark is marked as
  4652. * enabled, and LP1+ watermaks as disabled since
  4653. * we can't really reverse compute them in case
  4654. * multiple pipes are active.
  4655. */
  4656. active->wm[0].enable = true;
  4657. active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
  4658. active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
  4659. active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
  4660. active->linetime = hw->wm_linetime[pipe];
  4661. } else {
  4662. int level, max_level = ilk_wm_max_level(dev_priv);
  4663. /*
  4664. * For inactive pipes, all watermark levels
  4665. * should be marked as enabled but zeroed,
  4666. * which is what we'd compute them to.
  4667. */
  4668. for (level = 0; level <= max_level; level++)
  4669. active->wm[level].enable = true;
  4670. }
  4671. intel_crtc->wm.active.ilk = *active;
  4672. }
  4673. #define _FW_WM(value, plane) \
  4674. (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
  4675. #define _FW_WM_VLV(value, plane) \
  4676. (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
  4677. static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
  4678. struct g4x_wm_values *wm)
  4679. {
  4680. uint32_t tmp;
  4681. tmp = I915_READ(DSPFW1);
  4682. wm->sr.plane = _FW_WM(tmp, SR);
  4683. wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
  4684. wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
  4685. wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
  4686. tmp = I915_READ(DSPFW2);
  4687. wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
  4688. wm->sr.fbc = _FW_WM(tmp, FBC_SR);
  4689. wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
  4690. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
  4691. wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
  4692. wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
  4693. tmp = I915_READ(DSPFW3);
  4694. wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
  4695. wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
  4696. wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
  4697. wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
  4698. }
  4699. static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
  4700. struct vlv_wm_values *wm)
  4701. {
  4702. enum pipe pipe;
  4703. uint32_t tmp;
  4704. for_each_pipe(dev_priv, pipe) {
  4705. tmp = I915_READ(VLV_DDL(pipe));
  4706. wm->ddl[pipe].plane[PLANE_PRIMARY] =
  4707. (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  4708. wm->ddl[pipe].plane[PLANE_CURSOR] =
  4709. (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  4710. wm->ddl[pipe].plane[PLANE_SPRITE0] =
  4711. (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  4712. wm->ddl[pipe].plane[PLANE_SPRITE1] =
  4713. (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  4714. }
  4715. tmp = I915_READ(DSPFW1);
  4716. wm->sr.plane = _FW_WM(tmp, SR);
  4717. wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
  4718. wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
  4719. wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
  4720. tmp = I915_READ(DSPFW2);
  4721. wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
  4722. wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
  4723. wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
  4724. tmp = I915_READ(DSPFW3);
  4725. wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
  4726. if (IS_CHERRYVIEW(dev_priv)) {
  4727. tmp = I915_READ(DSPFW7_CHV);
  4728. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
  4729. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
  4730. tmp = I915_READ(DSPFW8_CHV);
  4731. wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
  4732. wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
  4733. tmp = I915_READ(DSPFW9_CHV);
  4734. wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
  4735. wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
  4736. tmp = I915_READ(DSPHOWM);
  4737. wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
  4738. wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
  4739. wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
  4740. wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
  4741. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
  4742. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
  4743. wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
  4744. wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
  4745. wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
  4746. wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
  4747. } else {
  4748. tmp = I915_READ(DSPFW7);
  4749. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
  4750. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
  4751. tmp = I915_READ(DSPHOWM);
  4752. wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
  4753. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
  4754. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
  4755. wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
  4756. wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
  4757. wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
  4758. wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
  4759. }
  4760. }
  4761. #undef _FW_WM
  4762. #undef _FW_WM_VLV
  4763. void g4x_wm_get_hw_state(struct drm_device *dev)
  4764. {
  4765. struct drm_i915_private *dev_priv = to_i915(dev);
  4766. struct g4x_wm_values *wm = &dev_priv->wm.g4x;
  4767. struct intel_crtc *crtc;
  4768. g4x_read_wm_values(dev_priv, wm);
  4769. wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
  4770. for_each_intel_crtc(dev, crtc) {
  4771. struct intel_crtc_state *crtc_state =
  4772. to_intel_crtc_state(crtc->base.state);
  4773. struct g4x_wm_state *active = &crtc->wm.active.g4x;
  4774. struct g4x_pipe_wm *raw;
  4775. enum pipe pipe = crtc->pipe;
  4776. enum plane_id plane_id;
  4777. int level, max_level;
  4778. active->cxsr = wm->cxsr;
  4779. active->hpll_en = wm->hpll_en;
  4780. active->fbc_en = wm->fbc_en;
  4781. active->sr = wm->sr;
  4782. active->hpll = wm->hpll;
  4783. for_each_plane_id_on_crtc(crtc, plane_id) {
  4784. active->wm.plane[plane_id] =
  4785. wm->pipe[pipe].plane[plane_id];
  4786. }
  4787. if (wm->cxsr && wm->hpll_en)
  4788. max_level = G4X_WM_LEVEL_HPLL;
  4789. else if (wm->cxsr)
  4790. max_level = G4X_WM_LEVEL_SR;
  4791. else
  4792. max_level = G4X_WM_LEVEL_NORMAL;
  4793. level = G4X_WM_LEVEL_NORMAL;
  4794. raw = &crtc_state->wm.g4x.raw[level];
  4795. for_each_plane_id_on_crtc(crtc, plane_id)
  4796. raw->plane[plane_id] = active->wm.plane[plane_id];
  4797. if (++level > max_level)
  4798. goto out;
  4799. raw = &crtc_state->wm.g4x.raw[level];
  4800. raw->plane[PLANE_PRIMARY] = active->sr.plane;
  4801. raw->plane[PLANE_CURSOR] = active->sr.cursor;
  4802. raw->plane[PLANE_SPRITE0] = 0;
  4803. raw->fbc = active->sr.fbc;
  4804. if (++level > max_level)
  4805. goto out;
  4806. raw = &crtc_state->wm.g4x.raw[level];
  4807. raw->plane[PLANE_PRIMARY] = active->hpll.plane;
  4808. raw->plane[PLANE_CURSOR] = active->hpll.cursor;
  4809. raw->plane[PLANE_SPRITE0] = 0;
  4810. raw->fbc = active->hpll.fbc;
  4811. out:
  4812. for_each_plane_id_on_crtc(crtc, plane_id)
  4813. g4x_raw_plane_wm_set(crtc_state, level,
  4814. plane_id, USHRT_MAX);
  4815. g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
  4816. crtc_state->wm.g4x.optimal = *active;
  4817. crtc_state->wm.g4x.intermediate = *active;
  4818. DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
  4819. pipe_name(pipe),
  4820. wm->pipe[pipe].plane[PLANE_PRIMARY],
  4821. wm->pipe[pipe].plane[PLANE_CURSOR],
  4822. wm->pipe[pipe].plane[PLANE_SPRITE0]);
  4823. }
  4824. DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
  4825. wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
  4826. DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
  4827. wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
  4828. DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
  4829. yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
  4830. }
  4831. void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
  4832. {
  4833. struct intel_plane *plane;
  4834. struct intel_crtc *crtc;
  4835. mutex_lock(&dev_priv->wm.wm_mutex);
  4836. for_each_intel_plane(&dev_priv->drm, plane) {
  4837. struct intel_crtc *crtc =
  4838. intel_get_crtc_for_pipe(dev_priv, plane->pipe);
  4839. struct intel_crtc_state *crtc_state =
  4840. to_intel_crtc_state(crtc->base.state);
  4841. struct intel_plane_state *plane_state =
  4842. to_intel_plane_state(plane->base.state);
  4843. struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
  4844. enum plane_id plane_id = plane->id;
  4845. int level;
  4846. if (plane_state->base.visible)
  4847. continue;
  4848. for (level = 0; level < 3; level++) {
  4849. struct g4x_pipe_wm *raw =
  4850. &crtc_state->wm.g4x.raw[level];
  4851. raw->plane[plane_id] = 0;
  4852. wm_state->wm.plane[plane_id] = 0;
  4853. }
  4854. if (plane_id == PLANE_PRIMARY) {
  4855. for (level = 0; level < 3; level++) {
  4856. struct g4x_pipe_wm *raw =
  4857. &crtc_state->wm.g4x.raw[level];
  4858. raw->fbc = 0;
  4859. }
  4860. wm_state->sr.fbc = 0;
  4861. wm_state->hpll.fbc = 0;
  4862. wm_state->fbc_en = false;
  4863. }
  4864. }
  4865. for_each_intel_crtc(&dev_priv->drm, crtc) {
  4866. struct intel_crtc_state *crtc_state =
  4867. to_intel_crtc_state(crtc->base.state);
  4868. crtc_state->wm.g4x.intermediate =
  4869. crtc_state->wm.g4x.optimal;
  4870. crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
  4871. }
  4872. g4x_program_watermarks(dev_priv);
  4873. mutex_unlock(&dev_priv->wm.wm_mutex);
  4874. }
  4875. void vlv_wm_get_hw_state(struct drm_device *dev)
  4876. {
  4877. struct drm_i915_private *dev_priv = to_i915(dev);
  4878. struct vlv_wm_values *wm = &dev_priv->wm.vlv;
  4879. struct intel_crtc *crtc;
  4880. u32 val;
  4881. vlv_read_wm_values(dev_priv, wm);
  4882. wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
  4883. wm->level = VLV_WM_LEVEL_PM2;
  4884. if (IS_CHERRYVIEW(dev_priv)) {
  4885. mutex_lock(&dev_priv->pcu_lock);
  4886. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4887. if (val & DSP_MAXFIFO_PM5_ENABLE)
  4888. wm->level = VLV_WM_LEVEL_PM5;
  4889. /*
  4890. * If DDR DVFS is disabled in the BIOS, Punit
  4891. * will never ack the request. So if that happens
  4892. * assume we don't have to enable/disable DDR DVFS
  4893. * dynamically. To test that just set the REQ_ACK
  4894. * bit to poke the Punit, but don't change the
  4895. * HIGH/LOW bits so that we don't actually change
  4896. * the current state.
  4897. */
  4898. val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
  4899. val |= FORCE_DDR_FREQ_REQ_ACK;
  4900. vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
  4901. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
  4902. FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
  4903. DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
  4904. "assuming DDR DVFS is disabled\n");
  4905. dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
  4906. } else {
  4907. val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
  4908. if ((val & FORCE_DDR_HIGH_FREQ) == 0)
  4909. wm->level = VLV_WM_LEVEL_DDR_DVFS;
  4910. }
  4911. mutex_unlock(&dev_priv->pcu_lock);
  4912. }
  4913. for_each_intel_crtc(dev, crtc) {
  4914. struct intel_crtc_state *crtc_state =
  4915. to_intel_crtc_state(crtc->base.state);
  4916. struct vlv_wm_state *active = &crtc->wm.active.vlv;
  4917. const struct vlv_fifo_state *fifo_state =
  4918. &crtc_state->wm.vlv.fifo_state;
  4919. enum pipe pipe = crtc->pipe;
  4920. enum plane_id plane_id;
  4921. int level;
  4922. vlv_get_fifo_size(crtc_state);
  4923. active->num_levels = wm->level + 1;
  4924. active->cxsr = wm->cxsr;
  4925. for (level = 0; level < active->num_levels; level++) {
  4926. struct g4x_pipe_wm *raw =
  4927. &crtc_state->wm.vlv.raw[level];
  4928. active->sr[level].plane = wm->sr.plane;
  4929. active->sr[level].cursor = wm->sr.cursor;
  4930. for_each_plane_id_on_crtc(crtc, plane_id) {
  4931. active->wm[level].plane[plane_id] =
  4932. wm->pipe[pipe].plane[plane_id];
  4933. raw->plane[plane_id] =
  4934. vlv_invert_wm_value(active->wm[level].plane[plane_id],
  4935. fifo_state->plane[plane_id]);
  4936. }
  4937. }
  4938. for_each_plane_id_on_crtc(crtc, plane_id)
  4939. vlv_raw_plane_wm_set(crtc_state, level,
  4940. plane_id, USHRT_MAX);
  4941. vlv_invalidate_wms(crtc, active, level);
  4942. crtc_state->wm.vlv.optimal = *active;
  4943. crtc_state->wm.vlv.intermediate = *active;
  4944. DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
  4945. pipe_name(pipe),
  4946. wm->pipe[pipe].plane[PLANE_PRIMARY],
  4947. wm->pipe[pipe].plane[PLANE_CURSOR],
  4948. wm->pipe[pipe].plane[PLANE_SPRITE0],
  4949. wm->pipe[pipe].plane[PLANE_SPRITE1]);
  4950. }
  4951. DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
  4952. wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
  4953. }
  4954. void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
  4955. {
  4956. struct intel_plane *plane;
  4957. struct intel_crtc *crtc;
  4958. mutex_lock(&dev_priv->wm.wm_mutex);
  4959. for_each_intel_plane(&dev_priv->drm, plane) {
  4960. struct intel_crtc *crtc =
  4961. intel_get_crtc_for_pipe(dev_priv, plane->pipe);
  4962. struct intel_crtc_state *crtc_state =
  4963. to_intel_crtc_state(crtc->base.state);
  4964. struct intel_plane_state *plane_state =
  4965. to_intel_plane_state(plane->base.state);
  4966. struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
  4967. const struct vlv_fifo_state *fifo_state =
  4968. &crtc_state->wm.vlv.fifo_state;
  4969. enum plane_id plane_id = plane->id;
  4970. int level;
  4971. if (plane_state->base.visible)
  4972. continue;
  4973. for (level = 0; level < wm_state->num_levels; level++) {
  4974. struct g4x_pipe_wm *raw =
  4975. &crtc_state->wm.vlv.raw[level];
  4976. raw->plane[plane_id] = 0;
  4977. wm_state->wm[level].plane[plane_id] =
  4978. vlv_invert_wm_value(raw->plane[plane_id],
  4979. fifo_state->plane[plane_id]);
  4980. }
  4981. }
  4982. for_each_intel_crtc(&dev_priv->drm, crtc) {
  4983. struct intel_crtc_state *crtc_state =
  4984. to_intel_crtc_state(crtc->base.state);
  4985. crtc_state->wm.vlv.intermediate =
  4986. crtc_state->wm.vlv.optimal;
  4987. crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
  4988. }
  4989. vlv_program_watermarks(dev_priv);
  4990. mutex_unlock(&dev_priv->wm.wm_mutex);
  4991. }
  4992. /*
  4993. * FIXME should probably kill this and improve
  4994. * the real watermark readout/sanitation instead
  4995. */
  4996. static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
  4997. {
  4998. I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
  4999. I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
  5000. I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
  5001. /*
  5002. * Don't touch WM1S_LP_EN here.
  5003. * Doing so could cause underruns.
  5004. */
  5005. }
  5006. void ilk_wm_get_hw_state(struct drm_device *dev)
  5007. {
  5008. struct drm_i915_private *dev_priv = to_i915(dev);
  5009. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  5010. struct drm_crtc *crtc;
  5011. ilk_init_lp_watermarks(dev_priv);
  5012. for_each_crtc(dev, crtc)
  5013. ilk_pipe_wm_get_hw_state(crtc);
  5014. hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
  5015. hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
  5016. hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
  5017. hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
  5018. if (INTEL_GEN(dev_priv) >= 7) {
  5019. hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
  5020. hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
  5021. }
  5022. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  5023. hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
  5024. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  5025. else if (IS_IVYBRIDGE(dev_priv))
  5026. hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
  5027. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  5028. hw->enable_fbc_wm =
  5029. !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
  5030. }
  5031. /**
  5032. * intel_update_watermarks - update FIFO watermark values based on current modes
  5033. * @crtc: the #intel_crtc on which to compute the WM
  5034. *
  5035. * Calculate watermark values for the various WM regs based on current mode
  5036. * and plane configuration.
  5037. *
  5038. * There are several cases to deal with here:
  5039. * - normal (i.e. non-self-refresh)
  5040. * - self-refresh (SR) mode
  5041. * - lines are large relative to FIFO size (buffer can hold up to 2)
  5042. * - lines are small relative to FIFO size (buffer can hold more than 2
  5043. * lines), so need to account for TLB latency
  5044. *
  5045. * The normal calculation is:
  5046. * watermark = dotclock * bytes per pixel * latency
  5047. * where latency is platform & configuration dependent (we assume pessimal
  5048. * values here).
  5049. *
  5050. * The SR calculation is:
  5051. * watermark = (trunc(latency/line time)+1) * surface width *
  5052. * bytes per pixel
  5053. * where
  5054. * line time = htotal / dotclock
  5055. * surface width = hdisplay for normal plane and 64 for cursor
  5056. * and latency is assumed to be high, as above.
  5057. *
  5058. * The final value programmed to the register should always be rounded up,
  5059. * and include an extra 2 entries to account for clock crossings.
  5060. *
  5061. * We don't use the sprite, so we can ignore that. And on Crestline we have
  5062. * to set the non-SR watermarks to 8.
  5063. */
  5064. void intel_update_watermarks(struct intel_crtc *crtc)
  5065. {
  5066. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5067. if (dev_priv->display.update_wm)
  5068. dev_priv->display.update_wm(crtc);
  5069. }
  5070. void intel_enable_ipc(struct drm_i915_private *dev_priv)
  5071. {
  5072. u32 val;
  5073. /* Display WA #0477 WaDisableIPC: skl */
  5074. if (IS_SKYLAKE(dev_priv)) {
  5075. dev_priv->ipc_enabled = false;
  5076. return;
  5077. }
  5078. val = I915_READ(DISP_ARB_CTL2);
  5079. if (dev_priv->ipc_enabled)
  5080. val |= DISP_IPC_ENABLE;
  5081. else
  5082. val &= ~DISP_IPC_ENABLE;
  5083. I915_WRITE(DISP_ARB_CTL2, val);
  5084. }
  5085. void intel_init_ipc(struct drm_i915_private *dev_priv)
  5086. {
  5087. dev_priv->ipc_enabled = false;
  5088. if (!HAS_IPC(dev_priv))
  5089. return;
  5090. dev_priv->ipc_enabled = true;
  5091. intel_enable_ipc(dev_priv);
  5092. }
  5093. /*
  5094. * Lock protecting IPS related data structures
  5095. */
  5096. DEFINE_SPINLOCK(mchdev_lock);
  5097. /* Global for IPS driver to get at the current i915 device. Protected by
  5098. * mchdev_lock. */
  5099. static struct drm_i915_private *i915_mch_dev;
  5100. bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
  5101. {
  5102. u16 rgvswctl;
  5103. lockdep_assert_held(&mchdev_lock);
  5104. rgvswctl = I915_READ16(MEMSWCTL);
  5105. if (rgvswctl & MEMCTL_CMD_STS) {
  5106. DRM_DEBUG("gpu busy, RCS change rejected\n");
  5107. return false; /* still busy with another command */
  5108. }
  5109. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  5110. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  5111. I915_WRITE16(MEMSWCTL, rgvswctl);
  5112. POSTING_READ16(MEMSWCTL);
  5113. rgvswctl |= MEMCTL_CMD_STS;
  5114. I915_WRITE16(MEMSWCTL, rgvswctl);
  5115. return true;
  5116. }
  5117. static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
  5118. {
  5119. u32 rgvmodectl;
  5120. u8 fmax, fmin, fstart, vstart;
  5121. spin_lock_irq(&mchdev_lock);
  5122. rgvmodectl = I915_READ(MEMMODECTL);
  5123. /* Enable temp reporting */
  5124. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  5125. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  5126. /* 100ms RC evaluation intervals */
  5127. I915_WRITE(RCUPEI, 100000);
  5128. I915_WRITE(RCDNEI, 100000);
  5129. /* Set max/min thresholds to 90ms and 80ms respectively */
  5130. I915_WRITE(RCBMAXAVG, 90000);
  5131. I915_WRITE(RCBMINAVG, 80000);
  5132. I915_WRITE(MEMIHYST, 1);
  5133. /* Set up min, max, and cur for interrupt handling */
  5134. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  5135. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  5136. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  5137. MEMMODE_FSTART_SHIFT;
  5138. vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
  5139. PXVFREQ_PX_SHIFT;
  5140. dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
  5141. dev_priv->ips.fstart = fstart;
  5142. dev_priv->ips.max_delay = fstart;
  5143. dev_priv->ips.min_delay = fmin;
  5144. dev_priv->ips.cur_delay = fstart;
  5145. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  5146. fmax, fmin, fstart);
  5147. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  5148. /*
  5149. * Interrupts will be enabled in ironlake_irq_postinstall
  5150. */
  5151. I915_WRITE(VIDSTART, vstart);
  5152. POSTING_READ(VIDSTART);
  5153. rgvmodectl |= MEMMODE_SWMODE_EN;
  5154. I915_WRITE(MEMMODECTL, rgvmodectl);
  5155. if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  5156. DRM_ERROR("stuck trying to change perf mode\n");
  5157. mdelay(1);
  5158. ironlake_set_drps(dev_priv, fstart);
  5159. dev_priv->ips.last_count1 = I915_READ(DMIEC) +
  5160. I915_READ(DDREC) + I915_READ(CSIEC);
  5161. dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
  5162. dev_priv->ips.last_count2 = I915_READ(GFXEC);
  5163. dev_priv->ips.last_time2 = ktime_get_raw_ns();
  5164. spin_unlock_irq(&mchdev_lock);
  5165. }
  5166. static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
  5167. {
  5168. u16 rgvswctl;
  5169. spin_lock_irq(&mchdev_lock);
  5170. rgvswctl = I915_READ16(MEMSWCTL);
  5171. /* Ack interrupts, disable EFC interrupt */
  5172. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  5173. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  5174. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  5175. I915_WRITE(DEIIR, DE_PCU_EVENT);
  5176. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  5177. /* Go back to the starting frequency */
  5178. ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
  5179. mdelay(1);
  5180. rgvswctl |= MEMCTL_CMD_STS;
  5181. I915_WRITE(MEMSWCTL, rgvswctl);
  5182. mdelay(1);
  5183. spin_unlock_irq(&mchdev_lock);
  5184. }
  5185. /* There's a funny hw issue where the hw returns all 0 when reading from
  5186. * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
  5187. * ourselves, instead of doing a rmw cycle (which might result in us clearing
  5188. * all limits and the gpu stuck at whatever frequency it is at atm).
  5189. */
  5190. static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
  5191. {
  5192. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5193. u32 limits;
  5194. /* Only set the down limit when we've reached the lowest level to avoid
  5195. * getting more interrupts, otherwise leave this clear. This prevents a
  5196. * race in the hw when coming out of rc6: There's a tiny window where
  5197. * the hw runs at the minimal clock before selecting the desired
  5198. * frequency, if the down threshold expires in that window we will not
  5199. * receive a down interrupt. */
  5200. if (INTEL_GEN(dev_priv) >= 9) {
  5201. limits = (rps->max_freq_softlimit) << 23;
  5202. if (val <= rps->min_freq_softlimit)
  5203. limits |= (rps->min_freq_softlimit) << 14;
  5204. } else {
  5205. limits = rps->max_freq_softlimit << 24;
  5206. if (val <= rps->min_freq_softlimit)
  5207. limits |= rps->min_freq_softlimit << 16;
  5208. }
  5209. return limits;
  5210. }
  5211. static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
  5212. {
  5213. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5214. int new_power;
  5215. u32 threshold_up = 0, threshold_down = 0; /* in % */
  5216. u32 ei_up = 0, ei_down = 0;
  5217. new_power = rps->power;
  5218. switch (rps->power) {
  5219. case LOW_POWER:
  5220. if (val > rps->efficient_freq + 1 &&
  5221. val > rps->cur_freq)
  5222. new_power = BETWEEN;
  5223. break;
  5224. case BETWEEN:
  5225. if (val <= rps->efficient_freq &&
  5226. val < rps->cur_freq)
  5227. new_power = LOW_POWER;
  5228. else if (val >= rps->rp0_freq &&
  5229. val > rps->cur_freq)
  5230. new_power = HIGH_POWER;
  5231. break;
  5232. case HIGH_POWER:
  5233. if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
  5234. val < rps->cur_freq)
  5235. new_power = BETWEEN;
  5236. break;
  5237. }
  5238. /* Max/min bins are special */
  5239. if (val <= rps->min_freq_softlimit)
  5240. new_power = LOW_POWER;
  5241. if (val >= rps->max_freq_softlimit)
  5242. new_power = HIGH_POWER;
  5243. if (new_power == rps->power)
  5244. return;
  5245. /* Note the units here are not exactly 1us, but 1280ns. */
  5246. switch (new_power) {
  5247. case LOW_POWER:
  5248. /* Upclock if more than 95% busy over 16ms */
  5249. ei_up = 16000;
  5250. threshold_up = 95;
  5251. /* Downclock if less than 85% busy over 32ms */
  5252. ei_down = 32000;
  5253. threshold_down = 85;
  5254. break;
  5255. case BETWEEN:
  5256. /* Upclock if more than 90% busy over 13ms */
  5257. ei_up = 13000;
  5258. threshold_up = 90;
  5259. /* Downclock if less than 75% busy over 32ms */
  5260. ei_down = 32000;
  5261. threshold_down = 75;
  5262. break;
  5263. case HIGH_POWER:
  5264. /* Upclock if more than 85% busy over 10ms */
  5265. ei_up = 10000;
  5266. threshold_up = 85;
  5267. /* Downclock if less than 60% busy over 32ms */
  5268. ei_down = 32000;
  5269. threshold_down = 60;
  5270. break;
  5271. }
  5272. /* When byt can survive without system hang with dynamic
  5273. * sw freq adjustments, this restriction can be lifted.
  5274. */
  5275. if (IS_VALLEYVIEW(dev_priv))
  5276. goto skip_hw_write;
  5277. I915_WRITE(GEN6_RP_UP_EI,
  5278. GT_INTERVAL_FROM_US(dev_priv, ei_up));
  5279. I915_WRITE(GEN6_RP_UP_THRESHOLD,
  5280. GT_INTERVAL_FROM_US(dev_priv,
  5281. ei_up * threshold_up / 100));
  5282. I915_WRITE(GEN6_RP_DOWN_EI,
  5283. GT_INTERVAL_FROM_US(dev_priv, ei_down));
  5284. I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
  5285. GT_INTERVAL_FROM_US(dev_priv,
  5286. ei_down * threshold_down / 100));
  5287. I915_WRITE(GEN6_RP_CONTROL,
  5288. GEN6_RP_MEDIA_TURBO |
  5289. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  5290. GEN6_RP_MEDIA_IS_GFX |
  5291. GEN6_RP_ENABLE |
  5292. GEN6_RP_UP_BUSY_AVG |
  5293. GEN6_RP_DOWN_IDLE_AVG);
  5294. skip_hw_write:
  5295. rps->power = new_power;
  5296. rps->up_threshold = threshold_up;
  5297. rps->down_threshold = threshold_down;
  5298. rps->last_adj = 0;
  5299. }
  5300. static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
  5301. {
  5302. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5303. u32 mask = 0;
  5304. /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
  5305. if (val > rps->min_freq_softlimit)
  5306. mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
  5307. if (val < rps->max_freq_softlimit)
  5308. mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
  5309. mask &= dev_priv->pm_rps_events;
  5310. return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
  5311. }
  5312. /* gen6_set_rps is called to update the frequency request, but should also be
  5313. * called when the range (min_delay and max_delay) is modified so that we can
  5314. * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
  5315. static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
  5316. {
  5317. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5318. /* min/max delay may still have been modified so be sure to
  5319. * write the limits value.
  5320. */
  5321. if (val != rps->cur_freq) {
  5322. gen6_set_rps_thresholds(dev_priv, val);
  5323. if (INTEL_GEN(dev_priv) >= 9)
  5324. I915_WRITE(GEN6_RPNSWREQ,
  5325. GEN9_FREQUENCY(val));
  5326. else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  5327. I915_WRITE(GEN6_RPNSWREQ,
  5328. HSW_FREQUENCY(val));
  5329. else
  5330. I915_WRITE(GEN6_RPNSWREQ,
  5331. GEN6_FREQUENCY(val) |
  5332. GEN6_OFFSET(0) |
  5333. GEN6_AGGRESSIVE_TURBO);
  5334. }
  5335. /* Make sure we continue to get interrupts
  5336. * until we hit the minimum or maximum frequencies.
  5337. */
  5338. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
  5339. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  5340. rps->cur_freq = val;
  5341. trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
  5342. return 0;
  5343. }
  5344. static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
  5345. {
  5346. int err;
  5347. if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
  5348. "Odd GPU freq value\n"))
  5349. val &= ~1;
  5350. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  5351. if (val != dev_priv->gt_pm.rps.cur_freq) {
  5352. err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
  5353. if (err)
  5354. return err;
  5355. gen6_set_rps_thresholds(dev_priv, val);
  5356. }
  5357. dev_priv->gt_pm.rps.cur_freq = val;
  5358. trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
  5359. return 0;
  5360. }
  5361. /* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
  5362. *
  5363. * * If Gfx is Idle, then
  5364. * 1. Forcewake Media well.
  5365. * 2. Request idle freq.
  5366. * 3. Release Forcewake of Media well.
  5367. */
  5368. static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
  5369. {
  5370. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5371. u32 val = rps->idle_freq;
  5372. int err;
  5373. if (rps->cur_freq <= val)
  5374. return;
  5375. /* The punit delays the write of the frequency and voltage until it
  5376. * determines the GPU is awake. During normal usage we don't want to
  5377. * waste power changing the frequency if the GPU is sleeping (rc6).
  5378. * However, the GPU and driver is now idle and we do not want to delay
  5379. * switching to minimum voltage (reducing power whilst idle) as we do
  5380. * not expect to be woken in the near future and so must flush the
  5381. * change by waking the device.
  5382. *
  5383. * We choose to take the media powerwell (either would do to trick the
  5384. * punit into committing the voltage change) as that takes a lot less
  5385. * power than the render powerwell.
  5386. */
  5387. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
  5388. err = valleyview_set_rps(dev_priv, val);
  5389. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
  5390. if (err)
  5391. DRM_ERROR("Failed to set RPS for idle\n");
  5392. }
  5393. void gen6_rps_busy(struct drm_i915_private *dev_priv)
  5394. {
  5395. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5396. mutex_lock(&dev_priv->pcu_lock);
  5397. if (rps->enabled) {
  5398. u8 freq;
  5399. if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
  5400. gen6_rps_reset_ei(dev_priv);
  5401. I915_WRITE(GEN6_PMINTRMSK,
  5402. gen6_rps_pm_mask(dev_priv, rps->cur_freq));
  5403. gen6_enable_rps_interrupts(dev_priv);
  5404. /* Use the user's desired frequency as a guide, but for better
  5405. * performance, jump directly to RPe as our starting frequency.
  5406. */
  5407. freq = max(rps->cur_freq,
  5408. rps->efficient_freq);
  5409. if (intel_set_rps(dev_priv,
  5410. clamp(freq,
  5411. rps->min_freq_softlimit,
  5412. rps->max_freq_softlimit)))
  5413. DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
  5414. }
  5415. mutex_unlock(&dev_priv->pcu_lock);
  5416. }
  5417. void gen6_rps_idle(struct drm_i915_private *dev_priv)
  5418. {
  5419. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5420. /* Flush our bottom-half so that it does not race with us
  5421. * setting the idle frequency and so that it is bounded by
  5422. * our rpm wakeref. And then disable the interrupts to stop any
  5423. * futher RPS reclocking whilst we are asleep.
  5424. */
  5425. gen6_disable_rps_interrupts(dev_priv);
  5426. mutex_lock(&dev_priv->pcu_lock);
  5427. if (rps->enabled) {
  5428. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  5429. vlv_set_rps_idle(dev_priv);
  5430. else
  5431. gen6_set_rps(dev_priv, rps->idle_freq);
  5432. rps->last_adj = 0;
  5433. I915_WRITE(GEN6_PMINTRMSK,
  5434. gen6_sanitize_rps_pm_mask(dev_priv, ~0));
  5435. }
  5436. mutex_unlock(&dev_priv->pcu_lock);
  5437. }
  5438. void gen6_rps_boost(struct i915_request *rq,
  5439. struct intel_rps_client *rps_client)
  5440. {
  5441. struct intel_rps *rps = &rq->i915->gt_pm.rps;
  5442. unsigned long flags;
  5443. bool boost;
  5444. /* This is intentionally racy! We peek at the state here, then
  5445. * validate inside the RPS worker.
  5446. */
  5447. if (!rps->enabled)
  5448. return;
  5449. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
  5450. return;
  5451. /* Serializes with i915_request_retire() */
  5452. boost = false;
  5453. spin_lock_irqsave(&rq->lock, flags);
  5454. if (!rq->waitboost && !dma_fence_is_signaled_locked(&rq->fence)) {
  5455. boost = !atomic_fetch_inc(&rps->num_waiters);
  5456. rq->waitboost = true;
  5457. }
  5458. spin_unlock_irqrestore(&rq->lock, flags);
  5459. if (!boost)
  5460. return;
  5461. if (READ_ONCE(rps->cur_freq) < rps->boost_freq)
  5462. schedule_work(&rps->work);
  5463. atomic_inc(rps_client ? &rps_client->boosts : &rps->boosts);
  5464. }
  5465. int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
  5466. {
  5467. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5468. int err;
  5469. lockdep_assert_held(&dev_priv->pcu_lock);
  5470. GEM_BUG_ON(val > rps->max_freq);
  5471. GEM_BUG_ON(val < rps->min_freq);
  5472. if (!rps->enabled) {
  5473. rps->cur_freq = val;
  5474. return 0;
  5475. }
  5476. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  5477. err = valleyview_set_rps(dev_priv, val);
  5478. else
  5479. err = gen6_set_rps(dev_priv, val);
  5480. return err;
  5481. }
  5482. static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
  5483. {
  5484. I915_WRITE(GEN6_RC_CONTROL, 0);
  5485. I915_WRITE(GEN9_PG_ENABLE, 0);
  5486. }
  5487. static void gen9_disable_rps(struct drm_i915_private *dev_priv)
  5488. {
  5489. I915_WRITE(GEN6_RP_CONTROL, 0);
  5490. }
  5491. static void gen6_disable_rc6(struct drm_i915_private *dev_priv)
  5492. {
  5493. I915_WRITE(GEN6_RC_CONTROL, 0);
  5494. }
  5495. static void gen6_disable_rps(struct drm_i915_private *dev_priv)
  5496. {
  5497. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  5498. I915_WRITE(GEN6_RP_CONTROL, 0);
  5499. }
  5500. static void cherryview_disable_rc6(struct drm_i915_private *dev_priv)
  5501. {
  5502. I915_WRITE(GEN6_RC_CONTROL, 0);
  5503. }
  5504. static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
  5505. {
  5506. I915_WRITE(GEN6_RP_CONTROL, 0);
  5507. }
  5508. static void valleyview_disable_rc6(struct drm_i915_private *dev_priv)
  5509. {
  5510. /* We're doing forcewake before Disabling RC6,
  5511. * This what the BIOS expects when going into suspend */
  5512. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5513. I915_WRITE(GEN6_RC_CONTROL, 0);
  5514. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5515. }
  5516. static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
  5517. {
  5518. I915_WRITE(GEN6_RP_CONTROL, 0);
  5519. }
  5520. static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
  5521. {
  5522. bool enable_rc6 = true;
  5523. unsigned long rc6_ctx_base;
  5524. u32 rc_ctl;
  5525. int rc_sw_target;
  5526. rc_ctl = I915_READ(GEN6_RC_CONTROL);
  5527. rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
  5528. RC_SW_TARGET_STATE_SHIFT;
  5529. DRM_DEBUG_DRIVER("BIOS enabled RC states: "
  5530. "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
  5531. onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
  5532. onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
  5533. rc_sw_target);
  5534. if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
  5535. DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
  5536. enable_rc6 = false;
  5537. }
  5538. /*
  5539. * The exact context size is not known for BXT, so assume a page size
  5540. * for this check.
  5541. */
  5542. rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
  5543. if (!((rc6_ctx_base >= dev_priv->dsm_reserved.start) &&
  5544. (rc6_ctx_base + PAGE_SIZE < dev_priv->dsm_reserved.end))) {
  5545. DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
  5546. enable_rc6 = false;
  5547. }
  5548. if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
  5549. ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
  5550. ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
  5551. ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
  5552. DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
  5553. enable_rc6 = false;
  5554. }
  5555. if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
  5556. !I915_READ(GEN8_PUSHBUS_ENABLE) ||
  5557. !I915_READ(GEN8_PUSHBUS_SHIFT)) {
  5558. DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
  5559. enable_rc6 = false;
  5560. }
  5561. if (!I915_READ(GEN6_GFXPAUSE)) {
  5562. DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
  5563. enable_rc6 = false;
  5564. }
  5565. if (!I915_READ(GEN8_MISC_CTRL0)) {
  5566. DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
  5567. enable_rc6 = false;
  5568. }
  5569. return enable_rc6;
  5570. }
  5571. static bool sanitize_rc6(struct drm_i915_private *i915)
  5572. {
  5573. struct intel_device_info *info = mkwrite_device_info(i915);
  5574. /* Powersaving is controlled by the host when inside a VM */
  5575. if (intel_vgpu_active(i915))
  5576. info->has_rc6 = 0;
  5577. if (info->has_rc6 &&
  5578. IS_GEN9_LP(i915) && !bxt_check_bios_rc6_setup(i915)) {
  5579. DRM_INFO("RC6 disabled by BIOS\n");
  5580. info->has_rc6 = 0;
  5581. }
  5582. /*
  5583. * We assume that we do not have any deep rc6 levels if we don't have
  5584. * have the previous rc6 level supported, i.e. we use HAS_RC6()
  5585. * as the initial coarse check for rc6 in general, moving on to
  5586. * progressively finer/deeper levels.
  5587. */
  5588. if (!info->has_rc6 && info->has_rc6p)
  5589. info->has_rc6p = 0;
  5590. return info->has_rc6;
  5591. }
  5592. static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
  5593. {
  5594. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5595. /* All of these values are in units of 50MHz */
  5596. /* static values from HW: RP0 > RP1 > RPn (min_freq) */
  5597. if (IS_GEN9_LP(dev_priv)) {
  5598. u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
  5599. rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
  5600. rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
  5601. rps->min_freq = (rp_state_cap >> 0) & 0xff;
  5602. } else {
  5603. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  5604. rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
  5605. rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
  5606. rps->min_freq = (rp_state_cap >> 16) & 0xff;
  5607. }
  5608. /* hw_max = RP0 until we check for overclocking */
  5609. rps->max_freq = rps->rp0_freq;
  5610. rps->efficient_freq = rps->rp1_freq;
  5611. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
  5612. IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  5613. u32 ddcc_status = 0;
  5614. if (sandybridge_pcode_read(dev_priv,
  5615. HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
  5616. &ddcc_status) == 0)
  5617. rps->efficient_freq =
  5618. clamp_t(u8,
  5619. ((ddcc_status >> 8) & 0xff),
  5620. rps->min_freq,
  5621. rps->max_freq);
  5622. }
  5623. if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  5624. /* Store the frequency values in 16.66 MHZ units, which is
  5625. * the natural hardware unit for SKL
  5626. */
  5627. rps->rp0_freq *= GEN9_FREQ_SCALER;
  5628. rps->rp1_freq *= GEN9_FREQ_SCALER;
  5629. rps->min_freq *= GEN9_FREQ_SCALER;
  5630. rps->max_freq *= GEN9_FREQ_SCALER;
  5631. rps->efficient_freq *= GEN9_FREQ_SCALER;
  5632. }
  5633. }
  5634. static void reset_rps(struct drm_i915_private *dev_priv,
  5635. int (*set)(struct drm_i915_private *, u8))
  5636. {
  5637. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5638. u8 freq = rps->cur_freq;
  5639. /* force a reset */
  5640. rps->power = -1;
  5641. rps->cur_freq = -1;
  5642. if (set(dev_priv, freq))
  5643. DRM_ERROR("Failed to reset RPS to initial values\n");
  5644. }
  5645. /* See the Gen9_GT_PM_Programming_Guide doc for the below */
  5646. static void gen9_enable_rps(struct drm_i915_private *dev_priv)
  5647. {
  5648. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5649. /* Program defaults and thresholds for RPS */
  5650. if (IS_GEN9(dev_priv))
  5651. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  5652. GEN9_FREQUENCY(dev_priv->gt_pm.rps.rp1_freq));
  5653. /* 1 second timeout*/
  5654. I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
  5655. GT_INTERVAL_FROM_US(dev_priv, 1000000));
  5656. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
  5657. /* Leaning on the below call to gen6_set_rps to program/setup the
  5658. * Up/Down EI & threshold registers, as well as the RP_CONTROL,
  5659. * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
  5660. reset_rps(dev_priv, gen6_set_rps);
  5661. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5662. }
  5663. static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
  5664. {
  5665. struct intel_engine_cs *engine;
  5666. enum intel_engine_id id;
  5667. u32 rc6_mode;
  5668. /* 1a: Software RC state - RC0 */
  5669. I915_WRITE(GEN6_RC_STATE, 0);
  5670. /* 1b: Get forcewake during program sequence. Although the driver
  5671. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  5672. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5673. /* 2a: Disable RC states. */
  5674. I915_WRITE(GEN6_RC_CONTROL, 0);
  5675. /* 2b: Program RC6 thresholds.*/
  5676. if (INTEL_GEN(dev_priv) >= 10) {
  5677. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16 | 85);
  5678. I915_WRITE(GEN10_MEDIA_WAKE_RATE_LIMIT, 150);
  5679. } else if (IS_SKYLAKE(dev_priv)) {
  5680. /*
  5681. * WaRsDoubleRc6WrlWithCoarsePowerGating:skl Doubling WRL only
  5682. * when CPG is enabled
  5683. */
  5684. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
  5685. } else {
  5686. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
  5687. }
  5688. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  5689. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  5690. for_each_engine(engine, dev_priv, id)
  5691. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  5692. if (HAS_GUC(dev_priv))
  5693. I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
  5694. I915_WRITE(GEN6_RC_SLEEP, 0);
  5695. /*
  5696. * 2c: Program Coarse Power Gating Policies.
  5697. *
  5698. * Bspec's guidance is to use 25us (really 25 * 1280ns) here. What we
  5699. * use instead is a more conservative estimate for the maximum time
  5700. * it takes us to service a CS interrupt and submit a new ELSP - that
  5701. * is the time which the GPU is idle waiting for the CPU to select the
  5702. * next request to execute. If the idle hysteresis is less than that
  5703. * interrupt service latency, the hardware will automatically gate
  5704. * the power well and we will then incur the wake up cost on top of
  5705. * the service latency. A similar guide from intel_pstate is that we
  5706. * do not want the enable hysteresis to less than the wakeup latency.
  5707. *
  5708. * igt/gem_exec_nop/sequential provides a rough estimate for the
  5709. * service latency, and puts it around 10us for Broadwell (and other
  5710. * big core) and around 40us for Broxton (and other low power cores).
  5711. * [Note that for legacy ringbuffer submission, this is less than 1us!]
  5712. * However, the wakeup latency on Broxton is closer to 100us. To be
  5713. * conservative, we have to factor in a context switch on top (due
  5714. * to ksoftirqd).
  5715. */
  5716. I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 250);
  5717. I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 250);
  5718. /* 3a: Enable RC6 */
  5719. I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
  5720. /* WaRsUseTimeoutMode:cnl (pre-prod) */
  5721. if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_C0))
  5722. rc6_mode = GEN7_RC_CTL_TO_MODE;
  5723. else
  5724. rc6_mode = GEN6_RC_CTL_EI_MODE(1);
  5725. I915_WRITE(GEN6_RC_CONTROL,
  5726. GEN6_RC_CTL_HW_ENABLE |
  5727. GEN6_RC_CTL_RC6_ENABLE |
  5728. rc6_mode);
  5729. /*
  5730. * 3b: Enable Coarse Power Gating only when RC6 is enabled.
  5731. * WaRsDisableCoarsePowerGating:skl,cnl - Render/Media PG need to be disabled with RC6.
  5732. */
  5733. if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
  5734. I915_WRITE(GEN9_PG_ENABLE, 0);
  5735. else
  5736. I915_WRITE(GEN9_PG_ENABLE,
  5737. GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE);
  5738. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5739. }
  5740. static void gen8_enable_rc6(struct drm_i915_private *dev_priv)
  5741. {
  5742. struct intel_engine_cs *engine;
  5743. enum intel_engine_id id;
  5744. /* 1a: Software RC state - RC0 */
  5745. I915_WRITE(GEN6_RC_STATE, 0);
  5746. /* 1b: Get forcewake during program sequence. Although the driver
  5747. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  5748. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5749. /* 2a: Disable RC states. */
  5750. I915_WRITE(GEN6_RC_CONTROL, 0);
  5751. /* 2b: Program RC6 thresholds.*/
  5752. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  5753. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  5754. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  5755. for_each_engine(engine, dev_priv, id)
  5756. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  5757. I915_WRITE(GEN6_RC_SLEEP, 0);
  5758. I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
  5759. /* 3: Enable RC6 */
  5760. I915_WRITE(GEN6_RC_CONTROL,
  5761. GEN6_RC_CTL_HW_ENABLE |
  5762. GEN7_RC_CTL_TO_MODE |
  5763. GEN6_RC_CTL_RC6_ENABLE);
  5764. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5765. }
  5766. static void gen8_enable_rps(struct drm_i915_private *dev_priv)
  5767. {
  5768. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5769. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5770. /* 1 Program defaults and thresholds for RPS*/
  5771. I915_WRITE(GEN6_RPNSWREQ,
  5772. HSW_FREQUENCY(rps->rp1_freq));
  5773. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  5774. HSW_FREQUENCY(rps->rp1_freq));
  5775. /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
  5776. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
  5777. /* Docs recommend 900MHz, and 300 MHz respectively */
  5778. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  5779. rps->max_freq_softlimit << 24 |
  5780. rps->min_freq_softlimit << 16);
  5781. I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
  5782. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
  5783. I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
  5784. I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
  5785. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  5786. /* 2: Enable RPS */
  5787. I915_WRITE(GEN6_RP_CONTROL,
  5788. GEN6_RP_MEDIA_TURBO |
  5789. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  5790. GEN6_RP_MEDIA_IS_GFX |
  5791. GEN6_RP_ENABLE |
  5792. GEN6_RP_UP_BUSY_AVG |
  5793. GEN6_RP_DOWN_IDLE_AVG);
  5794. reset_rps(dev_priv, gen6_set_rps);
  5795. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5796. }
  5797. static void gen6_enable_rc6(struct drm_i915_private *dev_priv)
  5798. {
  5799. struct intel_engine_cs *engine;
  5800. enum intel_engine_id id;
  5801. u32 rc6vids, rc6_mask;
  5802. u32 gtfifodbg;
  5803. int ret;
  5804. I915_WRITE(GEN6_RC_STATE, 0);
  5805. /* Clear the DBG now so we don't confuse earlier errors */
  5806. gtfifodbg = I915_READ(GTFIFODBG);
  5807. if (gtfifodbg) {
  5808. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  5809. I915_WRITE(GTFIFODBG, gtfifodbg);
  5810. }
  5811. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5812. /* disable the counters and set deterministic thresholds */
  5813. I915_WRITE(GEN6_RC_CONTROL, 0);
  5814. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  5815. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  5816. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  5817. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  5818. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  5819. for_each_engine(engine, dev_priv, id)
  5820. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  5821. I915_WRITE(GEN6_RC_SLEEP, 0);
  5822. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  5823. if (IS_IVYBRIDGE(dev_priv))
  5824. I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
  5825. else
  5826. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  5827. I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
  5828. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  5829. /* We don't use those on Haswell */
  5830. rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
  5831. if (HAS_RC6p(dev_priv))
  5832. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  5833. if (HAS_RC6pp(dev_priv))
  5834. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  5835. I915_WRITE(GEN6_RC_CONTROL,
  5836. rc6_mask |
  5837. GEN6_RC_CTL_EI_MODE(1) |
  5838. GEN6_RC_CTL_HW_ENABLE);
  5839. rc6vids = 0;
  5840. ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
  5841. if (IS_GEN6(dev_priv) && ret) {
  5842. DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
  5843. } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
  5844. DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
  5845. GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
  5846. rc6vids &= 0xffff00;
  5847. rc6vids |= GEN6_ENCODE_RC6_VID(450);
  5848. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
  5849. if (ret)
  5850. DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
  5851. }
  5852. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5853. }
  5854. static void gen6_enable_rps(struct drm_i915_private *dev_priv)
  5855. {
  5856. /* Here begins a magic sequence of register writes to enable
  5857. * auto-downclocking.
  5858. *
  5859. * Perhaps there might be some value in exposing these to
  5860. * userspace...
  5861. */
  5862. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5863. /* Power down if completely idle for over 50ms */
  5864. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
  5865. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  5866. reset_rps(dev_priv, gen6_set_rps);
  5867. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5868. }
  5869. static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
  5870. {
  5871. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5872. const int min_freq = 15;
  5873. const int scaling_factor = 180;
  5874. unsigned int gpu_freq;
  5875. unsigned int max_ia_freq, min_ring_freq;
  5876. unsigned int max_gpu_freq, min_gpu_freq;
  5877. struct cpufreq_policy *policy;
  5878. WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
  5879. if (rps->max_freq <= rps->min_freq)
  5880. return;
  5881. policy = cpufreq_cpu_get(0);
  5882. if (policy) {
  5883. max_ia_freq = policy->cpuinfo.max_freq;
  5884. cpufreq_cpu_put(policy);
  5885. } else {
  5886. /*
  5887. * Default to measured freq if none found, PCU will ensure we
  5888. * don't go over
  5889. */
  5890. max_ia_freq = tsc_khz;
  5891. }
  5892. /* Convert from kHz to MHz */
  5893. max_ia_freq /= 1000;
  5894. min_ring_freq = I915_READ(DCLK) & 0xf;
  5895. /* convert DDR frequency from units of 266.6MHz to bandwidth */
  5896. min_ring_freq = mult_frac(min_ring_freq, 8, 3);
  5897. min_gpu_freq = rps->min_freq;
  5898. max_gpu_freq = rps->max_freq;
  5899. if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  5900. /* Convert GT frequency to 50 HZ units */
  5901. min_gpu_freq /= GEN9_FREQ_SCALER;
  5902. max_gpu_freq /= GEN9_FREQ_SCALER;
  5903. }
  5904. /*
  5905. * For each potential GPU frequency, load a ring frequency we'd like
  5906. * to use for memory access. We do this by specifying the IA frequency
  5907. * the PCU should use as a reference to determine the ring frequency.
  5908. */
  5909. for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
  5910. const int diff = max_gpu_freq - gpu_freq;
  5911. unsigned int ia_freq = 0, ring_freq = 0;
  5912. if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  5913. /*
  5914. * ring_freq = 2 * GT. ring_freq is in 100MHz units
  5915. * No floor required for ring frequency on SKL.
  5916. */
  5917. ring_freq = gpu_freq;
  5918. } else if (INTEL_GEN(dev_priv) >= 8) {
  5919. /* max(2 * GT, DDR). NB: GT is 50MHz units */
  5920. ring_freq = max(min_ring_freq, gpu_freq);
  5921. } else if (IS_HASWELL(dev_priv)) {
  5922. ring_freq = mult_frac(gpu_freq, 5, 4);
  5923. ring_freq = max(min_ring_freq, ring_freq);
  5924. /* leave ia_freq as the default, chosen by cpufreq */
  5925. } else {
  5926. /* On older processors, there is no separate ring
  5927. * clock domain, so in order to boost the bandwidth
  5928. * of the ring, we need to upclock the CPU (ia_freq).
  5929. *
  5930. * For GPU frequencies less than 750MHz,
  5931. * just use the lowest ring freq.
  5932. */
  5933. if (gpu_freq < min_freq)
  5934. ia_freq = 800;
  5935. else
  5936. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  5937. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  5938. }
  5939. sandybridge_pcode_write(dev_priv,
  5940. GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
  5941. ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
  5942. ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
  5943. gpu_freq);
  5944. }
  5945. }
  5946. static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
  5947. {
  5948. u32 val, rp0;
  5949. val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
  5950. switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
  5951. case 8:
  5952. /* (2 * 4) config */
  5953. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
  5954. break;
  5955. case 12:
  5956. /* (2 * 6) config */
  5957. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
  5958. break;
  5959. case 16:
  5960. /* (2 * 8) config */
  5961. default:
  5962. /* Setting (2 * 8) Min RP0 for any other combination */
  5963. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
  5964. break;
  5965. }
  5966. rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
  5967. return rp0;
  5968. }
  5969. static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  5970. {
  5971. u32 val, rpe;
  5972. val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
  5973. rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
  5974. return rpe;
  5975. }
  5976. static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
  5977. {
  5978. u32 val, rp1;
  5979. val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
  5980. rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
  5981. return rp1;
  5982. }
  5983. static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
  5984. {
  5985. u32 val, rpn;
  5986. val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
  5987. rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
  5988. FB_GFX_FREQ_FUSE_MASK);
  5989. return rpn;
  5990. }
  5991. static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
  5992. {
  5993. u32 val, rp1;
  5994. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  5995. rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
  5996. return rp1;
  5997. }
  5998. static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
  5999. {
  6000. u32 val, rp0;
  6001. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  6002. rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
  6003. /* Clamp to max */
  6004. rp0 = min_t(u32, rp0, 0xea);
  6005. return rp0;
  6006. }
  6007. static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  6008. {
  6009. u32 val, rpe;
  6010. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
  6011. rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
  6012. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
  6013. rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
  6014. return rpe;
  6015. }
  6016. static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
  6017. {
  6018. u32 val;
  6019. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
  6020. /*
  6021. * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
  6022. * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
  6023. * a BYT-M B0 the above register contains 0xbf. Moreover when setting
  6024. * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
  6025. * to make sure it matches what Punit accepts.
  6026. */
  6027. return max_t(u32, val, 0xc0);
  6028. }
  6029. /* Check that the pctx buffer wasn't move under us. */
  6030. static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
  6031. {
  6032. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  6033. WARN_ON(pctx_addr != dev_priv->dsm.start +
  6034. dev_priv->vlv_pctx->stolen->start);
  6035. }
  6036. /* Check that the pcbr address is not empty. */
  6037. static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
  6038. {
  6039. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  6040. WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
  6041. }
  6042. static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
  6043. {
  6044. resource_size_t pctx_paddr, paddr;
  6045. resource_size_t pctx_size = 32*1024;
  6046. u32 pcbr;
  6047. pcbr = I915_READ(VLV_PCBR);
  6048. if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
  6049. DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
  6050. paddr = dev_priv->dsm.end + 1 - pctx_size;
  6051. GEM_BUG_ON(paddr > U32_MAX);
  6052. pctx_paddr = (paddr & (~4095));
  6053. I915_WRITE(VLV_PCBR, pctx_paddr);
  6054. }
  6055. DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
  6056. }
  6057. static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
  6058. {
  6059. struct drm_i915_gem_object *pctx;
  6060. resource_size_t pctx_paddr;
  6061. resource_size_t pctx_size = 24*1024;
  6062. u32 pcbr;
  6063. pcbr = I915_READ(VLV_PCBR);
  6064. if (pcbr) {
  6065. /* BIOS set it up already, grab the pre-alloc'd space */
  6066. resource_size_t pcbr_offset;
  6067. pcbr_offset = (pcbr & (~4095)) - dev_priv->dsm.start;
  6068. pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  6069. pcbr_offset,
  6070. I915_GTT_OFFSET_NONE,
  6071. pctx_size);
  6072. goto out;
  6073. }
  6074. DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
  6075. /*
  6076. * From the Gunit register HAS:
  6077. * The Gfx driver is expected to program this register and ensure
  6078. * proper allocation within Gfx stolen memory. For example, this
  6079. * register should be programmed such than the PCBR range does not
  6080. * overlap with other ranges, such as the frame buffer, protected
  6081. * memory, or any other relevant ranges.
  6082. */
  6083. pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
  6084. if (!pctx) {
  6085. DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
  6086. goto out;
  6087. }
  6088. GEM_BUG_ON(range_overflows_t(u64,
  6089. dev_priv->dsm.start,
  6090. pctx->stolen->start,
  6091. U32_MAX));
  6092. pctx_paddr = dev_priv->dsm.start + pctx->stolen->start;
  6093. I915_WRITE(VLV_PCBR, pctx_paddr);
  6094. out:
  6095. DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
  6096. dev_priv->vlv_pctx = pctx;
  6097. }
  6098. static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
  6099. {
  6100. if (WARN_ON(!dev_priv->vlv_pctx))
  6101. return;
  6102. i915_gem_object_put(dev_priv->vlv_pctx);
  6103. dev_priv->vlv_pctx = NULL;
  6104. }
  6105. static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
  6106. {
  6107. dev_priv->gt_pm.rps.gpll_ref_freq =
  6108. vlv_get_cck_clock(dev_priv, "GPLL ref",
  6109. CCK_GPLL_CLOCK_CONTROL,
  6110. dev_priv->czclk_freq);
  6111. DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
  6112. dev_priv->gt_pm.rps.gpll_ref_freq);
  6113. }
  6114. static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
  6115. {
  6116. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  6117. u32 val;
  6118. valleyview_setup_pctx(dev_priv);
  6119. vlv_init_gpll_ref_freq(dev_priv);
  6120. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  6121. switch ((val >> 6) & 3) {
  6122. case 0:
  6123. case 1:
  6124. dev_priv->mem_freq = 800;
  6125. break;
  6126. case 2:
  6127. dev_priv->mem_freq = 1066;
  6128. break;
  6129. case 3:
  6130. dev_priv->mem_freq = 1333;
  6131. break;
  6132. }
  6133. DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
  6134. rps->max_freq = valleyview_rps_max_freq(dev_priv);
  6135. rps->rp0_freq = rps->max_freq;
  6136. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  6137. intel_gpu_freq(dev_priv, rps->max_freq),
  6138. rps->max_freq);
  6139. rps->efficient_freq = valleyview_rps_rpe_freq(dev_priv);
  6140. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  6141. intel_gpu_freq(dev_priv, rps->efficient_freq),
  6142. rps->efficient_freq);
  6143. rps->rp1_freq = valleyview_rps_guar_freq(dev_priv);
  6144. DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
  6145. intel_gpu_freq(dev_priv, rps->rp1_freq),
  6146. rps->rp1_freq);
  6147. rps->min_freq = valleyview_rps_min_freq(dev_priv);
  6148. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  6149. intel_gpu_freq(dev_priv, rps->min_freq),
  6150. rps->min_freq);
  6151. }
  6152. static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
  6153. {
  6154. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  6155. u32 val;
  6156. cherryview_setup_pctx(dev_priv);
  6157. vlv_init_gpll_ref_freq(dev_priv);
  6158. mutex_lock(&dev_priv->sb_lock);
  6159. val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
  6160. mutex_unlock(&dev_priv->sb_lock);
  6161. switch ((val >> 2) & 0x7) {
  6162. case 3:
  6163. dev_priv->mem_freq = 2000;
  6164. break;
  6165. default:
  6166. dev_priv->mem_freq = 1600;
  6167. break;
  6168. }
  6169. DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
  6170. rps->max_freq = cherryview_rps_max_freq(dev_priv);
  6171. rps->rp0_freq = rps->max_freq;
  6172. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  6173. intel_gpu_freq(dev_priv, rps->max_freq),
  6174. rps->max_freq);
  6175. rps->efficient_freq = cherryview_rps_rpe_freq(dev_priv);
  6176. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  6177. intel_gpu_freq(dev_priv, rps->efficient_freq),
  6178. rps->efficient_freq);
  6179. rps->rp1_freq = cherryview_rps_guar_freq(dev_priv);
  6180. DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
  6181. intel_gpu_freq(dev_priv, rps->rp1_freq),
  6182. rps->rp1_freq);
  6183. rps->min_freq = cherryview_rps_min_freq(dev_priv);
  6184. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  6185. intel_gpu_freq(dev_priv, rps->min_freq),
  6186. rps->min_freq);
  6187. WARN_ONCE((rps->max_freq | rps->efficient_freq | rps->rp1_freq |
  6188. rps->min_freq) & 1,
  6189. "Odd GPU freq values\n");
  6190. }
  6191. static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
  6192. {
  6193. valleyview_cleanup_pctx(dev_priv);
  6194. }
  6195. static void cherryview_enable_rc6(struct drm_i915_private *dev_priv)
  6196. {
  6197. struct intel_engine_cs *engine;
  6198. enum intel_engine_id id;
  6199. u32 gtfifodbg, rc6_mode, pcbr;
  6200. gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
  6201. GT_FIFO_FREE_ENTRIES_CHV);
  6202. if (gtfifodbg) {
  6203. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  6204. gtfifodbg);
  6205. I915_WRITE(GTFIFODBG, gtfifodbg);
  6206. }
  6207. cherryview_check_pctx(dev_priv);
  6208. /* 1a & 1b: Get forcewake during program sequence. Although the driver
  6209. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  6210. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6211. /* Disable RC states. */
  6212. I915_WRITE(GEN6_RC_CONTROL, 0);
  6213. /* 2a: Program RC6 thresholds.*/
  6214. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  6215. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  6216. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  6217. for_each_engine(engine, dev_priv, id)
  6218. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  6219. I915_WRITE(GEN6_RC_SLEEP, 0);
  6220. /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
  6221. I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
  6222. /* Allows RC6 residency counter to work */
  6223. I915_WRITE(VLV_COUNTER_CONTROL,
  6224. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
  6225. VLV_MEDIA_RC6_COUNT_EN |
  6226. VLV_RENDER_RC6_COUNT_EN));
  6227. /* For now we assume BIOS is allocating and populating the PCBR */
  6228. pcbr = I915_READ(VLV_PCBR);
  6229. /* 3: Enable RC6 */
  6230. rc6_mode = 0;
  6231. if (pcbr >> VLV_PCBR_ADDR_SHIFT)
  6232. rc6_mode = GEN7_RC_CTL_TO_MODE;
  6233. I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
  6234. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6235. }
  6236. static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
  6237. {
  6238. u32 val;
  6239. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6240. /* 1: Program defaults and thresholds for RPS*/
  6241. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  6242. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  6243. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  6244. I915_WRITE(GEN6_RP_UP_EI, 66000);
  6245. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  6246. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  6247. /* 2: Enable RPS */
  6248. I915_WRITE(GEN6_RP_CONTROL,
  6249. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  6250. GEN6_RP_MEDIA_IS_GFX |
  6251. GEN6_RP_ENABLE |
  6252. GEN6_RP_UP_BUSY_AVG |
  6253. GEN6_RP_DOWN_IDLE_AVG);
  6254. /* Setting Fixed Bias */
  6255. val = VLV_OVERRIDE_EN |
  6256. VLV_SOC_TDP_EN |
  6257. CHV_BIAS_CPU_50_SOC_50;
  6258. vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
  6259. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  6260. /* RPS code assumes GPLL is used */
  6261. WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
  6262. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
  6263. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  6264. reset_rps(dev_priv, valleyview_set_rps);
  6265. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6266. }
  6267. static void valleyview_enable_rc6(struct drm_i915_private *dev_priv)
  6268. {
  6269. struct intel_engine_cs *engine;
  6270. enum intel_engine_id id;
  6271. u32 gtfifodbg;
  6272. valleyview_check_pctx(dev_priv);
  6273. gtfifodbg = I915_READ(GTFIFODBG);
  6274. if (gtfifodbg) {
  6275. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  6276. gtfifodbg);
  6277. I915_WRITE(GTFIFODBG, gtfifodbg);
  6278. }
  6279. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6280. /* Disable RC states. */
  6281. I915_WRITE(GEN6_RC_CONTROL, 0);
  6282. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
  6283. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  6284. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  6285. for_each_engine(engine, dev_priv, id)
  6286. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  6287. I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
  6288. /* Allows RC6 residency counter to work */
  6289. I915_WRITE(VLV_COUNTER_CONTROL,
  6290. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
  6291. VLV_MEDIA_RC0_COUNT_EN |
  6292. VLV_RENDER_RC0_COUNT_EN |
  6293. VLV_MEDIA_RC6_COUNT_EN |
  6294. VLV_RENDER_RC6_COUNT_EN));
  6295. I915_WRITE(GEN6_RC_CONTROL,
  6296. GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL);
  6297. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6298. }
  6299. static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
  6300. {
  6301. u32 val;
  6302. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6303. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  6304. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  6305. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  6306. I915_WRITE(GEN6_RP_UP_EI, 66000);
  6307. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  6308. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  6309. I915_WRITE(GEN6_RP_CONTROL,
  6310. GEN6_RP_MEDIA_TURBO |
  6311. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  6312. GEN6_RP_MEDIA_IS_GFX |
  6313. GEN6_RP_ENABLE |
  6314. GEN6_RP_UP_BUSY_AVG |
  6315. GEN6_RP_DOWN_IDLE_CONT);
  6316. /* Setting Fixed Bias */
  6317. val = VLV_OVERRIDE_EN |
  6318. VLV_SOC_TDP_EN |
  6319. VLV_BIAS_CPU_125_SOC_875;
  6320. vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
  6321. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  6322. /* RPS code assumes GPLL is used */
  6323. WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
  6324. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
  6325. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  6326. reset_rps(dev_priv, valleyview_set_rps);
  6327. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6328. }
  6329. static unsigned long intel_pxfreq(u32 vidfreq)
  6330. {
  6331. unsigned long freq;
  6332. int div = (vidfreq & 0x3f0000) >> 16;
  6333. int post = (vidfreq & 0x3000) >> 12;
  6334. int pre = (vidfreq & 0x7);
  6335. if (!pre)
  6336. return 0;
  6337. freq = ((div * 133333) / ((1<<post) * pre));
  6338. return freq;
  6339. }
  6340. static const struct cparams {
  6341. u16 i;
  6342. u16 t;
  6343. u16 m;
  6344. u16 c;
  6345. } cparams[] = {
  6346. { 1, 1333, 301, 28664 },
  6347. { 1, 1066, 294, 24460 },
  6348. { 1, 800, 294, 25192 },
  6349. { 0, 1333, 276, 27605 },
  6350. { 0, 1066, 276, 27605 },
  6351. { 0, 800, 231, 23784 },
  6352. };
  6353. static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
  6354. {
  6355. u64 total_count, diff, ret;
  6356. u32 count1, count2, count3, m = 0, c = 0;
  6357. unsigned long now = jiffies_to_msecs(jiffies), diff1;
  6358. int i;
  6359. lockdep_assert_held(&mchdev_lock);
  6360. diff1 = now - dev_priv->ips.last_time1;
  6361. /* Prevent division-by-zero if we are asking too fast.
  6362. * Also, we don't get interesting results if we are polling
  6363. * faster than once in 10ms, so just return the saved value
  6364. * in such cases.
  6365. */
  6366. if (diff1 <= 10)
  6367. return dev_priv->ips.chipset_power;
  6368. count1 = I915_READ(DMIEC);
  6369. count2 = I915_READ(DDREC);
  6370. count3 = I915_READ(CSIEC);
  6371. total_count = count1 + count2 + count3;
  6372. /* FIXME: handle per-counter overflow */
  6373. if (total_count < dev_priv->ips.last_count1) {
  6374. diff = ~0UL - dev_priv->ips.last_count1;
  6375. diff += total_count;
  6376. } else {
  6377. diff = total_count - dev_priv->ips.last_count1;
  6378. }
  6379. for (i = 0; i < ARRAY_SIZE(cparams); i++) {
  6380. if (cparams[i].i == dev_priv->ips.c_m &&
  6381. cparams[i].t == dev_priv->ips.r_t) {
  6382. m = cparams[i].m;
  6383. c = cparams[i].c;
  6384. break;
  6385. }
  6386. }
  6387. diff = div_u64(diff, diff1);
  6388. ret = ((m * diff) + c);
  6389. ret = div_u64(ret, 10);
  6390. dev_priv->ips.last_count1 = total_count;
  6391. dev_priv->ips.last_time1 = now;
  6392. dev_priv->ips.chipset_power = ret;
  6393. return ret;
  6394. }
  6395. unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
  6396. {
  6397. unsigned long val;
  6398. if (!IS_GEN5(dev_priv))
  6399. return 0;
  6400. spin_lock_irq(&mchdev_lock);
  6401. val = __i915_chipset_val(dev_priv);
  6402. spin_unlock_irq(&mchdev_lock);
  6403. return val;
  6404. }
  6405. unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
  6406. {
  6407. unsigned long m, x, b;
  6408. u32 tsfs;
  6409. tsfs = I915_READ(TSFS);
  6410. m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
  6411. x = I915_READ8(TR1);
  6412. b = tsfs & TSFS_INTR_MASK;
  6413. return ((m * x) / 127) - b;
  6414. }
  6415. static int _pxvid_to_vd(u8 pxvid)
  6416. {
  6417. if (pxvid == 0)
  6418. return 0;
  6419. if (pxvid >= 8 && pxvid < 31)
  6420. pxvid = 31;
  6421. return (pxvid + 2) * 125;
  6422. }
  6423. static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
  6424. {
  6425. const int vd = _pxvid_to_vd(pxvid);
  6426. const int vm = vd - 1125;
  6427. if (INTEL_INFO(dev_priv)->is_mobile)
  6428. return vm > 0 ? vm : 0;
  6429. return vd;
  6430. }
  6431. static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
  6432. {
  6433. u64 now, diff, diffms;
  6434. u32 count;
  6435. lockdep_assert_held(&mchdev_lock);
  6436. now = ktime_get_raw_ns();
  6437. diffms = now - dev_priv->ips.last_time2;
  6438. do_div(diffms, NSEC_PER_MSEC);
  6439. /* Don't divide by 0 */
  6440. if (!diffms)
  6441. return;
  6442. count = I915_READ(GFXEC);
  6443. if (count < dev_priv->ips.last_count2) {
  6444. diff = ~0UL - dev_priv->ips.last_count2;
  6445. diff += count;
  6446. } else {
  6447. diff = count - dev_priv->ips.last_count2;
  6448. }
  6449. dev_priv->ips.last_count2 = count;
  6450. dev_priv->ips.last_time2 = now;
  6451. /* More magic constants... */
  6452. diff = diff * 1181;
  6453. diff = div_u64(diff, diffms * 10);
  6454. dev_priv->ips.gfx_power = diff;
  6455. }
  6456. void i915_update_gfx_val(struct drm_i915_private *dev_priv)
  6457. {
  6458. if (!IS_GEN5(dev_priv))
  6459. return;
  6460. spin_lock_irq(&mchdev_lock);
  6461. __i915_update_gfx_val(dev_priv);
  6462. spin_unlock_irq(&mchdev_lock);
  6463. }
  6464. static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
  6465. {
  6466. unsigned long t, corr, state1, corr2, state2;
  6467. u32 pxvid, ext_v;
  6468. lockdep_assert_held(&mchdev_lock);
  6469. pxvid = I915_READ(PXVFREQ(dev_priv->gt_pm.rps.cur_freq));
  6470. pxvid = (pxvid >> 24) & 0x7f;
  6471. ext_v = pvid_to_extvid(dev_priv, pxvid);
  6472. state1 = ext_v;
  6473. t = i915_mch_val(dev_priv);
  6474. /* Revel in the empirically derived constants */
  6475. /* Correction factor in 1/100000 units */
  6476. if (t > 80)
  6477. corr = ((t * 2349) + 135940);
  6478. else if (t >= 50)
  6479. corr = ((t * 964) + 29317);
  6480. else /* < 50 */
  6481. corr = ((t * 301) + 1004);
  6482. corr = corr * ((150142 * state1) / 10000 - 78642);
  6483. corr /= 100000;
  6484. corr2 = (corr * dev_priv->ips.corr);
  6485. state2 = (corr2 * state1) / 10000;
  6486. state2 /= 100; /* convert to mW */
  6487. __i915_update_gfx_val(dev_priv);
  6488. return dev_priv->ips.gfx_power + state2;
  6489. }
  6490. unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
  6491. {
  6492. unsigned long val;
  6493. if (!IS_GEN5(dev_priv))
  6494. return 0;
  6495. spin_lock_irq(&mchdev_lock);
  6496. val = __i915_gfx_val(dev_priv);
  6497. spin_unlock_irq(&mchdev_lock);
  6498. return val;
  6499. }
  6500. /**
  6501. * i915_read_mch_val - return value for IPS use
  6502. *
  6503. * Calculate and return a value for the IPS driver to use when deciding whether
  6504. * we have thermal and power headroom to increase CPU or GPU power budget.
  6505. */
  6506. unsigned long i915_read_mch_val(void)
  6507. {
  6508. struct drm_i915_private *dev_priv;
  6509. unsigned long chipset_val, graphics_val, ret = 0;
  6510. spin_lock_irq(&mchdev_lock);
  6511. if (!i915_mch_dev)
  6512. goto out_unlock;
  6513. dev_priv = i915_mch_dev;
  6514. chipset_val = __i915_chipset_val(dev_priv);
  6515. graphics_val = __i915_gfx_val(dev_priv);
  6516. ret = chipset_val + graphics_val;
  6517. out_unlock:
  6518. spin_unlock_irq(&mchdev_lock);
  6519. return ret;
  6520. }
  6521. EXPORT_SYMBOL_GPL(i915_read_mch_val);
  6522. /**
  6523. * i915_gpu_raise - raise GPU frequency limit
  6524. *
  6525. * Raise the limit; IPS indicates we have thermal headroom.
  6526. */
  6527. bool i915_gpu_raise(void)
  6528. {
  6529. struct drm_i915_private *dev_priv;
  6530. bool ret = true;
  6531. spin_lock_irq(&mchdev_lock);
  6532. if (!i915_mch_dev) {
  6533. ret = false;
  6534. goto out_unlock;
  6535. }
  6536. dev_priv = i915_mch_dev;
  6537. if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
  6538. dev_priv->ips.max_delay--;
  6539. out_unlock:
  6540. spin_unlock_irq(&mchdev_lock);
  6541. return ret;
  6542. }
  6543. EXPORT_SYMBOL_GPL(i915_gpu_raise);
  6544. /**
  6545. * i915_gpu_lower - lower GPU frequency limit
  6546. *
  6547. * IPS indicates we're close to a thermal limit, so throttle back the GPU
  6548. * frequency maximum.
  6549. */
  6550. bool i915_gpu_lower(void)
  6551. {
  6552. struct drm_i915_private *dev_priv;
  6553. bool ret = true;
  6554. spin_lock_irq(&mchdev_lock);
  6555. if (!i915_mch_dev) {
  6556. ret = false;
  6557. goto out_unlock;
  6558. }
  6559. dev_priv = i915_mch_dev;
  6560. if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
  6561. dev_priv->ips.max_delay++;
  6562. out_unlock:
  6563. spin_unlock_irq(&mchdev_lock);
  6564. return ret;
  6565. }
  6566. EXPORT_SYMBOL_GPL(i915_gpu_lower);
  6567. /**
  6568. * i915_gpu_busy - indicate GPU business to IPS
  6569. *
  6570. * Tell the IPS driver whether or not the GPU is busy.
  6571. */
  6572. bool i915_gpu_busy(void)
  6573. {
  6574. bool ret = false;
  6575. spin_lock_irq(&mchdev_lock);
  6576. if (i915_mch_dev)
  6577. ret = i915_mch_dev->gt.awake;
  6578. spin_unlock_irq(&mchdev_lock);
  6579. return ret;
  6580. }
  6581. EXPORT_SYMBOL_GPL(i915_gpu_busy);
  6582. /**
  6583. * i915_gpu_turbo_disable - disable graphics turbo
  6584. *
  6585. * Disable graphics turbo by resetting the max frequency and setting the
  6586. * current frequency to the default.
  6587. */
  6588. bool i915_gpu_turbo_disable(void)
  6589. {
  6590. struct drm_i915_private *dev_priv;
  6591. bool ret = true;
  6592. spin_lock_irq(&mchdev_lock);
  6593. if (!i915_mch_dev) {
  6594. ret = false;
  6595. goto out_unlock;
  6596. }
  6597. dev_priv = i915_mch_dev;
  6598. dev_priv->ips.max_delay = dev_priv->ips.fstart;
  6599. if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
  6600. ret = false;
  6601. out_unlock:
  6602. spin_unlock_irq(&mchdev_lock);
  6603. return ret;
  6604. }
  6605. EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
  6606. /**
  6607. * Tells the intel_ips driver that the i915 driver is now loaded, if
  6608. * IPS got loaded first.
  6609. *
  6610. * This awkward dance is so that neither module has to depend on the
  6611. * other in order for IPS to do the appropriate communication of
  6612. * GPU turbo limits to i915.
  6613. */
  6614. static void
  6615. ips_ping_for_i915_load(void)
  6616. {
  6617. void (*link)(void);
  6618. link = symbol_get(ips_link_to_i915_driver);
  6619. if (link) {
  6620. link();
  6621. symbol_put(ips_link_to_i915_driver);
  6622. }
  6623. }
  6624. void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
  6625. {
  6626. /* We only register the i915 ips part with intel-ips once everything is
  6627. * set up, to avoid intel-ips sneaking in and reading bogus values. */
  6628. spin_lock_irq(&mchdev_lock);
  6629. i915_mch_dev = dev_priv;
  6630. spin_unlock_irq(&mchdev_lock);
  6631. ips_ping_for_i915_load();
  6632. }
  6633. void intel_gpu_ips_teardown(void)
  6634. {
  6635. spin_lock_irq(&mchdev_lock);
  6636. i915_mch_dev = NULL;
  6637. spin_unlock_irq(&mchdev_lock);
  6638. }
  6639. static void intel_init_emon(struct drm_i915_private *dev_priv)
  6640. {
  6641. u32 lcfuse;
  6642. u8 pxw[16];
  6643. int i;
  6644. /* Disable to program */
  6645. I915_WRITE(ECR, 0);
  6646. POSTING_READ(ECR);
  6647. /* Program energy weights for various events */
  6648. I915_WRITE(SDEW, 0x15040d00);
  6649. I915_WRITE(CSIEW0, 0x007f0000);
  6650. I915_WRITE(CSIEW1, 0x1e220004);
  6651. I915_WRITE(CSIEW2, 0x04000004);
  6652. for (i = 0; i < 5; i++)
  6653. I915_WRITE(PEW(i), 0);
  6654. for (i = 0; i < 3; i++)
  6655. I915_WRITE(DEW(i), 0);
  6656. /* Program P-state weights to account for frequency power adjustment */
  6657. for (i = 0; i < 16; i++) {
  6658. u32 pxvidfreq = I915_READ(PXVFREQ(i));
  6659. unsigned long freq = intel_pxfreq(pxvidfreq);
  6660. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  6661. PXVFREQ_PX_SHIFT;
  6662. unsigned long val;
  6663. val = vid * vid;
  6664. val *= (freq / 1000);
  6665. val *= 255;
  6666. val /= (127*127*900);
  6667. if (val > 0xff)
  6668. DRM_ERROR("bad pxval: %ld\n", val);
  6669. pxw[i] = val;
  6670. }
  6671. /* Render standby states get 0 weight */
  6672. pxw[14] = 0;
  6673. pxw[15] = 0;
  6674. for (i = 0; i < 4; i++) {
  6675. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  6676. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  6677. I915_WRITE(PXW(i), val);
  6678. }
  6679. /* Adjust magic regs to magic values (more experimental results) */
  6680. I915_WRITE(OGW0, 0);
  6681. I915_WRITE(OGW1, 0);
  6682. I915_WRITE(EG0, 0x00007f00);
  6683. I915_WRITE(EG1, 0x0000000e);
  6684. I915_WRITE(EG2, 0x000e0000);
  6685. I915_WRITE(EG3, 0x68000300);
  6686. I915_WRITE(EG4, 0x42000000);
  6687. I915_WRITE(EG5, 0x00140031);
  6688. I915_WRITE(EG6, 0);
  6689. I915_WRITE(EG7, 0);
  6690. for (i = 0; i < 8; i++)
  6691. I915_WRITE(PXWL(i), 0);
  6692. /* Enable PMON + select events */
  6693. I915_WRITE(ECR, 0x80000019);
  6694. lcfuse = I915_READ(LCFUSE02);
  6695. dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
  6696. }
  6697. void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
  6698. {
  6699. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  6700. /*
  6701. * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
  6702. * requirement.
  6703. */
  6704. if (!sanitize_rc6(dev_priv)) {
  6705. DRM_INFO("RC6 disabled, disabling runtime PM support\n");
  6706. intel_runtime_pm_get(dev_priv);
  6707. }
  6708. mutex_lock(&dev_priv->pcu_lock);
  6709. /* Initialize RPS limits (for userspace) */
  6710. if (IS_CHERRYVIEW(dev_priv))
  6711. cherryview_init_gt_powersave(dev_priv);
  6712. else if (IS_VALLEYVIEW(dev_priv))
  6713. valleyview_init_gt_powersave(dev_priv);
  6714. else if (INTEL_GEN(dev_priv) >= 6)
  6715. gen6_init_rps_frequencies(dev_priv);
  6716. /* Derive initial user preferences/limits from the hardware limits */
  6717. rps->idle_freq = rps->min_freq;
  6718. rps->cur_freq = rps->idle_freq;
  6719. rps->max_freq_softlimit = rps->max_freq;
  6720. rps->min_freq_softlimit = rps->min_freq;
  6721. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  6722. rps->min_freq_softlimit =
  6723. max_t(int,
  6724. rps->efficient_freq,
  6725. intel_freq_opcode(dev_priv, 450));
  6726. /* After setting max-softlimit, find the overclock max freq */
  6727. if (IS_GEN6(dev_priv) ||
  6728. IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  6729. u32 params = 0;
  6730. sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
  6731. if (params & BIT(31)) { /* OC supported */
  6732. DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
  6733. (rps->max_freq & 0xff) * 50,
  6734. (params & 0xff) * 50);
  6735. rps->max_freq = params & 0xff;
  6736. }
  6737. }
  6738. /* Finally allow us to boost to max by default */
  6739. rps->boost_freq = rps->max_freq;
  6740. mutex_unlock(&dev_priv->pcu_lock);
  6741. }
  6742. void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
  6743. {
  6744. if (IS_VALLEYVIEW(dev_priv))
  6745. valleyview_cleanup_gt_powersave(dev_priv);
  6746. if (!HAS_RC6(dev_priv))
  6747. intel_runtime_pm_put(dev_priv);
  6748. }
  6749. /**
  6750. * intel_suspend_gt_powersave - suspend PM work and helper threads
  6751. * @dev_priv: i915 device
  6752. *
  6753. * We don't want to disable RC6 or other features here, we just want
  6754. * to make sure any work we've queued has finished and won't bother
  6755. * us while we're suspended.
  6756. */
  6757. void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
  6758. {
  6759. if (INTEL_GEN(dev_priv) < 6)
  6760. return;
  6761. /* gen6_rps_idle() will be called later to disable interrupts */
  6762. }
  6763. void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
  6764. {
  6765. dev_priv->gt_pm.rps.enabled = true; /* force RPS disabling */
  6766. dev_priv->gt_pm.rc6.enabled = true; /* force RC6 disabling */
  6767. intel_disable_gt_powersave(dev_priv);
  6768. if (INTEL_GEN(dev_priv) >= 11)
  6769. gen11_reset_rps_interrupts(dev_priv);
  6770. else
  6771. gen6_reset_rps_interrupts(dev_priv);
  6772. }
  6773. static inline void intel_disable_llc_pstate(struct drm_i915_private *i915)
  6774. {
  6775. lockdep_assert_held(&i915->pcu_lock);
  6776. if (!i915->gt_pm.llc_pstate.enabled)
  6777. return;
  6778. /* Currently there is no HW configuration to be done to disable. */
  6779. i915->gt_pm.llc_pstate.enabled = false;
  6780. }
  6781. static void intel_disable_rc6(struct drm_i915_private *dev_priv)
  6782. {
  6783. lockdep_assert_held(&dev_priv->pcu_lock);
  6784. if (!dev_priv->gt_pm.rc6.enabled)
  6785. return;
  6786. if (INTEL_GEN(dev_priv) >= 9)
  6787. gen9_disable_rc6(dev_priv);
  6788. else if (IS_CHERRYVIEW(dev_priv))
  6789. cherryview_disable_rc6(dev_priv);
  6790. else if (IS_VALLEYVIEW(dev_priv))
  6791. valleyview_disable_rc6(dev_priv);
  6792. else if (INTEL_GEN(dev_priv) >= 6)
  6793. gen6_disable_rc6(dev_priv);
  6794. dev_priv->gt_pm.rc6.enabled = false;
  6795. }
  6796. static void intel_disable_rps(struct drm_i915_private *dev_priv)
  6797. {
  6798. lockdep_assert_held(&dev_priv->pcu_lock);
  6799. if (!dev_priv->gt_pm.rps.enabled)
  6800. return;
  6801. if (INTEL_GEN(dev_priv) >= 9)
  6802. gen9_disable_rps(dev_priv);
  6803. else if (IS_CHERRYVIEW(dev_priv))
  6804. cherryview_disable_rps(dev_priv);
  6805. else if (IS_VALLEYVIEW(dev_priv))
  6806. valleyview_disable_rps(dev_priv);
  6807. else if (INTEL_GEN(dev_priv) >= 6)
  6808. gen6_disable_rps(dev_priv);
  6809. else if (IS_IRONLAKE_M(dev_priv))
  6810. ironlake_disable_drps(dev_priv);
  6811. dev_priv->gt_pm.rps.enabled = false;
  6812. }
  6813. void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
  6814. {
  6815. mutex_lock(&dev_priv->pcu_lock);
  6816. intel_disable_rc6(dev_priv);
  6817. intel_disable_rps(dev_priv);
  6818. if (HAS_LLC(dev_priv))
  6819. intel_disable_llc_pstate(dev_priv);
  6820. mutex_unlock(&dev_priv->pcu_lock);
  6821. }
  6822. static inline void intel_enable_llc_pstate(struct drm_i915_private *i915)
  6823. {
  6824. lockdep_assert_held(&i915->pcu_lock);
  6825. if (i915->gt_pm.llc_pstate.enabled)
  6826. return;
  6827. gen6_update_ring_freq(i915);
  6828. i915->gt_pm.llc_pstate.enabled = true;
  6829. }
  6830. static void intel_enable_rc6(struct drm_i915_private *dev_priv)
  6831. {
  6832. lockdep_assert_held(&dev_priv->pcu_lock);
  6833. if (dev_priv->gt_pm.rc6.enabled)
  6834. return;
  6835. if (IS_CHERRYVIEW(dev_priv))
  6836. cherryview_enable_rc6(dev_priv);
  6837. else if (IS_VALLEYVIEW(dev_priv))
  6838. valleyview_enable_rc6(dev_priv);
  6839. else if (INTEL_GEN(dev_priv) >= 9)
  6840. gen9_enable_rc6(dev_priv);
  6841. else if (IS_BROADWELL(dev_priv))
  6842. gen8_enable_rc6(dev_priv);
  6843. else if (INTEL_GEN(dev_priv) >= 6)
  6844. gen6_enable_rc6(dev_priv);
  6845. dev_priv->gt_pm.rc6.enabled = true;
  6846. }
  6847. static void intel_enable_rps(struct drm_i915_private *dev_priv)
  6848. {
  6849. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  6850. lockdep_assert_held(&dev_priv->pcu_lock);
  6851. if (rps->enabled)
  6852. return;
  6853. if (IS_CHERRYVIEW(dev_priv)) {
  6854. cherryview_enable_rps(dev_priv);
  6855. } else if (IS_VALLEYVIEW(dev_priv)) {
  6856. valleyview_enable_rps(dev_priv);
  6857. } else if (INTEL_GEN(dev_priv) >= 9) {
  6858. gen9_enable_rps(dev_priv);
  6859. } else if (IS_BROADWELL(dev_priv)) {
  6860. gen8_enable_rps(dev_priv);
  6861. } else if (INTEL_GEN(dev_priv) >= 6) {
  6862. gen6_enable_rps(dev_priv);
  6863. } else if (IS_IRONLAKE_M(dev_priv)) {
  6864. ironlake_enable_drps(dev_priv);
  6865. intel_init_emon(dev_priv);
  6866. }
  6867. WARN_ON(rps->max_freq < rps->min_freq);
  6868. WARN_ON(rps->idle_freq > rps->max_freq);
  6869. WARN_ON(rps->efficient_freq < rps->min_freq);
  6870. WARN_ON(rps->efficient_freq > rps->max_freq);
  6871. rps->enabled = true;
  6872. }
  6873. void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
  6874. {
  6875. /* Powersaving is controlled by the host when inside a VM */
  6876. if (intel_vgpu_active(dev_priv))
  6877. return;
  6878. mutex_lock(&dev_priv->pcu_lock);
  6879. if (HAS_RC6(dev_priv))
  6880. intel_enable_rc6(dev_priv);
  6881. intel_enable_rps(dev_priv);
  6882. if (HAS_LLC(dev_priv))
  6883. intel_enable_llc_pstate(dev_priv);
  6884. mutex_unlock(&dev_priv->pcu_lock);
  6885. }
  6886. static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
  6887. {
  6888. /*
  6889. * On Ibex Peak and Cougar Point, we need to disable clock
  6890. * gating for the panel power sequencer or it will fail to
  6891. * start up when no ports are active.
  6892. */
  6893. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  6894. }
  6895. static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
  6896. {
  6897. enum pipe pipe;
  6898. for_each_pipe(dev_priv, pipe) {
  6899. I915_WRITE(DSPCNTR(pipe),
  6900. I915_READ(DSPCNTR(pipe)) |
  6901. DISPPLANE_TRICKLE_FEED_DISABLE);
  6902. I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
  6903. POSTING_READ(DSPSURF(pipe));
  6904. }
  6905. }
  6906. static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
  6907. {
  6908. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  6909. /*
  6910. * Required for FBC
  6911. * WaFbcDisableDpfcClockGating:ilk
  6912. */
  6913. dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
  6914. ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
  6915. ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
  6916. I915_WRITE(PCH_3DCGDIS0,
  6917. MARIUNIT_CLOCK_GATE_DISABLE |
  6918. SVSMUNIT_CLOCK_GATE_DISABLE);
  6919. I915_WRITE(PCH_3DCGDIS1,
  6920. VFMUNIT_CLOCK_GATE_DISABLE);
  6921. /*
  6922. * According to the spec the following bits should be set in
  6923. * order to enable memory self-refresh
  6924. * The bit 22/21 of 0x42004
  6925. * The bit 5 of 0x42020
  6926. * The bit 15 of 0x45000
  6927. */
  6928. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6929. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  6930. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  6931. dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
  6932. I915_WRITE(DISP_ARB_CTL,
  6933. (I915_READ(DISP_ARB_CTL) |
  6934. DISP_FBC_WM_DIS));
  6935. /*
  6936. * Based on the document from hardware guys the following bits
  6937. * should be set unconditionally in order to enable FBC.
  6938. * The bit 22 of 0x42000
  6939. * The bit 22 of 0x42004
  6940. * The bit 7,8,9 of 0x42020.
  6941. */
  6942. if (IS_IRONLAKE_M(dev_priv)) {
  6943. /* WaFbcAsynchFlipDisableFbcQueue:ilk */
  6944. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  6945. I915_READ(ILK_DISPLAY_CHICKEN1) |
  6946. ILK_FBCQ_DIS);
  6947. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6948. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6949. ILK_DPARB_GATE);
  6950. }
  6951. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  6952. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6953. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6954. ILK_ELPIN_409_SELECT);
  6955. I915_WRITE(_3D_CHICKEN2,
  6956. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  6957. _3D_CHICKEN2_WM_READ_PIPELINED);
  6958. /* WaDisableRenderCachePipelinedFlush:ilk */
  6959. I915_WRITE(CACHE_MODE_0,
  6960. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  6961. /* WaDisable_RenderCache_OperationalFlush:ilk */
  6962. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  6963. g4x_disable_trickle_feed(dev_priv);
  6964. ibx_init_clock_gating(dev_priv);
  6965. }
  6966. static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
  6967. {
  6968. int pipe;
  6969. uint32_t val;
  6970. /*
  6971. * On Ibex Peak and Cougar Point, we need to disable clock
  6972. * gating for the panel power sequencer or it will fail to
  6973. * start up when no ports are active.
  6974. */
  6975. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
  6976. PCH_DPLUNIT_CLOCK_GATE_DISABLE |
  6977. PCH_CPUNIT_CLOCK_GATE_DISABLE);
  6978. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  6979. DPLS_EDP_PPS_FIX_DIS);
  6980. /* The below fixes the weird display corruption, a few pixels shifted
  6981. * downward, on (only) LVDS of some HP laptops with IVY.
  6982. */
  6983. for_each_pipe(dev_priv, pipe) {
  6984. val = I915_READ(TRANS_CHICKEN2(pipe));
  6985. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  6986. val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  6987. if (dev_priv->vbt.fdi_rx_polarity_inverted)
  6988. val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  6989. val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
  6990. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
  6991. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
  6992. I915_WRITE(TRANS_CHICKEN2(pipe), val);
  6993. }
  6994. /* WADP0ClockGatingDisable */
  6995. for_each_pipe(dev_priv, pipe) {
  6996. I915_WRITE(TRANS_CHICKEN1(pipe),
  6997. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  6998. }
  6999. }
  7000. static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
  7001. {
  7002. uint32_t tmp;
  7003. tmp = I915_READ(MCH_SSKPD);
  7004. if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
  7005. DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
  7006. tmp);
  7007. }
  7008. static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
  7009. {
  7010. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  7011. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  7012. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7013. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7014. ILK_ELPIN_409_SELECT);
  7015. /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
  7016. I915_WRITE(_3D_CHICKEN,
  7017. _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
  7018. /* WaDisable_RenderCache_OperationalFlush:snb */
  7019. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7020. /*
  7021. * BSpec recoomends 8x4 when MSAA is used,
  7022. * however in practice 16x4 seems fastest.
  7023. *
  7024. * Note that PS/WM thread counts depend on the WIZ hashing
  7025. * disable bit, which we don't touch here, but it's good
  7026. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  7027. */
  7028. I915_WRITE(GEN6_GT_MODE,
  7029. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  7030. I915_WRITE(CACHE_MODE_0,
  7031. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  7032. I915_WRITE(GEN6_UCGCTL1,
  7033. I915_READ(GEN6_UCGCTL1) |
  7034. GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
  7035. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  7036. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  7037. * gating disable must be set. Failure to set it results in
  7038. * flickering pixels due to Z write ordering failures after
  7039. * some amount of runtime in the Mesa "fire" demo, and Unigine
  7040. * Sanctuary and Tropics, and apparently anything else with
  7041. * alpha test or pixel discard.
  7042. *
  7043. * According to the spec, bit 11 (RCCUNIT) must also be set,
  7044. * but we didn't debug actual testcases to find it out.
  7045. *
  7046. * WaDisableRCCUnitClockGating:snb
  7047. * WaDisableRCPBUnitClockGating:snb
  7048. */
  7049. I915_WRITE(GEN6_UCGCTL2,
  7050. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  7051. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  7052. /* WaStripsFansDisableFastClipPerformanceFix:snb */
  7053. I915_WRITE(_3D_CHICKEN3,
  7054. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
  7055. /*
  7056. * Bspec says:
  7057. * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
  7058. * 3DSTATE_SF number of SF output attributes is more than 16."
  7059. */
  7060. I915_WRITE(_3D_CHICKEN3,
  7061. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
  7062. /*
  7063. * According to the spec the following bits should be
  7064. * set in order to enable memory self-refresh and fbc:
  7065. * The bit21 and bit22 of 0x42000
  7066. * The bit21 and bit22 of 0x42004
  7067. * The bit5 and bit7 of 0x42020
  7068. * The bit14 of 0x70180
  7069. * The bit14 of 0x71180
  7070. *
  7071. * WaFbcAsynchFlipDisableFbcQueue:snb
  7072. */
  7073. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  7074. I915_READ(ILK_DISPLAY_CHICKEN1) |
  7075. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  7076. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7077. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7078. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  7079. I915_WRITE(ILK_DSPCLK_GATE_D,
  7080. I915_READ(ILK_DSPCLK_GATE_D) |
  7081. ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
  7082. ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
  7083. g4x_disable_trickle_feed(dev_priv);
  7084. cpt_init_clock_gating(dev_priv);
  7085. gen6_check_mch_setup(dev_priv);
  7086. }
  7087. static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
  7088. {
  7089. uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
  7090. /*
  7091. * WaVSThreadDispatchOverride:ivb,vlv
  7092. *
  7093. * This actually overrides the dispatch
  7094. * mode for all thread types.
  7095. */
  7096. reg &= ~GEN7_FF_SCHED_MASK;
  7097. reg |= GEN7_FF_TS_SCHED_HW;
  7098. reg |= GEN7_FF_VS_SCHED_HW;
  7099. reg |= GEN7_FF_DS_SCHED_HW;
  7100. I915_WRITE(GEN7_FF_THREAD_MODE, reg);
  7101. }
  7102. static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
  7103. {
  7104. /*
  7105. * TODO: this bit should only be enabled when really needed, then
  7106. * disabled when not needed anymore in order to save power.
  7107. */
  7108. if (HAS_PCH_LPT_LP(dev_priv))
  7109. I915_WRITE(SOUTH_DSPCLK_GATE_D,
  7110. I915_READ(SOUTH_DSPCLK_GATE_D) |
  7111. PCH_LP_PARTITION_LEVEL_DISABLE);
  7112. /* WADPOClockGatingDisable:hsw */
  7113. I915_WRITE(TRANS_CHICKEN1(PIPE_A),
  7114. I915_READ(TRANS_CHICKEN1(PIPE_A)) |
  7115. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  7116. }
  7117. static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
  7118. {
  7119. if (HAS_PCH_LPT_LP(dev_priv)) {
  7120. uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7121. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7122. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7123. }
  7124. }
  7125. static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
  7126. int general_prio_credits,
  7127. int high_prio_credits)
  7128. {
  7129. u32 misccpctl;
  7130. u32 val;
  7131. /* WaTempDisableDOPClkGating:bdw */
  7132. misccpctl = I915_READ(GEN7_MISCCPCTL);
  7133. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  7134. val = I915_READ(GEN8_L3SQCREG1);
  7135. val &= ~L3_PRIO_CREDITS_MASK;
  7136. val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
  7137. val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
  7138. I915_WRITE(GEN8_L3SQCREG1, val);
  7139. /*
  7140. * Wait at least 100 clocks before re-enabling clock gating.
  7141. * See the definition of L3SQCREG1 in BSpec.
  7142. */
  7143. POSTING_READ(GEN8_L3SQCREG1);
  7144. udelay(1);
  7145. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  7146. }
  7147. static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
  7148. {
  7149. /* This is not an Wa. Enable to reduce Sampler power */
  7150. I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
  7151. I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
  7152. }
  7153. static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
  7154. {
  7155. if (!HAS_PCH_CNP(dev_priv))
  7156. return;
  7157. /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
  7158. I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
  7159. CNP_PWM_CGE_GATING_DISABLE);
  7160. }
  7161. static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
  7162. {
  7163. u32 val;
  7164. cnp_init_clock_gating(dev_priv);
  7165. /* This is not an Wa. Enable for better image quality */
  7166. I915_WRITE(_3D_CHICKEN3,
  7167. _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
  7168. /* WaEnableChickenDCPR:cnl */
  7169. I915_WRITE(GEN8_CHICKEN_DCPR_1,
  7170. I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
  7171. /* WaFbcWakeMemOn:cnl */
  7172. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  7173. DISP_FBC_MEMORY_WAKE);
  7174. val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
  7175. /* ReadHitWriteOnlyDisable:cnl */
  7176. val |= RCCUNIT_CLKGATE_DIS;
  7177. /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
  7178. if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
  7179. val |= SARBUNIT_CLKGATE_DIS;
  7180. I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
  7181. /* Wa_2201832410:cnl */
  7182. val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
  7183. val |= GWUNIT_CLKGATE_DIS;
  7184. I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
  7185. /* WaDisableVFclkgate:cnl */
  7186. /* WaVFUnitClockGatingDisable:cnl */
  7187. val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
  7188. val |= VFUNIT_CLKGATE_DIS;
  7189. I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
  7190. }
  7191. static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
  7192. {
  7193. cnp_init_clock_gating(dev_priv);
  7194. gen9_init_clock_gating(dev_priv);
  7195. /* WaFbcNukeOnHostModify:cfl */
  7196. I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
  7197. ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
  7198. }
  7199. static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
  7200. {
  7201. gen9_init_clock_gating(dev_priv);
  7202. /* WaDisableSDEUnitClockGating:kbl */
  7203. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
  7204. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  7205. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  7206. /* WaDisableGamClockGating:kbl */
  7207. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
  7208. I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
  7209. GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
  7210. /* WaFbcNukeOnHostModify:kbl */
  7211. I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
  7212. ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
  7213. }
  7214. static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
  7215. {
  7216. gen9_init_clock_gating(dev_priv);
  7217. /* WAC6entrylatency:skl */
  7218. I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
  7219. FBC_LLC_FULLY_OPEN);
  7220. /* WaFbcNukeOnHostModify:skl */
  7221. I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
  7222. ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
  7223. }
  7224. static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
  7225. {
  7226. /* The GTT cache must be disabled if the system is using 2M pages. */
  7227. bool can_use_gtt_cache = !HAS_PAGE_SIZES(dev_priv,
  7228. I915_GTT_PAGE_SIZE_2M);
  7229. enum pipe pipe;
  7230. /* WaSwitchSolVfFArbitrationPriority:bdw */
  7231. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  7232. /* WaPsrDPAMaskVBlankInSRD:bdw */
  7233. I915_WRITE(CHICKEN_PAR1_1,
  7234. I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
  7235. /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
  7236. for_each_pipe(dev_priv, pipe) {
  7237. I915_WRITE(CHICKEN_PIPESL_1(pipe),
  7238. I915_READ(CHICKEN_PIPESL_1(pipe)) |
  7239. BDW_DPRS_MASK_VBLANK_SRD);
  7240. }
  7241. /* WaVSRefCountFullforceMissDisable:bdw */
  7242. /* WaDSRefCountFullforceMissDisable:bdw */
  7243. I915_WRITE(GEN7_FF_THREAD_MODE,
  7244. I915_READ(GEN7_FF_THREAD_MODE) &
  7245. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  7246. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  7247. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  7248. /* WaDisableSDEUnitClockGating:bdw */
  7249. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  7250. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  7251. /* WaProgramL3SqcReg1Default:bdw */
  7252. gen8_set_l3sqc_credits(dev_priv, 30, 2);
  7253. /* WaGttCachingOffByDefault:bdw */
  7254. I915_WRITE(HSW_GTT_CACHE_EN, can_use_gtt_cache ? GTT_CACHE_EN_ALL : 0);
  7255. /* WaKVMNotificationOnConfigChange:bdw */
  7256. I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
  7257. | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
  7258. lpt_init_clock_gating(dev_priv);
  7259. /* WaDisableDopClockGating:bdw
  7260. *
  7261. * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
  7262. * clock gating.
  7263. */
  7264. I915_WRITE(GEN6_UCGCTL1,
  7265. I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
  7266. }
  7267. static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
  7268. {
  7269. /* L3 caching of data atomics doesn't work -- disable it. */
  7270. I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
  7271. I915_WRITE(HSW_ROW_CHICKEN3,
  7272. _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
  7273. /* This is required by WaCatErrorRejectionIssue:hsw */
  7274. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  7275. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  7276. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  7277. /* WaVSRefCountFullforceMissDisable:hsw */
  7278. I915_WRITE(GEN7_FF_THREAD_MODE,
  7279. I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
  7280. /* WaDisable_RenderCache_OperationalFlush:hsw */
  7281. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7282. /* enable HiZ Raw Stall Optimization */
  7283. I915_WRITE(CACHE_MODE_0_GEN7,
  7284. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  7285. /* WaDisable4x2SubspanOptimization:hsw */
  7286. I915_WRITE(CACHE_MODE_1,
  7287. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  7288. /*
  7289. * BSpec recommends 8x4 when MSAA is used,
  7290. * however in practice 16x4 seems fastest.
  7291. *
  7292. * Note that PS/WM thread counts depend on the WIZ hashing
  7293. * disable bit, which we don't touch here, but it's good
  7294. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  7295. */
  7296. I915_WRITE(GEN7_GT_MODE,
  7297. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  7298. /* WaSampleCChickenBitEnable:hsw */
  7299. I915_WRITE(HALF_SLICE_CHICKEN3,
  7300. _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
  7301. /* WaSwitchSolVfFArbitrationPriority:hsw */
  7302. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  7303. lpt_init_clock_gating(dev_priv);
  7304. }
  7305. static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
  7306. {
  7307. uint32_t snpcr;
  7308. I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
  7309. /* WaDisableEarlyCull:ivb */
  7310. I915_WRITE(_3D_CHICKEN3,
  7311. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  7312. /* WaDisableBackToBackFlipFix:ivb */
  7313. I915_WRITE(IVB_CHICKEN3,
  7314. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  7315. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  7316. /* WaDisablePSDDualDispatchEnable:ivb */
  7317. if (IS_IVB_GT1(dev_priv))
  7318. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  7319. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  7320. /* WaDisable_RenderCache_OperationalFlush:ivb */
  7321. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7322. /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
  7323. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  7324. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  7325. /* WaApplyL3ControlAndL3ChickenMode:ivb */
  7326. I915_WRITE(GEN7_L3CNTLREG1,
  7327. GEN7_WA_FOR_GEN7_L3_CONTROL);
  7328. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  7329. GEN7_WA_L3_CHICKEN_MODE);
  7330. if (IS_IVB_GT1(dev_priv))
  7331. I915_WRITE(GEN7_ROW_CHICKEN2,
  7332. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  7333. else {
  7334. /* must write both registers */
  7335. I915_WRITE(GEN7_ROW_CHICKEN2,
  7336. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  7337. I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
  7338. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  7339. }
  7340. /* WaForceL3Serialization:ivb */
  7341. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  7342. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  7343. /*
  7344. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  7345. * This implements the WaDisableRCZUnitClockGating:ivb workaround.
  7346. */
  7347. I915_WRITE(GEN6_UCGCTL2,
  7348. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  7349. /* This is required by WaCatErrorRejectionIssue:ivb */
  7350. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  7351. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  7352. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  7353. g4x_disable_trickle_feed(dev_priv);
  7354. gen7_setup_fixed_func_scheduler(dev_priv);
  7355. if (0) { /* causes HiZ corruption on ivb:gt1 */
  7356. /* enable HiZ Raw Stall Optimization */
  7357. I915_WRITE(CACHE_MODE_0_GEN7,
  7358. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  7359. }
  7360. /* WaDisable4x2SubspanOptimization:ivb */
  7361. I915_WRITE(CACHE_MODE_1,
  7362. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  7363. /*
  7364. * BSpec recommends 8x4 when MSAA is used,
  7365. * however in practice 16x4 seems fastest.
  7366. *
  7367. * Note that PS/WM thread counts depend on the WIZ hashing
  7368. * disable bit, which we don't touch here, but it's good
  7369. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  7370. */
  7371. I915_WRITE(GEN7_GT_MODE,
  7372. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  7373. snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
  7374. snpcr &= ~GEN6_MBC_SNPCR_MASK;
  7375. snpcr |= GEN6_MBC_SNPCR_MED;
  7376. I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
  7377. if (!HAS_PCH_NOP(dev_priv))
  7378. cpt_init_clock_gating(dev_priv);
  7379. gen6_check_mch_setup(dev_priv);
  7380. }
  7381. static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
  7382. {
  7383. /* WaDisableEarlyCull:vlv */
  7384. I915_WRITE(_3D_CHICKEN3,
  7385. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  7386. /* WaDisableBackToBackFlipFix:vlv */
  7387. I915_WRITE(IVB_CHICKEN3,
  7388. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  7389. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  7390. /* WaPsdDispatchEnable:vlv */
  7391. /* WaDisablePSDDualDispatchEnable:vlv */
  7392. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  7393. _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
  7394. GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  7395. /* WaDisable_RenderCache_OperationalFlush:vlv */
  7396. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7397. /* WaForceL3Serialization:vlv */
  7398. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  7399. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  7400. /* WaDisableDopClockGating:vlv */
  7401. I915_WRITE(GEN7_ROW_CHICKEN2,
  7402. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  7403. /* This is required by WaCatErrorRejectionIssue:vlv */
  7404. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  7405. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  7406. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  7407. gen7_setup_fixed_func_scheduler(dev_priv);
  7408. /*
  7409. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  7410. * This implements the WaDisableRCZUnitClockGating:vlv workaround.
  7411. */
  7412. I915_WRITE(GEN6_UCGCTL2,
  7413. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  7414. /* WaDisableL3Bank2xClockGate:vlv
  7415. * Disabling L3 clock gating- MMIO 940c[25] = 1
  7416. * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
  7417. I915_WRITE(GEN7_UCGCTL4,
  7418. I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
  7419. /*
  7420. * BSpec says this must be set, even though
  7421. * WaDisable4x2SubspanOptimization isn't listed for VLV.
  7422. */
  7423. I915_WRITE(CACHE_MODE_1,
  7424. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  7425. /*
  7426. * BSpec recommends 8x4 when MSAA is used,
  7427. * however in practice 16x4 seems fastest.
  7428. *
  7429. * Note that PS/WM thread counts depend on the WIZ hashing
  7430. * disable bit, which we don't touch here, but it's good
  7431. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  7432. */
  7433. I915_WRITE(GEN7_GT_MODE,
  7434. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  7435. /*
  7436. * WaIncreaseL3CreditsForVLVB0:vlv
  7437. * This is the hardware default actually.
  7438. */
  7439. I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
  7440. /*
  7441. * WaDisableVLVClockGating_VBIIssue:vlv
  7442. * Disable clock gating on th GCFG unit to prevent a delay
  7443. * in the reporting of vblank events.
  7444. */
  7445. I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
  7446. }
  7447. static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
  7448. {
  7449. /* WaVSRefCountFullforceMissDisable:chv */
  7450. /* WaDSRefCountFullforceMissDisable:chv */
  7451. I915_WRITE(GEN7_FF_THREAD_MODE,
  7452. I915_READ(GEN7_FF_THREAD_MODE) &
  7453. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  7454. /* WaDisableSemaphoreAndSyncFlipWait:chv */
  7455. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  7456. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  7457. /* WaDisableCSUnitClockGating:chv */
  7458. I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
  7459. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  7460. /* WaDisableSDEUnitClockGating:chv */
  7461. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  7462. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  7463. /*
  7464. * WaProgramL3SqcReg1Default:chv
  7465. * See gfxspecs/Related Documents/Performance Guide/
  7466. * LSQC Setting Recommendations.
  7467. */
  7468. gen8_set_l3sqc_credits(dev_priv, 38, 2);
  7469. /*
  7470. * GTT cache may not work with big pages, so if those
  7471. * are ever enabled GTT cache may need to be disabled.
  7472. */
  7473. I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
  7474. }
  7475. static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
  7476. {
  7477. uint32_t dspclk_gate;
  7478. I915_WRITE(RENCLK_GATE_D1, 0);
  7479. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  7480. GS_UNIT_CLOCK_GATE_DISABLE |
  7481. CL_UNIT_CLOCK_GATE_DISABLE);
  7482. I915_WRITE(RAMCLK_GATE_D, 0);
  7483. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  7484. OVRUNIT_CLOCK_GATE_DISABLE |
  7485. OVCUNIT_CLOCK_GATE_DISABLE;
  7486. if (IS_GM45(dev_priv))
  7487. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  7488. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  7489. /* WaDisableRenderCachePipelinedFlush */
  7490. I915_WRITE(CACHE_MODE_0,
  7491. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  7492. /* WaDisable_RenderCache_OperationalFlush:g4x */
  7493. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7494. g4x_disable_trickle_feed(dev_priv);
  7495. }
  7496. static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
  7497. {
  7498. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  7499. I915_WRITE(RENCLK_GATE_D2, 0);
  7500. I915_WRITE(DSPCLK_GATE_D, 0);
  7501. I915_WRITE(RAMCLK_GATE_D, 0);
  7502. I915_WRITE16(DEUC, 0);
  7503. I915_WRITE(MI_ARB_STATE,
  7504. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  7505. /* WaDisable_RenderCache_OperationalFlush:gen4 */
  7506. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7507. }
  7508. static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
  7509. {
  7510. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  7511. I965_RCC_CLOCK_GATE_DISABLE |
  7512. I965_RCPB_CLOCK_GATE_DISABLE |
  7513. I965_ISC_CLOCK_GATE_DISABLE |
  7514. I965_FBC_CLOCK_GATE_DISABLE);
  7515. I915_WRITE(RENCLK_GATE_D2, 0);
  7516. I915_WRITE(MI_ARB_STATE,
  7517. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  7518. /* WaDisable_RenderCache_OperationalFlush:gen4 */
  7519. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7520. }
  7521. static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
  7522. {
  7523. u32 dstate = I915_READ(D_STATE);
  7524. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  7525. DSTATE_DOT_CLOCK_GATING;
  7526. I915_WRITE(D_STATE, dstate);
  7527. if (IS_PINEVIEW(dev_priv))
  7528. I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
  7529. /* IIR "flip pending" means done if this bit is set */
  7530. I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
  7531. /* interrupts should cause a wake up from C3 */
  7532. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
  7533. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  7534. I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  7535. I915_WRITE(MI_ARB_STATE,
  7536. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  7537. }
  7538. static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
  7539. {
  7540. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  7541. /* interrupts should cause a wake up from C3 */
  7542. I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
  7543. _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
  7544. I915_WRITE(MEM_MODE,
  7545. _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
  7546. }
  7547. static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
  7548. {
  7549. I915_WRITE(MEM_MODE,
  7550. _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
  7551. _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
  7552. }
  7553. void intel_init_clock_gating(struct drm_i915_private *dev_priv)
  7554. {
  7555. dev_priv->display.init_clock_gating(dev_priv);
  7556. }
  7557. void intel_suspend_hw(struct drm_i915_private *dev_priv)
  7558. {
  7559. if (HAS_PCH_LPT(dev_priv))
  7560. lpt_suspend_hw(dev_priv);
  7561. }
  7562. static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
  7563. {
  7564. DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
  7565. }
  7566. /**
  7567. * intel_init_clock_gating_hooks - setup the clock gating hooks
  7568. * @dev_priv: device private
  7569. *
  7570. * Setup the hooks that configure which clocks of a given platform can be
  7571. * gated and also apply various GT and display specific workarounds for these
  7572. * platforms. Note that some GT specific workarounds are applied separately
  7573. * when GPU contexts or batchbuffers start their execution.
  7574. */
  7575. void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
  7576. {
  7577. if (IS_ICELAKE(dev_priv))
  7578. dev_priv->display.init_clock_gating = icl_init_clock_gating;
  7579. else if (IS_CANNONLAKE(dev_priv))
  7580. dev_priv->display.init_clock_gating = cnl_init_clock_gating;
  7581. else if (IS_COFFEELAKE(dev_priv))
  7582. dev_priv->display.init_clock_gating = cfl_init_clock_gating;
  7583. else if (IS_SKYLAKE(dev_priv))
  7584. dev_priv->display.init_clock_gating = skl_init_clock_gating;
  7585. else if (IS_KABYLAKE(dev_priv))
  7586. dev_priv->display.init_clock_gating = kbl_init_clock_gating;
  7587. else if (IS_BROXTON(dev_priv))
  7588. dev_priv->display.init_clock_gating = bxt_init_clock_gating;
  7589. else if (IS_GEMINILAKE(dev_priv))
  7590. dev_priv->display.init_clock_gating = glk_init_clock_gating;
  7591. else if (IS_BROADWELL(dev_priv))
  7592. dev_priv->display.init_clock_gating = bdw_init_clock_gating;
  7593. else if (IS_CHERRYVIEW(dev_priv))
  7594. dev_priv->display.init_clock_gating = chv_init_clock_gating;
  7595. else if (IS_HASWELL(dev_priv))
  7596. dev_priv->display.init_clock_gating = hsw_init_clock_gating;
  7597. else if (IS_IVYBRIDGE(dev_priv))
  7598. dev_priv->display.init_clock_gating = ivb_init_clock_gating;
  7599. else if (IS_VALLEYVIEW(dev_priv))
  7600. dev_priv->display.init_clock_gating = vlv_init_clock_gating;
  7601. else if (IS_GEN6(dev_priv))
  7602. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  7603. else if (IS_GEN5(dev_priv))
  7604. dev_priv->display.init_clock_gating = ilk_init_clock_gating;
  7605. else if (IS_G4X(dev_priv))
  7606. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  7607. else if (IS_I965GM(dev_priv))
  7608. dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
  7609. else if (IS_I965G(dev_priv))
  7610. dev_priv->display.init_clock_gating = i965g_init_clock_gating;
  7611. else if (IS_GEN3(dev_priv))
  7612. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  7613. else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
  7614. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  7615. else if (IS_GEN2(dev_priv))
  7616. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  7617. else {
  7618. MISSING_CASE(INTEL_DEVID(dev_priv));
  7619. dev_priv->display.init_clock_gating = nop_init_clock_gating;
  7620. }
  7621. }
  7622. /* Set up chip specific power management-related functions */
  7623. void intel_init_pm(struct drm_i915_private *dev_priv)
  7624. {
  7625. intel_fbc_init(dev_priv);
  7626. /* For cxsr */
  7627. if (IS_PINEVIEW(dev_priv))
  7628. i915_pineview_get_mem_freq(dev_priv);
  7629. else if (IS_GEN5(dev_priv))
  7630. i915_ironlake_get_mem_freq(dev_priv);
  7631. /* For FIFO watermark updates */
  7632. if (INTEL_GEN(dev_priv) >= 9) {
  7633. skl_setup_wm_latency(dev_priv);
  7634. dev_priv->display.initial_watermarks = skl_initial_wm;
  7635. dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
  7636. dev_priv->display.compute_global_watermarks = skl_compute_wm;
  7637. } else if (HAS_PCH_SPLIT(dev_priv)) {
  7638. ilk_setup_wm_latency(dev_priv);
  7639. if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
  7640. dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
  7641. (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
  7642. dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
  7643. dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
  7644. dev_priv->display.compute_intermediate_wm =
  7645. ilk_compute_intermediate_wm;
  7646. dev_priv->display.initial_watermarks =
  7647. ilk_initial_watermarks;
  7648. dev_priv->display.optimize_watermarks =
  7649. ilk_optimize_watermarks;
  7650. } else {
  7651. DRM_DEBUG_KMS("Failed to read display plane latency. "
  7652. "Disable CxSR\n");
  7653. }
  7654. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  7655. vlv_setup_wm_latency(dev_priv);
  7656. dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
  7657. dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
  7658. dev_priv->display.initial_watermarks = vlv_initial_watermarks;
  7659. dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
  7660. dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
  7661. } else if (IS_G4X(dev_priv)) {
  7662. g4x_setup_wm_latency(dev_priv);
  7663. dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
  7664. dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
  7665. dev_priv->display.initial_watermarks = g4x_initial_watermarks;
  7666. dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
  7667. } else if (IS_PINEVIEW(dev_priv)) {
  7668. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
  7669. dev_priv->is_ddr3,
  7670. dev_priv->fsb_freq,
  7671. dev_priv->mem_freq)) {
  7672. DRM_INFO("failed to find known CxSR latency "
  7673. "(found ddr%s fsb freq %d, mem freq %d), "
  7674. "disabling CxSR\n",
  7675. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  7676. dev_priv->fsb_freq, dev_priv->mem_freq);
  7677. /* Disable CxSR and never update its watermark again */
  7678. intel_set_memory_cxsr(dev_priv, false);
  7679. dev_priv->display.update_wm = NULL;
  7680. } else
  7681. dev_priv->display.update_wm = pineview_update_wm;
  7682. } else if (IS_GEN4(dev_priv)) {
  7683. dev_priv->display.update_wm = i965_update_wm;
  7684. } else if (IS_GEN3(dev_priv)) {
  7685. dev_priv->display.update_wm = i9xx_update_wm;
  7686. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  7687. } else if (IS_GEN2(dev_priv)) {
  7688. if (INTEL_INFO(dev_priv)->num_pipes == 1) {
  7689. dev_priv->display.update_wm = i845_update_wm;
  7690. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  7691. } else {
  7692. dev_priv->display.update_wm = i9xx_update_wm;
  7693. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  7694. }
  7695. } else {
  7696. DRM_ERROR("unexpected fall-through in intel_init_pm\n");
  7697. }
  7698. }
  7699. static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
  7700. {
  7701. uint32_t flags =
  7702. I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
  7703. switch (flags) {
  7704. case GEN6_PCODE_SUCCESS:
  7705. return 0;
  7706. case GEN6_PCODE_UNIMPLEMENTED_CMD:
  7707. return -ENODEV;
  7708. case GEN6_PCODE_ILLEGAL_CMD:
  7709. return -ENXIO;
  7710. case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
  7711. case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
  7712. return -EOVERFLOW;
  7713. case GEN6_PCODE_TIMEOUT:
  7714. return -ETIMEDOUT;
  7715. default:
  7716. MISSING_CASE(flags);
  7717. return 0;
  7718. }
  7719. }
  7720. static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
  7721. {
  7722. uint32_t flags =
  7723. I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
  7724. switch (flags) {
  7725. case GEN6_PCODE_SUCCESS:
  7726. return 0;
  7727. case GEN6_PCODE_ILLEGAL_CMD:
  7728. return -ENXIO;
  7729. case GEN7_PCODE_TIMEOUT:
  7730. return -ETIMEDOUT;
  7731. case GEN7_PCODE_ILLEGAL_DATA:
  7732. return -EINVAL;
  7733. case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
  7734. return -EOVERFLOW;
  7735. default:
  7736. MISSING_CASE(flags);
  7737. return 0;
  7738. }
  7739. }
  7740. int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
  7741. {
  7742. int status;
  7743. WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
  7744. /* GEN6_PCODE_* are outside of the forcewake domain, we can
  7745. * use te fw I915_READ variants to reduce the amount of work
  7746. * required when reading/writing.
  7747. */
  7748. if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  7749. DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps\n",
  7750. mbox, __builtin_return_address(0));
  7751. return -EAGAIN;
  7752. }
  7753. I915_WRITE_FW(GEN6_PCODE_DATA, *val);
  7754. I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
  7755. I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  7756. if (__intel_wait_for_register_fw(dev_priv,
  7757. GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
  7758. 500, 0, NULL)) {
  7759. DRM_ERROR("timeout waiting for pcode read (from mbox %x) to finish for %ps\n",
  7760. mbox, __builtin_return_address(0));
  7761. return -ETIMEDOUT;
  7762. }
  7763. *val = I915_READ_FW(GEN6_PCODE_DATA);
  7764. I915_WRITE_FW(GEN6_PCODE_DATA, 0);
  7765. if (INTEL_GEN(dev_priv) > 6)
  7766. status = gen7_check_mailbox_status(dev_priv);
  7767. else
  7768. status = gen6_check_mailbox_status(dev_priv);
  7769. if (status) {
  7770. DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps: %d\n",
  7771. mbox, __builtin_return_address(0), status);
  7772. return status;
  7773. }
  7774. return 0;
  7775. }
  7776. int sandybridge_pcode_write_timeout(struct drm_i915_private *dev_priv,
  7777. u32 mbox, u32 val,
  7778. int fast_timeout_us, int slow_timeout_ms)
  7779. {
  7780. int status;
  7781. WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
  7782. /* GEN6_PCODE_* are outside of the forcewake domain, we can
  7783. * use te fw I915_READ variants to reduce the amount of work
  7784. * required when reading/writing.
  7785. */
  7786. if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  7787. DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps\n",
  7788. val, mbox, __builtin_return_address(0));
  7789. return -EAGAIN;
  7790. }
  7791. I915_WRITE_FW(GEN6_PCODE_DATA, val);
  7792. I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
  7793. I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  7794. if (__intel_wait_for_register_fw(dev_priv,
  7795. GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
  7796. fast_timeout_us, slow_timeout_ms,
  7797. NULL)) {
  7798. DRM_ERROR("timeout waiting for pcode write of 0x%08x to mbox %x to finish for %ps\n",
  7799. val, mbox, __builtin_return_address(0));
  7800. return -ETIMEDOUT;
  7801. }
  7802. I915_WRITE_FW(GEN6_PCODE_DATA, 0);
  7803. if (INTEL_GEN(dev_priv) > 6)
  7804. status = gen7_check_mailbox_status(dev_priv);
  7805. else
  7806. status = gen6_check_mailbox_status(dev_priv);
  7807. if (status) {
  7808. DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps: %d\n",
  7809. val, mbox, __builtin_return_address(0), status);
  7810. return status;
  7811. }
  7812. return 0;
  7813. }
  7814. static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
  7815. u32 request, u32 reply_mask, u32 reply,
  7816. u32 *status)
  7817. {
  7818. u32 val = request;
  7819. *status = sandybridge_pcode_read(dev_priv, mbox, &val);
  7820. return *status || ((val & reply_mask) == reply);
  7821. }
  7822. /**
  7823. * skl_pcode_request - send PCODE request until acknowledgment
  7824. * @dev_priv: device private
  7825. * @mbox: PCODE mailbox ID the request is targeted for
  7826. * @request: request ID
  7827. * @reply_mask: mask used to check for request acknowledgment
  7828. * @reply: value used to check for request acknowledgment
  7829. * @timeout_base_ms: timeout for polling with preemption enabled
  7830. *
  7831. * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
  7832. * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
  7833. * The request is acknowledged once the PCODE reply dword equals @reply after
  7834. * applying @reply_mask. Polling is first attempted with preemption enabled
  7835. * for @timeout_base_ms and if this times out for another 50 ms with
  7836. * preemption disabled.
  7837. *
  7838. * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
  7839. * other error as reported by PCODE.
  7840. */
  7841. int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
  7842. u32 reply_mask, u32 reply, int timeout_base_ms)
  7843. {
  7844. u32 status;
  7845. int ret;
  7846. WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
  7847. #define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
  7848. &status)
  7849. /*
  7850. * Prime the PCODE by doing a request first. Normally it guarantees
  7851. * that a subsequent request, at most @timeout_base_ms later, succeeds.
  7852. * _wait_for() doesn't guarantee when its passed condition is evaluated
  7853. * first, so send the first request explicitly.
  7854. */
  7855. if (COND) {
  7856. ret = 0;
  7857. goto out;
  7858. }
  7859. ret = _wait_for(COND, timeout_base_ms * 1000, 10, 10);
  7860. if (!ret)
  7861. goto out;
  7862. /*
  7863. * The above can time out if the number of requests was low (2 in the
  7864. * worst case) _and_ PCODE was busy for some reason even after a
  7865. * (queued) request and @timeout_base_ms delay. As a workaround retry
  7866. * the poll with preemption disabled to maximize the number of
  7867. * requests. Increase the timeout from @timeout_base_ms to 50ms to
  7868. * account for interrupts that could reduce the number of these
  7869. * requests, and for any quirks of the PCODE firmware that delays
  7870. * the request completion.
  7871. */
  7872. DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
  7873. WARN_ON_ONCE(timeout_base_ms > 3);
  7874. preempt_disable();
  7875. ret = wait_for_atomic(COND, 50);
  7876. preempt_enable();
  7877. out:
  7878. return ret ? ret : status;
  7879. #undef COND
  7880. }
  7881. static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
  7882. {
  7883. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  7884. /*
  7885. * N = val - 0xb7
  7886. * Slow = Fast = GPLL ref * N
  7887. */
  7888. return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
  7889. }
  7890. static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
  7891. {
  7892. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  7893. return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
  7894. }
  7895. static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
  7896. {
  7897. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  7898. /*
  7899. * N = val / 2
  7900. * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
  7901. */
  7902. return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
  7903. }
  7904. static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
  7905. {
  7906. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  7907. /* CHV needs even values */
  7908. return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
  7909. }
  7910. int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
  7911. {
  7912. if (INTEL_GEN(dev_priv) >= 9)
  7913. return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
  7914. GEN9_FREQ_SCALER);
  7915. else if (IS_CHERRYVIEW(dev_priv))
  7916. return chv_gpu_freq(dev_priv, val);
  7917. else if (IS_VALLEYVIEW(dev_priv))
  7918. return byt_gpu_freq(dev_priv, val);
  7919. else
  7920. return val * GT_FREQUENCY_MULTIPLIER;
  7921. }
  7922. int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
  7923. {
  7924. if (INTEL_GEN(dev_priv) >= 9)
  7925. return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
  7926. GT_FREQUENCY_MULTIPLIER);
  7927. else if (IS_CHERRYVIEW(dev_priv))
  7928. return chv_freq_opcode(dev_priv, val);
  7929. else if (IS_VALLEYVIEW(dev_priv))
  7930. return byt_freq_opcode(dev_priv, val);
  7931. else
  7932. return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
  7933. }
  7934. void intel_pm_setup(struct drm_i915_private *dev_priv)
  7935. {
  7936. mutex_init(&dev_priv->pcu_lock);
  7937. atomic_set(&dev_priv->gt_pm.rps.num_waiters, 0);
  7938. dev_priv->runtime_pm.suspended = false;
  7939. atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
  7940. }
  7941. static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
  7942. const i915_reg_t reg)
  7943. {
  7944. u32 lower, upper, tmp;
  7945. int loop = 2;
  7946. /*
  7947. * The register accessed do not need forcewake. We borrow
  7948. * uncore lock to prevent concurrent access to range reg.
  7949. */
  7950. lockdep_assert_held(&dev_priv->uncore.lock);
  7951. /*
  7952. * vlv and chv residency counters are 40 bits in width.
  7953. * With a control bit, we can choose between upper or lower
  7954. * 32bit window into this counter.
  7955. *
  7956. * Although we always use the counter in high-range mode elsewhere,
  7957. * userspace may attempt to read the value before rc6 is initialised,
  7958. * before we have set the default VLV_COUNTER_CONTROL value. So always
  7959. * set the high bit to be safe.
  7960. */
  7961. I915_WRITE_FW(VLV_COUNTER_CONTROL,
  7962. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
  7963. upper = I915_READ_FW(reg);
  7964. do {
  7965. tmp = upper;
  7966. I915_WRITE_FW(VLV_COUNTER_CONTROL,
  7967. _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
  7968. lower = I915_READ_FW(reg);
  7969. I915_WRITE_FW(VLV_COUNTER_CONTROL,
  7970. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
  7971. upper = I915_READ_FW(reg);
  7972. } while (upper != tmp && --loop);
  7973. /*
  7974. * Everywhere else we always use VLV_COUNTER_CONTROL with the
  7975. * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
  7976. * now.
  7977. */
  7978. return lower | (u64)upper << 8;
  7979. }
  7980. u64 intel_rc6_residency_ns(struct drm_i915_private *dev_priv,
  7981. const i915_reg_t reg)
  7982. {
  7983. u64 time_hw, prev_hw, overflow_hw;
  7984. unsigned int fw_domains;
  7985. unsigned long flags;
  7986. unsigned int i;
  7987. u32 mul, div;
  7988. if (!HAS_RC6(dev_priv))
  7989. return 0;
  7990. /*
  7991. * Store previous hw counter values for counter wrap-around handling.
  7992. *
  7993. * There are only four interesting registers and they live next to each
  7994. * other so we can use the relative address, compared to the smallest
  7995. * one as the index into driver storage.
  7996. */
  7997. i = (i915_mmio_reg_offset(reg) -
  7998. i915_mmio_reg_offset(GEN6_GT_GFX_RC6_LOCKED)) / sizeof(u32);
  7999. if (WARN_ON_ONCE(i >= ARRAY_SIZE(dev_priv->gt_pm.rc6.cur_residency)))
  8000. return 0;
  8001. fw_domains = intel_uncore_forcewake_for_reg(dev_priv, reg, FW_REG_READ);
  8002. spin_lock_irqsave(&dev_priv->uncore.lock, flags);
  8003. intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
  8004. /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
  8005. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  8006. mul = 1000000;
  8007. div = dev_priv->czclk_freq;
  8008. overflow_hw = BIT_ULL(40);
  8009. time_hw = vlv_residency_raw(dev_priv, reg);
  8010. } else {
  8011. /* 833.33ns units on Gen9LP, 1.28us elsewhere. */
  8012. if (IS_GEN9_LP(dev_priv)) {
  8013. mul = 10000;
  8014. div = 12;
  8015. } else {
  8016. mul = 1280;
  8017. div = 1;
  8018. }
  8019. overflow_hw = BIT_ULL(32);
  8020. time_hw = I915_READ_FW(reg);
  8021. }
  8022. /*
  8023. * Counter wrap handling.
  8024. *
  8025. * But relying on a sufficient frequency of queries otherwise counters
  8026. * can still wrap.
  8027. */
  8028. prev_hw = dev_priv->gt_pm.rc6.prev_hw_residency[i];
  8029. dev_priv->gt_pm.rc6.prev_hw_residency[i] = time_hw;
  8030. /* RC6 delta from last sample. */
  8031. if (time_hw >= prev_hw)
  8032. time_hw -= prev_hw;
  8033. else
  8034. time_hw += overflow_hw - prev_hw;
  8035. /* Add delta to RC6 extended raw driver copy. */
  8036. time_hw += dev_priv->gt_pm.rc6.cur_residency[i];
  8037. dev_priv->gt_pm.rc6.cur_residency[i] = time_hw;
  8038. intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
  8039. spin_unlock_irqrestore(&dev_priv->uncore.lock, flags);
  8040. return mul_u64_u32_div(time_hw, mul, div);
  8041. }
  8042. u32 intel_get_cagf(struct drm_i915_private *dev_priv, u32 rpstat)
  8043. {
  8044. u32 cagf;
  8045. if (INTEL_GEN(dev_priv) >= 9)
  8046. cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
  8047. else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  8048. cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
  8049. else
  8050. cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
  8051. return cagf;
  8052. }