intel_pm.c 225 KB

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