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