intel_pm.c 157 KB

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