intel_pm.c 225 KB

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