intel_pm.c 204 KB

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