intel_pm.c 266 KB

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