intel_pm.c 271 KB

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