intel_pm.c 240 KB

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