intel_pm.c 220 KB

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