intel_pm.c 218 KB

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