intel_pm.c 148 KB

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