intel_pm.c 240 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471
  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 drm_device *dev = state->dev;
  3635. struct skl_pipe_wm *pipe_wm;
  3636. bool changed = false;
  3637. int ret, i;
  3638. /*
  3639. * When we distrust bios wm we always need to recompute to set the
  3640. * expected DDB allocations for each CRTC.
  3641. */
  3642. if (to_i915(dev)->wm.distrust_bios_wm)
  3643. changed = true;
  3644. /*
  3645. * If this transaction isn't actually touching any CRTC's, don't
  3646. * bother with watermark calculation. Note that if we pass this
  3647. * test, we're guaranteed to hold at least one CRTC state mutex,
  3648. * which means we can safely use values like dev_priv->active_crtcs
  3649. * since any racing commits that want to update them would need to
  3650. * hold _all_ CRTC state mutexes.
  3651. */
  3652. for_each_new_crtc_in_state(state, crtc, cstate, i)
  3653. changed = true;
  3654. if (!changed)
  3655. return 0;
  3656. /* Clear all dirty flags */
  3657. results->dirty_pipes = 0;
  3658. ret = skl_compute_ddb(state);
  3659. if (ret)
  3660. return ret;
  3661. /*
  3662. * Calculate WM's for all pipes that are part of this transaction.
  3663. * Note that the DDB allocation above may have added more CRTC's that
  3664. * weren't otherwise being modified (and set bits in dirty_pipes) if
  3665. * pipe allocations had to change.
  3666. *
  3667. * FIXME: Now that we're doing this in the atomic check phase, we
  3668. * should allow skl_update_pipe_wm() to return failure in cases where
  3669. * no suitable watermark values can be found.
  3670. */
  3671. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  3672. struct intel_crtc_state *intel_cstate =
  3673. to_intel_crtc_state(cstate);
  3674. const struct skl_pipe_wm *old_pipe_wm =
  3675. &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
  3676. pipe_wm = &intel_cstate->wm.skl.optimal;
  3677. ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
  3678. &results->ddb, &changed);
  3679. if (ret)
  3680. return ret;
  3681. if (changed)
  3682. results->dirty_pipes |= drm_crtc_mask(crtc);
  3683. if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
  3684. /* This pipe's WM's did not change */
  3685. continue;
  3686. intel_cstate->update_wm_pre = true;
  3687. }
  3688. skl_print_wm_changes(state);
  3689. return 0;
  3690. }
  3691. static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
  3692. struct intel_crtc_state *cstate)
  3693. {
  3694. struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
  3695. struct drm_i915_private *dev_priv = to_i915(state->base.dev);
  3696. struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
  3697. const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
  3698. enum pipe pipe = crtc->pipe;
  3699. enum plane_id plane_id;
  3700. if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
  3701. return;
  3702. I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
  3703. for_each_plane_id_on_crtc(crtc, plane_id) {
  3704. if (plane_id != PLANE_CURSOR)
  3705. skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
  3706. ddb, plane_id);
  3707. else
  3708. skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
  3709. ddb);
  3710. }
  3711. }
  3712. static void skl_initial_wm(struct intel_atomic_state *state,
  3713. struct intel_crtc_state *cstate)
  3714. {
  3715. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  3716. struct drm_device *dev = intel_crtc->base.dev;
  3717. struct drm_i915_private *dev_priv = to_i915(dev);
  3718. struct skl_wm_values *results = &state->wm_results;
  3719. struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
  3720. enum pipe pipe = intel_crtc->pipe;
  3721. if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
  3722. return;
  3723. mutex_lock(&dev_priv->wm.wm_mutex);
  3724. if (cstate->base.active_changed)
  3725. skl_atomic_update_crtc_wm(state, cstate);
  3726. skl_copy_wm_for_pipe(hw_vals, results, pipe);
  3727. mutex_unlock(&dev_priv->wm.wm_mutex);
  3728. }
  3729. static void ilk_compute_wm_config(struct drm_device *dev,
  3730. struct intel_wm_config *config)
  3731. {
  3732. struct intel_crtc *crtc;
  3733. /* Compute the currently _active_ config */
  3734. for_each_intel_crtc(dev, crtc) {
  3735. const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
  3736. if (!wm->pipe_enabled)
  3737. continue;
  3738. config->sprites_enabled |= wm->sprites_enabled;
  3739. config->sprites_scaled |= wm->sprites_scaled;
  3740. config->num_pipes_active++;
  3741. }
  3742. }
  3743. static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
  3744. {
  3745. struct drm_device *dev = &dev_priv->drm;
  3746. struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
  3747. struct ilk_wm_maximums max;
  3748. struct intel_wm_config config = {};
  3749. struct ilk_wm_values results = {};
  3750. enum intel_ddb_partitioning partitioning;
  3751. ilk_compute_wm_config(dev, &config);
  3752. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
  3753. ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
  3754. /* 5/6 split only in single pipe config on IVB+ */
  3755. if (INTEL_GEN(dev_priv) >= 7 &&
  3756. config.num_pipes_active == 1 && config.sprites_enabled) {
  3757. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
  3758. ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
  3759. best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
  3760. } else {
  3761. best_lp_wm = &lp_wm_1_2;
  3762. }
  3763. partitioning = (best_lp_wm == &lp_wm_1_2) ?
  3764. INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
  3765. ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
  3766. ilk_write_wm_values(dev_priv, &results);
  3767. }
  3768. static void ilk_initial_watermarks(struct intel_atomic_state *state,
  3769. struct intel_crtc_state *cstate)
  3770. {
  3771. struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
  3772. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  3773. mutex_lock(&dev_priv->wm.wm_mutex);
  3774. intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
  3775. ilk_program_watermarks(dev_priv);
  3776. mutex_unlock(&dev_priv->wm.wm_mutex);
  3777. }
  3778. static void ilk_optimize_watermarks(struct intel_atomic_state *state,
  3779. struct intel_crtc_state *cstate)
  3780. {
  3781. struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
  3782. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  3783. mutex_lock(&dev_priv->wm.wm_mutex);
  3784. if (cstate->wm.need_postvbl_update) {
  3785. intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
  3786. ilk_program_watermarks(dev_priv);
  3787. }
  3788. mutex_unlock(&dev_priv->wm.wm_mutex);
  3789. }
  3790. static inline void skl_wm_level_from_reg_val(uint32_t val,
  3791. struct skl_wm_level *level)
  3792. {
  3793. level->plane_en = val & PLANE_WM_EN;
  3794. level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
  3795. level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
  3796. PLANE_WM_LINES_MASK;
  3797. }
  3798. void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
  3799. struct skl_pipe_wm *out)
  3800. {
  3801. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3802. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3803. enum pipe pipe = intel_crtc->pipe;
  3804. int level, max_level;
  3805. enum plane_id plane_id;
  3806. uint32_t val;
  3807. max_level = ilk_wm_max_level(dev_priv);
  3808. for_each_plane_id_on_crtc(intel_crtc, plane_id) {
  3809. struct skl_plane_wm *wm = &out->planes[plane_id];
  3810. for (level = 0; level <= max_level; level++) {
  3811. if (plane_id != PLANE_CURSOR)
  3812. val = I915_READ(PLANE_WM(pipe, plane_id, level));
  3813. else
  3814. val = I915_READ(CUR_WM(pipe, level));
  3815. skl_wm_level_from_reg_val(val, &wm->wm[level]);
  3816. }
  3817. if (plane_id != PLANE_CURSOR)
  3818. val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
  3819. else
  3820. val = I915_READ(CUR_WM_TRANS(pipe));
  3821. skl_wm_level_from_reg_val(val, &wm->trans_wm);
  3822. }
  3823. if (!intel_crtc->active)
  3824. return;
  3825. out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
  3826. }
  3827. void skl_wm_get_hw_state(struct drm_device *dev)
  3828. {
  3829. struct drm_i915_private *dev_priv = to_i915(dev);
  3830. struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
  3831. struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
  3832. struct drm_crtc *crtc;
  3833. struct intel_crtc *intel_crtc;
  3834. struct intel_crtc_state *cstate;
  3835. skl_ddb_get_hw_state(dev_priv, ddb);
  3836. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3837. intel_crtc = to_intel_crtc(crtc);
  3838. cstate = to_intel_crtc_state(crtc->state);
  3839. skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
  3840. if (intel_crtc->active)
  3841. hw->dirty_pipes |= drm_crtc_mask(crtc);
  3842. }
  3843. if (dev_priv->active_crtcs) {
  3844. /* Fully recompute DDB on first atomic commit */
  3845. dev_priv->wm.distrust_bios_wm = true;
  3846. } else {
  3847. /* Easy/common case; just sanitize DDB now if everything off */
  3848. memset(ddb, 0, sizeof(*ddb));
  3849. }
  3850. }
  3851. static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
  3852. {
  3853. struct drm_device *dev = crtc->dev;
  3854. struct drm_i915_private *dev_priv = to_i915(dev);
  3855. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  3856. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3857. struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
  3858. struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
  3859. enum pipe pipe = intel_crtc->pipe;
  3860. static const i915_reg_t wm0_pipe_reg[] = {
  3861. [PIPE_A] = WM0_PIPEA_ILK,
  3862. [PIPE_B] = WM0_PIPEB_ILK,
  3863. [PIPE_C] = WM0_PIPEC_IVB,
  3864. };
  3865. hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
  3866. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  3867. hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
  3868. memset(active, 0, sizeof(*active));
  3869. active->pipe_enabled = intel_crtc->active;
  3870. if (active->pipe_enabled) {
  3871. u32 tmp = hw->wm_pipe[pipe];
  3872. /*
  3873. * For active pipes LP0 watermark is marked as
  3874. * enabled, and LP1+ watermaks as disabled since
  3875. * we can't really reverse compute them in case
  3876. * multiple pipes are active.
  3877. */
  3878. active->wm[0].enable = true;
  3879. active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
  3880. active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
  3881. active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
  3882. active->linetime = hw->wm_linetime[pipe];
  3883. } else {
  3884. int level, max_level = ilk_wm_max_level(dev_priv);
  3885. /*
  3886. * For inactive pipes, all watermark levels
  3887. * should be marked as enabled but zeroed,
  3888. * which is what we'd compute them to.
  3889. */
  3890. for (level = 0; level <= max_level; level++)
  3891. active->wm[level].enable = true;
  3892. }
  3893. intel_crtc->wm.active.ilk = *active;
  3894. }
  3895. #define _FW_WM(value, plane) \
  3896. (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
  3897. #define _FW_WM_VLV(value, plane) \
  3898. (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
  3899. static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
  3900. struct vlv_wm_values *wm)
  3901. {
  3902. enum pipe pipe;
  3903. uint32_t tmp;
  3904. for_each_pipe(dev_priv, pipe) {
  3905. tmp = I915_READ(VLV_DDL(pipe));
  3906. wm->ddl[pipe].plane[PLANE_PRIMARY] =
  3907. (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  3908. wm->ddl[pipe].plane[PLANE_CURSOR] =
  3909. (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  3910. wm->ddl[pipe].plane[PLANE_SPRITE0] =
  3911. (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  3912. wm->ddl[pipe].plane[PLANE_SPRITE1] =
  3913. (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  3914. }
  3915. tmp = I915_READ(DSPFW1);
  3916. wm->sr.plane = _FW_WM(tmp, SR);
  3917. wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
  3918. wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
  3919. wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
  3920. tmp = I915_READ(DSPFW2);
  3921. wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
  3922. wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
  3923. wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
  3924. tmp = I915_READ(DSPFW3);
  3925. wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
  3926. if (IS_CHERRYVIEW(dev_priv)) {
  3927. tmp = I915_READ(DSPFW7_CHV);
  3928. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
  3929. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
  3930. tmp = I915_READ(DSPFW8_CHV);
  3931. wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
  3932. wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
  3933. tmp = I915_READ(DSPFW9_CHV);
  3934. wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
  3935. wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
  3936. tmp = I915_READ(DSPHOWM);
  3937. wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
  3938. wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
  3939. wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
  3940. wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
  3941. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
  3942. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
  3943. wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
  3944. wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
  3945. wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
  3946. wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
  3947. } else {
  3948. tmp = I915_READ(DSPFW7);
  3949. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
  3950. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
  3951. tmp = I915_READ(DSPHOWM);
  3952. wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
  3953. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
  3954. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
  3955. wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
  3956. wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
  3957. wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
  3958. wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
  3959. }
  3960. }
  3961. #undef _FW_WM
  3962. #undef _FW_WM_VLV
  3963. void vlv_wm_get_hw_state(struct drm_device *dev)
  3964. {
  3965. struct drm_i915_private *dev_priv = to_i915(dev);
  3966. struct vlv_wm_values *wm = &dev_priv->wm.vlv;
  3967. struct intel_crtc *crtc;
  3968. u32 val;
  3969. vlv_read_wm_values(dev_priv, wm);
  3970. wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
  3971. wm->level = VLV_WM_LEVEL_PM2;
  3972. if (IS_CHERRYVIEW(dev_priv)) {
  3973. mutex_lock(&dev_priv->rps.hw_lock);
  3974. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3975. if (val & DSP_MAXFIFO_PM5_ENABLE)
  3976. wm->level = VLV_WM_LEVEL_PM5;
  3977. /*
  3978. * If DDR DVFS is disabled in the BIOS, Punit
  3979. * will never ack the request. So if that happens
  3980. * assume we don't have to enable/disable DDR DVFS
  3981. * dynamically. To test that just set the REQ_ACK
  3982. * bit to poke the Punit, but don't change the
  3983. * HIGH/LOW bits so that we don't actually change
  3984. * the current state.
  3985. */
  3986. val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
  3987. val |= FORCE_DDR_FREQ_REQ_ACK;
  3988. vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
  3989. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
  3990. FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
  3991. DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
  3992. "assuming DDR DVFS is disabled\n");
  3993. dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
  3994. } else {
  3995. val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
  3996. if ((val & FORCE_DDR_HIGH_FREQ) == 0)
  3997. wm->level = VLV_WM_LEVEL_DDR_DVFS;
  3998. }
  3999. mutex_unlock(&dev_priv->rps.hw_lock);
  4000. }
  4001. for_each_intel_crtc(dev, crtc) {
  4002. struct intel_crtc_state *crtc_state =
  4003. to_intel_crtc_state(crtc->base.state);
  4004. struct vlv_wm_state *active = &crtc->wm.active.vlv;
  4005. const struct vlv_fifo_state *fifo_state =
  4006. &crtc_state->wm.vlv.fifo_state;
  4007. enum pipe pipe = crtc->pipe;
  4008. enum plane_id plane_id;
  4009. int level;
  4010. vlv_get_fifo_size(crtc_state);
  4011. active->num_levels = wm->level + 1;
  4012. active->cxsr = wm->cxsr;
  4013. for (level = 0; level < active->num_levels; level++) {
  4014. struct vlv_pipe_wm *raw =
  4015. &crtc_state->wm.vlv.raw[level];
  4016. active->sr[level].plane = wm->sr.plane;
  4017. active->sr[level].cursor = wm->sr.cursor;
  4018. for_each_plane_id_on_crtc(crtc, plane_id) {
  4019. active->wm[level].plane[plane_id] =
  4020. wm->pipe[pipe].plane[plane_id];
  4021. raw->plane[plane_id] =
  4022. vlv_invert_wm_value(active->wm[level].plane[plane_id],
  4023. fifo_state->plane[plane_id]);
  4024. }
  4025. }
  4026. for_each_plane_id_on_crtc(crtc, plane_id)
  4027. vlv_raw_plane_wm_set(crtc_state, level,
  4028. plane_id, USHRT_MAX);
  4029. vlv_invalidate_wms(crtc, active, level);
  4030. crtc_state->wm.vlv.optimal = *active;
  4031. crtc_state->wm.vlv.intermediate = *active;
  4032. DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
  4033. pipe_name(pipe),
  4034. wm->pipe[pipe].plane[PLANE_PRIMARY],
  4035. wm->pipe[pipe].plane[PLANE_CURSOR],
  4036. wm->pipe[pipe].plane[PLANE_SPRITE0],
  4037. wm->pipe[pipe].plane[PLANE_SPRITE1]);
  4038. }
  4039. DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
  4040. wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
  4041. }
  4042. void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
  4043. {
  4044. struct intel_plane *plane;
  4045. struct intel_crtc *crtc;
  4046. mutex_lock(&dev_priv->wm.wm_mutex);
  4047. for_each_intel_plane(&dev_priv->drm, plane) {
  4048. struct intel_crtc *crtc =
  4049. intel_get_crtc_for_pipe(dev_priv, plane->pipe);
  4050. struct intel_crtc_state *crtc_state =
  4051. to_intel_crtc_state(crtc->base.state);
  4052. struct intel_plane_state *plane_state =
  4053. to_intel_plane_state(plane->base.state);
  4054. struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
  4055. const struct vlv_fifo_state *fifo_state =
  4056. &crtc_state->wm.vlv.fifo_state;
  4057. enum plane_id plane_id = plane->id;
  4058. int level;
  4059. if (plane_state->base.visible)
  4060. continue;
  4061. for (level = 0; level < wm_state->num_levels; level++) {
  4062. struct vlv_pipe_wm *raw =
  4063. &crtc_state->wm.vlv.raw[level];
  4064. raw->plane[plane_id] = 0;
  4065. wm_state->wm[level].plane[plane_id] =
  4066. vlv_invert_wm_value(raw->plane[plane_id],
  4067. fifo_state->plane[plane_id]);
  4068. }
  4069. }
  4070. for_each_intel_crtc(&dev_priv->drm, crtc) {
  4071. struct intel_crtc_state *crtc_state =
  4072. to_intel_crtc_state(crtc->base.state);
  4073. crtc_state->wm.vlv.intermediate =
  4074. crtc_state->wm.vlv.optimal;
  4075. crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
  4076. }
  4077. vlv_program_watermarks(dev_priv);
  4078. mutex_unlock(&dev_priv->wm.wm_mutex);
  4079. }
  4080. void ilk_wm_get_hw_state(struct drm_device *dev)
  4081. {
  4082. struct drm_i915_private *dev_priv = to_i915(dev);
  4083. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  4084. struct drm_crtc *crtc;
  4085. for_each_crtc(dev, crtc)
  4086. ilk_pipe_wm_get_hw_state(crtc);
  4087. hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
  4088. hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
  4089. hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
  4090. hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
  4091. if (INTEL_GEN(dev_priv) >= 7) {
  4092. hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
  4093. hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
  4094. }
  4095. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4096. hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
  4097. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  4098. else if (IS_IVYBRIDGE(dev_priv))
  4099. hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
  4100. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  4101. hw->enable_fbc_wm =
  4102. !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
  4103. }
  4104. /**
  4105. * intel_update_watermarks - update FIFO watermark values based on current modes
  4106. *
  4107. * Calculate watermark values for the various WM regs based on current mode
  4108. * and plane configuration.
  4109. *
  4110. * There are several cases to deal with here:
  4111. * - normal (i.e. non-self-refresh)
  4112. * - self-refresh (SR) mode
  4113. * - lines are large relative to FIFO size (buffer can hold up to 2)
  4114. * - lines are small relative to FIFO size (buffer can hold more than 2
  4115. * lines), so need to account for TLB latency
  4116. *
  4117. * The normal calculation is:
  4118. * watermark = dotclock * bytes per pixel * latency
  4119. * where latency is platform & configuration dependent (we assume pessimal
  4120. * values here).
  4121. *
  4122. * The SR calculation is:
  4123. * watermark = (trunc(latency/line time)+1) * surface width *
  4124. * bytes per pixel
  4125. * where
  4126. * line time = htotal / dotclock
  4127. * surface width = hdisplay for normal plane and 64 for cursor
  4128. * and latency is assumed to be high, as above.
  4129. *
  4130. * The final value programmed to the register should always be rounded up,
  4131. * and include an extra 2 entries to account for clock crossings.
  4132. *
  4133. * We don't use the sprite, so we can ignore that. And on Crestline we have
  4134. * to set the non-SR watermarks to 8.
  4135. */
  4136. void intel_update_watermarks(struct intel_crtc *crtc)
  4137. {
  4138. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4139. if (dev_priv->display.update_wm)
  4140. dev_priv->display.update_wm(crtc);
  4141. }
  4142. /*
  4143. * Lock protecting IPS related data structures
  4144. */
  4145. DEFINE_SPINLOCK(mchdev_lock);
  4146. /* Global for IPS driver to get at the current i915 device. Protected by
  4147. * mchdev_lock. */
  4148. static struct drm_i915_private *i915_mch_dev;
  4149. bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
  4150. {
  4151. u16 rgvswctl;
  4152. lockdep_assert_held(&mchdev_lock);
  4153. rgvswctl = I915_READ16(MEMSWCTL);
  4154. if (rgvswctl & MEMCTL_CMD_STS) {
  4155. DRM_DEBUG("gpu busy, RCS change rejected\n");
  4156. return false; /* still busy with another command */
  4157. }
  4158. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  4159. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  4160. I915_WRITE16(MEMSWCTL, rgvswctl);
  4161. POSTING_READ16(MEMSWCTL);
  4162. rgvswctl |= MEMCTL_CMD_STS;
  4163. I915_WRITE16(MEMSWCTL, rgvswctl);
  4164. return true;
  4165. }
  4166. static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
  4167. {
  4168. u32 rgvmodectl;
  4169. u8 fmax, fmin, fstart, vstart;
  4170. spin_lock_irq(&mchdev_lock);
  4171. rgvmodectl = I915_READ(MEMMODECTL);
  4172. /* Enable temp reporting */
  4173. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  4174. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  4175. /* 100ms RC evaluation intervals */
  4176. I915_WRITE(RCUPEI, 100000);
  4177. I915_WRITE(RCDNEI, 100000);
  4178. /* Set max/min thresholds to 90ms and 80ms respectively */
  4179. I915_WRITE(RCBMAXAVG, 90000);
  4180. I915_WRITE(RCBMINAVG, 80000);
  4181. I915_WRITE(MEMIHYST, 1);
  4182. /* Set up min, max, and cur for interrupt handling */
  4183. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  4184. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  4185. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  4186. MEMMODE_FSTART_SHIFT;
  4187. vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
  4188. PXVFREQ_PX_SHIFT;
  4189. dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
  4190. dev_priv->ips.fstart = fstart;
  4191. dev_priv->ips.max_delay = fstart;
  4192. dev_priv->ips.min_delay = fmin;
  4193. dev_priv->ips.cur_delay = fstart;
  4194. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  4195. fmax, fmin, fstart);
  4196. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  4197. /*
  4198. * Interrupts will be enabled in ironlake_irq_postinstall
  4199. */
  4200. I915_WRITE(VIDSTART, vstart);
  4201. POSTING_READ(VIDSTART);
  4202. rgvmodectl |= MEMMODE_SWMODE_EN;
  4203. I915_WRITE(MEMMODECTL, rgvmodectl);
  4204. if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  4205. DRM_ERROR("stuck trying to change perf mode\n");
  4206. mdelay(1);
  4207. ironlake_set_drps(dev_priv, fstart);
  4208. dev_priv->ips.last_count1 = I915_READ(DMIEC) +
  4209. I915_READ(DDREC) + I915_READ(CSIEC);
  4210. dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
  4211. dev_priv->ips.last_count2 = I915_READ(GFXEC);
  4212. dev_priv->ips.last_time2 = ktime_get_raw_ns();
  4213. spin_unlock_irq(&mchdev_lock);
  4214. }
  4215. static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
  4216. {
  4217. u16 rgvswctl;
  4218. spin_lock_irq(&mchdev_lock);
  4219. rgvswctl = I915_READ16(MEMSWCTL);
  4220. /* Ack interrupts, disable EFC interrupt */
  4221. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  4222. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  4223. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  4224. I915_WRITE(DEIIR, DE_PCU_EVENT);
  4225. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  4226. /* Go back to the starting frequency */
  4227. ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
  4228. mdelay(1);
  4229. rgvswctl |= MEMCTL_CMD_STS;
  4230. I915_WRITE(MEMSWCTL, rgvswctl);
  4231. mdelay(1);
  4232. spin_unlock_irq(&mchdev_lock);
  4233. }
  4234. /* There's a funny hw issue where the hw returns all 0 when reading from
  4235. * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
  4236. * ourselves, instead of doing a rmw cycle (which might result in us clearing
  4237. * all limits and the gpu stuck at whatever frequency it is at atm).
  4238. */
  4239. static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
  4240. {
  4241. u32 limits;
  4242. /* Only set the down limit when we've reached the lowest level to avoid
  4243. * getting more interrupts, otherwise leave this clear. This prevents a
  4244. * race in the hw when coming out of rc6: There's a tiny window where
  4245. * the hw runs at the minimal clock before selecting the desired
  4246. * frequency, if the down threshold expires in that window we will not
  4247. * receive a down interrupt. */
  4248. if (IS_GEN9(dev_priv)) {
  4249. limits = (dev_priv->rps.max_freq_softlimit) << 23;
  4250. if (val <= dev_priv->rps.min_freq_softlimit)
  4251. limits |= (dev_priv->rps.min_freq_softlimit) << 14;
  4252. } else {
  4253. limits = dev_priv->rps.max_freq_softlimit << 24;
  4254. if (val <= dev_priv->rps.min_freq_softlimit)
  4255. limits |= dev_priv->rps.min_freq_softlimit << 16;
  4256. }
  4257. return limits;
  4258. }
  4259. static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
  4260. {
  4261. int new_power;
  4262. u32 threshold_up = 0, threshold_down = 0; /* in % */
  4263. u32 ei_up = 0, ei_down = 0;
  4264. new_power = dev_priv->rps.power;
  4265. switch (dev_priv->rps.power) {
  4266. case LOW_POWER:
  4267. if (val > dev_priv->rps.efficient_freq + 1 &&
  4268. val > dev_priv->rps.cur_freq)
  4269. new_power = BETWEEN;
  4270. break;
  4271. case BETWEEN:
  4272. if (val <= dev_priv->rps.efficient_freq &&
  4273. val < dev_priv->rps.cur_freq)
  4274. new_power = LOW_POWER;
  4275. else if (val >= dev_priv->rps.rp0_freq &&
  4276. val > dev_priv->rps.cur_freq)
  4277. new_power = HIGH_POWER;
  4278. break;
  4279. case HIGH_POWER:
  4280. if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
  4281. val < dev_priv->rps.cur_freq)
  4282. new_power = BETWEEN;
  4283. break;
  4284. }
  4285. /* Max/min bins are special */
  4286. if (val <= dev_priv->rps.min_freq_softlimit)
  4287. new_power = LOW_POWER;
  4288. if (val >= dev_priv->rps.max_freq_softlimit)
  4289. new_power = HIGH_POWER;
  4290. if (new_power == dev_priv->rps.power)
  4291. return;
  4292. /* Note the units here are not exactly 1us, but 1280ns. */
  4293. switch (new_power) {
  4294. case LOW_POWER:
  4295. /* Upclock if more than 95% busy over 16ms */
  4296. ei_up = 16000;
  4297. threshold_up = 95;
  4298. /* Downclock if less than 85% busy over 32ms */
  4299. ei_down = 32000;
  4300. threshold_down = 85;
  4301. break;
  4302. case BETWEEN:
  4303. /* Upclock if more than 90% busy over 13ms */
  4304. ei_up = 13000;
  4305. threshold_up = 90;
  4306. /* Downclock if less than 75% busy over 32ms */
  4307. ei_down = 32000;
  4308. threshold_down = 75;
  4309. break;
  4310. case HIGH_POWER:
  4311. /* Upclock if more than 85% busy over 10ms */
  4312. ei_up = 10000;
  4313. threshold_up = 85;
  4314. /* Downclock if less than 60% busy over 32ms */
  4315. ei_down = 32000;
  4316. threshold_down = 60;
  4317. break;
  4318. }
  4319. /* When byt can survive without system hang with dynamic
  4320. * sw freq adjustments, this restriction can be lifted.
  4321. */
  4322. if (IS_VALLEYVIEW(dev_priv))
  4323. goto skip_hw_write;
  4324. I915_WRITE(GEN6_RP_UP_EI,
  4325. GT_INTERVAL_FROM_US(dev_priv, ei_up));
  4326. I915_WRITE(GEN6_RP_UP_THRESHOLD,
  4327. GT_INTERVAL_FROM_US(dev_priv,
  4328. ei_up * threshold_up / 100));
  4329. I915_WRITE(GEN6_RP_DOWN_EI,
  4330. GT_INTERVAL_FROM_US(dev_priv, ei_down));
  4331. I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
  4332. GT_INTERVAL_FROM_US(dev_priv,
  4333. ei_down * threshold_down / 100));
  4334. I915_WRITE(GEN6_RP_CONTROL,
  4335. GEN6_RP_MEDIA_TURBO |
  4336. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  4337. GEN6_RP_MEDIA_IS_GFX |
  4338. GEN6_RP_ENABLE |
  4339. GEN6_RP_UP_BUSY_AVG |
  4340. GEN6_RP_DOWN_IDLE_AVG);
  4341. skip_hw_write:
  4342. dev_priv->rps.power = new_power;
  4343. dev_priv->rps.up_threshold = threshold_up;
  4344. dev_priv->rps.down_threshold = threshold_down;
  4345. dev_priv->rps.last_adj = 0;
  4346. }
  4347. static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
  4348. {
  4349. u32 mask = 0;
  4350. /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
  4351. if (val > dev_priv->rps.min_freq_softlimit)
  4352. mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
  4353. if (val < dev_priv->rps.max_freq_softlimit)
  4354. mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
  4355. mask &= dev_priv->pm_rps_events;
  4356. return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
  4357. }
  4358. /* gen6_set_rps is called to update the frequency request, but should also be
  4359. * called when the range (min_delay and max_delay) is modified so that we can
  4360. * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
  4361. static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
  4362. {
  4363. /* min/max delay may still have been modified so be sure to
  4364. * write the limits value.
  4365. */
  4366. if (val != dev_priv->rps.cur_freq) {
  4367. gen6_set_rps_thresholds(dev_priv, val);
  4368. if (IS_GEN9(dev_priv))
  4369. I915_WRITE(GEN6_RPNSWREQ,
  4370. GEN9_FREQUENCY(val));
  4371. else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4372. I915_WRITE(GEN6_RPNSWREQ,
  4373. HSW_FREQUENCY(val));
  4374. else
  4375. I915_WRITE(GEN6_RPNSWREQ,
  4376. GEN6_FREQUENCY(val) |
  4377. GEN6_OFFSET(0) |
  4378. GEN6_AGGRESSIVE_TURBO);
  4379. }
  4380. /* Make sure we continue to get interrupts
  4381. * until we hit the minimum or maximum frequencies.
  4382. */
  4383. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
  4384. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  4385. dev_priv->rps.cur_freq = val;
  4386. trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
  4387. return 0;
  4388. }
  4389. static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
  4390. {
  4391. int err;
  4392. if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
  4393. "Odd GPU freq value\n"))
  4394. val &= ~1;
  4395. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  4396. if (val != dev_priv->rps.cur_freq) {
  4397. err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
  4398. if (err)
  4399. return err;
  4400. gen6_set_rps_thresholds(dev_priv, val);
  4401. }
  4402. dev_priv->rps.cur_freq = val;
  4403. trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
  4404. return 0;
  4405. }
  4406. /* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
  4407. *
  4408. * * If Gfx is Idle, then
  4409. * 1. Forcewake Media well.
  4410. * 2. Request idle freq.
  4411. * 3. Release Forcewake of Media well.
  4412. */
  4413. static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
  4414. {
  4415. u32 val = dev_priv->rps.idle_freq;
  4416. int err;
  4417. if (dev_priv->rps.cur_freq <= val)
  4418. return;
  4419. /* The punit delays the write of the frequency and voltage until it
  4420. * determines the GPU is awake. During normal usage we don't want to
  4421. * waste power changing the frequency if the GPU is sleeping (rc6).
  4422. * However, the GPU and driver is now idle and we do not want to delay
  4423. * switching to minimum voltage (reducing power whilst idle) as we do
  4424. * not expect to be woken in the near future and so must flush the
  4425. * change by waking the device.
  4426. *
  4427. * We choose to take the media powerwell (either would do to trick the
  4428. * punit into committing the voltage change) as that takes a lot less
  4429. * power than the render powerwell.
  4430. */
  4431. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
  4432. err = valleyview_set_rps(dev_priv, val);
  4433. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
  4434. if (err)
  4435. DRM_ERROR("Failed to set RPS for idle\n");
  4436. }
  4437. void gen6_rps_busy(struct drm_i915_private *dev_priv)
  4438. {
  4439. mutex_lock(&dev_priv->rps.hw_lock);
  4440. if (dev_priv->rps.enabled) {
  4441. u8 freq;
  4442. if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
  4443. gen6_rps_reset_ei(dev_priv);
  4444. I915_WRITE(GEN6_PMINTRMSK,
  4445. gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
  4446. gen6_enable_rps_interrupts(dev_priv);
  4447. /* Use the user's desired frequency as a guide, but for better
  4448. * performance, jump directly to RPe as our starting frequency.
  4449. */
  4450. freq = max(dev_priv->rps.cur_freq,
  4451. dev_priv->rps.efficient_freq);
  4452. if (intel_set_rps(dev_priv,
  4453. clamp(freq,
  4454. dev_priv->rps.min_freq_softlimit,
  4455. dev_priv->rps.max_freq_softlimit)))
  4456. DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
  4457. }
  4458. mutex_unlock(&dev_priv->rps.hw_lock);
  4459. }
  4460. void gen6_rps_idle(struct drm_i915_private *dev_priv)
  4461. {
  4462. /* Flush our bottom-half so that it does not race with us
  4463. * setting the idle frequency and so that it is bounded by
  4464. * our rpm wakeref. And then disable the interrupts to stop any
  4465. * futher RPS reclocking whilst we are asleep.
  4466. */
  4467. gen6_disable_rps_interrupts(dev_priv);
  4468. mutex_lock(&dev_priv->rps.hw_lock);
  4469. if (dev_priv->rps.enabled) {
  4470. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4471. vlv_set_rps_idle(dev_priv);
  4472. else
  4473. gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
  4474. dev_priv->rps.last_adj = 0;
  4475. I915_WRITE(GEN6_PMINTRMSK,
  4476. gen6_sanitize_rps_pm_mask(dev_priv, ~0));
  4477. }
  4478. mutex_unlock(&dev_priv->rps.hw_lock);
  4479. spin_lock(&dev_priv->rps.client_lock);
  4480. while (!list_empty(&dev_priv->rps.clients))
  4481. list_del_init(dev_priv->rps.clients.next);
  4482. spin_unlock(&dev_priv->rps.client_lock);
  4483. }
  4484. void gen6_rps_boost(struct drm_i915_private *dev_priv,
  4485. struct intel_rps_client *rps,
  4486. unsigned long submitted)
  4487. {
  4488. /* This is intentionally racy! We peek at the state here, then
  4489. * validate inside the RPS worker.
  4490. */
  4491. if (!(dev_priv->gt.awake &&
  4492. dev_priv->rps.enabled &&
  4493. dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
  4494. return;
  4495. /* Force a RPS boost (and don't count it against the client) if
  4496. * the GPU is severely congested.
  4497. */
  4498. if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
  4499. rps = NULL;
  4500. spin_lock(&dev_priv->rps.client_lock);
  4501. if (rps == NULL || list_empty(&rps->link)) {
  4502. spin_lock_irq(&dev_priv->irq_lock);
  4503. if (dev_priv->rps.interrupts_enabled) {
  4504. dev_priv->rps.client_boost = true;
  4505. schedule_work(&dev_priv->rps.work);
  4506. }
  4507. spin_unlock_irq(&dev_priv->irq_lock);
  4508. if (rps != NULL) {
  4509. list_add(&rps->link, &dev_priv->rps.clients);
  4510. rps->boosts++;
  4511. } else
  4512. dev_priv->rps.boosts++;
  4513. }
  4514. spin_unlock(&dev_priv->rps.client_lock);
  4515. }
  4516. int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
  4517. {
  4518. int err;
  4519. lockdep_assert_held(&dev_priv->rps.hw_lock);
  4520. GEM_BUG_ON(val > dev_priv->rps.max_freq);
  4521. GEM_BUG_ON(val < dev_priv->rps.min_freq);
  4522. if (!dev_priv->rps.enabled) {
  4523. dev_priv->rps.cur_freq = val;
  4524. return 0;
  4525. }
  4526. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4527. err = valleyview_set_rps(dev_priv, val);
  4528. else
  4529. err = gen6_set_rps(dev_priv, val);
  4530. return err;
  4531. }
  4532. static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
  4533. {
  4534. I915_WRITE(GEN6_RC_CONTROL, 0);
  4535. I915_WRITE(GEN9_PG_ENABLE, 0);
  4536. }
  4537. static void gen9_disable_rps(struct drm_i915_private *dev_priv)
  4538. {
  4539. I915_WRITE(GEN6_RP_CONTROL, 0);
  4540. }
  4541. static void gen6_disable_rps(struct drm_i915_private *dev_priv)
  4542. {
  4543. I915_WRITE(GEN6_RC_CONTROL, 0);
  4544. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  4545. I915_WRITE(GEN6_RP_CONTROL, 0);
  4546. }
  4547. static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
  4548. {
  4549. I915_WRITE(GEN6_RC_CONTROL, 0);
  4550. }
  4551. static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
  4552. {
  4553. /* we're doing forcewake before Disabling RC6,
  4554. * This what the BIOS expects when going into suspend */
  4555. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4556. I915_WRITE(GEN6_RC_CONTROL, 0);
  4557. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4558. }
  4559. static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
  4560. {
  4561. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  4562. if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
  4563. mode = GEN6_RC_CTL_RC6_ENABLE;
  4564. else
  4565. mode = 0;
  4566. }
  4567. if (HAS_RC6p(dev_priv))
  4568. DRM_DEBUG_DRIVER("Enabling RC6 states: "
  4569. "RC6 %s RC6p %s RC6pp %s\n",
  4570. onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
  4571. onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
  4572. onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
  4573. else
  4574. DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
  4575. onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
  4576. }
  4577. static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
  4578. {
  4579. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  4580. bool enable_rc6 = true;
  4581. unsigned long rc6_ctx_base;
  4582. u32 rc_ctl;
  4583. int rc_sw_target;
  4584. rc_ctl = I915_READ(GEN6_RC_CONTROL);
  4585. rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
  4586. RC_SW_TARGET_STATE_SHIFT;
  4587. DRM_DEBUG_DRIVER("BIOS enabled RC states: "
  4588. "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
  4589. onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
  4590. onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
  4591. rc_sw_target);
  4592. if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
  4593. DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
  4594. enable_rc6 = false;
  4595. }
  4596. /*
  4597. * The exact context size is not known for BXT, so assume a page size
  4598. * for this check.
  4599. */
  4600. rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
  4601. if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
  4602. (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
  4603. ggtt->stolen_reserved_size))) {
  4604. DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
  4605. enable_rc6 = false;
  4606. }
  4607. if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
  4608. ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
  4609. ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
  4610. ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
  4611. DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
  4612. enable_rc6 = false;
  4613. }
  4614. if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
  4615. !I915_READ(GEN8_PUSHBUS_ENABLE) ||
  4616. !I915_READ(GEN8_PUSHBUS_SHIFT)) {
  4617. DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
  4618. enable_rc6 = false;
  4619. }
  4620. if (!I915_READ(GEN6_GFXPAUSE)) {
  4621. DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
  4622. enable_rc6 = false;
  4623. }
  4624. if (!I915_READ(GEN8_MISC_CTRL0)) {
  4625. DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
  4626. enable_rc6 = false;
  4627. }
  4628. return enable_rc6;
  4629. }
  4630. int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
  4631. {
  4632. /* No RC6 before Ironlake and code is gone for ilk. */
  4633. if (INTEL_INFO(dev_priv)->gen < 6)
  4634. return 0;
  4635. if (!enable_rc6)
  4636. return 0;
  4637. if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
  4638. DRM_INFO("RC6 disabled by BIOS\n");
  4639. return 0;
  4640. }
  4641. /* Respect the kernel parameter if it is set */
  4642. if (enable_rc6 >= 0) {
  4643. int mask;
  4644. if (HAS_RC6p(dev_priv))
  4645. mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
  4646. INTEL_RC6pp_ENABLE;
  4647. else
  4648. mask = INTEL_RC6_ENABLE;
  4649. if ((enable_rc6 & mask) != enable_rc6)
  4650. DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
  4651. "(requested %d, valid %d)\n",
  4652. enable_rc6 & mask, enable_rc6, mask);
  4653. return enable_rc6 & mask;
  4654. }
  4655. if (IS_IVYBRIDGE(dev_priv))
  4656. return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
  4657. return INTEL_RC6_ENABLE;
  4658. }
  4659. static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
  4660. {
  4661. /* All of these values are in units of 50MHz */
  4662. /* static values from HW: RP0 > RP1 > RPn (min_freq) */
  4663. if (IS_GEN9_LP(dev_priv)) {
  4664. u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
  4665. dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
  4666. dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
  4667. dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
  4668. } else {
  4669. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  4670. dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
  4671. dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
  4672. dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
  4673. }
  4674. /* hw_max = RP0 until we check for overclocking */
  4675. dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
  4676. dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
  4677. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
  4678. IS_GEN9_BC(dev_priv)) {
  4679. u32 ddcc_status = 0;
  4680. if (sandybridge_pcode_read(dev_priv,
  4681. HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
  4682. &ddcc_status) == 0)
  4683. dev_priv->rps.efficient_freq =
  4684. clamp_t(u8,
  4685. ((ddcc_status >> 8) & 0xff),
  4686. dev_priv->rps.min_freq,
  4687. dev_priv->rps.max_freq);
  4688. }
  4689. if (IS_GEN9_BC(dev_priv)) {
  4690. /* Store the frequency values in 16.66 MHZ units, which is
  4691. * the natural hardware unit for SKL
  4692. */
  4693. dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
  4694. dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
  4695. dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
  4696. dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
  4697. dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
  4698. }
  4699. }
  4700. static void reset_rps(struct drm_i915_private *dev_priv,
  4701. int (*set)(struct drm_i915_private *, u8))
  4702. {
  4703. u8 freq = dev_priv->rps.cur_freq;
  4704. /* force a reset */
  4705. dev_priv->rps.power = -1;
  4706. dev_priv->rps.cur_freq = -1;
  4707. if (set(dev_priv, freq))
  4708. DRM_ERROR("Failed to reset RPS to initial values\n");
  4709. }
  4710. /* See the Gen9_GT_PM_Programming_Guide doc for the below */
  4711. static void gen9_enable_rps(struct drm_i915_private *dev_priv)
  4712. {
  4713. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4714. /* Program defaults and thresholds for RPS*/
  4715. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  4716. GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
  4717. /* 1 second timeout*/
  4718. I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
  4719. GT_INTERVAL_FROM_US(dev_priv, 1000000));
  4720. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
  4721. /* Leaning on the below call to gen6_set_rps to program/setup the
  4722. * Up/Down EI & threshold registers, as well as the RP_CONTROL,
  4723. * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
  4724. reset_rps(dev_priv, gen6_set_rps);
  4725. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4726. }
  4727. static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
  4728. {
  4729. struct intel_engine_cs *engine;
  4730. enum intel_engine_id id;
  4731. uint32_t rc6_mask = 0;
  4732. /* 1a: Software RC state - RC0 */
  4733. I915_WRITE(GEN6_RC_STATE, 0);
  4734. /* 1b: Get forcewake during program sequence. Although the driver
  4735. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  4736. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4737. /* 2a: Disable RC states. */
  4738. I915_WRITE(GEN6_RC_CONTROL, 0);
  4739. /* 2b: Program RC6 thresholds.*/
  4740. /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
  4741. if (IS_SKYLAKE(dev_priv))
  4742. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
  4743. else
  4744. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
  4745. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  4746. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  4747. for_each_engine(engine, dev_priv, id)
  4748. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  4749. if (HAS_GUC(dev_priv))
  4750. I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
  4751. I915_WRITE(GEN6_RC_SLEEP, 0);
  4752. /* 2c: Program Coarse Power Gating Policies. */
  4753. I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
  4754. I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
  4755. /* 3a: Enable RC6 */
  4756. if (intel_enable_rc6() & INTEL_RC6_ENABLE)
  4757. rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
  4758. DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
  4759. I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
  4760. I915_WRITE(GEN6_RC_CONTROL,
  4761. GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
  4762. /*
  4763. * 3b: Enable Coarse Power Gating only when RC6 is enabled.
  4764. * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
  4765. */
  4766. if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
  4767. I915_WRITE(GEN9_PG_ENABLE, 0);
  4768. else
  4769. I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
  4770. (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
  4771. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4772. }
  4773. static void gen8_enable_rps(struct drm_i915_private *dev_priv)
  4774. {
  4775. struct intel_engine_cs *engine;
  4776. enum intel_engine_id id;
  4777. uint32_t rc6_mask = 0;
  4778. /* 1a: Software RC state - RC0 */
  4779. I915_WRITE(GEN6_RC_STATE, 0);
  4780. /* 1c & 1d: Get forcewake during program sequence. Although the driver
  4781. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  4782. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4783. /* 2a: Disable RC states. */
  4784. I915_WRITE(GEN6_RC_CONTROL, 0);
  4785. /* 2b: Program RC6 thresholds.*/
  4786. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  4787. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  4788. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  4789. for_each_engine(engine, dev_priv, id)
  4790. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  4791. I915_WRITE(GEN6_RC_SLEEP, 0);
  4792. if (IS_BROADWELL(dev_priv))
  4793. I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
  4794. else
  4795. I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
  4796. /* 3: Enable RC6 */
  4797. if (intel_enable_rc6() & INTEL_RC6_ENABLE)
  4798. rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
  4799. intel_print_rc6_info(dev_priv, rc6_mask);
  4800. if (IS_BROADWELL(dev_priv))
  4801. I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
  4802. GEN7_RC_CTL_TO_MODE |
  4803. rc6_mask);
  4804. else
  4805. I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
  4806. GEN6_RC_CTL_EI_MODE(1) |
  4807. rc6_mask);
  4808. /* 4 Program defaults and thresholds for RPS*/
  4809. I915_WRITE(GEN6_RPNSWREQ,
  4810. HSW_FREQUENCY(dev_priv->rps.rp1_freq));
  4811. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  4812. HSW_FREQUENCY(dev_priv->rps.rp1_freq));
  4813. /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
  4814. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
  4815. /* Docs recommend 900MHz, and 300 MHz respectively */
  4816. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  4817. dev_priv->rps.max_freq_softlimit << 24 |
  4818. dev_priv->rps.min_freq_softlimit << 16);
  4819. I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
  4820. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
  4821. I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
  4822. I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
  4823. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  4824. /* 5: Enable RPS */
  4825. I915_WRITE(GEN6_RP_CONTROL,
  4826. GEN6_RP_MEDIA_TURBO |
  4827. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  4828. GEN6_RP_MEDIA_IS_GFX |
  4829. GEN6_RP_ENABLE |
  4830. GEN6_RP_UP_BUSY_AVG |
  4831. GEN6_RP_DOWN_IDLE_AVG);
  4832. /* 6: Ring frequency + overclocking (our driver does this later */
  4833. reset_rps(dev_priv, gen6_set_rps);
  4834. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4835. }
  4836. static void gen6_enable_rps(struct drm_i915_private *dev_priv)
  4837. {
  4838. struct intel_engine_cs *engine;
  4839. enum intel_engine_id id;
  4840. u32 rc6vids, rc6_mask = 0;
  4841. u32 gtfifodbg;
  4842. int rc6_mode;
  4843. int ret;
  4844. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  4845. /* Here begins a magic sequence of register writes to enable
  4846. * auto-downclocking.
  4847. *
  4848. * Perhaps there might be some value in exposing these to
  4849. * userspace...
  4850. */
  4851. I915_WRITE(GEN6_RC_STATE, 0);
  4852. /* Clear the DBG now so we don't confuse earlier errors */
  4853. gtfifodbg = I915_READ(GTFIFODBG);
  4854. if (gtfifodbg) {
  4855. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  4856. I915_WRITE(GTFIFODBG, gtfifodbg);
  4857. }
  4858. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4859. /* disable the counters and set deterministic thresholds */
  4860. I915_WRITE(GEN6_RC_CONTROL, 0);
  4861. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  4862. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  4863. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  4864. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  4865. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  4866. for_each_engine(engine, dev_priv, id)
  4867. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  4868. I915_WRITE(GEN6_RC_SLEEP, 0);
  4869. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  4870. if (IS_IVYBRIDGE(dev_priv))
  4871. I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
  4872. else
  4873. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  4874. I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
  4875. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  4876. /* Check if we are enabling RC6 */
  4877. rc6_mode = intel_enable_rc6();
  4878. if (rc6_mode & INTEL_RC6_ENABLE)
  4879. rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
  4880. /* We don't use those on Haswell */
  4881. if (!IS_HASWELL(dev_priv)) {
  4882. if (rc6_mode & INTEL_RC6p_ENABLE)
  4883. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  4884. if (rc6_mode & INTEL_RC6pp_ENABLE)
  4885. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  4886. }
  4887. intel_print_rc6_info(dev_priv, rc6_mask);
  4888. I915_WRITE(GEN6_RC_CONTROL,
  4889. rc6_mask |
  4890. GEN6_RC_CTL_EI_MODE(1) |
  4891. GEN6_RC_CTL_HW_ENABLE);
  4892. /* Power down if completely idle for over 50ms */
  4893. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
  4894. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  4895. reset_rps(dev_priv, gen6_set_rps);
  4896. rc6vids = 0;
  4897. ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
  4898. if (IS_GEN6(dev_priv) && ret) {
  4899. DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
  4900. } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
  4901. DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
  4902. GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
  4903. rc6vids &= 0xffff00;
  4904. rc6vids |= GEN6_ENCODE_RC6_VID(450);
  4905. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
  4906. if (ret)
  4907. DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
  4908. }
  4909. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4910. }
  4911. static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
  4912. {
  4913. int min_freq = 15;
  4914. unsigned int gpu_freq;
  4915. unsigned int max_ia_freq, min_ring_freq;
  4916. unsigned int max_gpu_freq, min_gpu_freq;
  4917. int scaling_factor = 180;
  4918. struct cpufreq_policy *policy;
  4919. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  4920. policy = cpufreq_cpu_get(0);
  4921. if (policy) {
  4922. max_ia_freq = policy->cpuinfo.max_freq;
  4923. cpufreq_cpu_put(policy);
  4924. } else {
  4925. /*
  4926. * Default to measured freq if none found, PCU will ensure we
  4927. * don't go over
  4928. */
  4929. max_ia_freq = tsc_khz;
  4930. }
  4931. /* Convert from kHz to MHz */
  4932. max_ia_freq /= 1000;
  4933. min_ring_freq = I915_READ(DCLK) & 0xf;
  4934. /* convert DDR frequency from units of 266.6MHz to bandwidth */
  4935. min_ring_freq = mult_frac(min_ring_freq, 8, 3);
  4936. if (IS_GEN9_BC(dev_priv)) {
  4937. /* Convert GT frequency to 50 HZ units */
  4938. min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
  4939. max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
  4940. } else {
  4941. min_gpu_freq = dev_priv->rps.min_freq;
  4942. max_gpu_freq = dev_priv->rps.max_freq;
  4943. }
  4944. /*
  4945. * For each potential GPU frequency, load a ring frequency we'd like
  4946. * to use for memory access. We do this by specifying the IA frequency
  4947. * the PCU should use as a reference to determine the ring frequency.
  4948. */
  4949. for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
  4950. int diff = max_gpu_freq - gpu_freq;
  4951. unsigned int ia_freq = 0, ring_freq = 0;
  4952. if (IS_GEN9_BC(dev_priv)) {
  4953. /*
  4954. * ring_freq = 2 * GT. ring_freq is in 100MHz units
  4955. * No floor required for ring frequency on SKL.
  4956. */
  4957. ring_freq = gpu_freq;
  4958. } else if (INTEL_INFO(dev_priv)->gen >= 8) {
  4959. /* max(2 * GT, DDR). NB: GT is 50MHz units */
  4960. ring_freq = max(min_ring_freq, gpu_freq);
  4961. } else if (IS_HASWELL(dev_priv)) {
  4962. ring_freq = mult_frac(gpu_freq, 5, 4);
  4963. ring_freq = max(min_ring_freq, ring_freq);
  4964. /* leave ia_freq as the default, chosen by cpufreq */
  4965. } else {
  4966. /* On older processors, there is no separate ring
  4967. * clock domain, so in order to boost the bandwidth
  4968. * of the ring, we need to upclock the CPU (ia_freq).
  4969. *
  4970. * For GPU frequencies less than 750MHz,
  4971. * just use the lowest ring freq.
  4972. */
  4973. if (gpu_freq < min_freq)
  4974. ia_freq = 800;
  4975. else
  4976. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  4977. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  4978. }
  4979. sandybridge_pcode_write(dev_priv,
  4980. GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
  4981. ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
  4982. ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
  4983. gpu_freq);
  4984. }
  4985. }
  4986. static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
  4987. {
  4988. u32 val, rp0;
  4989. val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
  4990. switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
  4991. case 8:
  4992. /* (2 * 4) config */
  4993. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
  4994. break;
  4995. case 12:
  4996. /* (2 * 6) config */
  4997. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
  4998. break;
  4999. case 16:
  5000. /* (2 * 8) config */
  5001. default:
  5002. /* Setting (2 * 8) Min RP0 for any other combination */
  5003. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
  5004. break;
  5005. }
  5006. rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
  5007. return rp0;
  5008. }
  5009. static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  5010. {
  5011. u32 val, rpe;
  5012. val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
  5013. rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
  5014. return rpe;
  5015. }
  5016. static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
  5017. {
  5018. u32 val, rp1;
  5019. val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
  5020. rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
  5021. return rp1;
  5022. }
  5023. static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
  5024. {
  5025. u32 val, rpn;
  5026. val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
  5027. rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
  5028. FB_GFX_FREQ_FUSE_MASK);
  5029. return rpn;
  5030. }
  5031. static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
  5032. {
  5033. u32 val, rp1;
  5034. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  5035. rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
  5036. return rp1;
  5037. }
  5038. static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
  5039. {
  5040. u32 val, rp0;
  5041. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  5042. rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
  5043. /* Clamp to max */
  5044. rp0 = min_t(u32, rp0, 0xea);
  5045. return rp0;
  5046. }
  5047. static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  5048. {
  5049. u32 val, rpe;
  5050. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
  5051. rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
  5052. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
  5053. rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
  5054. return rpe;
  5055. }
  5056. static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
  5057. {
  5058. u32 val;
  5059. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
  5060. /*
  5061. * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
  5062. * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
  5063. * a BYT-M B0 the above register contains 0xbf. Moreover when setting
  5064. * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
  5065. * to make sure it matches what Punit accepts.
  5066. */
  5067. return max_t(u32, val, 0xc0);
  5068. }
  5069. /* Check that the pctx buffer wasn't move under us. */
  5070. static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
  5071. {
  5072. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  5073. WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
  5074. dev_priv->vlv_pctx->stolen->start);
  5075. }
  5076. /* Check that the pcbr address is not empty. */
  5077. static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
  5078. {
  5079. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  5080. WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
  5081. }
  5082. static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
  5083. {
  5084. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  5085. unsigned long pctx_paddr, paddr;
  5086. u32 pcbr;
  5087. int pctx_size = 32*1024;
  5088. pcbr = I915_READ(VLV_PCBR);
  5089. if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
  5090. DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
  5091. paddr = (dev_priv->mm.stolen_base +
  5092. (ggtt->stolen_size - pctx_size));
  5093. pctx_paddr = (paddr & (~4095));
  5094. I915_WRITE(VLV_PCBR, pctx_paddr);
  5095. }
  5096. DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
  5097. }
  5098. static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
  5099. {
  5100. struct drm_i915_gem_object *pctx;
  5101. unsigned long pctx_paddr;
  5102. u32 pcbr;
  5103. int pctx_size = 24*1024;
  5104. pcbr = I915_READ(VLV_PCBR);
  5105. if (pcbr) {
  5106. /* BIOS set it up already, grab the pre-alloc'd space */
  5107. int pcbr_offset;
  5108. pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
  5109. pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  5110. pcbr_offset,
  5111. I915_GTT_OFFSET_NONE,
  5112. pctx_size);
  5113. goto out;
  5114. }
  5115. DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
  5116. /*
  5117. * From the Gunit register HAS:
  5118. * The Gfx driver is expected to program this register and ensure
  5119. * proper allocation within Gfx stolen memory. For example, this
  5120. * register should be programmed such than the PCBR range does not
  5121. * overlap with other ranges, such as the frame buffer, protected
  5122. * memory, or any other relevant ranges.
  5123. */
  5124. pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
  5125. if (!pctx) {
  5126. DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
  5127. goto out;
  5128. }
  5129. pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
  5130. I915_WRITE(VLV_PCBR, pctx_paddr);
  5131. out:
  5132. DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
  5133. dev_priv->vlv_pctx = pctx;
  5134. }
  5135. static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
  5136. {
  5137. if (WARN_ON(!dev_priv->vlv_pctx))
  5138. return;
  5139. i915_gem_object_put(dev_priv->vlv_pctx);
  5140. dev_priv->vlv_pctx = NULL;
  5141. }
  5142. static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
  5143. {
  5144. dev_priv->rps.gpll_ref_freq =
  5145. vlv_get_cck_clock(dev_priv, "GPLL ref",
  5146. CCK_GPLL_CLOCK_CONTROL,
  5147. dev_priv->czclk_freq);
  5148. DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
  5149. dev_priv->rps.gpll_ref_freq);
  5150. }
  5151. static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
  5152. {
  5153. u32 val;
  5154. valleyview_setup_pctx(dev_priv);
  5155. vlv_init_gpll_ref_freq(dev_priv);
  5156. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  5157. switch ((val >> 6) & 3) {
  5158. case 0:
  5159. case 1:
  5160. dev_priv->mem_freq = 800;
  5161. break;
  5162. case 2:
  5163. dev_priv->mem_freq = 1066;
  5164. break;
  5165. case 3:
  5166. dev_priv->mem_freq = 1333;
  5167. break;
  5168. }
  5169. DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
  5170. dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
  5171. dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
  5172. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  5173. intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
  5174. dev_priv->rps.max_freq);
  5175. dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
  5176. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  5177. intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  5178. dev_priv->rps.efficient_freq);
  5179. dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
  5180. DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
  5181. intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
  5182. dev_priv->rps.rp1_freq);
  5183. dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
  5184. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  5185. intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
  5186. dev_priv->rps.min_freq);
  5187. }
  5188. static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
  5189. {
  5190. u32 val;
  5191. cherryview_setup_pctx(dev_priv);
  5192. vlv_init_gpll_ref_freq(dev_priv);
  5193. mutex_lock(&dev_priv->sb_lock);
  5194. val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
  5195. mutex_unlock(&dev_priv->sb_lock);
  5196. switch ((val >> 2) & 0x7) {
  5197. case 3:
  5198. dev_priv->mem_freq = 2000;
  5199. break;
  5200. default:
  5201. dev_priv->mem_freq = 1600;
  5202. break;
  5203. }
  5204. DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
  5205. dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
  5206. dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
  5207. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  5208. intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
  5209. dev_priv->rps.max_freq);
  5210. dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
  5211. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  5212. intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  5213. dev_priv->rps.efficient_freq);
  5214. dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
  5215. DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
  5216. intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
  5217. dev_priv->rps.rp1_freq);
  5218. dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
  5219. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  5220. intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
  5221. dev_priv->rps.min_freq);
  5222. WARN_ONCE((dev_priv->rps.max_freq |
  5223. dev_priv->rps.efficient_freq |
  5224. dev_priv->rps.rp1_freq |
  5225. dev_priv->rps.min_freq) & 1,
  5226. "Odd GPU freq values\n");
  5227. }
  5228. static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
  5229. {
  5230. valleyview_cleanup_pctx(dev_priv);
  5231. }
  5232. static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
  5233. {
  5234. struct intel_engine_cs *engine;
  5235. enum intel_engine_id id;
  5236. u32 gtfifodbg, val, rc6_mode = 0, pcbr;
  5237. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  5238. gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
  5239. GT_FIFO_FREE_ENTRIES_CHV);
  5240. if (gtfifodbg) {
  5241. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  5242. gtfifodbg);
  5243. I915_WRITE(GTFIFODBG, gtfifodbg);
  5244. }
  5245. cherryview_check_pctx(dev_priv);
  5246. /* 1a & 1b: Get forcewake during program sequence. Although the driver
  5247. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  5248. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5249. /* Disable RC states. */
  5250. I915_WRITE(GEN6_RC_CONTROL, 0);
  5251. /* 2a: Program RC6 thresholds.*/
  5252. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  5253. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  5254. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  5255. for_each_engine(engine, dev_priv, id)
  5256. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  5257. I915_WRITE(GEN6_RC_SLEEP, 0);
  5258. /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
  5259. I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
  5260. /* allows RC6 residency counter to work */
  5261. I915_WRITE(VLV_COUNTER_CONTROL,
  5262. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
  5263. VLV_MEDIA_RC6_COUNT_EN |
  5264. VLV_RENDER_RC6_COUNT_EN));
  5265. /* For now we assume BIOS is allocating and populating the PCBR */
  5266. pcbr = I915_READ(VLV_PCBR);
  5267. /* 3: Enable RC6 */
  5268. if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
  5269. (pcbr >> VLV_PCBR_ADDR_SHIFT))
  5270. rc6_mode = GEN7_RC_CTL_TO_MODE;
  5271. I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
  5272. /* 4 Program defaults and thresholds for RPS*/
  5273. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  5274. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  5275. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  5276. I915_WRITE(GEN6_RP_UP_EI, 66000);
  5277. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  5278. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  5279. /* 5: Enable RPS */
  5280. I915_WRITE(GEN6_RP_CONTROL,
  5281. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  5282. GEN6_RP_MEDIA_IS_GFX |
  5283. GEN6_RP_ENABLE |
  5284. GEN6_RP_UP_BUSY_AVG |
  5285. GEN6_RP_DOWN_IDLE_AVG);
  5286. /* Setting Fixed Bias */
  5287. val = VLV_OVERRIDE_EN |
  5288. VLV_SOC_TDP_EN |
  5289. CHV_BIAS_CPU_50_SOC_50;
  5290. vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
  5291. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  5292. /* RPS code assumes GPLL is used */
  5293. WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
  5294. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
  5295. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  5296. reset_rps(dev_priv, valleyview_set_rps);
  5297. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5298. }
  5299. static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
  5300. {
  5301. struct intel_engine_cs *engine;
  5302. enum intel_engine_id id;
  5303. u32 gtfifodbg, val, rc6_mode = 0;
  5304. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  5305. valleyview_check_pctx(dev_priv);
  5306. gtfifodbg = I915_READ(GTFIFODBG);
  5307. if (gtfifodbg) {
  5308. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  5309. gtfifodbg);
  5310. I915_WRITE(GTFIFODBG, gtfifodbg);
  5311. }
  5312. /* If VLV, Forcewake all wells, else re-direct to regular path */
  5313. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5314. /* Disable RC states. */
  5315. I915_WRITE(GEN6_RC_CONTROL, 0);
  5316. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  5317. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  5318. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  5319. I915_WRITE(GEN6_RP_UP_EI, 66000);
  5320. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  5321. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  5322. I915_WRITE(GEN6_RP_CONTROL,
  5323. GEN6_RP_MEDIA_TURBO |
  5324. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  5325. GEN6_RP_MEDIA_IS_GFX |
  5326. GEN6_RP_ENABLE |
  5327. GEN6_RP_UP_BUSY_AVG |
  5328. GEN6_RP_DOWN_IDLE_CONT);
  5329. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
  5330. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  5331. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  5332. for_each_engine(engine, dev_priv, id)
  5333. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  5334. I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
  5335. /* allows RC6 residency counter to work */
  5336. I915_WRITE(VLV_COUNTER_CONTROL,
  5337. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
  5338. VLV_MEDIA_RC0_COUNT_EN |
  5339. VLV_RENDER_RC0_COUNT_EN |
  5340. VLV_MEDIA_RC6_COUNT_EN |
  5341. VLV_RENDER_RC6_COUNT_EN));
  5342. if (intel_enable_rc6() & INTEL_RC6_ENABLE)
  5343. rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
  5344. intel_print_rc6_info(dev_priv, rc6_mode);
  5345. I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
  5346. /* Setting Fixed Bias */
  5347. val = VLV_OVERRIDE_EN |
  5348. VLV_SOC_TDP_EN |
  5349. VLV_BIAS_CPU_125_SOC_875;
  5350. vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
  5351. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  5352. /* RPS code assumes GPLL is used */
  5353. WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
  5354. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
  5355. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  5356. reset_rps(dev_priv, valleyview_set_rps);
  5357. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5358. }
  5359. static unsigned long intel_pxfreq(u32 vidfreq)
  5360. {
  5361. unsigned long freq;
  5362. int div = (vidfreq & 0x3f0000) >> 16;
  5363. int post = (vidfreq & 0x3000) >> 12;
  5364. int pre = (vidfreq & 0x7);
  5365. if (!pre)
  5366. return 0;
  5367. freq = ((div * 133333) / ((1<<post) * pre));
  5368. return freq;
  5369. }
  5370. static const struct cparams {
  5371. u16 i;
  5372. u16 t;
  5373. u16 m;
  5374. u16 c;
  5375. } cparams[] = {
  5376. { 1, 1333, 301, 28664 },
  5377. { 1, 1066, 294, 24460 },
  5378. { 1, 800, 294, 25192 },
  5379. { 0, 1333, 276, 27605 },
  5380. { 0, 1066, 276, 27605 },
  5381. { 0, 800, 231, 23784 },
  5382. };
  5383. static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
  5384. {
  5385. u64 total_count, diff, ret;
  5386. u32 count1, count2, count3, m = 0, c = 0;
  5387. unsigned long now = jiffies_to_msecs(jiffies), diff1;
  5388. int i;
  5389. lockdep_assert_held(&mchdev_lock);
  5390. diff1 = now - dev_priv->ips.last_time1;
  5391. /* Prevent division-by-zero if we are asking too fast.
  5392. * Also, we don't get interesting results if we are polling
  5393. * faster than once in 10ms, so just return the saved value
  5394. * in such cases.
  5395. */
  5396. if (diff1 <= 10)
  5397. return dev_priv->ips.chipset_power;
  5398. count1 = I915_READ(DMIEC);
  5399. count2 = I915_READ(DDREC);
  5400. count3 = I915_READ(CSIEC);
  5401. total_count = count1 + count2 + count3;
  5402. /* FIXME: handle per-counter overflow */
  5403. if (total_count < dev_priv->ips.last_count1) {
  5404. diff = ~0UL - dev_priv->ips.last_count1;
  5405. diff += total_count;
  5406. } else {
  5407. diff = total_count - dev_priv->ips.last_count1;
  5408. }
  5409. for (i = 0; i < ARRAY_SIZE(cparams); i++) {
  5410. if (cparams[i].i == dev_priv->ips.c_m &&
  5411. cparams[i].t == dev_priv->ips.r_t) {
  5412. m = cparams[i].m;
  5413. c = cparams[i].c;
  5414. break;
  5415. }
  5416. }
  5417. diff = div_u64(diff, diff1);
  5418. ret = ((m * diff) + c);
  5419. ret = div_u64(ret, 10);
  5420. dev_priv->ips.last_count1 = total_count;
  5421. dev_priv->ips.last_time1 = now;
  5422. dev_priv->ips.chipset_power = ret;
  5423. return ret;
  5424. }
  5425. unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
  5426. {
  5427. unsigned long val;
  5428. if (INTEL_INFO(dev_priv)->gen != 5)
  5429. return 0;
  5430. spin_lock_irq(&mchdev_lock);
  5431. val = __i915_chipset_val(dev_priv);
  5432. spin_unlock_irq(&mchdev_lock);
  5433. return val;
  5434. }
  5435. unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
  5436. {
  5437. unsigned long m, x, b;
  5438. u32 tsfs;
  5439. tsfs = I915_READ(TSFS);
  5440. m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
  5441. x = I915_READ8(TR1);
  5442. b = tsfs & TSFS_INTR_MASK;
  5443. return ((m * x) / 127) - b;
  5444. }
  5445. static int _pxvid_to_vd(u8 pxvid)
  5446. {
  5447. if (pxvid == 0)
  5448. return 0;
  5449. if (pxvid >= 8 && pxvid < 31)
  5450. pxvid = 31;
  5451. return (pxvid + 2) * 125;
  5452. }
  5453. static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
  5454. {
  5455. const int vd = _pxvid_to_vd(pxvid);
  5456. const int vm = vd - 1125;
  5457. if (INTEL_INFO(dev_priv)->is_mobile)
  5458. return vm > 0 ? vm : 0;
  5459. return vd;
  5460. }
  5461. static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
  5462. {
  5463. u64 now, diff, diffms;
  5464. u32 count;
  5465. lockdep_assert_held(&mchdev_lock);
  5466. now = ktime_get_raw_ns();
  5467. diffms = now - dev_priv->ips.last_time2;
  5468. do_div(diffms, NSEC_PER_MSEC);
  5469. /* Don't divide by 0 */
  5470. if (!diffms)
  5471. return;
  5472. count = I915_READ(GFXEC);
  5473. if (count < dev_priv->ips.last_count2) {
  5474. diff = ~0UL - dev_priv->ips.last_count2;
  5475. diff += count;
  5476. } else {
  5477. diff = count - dev_priv->ips.last_count2;
  5478. }
  5479. dev_priv->ips.last_count2 = count;
  5480. dev_priv->ips.last_time2 = now;
  5481. /* More magic constants... */
  5482. diff = diff * 1181;
  5483. diff = div_u64(diff, diffms * 10);
  5484. dev_priv->ips.gfx_power = diff;
  5485. }
  5486. void i915_update_gfx_val(struct drm_i915_private *dev_priv)
  5487. {
  5488. if (INTEL_INFO(dev_priv)->gen != 5)
  5489. return;
  5490. spin_lock_irq(&mchdev_lock);
  5491. __i915_update_gfx_val(dev_priv);
  5492. spin_unlock_irq(&mchdev_lock);
  5493. }
  5494. static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
  5495. {
  5496. unsigned long t, corr, state1, corr2, state2;
  5497. u32 pxvid, ext_v;
  5498. lockdep_assert_held(&mchdev_lock);
  5499. pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
  5500. pxvid = (pxvid >> 24) & 0x7f;
  5501. ext_v = pvid_to_extvid(dev_priv, pxvid);
  5502. state1 = ext_v;
  5503. t = i915_mch_val(dev_priv);
  5504. /* Revel in the empirically derived constants */
  5505. /* Correction factor in 1/100000 units */
  5506. if (t > 80)
  5507. corr = ((t * 2349) + 135940);
  5508. else if (t >= 50)
  5509. corr = ((t * 964) + 29317);
  5510. else /* < 50 */
  5511. corr = ((t * 301) + 1004);
  5512. corr = corr * ((150142 * state1) / 10000 - 78642);
  5513. corr /= 100000;
  5514. corr2 = (corr * dev_priv->ips.corr);
  5515. state2 = (corr2 * state1) / 10000;
  5516. state2 /= 100; /* convert to mW */
  5517. __i915_update_gfx_val(dev_priv);
  5518. return dev_priv->ips.gfx_power + state2;
  5519. }
  5520. unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
  5521. {
  5522. unsigned long val;
  5523. if (INTEL_INFO(dev_priv)->gen != 5)
  5524. return 0;
  5525. spin_lock_irq(&mchdev_lock);
  5526. val = __i915_gfx_val(dev_priv);
  5527. spin_unlock_irq(&mchdev_lock);
  5528. return val;
  5529. }
  5530. /**
  5531. * i915_read_mch_val - return value for IPS use
  5532. *
  5533. * Calculate and return a value for the IPS driver to use when deciding whether
  5534. * we have thermal and power headroom to increase CPU or GPU power budget.
  5535. */
  5536. unsigned long i915_read_mch_val(void)
  5537. {
  5538. struct drm_i915_private *dev_priv;
  5539. unsigned long chipset_val, graphics_val, ret = 0;
  5540. spin_lock_irq(&mchdev_lock);
  5541. if (!i915_mch_dev)
  5542. goto out_unlock;
  5543. dev_priv = i915_mch_dev;
  5544. chipset_val = __i915_chipset_val(dev_priv);
  5545. graphics_val = __i915_gfx_val(dev_priv);
  5546. ret = chipset_val + graphics_val;
  5547. out_unlock:
  5548. spin_unlock_irq(&mchdev_lock);
  5549. return ret;
  5550. }
  5551. EXPORT_SYMBOL_GPL(i915_read_mch_val);
  5552. /**
  5553. * i915_gpu_raise - raise GPU frequency limit
  5554. *
  5555. * Raise the limit; IPS indicates we have thermal headroom.
  5556. */
  5557. bool i915_gpu_raise(void)
  5558. {
  5559. struct drm_i915_private *dev_priv;
  5560. bool ret = true;
  5561. spin_lock_irq(&mchdev_lock);
  5562. if (!i915_mch_dev) {
  5563. ret = false;
  5564. goto out_unlock;
  5565. }
  5566. dev_priv = i915_mch_dev;
  5567. if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
  5568. dev_priv->ips.max_delay--;
  5569. out_unlock:
  5570. spin_unlock_irq(&mchdev_lock);
  5571. return ret;
  5572. }
  5573. EXPORT_SYMBOL_GPL(i915_gpu_raise);
  5574. /**
  5575. * i915_gpu_lower - lower GPU frequency limit
  5576. *
  5577. * IPS indicates we're close to a thermal limit, so throttle back the GPU
  5578. * frequency maximum.
  5579. */
  5580. bool i915_gpu_lower(void)
  5581. {
  5582. struct drm_i915_private *dev_priv;
  5583. bool ret = true;
  5584. spin_lock_irq(&mchdev_lock);
  5585. if (!i915_mch_dev) {
  5586. ret = false;
  5587. goto out_unlock;
  5588. }
  5589. dev_priv = i915_mch_dev;
  5590. if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
  5591. dev_priv->ips.max_delay++;
  5592. out_unlock:
  5593. spin_unlock_irq(&mchdev_lock);
  5594. return ret;
  5595. }
  5596. EXPORT_SYMBOL_GPL(i915_gpu_lower);
  5597. /**
  5598. * i915_gpu_busy - indicate GPU business to IPS
  5599. *
  5600. * Tell the IPS driver whether or not the GPU is busy.
  5601. */
  5602. bool i915_gpu_busy(void)
  5603. {
  5604. bool ret = false;
  5605. spin_lock_irq(&mchdev_lock);
  5606. if (i915_mch_dev)
  5607. ret = i915_mch_dev->gt.awake;
  5608. spin_unlock_irq(&mchdev_lock);
  5609. return ret;
  5610. }
  5611. EXPORT_SYMBOL_GPL(i915_gpu_busy);
  5612. /**
  5613. * i915_gpu_turbo_disable - disable graphics turbo
  5614. *
  5615. * Disable graphics turbo by resetting the max frequency and setting the
  5616. * current frequency to the default.
  5617. */
  5618. bool i915_gpu_turbo_disable(void)
  5619. {
  5620. struct drm_i915_private *dev_priv;
  5621. bool ret = true;
  5622. spin_lock_irq(&mchdev_lock);
  5623. if (!i915_mch_dev) {
  5624. ret = false;
  5625. goto out_unlock;
  5626. }
  5627. dev_priv = i915_mch_dev;
  5628. dev_priv->ips.max_delay = dev_priv->ips.fstart;
  5629. if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
  5630. ret = false;
  5631. out_unlock:
  5632. spin_unlock_irq(&mchdev_lock);
  5633. return ret;
  5634. }
  5635. EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
  5636. /**
  5637. * Tells the intel_ips driver that the i915 driver is now loaded, if
  5638. * IPS got loaded first.
  5639. *
  5640. * This awkward dance is so that neither module has to depend on the
  5641. * other in order for IPS to do the appropriate communication of
  5642. * GPU turbo limits to i915.
  5643. */
  5644. static void
  5645. ips_ping_for_i915_load(void)
  5646. {
  5647. void (*link)(void);
  5648. link = symbol_get(ips_link_to_i915_driver);
  5649. if (link) {
  5650. link();
  5651. symbol_put(ips_link_to_i915_driver);
  5652. }
  5653. }
  5654. void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
  5655. {
  5656. /* We only register the i915 ips part with intel-ips once everything is
  5657. * set up, to avoid intel-ips sneaking in and reading bogus values. */
  5658. spin_lock_irq(&mchdev_lock);
  5659. i915_mch_dev = dev_priv;
  5660. spin_unlock_irq(&mchdev_lock);
  5661. ips_ping_for_i915_load();
  5662. }
  5663. void intel_gpu_ips_teardown(void)
  5664. {
  5665. spin_lock_irq(&mchdev_lock);
  5666. i915_mch_dev = NULL;
  5667. spin_unlock_irq(&mchdev_lock);
  5668. }
  5669. static void intel_init_emon(struct drm_i915_private *dev_priv)
  5670. {
  5671. u32 lcfuse;
  5672. u8 pxw[16];
  5673. int i;
  5674. /* Disable to program */
  5675. I915_WRITE(ECR, 0);
  5676. POSTING_READ(ECR);
  5677. /* Program energy weights for various events */
  5678. I915_WRITE(SDEW, 0x15040d00);
  5679. I915_WRITE(CSIEW0, 0x007f0000);
  5680. I915_WRITE(CSIEW1, 0x1e220004);
  5681. I915_WRITE(CSIEW2, 0x04000004);
  5682. for (i = 0; i < 5; i++)
  5683. I915_WRITE(PEW(i), 0);
  5684. for (i = 0; i < 3; i++)
  5685. I915_WRITE(DEW(i), 0);
  5686. /* Program P-state weights to account for frequency power adjustment */
  5687. for (i = 0; i < 16; i++) {
  5688. u32 pxvidfreq = I915_READ(PXVFREQ(i));
  5689. unsigned long freq = intel_pxfreq(pxvidfreq);
  5690. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  5691. PXVFREQ_PX_SHIFT;
  5692. unsigned long val;
  5693. val = vid * vid;
  5694. val *= (freq / 1000);
  5695. val *= 255;
  5696. val /= (127*127*900);
  5697. if (val > 0xff)
  5698. DRM_ERROR("bad pxval: %ld\n", val);
  5699. pxw[i] = val;
  5700. }
  5701. /* Render standby states get 0 weight */
  5702. pxw[14] = 0;
  5703. pxw[15] = 0;
  5704. for (i = 0; i < 4; i++) {
  5705. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  5706. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  5707. I915_WRITE(PXW(i), val);
  5708. }
  5709. /* Adjust magic regs to magic values (more experimental results) */
  5710. I915_WRITE(OGW0, 0);
  5711. I915_WRITE(OGW1, 0);
  5712. I915_WRITE(EG0, 0x00007f00);
  5713. I915_WRITE(EG1, 0x0000000e);
  5714. I915_WRITE(EG2, 0x000e0000);
  5715. I915_WRITE(EG3, 0x68000300);
  5716. I915_WRITE(EG4, 0x42000000);
  5717. I915_WRITE(EG5, 0x00140031);
  5718. I915_WRITE(EG6, 0);
  5719. I915_WRITE(EG7, 0);
  5720. for (i = 0; i < 8; i++)
  5721. I915_WRITE(PXWL(i), 0);
  5722. /* Enable PMON + select events */
  5723. I915_WRITE(ECR, 0x80000019);
  5724. lcfuse = I915_READ(LCFUSE02);
  5725. dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
  5726. }
  5727. void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
  5728. {
  5729. /*
  5730. * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
  5731. * requirement.
  5732. */
  5733. if (!i915.enable_rc6) {
  5734. DRM_INFO("RC6 disabled, disabling runtime PM support\n");
  5735. intel_runtime_pm_get(dev_priv);
  5736. }
  5737. mutex_lock(&dev_priv->drm.struct_mutex);
  5738. mutex_lock(&dev_priv->rps.hw_lock);
  5739. /* Initialize RPS limits (for userspace) */
  5740. if (IS_CHERRYVIEW(dev_priv))
  5741. cherryview_init_gt_powersave(dev_priv);
  5742. else if (IS_VALLEYVIEW(dev_priv))
  5743. valleyview_init_gt_powersave(dev_priv);
  5744. else if (INTEL_GEN(dev_priv) >= 6)
  5745. gen6_init_rps_frequencies(dev_priv);
  5746. /* Derive initial user preferences/limits from the hardware limits */
  5747. dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
  5748. dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
  5749. dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
  5750. dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
  5751. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  5752. dev_priv->rps.min_freq_softlimit =
  5753. max_t(int,
  5754. dev_priv->rps.efficient_freq,
  5755. intel_freq_opcode(dev_priv, 450));
  5756. /* After setting max-softlimit, find the overclock max freq */
  5757. if (IS_GEN6(dev_priv) ||
  5758. IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  5759. u32 params = 0;
  5760. sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
  5761. if (params & BIT(31)) { /* OC supported */
  5762. DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
  5763. (dev_priv->rps.max_freq & 0xff) * 50,
  5764. (params & 0xff) * 50);
  5765. dev_priv->rps.max_freq = params & 0xff;
  5766. }
  5767. }
  5768. /* Finally allow us to boost to max by default */
  5769. dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
  5770. mutex_unlock(&dev_priv->rps.hw_lock);
  5771. mutex_unlock(&dev_priv->drm.struct_mutex);
  5772. intel_autoenable_gt_powersave(dev_priv);
  5773. }
  5774. void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
  5775. {
  5776. if (IS_VALLEYVIEW(dev_priv))
  5777. valleyview_cleanup_gt_powersave(dev_priv);
  5778. if (!i915.enable_rc6)
  5779. intel_runtime_pm_put(dev_priv);
  5780. }
  5781. /**
  5782. * intel_suspend_gt_powersave - suspend PM work and helper threads
  5783. * @dev_priv: i915 device
  5784. *
  5785. * We don't want to disable RC6 or other features here, we just want
  5786. * to make sure any work we've queued has finished and won't bother
  5787. * us while we're suspended.
  5788. */
  5789. void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
  5790. {
  5791. if (INTEL_GEN(dev_priv) < 6)
  5792. return;
  5793. if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
  5794. intel_runtime_pm_put(dev_priv);
  5795. /* gen6_rps_idle() will be called later to disable interrupts */
  5796. }
  5797. void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
  5798. {
  5799. dev_priv->rps.enabled = true; /* force disabling */
  5800. intel_disable_gt_powersave(dev_priv);
  5801. gen6_reset_rps_interrupts(dev_priv);
  5802. }
  5803. void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
  5804. {
  5805. if (!READ_ONCE(dev_priv->rps.enabled))
  5806. return;
  5807. mutex_lock(&dev_priv->rps.hw_lock);
  5808. if (INTEL_GEN(dev_priv) >= 9) {
  5809. gen9_disable_rc6(dev_priv);
  5810. gen9_disable_rps(dev_priv);
  5811. } else if (IS_CHERRYVIEW(dev_priv)) {
  5812. cherryview_disable_rps(dev_priv);
  5813. } else if (IS_VALLEYVIEW(dev_priv)) {
  5814. valleyview_disable_rps(dev_priv);
  5815. } else if (INTEL_GEN(dev_priv) >= 6) {
  5816. gen6_disable_rps(dev_priv);
  5817. } else if (IS_IRONLAKE_M(dev_priv)) {
  5818. ironlake_disable_drps(dev_priv);
  5819. }
  5820. dev_priv->rps.enabled = false;
  5821. mutex_unlock(&dev_priv->rps.hw_lock);
  5822. }
  5823. void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
  5824. {
  5825. /* We shouldn't be disabling as we submit, so this should be less
  5826. * racy than it appears!
  5827. */
  5828. if (READ_ONCE(dev_priv->rps.enabled))
  5829. return;
  5830. /* Powersaving is controlled by the host when inside a VM */
  5831. if (intel_vgpu_active(dev_priv))
  5832. return;
  5833. mutex_lock(&dev_priv->rps.hw_lock);
  5834. if (IS_CHERRYVIEW(dev_priv)) {
  5835. cherryview_enable_rps(dev_priv);
  5836. } else if (IS_VALLEYVIEW(dev_priv)) {
  5837. valleyview_enable_rps(dev_priv);
  5838. } else if (INTEL_GEN(dev_priv) >= 9) {
  5839. gen9_enable_rc6(dev_priv);
  5840. gen9_enable_rps(dev_priv);
  5841. if (IS_GEN9_BC(dev_priv))
  5842. gen6_update_ring_freq(dev_priv);
  5843. } else if (IS_BROADWELL(dev_priv)) {
  5844. gen8_enable_rps(dev_priv);
  5845. gen6_update_ring_freq(dev_priv);
  5846. } else if (INTEL_GEN(dev_priv) >= 6) {
  5847. gen6_enable_rps(dev_priv);
  5848. gen6_update_ring_freq(dev_priv);
  5849. } else if (IS_IRONLAKE_M(dev_priv)) {
  5850. ironlake_enable_drps(dev_priv);
  5851. intel_init_emon(dev_priv);
  5852. }
  5853. WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
  5854. WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
  5855. WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
  5856. WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
  5857. dev_priv->rps.enabled = true;
  5858. mutex_unlock(&dev_priv->rps.hw_lock);
  5859. }
  5860. static void __intel_autoenable_gt_powersave(struct work_struct *work)
  5861. {
  5862. struct drm_i915_private *dev_priv =
  5863. container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
  5864. struct intel_engine_cs *rcs;
  5865. struct drm_i915_gem_request *req;
  5866. if (READ_ONCE(dev_priv->rps.enabled))
  5867. goto out;
  5868. rcs = dev_priv->engine[RCS];
  5869. if (rcs->last_retired_context)
  5870. goto out;
  5871. if (!rcs->init_context)
  5872. goto out;
  5873. mutex_lock(&dev_priv->drm.struct_mutex);
  5874. req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
  5875. if (IS_ERR(req))
  5876. goto unlock;
  5877. if (!i915.enable_execlists && i915_switch_context(req) == 0)
  5878. rcs->init_context(req);
  5879. /* Mark the device busy, calling intel_enable_gt_powersave() */
  5880. i915_add_request(req);
  5881. unlock:
  5882. mutex_unlock(&dev_priv->drm.struct_mutex);
  5883. out:
  5884. intel_runtime_pm_put(dev_priv);
  5885. }
  5886. void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
  5887. {
  5888. if (READ_ONCE(dev_priv->rps.enabled))
  5889. return;
  5890. if (IS_IRONLAKE_M(dev_priv)) {
  5891. ironlake_enable_drps(dev_priv);
  5892. intel_init_emon(dev_priv);
  5893. } else if (INTEL_INFO(dev_priv)->gen >= 6) {
  5894. /*
  5895. * PCU communication is slow and this doesn't need to be
  5896. * done at any specific time, so do this out of our fast path
  5897. * to make resume and init faster.
  5898. *
  5899. * We depend on the HW RC6 power context save/restore
  5900. * mechanism when entering D3 through runtime PM suspend. So
  5901. * disable RPM until RPS/RC6 is properly setup. We can only
  5902. * get here via the driver load/system resume/runtime resume
  5903. * paths, so the _noresume version is enough (and in case of
  5904. * runtime resume it's necessary).
  5905. */
  5906. if (queue_delayed_work(dev_priv->wq,
  5907. &dev_priv->rps.autoenable_work,
  5908. round_jiffies_up_relative(HZ)))
  5909. intel_runtime_pm_get_noresume(dev_priv);
  5910. }
  5911. }
  5912. static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
  5913. {
  5914. /*
  5915. * On Ibex Peak and Cougar Point, we need to disable clock
  5916. * gating for the panel power sequencer or it will fail to
  5917. * start up when no ports are active.
  5918. */
  5919. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  5920. }
  5921. static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
  5922. {
  5923. enum pipe pipe;
  5924. for_each_pipe(dev_priv, pipe) {
  5925. I915_WRITE(DSPCNTR(pipe),
  5926. I915_READ(DSPCNTR(pipe)) |
  5927. DISPPLANE_TRICKLE_FEED_DISABLE);
  5928. I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
  5929. POSTING_READ(DSPSURF(pipe));
  5930. }
  5931. }
  5932. static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
  5933. {
  5934. I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
  5935. I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
  5936. I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
  5937. /*
  5938. * Don't touch WM1S_LP_EN here.
  5939. * Doing so could cause underruns.
  5940. */
  5941. }
  5942. static void ironlake_init_clock_gating(struct drm_i915_private *dev_priv)
  5943. {
  5944. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  5945. /*
  5946. * Required for FBC
  5947. * WaFbcDisableDpfcClockGating:ilk
  5948. */
  5949. dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
  5950. ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
  5951. ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
  5952. I915_WRITE(PCH_3DCGDIS0,
  5953. MARIUNIT_CLOCK_GATE_DISABLE |
  5954. SVSMUNIT_CLOCK_GATE_DISABLE);
  5955. I915_WRITE(PCH_3DCGDIS1,
  5956. VFMUNIT_CLOCK_GATE_DISABLE);
  5957. /*
  5958. * According to the spec the following bits should be set in
  5959. * order to enable memory self-refresh
  5960. * The bit 22/21 of 0x42004
  5961. * The bit 5 of 0x42020
  5962. * The bit 15 of 0x45000
  5963. */
  5964. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5965. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  5966. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  5967. dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
  5968. I915_WRITE(DISP_ARB_CTL,
  5969. (I915_READ(DISP_ARB_CTL) |
  5970. DISP_FBC_WM_DIS));
  5971. ilk_init_lp_watermarks(dev_priv);
  5972. /*
  5973. * Based on the document from hardware guys the following bits
  5974. * should be set unconditionally in order to enable FBC.
  5975. * The bit 22 of 0x42000
  5976. * The bit 22 of 0x42004
  5977. * The bit 7,8,9 of 0x42020.
  5978. */
  5979. if (IS_IRONLAKE_M(dev_priv)) {
  5980. /* WaFbcAsynchFlipDisableFbcQueue:ilk */
  5981. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5982. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5983. ILK_FBCQ_DIS);
  5984. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5985. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5986. ILK_DPARB_GATE);
  5987. }
  5988. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  5989. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5990. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5991. ILK_ELPIN_409_SELECT);
  5992. I915_WRITE(_3D_CHICKEN2,
  5993. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  5994. _3D_CHICKEN2_WM_READ_PIPELINED);
  5995. /* WaDisableRenderCachePipelinedFlush:ilk */
  5996. I915_WRITE(CACHE_MODE_0,
  5997. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  5998. /* WaDisable_RenderCache_OperationalFlush:ilk */
  5999. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  6000. g4x_disable_trickle_feed(dev_priv);
  6001. ibx_init_clock_gating(dev_priv);
  6002. }
  6003. static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
  6004. {
  6005. int pipe;
  6006. uint32_t val;
  6007. /*
  6008. * On Ibex Peak and Cougar Point, we need to disable clock
  6009. * gating for the panel power sequencer or it will fail to
  6010. * start up when no ports are active.
  6011. */
  6012. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
  6013. PCH_DPLUNIT_CLOCK_GATE_DISABLE |
  6014. PCH_CPUNIT_CLOCK_GATE_DISABLE);
  6015. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  6016. DPLS_EDP_PPS_FIX_DIS);
  6017. /* The below fixes the weird display corruption, a few pixels shifted
  6018. * downward, on (only) LVDS of some HP laptops with IVY.
  6019. */
  6020. for_each_pipe(dev_priv, pipe) {
  6021. val = I915_READ(TRANS_CHICKEN2(pipe));
  6022. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  6023. val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  6024. if (dev_priv->vbt.fdi_rx_polarity_inverted)
  6025. val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  6026. val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
  6027. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
  6028. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
  6029. I915_WRITE(TRANS_CHICKEN2(pipe), val);
  6030. }
  6031. /* WADP0ClockGatingDisable */
  6032. for_each_pipe(dev_priv, pipe) {
  6033. I915_WRITE(TRANS_CHICKEN1(pipe),
  6034. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  6035. }
  6036. }
  6037. static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
  6038. {
  6039. uint32_t tmp;
  6040. tmp = I915_READ(MCH_SSKPD);
  6041. if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
  6042. DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
  6043. tmp);
  6044. }
  6045. static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
  6046. {
  6047. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  6048. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  6049. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6050. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6051. ILK_ELPIN_409_SELECT);
  6052. /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
  6053. I915_WRITE(_3D_CHICKEN,
  6054. _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
  6055. /* WaDisable_RenderCache_OperationalFlush:snb */
  6056. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  6057. /*
  6058. * BSpec recoomends 8x4 when MSAA is used,
  6059. * however in practice 16x4 seems fastest.
  6060. *
  6061. * Note that PS/WM thread counts depend on the WIZ hashing
  6062. * disable bit, which we don't touch here, but it's good
  6063. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  6064. */
  6065. I915_WRITE(GEN6_GT_MODE,
  6066. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  6067. ilk_init_lp_watermarks(dev_priv);
  6068. I915_WRITE(CACHE_MODE_0,
  6069. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  6070. I915_WRITE(GEN6_UCGCTL1,
  6071. I915_READ(GEN6_UCGCTL1) |
  6072. GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
  6073. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  6074. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  6075. * gating disable must be set. Failure to set it results in
  6076. * flickering pixels due to Z write ordering failures after
  6077. * some amount of runtime in the Mesa "fire" demo, and Unigine
  6078. * Sanctuary and Tropics, and apparently anything else with
  6079. * alpha test or pixel discard.
  6080. *
  6081. * According to the spec, bit 11 (RCCUNIT) must also be set,
  6082. * but we didn't debug actual testcases to find it out.
  6083. *
  6084. * WaDisableRCCUnitClockGating:snb
  6085. * WaDisableRCPBUnitClockGating:snb
  6086. */
  6087. I915_WRITE(GEN6_UCGCTL2,
  6088. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  6089. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  6090. /* WaStripsFansDisableFastClipPerformanceFix:snb */
  6091. I915_WRITE(_3D_CHICKEN3,
  6092. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
  6093. /*
  6094. * Bspec says:
  6095. * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
  6096. * 3DSTATE_SF number of SF output attributes is more than 16."
  6097. */
  6098. I915_WRITE(_3D_CHICKEN3,
  6099. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
  6100. /*
  6101. * According to the spec the following bits should be
  6102. * set in order to enable memory self-refresh and fbc:
  6103. * The bit21 and bit22 of 0x42000
  6104. * The bit21 and bit22 of 0x42004
  6105. * The bit5 and bit7 of 0x42020
  6106. * The bit14 of 0x70180
  6107. * The bit14 of 0x71180
  6108. *
  6109. * WaFbcAsynchFlipDisableFbcQueue:snb
  6110. */
  6111. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  6112. I915_READ(ILK_DISPLAY_CHICKEN1) |
  6113. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  6114. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6115. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6116. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  6117. I915_WRITE(ILK_DSPCLK_GATE_D,
  6118. I915_READ(ILK_DSPCLK_GATE_D) |
  6119. ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
  6120. ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
  6121. g4x_disable_trickle_feed(dev_priv);
  6122. cpt_init_clock_gating(dev_priv);
  6123. gen6_check_mch_setup(dev_priv);
  6124. }
  6125. static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
  6126. {
  6127. uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
  6128. /*
  6129. * WaVSThreadDispatchOverride:ivb,vlv
  6130. *
  6131. * This actually overrides the dispatch
  6132. * mode for all thread types.
  6133. */
  6134. reg &= ~GEN7_FF_SCHED_MASK;
  6135. reg |= GEN7_FF_TS_SCHED_HW;
  6136. reg |= GEN7_FF_VS_SCHED_HW;
  6137. reg |= GEN7_FF_DS_SCHED_HW;
  6138. I915_WRITE(GEN7_FF_THREAD_MODE, reg);
  6139. }
  6140. static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
  6141. {
  6142. /*
  6143. * TODO: this bit should only be enabled when really needed, then
  6144. * disabled when not needed anymore in order to save power.
  6145. */
  6146. if (HAS_PCH_LPT_LP(dev_priv))
  6147. I915_WRITE(SOUTH_DSPCLK_GATE_D,
  6148. I915_READ(SOUTH_DSPCLK_GATE_D) |
  6149. PCH_LP_PARTITION_LEVEL_DISABLE);
  6150. /* WADPOClockGatingDisable:hsw */
  6151. I915_WRITE(TRANS_CHICKEN1(PIPE_A),
  6152. I915_READ(TRANS_CHICKEN1(PIPE_A)) |
  6153. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  6154. }
  6155. static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
  6156. {
  6157. if (HAS_PCH_LPT_LP(dev_priv)) {
  6158. uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6159. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6160. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6161. }
  6162. }
  6163. static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
  6164. int general_prio_credits,
  6165. int high_prio_credits)
  6166. {
  6167. u32 misccpctl;
  6168. /* WaTempDisableDOPClkGating:bdw */
  6169. misccpctl = I915_READ(GEN7_MISCCPCTL);
  6170. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  6171. I915_WRITE(GEN8_L3SQCREG1,
  6172. L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
  6173. L3_HIGH_PRIO_CREDITS(high_prio_credits));
  6174. /*
  6175. * Wait at least 100 clocks before re-enabling clock gating.
  6176. * See the definition of L3SQCREG1 in BSpec.
  6177. */
  6178. POSTING_READ(GEN8_L3SQCREG1);
  6179. udelay(1);
  6180. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  6181. }
  6182. static void kabylake_init_clock_gating(struct drm_i915_private *dev_priv)
  6183. {
  6184. gen9_init_clock_gating(dev_priv);
  6185. /* WaDisableSDEUnitClockGating:kbl */
  6186. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
  6187. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  6188. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  6189. /* WaDisableGamClockGating:kbl */
  6190. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
  6191. I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
  6192. GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
  6193. /* WaFbcNukeOnHostModify:kbl */
  6194. I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
  6195. ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
  6196. }
  6197. static void skylake_init_clock_gating(struct drm_i915_private *dev_priv)
  6198. {
  6199. gen9_init_clock_gating(dev_priv);
  6200. /* WAC6entrylatency:skl */
  6201. I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
  6202. FBC_LLC_FULLY_OPEN);
  6203. /* WaFbcNukeOnHostModify:skl */
  6204. I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
  6205. ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
  6206. }
  6207. static void broadwell_init_clock_gating(struct drm_i915_private *dev_priv)
  6208. {
  6209. enum pipe pipe;
  6210. ilk_init_lp_watermarks(dev_priv);
  6211. /* WaSwitchSolVfFArbitrationPriority:bdw */
  6212. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  6213. /* WaPsrDPAMaskVBlankInSRD:bdw */
  6214. I915_WRITE(CHICKEN_PAR1_1,
  6215. I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
  6216. /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
  6217. for_each_pipe(dev_priv, pipe) {
  6218. I915_WRITE(CHICKEN_PIPESL_1(pipe),
  6219. I915_READ(CHICKEN_PIPESL_1(pipe)) |
  6220. BDW_DPRS_MASK_VBLANK_SRD);
  6221. }
  6222. /* WaVSRefCountFullforceMissDisable:bdw */
  6223. /* WaDSRefCountFullforceMissDisable:bdw */
  6224. I915_WRITE(GEN7_FF_THREAD_MODE,
  6225. I915_READ(GEN7_FF_THREAD_MODE) &
  6226. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  6227. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  6228. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  6229. /* WaDisableSDEUnitClockGating:bdw */
  6230. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  6231. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  6232. /* WaProgramL3SqcReg1Default:bdw */
  6233. gen8_set_l3sqc_credits(dev_priv, 30, 2);
  6234. /*
  6235. * WaGttCachingOffByDefault:bdw
  6236. * GTT cache may not work with big pages, so if those
  6237. * are ever enabled GTT cache may need to be disabled.
  6238. */
  6239. I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
  6240. /* WaKVMNotificationOnConfigChange:bdw */
  6241. I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
  6242. | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
  6243. lpt_init_clock_gating(dev_priv);
  6244. /* WaDisableDopClockGating:bdw
  6245. *
  6246. * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
  6247. * clock gating.
  6248. */
  6249. I915_WRITE(GEN6_UCGCTL1,
  6250. I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
  6251. }
  6252. static void haswell_init_clock_gating(struct drm_i915_private *dev_priv)
  6253. {
  6254. ilk_init_lp_watermarks(dev_priv);
  6255. /* L3 caching of data atomics doesn't work -- disable it. */
  6256. I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
  6257. I915_WRITE(HSW_ROW_CHICKEN3,
  6258. _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
  6259. /* This is required by WaCatErrorRejectionIssue:hsw */
  6260. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  6261. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  6262. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  6263. /* WaVSRefCountFullforceMissDisable:hsw */
  6264. I915_WRITE(GEN7_FF_THREAD_MODE,
  6265. I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
  6266. /* WaDisable_RenderCache_OperationalFlush:hsw */
  6267. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  6268. /* enable HiZ Raw Stall Optimization */
  6269. I915_WRITE(CACHE_MODE_0_GEN7,
  6270. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  6271. /* WaDisable4x2SubspanOptimization:hsw */
  6272. I915_WRITE(CACHE_MODE_1,
  6273. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  6274. /*
  6275. * BSpec recommends 8x4 when MSAA is used,
  6276. * however in practice 16x4 seems fastest.
  6277. *
  6278. * Note that PS/WM thread counts depend on the WIZ hashing
  6279. * disable bit, which we don't touch here, but it's good
  6280. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  6281. */
  6282. I915_WRITE(GEN7_GT_MODE,
  6283. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  6284. /* WaSampleCChickenBitEnable:hsw */
  6285. I915_WRITE(HALF_SLICE_CHICKEN3,
  6286. _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
  6287. /* WaSwitchSolVfFArbitrationPriority:hsw */
  6288. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  6289. /* WaRsPkgCStateDisplayPMReq:hsw */
  6290. I915_WRITE(CHICKEN_PAR1_1,
  6291. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  6292. lpt_init_clock_gating(dev_priv);
  6293. }
  6294. static void ivybridge_init_clock_gating(struct drm_i915_private *dev_priv)
  6295. {
  6296. uint32_t snpcr;
  6297. ilk_init_lp_watermarks(dev_priv);
  6298. I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
  6299. /* WaDisableEarlyCull:ivb */
  6300. I915_WRITE(_3D_CHICKEN3,
  6301. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  6302. /* WaDisableBackToBackFlipFix:ivb */
  6303. I915_WRITE(IVB_CHICKEN3,
  6304. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  6305. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  6306. /* WaDisablePSDDualDispatchEnable:ivb */
  6307. if (IS_IVB_GT1(dev_priv))
  6308. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  6309. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  6310. /* WaDisable_RenderCache_OperationalFlush:ivb */
  6311. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  6312. /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
  6313. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  6314. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  6315. /* WaApplyL3ControlAndL3ChickenMode:ivb */
  6316. I915_WRITE(GEN7_L3CNTLREG1,
  6317. GEN7_WA_FOR_GEN7_L3_CONTROL);
  6318. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  6319. GEN7_WA_L3_CHICKEN_MODE);
  6320. if (IS_IVB_GT1(dev_priv))
  6321. I915_WRITE(GEN7_ROW_CHICKEN2,
  6322. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  6323. else {
  6324. /* must write both registers */
  6325. I915_WRITE(GEN7_ROW_CHICKEN2,
  6326. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  6327. I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
  6328. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  6329. }
  6330. /* WaForceL3Serialization:ivb */
  6331. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  6332. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  6333. /*
  6334. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  6335. * This implements the WaDisableRCZUnitClockGating:ivb workaround.
  6336. */
  6337. I915_WRITE(GEN6_UCGCTL2,
  6338. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  6339. /* This is required by WaCatErrorRejectionIssue:ivb */
  6340. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  6341. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  6342. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  6343. g4x_disable_trickle_feed(dev_priv);
  6344. gen7_setup_fixed_func_scheduler(dev_priv);
  6345. if (0) { /* causes HiZ corruption on ivb:gt1 */
  6346. /* enable HiZ Raw Stall Optimization */
  6347. I915_WRITE(CACHE_MODE_0_GEN7,
  6348. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  6349. }
  6350. /* WaDisable4x2SubspanOptimization:ivb */
  6351. I915_WRITE(CACHE_MODE_1,
  6352. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  6353. /*
  6354. * BSpec recommends 8x4 when MSAA is used,
  6355. * however in practice 16x4 seems fastest.
  6356. *
  6357. * Note that PS/WM thread counts depend on the WIZ hashing
  6358. * disable bit, which we don't touch here, but it's good
  6359. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  6360. */
  6361. I915_WRITE(GEN7_GT_MODE,
  6362. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  6363. snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
  6364. snpcr &= ~GEN6_MBC_SNPCR_MASK;
  6365. snpcr |= GEN6_MBC_SNPCR_MED;
  6366. I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
  6367. if (!HAS_PCH_NOP(dev_priv))
  6368. cpt_init_clock_gating(dev_priv);
  6369. gen6_check_mch_setup(dev_priv);
  6370. }
  6371. static void valleyview_init_clock_gating(struct drm_i915_private *dev_priv)
  6372. {
  6373. /* WaDisableEarlyCull:vlv */
  6374. I915_WRITE(_3D_CHICKEN3,
  6375. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  6376. /* WaDisableBackToBackFlipFix:vlv */
  6377. I915_WRITE(IVB_CHICKEN3,
  6378. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  6379. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  6380. /* WaPsdDispatchEnable:vlv */
  6381. /* WaDisablePSDDualDispatchEnable:vlv */
  6382. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  6383. _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
  6384. GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  6385. /* WaDisable_RenderCache_OperationalFlush:vlv */
  6386. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  6387. /* WaForceL3Serialization:vlv */
  6388. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  6389. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  6390. /* WaDisableDopClockGating:vlv */
  6391. I915_WRITE(GEN7_ROW_CHICKEN2,
  6392. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  6393. /* This is required by WaCatErrorRejectionIssue:vlv */
  6394. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  6395. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  6396. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  6397. gen7_setup_fixed_func_scheduler(dev_priv);
  6398. /*
  6399. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  6400. * This implements the WaDisableRCZUnitClockGating:vlv workaround.
  6401. */
  6402. I915_WRITE(GEN6_UCGCTL2,
  6403. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  6404. /* WaDisableL3Bank2xClockGate:vlv
  6405. * Disabling L3 clock gating- MMIO 940c[25] = 1
  6406. * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
  6407. I915_WRITE(GEN7_UCGCTL4,
  6408. I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
  6409. /*
  6410. * BSpec says this must be set, even though
  6411. * WaDisable4x2SubspanOptimization isn't listed for VLV.
  6412. */
  6413. I915_WRITE(CACHE_MODE_1,
  6414. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  6415. /*
  6416. * BSpec recommends 8x4 when MSAA is used,
  6417. * however in practice 16x4 seems fastest.
  6418. *
  6419. * Note that PS/WM thread counts depend on the WIZ hashing
  6420. * disable bit, which we don't touch here, but it's good
  6421. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  6422. */
  6423. I915_WRITE(GEN7_GT_MODE,
  6424. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  6425. /*
  6426. * WaIncreaseL3CreditsForVLVB0:vlv
  6427. * This is the hardware default actually.
  6428. */
  6429. I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
  6430. /*
  6431. * WaDisableVLVClockGating_VBIIssue:vlv
  6432. * Disable clock gating on th GCFG unit to prevent a delay
  6433. * in the reporting of vblank events.
  6434. */
  6435. I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
  6436. }
  6437. static void cherryview_init_clock_gating(struct drm_i915_private *dev_priv)
  6438. {
  6439. /* WaVSRefCountFullforceMissDisable:chv */
  6440. /* WaDSRefCountFullforceMissDisable:chv */
  6441. I915_WRITE(GEN7_FF_THREAD_MODE,
  6442. I915_READ(GEN7_FF_THREAD_MODE) &
  6443. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  6444. /* WaDisableSemaphoreAndSyncFlipWait:chv */
  6445. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  6446. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  6447. /* WaDisableCSUnitClockGating:chv */
  6448. I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
  6449. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  6450. /* WaDisableSDEUnitClockGating:chv */
  6451. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  6452. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  6453. /*
  6454. * WaProgramL3SqcReg1Default:chv
  6455. * See gfxspecs/Related Documents/Performance Guide/
  6456. * LSQC Setting Recommendations.
  6457. */
  6458. gen8_set_l3sqc_credits(dev_priv, 38, 2);
  6459. /*
  6460. * GTT cache may not work with big pages, so if those
  6461. * are ever enabled GTT cache may need to be disabled.
  6462. */
  6463. I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
  6464. }
  6465. static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
  6466. {
  6467. uint32_t dspclk_gate;
  6468. I915_WRITE(RENCLK_GATE_D1, 0);
  6469. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  6470. GS_UNIT_CLOCK_GATE_DISABLE |
  6471. CL_UNIT_CLOCK_GATE_DISABLE);
  6472. I915_WRITE(RAMCLK_GATE_D, 0);
  6473. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  6474. OVRUNIT_CLOCK_GATE_DISABLE |
  6475. OVCUNIT_CLOCK_GATE_DISABLE;
  6476. if (IS_GM45(dev_priv))
  6477. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  6478. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  6479. /* WaDisableRenderCachePipelinedFlush */
  6480. I915_WRITE(CACHE_MODE_0,
  6481. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  6482. /* WaDisable_RenderCache_OperationalFlush:g4x */
  6483. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  6484. g4x_disable_trickle_feed(dev_priv);
  6485. }
  6486. static void crestline_init_clock_gating(struct drm_i915_private *dev_priv)
  6487. {
  6488. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  6489. I915_WRITE(RENCLK_GATE_D2, 0);
  6490. I915_WRITE(DSPCLK_GATE_D, 0);
  6491. I915_WRITE(RAMCLK_GATE_D, 0);
  6492. I915_WRITE16(DEUC, 0);
  6493. I915_WRITE(MI_ARB_STATE,
  6494. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  6495. /* WaDisable_RenderCache_OperationalFlush:gen4 */
  6496. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  6497. }
  6498. static void broadwater_init_clock_gating(struct drm_i915_private *dev_priv)
  6499. {
  6500. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  6501. I965_RCC_CLOCK_GATE_DISABLE |
  6502. I965_RCPB_CLOCK_GATE_DISABLE |
  6503. I965_ISC_CLOCK_GATE_DISABLE |
  6504. I965_FBC_CLOCK_GATE_DISABLE);
  6505. I915_WRITE(RENCLK_GATE_D2, 0);
  6506. I915_WRITE(MI_ARB_STATE,
  6507. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  6508. /* WaDisable_RenderCache_OperationalFlush:gen4 */
  6509. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  6510. }
  6511. static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
  6512. {
  6513. u32 dstate = I915_READ(D_STATE);
  6514. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  6515. DSTATE_DOT_CLOCK_GATING;
  6516. I915_WRITE(D_STATE, dstate);
  6517. if (IS_PINEVIEW(dev_priv))
  6518. I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
  6519. /* IIR "flip pending" means done if this bit is set */
  6520. I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
  6521. /* interrupts should cause a wake up from C3 */
  6522. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
  6523. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  6524. I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  6525. I915_WRITE(MI_ARB_STATE,
  6526. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  6527. }
  6528. static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
  6529. {
  6530. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  6531. /* interrupts should cause a wake up from C3 */
  6532. I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
  6533. _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
  6534. I915_WRITE(MEM_MODE,
  6535. _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
  6536. }
  6537. static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
  6538. {
  6539. I915_WRITE(MEM_MODE,
  6540. _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
  6541. _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
  6542. }
  6543. void intel_init_clock_gating(struct drm_i915_private *dev_priv)
  6544. {
  6545. dev_priv->display.init_clock_gating(dev_priv);
  6546. }
  6547. void intel_suspend_hw(struct drm_i915_private *dev_priv)
  6548. {
  6549. if (HAS_PCH_LPT(dev_priv))
  6550. lpt_suspend_hw(dev_priv);
  6551. }
  6552. static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
  6553. {
  6554. DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
  6555. }
  6556. /**
  6557. * intel_init_clock_gating_hooks - setup the clock gating hooks
  6558. * @dev_priv: device private
  6559. *
  6560. * Setup the hooks that configure which clocks of a given platform can be
  6561. * gated and also apply various GT and display specific workarounds for these
  6562. * platforms. Note that some GT specific workarounds are applied separately
  6563. * when GPU contexts or batchbuffers start their execution.
  6564. */
  6565. void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
  6566. {
  6567. if (IS_SKYLAKE(dev_priv))
  6568. dev_priv->display.init_clock_gating = skylake_init_clock_gating;
  6569. else if (IS_KABYLAKE(dev_priv))
  6570. dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
  6571. else if (IS_BROXTON(dev_priv))
  6572. dev_priv->display.init_clock_gating = bxt_init_clock_gating;
  6573. else if (IS_GEMINILAKE(dev_priv))
  6574. dev_priv->display.init_clock_gating = glk_init_clock_gating;
  6575. else if (IS_BROADWELL(dev_priv))
  6576. dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
  6577. else if (IS_CHERRYVIEW(dev_priv))
  6578. dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
  6579. else if (IS_HASWELL(dev_priv))
  6580. dev_priv->display.init_clock_gating = haswell_init_clock_gating;
  6581. else if (IS_IVYBRIDGE(dev_priv))
  6582. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  6583. else if (IS_VALLEYVIEW(dev_priv))
  6584. dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
  6585. else if (IS_GEN6(dev_priv))
  6586. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  6587. else if (IS_GEN5(dev_priv))
  6588. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  6589. else if (IS_G4X(dev_priv))
  6590. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  6591. else if (IS_I965GM(dev_priv))
  6592. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  6593. else if (IS_I965G(dev_priv))
  6594. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  6595. else if (IS_GEN3(dev_priv))
  6596. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  6597. else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
  6598. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  6599. else if (IS_GEN2(dev_priv))
  6600. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  6601. else {
  6602. MISSING_CASE(INTEL_DEVID(dev_priv));
  6603. dev_priv->display.init_clock_gating = nop_init_clock_gating;
  6604. }
  6605. }
  6606. /* Set up chip specific power management-related functions */
  6607. void intel_init_pm(struct drm_i915_private *dev_priv)
  6608. {
  6609. intel_fbc_init(dev_priv);
  6610. /* For cxsr */
  6611. if (IS_PINEVIEW(dev_priv))
  6612. i915_pineview_get_mem_freq(dev_priv);
  6613. else if (IS_GEN5(dev_priv))
  6614. i915_ironlake_get_mem_freq(dev_priv);
  6615. /* For FIFO watermark updates */
  6616. if (INTEL_GEN(dev_priv) >= 9) {
  6617. skl_setup_wm_latency(dev_priv);
  6618. dev_priv->display.initial_watermarks = skl_initial_wm;
  6619. dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
  6620. dev_priv->display.compute_global_watermarks = skl_compute_wm;
  6621. } else if (HAS_PCH_SPLIT(dev_priv)) {
  6622. ilk_setup_wm_latency(dev_priv);
  6623. if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
  6624. dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
  6625. (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
  6626. dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
  6627. dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
  6628. dev_priv->display.compute_intermediate_wm =
  6629. ilk_compute_intermediate_wm;
  6630. dev_priv->display.initial_watermarks =
  6631. ilk_initial_watermarks;
  6632. dev_priv->display.optimize_watermarks =
  6633. ilk_optimize_watermarks;
  6634. } else {
  6635. DRM_DEBUG_KMS("Failed to read display plane latency. "
  6636. "Disable CxSR\n");
  6637. }
  6638. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  6639. vlv_setup_wm_latency(dev_priv);
  6640. dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
  6641. dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
  6642. dev_priv->display.initial_watermarks = vlv_initial_watermarks;
  6643. dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
  6644. dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
  6645. } else if (IS_PINEVIEW(dev_priv)) {
  6646. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
  6647. dev_priv->is_ddr3,
  6648. dev_priv->fsb_freq,
  6649. dev_priv->mem_freq)) {
  6650. DRM_INFO("failed to find known CxSR latency "
  6651. "(found ddr%s fsb freq %d, mem freq %d), "
  6652. "disabling CxSR\n",
  6653. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  6654. dev_priv->fsb_freq, dev_priv->mem_freq);
  6655. /* Disable CxSR and never update its watermark again */
  6656. intel_set_memory_cxsr(dev_priv, false);
  6657. dev_priv->display.update_wm = NULL;
  6658. } else
  6659. dev_priv->display.update_wm = pineview_update_wm;
  6660. } else if (IS_G4X(dev_priv)) {
  6661. dev_priv->display.update_wm = g4x_update_wm;
  6662. } else if (IS_GEN4(dev_priv)) {
  6663. dev_priv->display.update_wm = i965_update_wm;
  6664. } else if (IS_GEN3(dev_priv)) {
  6665. dev_priv->display.update_wm = i9xx_update_wm;
  6666. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  6667. } else if (IS_GEN2(dev_priv)) {
  6668. if (INTEL_INFO(dev_priv)->num_pipes == 1) {
  6669. dev_priv->display.update_wm = i845_update_wm;
  6670. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  6671. } else {
  6672. dev_priv->display.update_wm = i9xx_update_wm;
  6673. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  6674. }
  6675. } else {
  6676. DRM_ERROR("unexpected fall-through in intel_init_pm\n");
  6677. }
  6678. }
  6679. static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
  6680. {
  6681. uint32_t flags =
  6682. I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
  6683. switch (flags) {
  6684. case GEN6_PCODE_SUCCESS:
  6685. return 0;
  6686. case GEN6_PCODE_UNIMPLEMENTED_CMD:
  6687. case GEN6_PCODE_ILLEGAL_CMD:
  6688. return -ENXIO;
  6689. case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
  6690. case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
  6691. return -EOVERFLOW;
  6692. case GEN6_PCODE_TIMEOUT:
  6693. return -ETIMEDOUT;
  6694. default:
  6695. MISSING_CASE(flags);
  6696. return 0;
  6697. }
  6698. }
  6699. static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
  6700. {
  6701. uint32_t flags =
  6702. I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
  6703. switch (flags) {
  6704. case GEN6_PCODE_SUCCESS:
  6705. return 0;
  6706. case GEN6_PCODE_ILLEGAL_CMD:
  6707. return -ENXIO;
  6708. case GEN7_PCODE_TIMEOUT:
  6709. return -ETIMEDOUT;
  6710. case GEN7_PCODE_ILLEGAL_DATA:
  6711. return -EINVAL;
  6712. case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
  6713. return -EOVERFLOW;
  6714. default:
  6715. MISSING_CASE(flags);
  6716. return 0;
  6717. }
  6718. }
  6719. int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
  6720. {
  6721. int status;
  6722. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  6723. /* GEN6_PCODE_* are outside of the forcewake domain, we can
  6724. * use te fw I915_READ variants to reduce the amount of work
  6725. * required when reading/writing.
  6726. */
  6727. if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  6728. DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
  6729. return -EAGAIN;
  6730. }
  6731. I915_WRITE_FW(GEN6_PCODE_DATA, *val);
  6732. I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
  6733. I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  6734. if (intel_wait_for_register_fw(dev_priv,
  6735. GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
  6736. 500)) {
  6737. DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
  6738. return -ETIMEDOUT;
  6739. }
  6740. *val = I915_READ_FW(GEN6_PCODE_DATA);
  6741. I915_WRITE_FW(GEN6_PCODE_DATA, 0);
  6742. if (INTEL_GEN(dev_priv) > 6)
  6743. status = gen7_check_mailbox_status(dev_priv);
  6744. else
  6745. status = gen6_check_mailbox_status(dev_priv);
  6746. if (status) {
  6747. DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
  6748. status);
  6749. return status;
  6750. }
  6751. return 0;
  6752. }
  6753. int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
  6754. u32 mbox, u32 val)
  6755. {
  6756. int status;
  6757. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  6758. /* GEN6_PCODE_* are outside of the forcewake domain, we can
  6759. * use te fw I915_READ variants to reduce the amount of work
  6760. * required when reading/writing.
  6761. */
  6762. if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  6763. DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
  6764. return -EAGAIN;
  6765. }
  6766. I915_WRITE_FW(GEN6_PCODE_DATA, val);
  6767. I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
  6768. I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  6769. if (intel_wait_for_register_fw(dev_priv,
  6770. GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
  6771. 500)) {
  6772. DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
  6773. return -ETIMEDOUT;
  6774. }
  6775. I915_WRITE_FW(GEN6_PCODE_DATA, 0);
  6776. if (INTEL_GEN(dev_priv) > 6)
  6777. status = gen7_check_mailbox_status(dev_priv);
  6778. else
  6779. status = gen6_check_mailbox_status(dev_priv);
  6780. if (status) {
  6781. DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
  6782. status);
  6783. return status;
  6784. }
  6785. return 0;
  6786. }
  6787. static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
  6788. u32 request, u32 reply_mask, u32 reply,
  6789. u32 *status)
  6790. {
  6791. u32 val = request;
  6792. *status = sandybridge_pcode_read(dev_priv, mbox, &val);
  6793. return *status || ((val & reply_mask) == reply);
  6794. }
  6795. /**
  6796. * skl_pcode_request - send PCODE request until acknowledgment
  6797. * @dev_priv: device private
  6798. * @mbox: PCODE mailbox ID the request is targeted for
  6799. * @request: request ID
  6800. * @reply_mask: mask used to check for request acknowledgment
  6801. * @reply: value used to check for request acknowledgment
  6802. * @timeout_base_ms: timeout for polling with preemption enabled
  6803. *
  6804. * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
  6805. * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
  6806. * The request is acknowledged once the PCODE reply dword equals @reply after
  6807. * applying @reply_mask. Polling is first attempted with preemption enabled
  6808. * for @timeout_base_ms and if this times out for another 50 ms with
  6809. * preemption disabled.
  6810. *
  6811. * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
  6812. * other error as reported by PCODE.
  6813. */
  6814. int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
  6815. u32 reply_mask, u32 reply, int timeout_base_ms)
  6816. {
  6817. u32 status;
  6818. int ret;
  6819. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  6820. #define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
  6821. &status)
  6822. /*
  6823. * Prime the PCODE by doing a request first. Normally it guarantees
  6824. * that a subsequent request, at most @timeout_base_ms later, succeeds.
  6825. * _wait_for() doesn't guarantee when its passed condition is evaluated
  6826. * first, so send the first request explicitly.
  6827. */
  6828. if (COND) {
  6829. ret = 0;
  6830. goto out;
  6831. }
  6832. ret = _wait_for(COND, timeout_base_ms * 1000, 10);
  6833. if (!ret)
  6834. goto out;
  6835. /*
  6836. * The above can time out if the number of requests was low (2 in the
  6837. * worst case) _and_ PCODE was busy for some reason even after a
  6838. * (queued) request and @timeout_base_ms delay. As a workaround retry
  6839. * the poll with preemption disabled to maximize the number of
  6840. * requests. Increase the timeout from @timeout_base_ms to 50ms to
  6841. * account for interrupts that could reduce the number of these
  6842. * requests, and for any quirks of the PCODE firmware that delays
  6843. * the request completion.
  6844. */
  6845. DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
  6846. WARN_ON_ONCE(timeout_base_ms > 3);
  6847. preempt_disable();
  6848. ret = wait_for_atomic(COND, 50);
  6849. preempt_enable();
  6850. out:
  6851. return ret ? ret : status;
  6852. #undef COND
  6853. }
  6854. static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
  6855. {
  6856. /*
  6857. * N = val - 0xb7
  6858. * Slow = Fast = GPLL ref * N
  6859. */
  6860. return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
  6861. }
  6862. static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
  6863. {
  6864. return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
  6865. }
  6866. static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
  6867. {
  6868. /*
  6869. * N = val / 2
  6870. * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
  6871. */
  6872. return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
  6873. }
  6874. static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
  6875. {
  6876. /* CHV needs even values */
  6877. return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
  6878. }
  6879. int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
  6880. {
  6881. if (IS_GEN9(dev_priv))
  6882. return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
  6883. GEN9_FREQ_SCALER);
  6884. else if (IS_CHERRYVIEW(dev_priv))
  6885. return chv_gpu_freq(dev_priv, val);
  6886. else if (IS_VALLEYVIEW(dev_priv))
  6887. return byt_gpu_freq(dev_priv, val);
  6888. else
  6889. return val * GT_FREQUENCY_MULTIPLIER;
  6890. }
  6891. int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
  6892. {
  6893. if (IS_GEN9(dev_priv))
  6894. return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
  6895. GT_FREQUENCY_MULTIPLIER);
  6896. else if (IS_CHERRYVIEW(dev_priv))
  6897. return chv_freq_opcode(dev_priv, val);
  6898. else if (IS_VALLEYVIEW(dev_priv))
  6899. return byt_freq_opcode(dev_priv, val);
  6900. else
  6901. return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
  6902. }
  6903. struct request_boost {
  6904. struct work_struct work;
  6905. struct drm_i915_gem_request *req;
  6906. };
  6907. static void __intel_rps_boost_work(struct work_struct *work)
  6908. {
  6909. struct request_boost *boost = container_of(work, struct request_boost, work);
  6910. struct drm_i915_gem_request *req = boost->req;
  6911. if (!i915_gem_request_completed(req))
  6912. gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
  6913. i915_gem_request_put(req);
  6914. kfree(boost);
  6915. }
  6916. void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
  6917. {
  6918. struct request_boost *boost;
  6919. if (req == NULL || INTEL_GEN(req->i915) < 6)
  6920. return;
  6921. if (i915_gem_request_completed(req))
  6922. return;
  6923. boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
  6924. if (boost == NULL)
  6925. return;
  6926. boost->req = i915_gem_request_get(req);
  6927. INIT_WORK(&boost->work, __intel_rps_boost_work);
  6928. queue_work(req->i915->wq, &boost->work);
  6929. }
  6930. void intel_pm_setup(struct drm_i915_private *dev_priv)
  6931. {
  6932. mutex_init(&dev_priv->rps.hw_lock);
  6933. spin_lock_init(&dev_priv->rps.client_lock);
  6934. INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
  6935. __intel_autoenable_gt_powersave);
  6936. INIT_LIST_HEAD(&dev_priv->rps.clients);
  6937. dev_priv->pm.suspended = false;
  6938. atomic_set(&dev_priv->pm.wakeref_count, 0);
  6939. }
  6940. static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
  6941. const i915_reg_t reg)
  6942. {
  6943. u32 lower, upper, tmp;
  6944. int loop = 2;
  6945. /* The register accessed do not need forcewake. We borrow
  6946. * uncore lock to prevent concurrent access to range reg.
  6947. */
  6948. spin_lock_irq(&dev_priv->uncore.lock);
  6949. /* vlv and chv residency counters are 40 bits in width.
  6950. * With a control bit, we can choose between upper or lower
  6951. * 32bit window into this counter.
  6952. *
  6953. * Although we always use the counter in high-range mode elsewhere,
  6954. * userspace may attempt to read the value before rc6 is initialised,
  6955. * before we have set the default VLV_COUNTER_CONTROL value. So always
  6956. * set the high bit to be safe.
  6957. */
  6958. I915_WRITE_FW(VLV_COUNTER_CONTROL,
  6959. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
  6960. upper = I915_READ_FW(reg);
  6961. do {
  6962. tmp = upper;
  6963. I915_WRITE_FW(VLV_COUNTER_CONTROL,
  6964. _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
  6965. lower = I915_READ_FW(reg);
  6966. I915_WRITE_FW(VLV_COUNTER_CONTROL,
  6967. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
  6968. upper = I915_READ_FW(reg);
  6969. } while (upper != tmp && --loop);
  6970. /* Everywhere else we always use VLV_COUNTER_CONTROL with the
  6971. * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
  6972. * now.
  6973. */
  6974. spin_unlock_irq(&dev_priv->uncore.lock);
  6975. return lower | (u64)upper << 8;
  6976. }
  6977. u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
  6978. const i915_reg_t reg)
  6979. {
  6980. u64 time_hw, units, div;
  6981. if (!intel_enable_rc6())
  6982. return 0;
  6983. intel_runtime_pm_get(dev_priv);
  6984. /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
  6985. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  6986. units = 1000;
  6987. div = dev_priv->czclk_freq;
  6988. time_hw = vlv_residency_raw(dev_priv, reg);
  6989. } else if (IS_GEN9_LP(dev_priv)) {
  6990. units = 1000;
  6991. div = 1200; /* 833.33ns */
  6992. time_hw = I915_READ(reg);
  6993. } else {
  6994. units = 128000; /* 1.28us */
  6995. div = 100000;
  6996. time_hw = I915_READ(reg);
  6997. }
  6998. intel_runtime_pm_put(dev_priv);
  6999. return DIV_ROUND_UP_ULL(time_hw * units, div);
  7000. }