intel_pm.c 155 KB

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