intel_pm.c 205 KB

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