intel_pm.c 152 KB

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