intel_pm.c 194 KB

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