intel_pm.c 153 KB

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