intel_pm.c 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484
  1. /*
  2. * Copyright © 2012 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eugeni Dodonov <eugeni.dodonov@intel.com>
  25. *
  26. */
  27. #include <linux/cpufreq.h>
  28. #include "i915_drv.h"
  29. #include "intel_drv.h"
  30. #include "../../../platform/x86/intel_ips.h"
  31. #include <linux/module.h>
  32. #include <drm/i915_powerwell.h>
  33. /* FBC, or Frame Buffer Compression, is a technique employed to compress the
  34. * framebuffer contents in-memory, aiming at reducing the required bandwidth
  35. * during in-memory transfers and, therefore, reduce the power packet.
  36. *
  37. * The benefits of FBC are mostly visible with solid backgrounds and
  38. * variation-less patterns.
  39. *
  40. * FBC-related functionality can be enabled by the means of the
  41. * i915.i915_enable_fbc parameter
  42. */
  43. static bool intel_crtc_active(struct drm_crtc *crtc)
  44. {
  45. /* Be paranoid as we can arrive here with only partial
  46. * state retrieved from the hardware during setup.
  47. */
  48. return to_intel_crtc(crtc)->active && crtc->fb && crtc->mode.clock;
  49. }
  50. static void i8xx_disable_fbc(struct drm_device *dev)
  51. {
  52. struct drm_i915_private *dev_priv = dev->dev_private;
  53. u32 fbc_ctl;
  54. /* Disable compression */
  55. fbc_ctl = I915_READ(FBC_CONTROL);
  56. if ((fbc_ctl & FBC_CTL_EN) == 0)
  57. return;
  58. fbc_ctl &= ~FBC_CTL_EN;
  59. I915_WRITE(FBC_CONTROL, fbc_ctl);
  60. /* Wait for compressing bit to clear */
  61. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  62. DRM_DEBUG_KMS("FBC idle timed out\n");
  63. return;
  64. }
  65. DRM_DEBUG_KMS("disabled FBC\n");
  66. }
  67. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  68. {
  69. struct drm_device *dev = crtc->dev;
  70. struct drm_i915_private *dev_priv = dev->dev_private;
  71. struct drm_framebuffer *fb = crtc->fb;
  72. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  73. struct drm_i915_gem_object *obj = intel_fb->obj;
  74. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  75. int cfb_pitch;
  76. int plane, i;
  77. u32 fbc_ctl, fbc_ctl2;
  78. cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
  79. if (fb->pitches[0] < cfb_pitch)
  80. cfb_pitch = fb->pitches[0];
  81. /* FBC_CTL wants 64B units */
  82. cfb_pitch = (cfb_pitch / 64) - 1;
  83. plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  84. /* Clear old tags */
  85. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  86. I915_WRITE(FBC_TAG + (i * 4), 0);
  87. /* Set it up... */
  88. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  89. fbc_ctl2 |= plane;
  90. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  91. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  92. /* enable it... */
  93. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  94. if (IS_I945GM(dev))
  95. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  96. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  97. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  98. fbc_ctl |= obj->fence_reg;
  99. I915_WRITE(FBC_CONTROL, fbc_ctl);
  100. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c, ",
  101. cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
  102. }
  103. static bool i8xx_fbc_enabled(struct drm_device *dev)
  104. {
  105. struct drm_i915_private *dev_priv = dev->dev_private;
  106. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  107. }
  108. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  109. {
  110. struct drm_device *dev = crtc->dev;
  111. struct drm_i915_private *dev_priv = dev->dev_private;
  112. struct drm_framebuffer *fb = crtc->fb;
  113. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  114. struct drm_i915_gem_object *obj = intel_fb->obj;
  115. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  116. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  117. unsigned long stall_watermark = 200;
  118. u32 dpfc_ctl;
  119. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  120. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  121. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  122. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  123. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  124. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  125. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  126. /* enable it... */
  127. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  128. DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
  129. }
  130. static void g4x_disable_fbc(struct drm_device *dev)
  131. {
  132. struct drm_i915_private *dev_priv = dev->dev_private;
  133. u32 dpfc_ctl;
  134. /* Disable compression */
  135. dpfc_ctl = I915_READ(DPFC_CONTROL);
  136. if (dpfc_ctl & DPFC_CTL_EN) {
  137. dpfc_ctl &= ~DPFC_CTL_EN;
  138. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  139. DRM_DEBUG_KMS("disabled FBC\n");
  140. }
  141. }
  142. static bool g4x_fbc_enabled(struct drm_device *dev)
  143. {
  144. struct drm_i915_private *dev_priv = dev->dev_private;
  145. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  146. }
  147. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  148. {
  149. struct drm_i915_private *dev_priv = dev->dev_private;
  150. u32 blt_ecoskpd;
  151. /* Make sure blitter notifies FBC of writes */
  152. gen6_gt_force_wake_get(dev_priv);
  153. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  154. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  155. GEN6_BLITTER_LOCK_SHIFT;
  156. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  157. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  158. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  159. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  160. GEN6_BLITTER_LOCK_SHIFT);
  161. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  162. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  163. gen6_gt_force_wake_put(dev_priv);
  164. }
  165. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  166. {
  167. struct drm_device *dev = crtc->dev;
  168. struct drm_i915_private *dev_priv = dev->dev_private;
  169. struct drm_framebuffer *fb = crtc->fb;
  170. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  171. struct drm_i915_gem_object *obj = intel_fb->obj;
  172. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  173. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  174. unsigned long stall_watermark = 200;
  175. u32 dpfc_ctl;
  176. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  177. dpfc_ctl &= DPFC_RESERVED;
  178. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  179. /* Set persistent mode for front-buffer rendering, ala X. */
  180. dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
  181. dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
  182. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  183. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  184. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  185. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  186. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  187. I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
  188. /* enable it... */
  189. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  190. if (IS_GEN6(dev)) {
  191. I915_WRITE(SNB_DPFC_CTL_SA,
  192. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  193. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  194. sandybridge_blit_fbc_update(dev);
  195. }
  196. DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
  197. }
  198. static void ironlake_disable_fbc(struct drm_device *dev)
  199. {
  200. struct drm_i915_private *dev_priv = dev->dev_private;
  201. u32 dpfc_ctl;
  202. /* Disable compression */
  203. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  204. if (dpfc_ctl & DPFC_CTL_EN) {
  205. dpfc_ctl &= ~DPFC_CTL_EN;
  206. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  207. if (IS_IVYBRIDGE(dev))
  208. /* WaFbcDisableDpfcClockGating:ivb */
  209. I915_WRITE(ILK_DSPCLK_GATE_D,
  210. I915_READ(ILK_DSPCLK_GATE_D) &
  211. ~ILK_DPFCUNIT_CLOCK_GATE_DISABLE);
  212. if (IS_HASWELL(dev))
  213. /* WaFbcDisableDpfcClockGating:hsw */
  214. I915_WRITE(HSW_CLKGATE_DISABLE_PART_1,
  215. I915_READ(HSW_CLKGATE_DISABLE_PART_1) &
  216. ~HSW_DPFC_GATING_DISABLE);
  217. DRM_DEBUG_KMS("disabled FBC\n");
  218. }
  219. }
  220. static bool ironlake_fbc_enabled(struct drm_device *dev)
  221. {
  222. struct drm_i915_private *dev_priv = dev->dev_private;
  223. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  224. }
  225. static void gen7_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  226. {
  227. struct drm_device *dev = crtc->dev;
  228. struct drm_i915_private *dev_priv = dev->dev_private;
  229. struct drm_framebuffer *fb = crtc->fb;
  230. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  231. struct drm_i915_gem_object *obj = intel_fb->obj;
  232. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  233. I915_WRITE(IVB_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj));
  234. I915_WRITE(ILK_DPFC_CONTROL, DPFC_CTL_EN | DPFC_CTL_LIMIT_1X |
  235. IVB_DPFC_CTL_FENCE_EN |
  236. intel_crtc->plane << IVB_DPFC_CTL_PLANE_SHIFT);
  237. if (IS_IVYBRIDGE(dev)) {
  238. /* WaFbcAsynchFlipDisableFbcQueue:ivb */
  239. I915_WRITE(ILK_DISPLAY_CHICKEN1, ILK_FBCQ_DIS);
  240. /* WaFbcDisableDpfcClockGating:ivb */
  241. I915_WRITE(ILK_DSPCLK_GATE_D,
  242. I915_READ(ILK_DSPCLK_GATE_D) |
  243. ILK_DPFCUNIT_CLOCK_GATE_DISABLE);
  244. } else {
  245. /* WaFbcAsynchFlipDisableFbcQueue:hsw */
  246. I915_WRITE(HSW_PIPE_SLICE_CHICKEN_1(intel_crtc->pipe),
  247. HSW_BYPASS_FBC_QUEUE);
  248. /* WaFbcDisableDpfcClockGating:hsw */
  249. I915_WRITE(HSW_CLKGATE_DISABLE_PART_1,
  250. I915_READ(HSW_CLKGATE_DISABLE_PART_1) |
  251. HSW_DPFC_GATING_DISABLE);
  252. }
  253. I915_WRITE(SNB_DPFC_CTL_SA,
  254. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  255. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  256. sandybridge_blit_fbc_update(dev);
  257. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  258. }
  259. bool intel_fbc_enabled(struct drm_device *dev)
  260. {
  261. struct drm_i915_private *dev_priv = dev->dev_private;
  262. if (!dev_priv->display.fbc_enabled)
  263. return false;
  264. return dev_priv->display.fbc_enabled(dev);
  265. }
  266. static void intel_fbc_work_fn(struct work_struct *__work)
  267. {
  268. struct intel_fbc_work *work =
  269. container_of(to_delayed_work(__work),
  270. struct intel_fbc_work, work);
  271. struct drm_device *dev = work->crtc->dev;
  272. struct drm_i915_private *dev_priv = dev->dev_private;
  273. mutex_lock(&dev->struct_mutex);
  274. if (work == dev_priv->fbc.fbc_work) {
  275. /* Double check that we haven't switched fb without cancelling
  276. * the prior work.
  277. */
  278. if (work->crtc->fb == work->fb) {
  279. dev_priv->display.enable_fbc(work->crtc,
  280. work->interval);
  281. dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
  282. dev_priv->fbc.fb_id = work->crtc->fb->base.id;
  283. dev_priv->fbc.y = work->crtc->y;
  284. }
  285. dev_priv->fbc.fbc_work = NULL;
  286. }
  287. mutex_unlock(&dev->struct_mutex);
  288. kfree(work);
  289. }
  290. static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
  291. {
  292. if (dev_priv->fbc.fbc_work == NULL)
  293. return;
  294. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  295. /* Synchronisation is provided by struct_mutex and checking of
  296. * dev_priv->fbc.fbc_work, so we can perform the cancellation
  297. * entirely asynchronously.
  298. */
  299. if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
  300. /* tasklet was killed before being run, clean up */
  301. kfree(dev_priv->fbc.fbc_work);
  302. /* Mark the work as no longer wanted so that if it does
  303. * wake-up (because the work was already running and waiting
  304. * for our mutex), it will discover that is no longer
  305. * necessary to run.
  306. */
  307. dev_priv->fbc.fbc_work = NULL;
  308. }
  309. static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  310. {
  311. struct intel_fbc_work *work;
  312. struct drm_device *dev = crtc->dev;
  313. struct drm_i915_private *dev_priv = dev->dev_private;
  314. if (!dev_priv->display.enable_fbc)
  315. return;
  316. intel_cancel_fbc_work(dev_priv);
  317. work = kzalloc(sizeof *work, GFP_KERNEL);
  318. if (work == NULL) {
  319. DRM_ERROR("Failed to allocate FBC work structure\n");
  320. dev_priv->display.enable_fbc(crtc, interval);
  321. return;
  322. }
  323. work->crtc = crtc;
  324. work->fb = crtc->fb;
  325. work->interval = interval;
  326. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  327. dev_priv->fbc.fbc_work = work;
  328. /* Delay the actual enabling to let pageflipping cease and the
  329. * display to settle before starting the compression. Note that
  330. * this delay also serves a second purpose: it allows for a
  331. * vblank to pass after disabling the FBC before we attempt
  332. * to modify the control registers.
  333. *
  334. * A more complicated solution would involve tracking vblanks
  335. * following the termination of the page-flipping sequence
  336. * and indeed performing the enable as a co-routine and not
  337. * waiting synchronously upon the vblank.
  338. *
  339. * WaFbcWaitForVBlankBeforeEnable:ilk,snb
  340. */
  341. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  342. }
  343. void intel_disable_fbc(struct drm_device *dev)
  344. {
  345. struct drm_i915_private *dev_priv = dev->dev_private;
  346. intel_cancel_fbc_work(dev_priv);
  347. if (!dev_priv->display.disable_fbc)
  348. return;
  349. dev_priv->display.disable_fbc(dev);
  350. dev_priv->fbc.plane = -1;
  351. }
  352. static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
  353. enum no_fbc_reason reason)
  354. {
  355. if (dev_priv->fbc.no_fbc_reason == reason)
  356. return false;
  357. dev_priv->fbc.no_fbc_reason = reason;
  358. return true;
  359. }
  360. /**
  361. * intel_update_fbc - enable/disable FBC as needed
  362. * @dev: the drm_device
  363. *
  364. * Set up the framebuffer compression hardware at mode set time. We
  365. * enable it if possible:
  366. * - plane A only (on pre-965)
  367. * - no pixel mulitply/line duplication
  368. * - no alpha buffer discard
  369. * - no dual wide
  370. * - framebuffer <= max_hdisplay in width, max_vdisplay in height
  371. *
  372. * We can't assume that any compression will take place (worst case),
  373. * so the compressed buffer has to be the same size as the uncompressed
  374. * one. It also must reside (along with the line length buffer) in
  375. * stolen memory.
  376. *
  377. * We need to enable/disable FBC on a global basis.
  378. */
  379. void intel_update_fbc(struct drm_device *dev)
  380. {
  381. struct drm_i915_private *dev_priv = dev->dev_private;
  382. struct drm_crtc *crtc = NULL, *tmp_crtc;
  383. struct intel_crtc *intel_crtc;
  384. struct drm_framebuffer *fb;
  385. struct intel_framebuffer *intel_fb;
  386. struct drm_i915_gem_object *obj;
  387. unsigned int max_hdisplay, max_vdisplay;
  388. if (!I915_HAS_FBC(dev)) {
  389. set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
  390. return;
  391. }
  392. if (!i915_powersave) {
  393. if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
  394. DRM_DEBUG_KMS("fbc disabled per module param\n");
  395. return;
  396. }
  397. /*
  398. * If FBC is already on, we just have to verify that we can
  399. * keep it that way...
  400. * Need to disable if:
  401. * - more than one pipe is active
  402. * - changing FBC params (stride, fence, mode)
  403. * - new fb is too large to fit in compressed buffer
  404. * - going to an unsupported config (interlace, pixel multiply, etc.)
  405. */
  406. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  407. if (intel_crtc_active(tmp_crtc) &&
  408. !to_intel_crtc(tmp_crtc)->primary_disabled) {
  409. if (crtc) {
  410. if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
  411. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  412. goto out_disable;
  413. }
  414. crtc = tmp_crtc;
  415. }
  416. }
  417. if (!crtc || crtc->fb == NULL) {
  418. if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
  419. DRM_DEBUG_KMS("no output, disabling\n");
  420. goto out_disable;
  421. }
  422. intel_crtc = to_intel_crtc(crtc);
  423. fb = crtc->fb;
  424. intel_fb = to_intel_framebuffer(fb);
  425. obj = intel_fb->obj;
  426. if (i915_enable_fbc < 0 &&
  427. INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev)) {
  428. if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
  429. DRM_DEBUG_KMS("disabled per chip default\n");
  430. goto out_disable;
  431. }
  432. if (!i915_enable_fbc) {
  433. if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
  434. DRM_DEBUG_KMS("fbc disabled per module param\n");
  435. goto out_disable;
  436. }
  437. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  438. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  439. if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
  440. DRM_DEBUG_KMS("mode incompatible with compression, "
  441. "disabling\n");
  442. goto out_disable;
  443. }
  444. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  445. max_hdisplay = 4096;
  446. max_vdisplay = 2048;
  447. } else {
  448. max_hdisplay = 2048;
  449. max_vdisplay = 1536;
  450. }
  451. if ((crtc->mode.hdisplay > max_hdisplay) ||
  452. (crtc->mode.vdisplay > max_vdisplay)) {
  453. if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
  454. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  455. goto out_disable;
  456. }
  457. if ((IS_I915GM(dev) || IS_I945GM(dev) || IS_HASWELL(dev)) &&
  458. intel_crtc->plane != 0) {
  459. if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
  460. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  461. goto out_disable;
  462. }
  463. /* The use of a CPU fence is mandatory in order to detect writes
  464. * by the CPU to the scanout and trigger updates to the FBC.
  465. */
  466. if (obj->tiling_mode != I915_TILING_X ||
  467. obj->fence_reg == I915_FENCE_REG_NONE) {
  468. if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
  469. DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
  470. goto out_disable;
  471. }
  472. /* If the kernel debugger is active, always disable compression */
  473. if (in_dbg_master())
  474. goto out_disable;
  475. if (i915_gem_stolen_setup_compression(dev, intel_fb->obj->base.size)) {
  476. if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
  477. DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
  478. goto out_disable;
  479. }
  480. /* If the scanout has not changed, don't modify the FBC settings.
  481. * Note that we make the fundamental assumption that the fb->obj
  482. * cannot be unpinned (and have its GTT offset and fence revoked)
  483. * without first being decoupled from the scanout and FBC disabled.
  484. */
  485. if (dev_priv->fbc.plane == intel_crtc->plane &&
  486. dev_priv->fbc.fb_id == fb->base.id &&
  487. dev_priv->fbc.y == crtc->y)
  488. return;
  489. if (intel_fbc_enabled(dev)) {
  490. /* We update FBC along two paths, after changing fb/crtc
  491. * configuration (modeswitching) and after page-flipping
  492. * finishes. For the latter, we know that not only did
  493. * we disable the FBC at the start of the page-flip
  494. * sequence, but also more than one vblank has passed.
  495. *
  496. * For the former case of modeswitching, it is possible
  497. * to switch between two FBC valid configurations
  498. * instantaneously so we do need to disable the FBC
  499. * before we can modify its control registers. We also
  500. * have to wait for the next vblank for that to take
  501. * effect. However, since we delay enabling FBC we can
  502. * assume that a vblank has passed since disabling and
  503. * that we can safely alter the registers in the deferred
  504. * callback.
  505. *
  506. * In the scenario that we go from a valid to invalid
  507. * and then back to valid FBC configuration we have
  508. * no strict enforcement that a vblank occurred since
  509. * disabling the FBC. However, along all current pipe
  510. * disabling paths we do need to wait for a vblank at
  511. * some point. And we wait before enabling FBC anyway.
  512. */
  513. DRM_DEBUG_KMS("disabling active FBC for update\n");
  514. intel_disable_fbc(dev);
  515. }
  516. intel_enable_fbc(crtc, 500);
  517. dev_priv->fbc.no_fbc_reason = FBC_OK;
  518. return;
  519. out_disable:
  520. /* Multiple disables should be harmless */
  521. if (intel_fbc_enabled(dev)) {
  522. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  523. intel_disable_fbc(dev);
  524. }
  525. i915_gem_stolen_cleanup_compression(dev);
  526. }
  527. static void i915_pineview_get_mem_freq(struct drm_device *dev)
  528. {
  529. drm_i915_private_t *dev_priv = dev->dev_private;
  530. u32 tmp;
  531. tmp = I915_READ(CLKCFG);
  532. switch (tmp & CLKCFG_FSB_MASK) {
  533. case CLKCFG_FSB_533:
  534. dev_priv->fsb_freq = 533; /* 133*4 */
  535. break;
  536. case CLKCFG_FSB_800:
  537. dev_priv->fsb_freq = 800; /* 200*4 */
  538. break;
  539. case CLKCFG_FSB_667:
  540. dev_priv->fsb_freq = 667; /* 167*4 */
  541. break;
  542. case CLKCFG_FSB_400:
  543. dev_priv->fsb_freq = 400; /* 100*4 */
  544. break;
  545. }
  546. switch (tmp & CLKCFG_MEM_MASK) {
  547. case CLKCFG_MEM_533:
  548. dev_priv->mem_freq = 533;
  549. break;
  550. case CLKCFG_MEM_667:
  551. dev_priv->mem_freq = 667;
  552. break;
  553. case CLKCFG_MEM_800:
  554. dev_priv->mem_freq = 800;
  555. break;
  556. }
  557. /* detect pineview DDR3 setting */
  558. tmp = I915_READ(CSHRDDR3CTL);
  559. dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
  560. }
  561. static void i915_ironlake_get_mem_freq(struct drm_device *dev)
  562. {
  563. drm_i915_private_t *dev_priv = dev->dev_private;
  564. u16 ddrpll, csipll;
  565. ddrpll = I915_READ16(DDRMPLL1);
  566. csipll = I915_READ16(CSIPLL0);
  567. switch (ddrpll & 0xff) {
  568. case 0xc:
  569. dev_priv->mem_freq = 800;
  570. break;
  571. case 0x10:
  572. dev_priv->mem_freq = 1066;
  573. break;
  574. case 0x14:
  575. dev_priv->mem_freq = 1333;
  576. break;
  577. case 0x18:
  578. dev_priv->mem_freq = 1600;
  579. break;
  580. default:
  581. DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
  582. ddrpll & 0xff);
  583. dev_priv->mem_freq = 0;
  584. break;
  585. }
  586. dev_priv->ips.r_t = dev_priv->mem_freq;
  587. switch (csipll & 0x3ff) {
  588. case 0x00c:
  589. dev_priv->fsb_freq = 3200;
  590. break;
  591. case 0x00e:
  592. dev_priv->fsb_freq = 3733;
  593. break;
  594. case 0x010:
  595. dev_priv->fsb_freq = 4266;
  596. break;
  597. case 0x012:
  598. dev_priv->fsb_freq = 4800;
  599. break;
  600. case 0x014:
  601. dev_priv->fsb_freq = 5333;
  602. break;
  603. case 0x016:
  604. dev_priv->fsb_freq = 5866;
  605. break;
  606. case 0x018:
  607. dev_priv->fsb_freq = 6400;
  608. break;
  609. default:
  610. DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
  611. csipll & 0x3ff);
  612. dev_priv->fsb_freq = 0;
  613. break;
  614. }
  615. if (dev_priv->fsb_freq == 3200) {
  616. dev_priv->ips.c_m = 0;
  617. } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
  618. dev_priv->ips.c_m = 1;
  619. } else {
  620. dev_priv->ips.c_m = 2;
  621. }
  622. }
  623. static const struct cxsr_latency cxsr_latency_table[] = {
  624. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  625. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  626. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  627. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  628. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  629. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  630. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  631. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  632. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  633. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  634. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  635. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  636. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  637. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  638. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  639. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  640. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  641. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  642. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  643. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  644. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  645. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  646. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  647. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  648. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  649. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  650. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  651. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  652. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  653. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  654. };
  655. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  656. int is_ddr3,
  657. int fsb,
  658. int mem)
  659. {
  660. const struct cxsr_latency *latency;
  661. int i;
  662. if (fsb == 0 || mem == 0)
  663. return NULL;
  664. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  665. latency = &cxsr_latency_table[i];
  666. if (is_desktop == latency->is_desktop &&
  667. is_ddr3 == latency->is_ddr3 &&
  668. fsb == latency->fsb_freq && mem == latency->mem_freq)
  669. return latency;
  670. }
  671. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  672. return NULL;
  673. }
  674. static void pineview_disable_cxsr(struct drm_device *dev)
  675. {
  676. struct drm_i915_private *dev_priv = dev->dev_private;
  677. /* deactivate cxsr */
  678. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  679. }
  680. /*
  681. * Latency for FIFO fetches is dependent on several factors:
  682. * - memory configuration (speed, channels)
  683. * - chipset
  684. * - current MCH state
  685. * It can be fairly high in some situations, so here we assume a fairly
  686. * pessimal value. It's a tradeoff between extra memory fetches (if we
  687. * set this value too high, the FIFO will fetch frequently to stay full)
  688. * and power consumption (set it too low to save power and we might see
  689. * FIFO underruns and display "flicker").
  690. *
  691. * A value of 5us seems to be a good balance; safe for very low end
  692. * platforms but not overly aggressive on lower latency configs.
  693. */
  694. static const int latency_ns = 5000;
  695. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  696. {
  697. struct drm_i915_private *dev_priv = dev->dev_private;
  698. uint32_t dsparb = I915_READ(DSPARB);
  699. int size;
  700. size = dsparb & 0x7f;
  701. if (plane)
  702. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  703. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  704. plane ? "B" : "A", size);
  705. return size;
  706. }
  707. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  708. {
  709. struct drm_i915_private *dev_priv = dev->dev_private;
  710. uint32_t dsparb = I915_READ(DSPARB);
  711. int size;
  712. size = dsparb & 0x1ff;
  713. if (plane)
  714. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  715. size >>= 1; /* Convert to cachelines */
  716. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  717. plane ? "B" : "A", size);
  718. return size;
  719. }
  720. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  721. {
  722. struct drm_i915_private *dev_priv = dev->dev_private;
  723. uint32_t dsparb = I915_READ(DSPARB);
  724. int size;
  725. size = dsparb & 0x7f;
  726. size >>= 2; /* Convert to cachelines */
  727. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  728. plane ? "B" : "A",
  729. size);
  730. return size;
  731. }
  732. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  733. {
  734. struct drm_i915_private *dev_priv = dev->dev_private;
  735. uint32_t dsparb = I915_READ(DSPARB);
  736. int size;
  737. size = dsparb & 0x7f;
  738. size >>= 1; /* Convert to cachelines */
  739. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  740. plane ? "B" : "A", size);
  741. return size;
  742. }
  743. /* Pineview has different values for various configs */
  744. static const struct intel_watermark_params pineview_display_wm = {
  745. PINEVIEW_DISPLAY_FIFO,
  746. PINEVIEW_MAX_WM,
  747. PINEVIEW_DFT_WM,
  748. PINEVIEW_GUARD_WM,
  749. PINEVIEW_FIFO_LINE_SIZE
  750. };
  751. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  752. PINEVIEW_DISPLAY_FIFO,
  753. PINEVIEW_MAX_WM,
  754. PINEVIEW_DFT_HPLLOFF_WM,
  755. PINEVIEW_GUARD_WM,
  756. PINEVIEW_FIFO_LINE_SIZE
  757. };
  758. static const struct intel_watermark_params pineview_cursor_wm = {
  759. PINEVIEW_CURSOR_FIFO,
  760. PINEVIEW_CURSOR_MAX_WM,
  761. PINEVIEW_CURSOR_DFT_WM,
  762. PINEVIEW_CURSOR_GUARD_WM,
  763. PINEVIEW_FIFO_LINE_SIZE,
  764. };
  765. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  766. PINEVIEW_CURSOR_FIFO,
  767. PINEVIEW_CURSOR_MAX_WM,
  768. PINEVIEW_CURSOR_DFT_WM,
  769. PINEVIEW_CURSOR_GUARD_WM,
  770. PINEVIEW_FIFO_LINE_SIZE
  771. };
  772. static const struct intel_watermark_params g4x_wm_info = {
  773. G4X_FIFO_SIZE,
  774. G4X_MAX_WM,
  775. G4X_MAX_WM,
  776. 2,
  777. G4X_FIFO_LINE_SIZE,
  778. };
  779. static const struct intel_watermark_params g4x_cursor_wm_info = {
  780. I965_CURSOR_FIFO,
  781. I965_CURSOR_MAX_WM,
  782. I965_CURSOR_DFT_WM,
  783. 2,
  784. G4X_FIFO_LINE_SIZE,
  785. };
  786. static const struct intel_watermark_params valleyview_wm_info = {
  787. VALLEYVIEW_FIFO_SIZE,
  788. VALLEYVIEW_MAX_WM,
  789. VALLEYVIEW_MAX_WM,
  790. 2,
  791. G4X_FIFO_LINE_SIZE,
  792. };
  793. static const struct intel_watermark_params valleyview_cursor_wm_info = {
  794. I965_CURSOR_FIFO,
  795. VALLEYVIEW_CURSOR_MAX_WM,
  796. I965_CURSOR_DFT_WM,
  797. 2,
  798. G4X_FIFO_LINE_SIZE,
  799. };
  800. static const struct intel_watermark_params i965_cursor_wm_info = {
  801. I965_CURSOR_FIFO,
  802. I965_CURSOR_MAX_WM,
  803. I965_CURSOR_DFT_WM,
  804. 2,
  805. I915_FIFO_LINE_SIZE,
  806. };
  807. static const struct intel_watermark_params i945_wm_info = {
  808. I945_FIFO_SIZE,
  809. I915_MAX_WM,
  810. 1,
  811. 2,
  812. I915_FIFO_LINE_SIZE
  813. };
  814. static const struct intel_watermark_params i915_wm_info = {
  815. I915_FIFO_SIZE,
  816. I915_MAX_WM,
  817. 1,
  818. 2,
  819. I915_FIFO_LINE_SIZE
  820. };
  821. static const struct intel_watermark_params i855_wm_info = {
  822. I855GM_FIFO_SIZE,
  823. I915_MAX_WM,
  824. 1,
  825. 2,
  826. I830_FIFO_LINE_SIZE
  827. };
  828. static const struct intel_watermark_params i830_wm_info = {
  829. I830_FIFO_SIZE,
  830. I915_MAX_WM,
  831. 1,
  832. 2,
  833. I830_FIFO_LINE_SIZE
  834. };
  835. static const struct intel_watermark_params ironlake_display_wm_info = {
  836. ILK_DISPLAY_FIFO,
  837. ILK_DISPLAY_MAXWM,
  838. ILK_DISPLAY_DFTWM,
  839. 2,
  840. ILK_FIFO_LINE_SIZE
  841. };
  842. static const struct intel_watermark_params ironlake_cursor_wm_info = {
  843. ILK_CURSOR_FIFO,
  844. ILK_CURSOR_MAXWM,
  845. ILK_CURSOR_DFTWM,
  846. 2,
  847. ILK_FIFO_LINE_SIZE
  848. };
  849. static const struct intel_watermark_params ironlake_display_srwm_info = {
  850. ILK_DISPLAY_SR_FIFO,
  851. ILK_DISPLAY_MAX_SRWM,
  852. ILK_DISPLAY_DFT_SRWM,
  853. 2,
  854. ILK_FIFO_LINE_SIZE
  855. };
  856. static const struct intel_watermark_params ironlake_cursor_srwm_info = {
  857. ILK_CURSOR_SR_FIFO,
  858. ILK_CURSOR_MAX_SRWM,
  859. ILK_CURSOR_DFT_SRWM,
  860. 2,
  861. ILK_FIFO_LINE_SIZE
  862. };
  863. static const struct intel_watermark_params sandybridge_display_wm_info = {
  864. SNB_DISPLAY_FIFO,
  865. SNB_DISPLAY_MAXWM,
  866. SNB_DISPLAY_DFTWM,
  867. 2,
  868. SNB_FIFO_LINE_SIZE
  869. };
  870. static const struct intel_watermark_params sandybridge_cursor_wm_info = {
  871. SNB_CURSOR_FIFO,
  872. SNB_CURSOR_MAXWM,
  873. SNB_CURSOR_DFTWM,
  874. 2,
  875. SNB_FIFO_LINE_SIZE
  876. };
  877. static const struct intel_watermark_params sandybridge_display_srwm_info = {
  878. SNB_DISPLAY_SR_FIFO,
  879. SNB_DISPLAY_MAX_SRWM,
  880. SNB_DISPLAY_DFT_SRWM,
  881. 2,
  882. SNB_FIFO_LINE_SIZE
  883. };
  884. static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
  885. SNB_CURSOR_SR_FIFO,
  886. SNB_CURSOR_MAX_SRWM,
  887. SNB_CURSOR_DFT_SRWM,
  888. 2,
  889. SNB_FIFO_LINE_SIZE
  890. };
  891. /**
  892. * intel_calculate_wm - calculate watermark level
  893. * @clock_in_khz: pixel clock
  894. * @wm: chip FIFO params
  895. * @pixel_size: display pixel size
  896. * @latency_ns: memory latency for the platform
  897. *
  898. * Calculate the watermark level (the level at which the display plane will
  899. * start fetching from memory again). Each chip has a different display
  900. * FIFO size and allocation, so the caller needs to figure that out and pass
  901. * in the correct intel_watermark_params structure.
  902. *
  903. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  904. * on the pixel size. When it reaches the watermark level, it'll start
  905. * fetching FIFO line sized based chunks from memory until the FIFO fills
  906. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  907. * will occur, and a display engine hang could result.
  908. */
  909. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  910. const struct intel_watermark_params *wm,
  911. int fifo_size,
  912. int pixel_size,
  913. unsigned long latency_ns)
  914. {
  915. long entries_required, wm_size;
  916. /*
  917. * Note: we need to make sure we don't overflow for various clock &
  918. * latency values.
  919. * clocks go from a few thousand to several hundred thousand.
  920. * latency is usually a few thousand
  921. */
  922. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  923. 1000;
  924. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  925. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  926. wm_size = fifo_size - (entries_required + wm->guard_size);
  927. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  928. /* Don't promote wm_size to unsigned... */
  929. if (wm_size > (long)wm->max_wm)
  930. wm_size = wm->max_wm;
  931. if (wm_size <= 0)
  932. wm_size = wm->default_wm;
  933. return wm_size;
  934. }
  935. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  936. {
  937. struct drm_crtc *crtc, *enabled = NULL;
  938. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  939. if (intel_crtc_active(crtc)) {
  940. if (enabled)
  941. return NULL;
  942. enabled = crtc;
  943. }
  944. }
  945. return enabled;
  946. }
  947. static void pineview_update_wm(struct drm_device *dev)
  948. {
  949. struct drm_i915_private *dev_priv = dev->dev_private;
  950. struct drm_crtc *crtc;
  951. const struct cxsr_latency *latency;
  952. u32 reg;
  953. unsigned long wm;
  954. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  955. dev_priv->fsb_freq, dev_priv->mem_freq);
  956. if (!latency) {
  957. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  958. pineview_disable_cxsr(dev);
  959. return;
  960. }
  961. crtc = single_enabled_crtc(dev);
  962. if (crtc) {
  963. int clock = crtc->mode.clock;
  964. int pixel_size = crtc->fb->bits_per_pixel / 8;
  965. /* Display SR */
  966. wm = intel_calculate_wm(clock, &pineview_display_wm,
  967. pineview_display_wm.fifo_size,
  968. pixel_size, latency->display_sr);
  969. reg = I915_READ(DSPFW1);
  970. reg &= ~DSPFW_SR_MASK;
  971. reg |= wm << DSPFW_SR_SHIFT;
  972. I915_WRITE(DSPFW1, reg);
  973. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  974. /* cursor SR */
  975. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  976. pineview_display_wm.fifo_size,
  977. pixel_size, latency->cursor_sr);
  978. reg = I915_READ(DSPFW3);
  979. reg &= ~DSPFW_CURSOR_SR_MASK;
  980. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  981. I915_WRITE(DSPFW3, reg);
  982. /* Display HPLL off SR */
  983. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  984. pineview_display_hplloff_wm.fifo_size,
  985. pixel_size, latency->display_hpll_disable);
  986. reg = I915_READ(DSPFW3);
  987. reg &= ~DSPFW_HPLL_SR_MASK;
  988. reg |= wm & DSPFW_HPLL_SR_MASK;
  989. I915_WRITE(DSPFW3, reg);
  990. /* cursor HPLL off SR */
  991. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  992. pineview_display_hplloff_wm.fifo_size,
  993. pixel_size, latency->cursor_hpll_disable);
  994. reg = I915_READ(DSPFW3);
  995. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  996. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  997. I915_WRITE(DSPFW3, reg);
  998. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  999. /* activate cxsr */
  1000. I915_WRITE(DSPFW3,
  1001. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  1002. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  1003. } else {
  1004. pineview_disable_cxsr(dev);
  1005. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  1006. }
  1007. }
  1008. static bool g4x_compute_wm0(struct drm_device *dev,
  1009. int plane,
  1010. const struct intel_watermark_params *display,
  1011. int display_latency_ns,
  1012. const struct intel_watermark_params *cursor,
  1013. int cursor_latency_ns,
  1014. int *plane_wm,
  1015. int *cursor_wm)
  1016. {
  1017. struct drm_crtc *crtc;
  1018. int htotal, hdisplay, clock, pixel_size;
  1019. int line_time_us, line_count;
  1020. int entries, tlb_miss;
  1021. crtc = intel_get_crtc_for_plane(dev, plane);
  1022. if (!intel_crtc_active(crtc)) {
  1023. *cursor_wm = cursor->guard_size;
  1024. *plane_wm = display->guard_size;
  1025. return false;
  1026. }
  1027. htotal = crtc->mode.htotal;
  1028. hdisplay = crtc->mode.hdisplay;
  1029. clock = crtc->mode.clock;
  1030. pixel_size = crtc->fb->bits_per_pixel / 8;
  1031. /* Use the small buffer method to calculate plane watermark */
  1032. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  1033. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  1034. if (tlb_miss > 0)
  1035. entries += tlb_miss;
  1036. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  1037. *plane_wm = entries + display->guard_size;
  1038. if (*plane_wm > (int)display->max_wm)
  1039. *plane_wm = display->max_wm;
  1040. /* Use the large buffer method to calculate cursor watermark */
  1041. line_time_us = ((htotal * 1000) / clock);
  1042. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  1043. entries = line_count * 64 * pixel_size;
  1044. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  1045. if (tlb_miss > 0)
  1046. entries += tlb_miss;
  1047. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1048. *cursor_wm = entries + cursor->guard_size;
  1049. if (*cursor_wm > (int)cursor->max_wm)
  1050. *cursor_wm = (int)cursor->max_wm;
  1051. return true;
  1052. }
  1053. /*
  1054. * Check the wm result.
  1055. *
  1056. * If any calculated watermark values is larger than the maximum value that
  1057. * can be programmed into the associated watermark register, that watermark
  1058. * must be disabled.
  1059. */
  1060. static bool g4x_check_srwm(struct drm_device *dev,
  1061. int display_wm, int cursor_wm,
  1062. const struct intel_watermark_params *display,
  1063. const struct intel_watermark_params *cursor)
  1064. {
  1065. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  1066. display_wm, cursor_wm);
  1067. if (display_wm > display->max_wm) {
  1068. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  1069. display_wm, display->max_wm);
  1070. return false;
  1071. }
  1072. if (cursor_wm > cursor->max_wm) {
  1073. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  1074. cursor_wm, cursor->max_wm);
  1075. return false;
  1076. }
  1077. if (!(display_wm || cursor_wm)) {
  1078. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  1079. return false;
  1080. }
  1081. return true;
  1082. }
  1083. static bool g4x_compute_srwm(struct drm_device *dev,
  1084. int plane,
  1085. int latency_ns,
  1086. const struct intel_watermark_params *display,
  1087. const struct intel_watermark_params *cursor,
  1088. int *display_wm, int *cursor_wm)
  1089. {
  1090. struct drm_crtc *crtc;
  1091. int hdisplay, htotal, pixel_size, clock;
  1092. unsigned long line_time_us;
  1093. int line_count, line_size;
  1094. int small, large;
  1095. int entries;
  1096. if (!latency_ns) {
  1097. *display_wm = *cursor_wm = 0;
  1098. return false;
  1099. }
  1100. crtc = intel_get_crtc_for_plane(dev, plane);
  1101. hdisplay = crtc->mode.hdisplay;
  1102. htotal = crtc->mode.htotal;
  1103. clock = crtc->mode.clock;
  1104. pixel_size = crtc->fb->bits_per_pixel / 8;
  1105. line_time_us = (htotal * 1000) / clock;
  1106. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1107. line_size = hdisplay * pixel_size;
  1108. /* Use the minimum of the small and large buffer method for primary */
  1109. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1110. large = line_count * line_size;
  1111. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1112. *display_wm = entries + display->guard_size;
  1113. /* calculate the self-refresh watermark for display cursor */
  1114. entries = line_count * pixel_size * 64;
  1115. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1116. *cursor_wm = entries + cursor->guard_size;
  1117. return g4x_check_srwm(dev,
  1118. *display_wm, *cursor_wm,
  1119. display, cursor);
  1120. }
  1121. static bool vlv_compute_drain_latency(struct drm_device *dev,
  1122. int plane,
  1123. int *plane_prec_mult,
  1124. int *plane_dl,
  1125. int *cursor_prec_mult,
  1126. int *cursor_dl)
  1127. {
  1128. struct drm_crtc *crtc;
  1129. int clock, pixel_size;
  1130. int entries;
  1131. crtc = intel_get_crtc_for_plane(dev, plane);
  1132. if (!intel_crtc_active(crtc))
  1133. return false;
  1134. clock = crtc->mode.clock; /* VESA DOT Clock */
  1135. pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
  1136. entries = (clock / 1000) * pixel_size;
  1137. *plane_prec_mult = (entries > 256) ?
  1138. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  1139. *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
  1140. pixel_size);
  1141. entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
  1142. *cursor_prec_mult = (entries > 256) ?
  1143. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  1144. *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
  1145. return true;
  1146. }
  1147. /*
  1148. * Update drain latency registers of memory arbiter
  1149. *
  1150. * Valleyview SoC has a new memory arbiter and needs drain latency registers
  1151. * to be programmed. Each plane has a drain latency multiplier and a drain
  1152. * latency value.
  1153. */
  1154. static void vlv_update_drain_latency(struct drm_device *dev)
  1155. {
  1156. struct drm_i915_private *dev_priv = dev->dev_private;
  1157. int planea_prec, planea_dl, planeb_prec, planeb_dl;
  1158. int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
  1159. int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
  1160. either 16 or 32 */
  1161. /* For plane A, Cursor A */
  1162. if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
  1163. &cursor_prec_mult, &cursora_dl)) {
  1164. cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1165. DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
  1166. planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1167. DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
  1168. I915_WRITE(VLV_DDL1, cursora_prec |
  1169. (cursora_dl << DDL_CURSORA_SHIFT) |
  1170. planea_prec | planea_dl);
  1171. }
  1172. /* For plane B, Cursor B */
  1173. if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
  1174. &cursor_prec_mult, &cursorb_dl)) {
  1175. cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1176. DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
  1177. planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  1178. DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
  1179. I915_WRITE(VLV_DDL2, cursorb_prec |
  1180. (cursorb_dl << DDL_CURSORB_SHIFT) |
  1181. planeb_prec | planeb_dl);
  1182. }
  1183. }
  1184. #define single_plane_enabled(mask) is_power_of_2(mask)
  1185. static void valleyview_update_wm(struct drm_device *dev)
  1186. {
  1187. static const int sr_latency_ns = 12000;
  1188. struct drm_i915_private *dev_priv = dev->dev_private;
  1189. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1190. int plane_sr, cursor_sr;
  1191. int ignore_plane_sr, ignore_cursor_sr;
  1192. unsigned int enabled = 0;
  1193. vlv_update_drain_latency(dev);
  1194. if (g4x_compute_wm0(dev, PIPE_A,
  1195. &valleyview_wm_info, latency_ns,
  1196. &valleyview_cursor_wm_info, latency_ns,
  1197. &planea_wm, &cursora_wm))
  1198. enabled |= 1 << PIPE_A;
  1199. if (g4x_compute_wm0(dev, PIPE_B,
  1200. &valleyview_wm_info, latency_ns,
  1201. &valleyview_cursor_wm_info, latency_ns,
  1202. &planeb_wm, &cursorb_wm))
  1203. enabled |= 1 << PIPE_B;
  1204. if (single_plane_enabled(enabled) &&
  1205. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1206. sr_latency_ns,
  1207. &valleyview_wm_info,
  1208. &valleyview_cursor_wm_info,
  1209. &plane_sr, &ignore_cursor_sr) &&
  1210. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1211. 2*sr_latency_ns,
  1212. &valleyview_wm_info,
  1213. &valleyview_cursor_wm_info,
  1214. &ignore_plane_sr, &cursor_sr)) {
  1215. I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
  1216. } else {
  1217. I915_WRITE(FW_BLC_SELF_VLV,
  1218. I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
  1219. plane_sr = cursor_sr = 0;
  1220. }
  1221. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1222. planea_wm, cursora_wm,
  1223. planeb_wm, cursorb_wm,
  1224. plane_sr, cursor_sr);
  1225. I915_WRITE(DSPFW1,
  1226. (plane_sr << DSPFW_SR_SHIFT) |
  1227. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1228. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1229. planea_wm);
  1230. I915_WRITE(DSPFW2,
  1231. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  1232. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1233. I915_WRITE(DSPFW3,
  1234. (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
  1235. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1236. }
  1237. static void g4x_update_wm(struct drm_device *dev)
  1238. {
  1239. static const int sr_latency_ns = 12000;
  1240. struct drm_i915_private *dev_priv = dev->dev_private;
  1241. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  1242. int plane_sr, cursor_sr;
  1243. unsigned int enabled = 0;
  1244. if (g4x_compute_wm0(dev, PIPE_A,
  1245. &g4x_wm_info, latency_ns,
  1246. &g4x_cursor_wm_info, latency_ns,
  1247. &planea_wm, &cursora_wm))
  1248. enabled |= 1 << PIPE_A;
  1249. if (g4x_compute_wm0(dev, PIPE_B,
  1250. &g4x_wm_info, latency_ns,
  1251. &g4x_cursor_wm_info, latency_ns,
  1252. &planeb_wm, &cursorb_wm))
  1253. enabled |= 1 << PIPE_B;
  1254. if (single_plane_enabled(enabled) &&
  1255. g4x_compute_srwm(dev, ffs(enabled) - 1,
  1256. sr_latency_ns,
  1257. &g4x_wm_info,
  1258. &g4x_cursor_wm_info,
  1259. &plane_sr, &cursor_sr)) {
  1260. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1261. } else {
  1262. I915_WRITE(FW_BLC_SELF,
  1263. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  1264. plane_sr = cursor_sr = 0;
  1265. }
  1266. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  1267. planea_wm, cursora_wm,
  1268. planeb_wm, cursorb_wm,
  1269. plane_sr, cursor_sr);
  1270. I915_WRITE(DSPFW1,
  1271. (plane_sr << DSPFW_SR_SHIFT) |
  1272. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  1273. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  1274. planea_wm);
  1275. I915_WRITE(DSPFW2,
  1276. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  1277. (cursora_wm << DSPFW_CURSORA_SHIFT));
  1278. /* HPLL off in SR has some issues on G4x... disable it */
  1279. I915_WRITE(DSPFW3,
  1280. (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
  1281. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1282. }
  1283. static void i965_update_wm(struct drm_device *dev)
  1284. {
  1285. struct drm_i915_private *dev_priv = dev->dev_private;
  1286. struct drm_crtc *crtc;
  1287. int srwm = 1;
  1288. int cursor_sr = 16;
  1289. /* Calc sr entries for one plane configs */
  1290. crtc = single_enabled_crtc(dev);
  1291. if (crtc) {
  1292. /* self-refresh has much higher latency */
  1293. static const int sr_latency_ns = 12000;
  1294. int clock = crtc->mode.clock;
  1295. int htotal = crtc->mode.htotal;
  1296. int hdisplay = crtc->mode.hdisplay;
  1297. int pixel_size = crtc->fb->bits_per_pixel / 8;
  1298. unsigned long line_time_us;
  1299. int entries;
  1300. line_time_us = ((htotal * 1000) / clock);
  1301. /* Use ns/us then divide to preserve precision */
  1302. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1303. pixel_size * hdisplay;
  1304. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  1305. srwm = I965_FIFO_SIZE - entries;
  1306. if (srwm < 0)
  1307. srwm = 1;
  1308. srwm &= 0x1ff;
  1309. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  1310. entries, srwm);
  1311. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1312. pixel_size * 64;
  1313. entries = DIV_ROUND_UP(entries,
  1314. i965_cursor_wm_info.cacheline_size);
  1315. cursor_sr = i965_cursor_wm_info.fifo_size -
  1316. (entries + i965_cursor_wm_info.guard_size);
  1317. if (cursor_sr > i965_cursor_wm_info.max_wm)
  1318. cursor_sr = i965_cursor_wm_info.max_wm;
  1319. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  1320. "cursor %d\n", srwm, cursor_sr);
  1321. if (IS_CRESTLINE(dev))
  1322. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  1323. } else {
  1324. /* Turn off self refresh if both pipes are enabled */
  1325. if (IS_CRESTLINE(dev))
  1326. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  1327. & ~FW_BLC_SELF_EN);
  1328. }
  1329. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  1330. srwm);
  1331. /* 965 has limitations... */
  1332. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  1333. (8 << 16) | (8 << 8) | (8 << 0));
  1334. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  1335. /* update cursor SR watermark */
  1336. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  1337. }
  1338. static void i9xx_update_wm(struct drm_device *dev)
  1339. {
  1340. struct drm_i915_private *dev_priv = dev->dev_private;
  1341. const struct intel_watermark_params *wm_info;
  1342. uint32_t fwater_lo;
  1343. uint32_t fwater_hi;
  1344. int cwm, srwm = 1;
  1345. int fifo_size;
  1346. int planea_wm, planeb_wm;
  1347. struct drm_crtc *crtc, *enabled = NULL;
  1348. if (IS_I945GM(dev))
  1349. wm_info = &i945_wm_info;
  1350. else if (!IS_GEN2(dev))
  1351. wm_info = &i915_wm_info;
  1352. else
  1353. wm_info = &i855_wm_info;
  1354. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  1355. crtc = intel_get_crtc_for_plane(dev, 0);
  1356. if (intel_crtc_active(crtc)) {
  1357. int cpp = crtc->fb->bits_per_pixel / 8;
  1358. if (IS_GEN2(dev))
  1359. cpp = 4;
  1360. planea_wm = intel_calculate_wm(crtc->mode.clock,
  1361. wm_info, fifo_size, cpp,
  1362. latency_ns);
  1363. enabled = crtc;
  1364. } else
  1365. planea_wm = fifo_size - wm_info->guard_size;
  1366. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  1367. crtc = intel_get_crtc_for_plane(dev, 1);
  1368. if (intel_crtc_active(crtc)) {
  1369. int cpp = crtc->fb->bits_per_pixel / 8;
  1370. if (IS_GEN2(dev))
  1371. cpp = 4;
  1372. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  1373. wm_info, fifo_size, cpp,
  1374. latency_ns);
  1375. if (enabled == NULL)
  1376. enabled = crtc;
  1377. else
  1378. enabled = NULL;
  1379. } else
  1380. planeb_wm = fifo_size - wm_info->guard_size;
  1381. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  1382. /*
  1383. * Overlay gets an aggressive default since video jitter is bad.
  1384. */
  1385. cwm = 2;
  1386. /* Play safe and disable self-refresh before adjusting watermarks. */
  1387. if (IS_I945G(dev) || IS_I945GM(dev))
  1388. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  1389. else if (IS_I915GM(dev))
  1390. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  1391. /* Calc sr entries for one plane configs */
  1392. if (HAS_FW_BLC(dev) && enabled) {
  1393. /* self-refresh has much higher latency */
  1394. static const int sr_latency_ns = 6000;
  1395. int clock = enabled->mode.clock;
  1396. int htotal = enabled->mode.htotal;
  1397. int hdisplay = enabled->mode.hdisplay;
  1398. int pixel_size = enabled->fb->bits_per_pixel / 8;
  1399. unsigned long line_time_us;
  1400. int entries;
  1401. line_time_us = (htotal * 1000) / clock;
  1402. /* Use ns/us then divide to preserve precision */
  1403. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1404. pixel_size * hdisplay;
  1405. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  1406. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  1407. srwm = wm_info->fifo_size - entries;
  1408. if (srwm < 0)
  1409. srwm = 1;
  1410. if (IS_I945G(dev) || IS_I945GM(dev))
  1411. I915_WRITE(FW_BLC_SELF,
  1412. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  1413. else if (IS_I915GM(dev))
  1414. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  1415. }
  1416. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  1417. planea_wm, planeb_wm, cwm, srwm);
  1418. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  1419. fwater_hi = (cwm & 0x1f);
  1420. /* Set request length to 8 cachelines per fetch */
  1421. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  1422. fwater_hi = fwater_hi | (1 << 8);
  1423. I915_WRITE(FW_BLC, fwater_lo);
  1424. I915_WRITE(FW_BLC2, fwater_hi);
  1425. if (HAS_FW_BLC(dev)) {
  1426. if (enabled) {
  1427. if (IS_I945G(dev) || IS_I945GM(dev))
  1428. I915_WRITE(FW_BLC_SELF,
  1429. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  1430. else if (IS_I915GM(dev))
  1431. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  1432. DRM_DEBUG_KMS("memory self refresh enabled\n");
  1433. } else
  1434. DRM_DEBUG_KMS("memory self refresh disabled\n");
  1435. }
  1436. }
  1437. static void i830_update_wm(struct drm_device *dev)
  1438. {
  1439. struct drm_i915_private *dev_priv = dev->dev_private;
  1440. struct drm_crtc *crtc;
  1441. uint32_t fwater_lo;
  1442. int planea_wm;
  1443. crtc = single_enabled_crtc(dev);
  1444. if (crtc == NULL)
  1445. return;
  1446. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  1447. dev_priv->display.get_fifo_size(dev, 0),
  1448. 4, latency_ns);
  1449. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  1450. fwater_lo |= (3<<8) | planea_wm;
  1451. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  1452. I915_WRITE(FW_BLC, fwater_lo);
  1453. }
  1454. #define ILK_LP0_PLANE_LATENCY 700
  1455. #define ILK_LP0_CURSOR_LATENCY 1300
  1456. /*
  1457. * Check the wm result.
  1458. *
  1459. * If any calculated watermark values is larger than the maximum value that
  1460. * can be programmed into the associated watermark register, that watermark
  1461. * must be disabled.
  1462. */
  1463. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  1464. int fbc_wm, int display_wm, int cursor_wm,
  1465. const struct intel_watermark_params *display,
  1466. const struct intel_watermark_params *cursor)
  1467. {
  1468. struct drm_i915_private *dev_priv = dev->dev_private;
  1469. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  1470. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  1471. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  1472. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  1473. fbc_wm, SNB_FBC_MAX_SRWM, level);
  1474. /* fbc has it's own way to disable FBC WM */
  1475. I915_WRITE(DISP_ARB_CTL,
  1476. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  1477. return false;
  1478. } else if (INTEL_INFO(dev)->gen >= 6) {
  1479. /* enable FBC WM (except on ILK, where it must remain off) */
  1480. I915_WRITE(DISP_ARB_CTL,
  1481. I915_READ(DISP_ARB_CTL) & ~DISP_FBC_WM_DIS);
  1482. }
  1483. if (display_wm > display->max_wm) {
  1484. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  1485. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  1486. return false;
  1487. }
  1488. if (cursor_wm > cursor->max_wm) {
  1489. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  1490. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  1491. return false;
  1492. }
  1493. if (!(fbc_wm || display_wm || cursor_wm)) {
  1494. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  1495. return false;
  1496. }
  1497. return true;
  1498. }
  1499. /*
  1500. * Compute watermark values of WM[1-3],
  1501. */
  1502. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  1503. int latency_ns,
  1504. const struct intel_watermark_params *display,
  1505. const struct intel_watermark_params *cursor,
  1506. int *fbc_wm, int *display_wm, int *cursor_wm)
  1507. {
  1508. struct drm_crtc *crtc;
  1509. unsigned long line_time_us;
  1510. int hdisplay, htotal, pixel_size, clock;
  1511. int line_count, line_size;
  1512. int small, large;
  1513. int entries;
  1514. if (!latency_ns) {
  1515. *fbc_wm = *display_wm = *cursor_wm = 0;
  1516. return false;
  1517. }
  1518. crtc = intel_get_crtc_for_plane(dev, plane);
  1519. hdisplay = crtc->mode.hdisplay;
  1520. htotal = crtc->mode.htotal;
  1521. clock = crtc->mode.clock;
  1522. pixel_size = crtc->fb->bits_per_pixel / 8;
  1523. line_time_us = (htotal * 1000) / clock;
  1524. line_count = (latency_ns / line_time_us + 1000) / 1000;
  1525. line_size = hdisplay * pixel_size;
  1526. /* Use the minimum of the small and large buffer method for primary */
  1527. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  1528. large = line_count * line_size;
  1529. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  1530. *display_wm = entries + display->guard_size;
  1531. /*
  1532. * Spec says:
  1533. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  1534. */
  1535. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  1536. /* calculate the self-refresh watermark for display cursor */
  1537. entries = line_count * pixel_size * 64;
  1538. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  1539. *cursor_wm = entries + cursor->guard_size;
  1540. return ironlake_check_srwm(dev, level,
  1541. *fbc_wm, *display_wm, *cursor_wm,
  1542. display, cursor);
  1543. }
  1544. static void ironlake_update_wm(struct drm_device *dev)
  1545. {
  1546. struct drm_i915_private *dev_priv = dev->dev_private;
  1547. int fbc_wm, plane_wm, cursor_wm;
  1548. unsigned int enabled;
  1549. enabled = 0;
  1550. if (g4x_compute_wm0(dev, PIPE_A,
  1551. &ironlake_display_wm_info,
  1552. ILK_LP0_PLANE_LATENCY,
  1553. &ironlake_cursor_wm_info,
  1554. ILK_LP0_CURSOR_LATENCY,
  1555. &plane_wm, &cursor_wm)) {
  1556. I915_WRITE(WM0_PIPEA_ILK,
  1557. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1558. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1559. " plane %d, " "cursor: %d\n",
  1560. plane_wm, cursor_wm);
  1561. enabled |= 1 << PIPE_A;
  1562. }
  1563. if (g4x_compute_wm0(dev, PIPE_B,
  1564. &ironlake_display_wm_info,
  1565. ILK_LP0_PLANE_LATENCY,
  1566. &ironlake_cursor_wm_info,
  1567. ILK_LP0_CURSOR_LATENCY,
  1568. &plane_wm, &cursor_wm)) {
  1569. I915_WRITE(WM0_PIPEB_ILK,
  1570. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  1571. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1572. " plane %d, cursor: %d\n",
  1573. plane_wm, cursor_wm);
  1574. enabled |= 1 << PIPE_B;
  1575. }
  1576. /*
  1577. * Calculate and update the self-refresh watermark only when one
  1578. * display plane is used.
  1579. */
  1580. I915_WRITE(WM3_LP_ILK, 0);
  1581. I915_WRITE(WM2_LP_ILK, 0);
  1582. I915_WRITE(WM1_LP_ILK, 0);
  1583. if (!single_plane_enabled(enabled))
  1584. return;
  1585. enabled = ffs(enabled) - 1;
  1586. /* WM1 */
  1587. if (!ironlake_compute_srwm(dev, 1, enabled,
  1588. ILK_READ_WM1_LATENCY() * 500,
  1589. &ironlake_display_srwm_info,
  1590. &ironlake_cursor_srwm_info,
  1591. &fbc_wm, &plane_wm, &cursor_wm))
  1592. return;
  1593. I915_WRITE(WM1_LP_ILK,
  1594. WM1_LP_SR_EN |
  1595. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1596. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1597. (plane_wm << WM1_LP_SR_SHIFT) |
  1598. cursor_wm);
  1599. /* WM2 */
  1600. if (!ironlake_compute_srwm(dev, 2, enabled,
  1601. ILK_READ_WM2_LATENCY() * 500,
  1602. &ironlake_display_srwm_info,
  1603. &ironlake_cursor_srwm_info,
  1604. &fbc_wm, &plane_wm, &cursor_wm))
  1605. return;
  1606. I915_WRITE(WM2_LP_ILK,
  1607. WM2_LP_EN |
  1608. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1609. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1610. (plane_wm << WM1_LP_SR_SHIFT) |
  1611. cursor_wm);
  1612. /*
  1613. * WM3 is unsupported on ILK, probably because we don't have latency
  1614. * data for that power state
  1615. */
  1616. }
  1617. static void sandybridge_update_wm(struct drm_device *dev)
  1618. {
  1619. struct drm_i915_private *dev_priv = dev->dev_private;
  1620. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1621. u32 val;
  1622. int fbc_wm, plane_wm, cursor_wm;
  1623. unsigned int enabled;
  1624. enabled = 0;
  1625. if (g4x_compute_wm0(dev, PIPE_A,
  1626. &sandybridge_display_wm_info, latency,
  1627. &sandybridge_cursor_wm_info, latency,
  1628. &plane_wm, &cursor_wm)) {
  1629. val = I915_READ(WM0_PIPEA_ILK);
  1630. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1631. I915_WRITE(WM0_PIPEA_ILK, val |
  1632. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1633. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1634. " plane %d, " "cursor: %d\n",
  1635. plane_wm, cursor_wm);
  1636. enabled |= 1 << PIPE_A;
  1637. }
  1638. if (g4x_compute_wm0(dev, PIPE_B,
  1639. &sandybridge_display_wm_info, latency,
  1640. &sandybridge_cursor_wm_info, latency,
  1641. &plane_wm, &cursor_wm)) {
  1642. val = I915_READ(WM0_PIPEB_ILK);
  1643. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1644. I915_WRITE(WM0_PIPEB_ILK, val |
  1645. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1646. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1647. " plane %d, cursor: %d\n",
  1648. plane_wm, cursor_wm);
  1649. enabled |= 1 << PIPE_B;
  1650. }
  1651. /*
  1652. * Calculate and update the self-refresh watermark only when one
  1653. * display plane is used.
  1654. *
  1655. * SNB support 3 levels of watermark.
  1656. *
  1657. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  1658. * and disabled in the descending order
  1659. *
  1660. */
  1661. I915_WRITE(WM3_LP_ILK, 0);
  1662. I915_WRITE(WM2_LP_ILK, 0);
  1663. I915_WRITE(WM1_LP_ILK, 0);
  1664. if (!single_plane_enabled(enabled) ||
  1665. dev_priv->sprite_scaling_enabled)
  1666. return;
  1667. enabled = ffs(enabled) - 1;
  1668. /* WM1 */
  1669. if (!ironlake_compute_srwm(dev, 1, enabled,
  1670. SNB_READ_WM1_LATENCY() * 500,
  1671. &sandybridge_display_srwm_info,
  1672. &sandybridge_cursor_srwm_info,
  1673. &fbc_wm, &plane_wm, &cursor_wm))
  1674. return;
  1675. I915_WRITE(WM1_LP_ILK,
  1676. WM1_LP_SR_EN |
  1677. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1678. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1679. (plane_wm << WM1_LP_SR_SHIFT) |
  1680. cursor_wm);
  1681. /* WM2 */
  1682. if (!ironlake_compute_srwm(dev, 2, enabled,
  1683. SNB_READ_WM2_LATENCY() * 500,
  1684. &sandybridge_display_srwm_info,
  1685. &sandybridge_cursor_srwm_info,
  1686. &fbc_wm, &plane_wm, &cursor_wm))
  1687. return;
  1688. I915_WRITE(WM2_LP_ILK,
  1689. WM2_LP_EN |
  1690. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1691. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1692. (plane_wm << WM1_LP_SR_SHIFT) |
  1693. cursor_wm);
  1694. /* WM3 */
  1695. if (!ironlake_compute_srwm(dev, 3, enabled,
  1696. SNB_READ_WM3_LATENCY() * 500,
  1697. &sandybridge_display_srwm_info,
  1698. &sandybridge_cursor_srwm_info,
  1699. &fbc_wm, &plane_wm, &cursor_wm))
  1700. return;
  1701. I915_WRITE(WM3_LP_ILK,
  1702. WM3_LP_EN |
  1703. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1704. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1705. (plane_wm << WM1_LP_SR_SHIFT) |
  1706. cursor_wm);
  1707. }
  1708. static void ivybridge_update_wm(struct drm_device *dev)
  1709. {
  1710. struct drm_i915_private *dev_priv = dev->dev_private;
  1711. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  1712. u32 val;
  1713. int fbc_wm, plane_wm, cursor_wm;
  1714. int ignore_fbc_wm, ignore_plane_wm, ignore_cursor_wm;
  1715. unsigned int enabled;
  1716. enabled = 0;
  1717. if (g4x_compute_wm0(dev, PIPE_A,
  1718. &sandybridge_display_wm_info, latency,
  1719. &sandybridge_cursor_wm_info, latency,
  1720. &plane_wm, &cursor_wm)) {
  1721. val = I915_READ(WM0_PIPEA_ILK);
  1722. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1723. I915_WRITE(WM0_PIPEA_ILK, val |
  1724. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1725. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  1726. " plane %d, " "cursor: %d\n",
  1727. plane_wm, cursor_wm);
  1728. enabled |= 1 << PIPE_A;
  1729. }
  1730. if (g4x_compute_wm0(dev, PIPE_B,
  1731. &sandybridge_display_wm_info, latency,
  1732. &sandybridge_cursor_wm_info, latency,
  1733. &plane_wm, &cursor_wm)) {
  1734. val = I915_READ(WM0_PIPEB_ILK);
  1735. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1736. I915_WRITE(WM0_PIPEB_ILK, val |
  1737. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1738. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  1739. " plane %d, cursor: %d\n",
  1740. plane_wm, cursor_wm);
  1741. enabled |= 1 << PIPE_B;
  1742. }
  1743. if (g4x_compute_wm0(dev, PIPE_C,
  1744. &sandybridge_display_wm_info, latency,
  1745. &sandybridge_cursor_wm_info, latency,
  1746. &plane_wm, &cursor_wm)) {
  1747. val = I915_READ(WM0_PIPEC_IVB);
  1748. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  1749. I915_WRITE(WM0_PIPEC_IVB, val |
  1750. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  1751. DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
  1752. " plane %d, cursor: %d\n",
  1753. plane_wm, cursor_wm);
  1754. enabled |= 1 << PIPE_C;
  1755. }
  1756. /*
  1757. * Calculate and update the self-refresh watermark only when one
  1758. * display plane is used.
  1759. *
  1760. * SNB support 3 levels of watermark.
  1761. *
  1762. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  1763. * and disabled in the descending order
  1764. *
  1765. */
  1766. I915_WRITE(WM3_LP_ILK, 0);
  1767. I915_WRITE(WM2_LP_ILK, 0);
  1768. I915_WRITE(WM1_LP_ILK, 0);
  1769. if (!single_plane_enabled(enabled) ||
  1770. dev_priv->sprite_scaling_enabled)
  1771. return;
  1772. enabled = ffs(enabled) - 1;
  1773. /* WM1 */
  1774. if (!ironlake_compute_srwm(dev, 1, enabled,
  1775. SNB_READ_WM1_LATENCY() * 500,
  1776. &sandybridge_display_srwm_info,
  1777. &sandybridge_cursor_srwm_info,
  1778. &fbc_wm, &plane_wm, &cursor_wm))
  1779. return;
  1780. I915_WRITE(WM1_LP_ILK,
  1781. WM1_LP_SR_EN |
  1782. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1783. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1784. (plane_wm << WM1_LP_SR_SHIFT) |
  1785. cursor_wm);
  1786. /* WM2 */
  1787. if (!ironlake_compute_srwm(dev, 2, enabled,
  1788. SNB_READ_WM2_LATENCY() * 500,
  1789. &sandybridge_display_srwm_info,
  1790. &sandybridge_cursor_srwm_info,
  1791. &fbc_wm, &plane_wm, &cursor_wm))
  1792. return;
  1793. I915_WRITE(WM2_LP_ILK,
  1794. WM2_LP_EN |
  1795. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1796. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1797. (plane_wm << WM1_LP_SR_SHIFT) |
  1798. cursor_wm);
  1799. /* WM3, note we have to correct the cursor latency */
  1800. if (!ironlake_compute_srwm(dev, 3, enabled,
  1801. SNB_READ_WM3_LATENCY() * 500,
  1802. &sandybridge_display_srwm_info,
  1803. &sandybridge_cursor_srwm_info,
  1804. &fbc_wm, &plane_wm, &ignore_cursor_wm) ||
  1805. !ironlake_compute_srwm(dev, 3, enabled,
  1806. 2 * SNB_READ_WM3_LATENCY() * 500,
  1807. &sandybridge_display_srwm_info,
  1808. &sandybridge_cursor_srwm_info,
  1809. &ignore_fbc_wm, &ignore_plane_wm, &cursor_wm))
  1810. return;
  1811. I915_WRITE(WM3_LP_ILK,
  1812. WM3_LP_EN |
  1813. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  1814. (fbc_wm << WM1_LP_FBC_SHIFT) |
  1815. (plane_wm << WM1_LP_SR_SHIFT) |
  1816. cursor_wm);
  1817. }
  1818. static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
  1819. struct drm_crtc *crtc)
  1820. {
  1821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1822. uint32_t pixel_rate, pfit_size;
  1823. pixel_rate = intel_crtc->config.adjusted_mode.clock;
  1824. /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
  1825. * adjust the pixel_rate here. */
  1826. pfit_size = intel_crtc->config.pch_pfit.size;
  1827. if (pfit_size) {
  1828. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  1829. pipe_w = intel_crtc->config.requested_mode.hdisplay;
  1830. pipe_h = intel_crtc->config.requested_mode.vdisplay;
  1831. pfit_w = (pfit_size >> 16) & 0xFFFF;
  1832. pfit_h = pfit_size & 0xFFFF;
  1833. if (pipe_w < pfit_w)
  1834. pipe_w = pfit_w;
  1835. if (pipe_h < pfit_h)
  1836. pipe_h = pfit_h;
  1837. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  1838. pfit_w * pfit_h);
  1839. }
  1840. return pixel_rate;
  1841. }
  1842. static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
  1843. uint32_t latency)
  1844. {
  1845. uint64_t ret;
  1846. ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
  1847. ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
  1848. return ret;
  1849. }
  1850. static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
  1851. uint32_t horiz_pixels, uint8_t bytes_per_pixel,
  1852. uint32_t latency)
  1853. {
  1854. uint32_t ret;
  1855. ret = (latency * pixel_rate) / (pipe_htotal * 10000);
  1856. ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
  1857. ret = DIV_ROUND_UP(ret, 64) + 2;
  1858. return ret;
  1859. }
  1860. static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
  1861. uint8_t bytes_per_pixel)
  1862. {
  1863. return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
  1864. }
  1865. struct hsw_pipe_wm_parameters {
  1866. bool active;
  1867. bool sprite_enabled;
  1868. uint8_t pri_bytes_per_pixel;
  1869. uint8_t spr_bytes_per_pixel;
  1870. uint8_t cur_bytes_per_pixel;
  1871. uint32_t pri_horiz_pixels;
  1872. uint32_t spr_horiz_pixels;
  1873. uint32_t cur_horiz_pixels;
  1874. uint32_t pipe_htotal;
  1875. uint32_t pixel_rate;
  1876. };
  1877. struct hsw_wm_maximums {
  1878. uint16_t pri;
  1879. uint16_t spr;
  1880. uint16_t cur;
  1881. uint16_t fbc;
  1882. };
  1883. struct hsw_lp_wm_result {
  1884. bool enable;
  1885. bool fbc_enable;
  1886. uint32_t pri_val;
  1887. uint32_t spr_val;
  1888. uint32_t cur_val;
  1889. uint32_t fbc_val;
  1890. };
  1891. struct hsw_wm_values {
  1892. uint32_t wm_pipe[3];
  1893. uint32_t wm_lp[3];
  1894. uint32_t wm_lp_spr[3];
  1895. uint32_t wm_linetime[3];
  1896. bool enable_fbc_wm;
  1897. };
  1898. enum hsw_data_buf_partitioning {
  1899. HSW_DATA_BUF_PART_1_2,
  1900. HSW_DATA_BUF_PART_5_6,
  1901. };
  1902. /* For both WM_PIPE and WM_LP. */
  1903. static uint32_t ilk_compute_pri_wm(struct hsw_pipe_wm_parameters *params,
  1904. uint32_t mem_value,
  1905. bool is_lp)
  1906. {
  1907. uint32_t method1, method2;
  1908. /* TODO: for now, assume the primary plane is always enabled. */
  1909. if (!params->active)
  1910. return 0;
  1911. method1 = ilk_wm_method1(params->pixel_rate,
  1912. params->pri_bytes_per_pixel,
  1913. mem_value);
  1914. if (!is_lp)
  1915. return method1;
  1916. method2 = ilk_wm_method2(params->pixel_rate,
  1917. params->pipe_htotal,
  1918. params->pri_horiz_pixels,
  1919. params->pri_bytes_per_pixel,
  1920. mem_value);
  1921. return min(method1, method2);
  1922. }
  1923. /* For both WM_PIPE and WM_LP. */
  1924. static uint32_t ilk_compute_spr_wm(struct hsw_pipe_wm_parameters *params,
  1925. uint32_t mem_value)
  1926. {
  1927. uint32_t method1, method2;
  1928. if (!params->active || !params->sprite_enabled)
  1929. return 0;
  1930. method1 = ilk_wm_method1(params->pixel_rate,
  1931. params->spr_bytes_per_pixel,
  1932. mem_value);
  1933. method2 = ilk_wm_method2(params->pixel_rate,
  1934. params->pipe_htotal,
  1935. params->spr_horiz_pixels,
  1936. params->spr_bytes_per_pixel,
  1937. mem_value);
  1938. return min(method1, method2);
  1939. }
  1940. /* For both WM_PIPE and WM_LP. */
  1941. static uint32_t ilk_compute_cur_wm(struct hsw_pipe_wm_parameters *params,
  1942. uint32_t mem_value)
  1943. {
  1944. if (!params->active)
  1945. return 0;
  1946. return ilk_wm_method2(params->pixel_rate,
  1947. params->pipe_htotal,
  1948. params->cur_horiz_pixels,
  1949. params->cur_bytes_per_pixel,
  1950. mem_value);
  1951. }
  1952. /* Only for WM_LP. */
  1953. static uint32_t ilk_compute_fbc_wm(struct hsw_pipe_wm_parameters *params,
  1954. uint32_t pri_val)
  1955. {
  1956. if (!params->active)
  1957. return 0;
  1958. return ilk_wm_fbc(pri_val,
  1959. params->pri_horiz_pixels,
  1960. params->pri_bytes_per_pixel);
  1961. }
  1962. static bool hsw_compute_lp_wm(struct drm_i915_private *dev_priv,
  1963. int level, struct hsw_wm_maximums *max,
  1964. struct hsw_pipe_wm_parameters *params,
  1965. struct hsw_lp_wm_result *result)
  1966. {
  1967. enum pipe pipe;
  1968. uint32_t pri_val[3], spr_val[3], cur_val[3], fbc_val[3];
  1969. for (pipe = PIPE_A; pipe <= PIPE_C; pipe++) {
  1970. struct hsw_pipe_wm_parameters *p = &params[pipe];
  1971. /* WM1+ latency values stored in 0.5us units */
  1972. uint16_t pri_latency = dev_priv->wm.pri_latency[level] * 5;
  1973. uint16_t spr_latency = dev_priv->wm.spr_latency[level] * 5;
  1974. uint16_t cur_latency = dev_priv->wm.cur_latency[level] * 5;
  1975. pri_val[pipe] = ilk_compute_pri_wm(p, pri_latency, true);
  1976. spr_val[pipe] = ilk_compute_spr_wm(p, spr_latency);
  1977. cur_val[pipe] = ilk_compute_cur_wm(p, cur_latency);
  1978. fbc_val[pipe] = ilk_compute_fbc_wm(p, pri_val[pipe]);
  1979. }
  1980. result->pri_val = max3(pri_val[0], pri_val[1], pri_val[2]);
  1981. result->spr_val = max3(spr_val[0], spr_val[1], spr_val[2]);
  1982. result->cur_val = max3(cur_val[0], cur_val[1], cur_val[2]);
  1983. result->fbc_val = max3(fbc_val[0], fbc_val[1], fbc_val[2]);
  1984. if (result->fbc_val > max->fbc) {
  1985. result->fbc_enable = false;
  1986. result->fbc_val = 0;
  1987. } else {
  1988. result->fbc_enable = true;
  1989. }
  1990. result->enable = result->pri_val <= max->pri &&
  1991. result->spr_val <= max->spr &&
  1992. result->cur_val <= max->cur;
  1993. return result->enable;
  1994. }
  1995. static uint32_t hsw_compute_wm_pipe(struct drm_i915_private *dev_priv,
  1996. enum pipe pipe,
  1997. struct hsw_pipe_wm_parameters *params)
  1998. {
  1999. uint32_t pri_val, cur_val, spr_val;
  2000. /* WM0 latency values stored in 0.1us units */
  2001. uint16_t pri_latency = dev_priv->wm.pri_latency[0];
  2002. uint16_t spr_latency = dev_priv->wm.spr_latency[0];
  2003. uint16_t cur_latency = dev_priv->wm.cur_latency[0];
  2004. pri_val = ilk_compute_pri_wm(params, pri_latency, false);
  2005. spr_val = ilk_compute_spr_wm(params, spr_latency);
  2006. cur_val = ilk_compute_cur_wm(params, cur_latency);
  2007. WARN(pri_val > 127,
  2008. "Primary WM error, mode not supported for pipe %c\n",
  2009. pipe_name(pipe));
  2010. WARN(spr_val > 127,
  2011. "Sprite WM error, mode not supported for pipe %c\n",
  2012. pipe_name(pipe));
  2013. WARN(cur_val > 63,
  2014. "Cursor WM error, mode not supported for pipe %c\n",
  2015. pipe_name(pipe));
  2016. return (pri_val << WM0_PIPE_PLANE_SHIFT) |
  2017. (spr_val << WM0_PIPE_SPRITE_SHIFT) |
  2018. cur_val;
  2019. }
  2020. static uint32_t
  2021. hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
  2022. {
  2023. struct drm_i915_private *dev_priv = dev->dev_private;
  2024. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2025. struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
  2026. u32 linetime, ips_linetime;
  2027. if (!intel_crtc_active(crtc))
  2028. return 0;
  2029. /* The WM are computed with base on how long it takes to fill a single
  2030. * row at the given clock rate, multiplied by 8.
  2031. * */
  2032. linetime = DIV_ROUND_CLOSEST(mode->htotal * 1000 * 8, mode->clock);
  2033. ips_linetime = DIV_ROUND_CLOSEST(mode->htotal * 1000 * 8,
  2034. intel_ddi_get_cdclk_freq(dev_priv));
  2035. return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
  2036. PIPE_WM_LINETIME_TIME(linetime);
  2037. }
  2038. static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
  2039. {
  2040. struct drm_i915_private *dev_priv = dev->dev_private;
  2041. if (IS_HASWELL(dev)) {
  2042. uint64_t sskpd = I915_READ64(MCH_SSKPD);
  2043. wm[0] = (sskpd >> 56) & 0xFF;
  2044. if (wm[0] == 0)
  2045. wm[0] = sskpd & 0xF;
  2046. wm[1] = (sskpd >> 4) & 0xFF;
  2047. wm[2] = (sskpd >> 12) & 0xFF;
  2048. wm[3] = (sskpd >> 20) & 0x1FF;
  2049. wm[4] = (sskpd >> 32) & 0x1FF;
  2050. } else if (INTEL_INFO(dev)->gen >= 6) {
  2051. uint32_t sskpd = I915_READ(MCH_SSKPD);
  2052. wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
  2053. wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
  2054. wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
  2055. wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
  2056. } else if (INTEL_INFO(dev)->gen >= 5) {
  2057. uint32_t mltr = I915_READ(MLTR_ILK);
  2058. /* ILK primary LP0 latency is 700 ns */
  2059. wm[0] = 7;
  2060. wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
  2061. wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
  2062. }
  2063. }
  2064. static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
  2065. {
  2066. /* ILK sprite LP0 latency is 1300 ns */
  2067. if (INTEL_INFO(dev)->gen == 5)
  2068. wm[0] = 13;
  2069. }
  2070. static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
  2071. {
  2072. /* ILK cursor LP0 latency is 1300 ns */
  2073. if (INTEL_INFO(dev)->gen == 5)
  2074. wm[0] = 13;
  2075. /* WaDoubleCursorLP3Latency:ivb */
  2076. if (IS_IVYBRIDGE(dev))
  2077. wm[3] *= 2;
  2078. }
  2079. static void intel_setup_wm_latency(struct drm_device *dev)
  2080. {
  2081. struct drm_i915_private *dev_priv = dev->dev_private;
  2082. intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
  2083. memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
  2084. sizeof(dev_priv->wm.pri_latency));
  2085. memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
  2086. sizeof(dev_priv->wm.pri_latency));
  2087. intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
  2088. intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
  2089. }
  2090. static void hsw_compute_wm_parameters(struct drm_device *dev,
  2091. struct hsw_pipe_wm_parameters *params,
  2092. struct hsw_wm_maximums *lp_max_1_2,
  2093. struct hsw_wm_maximums *lp_max_5_6)
  2094. {
  2095. struct drm_crtc *crtc;
  2096. struct drm_plane *plane;
  2097. enum pipe pipe;
  2098. int pipes_active = 0, sprites_enabled = 0;
  2099. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2100. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2101. struct hsw_pipe_wm_parameters *p;
  2102. pipe = intel_crtc->pipe;
  2103. p = &params[pipe];
  2104. p->active = intel_crtc_active(crtc);
  2105. if (!p->active)
  2106. continue;
  2107. pipes_active++;
  2108. p->pipe_htotal = intel_crtc->config.adjusted_mode.htotal;
  2109. p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
  2110. p->pri_bytes_per_pixel = crtc->fb->bits_per_pixel / 8;
  2111. p->cur_bytes_per_pixel = 4;
  2112. p->pri_horiz_pixels =
  2113. intel_crtc->config.requested_mode.hdisplay;
  2114. p->cur_horiz_pixels = 64;
  2115. }
  2116. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  2117. struct intel_plane *intel_plane = to_intel_plane(plane);
  2118. struct hsw_pipe_wm_parameters *p;
  2119. pipe = intel_plane->pipe;
  2120. p = &params[pipe];
  2121. p->sprite_enabled = intel_plane->wm.enabled;
  2122. p->spr_bytes_per_pixel = intel_plane->wm.bytes_per_pixel;
  2123. p->spr_horiz_pixels = intel_plane->wm.horiz_pixels;
  2124. if (p->sprite_enabled)
  2125. sprites_enabled++;
  2126. }
  2127. if (pipes_active > 1) {
  2128. lp_max_1_2->pri = lp_max_5_6->pri = sprites_enabled ? 128 : 256;
  2129. lp_max_1_2->spr = lp_max_5_6->spr = 128;
  2130. lp_max_1_2->cur = lp_max_5_6->cur = 64;
  2131. } else {
  2132. lp_max_1_2->pri = sprites_enabled ? 384 : 768;
  2133. lp_max_5_6->pri = sprites_enabled ? 128 : 768;
  2134. lp_max_1_2->spr = 384;
  2135. lp_max_5_6->spr = 640;
  2136. lp_max_1_2->cur = lp_max_5_6->cur = 255;
  2137. }
  2138. lp_max_1_2->fbc = lp_max_5_6->fbc = 15;
  2139. }
  2140. static void hsw_compute_wm_results(struct drm_device *dev,
  2141. struct hsw_pipe_wm_parameters *params,
  2142. struct hsw_wm_maximums *lp_maximums,
  2143. struct hsw_wm_values *results)
  2144. {
  2145. struct drm_i915_private *dev_priv = dev->dev_private;
  2146. struct drm_crtc *crtc;
  2147. struct hsw_lp_wm_result lp_results[4] = {};
  2148. enum pipe pipe;
  2149. int level, max_level, wm_lp;
  2150. for (level = 1; level <= 4; level++)
  2151. if (!hsw_compute_lp_wm(dev_priv, level,
  2152. lp_maximums, params,
  2153. &lp_results[level - 1]))
  2154. break;
  2155. max_level = level - 1;
  2156. /* The spec says it is preferred to disable FBC WMs instead of disabling
  2157. * a WM level. */
  2158. results->enable_fbc_wm = true;
  2159. for (level = 1; level <= max_level; level++) {
  2160. if (!lp_results[level - 1].fbc_enable) {
  2161. results->enable_fbc_wm = false;
  2162. break;
  2163. }
  2164. }
  2165. memset(results, 0, sizeof(*results));
  2166. for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
  2167. const struct hsw_lp_wm_result *r;
  2168. level = (max_level == 4 && wm_lp > 1) ? wm_lp + 1 : wm_lp;
  2169. if (level > max_level)
  2170. break;
  2171. r = &lp_results[level - 1];
  2172. results->wm_lp[wm_lp - 1] = HSW_WM_LP_VAL(level * 2,
  2173. r->fbc_val,
  2174. r->pri_val,
  2175. r->cur_val);
  2176. results->wm_lp_spr[wm_lp - 1] = r->spr_val;
  2177. }
  2178. for_each_pipe(pipe)
  2179. results->wm_pipe[pipe] = hsw_compute_wm_pipe(dev_priv, pipe,
  2180. &params[pipe]);
  2181. for_each_pipe(pipe) {
  2182. crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  2183. results->wm_linetime[pipe] = hsw_compute_linetime_wm(dev, crtc);
  2184. }
  2185. }
  2186. /* Find the result with the highest level enabled. Check for enable_fbc_wm in
  2187. * case both are at the same level. Prefer r1 in case they're the same. */
  2188. static struct hsw_wm_values *hsw_find_best_result(struct hsw_wm_values *r1,
  2189. struct hsw_wm_values *r2)
  2190. {
  2191. int i, val_r1 = 0, val_r2 = 0;
  2192. for (i = 0; i < 3; i++) {
  2193. if (r1->wm_lp[i] & WM3_LP_EN)
  2194. val_r1 = r1->wm_lp[i] & WM1_LP_LATENCY_MASK;
  2195. if (r2->wm_lp[i] & WM3_LP_EN)
  2196. val_r2 = r2->wm_lp[i] & WM1_LP_LATENCY_MASK;
  2197. }
  2198. if (val_r1 == val_r2) {
  2199. if (r2->enable_fbc_wm && !r1->enable_fbc_wm)
  2200. return r2;
  2201. else
  2202. return r1;
  2203. } else if (val_r1 > val_r2) {
  2204. return r1;
  2205. } else {
  2206. return r2;
  2207. }
  2208. }
  2209. /*
  2210. * The spec says we shouldn't write when we don't need, because every write
  2211. * causes WMs to be re-evaluated, expending some power.
  2212. */
  2213. static void hsw_write_wm_values(struct drm_i915_private *dev_priv,
  2214. struct hsw_wm_values *results,
  2215. enum hsw_data_buf_partitioning partitioning)
  2216. {
  2217. struct hsw_wm_values previous;
  2218. uint32_t val;
  2219. enum hsw_data_buf_partitioning prev_partitioning;
  2220. bool prev_enable_fbc_wm;
  2221. previous.wm_pipe[0] = I915_READ(WM0_PIPEA_ILK);
  2222. previous.wm_pipe[1] = I915_READ(WM0_PIPEB_ILK);
  2223. previous.wm_pipe[2] = I915_READ(WM0_PIPEC_IVB);
  2224. previous.wm_lp[0] = I915_READ(WM1_LP_ILK);
  2225. previous.wm_lp[1] = I915_READ(WM2_LP_ILK);
  2226. previous.wm_lp[2] = I915_READ(WM3_LP_ILK);
  2227. previous.wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
  2228. previous.wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
  2229. previous.wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
  2230. previous.wm_linetime[0] = I915_READ(PIPE_WM_LINETIME(PIPE_A));
  2231. previous.wm_linetime[1] = I915_READ(PIPE_WM_LINETIME(PIPE_B));
  2232. previous.wm_linetime[2] = I915_READ(PIPE_WM_LINETIME(PIPE_C));
  2233. prev_partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
  2234. HSW_DATA_BUF_PART_5_6 : HSW_DATA_BUF_PART_1_2;
  2235. prev_enable_fbc_wm = !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
  2236. if (memcmp(results->wm_pipe, previous.wm_pipe,
  2237. sizeof(results->wm_pipe)) == 0 &&
  2238. memcmp(results->wm_lp, previous.wm_lp,
  2239. sizeof(results->wm_lp)) == 0 &&
  2240. memcmp(results->wm_lp_spr, previous.wm_lp_spr,
  2241. sizeof(results->wm_lp_spr)) == 0 &&
  2242. memcmp(results->wm_linetime, previous.wm_linetime,
  2243. sizeof(results->wm_linetime)) == 0 &&
  2244. partitioning == prev_partitioning &&
  2245. results->enable_fbc_wm == prev_enable_fbc_wm)
  2246. return;
  2247. if (previous.wm_lp[2] != 0)
  2248. I915_WRITE(WM3_LP_ILK, 0);
  2249. if (previous.wm_lp[1] != 0)
  2250. I915_WRITE(WM2_LP_ILK, 0);
  2251. if (previous.wm_lp[0] != 0)
  2252. I915_WRITE(WM1_LP_ILK, 0);
  2253. if (previous.wm_pipe[0] != results->wm_pipe[0])
  2254. I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
  2255. if (previous.wm_pipe[1] != results->wm_pipe[1])
  2256. I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
  2257. if (previous.wm_pipe[2] != results->wm_pipe[2])
  2258. I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
  2259. if (previous.wm_linetime[0] != results->wm_linetime[0])
  2260. I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
  2261. if (previous.wm_linetime[1] != results->wm_linetime[1])
  2262. I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
  2263. if (previous.wm_linetime[2] != results->wm_linetime[2])
  2264. I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
  2265. if (prev_partitioning != partitioning) {
  2266. val = I915_READ(WM_MISC);
  2267. if (partitioning == HSW_DATA_BUF_PART_1_2)
  2268. val &= ~WM_MISC_DATA_PARTITION_5_6;
  2269. else
  2270. val |= WM_MISC_DATA_PARTITION_5_6;
  2271. I915_WRITE(WM_MISC, val);
  2272. }
  2273. if (prev_enable_fbc_wm != results->enable_fbc_wm) {
  2274. val = I915_READ(DISP_ARB_CTL);
  2275. if (results->enable_fbc_wm)
  2276. val &= ~DISP_FBC_WM_DIS;
  2277. else
  2278. val |= DISP_FBC_WM_DIS;
  2279. I915_WRITE(DISP_ARB_CTL, val);
  2280. }
  2281. if (previous.wm_lp_spr[0] != results->wm_lp_spr[0])
  2282. I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
  2283. if (previous.wm_lp_spr[1] != results->wm_lp_spr[1])
  2284. I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
  2285. if (previous.wm_lp_spr[2] != results->wm_lp_spr[2])
  2286. I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
  2287. if (results->wm_lp[0] != 0)
  2288. I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
  2289. if (results->wm_lp[1] != 0)
  2290. I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
  2291. if (results->wm_lp[2] != 0)
  2292. I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
  2293. }
  2294. static void haswell_update_wm(struct drm_device *dev)
  2295. {
  2296. struct drm_i915_private *dev_priv = dev->dev_private;
  2297. struct hsw_wm_maximums lp_max_1_2, lp_max_5_6;
  2298. struct hsw_pipe_wm_parameters params[3];
  2299. struct hsw_wm_values results_1_2, results_5_6, *best_results;
  2300. enum hsw_data_buf_partitioning partitioning;
  2301. hsw_compute_wm_parameters(dev, params, &lp_max_1_2, &lp_max_5_6);
  2302. hsw_compute_wm_results(dev, params,
  2303. &lp_max_1_2, &results_1_2);
  2304. if (lp_max_1_2.pri != lp_max_5_6.pri) {
  2305. hsw_compute_wm_results(dev, params,
  2306. &lp_max_5_6, &results_5_6);
  2307. best_results = hsw_find_best_result(&results_1_2, &results_5_6);
  2308. } else {
  2309. best_results = &results_1_2;
  2310. }
  2311. partitioning = (best_results == &results_1_2) ?
  2312. HSW_DATA_BUF_PART_1_2 : HSW_DATA_BUF_PART_5_6;
  2313. hsw_write_wm_values(dev_priv, best_results, partitioning);
  2314. }
  2315. static void haswell_update_sprite_wm(struct drm_device *dev, int pipe,
  2316. uint32_t sprite_width, int pixel_size,
  2317. bool enabled, bool scaled)
  2318. {
  2319. struct drm_plane *plane;
  2320. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  2321. struct intel_plane *intel_plane = to_intel_plane(plane);
  2322. if (intel_plane->pipe == pipe) {
  2323. intel_plane->wm.enabled = enabled;
  2324. intel_plane->wm.scaled = scaled;
  2325. intel_plane->wm.horiz_pixels = sprite_width;
  2326. intel_plane->wm.bytes_per_pixel = pixel_size;
  2327. break;
  2328. }
  2329. }
  2330. haswell_update_wm(dev);
  2331. }
  2332. static bool
  2333. sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
  2334. uint32_t sprite_width, int pixel_size,
  2335. const struct intel_watermark_params *display,
  2336. int display_latency_ns, int *sprite_wm)
  2337. {
  2338. struct drm_crtc *crtc;
  2339. int clock;
  2340. int entries, tlb_miss;
  2341. crtc = intel_get_crtc_for_plane(dev, plane);
  2342. if (!intel_crtc_active(crtc)) {
  2343. *sprite_wm = display->guard_size;
  2344. return false;
  2345. }
  2346. clock = crtc->mode.clock;
  2347. /* Use the small buffer method to calculate the sprite watermark */
  2348. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  2349. tlb_miss = display->fifo_size*display->cacheline_size -
  2350. sprite_width * 8;
  2351. if (tlb_miss > 0)
  2352. entries += tlb_miss;
  2353. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  2354. *sprite_wm = entries + display->guard_size;
  2355. if (*sprite_wm > (int)display->max_wm)
  2356. *sprite_wm = display->max_wm;
  2357. return true;
  2358. }
  2359. static bool
  2360. sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
  2361. uint32_t sprite_width, int pixel_size,
  2362. const struct intel_watermark_params *display,
  2363. int latency_ns, int *sprite_wm)
  2364. {
  2365. struct drm_crtc *crtc;
  2366. unsigned long line_time_us;
  2367. int clock;
  2368. int line_count, line_size;
  2369. int small, large;
  2370. int entries;
  2371. if (!latency_ns) {
  2372. *sprite_wm = 0;
  2373. return false;
  2374. }
  2375. crtc = intel_get_crtc_for_plane(dev, plane);
  2376. clock = crtc->mode.clock;
  2377. if (!clock) {
  2378. *sprite_wm = 0;
  2379. return false;
  2380. }
  2381. line_time_us = (sprite_width * 1000) / clock;
  2382. if (!line_time_us) {
  2383. *sprite_wm = 0;
  2384. return false;
  2385. }
  2386. line_count = (latency_ns / line_time_us + 1000) / 1000;
  2387. line_size = sprite_width * pixel_size;
  2388. /* Use the minimum of the small and large buffer method for primary */
  2389. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  2390. large = line_count * line_size;
  2391. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  2392. *sprite_wm = entries + display->guard_size;
  2393. return *sprite_wm > 0x3ff ? false : true;
  2394. }
  2395. static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
  2396. uint32_t sprite_width, int pixel_size,
  2397. bool enable, bool scaled)
  2398. {
  2399. struct drm_i915_private *dev_priv = dev->dev_private;
  2400. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  2401. u32 val;
  2402. int sprite_wm, reg;
  2403. int ret;
  2404. if (!enable)
  2405. return;
  2406. switch (pipe) {
  2407. case 0:
  2408. reg = WM0_PIPEA_ILK;
  2409. break;
  2410. case 1:
  2411. reg = WM0_PIPEB_ILK;
  2412. break;
  2413. case 2:
  2414. reg = WM0_PIPEC_IVB;
  2415. break;
  2416. default:
  2417. return; /* bad pipe */
  2418. }
  2419. ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
  2420. &sandybridge_display_wm_info,
  2421. latency, &sprite_wm);
  2422. if (!ret) {
  2423. DRM_DEBUG_KMS("failed to compute sprite wm for pipe %c\n",
  2424. pipe_name(pipe));
  2425. return;
  2426. }
  2427. val = I915_READ(reg);
  2428. val &= ~WM0_PIPE_SPRITE_MASK;
  2429. I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
  2430. DRM_DEBUG_KMS("sprite watermarks For pipe %c - %d\n", pipe_name(pipe), sprite_wm);
  2431. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  2432. pixel_size,
  2433. &sandybridge_display_srwm_info,
  2434. SNB_READ_WM1_LATENCY() * 500,
  2435. &sprite_wm);
  2436. if (!ret) {
  2437. DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %c\n",
  2438. pipe_name(pipe));
  2439. return;
  2440. }
  2441. I915_WRITE(WM1S_LP_ILK, sprite_wm);
  2442. /* Only IVB has two more LP watermarks for sprite */
  2443. if (!IS_IVYBRIDGE(dev))
  2444. return;
  2445. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  2446. pixel_size,
  2447. &sandybridge_display_srwm_info,
  2448. SNB_READ_WM2_LATENCY() * 500,
  2449. &sprite_wm);
  2450. if (!ret) {
  2451. DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %c\n",
  2452. pipe_name(pipe));
  2453. return;
  2454. }
  2455. I915_WRITE(WM2S_LP_IVB, sprite_wm);
  2456. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  2457. pixel_size,
  2458. &sandybridge_display_srwm_info,
  2459. SNB_READ_WM3_LATENCY() * 500,
  2460. &sprite_wm);
  2461. if (!ret) {
  2462. DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %c\n",
  2463. pipe_name(pipe));
  2464. return;
  2465. }
  2466. I915_WRITE(WM3S_LP_IVB, sprite_wm);
  2467. }
  2468. /**
  2469. * intel_update_watermarks - update FIFO watermark values based on current modes
  2470. *
  2471. * Calculate watermark values for the various WM regs based on current mode
  2472. * and plane configuration.
  2473. *
  2474. * There are several cases to deal with here:
  2475. * - normal (i.e. non-self-refresh)
  2476. * - self-refresh (SR) mode
  2477. * - lines are large relative to FIFO size (buffer can hold up to 2)
  2478. * - lines are small relative to FIFO size (buffer can hold more than 2
  2479. * lines), so need to account for TLB latency
  2480. *
  2481. * The normal calculation is:
  2482. * watermark = dotclock * bytes per pixel * latency
  2483. * where latency is platform & configuration dependent (we assume pessimal
  2484. * values here).
  2485. *
  2486. * The SR calculation is:
  2487. * watermark = (trunc(latency/line time)+1) * surface width *
  2488. * bytes per pixel
  2489. * where
  2490. * line time = htotal / dotclock
  2491. * surface width = hdisplay for normal plane and 64 for cursor
  2492. * and latency is assumed to be high, as above.
  2493. *
  2494. * The final value programmed to the register should always be rounded up,
  2495. * and include an extra 2 entries to account for clock crossings.
  2496. *
  2497. * We don't use the sprite, so we can ignore that. And on Crestline we have
  2498. * to set the non-SR watermarks to 8.
  2499. */
  2500. void intel_update_watermarks(struct drm_device *dev)
  2501. {
  2502. struct drm_i915_private *dev_priv = dev->dev_private;
  2503. if (dev_priv->display.update_wm)
  2504. dev_priv->display.update_wm(dev);
  2505. }
  2506. void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
  2507. uint32_t sprite_width, int pixel_size,
  2508. bool enable, bool scaled)
  2509. {
  2510. struct drm_i915_private *dev_priv = dev->dev_private;
  2511. if (dev_priv->display.update_sprite_wm)
  2512. dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
  2513. pixel_size, enable, scaled);
  2514. }
  2515. static struct drm_i915_gem_object *
  2516. intel_alloc_context_page(struct drm_device *dev)
  2517. {
  2518. struct drm_i915_gem_object *ctx;
  2519. int ret;
  2520. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2521. ctx = i915_gem_alloc_object(dev, 4096);
  2522. if (!ctx) {
  2523. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  2524. return NULL;
  2525. }
  2526. ret = i915_gem_obj_ggtt_pin(ctx, 4096, true, false);
  2527. if (ret) {
  2528. DRM_ERROR("failed to pin power context: %d\n", ret);
  2529. goto err_unref;
  2530. }
  2531. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  2532. if (ret) {
  2533. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  2534. goto err_unpin;
  2535. }
  2536. return ctx;
  2537. err_unpin:
  2538. i915_gem_object_unpin(ctx);
  2539. err_unref:
  2540. drm_gem_object_unreference(&ctx->base);
  2541. return NULL;
  2542. }
  2543. /**
  2544. * Lock protecting IPS related data structures
  2545. */
  2546. DEFINE_SPINLOCK(mchdev_lock);
  2547. /* Global for IPS driver to get at the current i915 device. Protected by
  2548. * mchdev_lock. */
  2549. static struct drm_i915_private *i915_mch_dev;
  2550. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  2551. {
  2552. struct drm_i915_private *dev_priv = dev->dev_private;
  2553. u16 rgvswctl;
  2554. assert_spin_locked(&mchdev_lock);
  2555. rgvswctl = I915_READ16(MEMSWCTL);
  2556. if (rgvswctl & MEMCTL_CMD_STS) {
  2557. DRM_DEBUG("gpu busy, RCS change rejected\n");
  2558. return false; /* still busy with another command */
  2559. }
  2560. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  2561. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  2562. I915_WRITE16(MEMSWCTL, rgvswctl);
  2563. POSTING_READ16(MEMSWCTL);
  2564. rgvswctl |= MEMCTL_CMD_STS;
  2565. I915_WRITE16(MEMSWCTL, rgvswctl);
  2566. return true;
  2567. }
  2568. static void ironlake_enable_drps(struct drm_device *dev)
  2569. {
  2570. struct drm_i915_private *dev_priv = dev->dev_private;
  2571. u32 rgvmodectl = I915_READ(MEMMODECTL);
  2572. u8 fmax, fmin, fstart, vstart;
  2573. spin_lock_irq(&mchdev_lock);
  2574. /* Enable temp reporting */
  2575. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  2576. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  2577. /* 100ms RC evaluation intervals */
  2578. I915_WRITE(RCUPEI, 100000);
  2579. I915_WRITE(RCDNEI, 100000);
  2580. /* Set max/min thresholds to 90ms and 80ms respectively */
  2581. I915_WRITE(RCBMAXAVG, 90000);
  2582. I915_WRITE(RCBMINAVG, 80000);
  2583. I915_WRITE(MEMIHYST, 1);
  2584. /* Set up min, max, and cur for interrupt handling */
  2585. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  2586. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  2587. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  2588. MEMMODE_FSTART_SHIFT;
  2589. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  2590. PXVFREQ_PX_SHIFT;
  2591. dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
  2592. dev_priv->ips.fstart = fstart;
  2593. dev_priv->ips.max_delay = fstart;
  2594. dev_priv->ips.min_delay = fmin;
  2595. dev_priv->ips.cur_delay = fstart;
  2596. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  2597. fmax, fmin, fstart);
  2598. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  2599. /*
  2600. * Interrupts will be enabled in ironlake_irq_postinstall
  2601. */
  2602. I915_WRITE(VIDSTART, vstart);
  2603. POSTING_READ(VIDSTART);
  2604. rgvmodectl |= MEMMODE_SWMODE_EN;
  2605. I915_WRITE(MEMMODECTL, rgvmodectl);
  2606. if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  2607. DRM_ERROR("stuck trying to change perf mode\n");
  2608. mdelay(1);
  2609. ironlake_set_drps(dev, fstart);
  2610. dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  2611. I915_READ(0x112e0);
  2612. dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
  2613. dev_priv->ips.last_count2 = I915_READ(0x112f4);
  2614. getrawmonotonic(&dev_priv->ips.last_time2);
  2615. spin_unlock_irq(&mchdev_lock);
  2616. }
  2617. static void ironlake_disable_drps(struct drm_device *dev)
  2618. {
  2619. struct drm_i915_private *dev_priv = dev->dev_private;
  2620. u16 rgvswctl;
  2621. spin_lock_irq(&mchdev_lock);
  2622. rgvswctl = I915_READ16(MEMSWCTL);
  2623. /* Ack interrupts, disable EFC interrupt */
  2624. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  2625. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  2626. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  2627. I915_WRITE(DEIIR, DE_PCU_EVENT);
  2628. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  2629. /* Go back to the starting frequency */
  2630. ironlake_set_drps(dev, dev_priv->ips.fstart);
  2631. mdelay(1);
  2632. rgvswctl |= MEMCTL_CMD_STS;
  2633. I915_WRITE(MEMSWCTL, rgvswctl);
  2634. mdelay(1);
  2635. spin_unlock_irq(&mchdev_lock);
  2636. }
  2637. /* There's a funny hw issue where the hw returns all 0 when reading from
  2638. * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
  2639. * ourselves, instead of doing a rmw cycle (which might result in us clearing
  2640. * all limits and the gpu stuck at whatever frequency it is at atm).
  2641. */
  2642. static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 *val)
  2643. {
  2644. u32 limits;
  2645. limits = 0;
  2646. if (*val >= dev_priv->rps.max_delay)
  2647. *val = dev_priv->rps.max_delay;
  2648. limits |= dev_priv->rps.max_delay << 24;
  2649. /* Only set the down limit when we've reached the lowest level to avoid
  2650. * getting more interrupts, otherwise leave this clear. This prevents a
  2651. * race in the hw when coming out of rc6: There's a tiny window where
  2652. * the hw runs at the minimal clock before selecting the desired
  2653. * frequency, if the down threshold expires in that window we will not
  2654. * receive a down interrupt. */
  2655. if (*val <= dev_priv->rps.min_delay) {
  2656. *val = dev_priv->rps.min_delay;
  2657. limits |= dev_priv->rps.min_delay << 16;
  2658. }
  2659. return limits;
  2660. }
  2661. void gen6_set_rps(struct drm_device *dev, u8 val)
  2662. {
  2663. struct drm_i915_private *dev_priv = dev->dev_private;
  2664. u32 limits = gen6_rps_limits(dev_priv, &val);
  2665. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2666. WARN_ON(val > dev_priv->rps.max_delay);
  2667. WARN_ON(val < dev_priv->rps.min_delay);
  2668. if (val == dev_priv->rps.cur_delay)
  2669. return;
  2670. if (IS_HASWELL(dev))
  2671. I915_WRITE(GEN6_RPNSWREQ,
  2672. HSW_FREQUENCY(val));
  2673. else
  2674. I915_WRITE(GEN6_RPNSWREQ,
  2675. GEN6_FREQUENCY(val) |
  2676. GEN6_OFFSET(0) |
  2677. GEN6_AGGRESSIVE_TURBO);
  2678. /* Make sure we continue to get interrupts
  2679. * until we hit the minimum or maximum frequencies.
  2680. */
  2681. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, limits);
  2682. POSTING_READ(GEN6_RPNSWREQ);
  2683. dev_priv->rps.cur_delay = val;
  2684. trace_intel_gpu_freq_change(val * 50);
  2685. }
  2686. /*
  2687. * Wait until the previous freq change has completed,
  2688. * or the timeout elapsed, and then update our notion
  2689. * of the current GPU frequency.
  2690. */
  2691. static void vlv_update_rps_cur_delay(struct drm_i915_private *dev_priv)
  2692. {
  2693. u32 pval;
  2694. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2695. if (wait_for(((pval = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS)) & GENFREQSTATUS) == 0, 10))
  2696. DRM_DEBUG_DRIVER("timed out waiting for Punit\n");
  2697. pval >>= 8;
  2698. if (pval != dev_priv->rps.cur_delay)
  2699. DRM_DEBUG_DRIVER("Punit overrode GPU freq: %d MHz (%u) requested, but got %d Mhz (%u)\n",
  2700. vlv_gpu_freq(dev_priv->mem_freq, dev_priv->rps.cur_delay),
  2701. dev_priv->rps.cur_delay,
  2702. vlv_gpu_freq(dev_priv->mem_freq, pval), pval);
  2703. dev_priv->rps.cur_delay = pval;
  2704. }
  2705. void valleyview_set_rps(struct drm_device *dev, u8 val)
  2706. {
  2707. struct drm_i915_private *dev_priv = dev->dev_private;
  2708. gen6_rps_limits(dev_priv, &val);
  2709. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2710. WARN_ON(val > dev_priv->rps.max_delay);
  2711. WARN_ON(val < dev_priv->rps.min_delay);
  2712. vlv_update_rps_cur_delay(dev_priv);
  2713. DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
  2714. vlv_gpu_freq(dev_priv->mem_freq,
  2715. dev_priv->rps.cur_delay),
  2716. dev_priv->rps.cur_delay,
  2717. vlv_gpu_freq(dev_priv->mem_freq, val), val);
  2718. if (val == dev_priv->rps.cur_delay)
  2719. return;
  2720. vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
  2721. dev_priv->rps.cur_delay = val;
  2722. trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv->mem_freq, val));
  2723. }
  2724. static void gen6_disable_rps_interrupts(struct drm_device *dev)
  2725. {
  2726. struct drm_i915_private *dev_priv = dev->dev_private;
  2727. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  2728. I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) & ~GEN6_PM_RPS_EVENTS);
  2729. /* Complete PM interrupt masking here doesn't race with the rps work
  2730. * item again unmasking PM interrupts because that is using a different
  2731. * register (PMIMR) to mask PM interrupts. The only risk is in leaving
  2732. * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
  2733. spin_lock_irq(&dev_priv->irq_lock);
  2734. dev_priv->rps.pm_iir = 0;
  2735. spin_unlock_irq(&dev_priv->irq_lock);
  2736. I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
  2737. }
  2738. static void gen6_disable_rps(struct drm_device *dev)
  2739. {
  2740. struct drm_i915_private *dev_priv = dev->dev_private;
  2741. I915_WRITE(GEN6_RC_CONTROL, 0);
  2742. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  2743. gen6_disable_rps_interrupts(dev);
  2744. }
  2745. static void valleyview_disable_rps(struct drm_device *dev)
  2746. {
  2747. struct drm_i915_private *dev_priv = dev->dev_private;
  2748. I915_WRITE(GEN6_RC_CONTROL, 0);
  2749. gen6_disable_rps_interrupts(dev);
  2750. if (dev_priv->vlv_pctx) {
  2751. drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
  2752. dev_priv->vlv_pctx = NULL;
  2753. }
  2754. }
  2755. int intel_enable_rc6(const struct drm_device *dev)
  2756. {
  2757. /* No RC6 before Ironlake */
  2758. if (INTEL_INFO(dev)->gen < 5)
  2759. return 0;
  2760. /* Respect the kernel parameter if it is set */
  2761. if (i915_enable_rc6 >= 0)
  2762. return i915_enable_rc6;
  2763. /* Disable RC6 on Ironlake */
  2764. if (INTEL_INFO(dev)->gen == 5)
  2765. return 0;
  2766. if (IS_HASWELL(dev)) {
  2767. DRM_DEBUG_DRIVER("Haswell: only RC6 available\n");
  2768. return INTEL_RC6_ENABLE;
  2769. }
  2770. /* snb/ivb have more than one rc6 state. */
  2771. if (INTEL_INFO(dev)->gen == 6) {
  2772. DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
  2773. return INTEL_RC6_ENABLE;
  2774. }
  2775. DRM_DEBUG_DRIVER("RC6 and deep RC6 enabled\n");
  2776. return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
  2777. }
  2778. static void gen6_enable_rps_interrupts(struct drm_device *dev)
  2779. {
  2780. struct drm_i915_private *dev_priv = dev->dev_private;
  2781. spin_lock_irq(&dev_priv->irq_lock);
  2782. WARN_ON(dev_priv->rps.pm_iir);
  2783. I915_WRITE(GEN6_PMIMR, I915_READ(GEN6_PMIMR) & ~GEN6_PM_RPS_EVENTS);
  2784. I915_WRITE(GEN6_PMIIR, GEN6_PM_RPS_EVENTS);
  2785. spin_unlock_irq(&dev_priv->irq_lock);
  2786. /* unmask all PM interrupts */
  2787. I915_WRITE(GEN6_PMINTRMSK, 0);
  2788. }
  2789. static void gen6_enable_rps(struct drm_device *dev)
  2790. {
  2791. struct drm_i915_private *dev_priv = dev->dev_private;
  2792. struct intel_ring_buffer *ring;
  2793. u32 rp_state_cap;
  2794. u32 gt_perf_status;
  2795. u32 rc6vids, pcu_mbox, rc6_mask = 0;
  2796. u32 gtfifodbg;
  2797. int rc6_mode;
  2798. int i, ret;
  2799. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2800. /* Here begins a magic sequence of register writes to enable
  2801. * auto-downclocking.
  2802. *
  2803. * Perhaps there might be some value in exposing these to
  2804. * userspace...
  2805. */
  2806. I915_WRITE(GEN6_RC_STATE, 0);
  2807. /* Clear the DBG now so we don't confuse earlier errors */
  2808. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  2809. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  2810. I915_WRITE(GTFIFODBG, gtfifodbg);
  2811. }
  2812. gen6_gt_force_wake_get(dev_priv);
  2813. rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  2814. gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  2815. /* In units of 50MHz */
  2816. dev_priv->rps.hw_max = dev_priv->rps.max_delay = rp_state_cap & 0xff;
  2817. dev_priv->rps.min_delay = (rp_state_cap & 0xff0000) >> 16;
  2818. dev_priv->rps.cur_delay = 0;
  2819. /* disable the counters and set deterministic thresholds */
  2820. I915_WRITE(GEN6_RC_CONTROL, 0);
  2821. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  2822. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  2823. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  2824. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  2825. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  2826. for_each_ring(ring, dev_priv, i)
  2827. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  2828. I915_WRITE(GEN6_RC_SLEEP, 0);
  2829. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  2830. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  2831. I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
  2832. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  2833. /* Check if we are enabling RC6 */
  2834. rc6_mode = intel_enable_rc6(dev_priv->dev);
  2835. if (rc6_mode & INTEL_RC6_ENABLE)
  2836. rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
  2837. /* We don't use those on Haswell */
  2838. if (!IS_HASWELL(dev)) {
  2839. if (rc6_mode & INTEL_RC6p_ENABLE)
  2840. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  2841. if (rc6_mode & INTEL_RC6pp_ENABLE)
  2842. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  2843. }
  2844. DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
  2845. (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
  2846. (rc6_mask & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
  2847. (rc6_mask & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
  2848. I915_WRITE(GEN6_RC_CONTROL,
  2849. rc6_mask |
  2850. GEN6_RC_CTL_EI_MODE(1) |
  2851. GEN6_RC_CTL_HW_ENABLE);
  2852. if (IS_HASWELL(dev)) {
  2853. I915_WRITE(GEN6_RPNSWREQ,
  2854. HSW_FREQUENCY(10));
  2855. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  2856. HSW_FREQUENCY(12));
  2857. } else {
  2858. I915_WRITE(GEN6_RPNSWREQ,
  2859. GEN6_FREQUENCY(10) |
  2860. GEN6_OFFSET(0) |
  2861. GEN6_AGGRESSIVE_TURBO);
  2862. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  2863. GEN6_FREQUENCY(12));
  2864. }
  2865. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  2866. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  2867. dev_priv->rps.max_delay << 24 |
  2868. dev_priv->rps.min_delay << 16);
  2869. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  2870. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  2871. I915_WRITE(GEN6_RP_UP_EI, 66000);
  2872. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  2873. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  2874. I915_WRITE(GEN6_RP_CONTROL,
  2875. GEN6_RP_MEDIA_TURBO |
  2876. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  2877. GEN6_RP_MEDIA_IS_GFX |
  2878. GEN6_RP_ENABLE |
  2879. GEN6_RP_UP_BUSY_AVG |
  2880. (IS_HASWELL(dev) ? GEN7_RP_DOWN_IDLE_AVG : GEN6_RP_DOWN_IDLE_CONT));
  2881. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
  2882. if (!ret) {
  2883. pcu_mbox = 0;
  2884. ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
  2885. if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
  2886. DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
  2887. (dev_priv->rps.max_delay & 0xff) * 50,
  2888. (pcu_mbox & 0xff) * 50);
  2889. dev_priv->rps.hw_max = pcu_mbox & 0xff;
  2890. }
  2891. } else {
  2892. DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
  2893. }
  2894. gen6_set_rps(dev_priv->dev, (gt_perf_status & 0xff00) >> 8);
  2895. gen6_enable_rps_interrupts(dev);
  2896. rc6vids = 0;
  2897. ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
  2898. if (IS_GEN6(dev) && ret) {
  2899. DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
  2900. } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
  2901. DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
  2902. GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
  2903. rc6vids &= 0xffff00;
  2904. rc6vids |= GEN6_ENCODE_RC6_VID(450);
  2905. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
  2906. if (ret)
  2907. DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
  2908. }
  2909. gen6_gt_force_wake_put(dev_priv);
  2910. }
  2911. static void gen6_update_ring_freq(struct drm_device *dev)
  2912. {
  2913. struct drm_i915_private *dev_priv = dev->dev_private;
  2914. int min_freq = 15;
  2915. unsigned int gpu_freq;
  2916. unsigned int max_ia_freq, min_ring_freq;
  2917. int scaling_factor = 180;
  2918. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  2919. max_ia_freq = cpufreq_quick_get_max(0);
  2920. /*
  2921. * Default to measured freq if none found, PCU will ensure we don't go
  2922. * over
  2923. */
  2924. if (!max_ia_freq)
  2925. max_ia_freq = tsc_khz;
  2926. /* Convert from kHz to MHz */
  2927. max_ia_freq /= 1000;
  2928. min_ring_freq = I915_READ(MCHBAR_MIRROR_BASE_SNB + DCLK);
  2929. /* convert DDR frequency from units of 133.3MHz to bandwidth */
  2930. min_ring_freq = (2 * 4 * min_ring_freq + 2) / 3;
  2931. /*
  2932. * For each potential GPU frequency, load a ring frequency we'd like
  2933. * to use for memory access. We do this by specifying the IA frequency
  2934. * the PCU should use as a reference to determine the ring frequency.
  2935. */
  2936. for (gpu_freq = dev_priv->rps.max_delay; gpu_freq >= dev_priv->rps.min_delay;
  2937. gpu_freq--) {
  2938. int diff = dev_priv->rps.max_delay - gpu_freq;
  2939. unsigned int ia_freq = 0, ring_freq = 0;
  2940. if (IS_HASWELL(dev)) {
  2941. ring_freq = (gpu_freq * 5 + 3) / 4;
  2942. ring_freq = max(min_ring_freq, ring_freq);
  2943. /* leave ia_freq as the default, chosen by cpufreq */
  2944. } else {
  2945. /* On older processors, there is no separate ring
  2946. * clock domain, so in order to boost the bandwidth
  2947. * of the ring, we need to upclock the CPU (ia_freq).
  2948. *
  2949. * For GPU frequencies less than 750MHz,
  2950. * just use the lowest ring freq.
  2951. */
  2952. if (gpu_freq < min_freq)
  2953. ia_freq = 800;
  2954. else
  2955. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  2956. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  2957. }
  2958. sandybridge_pcode_write(dev_priv,
  2959. GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
  2960. ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
  2961. ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
  2962. gpu_freq);
  2963. }
  2964. }
  2965. int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
  2966. {
  2967. u32 val, rp0;
  2968. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  2969. rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
  2970. /* Clamp to max */
  2971. rp0 = min_t(u32, rp0, 0xea);
  2972. return rp0;
  2973. }
  2974. static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  2975. {
  2976. u32 val, rpe;
  2977. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
  2978. rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
  2979. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
  2980. rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
  2981. return rpe;
  2982. }
  2983. int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
  2984. {
  2985. return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
  2986. }
  2987. static void vlv_rps_timer_work(struct work_struct *work)
  2988. {
  2989. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  2990. rps.vlv_work.work);
  2991. /*
  2992. * Timer fired, we must be idle. Drop to min voltage state.
  2993. * Note: we use RPe here since it should match the
  2994. * Vmin we were shooting for. That should give us better
  2995. * perf when we come back out of RC6 than if we used the
  2996. * min freq available.
  2997. */
  2998. mutex_lock(&dev_priv->rps.hw_lock);
  2999. if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
  3000. valleyview_set_rps(dev_priv->dev, dev_priv->rps.rpe_delay);
  3001. mutex_unlock(&dev_priv->rps.hw_lock);
  3002. }
  3003. static void valleyview_setup_pctx(struct drm_device *dev)
  3004. {
  3005. struct drm_i915_private *dev_priv = dev->dev_private;
  3006. struct drm_i915_gem_object *pctx;
  3007. unsigned long pctx_paddr;
  3008. u32 pcbr;
  3009. int pctx_size = 24*1024;
  3010. pcbr = I915_READ(VLV_PCBR);
  3011. if (pcbr) {
  3012. /* BIOS set it up already, grab the pre-alloc'd space */
  3013. int pcbr_offset;
  3014. pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
  3015. pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
  3016. pcbr_offset,
  3017. I915_GTT_OFFSET_NONE,
  3018. pctx_size);
  3019. goto out;
  3020. }
  3021. /*
  3022. * From the Gunit register HAS:
  3023. * The Gfx driver is expected to program this register and ensure
  3024. * proper allocation within Gfx stolen memory. For example, this
  3025. * register should be programmed such than the PCBR range does not
  3026. * overlap with other ranges, such as the frame buffer, protected
  3027. * memory, or any other relevant ranges.
  3028. */
  3029. pctx = i915_gem_object_create_stolen(dev, pctx_size);
  3030. if (!pctx) {
  3031. DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
  3032. return;
  3033. }
  3034. pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
  3035. I915_WRITE(VLV_PCBR, pctx_paddr);
  3036. out:
  3037. dev_priv->vlv_pctx = pctx;
  3038. }
  3039. static void valleyview_enable_rps(struct drm_device *dev)
  3040. {
  3041. struct drm_i915_private *dev_priv = dev->dev_private;
  3042. struct intel_ring_buffer *ring;
  3043. u32 gtfifodbg, val;
  3044. int i;
  3045. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3046. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  3047. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  3048. I915_WRITE(GTFIFODBG, gtfifodbg);
  3049. }
  3050. valleyview_setup_pctx(dev);
  3051. gen6_gt_force_wake_get(dev_priv);
  3052. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  3053. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  3054. I915_WRITE(GEN6_RP_UP_EI, 66000);
  3055. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  3056. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  3057. I915_WRITE(GEN6_RP_CONTROL,
  3058. GEN6_RP_MEDIA_TURBO |
  3059. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3060. GEN6_RP_MEDIA_IS_GFX |
  3061. GEN6_RP_ENABLE |
  3062. GEN6_RP_UP_BUSY_AVG |
  3063. GEN6_RP_DOWN_IDLE_CONT);
  3064. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
  3065. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  3066. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  3067. for_each_ring(ring, dev_priv, i)
  3068. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  3069. I915_WRITE(GEN6_RC6_THRESHOLD, 0xc350);
  3070. /* allows RC6 residency counter to work */
  3071. I915_WRITE(0x138104, _MASKED_BIT_ENABLE(0x3));
  3072. I915_WRITE(GEN6_RC_CONTROL,
  3073. GEN7_RC_CTL_TO_MODE);
  3074. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  3075. switch ((val >> 6) & 3) {
  3076. case 0:
  3077. case 1:
  3078. dev_priv->mem_freq = 800;
  3079. break;
  3080. case 2:
  3081. dev_priv->mem_freq = 1066;
  3082. break;
  3083. case 3:
  3084. dev_priv->mem_freq = 1333;
  3085. break;
  3086. }
  3087. DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
  3088. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
  3089. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  3090. dev_priv->rps.cur_delay = (val >> 8) & 0xff;
  3091. DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
  3092. vlv_gpu_freq(dev_priv->mem_freq,
  3093. dev_priv->rps.cur_delay),
  3094. dev_priv->rps.cur_delay);
  3095. dev_priv->rps.max_delay = valleyview_rps_max_freq(dev_priv);
  3096. dev_priv->rps.hw_max = dev_priv->rps.max_delay;
  3097. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  3098. vlv_gpu_freq(dev_priv->mem_freq,
  3099. dev_priv->rps.max_delay),
  3100. dev_priv->rps.max_delay);
  3101. dev_priv->rps.rpe_delay = valleyview_rps_rpe_freq(dev_priv);
  3102. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  3103. vlv_gpu_freq(dev_priv->mem_freq,
  3104. dev_priv->rps.rpe_delay),
  3105. dev_priv->rps.rpe_delay);
  3106. dev_priv->rps.min_delay = valleyview_rps_min_freq(dev_priv);
  3107. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  3108. vlv_gpu_freq(dev_priv->mem_freq,
  3109. dev_priv->rps.min_delay),
  3110. dev_priv->rps.min_delay);
  3111. DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
  3112. vlv_gpu_freq(dev_priv->mem_freq,
  3113. dev_priv->rps.rpe_delay),
  3114. dev_priv->rps.rpe_delay);
  3115. INIT_DELAYED_WORK(&dev_priv->rps.vlv_work, vlv_rps_timer_work);
  3116. valleyview_set_rps(dev_priv->dev, dev_priv->rps.rpe_delay);
  3117. gen6_enable_rps_interrupts(dev);
  3118. gen6_gt_force_wake_put(dev_priv);
  3119. }
  3120. void ironlake_teardown_rc6(struct drm_device *dev)
  3121. {
  3122. struct drm_i915_private *dev_priv = dev->dev_private;
  3123. if (dev_priv->ips.renderctx) {
  3124. i915_gem_object_unpin(dev_priv->ips.renderctx);
  3125. drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
  3126. dev_priv->ips.renderctx = NULL;
  3127. }
  3128. if (dev_priv->ips.pwrctx) {
  3129. i915_gem_object_unpin(dev_priv->ips.pwrctx);
  3130. drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
  3131. dev_priv->ips.pwrctx = NULL;
  3132. }
  3133. }
  3134. static void ironlake_disable_rc6(struct drm_device *dev)
  3135. {
  3136. struct drm_i915_private *dev_priv = dev->dev_private;
  3137. if (I915_READ(PWRCTXA)) {
  3138. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  3139. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  3140. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  3141. 50);
  3142. I915_WRITE(PWRCTXA, 0);
  3143. POSTING_READ(PWRCTXA);
  3144. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  3145. POSTING_READ(RSTDBYCTL);
  3146. }
  3147. }
  3148. static int ironlake_setup_rc6(struct drm_device *dev)
  3149. {
  3150. struct drm_i915_private *dev_priv = dev->dev_private;
  3151. if (dev_priv->ips.renderctx == NULL)
  3152. dev_priv->ips.renderctx = intel_alloc_context_page(dev);
  3153. if (!dev_priv->ips.renderctx)
  3154. return -ENOMEM;
  3155. if (dev_priv->ips.pwrctx == NULL)
  3156. dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
  3157. if (!dev_priv->ips.pwrctx) {
  3158. ironlake_teardown_rc6(dev);
  3159. return -ENOMEM;
  3160. }
  3161. return 0;
  3162. }
  3163. static void ironlake_enable_rc6(struct drm_device *dev)
  3164. {
  3165. struct drm_i915_private *dev_priv = dev->dev_private;
  3166. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  3167. bool was_interruptible;
  3168. int ret;
  3169. /* rc6 disabled by default due to repeated reports of hanging during
  3170. * boot and resume.
  3171. */
  3172. if (!intel_enable_rc6(dev))
  3173. return;
  3174. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  3175. ret = ironlake_setup_rc6(dev);
  3176. if (ret)
  3177. return;
  3178. was_interruptible = dev_priv->mm.interruptible;
  3179. dev_priv->mm.interruptible = false;
  3180. /*
  3181. * GPU can automatically power down the render unit if given a page
  3182. * to save state.
  3183. */
  3184. ret = intel_ring_begin(ring, 6);
  3185. if (ret) {
  3186. ironlake_teardown_rc6(dev);
  3187. dev_priv->mm.interruptible = was_interruptible;
  3188. return;
  3189. }
  3190. intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  3191. intel_ring_emit(ring, MI_SET_CONTEXT);
  3192. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
  3193. MI_MM_SPACE_GTT |
  3194. MI_SAVE_EXT_STATE_EN |
  3195. MI_RESTORE_EXT_STATE_EN |
  3196. MI_RESTORE_INHIBIT);
  3197. intel_ring_emit(ring, MI_SUSPEND_FLUSH);
  3198. intel_ring_emit(ring, MI_NOOP);
  3199. intel_ring_emit(ring, MI_FLUSH);
  3200. intel_ring_advance(ring);
  3201. /*
  3202. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  3203. * does an implicit flush, combined with MI_FLUSH above, it should be
  3204. * safe to assume that renderctx is valid
  3205. */
  3206. ret = intel_ring_idle(ring);
  3207. dev_priv->mm.interruptible = was_interruptible;
  3208. if (ret) {
  3209. DRM_ERROR("failed to enable ironlake power savings\n");
  3210. ironlake_teardown_rc6(dev);
  3211. return;
  3212. }
  3213. I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
  3214. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  3215. }
  3216. static unsigned long intel_pxfreq(u32 vidfreq)
  3217. {
  3218. unsigned long freq;
  3219. int div = (vidfreq & 0x3f0000) >> 16;
  3220. int post = (vidfreq & 0x3000) >> 12;
  3221. int pre = (vidfreq & 0x7);
  3222. if (!pre)
  3223. return 0;
  3224. freq = ((div * 133333) / ((1<<post) * pre));
  3225. return freq;
  3226. }
  3227. static const struct cparams {
  3228. u16 i;
  3229. u16 t;
  3230. u16 m;
  3231. u16 c;
  3232. } cparams[] = {
  3233. { 1, 1333, 301, 28664 },
  3234. { 1, 1066, 294, 24460 },
  3235. { 1, 800, 294, 25192 },
  3236. { 0, 1333, 276, 27605 },
  3237. { 0, 1066, 276, 27605 },
  3238. { 0, 800, 231, 23784 },
  3239. };
  3240. static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
  3241. {
  3242. u64 total_count, diff, ret;
  3243. u32 count1, count2, count3, m = 0, c = 0;
  3244. unsigned long now = jiffies_to_msecs(jiffies), diff1;
  3245. int i;
  3246. assert_spin_locked(&mchdev_lock);
  3247. diff1 = now - dev_priv->ips.last_time1;
  3248. /* Prevent division-by-zero if we are asking too fast.
  3249. * Also, we don't get interesting results if we are polling
  3250. * faster than once in 10ms, so just return the saved value
  3251. * in such cases.
  3252. */
  3253. if (diff1 <= 10)
  3254. return dev_priv->ips.chipset_power;
  3255. count1 = I915_READ(DMIEC);
  3256. count2 = I915_READ(DDREC);
  3257. count3 = I915_READ(CSIEC);
  3258. total_count = count1 + count2 + count3;
  3259. /* FIXME: handle per-counter overflow */
  3260. if (total_count < dev_priv->ips.last_count1) {
  3261. diff = ~0UL - dev_priv->ips.last_count1;
  3262. diff += total_count;
  3263. } else {
  3264. diff = total_count - dev_priv->ips.last_count1;
  3265. }
  3266. for (i = 0; i < ARRAY_SIZE(cparams); i++) {
  3267. if (cparams[i].i == dev_priv->ips.c_m &&
  3268. cparams[i].t == dev_priv->ips.r_t) {
  3269. m = cparams[i].m;
  3270. c = cparams[i].c;
  3271. break;
  3272. }
  3273. }
  3274. diff = div_u64(diff, diff1);
  3275. ret = ((m * diff) + c);
  3276. ret = div_u64(ret, 10);
  3277. dev_priv->ips.last_count1 = total_count;
  3278. dev_priv->ips.last_time1 = now;
  3279. dev_priv->ips.chipset_power = ret;
  3280. return ret;
  3281. }
  3282. unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
  3283. {
  3284. unsigned long val;
  3285. if (dev_priv->info->gen != 5)
  3286. return 0;
  3287. spin_lock_irq(&mchdev_lock);
  3288. val = __i915_chipset_val(dev_priv);
  3289. spin_unlock_irq(&mchdev_lock);
  3290. return val;
  3291. }
  3292. unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
  3293. {
  3294. unsigned long m, x, b;
  3295. u32 tsfs;
  3296. tsfs = I915_READ(TSFS);
  3297. m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
  3298. x = I915_READ8(TR1);
  3299. b = tsfs & TSFS_INTR_MASK;
  3300. return ((m * x) / 127) - b;
  3301. }
  3302. static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
  3303. {
  3304. static const struct v_table {
  3305. u16 vd; /* in .1 mil */
  3306. u16 vm; /* in .1 mil */
  3307. } v_table[] = {
  3308. { 0, 0, },
  3309. { 375, 0, },
  3310. { 500, 0, },
  3311. { 625, 0, },
  3312. { 750, 0, },
  3313. { 875, 0, },
  3314. { 1000, 0, },
  3315. { 1125, 0, },
  3316. { 4125, 3000, },
  3317. { 4125, 3000, },
  3318. { 4125, 3000, },
  3319. { 4125, 3000, },
  3320. { 4125, 3000, },
  3321. { 4125, 3000, },
  3322. { 4125, 3000, },
  3323. { 4125, 3000, },
  3324. { 4125, 3000, },
  3325. { 4125, 3000, },
  3326. { 4125, 3000, },
  3327. { 4125, 3000, },
  3328. { 4125, 3000, },
  3329. { 4125, 3000, },
  3330. { 4125, 3000, },
  3331. { 4125, 3000, },
  3332. { 4125, 3000, },
  3333. { 4125, 3000, },
  3334. { 4125, 3000, },
  3335. { 4125, 3000, },
  3336. { 4125, 3000, },
  3337. { 4125, 3000, },
  3338. { 4125, 3000, },
  3339. { 4125, 3000, },
  3340. { 4250, 3125, },
  3341. { 4375, 3250, },
  3342. { 4500, 3375, },
  3343. { 4625, 3500, },
  3344. { 4750, 3625, },
  3345. { 4875, 3750, },
  3346. { 5000, 3875, },
  3347. { 5125, 4000, },
  3348. { 5250, 4125, },
  3349. { 5375, 4250, },
  3350. { 5500, 4375, },
  3351. { 5625, 4500, },
  3352. { 5750, 4625, },
  3353. { 5875, 4750, },
  3354. { 6000, 4875, },
  3355. { 6125, 5000, },
  3356. { 6250, 5125, },
  3357. { 6375, 5250, },
  3358. { 6500, 5375, },
  3359. { 6625, 5500, },
  3360. { 6750, 5625, },
  3361. { 6875, 5750, },
  3362. { 7000, 5875, },
  3363. { 7125, 6000, },
  3364. { 7250, 6125, },
  3365. { 7375, 6250, },
  3366. { 7500, 6375, },
  3367. { 7625, 6500, },
  3368. { 7750, 6625, },
  3369. { 7875, 6750, },
  3370. { 8000, 6875, },
  3371. { 8125, 7000, },
  3372. { 8250, 7125, },
  3373. { 8375, 7250, },
  3374. { 8500, 7375, },
  3375. { 8625, 7500, },
  3376. { 8750, 7625, },
  3377. { 8875, 7750, },
  3378. { 9000, 7875, },
  3379. { 9125, 8000, },
  3380. { 9250, 8125, },
  3381. { 9375, 8250, },
  3382. { 9500, 8375, },
  3383. { 9625, 8500, },
  3384. { 9750, 8625, },
  3385. { 9875, 8750, },
  3386. { 10000, 8875, },
  3387. { 10125, 9000, },
  3388. { 10250, 9125, },
  3389. { 10375, 9250, },
  3390. { 10500, 9375, },
  3391. { 10625, 9500, },
  3392. { 10750, 9625, },
  3393. { 10875, 9750, },
  3394. { 11000, 9875, },
  3395. { 11125, 10000, },
  3396. { 11250, 10125, },
  3397. { 11375, 10250, },
  3398. { 11500, 10375, },
  3399. { 11625, 10500, },
  3400. { 11750, 10625, },
  3401. { 11875, 10750, },
  3402. { 12000, 10875, },
  3403. { 12125, 11000, },
  3404. { 12250, 11125, },
  3405. { 12375, 11250, },
  3406. { 12500, 11375, },
  3407. { 12625, 11500, },
  3408. { 12750, 11625, },
  3409. { 12875, 11750, },
  3410. { 13000, 11875, },
  3411. { 13125, 12000, },
  3412. { 13250, 12125, },
  3413. { 13375, 12250, },
  3414. { 13500, 12375, },
  3415. { 13625, 12500, },
  3416. { 13750, 12625, },
  3417. { 13875, 12750, },
  3418. { 14000, 12875, },
  3419. { 14125, 13000, },
  3420. { 14250, 13125, },
  3421. { 14375, 13250, },
  3422. { 14500, 13375, },
  3423. { 14625, 13500, },
  3424. { 14750, 13625, },
  3425. { 14875, 13750, },
  3426. { 15000, 13875, },
  3427. { 15125, 14000, },
  3428. { 15250, 14125, },
  3429. { 15375, 14250, },
  3430. { 15500, 14375, },
  3431. { 15625, 14500, },
  3432. { 15750, 14625, },
  3433. { 15875, 14750, },
  3434. { 16000, 14875, },
  3435. { 16125, 15000, },
  3436. };
  3437. if (dev_priv->info->is_mobile)
  3438. return v_table[pxvid].vm;
  3439. else
  3440. return v_table[pxvid].vd;
  3441. }
  3442. static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
  3443. {
  3444. struct timespec now, diff1;
  3445. u64 diff;
  3446. unsigned long diffms;
  3447. u32 count;
  3448. assert_spin_locked(&mchdev_lock);
  3449. getrawmonotonic(&now);
  3450. diff1 = timespec_sub(now, dev_priv->ips.last_time2);
  3451. /* Don't divide by 0 */
  3452. diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
  3453. if (!diffms)
  3454. return;
  3455. count = I915_READ(GFXEC);
  3456. if (count < dev_priv->ips.last_count2) {
  3457. diff = ~0UL - dev_priv->ips.last_count2;
  3458. diff += count;
  3459. } else {
  3460. diff = count - dev_priv->ips.last_count2;
  3461. }
  3462. dev_priv->ips.last_count2 = count;
  3463. dev_priv->ips.last_time2 = now;
  3464. /* More magic constants... */
  3465. diff = diff * 1181;
  3466. diff = div_u64(diff, diffms * 10);
  3467. dev_priv->ips.gfx_power = diff;
  3468. }
  3469. void i915_update_gfx_val(struct drm_i915_private *dev_priv)
  3470. {
  3471. if (dev_priv->info->gen != 5)
  3472. return;
  3473. spin_lock_irq(&mchdev_lock);
  3474. __i915_update_gfx_val(dev_priv);
  3475. spin_unlock_irq(&mchdev_lock);
  3476. }
  3477. static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
  3478. {
  3479. unsigned long t, corr, state1, corr2, state2;
  3480. u32 pxvid, ext_v;
  3481. assert_spin_locked(&mchdev_lock);
  3482. pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_delay * 4));
  3483. pxvid = (pxvid >> 24) & 0x7f;
  3484. ext_v = pvid_to_extvid(dev_priv, pxvid);
  3485. state1 = ext_v;
  3486. t = i915_mch_val(dev_priv);
  3487. /* Revel in the empirically derived constants */
  3488. /* Correction factor in 1/100000 units */
  3489. if (t > 80)
  3490. corr = ((t * 2349) + 135940);
  3491. else if (t >= 50)
  3492. corr = ((t * 964) + 29317);
  3493. else /* < 50 */
  3494. corr = ((t * 301) + 1004);
  3495. corr = corr * ((150142 * state1) / 10000 - 78642);
  3496. corr /= 100000;
  3497. corr2 = (corr * dev_priv->ips.corr);
  3498. state2 = (corr2 * state1) / 10000;
  3499. state2 /= 100; /* convert to mW */
  3500. __i915_update_gfx_val(dev_priv);
  3501. return dev_priv->ips.gfx_power + state2;
  3502. }
  3503. unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
  3504. {
  3505. unsigned long val;
  3506. if (dev_priv->info->gen != 5)
  3507. return 0;
  3508. spin_lock_irq(&mchdev_lock);
  3509. val = __i915_gfx_val(dev_priv);
  3510. spin_unlock_irq(&mchdev_lock);
  3511. return val;
  3512. }
  3513. /**
  3514. * i915_read_mch_val - return value for IPS use
  3515. *
  3516. * Calculate and return a value for the IPS driver to use when deciding whether
  3517. * we have thermal and power headroom to increase CPU or GPU power budget.
  3518. */
  3519. unsigned long i915_read_mch_val(void)
  3520. {
  3521. struct drm_i915_private *dev_priv;
  3522. unsigned long chipset_val, graphics_val, ret = 0;
  3523. spin_lock_irq(&mchdev_lock);
  3524. if (!i915_mch_dev)
  3525. goto out_unlock;
  3526. dev_priv = i915_mch_dev;
  3527. chipset_val = __i915_chipset_val(dev_priv);
  3528. graphics_val = __i915_gfx_val(dev_priv);
  3529. ret = chipset_val + graphics_val;
  3530. out_unlock:
  3531. spin_unlock_irq(&mchdev_lock);
  3532. return ret;
  3533. }
  3534. EXPORT_SYMBOL_GPL(i915_read_mch_val);
  3535. /**
  3536. * i915_gpu_raise - raise GPU frequency limit
  3537. *
  3538. * Raise the limit; IPS indicates we have thermal headroom.
  3539. */
  3540. bool i915_gpu_raise(void)
  3541. {
  3542. struct drm_i915_private *dev_priv;
  3543. bool ret = true;
  3544. spin_lock_irq(&mchdev_lock);
  3545. if (!i915_mch_dev) {
  3546. ret = false;
  3547. goto out_unlock;
  3548. }
  3549. dev_priv = i915_mch_dev;
  3550. if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
  3551. dev_priv->ips.max_delay--;
  3552. out_unlock:
  3553. spin_unlock_irq(&mchdev_lock);
  3554. return ret;
  3555. }
  3556. EXPORT_SYMBOL_GPL(i915_gpu_raise);
  3557. /**
  3558. * i915_gpu_lower - lower GPU frequency limit
  3559. *
  3560. * IPS indicates we're close to a thermal limit, so throttle back the GPU
  3561. * frequency maximum.
  3562. */
  3563. bool i915_gpu_lower(void)
  3564. {
  3565. struct drm_i915_private *dev_priv;
  3566. bool ret = true;
  3567. spin_lock_irq(&mchdev_lock);
  3568. if (!i915_mch_dev) {
  3569. ret = false;
  3570. goto out_unlock;
  3571. }
  3572. dev_priv = i915_mch_dev;
  3573. if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
  3574. dev_priv->ips.max_delay++;
  3575. out_unlock:
  3576. spin_unlock_irq(&mchdev_lock);
  3577. return ret;
  3578. }
  3579. EXPORT_SYMBOL_GPL(i915_gpu_lower);
  3580. /**
  3581. * i915_gpu_busy - indicate GPU business to IPS
  3582. *
  3583. * Tell the IPS driver whether or not the GPU is busy.
  3584. */
  3585. bool i915_gpu_busy(void)
  3586. {
  3587. struct drm_i915_private *dev_priv;
  3588. struct intel_ring_buffer *ring;
  3589. bool ret = false;
  3590. int i;
  3591. spin_lock_irq(&mchdev_lock);
  3592. if (!i915_mch_dev)
  3593. goto out_unlock;
  3594. dev_priv = i915_mch_dev;
  3595. for_each_ring(ring, dev_priv, i)
  3596. ret |= !list_empty(&ring->request_list);
  3597. out_unlock:
  3598. spin_unlock_irq(&mchdev_lock);
  3599. return ret;
  3600. }
  3601. EXPORT_SYMBOL_GPL(i915_gpu_busy);
  3602. /**
  3603. * i915_gpu_turbo_disable - disable graphics turbo
  3604. *
  3605. * Disable graphics turbo by resetting the max frequency and setting the
  3606. * current frequency to the default.
  3607. */
  3608. bool i915_gpu_turbo_disable(void)
  3609. {
  3610. struct drm_i915_private *dev_priv;
  3611. bool ret = true;
  3612. spin_lock_irq(&mchdev_lock);
  3613. if (!i915_mch_dev) {
  3614. ret = false;
  3615. goto out_unlock;
  3616. }
  3617. dev_priv = i915_mch_dev;
  3618. dev_priv->ips.max_delay = dev_priv->ips.fstart;
  3619. if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
  3620. ret = false;
  3621. out_unlock:
  3622. spin_unlock_irq(&mchdev_lock);
  3623. return ret;
  3624. }
  3625. EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
  3626. /**
  3627. * Tells the intel_ips driver that the i915 driver is now loaded, if
  3628. * IPS got loaded first.
  3629. *
  3630. * This awkward dance is so that neither module has to depend on the
  3631. * other in order for IPS to do the appropriate communication of
  3632. * GPU turbo limits to i915.
  3633. */
  3634. static void
  3635. ips_ping_for_i915_load(void)
  3636. {
  3637. void (*link)(void);
  3638. link = symbol_get(ips_link_to_i915_driver);
  3639. if (link) {
  3640. link();
  3641. symbol_put(ips_link_to_i915_driver);
  3642. }
  3643. }
  3644. void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
  3645. {
  3646. /* We only register the i915 ips part with intel-ips once everything is
  3647. * set up, to avoid intel-ips sneaking in and reading bogus values. */
  3648. spin_lock_irq(&mchdev_lock);
  3649. i915_mch_dev = dev_priv;
  3650. spin_unlock_irq(&mchdev_lock);
  3651. ips_ping_for_i915_load();
  3652. }
  3653. void intel_gpu_ips_teardown(void)
  3654. {
  3655. spin_lock_irq(&mchdev_lock);
  3656. i915_mch_dev = NULL;
  3657. spin_unlock_irq(&mchdev_lock);
  3658. }
  3659. static void intel_init_emon(struct drm_device *dev)
  3660. {
  3661. struct drm_i915_private *dev_priv = dev->dev_private;
  3662. u32 lcfuse;
  3663. u8 pxw[16];
  3664. int i;
  3665. /* Disable to program */
  3666. I915_WRITE(ECR, 0);
  3667. POSTING_READ(ECR);
  3668. /* Program energy weights for various events */
  3669. I915_WRITE(SDEW, 0x15040d00);
  3670. I915_WRITE(CSIEW0, 0x007f0000);
  3671. I915_WRITE(CSIEW1, 0x1e220004);
  3672. I915_WRITE(CSIEW2, 0x04000004);
  3673. for (i = 0; i < 5; i++)
  3674. I915_WRITE(PEW + (i * 4), 0);
  3675. for (i = 0; i < 3; i++)
  3676. I915_WRITE(DEW + (i * 4), 0);
  3677. /* Program P-state weights to account for frequency power adjustment */
  3678. for (i = 0; i < 16; i++) {
  3679. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  3680. unsigned long freq = intel_pxfreq(pxvidfreq);
  3681. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  3682. PXVFREQ_PX_SHIFT;
  3683. unsigned long val;
  3684. val = vid * vid;
  3685. val *= (freq / 1000);
  3686. val *= 255;
  3687. val /= (127*127*900);
  3688. if (val > 0xff)
  3689. DRM_ERROR("bad pxval: %ld\n", val);
  3690. pxw[i] = val;
  3691. }
  3692. /* Render standby states get 0 weight */
  3693. pxw[14] = 0;
  3694. pxw[15] = 0;
  3695. for (i = 0; i < 4; i++) {
  3696. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  3697. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  3698. I915_WRITE(PXW + (i * 4), val);
  3699. }
  3700. /* Adjust magic regs to magic values (more experimental results) */
  3701. I915_WRITE(OGW0, 0);
  3702. I915_WRITE(OGW1, 0);
  3703. I915_WRITE(EG0, 0x00007f00);
  3704. I915_WRITE(EG1, 0x0000000e);
  3705. I915_WRITE(EG2, 0x000e0000);
  3706. I915_WRITE(EG3, 0x68000300);
  3707. I915_WRITE(EG4, 0x42000000);
  3708. I915_WRITE(EG5, 0x00140031);
  3709. I915_WRITE(EG6, 0);
  3710. I915_WRITE(EG7, 0);
  3711. for (i = 0; i < 8; i++)
  3712. I915_WRITE(PXWL + (i * 4), 0);
  3713. /* Enable PMON + select events */
  3714. I915_WRITE(ECR, 0x80000019);
  3715. lcfuse = I915_READ(LCFUSE02);
  3716. dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
  3717. }
  3718. void intel_disable_gt_powersave(struct drm_device *dev)
  3719. {
  3720. struct drm_i915_private *dev_priv = dev->dev_private;
  3721. /* Interrupts should be disabled already to avoid re-arming. */
  3722. WARN_ON(dev->irq_enabled);
  3723. if (IS_IRONLAKE_M(dev)) {
  3724. ironlake_disable_drps(dev);
  3725. ironlake_disable_rc6(dev);
  3726. } else if (INTEL_INFO(dev)->gen >= 6) {
  3727. cancel_delayed_work_sync(&dev_priv->rps.delayed_resume_work);
  3728. cancel_work_sync(&dev_priv->rps.work);
  3729. if (IS_VALLEYVIEW(dev))
  3730. cancel_delayed_work_sync(&dev_priv->rps.vlv_work);
  3731. mutex_lock(&dev_priv->rps.hw_lock);
  3732. if (IS_VALLEYVIEW(dev))
  3733. valleyview_disable_rps(dev);
  3734. else
  3735. gen6_disable_rps(dev);
  3736. mutex_unlock(&dev_priv->rps.hw_lock);
  3737. }
  3738. }
  3739. static void intel_gen6_powersave_work(struct work_struct *work)
  3740. {
  3741. struct drm_i915_private *dev_priv =
  3742. container_of(work, struct drm_i915_private,
  3743. rps.delayed_resume_work.work);
  3744. struct drm_device *dev = dev_priv->dev;
  3745. mutex_lock(&dev_priv->rps.hw_lock);
  3746. if (IS_VALLEYVIEW(dev)) {
  3747. valleyview_enable_rps(dev);
  3748. } else {
  3749. gen6_enable_rps(dev);
  3750. gen6_update_ring_freq(dev);
  3751. }
  3752. mutex_unlock(&dev_priv->rps.hw_lock);
  3753. }
  3754. void intel_enable_gt_powersave(struct drm_device *dev)
  3755. {
  3756. struct drm_i915_private *dev_priv = dev->dev_private;
  3757. if (IS_IRONLAKE_M(dev)) {
  3758. ironlake_enable_drps(dev);
  3759. ironlake_enable_rc6(dev);
  3760. intel_init_emon(dev);
  3761. } else if (IS_GEN6(dev) || IS_GEN7(dev)) {
  3762. /*
  3763. * PCU communication is slow and this doesn't need to be
  3764. * done at any specific time, so do this out of our fast path
  3765. * to make resume and init faster.
  3766. */
  3767. schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
  3768. round_jiffies_up_relative(HZ));
  3769. }
  3770. }
  3771. static void ibx_init_clock_gating(struct drm_device *dev)
  3772. {
  3773. struct drm_i915_private *dev_priv = dev->dev_private;
  3774. /*
  3775. * On Ibex Peak and Cougar Point, we need to disable clock
  3776. * gating for the panel power sequencer or it will fail to
  3777. * start up when no ports are active.
  3778. */
  3779. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  3780. }
  3781. static void g4x_disable_trickle_feed(struct drm_device *dev)
  3782. {
  3783. struct drm_i915_private *dev_priv = dev->dev_private;
  3784. int pipe;
  3785. for_each_pipe(pipe) {
  3786. I915_WRITE(DSPCNTR(pipe),
  3787. I915_READ(DSPCNTR(pipe)) |
  3788. DISPPLANE_TRICKLE_FEED_DISABLE);
  3789. intel_flush_display_plane(dev_priv, pipe);
  3790. }
  3791. }
  3792. static void ironlake_init_clock_gating(struct drm_device *dev)
  3793. {
  3794. struct drm_i915_private *dev_priv = dev->dev_private;
  3795. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  3796. /*
  3797. * Required for FBC
  3798. * WaFbcDisableDpfcClockGating:ilk
  3799. */
  3800. dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
  3801. ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
  3802. ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
  3803. I915_WRITE(PCH_3DCGDIS0,
  3804. MARIUNIT_CLOCK_GATE_DISABLE |
  3805. SVSMUNIT_CLOCK_GATE_DISABLE);
  3806. I915_WRITE(PCH_3DCGDIS1,
  3807. VFMUNIT_CLOCK_GATE_DISABLE);
  3808. /*
  3809. * According to the spec the following bits should be set in
  3810. * order to enable memory self-refresh
  3811. * The bit 22/21 of 0x42004
  3812. * The bit 5 of 0x42020
  3813. * The bit 15 of 0x45000
  3814. */
  3815. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3816. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  3817. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  3818. dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
  3819. I915_WRITE(DISP_ARB_CTL,
  3820. (I915_READ(DISP_ARB_CTL) |
  3821. DISP_FBC_WM_DIS));
  3822. I915_WRITE(WM3_LP_ILK, 0);
  3823. I915_WRITE(WM2_LP_ILK, 0);
  3824. I915_WRITE(WM1_LP_ILK, 0);
  3825. /*
  3826. * Based on the document from hardware guys the following bits
  3827. * should be set unconditionally in order to enable FBC.
  3828. * The bit 22 of 0x42000
  3829. * The bit 22 of 0x42004
  3830. * The bit 7,8,9 of 0x42020.
  3831. */
  3832. if (IS_IRONLAKE_M(dev)) {
  3833. /* WaFbcAsynchFlipDisableFbcQueue:ilk */
  3834. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  3835. I915_READ(ILK_DISPLAY_CHICKEN1) |
  3836. ILK_FBCQ_DIS);
  3837. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3838. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3839. ILK_DPARB_GATE);
  3840. }
  3841. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  3842. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3843. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3844. ILK_ELPIN_409_SELECT);
  3845. I915_WRITE(_3D_CHICKEN2,
  3846. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  3847. _3D_CHICKEN2_WM_READ_PIPELINED);
  3848. /* WaDisableRenderCachePipelinedFlush:ilk */
  3849. I915_WRITE(CACHE_MODE_0,
  3850. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  3851. g4x_disable_trickle_feed(dev);
  3852. ibx_init_clock_gating(dev);
  3853. }
  3854. static void cpt_init_clock_gating(struct drm_device *dev)
  3855. {
  3856. struct drm_i915_private *dev_priv = dev->dev_private;
  3857. int pipe;
  3858. uint32_t val;
  3859. /*
  3860. * On Ibex Peak and Cougar Point, we need to disable clock
  3861. * gating for the panel power sequencer or it will fail to
  3862. * start up when no ports are active.
  3863. */
  3864. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  3865. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  3866. DPLS_EDP_PPS_FIX_DIS);
  3867. /* The below fixes the weird display corruption, a few pixels shifted
  3868. * downward, on (only) LVDS of some HP laptops with IVY.
  3869. */
  3870. for_each_pipe(pipe) {
  3871. val = I915_READ(TRANS_CHICKEN2(pipe));
  3872. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  3873. val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  3874. if (dev_priv->vbt.fdi_rx_polarity_inverted)
  3875. val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  3876. val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
  3877. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
  3878. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
  3879. I915_WRITE(TRANS_CHICKEN2(pipe), val);
  3880. }
  3881. /* WADP0ClockGatingDisable */
  3882. for_each_pipe(pipe) {
  3883. I915_WRITE(TRANS_CHICKEN1(pipe),
  3884. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  3885. }
  3886. }
  3887. static void gen6_check_mch_setup(struct drm_device *dev)
  3888. {
  3889. struct drm_i915_private *dev_priv = dev->dev_private;
  3890. uint32_t tmp;
  3891. tmp = I915_READ(MCH_SSKPD);
  3892. if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL) {
  3893. DRM_INFO("Wrong MCH_SSKPD value: 0x%08x\n", tmp);
  3894. DRM_INFO("This can cause pipe underruns and display issues.\n");
  3895. DRM_INFO("Please upgrade your BIOS to fix this.\n");
  3896. }
  3897. }
  3898. static void gen6_init_clock_gating(struct drm_device *dev)
  3899. {
  3900. struct drm_i915_private *dev_priv = dev->dev_private;
  3901. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  3902. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  3903. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3904. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3905. ILK_ELPIN_409_SELECT);
  3906. /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
  3907. I915_WRITE(_3D_CHICKEN,
  3908. _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
  3909. /* WaSetupGtModeTdRowDispatch:snb */
  3910. if (IS_SNB_GT1(dev))
  3911. I915_WRITE(GEN6_GT_MODE,
  3912. _MASKED_BIT_ENABLE(GEN6_TD_FOUR_ROW_DISPATCH_DISABLE));
  3913. I915_WRITE(WM3_LP_ILK, 0);
  3914. I915_WRITE(WM2_LP_ILK, 0);
  3915. I915_WRITE(WM1_LP_ILK, 0);
  3916. I915_WRITE(CACHE_MODE_0,
  3917. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  3918. I915_WRITE(GEN6_UCGCTL1,
  3919. I915_READ(GEN6_UCGCTL1) |
  3920. GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
  3921. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  3922. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  3923. * gating disable must be set. Failure to set it results in
  3924. * flickering pixels due to Z write ordering failures after
  3925. * some amount of runtime in the Mesa "fire" demo, and Unigine
  3926. * Sanctuary and Tropics, and apparently anything else with
  3927. * alpha test or pixel discard.
  3928. *
  3929. * According to the spec, bit 11 (RCCUNIT) must also be set,
  3930. * but we didn't debug actual testcases to find it out.
  3931. *
  3932. * Also apply WaDisableVDSUnitClockGating:snb and
  3933. * WaDisableRCPBUnitClockGating:snb.
  3934. */
  3935. I915_WRITE(GEN6_UCGCTL2,
  3936. GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
  3937. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  3938. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  3939. /* Bspec says we need to always set all mask bits. */
  3940. I915_WRITE(_3D_CHICKEN3, (0xFFFF << 16) |
  3941. _3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL);
  3942. /*
  3943. * According to the spec the following bits should be
  3944. * set in order to enable memory self-refresh and fbc:
  3945. * The bit21 and bit22 of 0x42000
  3946. * The bit21 and bit22 of 0x42004
  3947. * The bit5 and bit7 of 0x42020
  3948. * The bit14 of 0x70180
  3949. * The bit14 of 0x71180
  3950. *
  3951. * WaFbcAsynchFlipDisableFbcQueue:snb
  3952. */
  3953. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  3954. I915_READ(ILK_DISPLAY_CHICKEN1) |
  3955. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  3956. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  3957. I915_READ(ILK_DISPLAY_CHICKEN2) |
  3958. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  3959. I915_WRITE(ILK_DSPCLK_GATE_D,
  3960. I915_READ(ILK_DSPCLK_GATE_D) |
  3961. ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
  3962. ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
  3963. /* WaMbcDriverBootEnable:snb */
  3964. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  3965. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  3966. g4x_disable_trickle_feed(dev);
  3967. /* The default value should be 0x200 according to docs, but the two
  3968. * platforms I checked have a 0 for this. (Maybe BIOS overrides?) */
  3969. I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_DISABLE(0xffff));
  3970. I915_WRITE(GEN6_GT_MODE, _MASKED_BIT_ENABLE(GEN6_GT_MODE_HI));
  3971. cpt_init_clock_gating(dev);
  3972. gen6_check_mch_setup(dev);
  3973. }
  3974. static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
  3975. {
  3976. uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
  3977. reg &= ~GEN7_FF_SCHED_MASK;
  3978. reg |= GEN7_FF_TS_SCHED_HW;
  3979. reg |= GEN7_FF_VS_SCHED_HW;
  3980. reg |= GEN7_FF_DS_SCHED_HW;
  3981. if (IS_HASWELL(dev_priv->dev))
  3982. reg &= ~GEN7_FF_VS_REF_CNT_FFME;
  3983. I915_WRITE(GEN7_FF_THREAD_MODE, reg);
  3984. }
  3985. static void lpt_init_clock_gating(struct drm_device *dev)
  3986. {
  3987. struct drm_i915_private *dev_priv = dev->dev_private;
  3988. /*
  3989. * TODO: this bit should only be enabled when really needed, then
  3990. * disabled when not needed anymore in order to save power.
  3991. */
  3992. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
  3993. I915_WRITE(SOUTH_DSPCLK_GATE_D,
  3994. I915_READ(SOUTH_DSPCLK_GATE_D) |
  3995. PCH_LP_PARTITION_LEVEL_DISABLE);
  3996. /* WADPOClockGatingDisable:hsw */
  3997. I915_WRITE(_TRANSA_CHICKEN1,
  3998. I915_READ(_TRANSA_CHICKEN1) |
  3999. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  4000. }
  4001. static void lpt_suspend_hw(struct drm_device *dev)
  4002. {
  4003. struct drm_i915_private *dev_priv = dev->dev_private;
  4004. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  4005. uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
  4006. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  4007. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  4008. }
  4009. }
  4010. static void haswell_init_clock_gating(struct drm_device *dev)
  4011. {
  4012. struct drm_i915_private *dev_priv = dev->dev_private;
  4013. I915_WRITE(WM3_LP_ILK, 0);
  4014. I915_WRITE(WM2_LP_ILK, 0);
  4015. I915_WRITE(WM1_LP_ILK, 0);
  4016. /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  4017. * This implements the WaDisableRCZUnitClockGating:hsw workaround.
  4018. */
  4019. I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  4020. /* Apply the WaDisableRHWOOptimizationForRenderHang:hsw workaround. */
  4021. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  4022. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  4023. /* WaApplyL3ControlAndL3ChickenMode:hsw */
  4024. I915_WRITE(GEN7_L3CNTLREG1,
  4025. GEN7_WA_FOR_GEN7_L3_CONTROL);
  4026. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  4027. GEN7_WA_L3_CHICKEN_MODE);
  4028. /* This is required by WaCatErrorRejectionIssue:hsw */
  4029. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  4030. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  4031. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  4032. g4x_disable_trickle_feed(dev);
  4033. /* WaVSRefCountFullforceMissDisable:hsw */
  4034. gen7_setup_fixed_func_scheduler(dev_priv);
  4035. /* WaDisable4x2SubspanOptimization:hsw */
  4036. I915_WRITE(CACHE_MODE_1,
  4037. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  4038. /* WaMbcDriverBootEnable:hsw */
  4039. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  4040. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  4041. /* WaSwitchSolVfFArbitrationPriority:hsw */
  4042. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  4043. /* WaRsPkgCStateDisplayPMReq:hsw */
  4044. I915_WRITE(CHICKEN_PAR1_1,
  4045. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  4046. lpt_init_clock_gating(dev);
  4047. }
  4048. static void ivybridge_init_clock_gating(struct drm_device *dev)
  4049. {
  4050. struct drm_i915_private *dev_priv = dev->dev_private;
  4051. uint32_t snpcr;
  4052. I915_WRITE(WM3_LP_ILK, 0);
  4053. I915_WRITE(WM2_LP_ILK, 0);
  4054. I915_WRITE(WM1_LP_ILK, 0);
  4055. I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
  4056. /* WaDisableEarlyCull:ivb */
  4057. I915_WRITE(_3D_CHICKEN3,
  4058. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  4059. /* WaDisableBackToBackFlipFix:ivb */
  4060. I915_WRITE(IVB_CHICKEN3,
  4061. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  4062. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  4063. /* WaDisablePSDDualDispatchEnable:ivb */
  4064. if (IS_IVB_GT1(dev))
  4065. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  4066. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  4067. else
  4068. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1_GT2,
  4069. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  4070. /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
  4071. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  4072. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  4073. /* WaApplyL3ControlAndL3ChickenMode:ivb */
  4074. I915_WRITE(GEN7_L3CNTLREG1,
  4075. GEN7_WA_FOR_GEN7_L3_CONTROL);
  4076. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  4077. GEN7_WA_L3_CHICKEN_MODE);
  4078. if (IS_IVB_GT1(dev))
  4079. I915_WRITE(GEN7_ROW_CHICKEN2,
  4080. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  4081. else
  4082. I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
  4083. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  4084. /* WaForceL3Serialization:ivb */
  4085. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  4086. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  4087. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  4088. * gating disable must be set. Failure to set it results in
  4089. * flickering pixels due to Z write ordering failures after
  4090. * some amount of runtime in the Mesa "fire" demo, and Unigine
  4091. * Sanctuary and Tropics, and apparently anything else with
  4092. * alpha test or pixel discard.
  4093. *
  4094. * According to the spec, bit 11 (RCCUNIT) must also be set,
  4095. * but we didn't debug actual testcases to find it out.
  4096. *
  4097. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  4098. * This implements the WaDisableRCZUnitClockGating:ivb workaround.
  4099. */
  4100. I915_WRITE(GEN6_UCGCTL2,
  4101. GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
  4102. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  4103. /* This is required by WaCatErrorRejectionIssue:ivb */
  4104. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  4105. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  4106. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  4107. g4x_disable_trickle_feed(dev);
  4108. /* WaMbcDriverBootEnable:ivb */
  4109. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  4110. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  4111. /* WaVSRefCountFullforceMissDisable:ivb */
  4112. gen7_setup_fixed_func_scheduler(dev_priv);
  4113. /* WaDisable4x2SubspanOptimization:ivb */
  4114. I915_WRITE(CACHE_MODE_1,
  4115. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  4116. snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
  4117. snpcr &= ~GEN6_MBC_SNPCR_MASK;
  4118. snpcr |= GEN6_MBC_SNPCR_MED;
  4119. I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
  4120. if (!HAS_PCH_NOP(dev))
  4121. cpt_init_clock_gating(dev);
  4122. gen6_check_mch_setup(dev);
  4123. }
  4124. static void valleyview_init_clock_gating(struct drm_device *dev)
  4125. {
  4126. struct drm_i915_private *dev_priv = dev->dev_private;
  4127. I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
  4128. /* WaDisableEarlyCull:vlv */
  4129. I915_WRITE(_3D_CHICKEN3,
  4130. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  4131. /* WaDisableBackToBackFlipFix:vlv */
  4132. I915_WRITE(IVB_CHICKEN3,
  4133. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  4134. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  4135. /* WaDisablePSDDualDispatchEnable:vlv */
  4136. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  4137. _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
  4138. GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  4139. /* Apply the WaDisableRHWOOptimizationForRenderHang:vlv workaround. */
  4140. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  4141. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  4142. /* WaApplyL3ControlAndL3ChickenMode:vlv */
  4143. I915_WRITE(GEN7_L3CNTLREG1, I915_READ(GEN7_L3CNTLREG1) | GEN7_L3AGDIS);
  4144. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
  4145. /* WaForceL3Serialization:vlv */
  4146. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  4147. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  4148. /* WaDisableDopClockGating:vlv */
  4149. I915_WRITE(GEN7_ROW_CHICKEN2,
  4150. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  4151. /* This is required by WaCatErrorRejectionIssue:vlv */
  4152. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  4153. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  4154. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  4155. /* WaMbcDriverBootEnable:vlv */
  4156. I915_WRITE(GEN6_MBCTL, I915_READ(GEN6_MBCTL) |
  4157. GEN6_MBCTL_ENABLE_BOOT_FETCH);
  4158. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  4159. * gating disable must be set. Failure to set it results in
  4160. * flickering pixels due to Z write ordering failures after
  4161. * some amount of runtime in the Mesa "fire" demo, and Unigine
  4162. * Sanctuary and Tropics, and apparently anything else with
  4163. * alpha test or pixel discard.
  4164. *
  4165. * According to the spec, bit 11 (RCCUNIT) must also be set,
  4166. * but we didn't debug actual testcases to find it out.
  4167. *
  4168. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  4169. * This implements the WaDisableRCZUnitClockGating:vlv workaround.
  4170. *
  4171. * Also apply WaDisableVDSUnitClockGating:vlv and
  4172. * WaDisableRCPBUnitClockGating:vlv.
  4173. */
  4174. I915_WRITE(GEN6_UCGCTL2,
  4175. GEN7_VDSUNIT_CLOCK_GATE_DISABLE |
  4176. GEN7_TDLUNIT_CLOCK_GATE_DISABLE |
  4177. GEN6_RCZUNIT_CLOCK_GATE_DISABLE |
  4178. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  4179. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  4180. I915_WRITE(GEN7_UCGCTL4, GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
  4181. I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
  4182. I915_WRITE(CACHE_MODE_1,
  4183. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  4184. /*
  4185. * WaDisableVLVClockGating_VBIIssue:vlv
  4186. * Disable clock gating on th GCFG unit to prevent a delay
  4187. * in the reporting of vblank events.
  4188. */
  4189. I915_WRITE(VLV_GUNIT_CLOCK_GATE, 0xffffffff);
  4190. /* Conservative clock gating settings for now */
  4191. I915_WRITE(0x9400, 0xffffffff);
  4192. I915_WRITE(0x9404, 0xffffffff);
  4193. I915_WRITE(0x9408, 0xffffffff);
  4194. I915_WRITE(0x940c, 0xffffffff);
  4195. I915_WRITE(0x9410, 0xffffffff);
  4196. I915_WRITE(0x9414, 0xffffffff);
  4197. I915_WRITE(0x9418, 0xffffffff);
  4198. }
  4199. static void g4x_init_clock_gating(struct drm_device *dev)
  4200. {
  4201. struct drm_i915_private *dev_priv = dev->dev_private;
  4202. uint32_t dspclk_gate;
  4203. I915_WRITE(RENCLK_GATE_D1, 0);
  4204. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  4205. GS_UNIT_CLOCK_GATE_DISABLE |
  4206. CL_UNIT_CLOCK_GATE_DISABLE);
  4207. I915_WRITE(RAMCLK_GATE_D, 0);
  4208. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  4209. OVRUNIT_CLOCK_GATE_DISABLE |
  4210. OVCUNIT_CLOCK_GATE_DISABLE;
  4211. if (IS_GM45(dev))
  4212. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  4213. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  4214. /* WaDisableRenderCachePipelinedFlush */
  4215. I915_WRITE(CACHE_MODE_0,
  4216. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  4217. g4x_disable_trickle_feed(dev);
  4218. }
  4219. static void crestline_init_clock_gating(struct drm_device *dev)
  4220. {
  4221. struct drm_i915_private *dev_priv = dev->dev_private;
  4222. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  4223. I915_WRITE(RENCLK_GATE_D2, 0);
  4224. I915_WRITE(DSPCLK_GATE_D, 0);
  4225. I915_WRITE(RAMCLK_GATE_D, 0);
  4226. I915_WRITE16(DEUC, 0);
  4227. I915_WRITE(MI_ARB_STATE,
  4228. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  4229. }
  4230. static void broadwater_init_clock_gating(struct drm_device *dev)
  4231. {
  4232. struct drm_i915_private *dev_priv = dev->dev_private;
  4233. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  4234. I965_RCC_CLOCK_GATE_DISABLE |
  4235. I965_RCPB_CLOCK_GATE_DISABLE |
  4236. I965_ISC_CLOCK_GATE_DISABLE |
  4237. I965_FBC_CLOCK_GATE_DISABLE);
  4238. I915_WRITE(RENCLK_GATE_D2, 0);
  4239. I915_WRITE(MI_ARB_STATE,
  4240. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  4241. }
  4242. static void gen3_init_clock_gating(struct drm_device *dev)
  4243. {
  4244. struct drm_i915_private *dev_priv = dev->dev_private;
  4245. u32 dstate = I915_READ(D_STATE);
  4246. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  4247. DSTATE_DOT_CLOCK_GATING;
  4248. I915_WRITE(D_STATE, dstate);
  4249. if (IS_PINEVIEW(dev))
  4250. I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
  4251. /* IIR "flip pending" means done if this bit is set */
  4252. I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
  4253. }
  4254. static void i85x_init_clock_gating(struct drm_device *dev)
  4255. {
  4256. struct drm_i915_private *dev_priv = dev->dev_private;
  4257. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  4258. }
  4259. static void i830_init_clock_gating(struct drm_device *dev)
  4260. {
  4261. struct drm_i915_private *dev_priv = dev->dev_private;
  4262. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  4263. }
  4264. void intel_init_clock_gating(struct drm_device *dev)
  4265. {
  4266. struct drm_i915_private *dev_priv = dev->dev_private;
  4267. dev_priv->display.init_clock_gating(dev);
  4268. }
  4269. void intel_suspend_hw(struct drm_device *dev)
  4270. {
  4271. if (HAS_PCH_LPT(dev))
  4272. lpt_suspend_hw(dev);
  4273. }
  4274. /**
  4275. * We should only use the power well if we explicitly asked the hardware to
  4276. * enable it, so check if it's enabled and also check if we've requested it to
  4277. * be enabled.
  4278. */
  4279. bool intel_display_power_enabled(struct drm_device *dev,
  4280. enum intel_display_power_domain domain)
  4281. {
  4282. struct drm_i915_private *dev_priv = dev->dev_private;
  4283. if (!HAS_POWER_WELL(dev))
  4284. return true;
  4285. switch (domain) {
  4286. case POWER_DOMAIN_PIPE_A:
  4287. case POWER_DOMAIN_TRANSCODER_EDP:
  4288. return true;
  4289. case POWER_DOMAIN_PIPE_B:
  4290. case POWER_DOMAIN_PIPE_C:
  4291. case POWER_DOMAIN_PIPE_A_PANEL_FITTER:
  4292. case POWER_DOMAIN_PIPE_B_PANEL_FITTER:
  4293. case POWER_DOMAIN_PIPE_C_PANEL_FITTER:
  4294. case POWER_DOMAIN_TRANSCODER_A:
  4295. case POWER_DOMAIN_TRANSCODER_B:
  4296. case POWER_DOMAIN_TRANSCODER_C:
  4297. return I915_READ(HSW_PWR_WELL_DRIVER) ==
  4298. (HSW_PWR_WELL_ENABLE | HSW_PWR_WELL_STATE);
  4299. default:
  4300. BUG();
  4301. }
  4302. }
  4303. static void __intel_set_power_well(struct drm_device *dev, bool enable)
  4304. {
  4305. struct drm_i915_private *dev_priv = dev->dev_private;
  4306. bool is_enabled, enable_requested;
  4307. uint32_t tmp;
  4308. tmp = I915_READ(HSW_PWR_WELL_DRIVER);
  4309. is_enabled = tmp & HSW_PWR_WELL_STATE;
  4310. enable_requested = tmp & HSW_PWR_WELL_ENABLE;
  4311. if (enable) {
  4312. if (!enable_requested)
  4313. I915_WRITE(HSW_PWR_WELL_DRIVER, HSW_PWR_WELL_ENABLE);
  4314. if (!is_enabled) {
  4315. DRM_DEBUG_KMS("Enabling power well\n");
  4316. if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
  4317. HSW_PWR_WELL_STATE), 20))
  4318. DRM_ERROR("Timeout enabling power well\n");
  4319. }
  4320. } else {
  4321. if (enable_requested) {
  4322. I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
  4323. DRM_DEBUG_KMS("Requesting to disable the power well\n");
  4324. }
  4325. }
  4326. }
  4327. static struct i915_power_well *hsw_pwr;
  4328. /* Display audio driver power well request */
  4329. void i915_request_power_well(void)
  4330. {
  4331. if (WARN_ON(!hsw_pwr))
  4332. return;
  4333. spin_lock_irq(&hsw_pwr->lock);
  4334. if (!hsw_pwr->count++ &&
  4335. !hsw_pwr->i915_request)
  4336. __intel_set_power_well(hsw_pwr->device, true);
  4337. spin_unlock_irq(&hsw_pwr->lock);
  4338. }
  4339. EXPORT_SYMBOL_GPL(i915_request_power_well);
  4340. /* Display audio driver power well release */
  4341. void i915_release_power_well(void)
  4342. {
  4343. if (WARN_ON(!hsw_pwr))
  4344. return;
  4345. spin_lock_irq(&hsw_pwr->lock);
  4346. WARN_ON(!hsw_pwr->count);
  4347. if (!--hsw_pwr->count &&
  4348. !hsw_pwr->i915_request)
  4349. __intel_set_power_well(hsw_pwr->device, false);
  4350. spin_unlock_irq(&hsw_pwr->lock);
  4351. }
  4352. EXPORT_SYMBOL_GPL(i915_release_power_well);
  4353. int i915_init_power_well(struct drm_device *dev)
  4354. {
  4355. struct drm_i915_private *dev_priv = dev->dev_private;
  4356. hsw_pwr = &dev_priv->power_well;
  4357. hsw_pwr->device = dev;
  4358. spin_lock_init(&hsw_pwr->lock);
  4359. hsw_pwr->count = 0;
  4360. return 0;
  4361. }
  4362. void i915_remove_power_well(struct drm_device *dev)
  4363. {
  4364. hsw_pwr = NULL;
  4365. }
  4366. void intel_set_power_well(struct drm_device *dev, bool enable)
  4367. {
  4368. struct drm_i915_private *dev_priv = dev->dev_private;
  4369. struct i915_power_well *power_well = &dev_priv->power_well;
  4370. if (!HAS_POWER_WELL(dev))
  4371. return;
  4372. if (!i915_disable_power_well && !enable)
  4373. return;
  4374. spin_lock_irq(&power_well->lock);
  4375. power_well->i915_request = enable;
  4376. /* only reject "disable" power well request */
  4377. if (power_well->count && !enable) {
  4378. spin_unlock_irq(&power_well->lock);
  4379. return;
  4380. }
  4381. __intel_set_power_well(dev, enable);
  4382. spin_unlock_irq(&power_well->lock);
  4383. }
  4384. /*
  4385. * Starting with Haswell, we have a "Power Down Well" that can be turned off
  4386. * when not needed anymore. We have 4 registers that can request the power well
  4387. * to be enabled, and it will only be disabled if none of the registers is
  4388. * requesting it to be enabled.
  4389. */
  4390. void intel_init_power_well(struct drm_device *dev)
  4391. {
  4392. struct drm_i915_private *dev_priv = dev->dev_private;
  4393. if (!HAS_POWER_WELL(dev))
  4394. return;
  4395. /* For now, we need the power well to be always enabled. */
  4396. intel_set_power_well(dev, true);
  4397. /* We're taking over the BIOS, so clear any requests made by it since
  4398. * the driver is in charge now. */
  4399. if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE)
  4400. I915_WRITE(HSW_PWR_WELL_BIOS, 0);
  4401. }
  4402. /* Set up chip specific power management-related functions */
  4403. void intel_init_pm(struct drm_device *dev)
  4404. {
  4405. struct drm_i915_private *dev_priv = dev->dev_private;
  4406. if (I915_HAS_FBC(dev)) {
  4407. if (HAS_PCH_SPLIT(dev)) {
  4408. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  4409. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  4410. dev_priv->display.enable_fbc =
  4411. gen7_enable_fbc;
  4412. else
  4413. dev_priv->display.enable_fbc =
  4414. ironlake_enable_fbc;
  4415. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  4416. } else if (IS_GM45(dev)) {
  4417. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  4418. dev_priv->display.enable_fbc = g4x_enable_fbc;
  4419. dev_priv->display.disable_fbc = g4x_disable_fbc;
  4420. } else if (IS_CRESTLINE(dev)) {
  4421. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  4422. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  4423. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  4424. }
  4425. /* 855GM needs testing */
  4426. }
  4427. /* For cxsr */
  4428. if (IS_PINEVIEW(dev))
  4429. i915_pineview_get_mem_freq(dev);
  4430. else if (IS_GEN5(dev))
  4431. i915_ironlake_get_mem_freq(dev);
  4432. /* For FIFO watermark updates */
  4433. if (HAS_PCH_SPLIT(dev)) {
  4434. intel_setup_wm_latency(dev);
  4435. if (IS_GEN5(dev)) {
  4436. if (dev_priv->wm.pri_latency[1] &&
  4437. dev_priv->wm.spr_latency[1] &&
  4438. dev_priv->wm.cur_latency[1])
  4439. dev_priv->display.update_wm = ironlake_update_wm;
  4440. else {
  4441. DRM_DEBUG_KMS("Failed to get proper latency. "
  4442. "Disable CxSR\n");
  4443. dev_priv->display.update_wm = NULL;
  4444. }
  4445. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  4446. } else if (IS_GEN6(dev)) {
  4447. if (dev_priv->wm.pri_latency[0] &&
  4448. dev_priv->wm.spr_latency[0] &&
  4449. dev_priv->wm.cur_latency[0]) {
  4450. dev_priv->display.update_wm = sandybridge_update_wm;
  4451. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  4452. } else {
  4453. DRM_DEBUG_KMS("Failed to read display plane latency. "
  4454. "Disable CxSR\n");
  4455. dev_priv->display.update_wm = NULL;
  4456. }
  4457. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  4458. } else if (IS_IVYBRIDGE(dev)) {
  4459. if (dev_priv->wm.pri_latency[0] &&
  4460. dev_priv->wm.spr_latency[0] &&
  4461. dev_priv->wm.cur_latency[0]) {
  4462. dev_priv->display.update_wm = ivybridge_update_wm;
  4463. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  4464. } else {
  4465. DRM_DEBUG_KMS("Failed to read display plane latency. "
  4466. "Disable CxSR\n");
  4467. dev_priv->display.update_wm = NULL;
  4468. }
  4469. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  4470. } else if (IS_HASWELL(dev)) {
  4471. if (dev_priv->wm.pri_latency[0] &&
  4472. dev_priv->wm.spr_latency[0] &&
  4473. dev_priv->wm.cur_latency[0]) {
  4474. dev_priv->display.update_wm = haswell_update_wm;
  4475. dev_priv->display.update_sprite_wm =
  4476. haswell_update_sprite_wm;
  4477. } else {
  4478. DRM_DEBUG_KMS("Failed to read display plane latency. "
  4479. "Disable CxSR\n");
  4480. dev_priv->display.update_wm = NULL;
  4481. }
  4482. dev_priv->display.init_clock_gating = haswell_init_clock_gating;
  4483. } else
  4484. dev_priv->display.update_wm = NULL;
  4485. } else if (IS_VALLEYVIEW(dev)) {
  4486. dev_priv->display.update_wm = valleyview_update_wm;
  4487. dev_priv->display.init_clock_gating =
  4488. valleyview_init_clock_gating;
  4489. } else if (IS_PINEVIEW(dev)) {
  4490. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  4491. dev_priv->is_ddr3,
  4492. dev_priv->fsb_freq,
  4493. dev_priv->mem_freq)) {
  4494. DRM_INFO("failed to find known CxSR latency "
  4495. "(found ddr%s fsb freq %d, mem freq %d), "
  4496. "disabling CxSR\n",
  4497. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  4498. dev_priv->fsb_freq, dev_priv->mem_freq);
  4499. /* Disable CxSR and never update its watermark again */
  4500. pineview_disable_cxsr(dev);
  4501. dev_priv->display.update_wm = NULL;
  4502. } else
  4503. dev_priv->display.update_wm = pineview_update_wm;
  4504. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  4505. } else if (IS_G4X(dev)) {
  4506. dev_priv->display.update_wm = g4x_update_wm;
  4507. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  4508. } else if (IS_GEN4(dev)) {
  4509. dev_priv->display.update_wm = i965_update_wm;
  4510. if (IS_CRESTLINE(dev))
  4511. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  4512. else if (IS_BROADWATER(dev))
  4513. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  4514. } else if (IS_GEN3(dev)) {
  4515. dev_priv->display.update_wm = i9xx_update_wm;
  4516. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  4517. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  4518. } else if (IS_I865G(dev)) {
  4519. dev_priv->display.update_wm = i830_update_wm;
  4520. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  4521. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  4522. } else if (IS_I85X(dev)) {
  4523. dev_priv->display.update_wm = i9xx_update_wm;
  4524. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  4525. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  4526. } else {
  4527. dev_priv->display.update_wm = i830_update_wm;
  4528. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  4529. if (IS_845G(dev))
  4530. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  4531. else
  4532. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  4533. }
  4534. }
  4535. int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
  4536. {
  4537. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  4538. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  4539. DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
  4540. return -EAGAIN;
  4541. }
  4542. I915_WRITE(GEN6_PCODE_DATA, *val);
  4543. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  4544. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  4545. 500)) {
  4546. DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
  4547. return -ETIMEDOUT;
  4548. }
  4549. *val = I915_READ(GEN6_PCODE_DATA);
  4550. I915_WRITE(GEN6_PCODE_DATA, 0);
  4551. return 0;
  4552. }
  4553. int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
  4554. {
  4555. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  4556. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  4557. DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
  4558. return -EAGAIN;
  4559. }
  4560. I915_WRITE(GEN6_PCODE_DATA, val);
  4561. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  4562. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  4563. 500)) {
  4564. DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
  4565. return -ETIMEDOUT;
  4566. }
  4567. I915_WRITE(GEN6_PCODE_DATA, 0);
  4568. return 0;
  4569. }
  4570. int vlv_gpu_freq(int ddr_freq, int val)
  4571. {
  4572. int mult, base;
  4573. switch (ddr_freq) {
  4574. case 800:
  4575. mult = 20;
  4576. base = 120;
  4577. break;
  4578. case 1066:
  4579. mult = 22;
  4580. base = 133;
  4581. break;
  4582. case 1333:
  4583. mult = 21;
  4584. base = 125;
  4585. break;
  4586. default:
  4587. return -1;
  4588. }
  4589. return ((val - 0xbd) * mult) + base;
  4590. }
  4591. int vlv_freq_opcode(int ddr_freq, int val)
  4592. {
  4593. int mult, base;
  4594. switch (ddr_freq) {
  4595. case 800:
  4596. mult = 20;
  4597. base = 120;
  4598. break;
  4599. case 1066:
  4600. mult = 22;
  4601. base = 133;
  4602. break;
  4603. case 1333:
  4604. mult = 21;
  4605. base = 125;
  4606. break;
  4607. default:
  4608. return -1;
  4609. }
  4610. val /= mult;
  4611. val -= base / mult;
  4612. val += 0xbd;
  4613. if (val > 0xea)
  4614. val = 0xea;
  4615. return val;
  4616. }
  4617. void intel_pm_init(struct drm_device *dev)
  4618. {
  4619. struct drm_i915_private *dev_priv = dev->dev_private;
  4620. INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
  4621. intel_gen6_powersave_work);
  4622. }