intel_pm.c 269 KB

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