intel_pm.c 221 KB

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