intel_pm.c 206 KB

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