intel_pm.c 280 KB

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