intel_pm.c 205 KB

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