intel_pm.c 125 KB

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