intel_pm.c 224 KB

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